1 /*
2 * PSA hashing layer on top of Mbed TLS software crypto
3 */
4 /*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 */
8
9 #include "common.h"
10
11 #if defined(MBEDTLS_PSA_CRYPTO_C)
12
13 #include <psa/crypto.h>
14
15 #include "psa_util_internal.h"
16
17 /* The following includes are needed for MBEDTLS_ERR_XXX macros */
18 #include <mbedtls/error.h>
19 #if defined(MBEDTLS_MD_LIGHT)
20 #include <mbedtls/md.h>
21 #endif
22 #if defined(MBEDTLS_LMS_C)
23 #include <mbedtls/lms.h>
24 #endif
25 #if defined(MBEDTLS_SSL_TLS_C) && \
26 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
27 #include <mbedtls/ssl.h>
28 #endif
29 #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
30 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
31 #include <mbedtls/rsa.h>
32 #endif
33 #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
34 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
35 #include <mbedtls/ecp.h>
36 #endif
37 #if defined(MBEDTLS_PK_C)
38 #include <mbedtls/pk.h>
39 #endif
40
41 /* PSA_SUCCESS is kept at the top of each error table since
42 * it's the most common status when everything functions properly. */
43 #if defined(MBEDTLS_MD_LIGHT)
44 const mbedtls_error_pair_t psa_to_md_errors[] =
45 {
46 { PSA_SUCCESS, 0 },
47 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE },
48 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_MD_BAD_INPUT_DATA },
49 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_MD_ALLOC_FAILED }
50 };
51 #endif
52 #if defined(MBEDTLS_LMS_C)
53 const mbedtls_error_pair_t psa_to_lms_errors[] =
54 {
55 { PSA_SUCCESS, 0 },
56 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL },
57 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_LMS_BAD_INPUT_DATA }
58 };
59 #endif
60 #if defined(MBEDTLS_SSL_TLS_C) && \
61 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
62 const mbedtls_error_pair_t psa_to_ssl_errors[] =
63 {
64 { PSA_SUCCESS, 0 },
65 { PSA_ERROR_INSUFFICIENT_MEMORY, MBEDTLS_ERR_SSL_ALLOC_FAILED },
66 { PSA_ERROR_NOT_SUPPORTED, MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE },
67 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_SSL_INVALID_MAC },
68 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_SSL_BAD_INPUT_DATA },
69 { PSA_ERROR_BAD_STATE, MBEDTLS_ERR_SSL_INTERNAL_ERROR },
70 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL }
71 };
72 #endif
73
74 #if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
75 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
76 const mbedtls_error_pair_t psa_to_pk_rsa_errors[] =
77 {
78 { PSA_SUCCESS, 0 },
79 { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
80 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
81 { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_RSA_BAD_INPUT_DATA },
82 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE },
83 { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_RSA_RNG_FAILED },
84 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_RSA_VERIFY_FAILED },
85 { PSA_ERROR_INVALID_PADDING, MBEDTLS_ERR_RSA_INVALID_PADDING }
86 };
87 #endif
88
89 #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
90 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
91 const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[] =
92 {
93 { PSA_SUCCESS, 0 },
94 { PSA_ERROR_NOT_PERMITTED, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
95 { PSA_ERROR_INVALID_ARGUMENT, MBEDTLS_ERR_ECP_BAD_INPUT_DATA },
96 { PSA_ERROR_INVALID_HANDLE, MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE },
97 { PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL },
98 { PSA_ERROR_INSUFFICIENT_ENTROPY, MBEDTLS_ERR_ECP_RANDOM_FAILED },
99 { PSA_ERROR_INVALID_SIGNATURE, MBEDTLS_ERR_ECP_VERIFY_FAILED }
100 };
101 #endif
102
psa_generic_status_to_mbedtls(psa_status_t status)103 int psa_generic_status_to_mbedtls(psa_status_t status)
104 {
105 switch (status) {
106 case PSA_SUCCESS:
107 return 0;
108 case PSA_ERROR_NOT_SUPPORTED:
109 return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
110 case PSA_ERROR_CORRUPTION_DETECTED:
111 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
112 case PSA_ERROR_COMMUNICATION_FAILURE:
113 case PSA_ERROR_HARDWARE_FAILURE:
114 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
115 case PSA_ERROR_NOT_PERMITTED:
116 default:
117 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
118 }
119 }
120
psa_status_to_mbedtls(psa_status_t status,const mbedtls_error_pair_t * local_translations,size_t local_errors_num,int (* fallback_f)(psa_status_t))121 int psa_status_to_mbedtls(psa_status_t status,
122 const mbedtls_error_pair_t *local_translations,
123 size_t local_errors_num,
124 int (*fallback_f)(psa_status_t))
125 {
126 for (size_t i = 0; i < local_errors_num; i++) {
127 if (status == local_translations[i].psa_status) {
128 return local_translations[i].mbedtls_error;
129 }
130 }
131 return fallback_f(status);
132 }
133
134 #if defined(MBEDTLS_PK_C)
psa_pk_status_to_mbedtls(psa_status_t status)135 int psa_pk_status_to_mbedtls(psa_status_t status)
136 {
137 switch (status) {
138 case PSA_ERROR_INVALID_HANDLE:
139 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
140 case PSA_ERROR_BUFFER_TOO_SMALL:
141 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
142 case PSA_ERROR_NOT_SUPPORTED:
143 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
144 case PSA_ERROR_INVALID_ARGUMENT:
145 return MBEDTLS_ERR_PK_INVALID_ALG;
146 case PSA_ERROR_INSUFFICIENT_MEMORY:
147 return MBEDTLS_ERR_PK_ALLOC_FAILED;
148 case PSA_ERROR_BAD_STATE:
149 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
150 case PSA_ERROR_DATA_CORRUPT:
151 case PSA_ERROR_DATA_INVALID:
152 case PSA_ERROR_STORAGE_FAILURE:
153 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
154 default:
155 return psa_generic_status_to_mbedtls(status);
156 }
157 }
158 #endif /* MBEDTLS_PK_C */
159
160 /****************************************************************/
161 /* Key management */
162 /****************************************************************/
163
164 #if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,size_t * bits)165 psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
166 size_t *bits)
167 {
168 switch (grpid) {
169 #if defined(MBEDTLS_ECP_HAVE_SECP192R1)
170 case MBEDTLS_ECP_DP_SECP192R1:
171 *bits = 192;
172 return PSA_ECC_FAMILY_SECP_R1;
173 #endif
174 #if defined(MBEDTLS_ECP_HAVE_SECP224R1)
175 case MBEDTLS_ECP_DP_SECP224R1:
176 *bits = 224;
177 return PSA_ECC_FAMILY_SECP_R1;
178 #endif
179 #if defined(MBEDTLS_ECP_HAVE_SECP256R1)
180 case MBEDTLS_ECP_DP_SECP256R1:
181 *bits = 256;
182 return PSA_ECC_FAMILY_SECP_R1;
183 #endif
184 #if defined(MBEDTLS_ECP_HAVE_SECP384R1)
185 case MBEDTLS_ECP_DP_SECP384R1:
186 *bits = 384;
187 return PSA_ECC_FAMILY_SECP_R1;
188 #endif
189 #if defined(MBEDTLS_ECP_HAVE_SECP521R1)
190 case MBEDTLS_ECP_DP_SECP521R1:
191 *bits = 521;
192 return PSA_ECC_FAMILY_SECP_R1;
193 #endif
194 #if defined(MBEDTLS_ECP_HAVE_BP256R1)
195 case MBEDTLS_ECP_DP_BP256R1:
196 *bits = 256;
197 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
198 #endif
199 #if defined(MBEDTLS_ECP_HAVE_BP384R1)
200 case MBEDTLS_ECP_DP_BP384R1:
201 *bits = 384;
202 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
203 #endif
204 #if defined(MBEDTLS_ECP_HAVE_BP512R1)
205 case MBEDTLS_ECP_DP_BP512R1:
206 *bits = 512;
207 return PSA_ECC_FAMILY_BRAINPOOL_P_R1;
208 #endif
209 #if defined(MBEDTLS_ECP_HAVE_CURVE25519)
210 case MBEDTLS_ECP_DP_CURVE25519:
211 *bits = 255;
212 return PSA_ECC_FAMILY_MONTGOMERY;
213 #endif
214 #if defined(MBEDTLS_ECP_HAVE_SECP192K1)
215 case MBEDTLS_ECP_DP_SECP192K1:
216 *bits = 192;
217 return PSA_ECC_FAMILY_SECP_K1;
218 #endif
219 #if defined(MBEDTLS_ECP_HAVE_SECP224K1)
220 case MBEDTLS_ECP_DP_SECP224K1:
221 *bits = 224;
222 return PSA_ECC_FAMILY_SECP_K1;
223 #endif
224 #if defined(MBEDTLS_ECP_HAVE_SECP256K1)
225 case MBEDTLS_ECP_DP_SECP256K1:
226 *bits = 256;
227 return PSA_ECC_FAMILY_SECP_K1;
228 #endif
229 #if defined(MBEDTLS_ECP_HAVE_CURVE448)
230 case MBEDTLS_ECP_DP_CURVE448:
231 *bits = 448;
232 return PSA_ECC_FAMILY_MONTGOMERY;
233 #endif
234 default:
235 *bits = 0;
236 return 0;
237 }
238 }
239
mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,size_t bits,int bits_is_sloppy)240 mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
241 size_t bits,
242 int bits_is_sloppy)
243 {
244 switch (curve) {
245 case PSA_ECC_FAMILY_SECP_R1:
246 switch (bits) {
247 #if defined(PSA_WANT_ECC_SECP_R1_192)
248 case 192:
249 return MBEDTLS_ECP_DP_SECP192R1;
250 #endif
251 #if defined(PSA_WANT_ECC_SECP_R1_224)
252 case 224:
253 return MBEDTLS_ECP_DP_SECP224R1;
254 #endif
255 #if defined(PSA_WANT_ECC_SECP_R1_256)
256 case 256:
257 return MBEDTLS_ECP_DP_SECP256R1;
258 #endif
259 #if defined(PSA_WANT_ECC_SECP_R1_384)
260 case 384:
261 return MBEDTLS_ECP_DP_SECP384R1;
262 #endif
263 #if defined(PSA_WANT_ECC_SECP_R1_521)
264 case 521:
265 return MBEDTLS_ECP_DP_SECP521R1;
266 case 528:
267 if (bits_is_sloppy) {
268 return MBEDTLS_ECP_DP_SECP521R1;
269 }
270 break;
271 #endif
272 }
273 break;
274
275 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
276 switch (bits) {
277 #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
278 case 256:
279 return MBEDTLS_ECP_DP_BP256R1;
280 #endif
281 #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
282 case 384:
283 return MBEDTLS_ECP_DP_BP384R1;
284 #endif
285 #if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
286 case 512:
287 return MBEDTLS_ECP_DP_BP512R1;
288 #endif
289 }
290 break;
291
292 case PSA_ECC_FAMILY_MONTGOMERY:
293 switch (bits) {
294 #if defined(PSA_WANT_ECC_MONTGOMERY_255)
295 case 255:
296 return MBEDTLS_ECP_DP_CURVE25519;
297 case 256:
298 if (bits_is_sloppy) {
299 return MBEDTLS_ECP_DP_CURVE25519;
300 }
301 break;
302 #endif
303 #if defined(PSA_WANT_ECC_MONTGOMERY_448)
304 case 448:
305 return MBEDTLS_ECP_DP_CURVE448;
306 #endif
307 }
308 break;
309
310 case PSA_ECC_FAMILY_SECP_K1:
311 switch (bits) {
312 #if defined(PSA_WANT_ECC_SECP_K1_192)
313 case 192:
314 return MBEDTLS_ECP_DP_SECP192K1;
315 #endif
316 #if defined(PSA_WANT_ECC_SECP_K1_224)
317 case 224:
318 return MBEDTLS_ECP_DP_SECP224K1;
319 #endif
320 #if defined(PSA_WANT_ECC_SECP_K1_256)
321 case 256:
322 return MBEDTLS_ECP_DP_SECP256K1;
323 #endif
324 }
325 break;
326 }
327
328 (void) bits_is_sloppy;
329 return MBEDTLS_ECP_DP_NONE;
330 }
331 #endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
332
333 #endif /* MBEDTLS_PSA_CRYPTO_C */
334