/src/duckdb/third_party/mbedtls/library/pk_wrap.cpp
Line | Count | Source |
1 | | /* |
2 | | * Public Key abstraction layer: wrapper functions |
3 | | * |
4 | | * Copyright The Mbed TLS Contributors |
5 | | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
6 | | */ |
7 | | |
8 | | #include "common.h" |
9 | | |
10 | | #include "mbedtls/platform_util.h" |
11 | | |
12 | | #if defined(MBEDTLS_PK_C) |
13 | | #include "pk_wrap.h" |
14 | | #include "pk_internal.h" |
15 | | #include "mbedtls/error.h" |
16 | | #include "mbedtls/psa_util.h" |
17 | | |
18 | | /* Even if RSA not activated, for the sake of RSA-alt */ |
19 | | #include "mbedtls/rsa.h" |
20 | | |
21 | | #if defined(MBEDTLS_ECP_C) |
22 | | #include "mbedtls/ecp.h" |
23 | | #endif |
24 | | |
25 | | #if defined(MBEDTLS_ECDSA_C) |
26 | | #include "mbedtls/ecdsa.h" |
27 | | #endif |
28 | | |
29 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
30 | | #include "psa_util_internal.h" |
31 | | #include "psa/crypto.h" |
32 | | |
33 | | #if defined(MBEDTLS_RSA_C) |
34 | | #include "pkwrite.h" |
35 | | #include "rsa_internal.h" |
36 | | #include "constant_time_internal.h" |
37 | | #endif |
38 | | |
39 | | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
40 | | #include "mbedtls/asn1write.h" |
41 | | #include "mbedtls/asn1.h" |
42 | | #endif |
43 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
44 | | |
45 | | #include "mbedtls/platform.h" |
46 | | |
47 | | #include <limits.h> |
48 | | #include <stdint.h> |
49 | | #include <string.h> |
50 | | |
51 | | #if defined(MBEDTLS_RSA_C) |
52 | | static int rsa_can_do(mbedtls_pk_type_t type) |
53 | 0 | { |
54 | 0 | return type == MBEDTLS_PK_RSA || |
55 | 0 | type == MBEDTLS_PK_RSASSA_PSS; |
56 | 0 | } |
57 | | |
58 | | static size_t rsa_get_bitlen(mbedtls_pk_context *pk) |
59 | 0 | { |
60 | 0 | const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx; |
61 | 0 | return mbedtls_rsa_get_bitlen(rsa); |
62 | 0 | } |
63 | | |
64 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
65 | | static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
66 | | const unsigned char *hash, size_t hash_len, |
67 | | const unsigned char *sig, size_t sig_len) |
68 | | { |
69 | | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
70 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
71 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
72 | | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
73 | | psa_status_t status; |
74 | | int key_len; |
75 | | unsigned char buf[PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)]; |
76 | | unsigned char *p = buf + sizeof(buf); |
77 | | psa_algorithm_t psa_alg_md; |
78 | | size_t rsa_len = mbedtls_rsa_get_len(rsa); |
79 | | |
80 | | #if SIZE_MAX > UINT_MAX |
81 | | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
82 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
83 | | } |
84 | | #endif |
85 | | |
86 | | if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) { |
87 | | psa_alg_md = PSA_ALG_RSA_PSS(mbedtls_md_psa_alg_from_type(md_alg)); |
88 | | } else { |
89 | | psa_alg_md = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg)); |
90 | | } |
91 | | |
92 | | if (sig_len < rsa_len) { |
93 | | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
94 | | } |
95 | | |
96 | | key_len = mbedtls_rsa_write_pubkey(rsa, buf, &p); |
97 | | if (key_len <= 0) { |
98 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
99 | | } |
100 | | |
101 | | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
102 | | psa_set_key_algorithm(&attributes, psa_alg_md); |
103 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
104 | | |
105 | | status = psa_import_key(&attributes, |
106 | | buf + sizeof(buf) - key_len, key_len, |
107 | | &key_id); |
108 | | if (status != PSA_SUCCESS) { |
109 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
110 | | goto cleanup; |
111 | | } |
112 | | |
113 | | status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len, |
114 | | sig, sig_len); |
115 | | if (status != PSA_SUCCESS) { |
116 | | ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
117 | | goto cleanup; |
118 | | } |
119 | | ret = 0; |
120 | | |
121 | | cleanup: |
122 | | status = psa_destroy_key(key_id); |
123 | | if (ret == 0 && status != PSA_SUCCESS) { |
124 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
125 | | } |
126 | | |
127 | | return ret; |
128 | | } |
129 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
130 | | static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
131 | | const unsigned char *hash, size_t hash_len, |
132 | | const unsigned char *sig, size_t sig_len) |
133 | 0 | { |
134 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
135 | 0 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
136 | 0 | size_t rsa_len = mbedtls_rsa_get_len(rsa); |
137 | |
|
138 | 0 | #if SIZE_MAX > UINT_MAX |
139 | 0 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
140 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
141 | 0 | } |
142 | 0 | #endif |
143 | | |
144 | 0 | if (sig_len < rsa_len) { |
145 | 0 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
146 | 0 | } |
147 | | |
148 | 0 | if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg, |
149 | 0 | (unsigned int) hash_len, |
150 | 0 | hash, sig)) != 0) { |
151 | 0 | return ret; |
152 | 0 | } |
153 | | |
154 | | /* The buffer contains a valid signature followed by extra data. |
155 | | * We have a special error code for that so that so that callers can |
156 | | * use mbedtls_pk_verify() to check "Does the buffer start with a |
157 | | * valid signature?" and not just "Does the buffer contain a valid |
158 | | * signature?". */ |
159 | 0 | if (sig_len > rsa_len) { |
160 | 0 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
161 | 0 | } |
162 | | |
163 | 0 | return 0; |
164 | 0 | } |
165 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
166 | | |
167 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
168 | | int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg, |
169 | | mbedtls_rsa_context *rsa_ctx, |
170 | | const unsigned char *hash, size_t hash_len, |
171 | | unsigned char *sig, size_t sig_size, |
172 | | size_t *sig_len) |
173 | | { |
174 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
175 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
176 | | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
177 | | psa_status_t status; |
178 | | int key_len; |
179 | | unsigned char *buf = NULL; |
180 | | unsigned char *p; |
181 | | |
182 | | buf = mbedtls_calloc(1, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES); |
183 | | if (buf == NULL) { |
184 | | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
185 | | } |
186 | | p = buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES; |
187 | | |
188 | | *sig_len = mbedtls_rsa_get_len(rsa_ctx); |
189 | | if (sig_size < *sig_len) { |
190 | | mbedtls_free(buf); |
191 | | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
192 | | } |
193 | | |
194 | | key_len = mbedtls_rsa_write_key(rsa_ctx, buf, &p); |
195 | | if (key_len <= 0) { |
196 | | mbedtls_free(buf); |
197 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
198 | | } |
199 | | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
200 | | psa_set_key_algorithm(&attributes, alg); |
201 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR); |
202 | | |
203 | | status = psa_import_key(&attributes, |
204 | | buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES - key_len, key_len, |
205 | | &key_id); |
206 | | if (status != PSA_SUCCESS) { |
207 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
208 | | goto cleanup; |
209 | | } |
210 | | status = psa_sign_hash(key_id, alg, hash, hash_len, |
211 | | sig, sig_size, sig_len); |
212 | | if (status != PSA_SUCCESS) { |
213 | | ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
214 | | goto cleanup; |
215 | | } |
216 | | |
217 | | ret = 0; |
218 | | |
219 | | cleanup: |
220 | | mbedtls_free(buf); |
221 | | status = psa_destroy_key(key_id); |
222 | | if (ret == 0 && status != PSA_SUCCESS) { |
223 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
224 | | } |
225 | | return ret; |
226 | | } |
227 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
228 | | |
229 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
230 | | static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
231 | | const unsigned char *hash, size_t hash_len, |
232 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
233 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
234 | | { |
235 | | ((void) f_rng); |
236 | | ((void) p_rng); |
237 | | |
238 | | psa_algorithm_t psa_md_alg; |
239 | | psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg); |
240 | | if (psa_md_alg == 0) { |
241 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
242 | | } |
243 | | psa_algorithm_t psa_alg; |
244 | | if (mbedtls_rsa_get_padding_mode(mbedtls_pk_rsa(*pk)) == MBEDTLS_RSA_PKCS_V21) { |
245 | | psa_alg = PSA_ALG_RSA_PSS(psa_md_alg); |
246 | | } else { |
247 | | psa_alg = PSA_ALG_RSA_PKCS1V15_SIGN(psa_md_alg); |
248 | | } |
249 | | |
250 | | return mbedtls_pk_psa_rsa_sign_ext(psa_alg, pk->pk_ctx, hash, hash_len, |
251 | | sig, sig_size, sig_len); |
252 | | } |
253 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
254 | | static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
255 | | const unsigned char *hash, size_t hash_len, |
256 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
257 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
258 | 0 | { |
259 | 0 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
260 | |
|
261 | 0 | #if SIZE_MAX > UINT_MAX |
262 | 0 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
263 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
264 | 0 | } |
265 | 0 | #endif |
266 | | |
267 | 0 | *sig_len = mbedtls_rsa_get_len(rsa); |
268 | 0 | if (sig_size < *sig_len) { |
269 | 0 | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
270 | 0 | } |
271 | | |
272 | 0 | return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng, |
273 | 0 | md_alg, (unsigned int) hash_len, |
274 | 0 | hash, sig); |
275 | 0 | } |
276 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
277 | | |
278 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
279 | | static int rsa_decrypt_wrap(mbedtls_pk_context *pk, |
280 | | const unsigned char *input, size_t ilen, |
281 | | unsigned char *output, size_t *olen, size_t osize, |
282 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
283 | | { |
284 | | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
285 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
286 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
287 | | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
288 | | psa_algorithm_t psa_md_alg, decrypt_alg; |
289 | | psa_status_t status; |
290 | | int key_len; |
291 | | ((void) f_rng); |
292 | | ((void) p_rng); |
293 | | |
294 | | if (ilen != mbedtls_rsa_get_len(rsa)) { |
295 | | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
296 | | } |
297 | | |
298 | | const size_t key_bits = mbedtls_pk_get_bitlen(pk); |
299 | | /* mbedtls_rsa_write_key() uses the same format as PSA export, which |
300 | | * actually calls it under the hood, so we can use the PSA size macro. */ |
301 | | const size_t buf_size = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(key_bits); |
302 | | unsigned char *buf = mbedtls_calloc(1, buf_size); |
303 | | |
304 | | unsigned char *p = buf + buf_size; |
305 | | key_len = mbedtls_rsa_write_key(rsa, buf, &p); |
306 | | if (key_len <= 0) { |
307 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
308 | | } |
309 | | |
310 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR); |
311 | | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT); |
312 | | if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) { |
313 | | psa_md_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa)); |
314 | | decrypt_alg = PSA_ALG_RSA_OAEP(psa_md_alg); |
315 | | } else { |
316 | | decrypt_alg = PSA_ALG_RSA_PKCS1V15_CRYPT; |
317 | | } |
318 | | psa_set_key_algorithm(&attributes, decrypt_alg); |
319 | | |
320 | | status = psa_import_key(&attributes, |
321 | | buf + buf_size - key_len, key_len, |
322 | | &key_id); |
323 | | if (status != PSA_SUCCESS) { |
324 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
325 | | goto cleanup; |
326 | | } |
327 | | |
328 | | status = psa_asymmetric_decrypt(key_id, decrypt_alg, |
329 | | input, ilen, |
330 | | NULL, 0, |
331 | | output, osize, olen); |
332 | | |
333 | | #if defined(MBEDTLS_PKCS1_V15) |
334 | | /* Translate error codes from PSA to legacy |
335 | | * Success vs INVALID_PADDING vs BUFFER_TOO_SMALL is sensitive |
336 | | * (padding oracle attack), so we take care to translate that |
337 | | * part in constant time. |
338 | | */ |
339 | | int problem; |
340 | | status = mbedtls_rsa_decrypt_decompose_ret( |
341 | | PSA_ERROR_INVALID_PADDING, MBEDTLS_ERR_RSA_INVALID_PADDING, |
342 | | PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE, |
343 | | status, &problem); |
344 | | ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
345 | | ret |= problem; |
346 | | #else |
347 | | ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
348 | | #endif |
349 | | |
350 | | cleanup: |
351 | | mbedtls_zeroize_and_free(buf, buf_size); |
352 | | status = psa_destroy_key(key_id); |
353 | | /* Don't branch on ret as it is a sensitive value |
354 | | * (it reveals whether padding was valid). |
355 | | * Instead branch on status. That means when both ret and |
356 | | * status were errors, we'll return the unlock status while we would |
357 | | * normally return the first error, but that's better than leaking info. */ |
358 | | return (status != PSA_SUCCESS) ? PSA_PK_TO_MBEDTLS_ERR(status) : ret; |
359 | | } |
360 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
361 | | static int rsa_decrypt_wrap(mbedtls_pk_context *pk, |
362 | | const unsigned char *input, size_t ilen, |
363 | | unsigned char *output, size_t *olen, size_t osize, |
364 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
365 | 0 | { |
366 | 0 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
367 | |
|
368 | 0 | if (ilen != mbedtls_rsa_get_len(rsa)) { |
369 | 0 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
370 | 0 | } |
371 | | |
372 | 0 | return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng, |
373 | 0 | olen, input, output, osize); |
374 | 0 | } |
375 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
376 | | |
377 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
378 | | static int rsa_encrypt_wrap(mbedtls_pk_context *pk, |
379 | | const unsigned char *input, size_t ilen, |
380 | | unsigned char *output, size_t *olen, size_t osize, |
381 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
382 | | { |
383 | | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
384 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
385 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
386 | | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
387 | | psa_algorithm_t psa_md_alg, psa_encrypt_alg; |
388 | | psa_status_t status; |
389 | | int key_len; |
390 | | unsigned char buf[PSA_KEY_EXPORT_RSA_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)]; |
391 | | unsigned char *p = buf + sizeof(buf); |
392 | | |
393 | | ((void) f_rng); |
394 | | ((void) p_rng); |
395 | | |
396 | | if (mbedtls_rsa_get_len(rsa) > osize) { |
397 | | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
398 | | } |
399 | | |
400 | | key_len = mbedtls_rsa_write_pubkey(rsa, buf, &p); |
401 | | if (key_len <= 0) { |
402 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
403 | | } |
404 | | |
405 | | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT); |
406 | | if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) { |
407 | | psa_md_alg = mbedtls_md_psa_alg_from_type((mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa)); |
408 | | psa_encrypt_alg = PSA_ALG_RSA_OAEP(psa_md_alg); |
409 | | } else { |
410 | | psa_encrypt_alg = PSA_ALG_RSA_PKCS1V15_CRYPT; |
411 | | } |
412 | | psa_set_key_algorithm(&attributes, psa_encrypt_alg); |
413 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY); |
414 | | |
415 | | status = psa_import_key(&attributes, |
416 | | buf + sizeof(buf) - key_len, key_len, |
417 | | &key_id); |
418 | | if (status != PSA_SUCCESS) { |
419 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
420 | | goto cleanup; |
421 | | } |
422 | | |
423 | | status = psa_asymmetric_encrypt(key_id, psa_encrypt_alg, |
424 | | input, ilen, |
425 | | NULL, 0, |
426 | | output, osize, olen); |
427 | | if (status != PSA_SUCCESS) { |
428 | | ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
429 | | goto cleanup; |
430 | | } |
431 | | |
432 | | ret = 0; |
433 | | |
434 | | cleanup: |
435 | | status = psa_destroy_key(key_id); |
436 | | if (ret == 0 && status != PSA_SUCCESS) { |
437 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
438 | | } |
439 | | |
440 | | return ret; |
441 | | } |
442 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
443 | | static int rsa_encrypt_wrap(mbedtls_pk_context *pk, |
444 | | const unsigned char *input, size_t ilen, |
445 | | unsigned char *output, size_t *olen, size_t osize, |
446 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
447 | 0 | { |
448 | 0 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
449 | 0 | *olen = mbedtls_rsa_get_len(rsa); |
450 | |
|
451 | 0 | if (*olen > osize) { |
452 | 0 | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
453 | 0 | } |
454 | | |
455 | 0 | return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng, |
456 | 0 | ilen, input, output); |
457 | 0 | } |
458 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
459 | | |
460 | | static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv, |
461 | | int (*f_rng)(void *, unsigned char *, size_t), |
462 | | void *p_rng) |
463 | 0 | { |
464 | 0 | (void) f_rng; |
465 | 0 | (void) p_rng; |
466 | 0 | return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx, |
467 | 0 | (const mbedtls_rsa_context *) prv->pk_ctx); |
468 | 0 | } |
469 | | |
470 | | static void *rsa_alloc_wrap(void) |
471 | 0 | { |
472 | 0 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context)); |
473 | |
|
474 | 0 | if (ctx != NULL) { |
475 | 0 | mbedtls_rsa_init((mbedtls_rsa_context *) ctx); |
476 | 0 | } |
477 | |
|
478 | 0 | return ctx; |
479 | 0 | } |
480 | | |
481 | | static void rsa_free_wrap(void *ctx) |
482 | 0 | { |
483 | 0 | mbedtls_rsa_free((mbedtls_rsa_context *) ctx); |
484 | 0 | mbedtls_free(ctx); |
485 | 0 | } |
486 | | |
487 | | static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items) |
488 | 0 | { |
489 | | #if defined(MBEDTLS_RSA_ALT) |
490 | | /* Not supported */ |
491 | | (void) pk; |
492 | | (void) items; |
493 | | #else |
494 | 0 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx; |
495 | |
|
496 | 0 | items->type = MBEDTLS_PK_DEBUG_MPI; |
497 | 0 | items->name = "rsa.N"; |
498 | 0 | items->value = &(rsa->N); |
499 | |
|
500 | 0 | items++; |
501 | |
|
502 | 0 | items->type = MBEDTLS_PK_DEBUG_MPI; |
503 | 0 | items->name = "rsa.E"; |
504 | 0 | items->value = &(rsa->E); |
505 | 0 | #endif |
506 | 0 | } |
507 | | |
508 | | const mbedtls_pk_info_t mbedtls_rsa_info = { |
509 | | /* .type = */ MBEDTLS_PK_RSA, |
510 | | /* .name = */ "RSA", |
511 | | /* .get_bitlen = */ rsa_get_bitlen, |
512 | | /* .can_do = */ rsa_can_do, |
513 | | /* .verify_func = */ rsa_verify_wrap, |
514 | | /* .sign_func = */ rsa_sign_wrap, |
515 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
516 | | /* .verify_rs_func = */ NULL, |
517 | | /* .sign_rs_func = */ NULL, |
518 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
519 | | /* .decrypt_func = */ rsa_decrypt_wrap, |
520 | | /* .encrypt_func = */ rsa_encrypt_wrap, |
521 | | /* .check_pair_func = */ rsa_check_pair_wrap, |
522 | | /* .ctx_alloc_func = */ rsa_alloc_wrap, |
523 | | /* .ctx_free_func = */ rsa_free_wrap, |
524 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
525 | | /* .rs_alloc_func = */ NULL, |
526 | | /* .rs_free_func = */ NULL, |
527 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
528 | | /* .debug_func = */ rsa_debug, |
529 | | }; |
530 | | #endif /* MBEDTLS_RSA_C */ |
531 | | |
532 | | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
533 | | /* |
534 | | * Generic EC key |
535 | | */ |
536 | | static int eckey_can_do(mbedtls_pk_type_t type) |
537 | | { |
538 | | return type == MBEDTLS_PK_ECKEY || |
539 | | type == MBEDTLS_PK_ECKEY_DH || |
540 | | type == MBEDTLS_PK_ECDSA; |
541 | | } |
542 | | |
543 | | static size_t eckey_get_bitlen(mbedtls_pk_context *pk) |
544 | | { |
545 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
546 | | return pk->ec_bits; |
547 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
548 | | mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx; |
549 | | return ecp->grp.pbits; |
550 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
551 | | } |
552 | | |
553 | | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
554 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
555 | | /* Common helper for ECDSA verify using PSA functions. */ |
556 | | static int ecdsa_verify_psa(unsigned char *key, size_t key_len, |
557 | | psa_ecc_family_t curve, size_t curve_bits, |
558 | | const unsigned char *hash, size_t hash_len, |
559 | | const unsigned char *sig, size_t sig_len) |
560 | | { |
561 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
562 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
563 | | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
564 | | psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY; |
565 | | size_t signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits); |
566 | | size_t converted_sig_len; |
567 | | unsigned char extracted_sig[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE]; |
568 | | unsigned char *p; |
569 | | psa_status_t status; |
570 | | |
571 | | if (curve == 0) { |
572 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
573 | | } |
574 | | |
575 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve)); |
576 | | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
577 | | psa_set_key_algorithm(&attributes, psa_sig_md); |
578 | | |
579 | | status = psa_import_key(&attributes, key, key_len, &key_id); |
580 | | if (status != PSA_SUCCESS) { |
581 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
582 | | goto cleanup; |
583 | | } |
584 | | |
585 | | if (signature_len > sizeof(extracted_sig)) { |
586 | | ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
587 | | goto cleanup; |
588 | | } |
589 | | |
590 | | p = (unsigned char *) sig; |
591 | | ret = mbedtls_ecdsa_der_to_raw(curve_bits, p, sig_len, extracted_sig, |
592 | | sizeof(extracted_sig), &converted_sig_len); |
593 | | if (ret != 0) { |
594 | | goto cleanup; |
595 | | } |
596 | | |
597 | | if (converted_sig_len != signature_len) { |
598 | | ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
599 | | goto cleanup; |
600 | | } |
601 | | |
602 | | status = psa_verify_hash(key_id, psa_sig_md, hash, hash_len, |
603 | | extracted_sig, signature_len); |
604 | | if (status != PSA_SUCCESS) { |
605 | | ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
606 | | goto cleanup; |
607 | | } |
608 | | |
609 | | ret = 0; |
610 | | |
611 | | cleanup: |
612 | | status = psa_destroy_key(key_id); |
613 | | if (ret == 0 && status != PSA_SUCCESS) { |
614 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
615 | | } |
616 | | |
617 | | return ret; |
618 | | } |
619 | | |
620 | | static int ecdsa_opaque_verify_wrap(mbedtls_pk_context *pk, |
621 | | mbedtls_md_type_t md_alg, |
622 | | const unsigned char *hash, size_t hash_len, |
623 | | const unsigned char *sig, size_t sig_len) |
624 | | { |
625 | | (void) md_alg; |
626 | | unsigned char key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; |
627 | | size_t key_len; |
628 | | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
629 | | psa_ecc_family_t curve; |
630 | | size_t curve_bits; |
631 | | psa_status_t status; |
632 | | |
633 | | status = psa_get_key_attributes(pk->priv_id, &key_attr); |
634 | | if (status != PSA_SUCCESS) { |
635 | | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
636 | | } |
637 | | curve = PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(&key_attr)); |
638 | | curve_bits = psa_get_key_bits(&key_attr); |
639 | | psa_reset_key_attributes(&key_attr); |
640 | | |
641 | | status = psa_export_public_key(pk->priv_id, key, sizeof(key), &key_len); |
642 | | if (status != PSA_SUCCESS) { |
643 | | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
644 | | } |
645 | | |
646 | | return ecdsa_verify_psa(key, key_len, curve, curve_bits, |
647 | | hash, hash_len, sig, sig_len); |
648 | | } |
649 | | |
650 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
651 | | static int ecdsa_verify_wrap(mbedtls_pk_context *pk, |
652 | | mbedtls_md_type_t md_alg, |
653 | | const unsigned char *hash, size_t hash_len, |
654 | | const unsigned char *sig, size_t sig_len) |
655 | | { |
656 | | (void) md_alg; |
657 | | psa_ecc_family_t curve = pk->ec_family; |
658 | | size_t curve_bits = pk->ec_bits; |
659 | | |
660 | | return ecdsa_verify_psa(pk->pub_raw, pk->pub_raw_len, curve, curve_bits, |
661 | | hash, hash_len, sig, sig_len); |
662 | | } |
663 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
664 | | static int ecdsa_verify_wrap(mbedtls_pk_context *pk, |
665 | | mbedtls_md_type_t md_alg, |
666 | | const unsigned char *hash, size_t hash_len, |
667 | | const unsigned char *sig, size_t sig_len) |
668 | | { |
669 | | (void) md_alg; |
670 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
671 | | mbedtls_ecp_keypair *ctx = pk->pk_ctx; |
672 | | unsigned char key[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
673 | | size_t key_len; |
674 | | size_t curve_bits; |
675 | | psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits); |
676 | | |
677 | | ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q, |
678 | | MBEDTLS_ECP_PF_UNCOMPRESSED, |
679 | | &key_len, key, sizeof(key)); |
680 | | if (ret != 0) { |
681 | | return ret; |
682 | | } |
683 | | |
684 | | return ecdsa_verify_psa(key, key_len, curve, curve_bits, |
685 | | hash, hash_len, sig, sig_len); |
686 | | } |
687 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
688 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
689 | | static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
690 | | const unsigned char *hash, size_t hash_len, |
691 | | const unsigned char *sig, size_t sig_len) |
692 | | { |
693 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
694 | | ((void) md_alg); |
695 | | |
696 | | ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx, |
697 | | hash, hash_len, sig, sig_len); |
698 | | |
699 | | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
700 | | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
701 | | } |
702 | | |
703 | | return ret; |
704 | | } |
705 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
706 | | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
707 | | |
708 | | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
709 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
710 | | /* Common helper for ECDSA sign using PSA functions. |
711 | | * Instead of extracting key's properties in order to check which kind of ECDSA |
712 | | * signature it supports, we try both deterministic and non-deterministic. |
713 | | */ |
714 | | static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg, |
715 | | const unsigned char *hash, size_t hash_len, |
716 | | unsigned char *sig, size_t sig_size, size_t *sig_len) |
717 | | { |
718 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
719 | | psa_status_t status; |
720 | | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
721 | | size_t key_bits = 0; |
722 | | |
723 | | status = psa_get_key_attributes(key_id, &key_attr); |
724 | | if (status != PSA_SUCCESS) { |
725 | | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
726 | | } |
727 | | key_bits = psa_get_key_bits(&key_attr); |
728 | | psa_reset_key_attributes(&key_attr); |
729 | | |
730 | | status = psa_sign_hash(key_id, |
731 | | PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)), |
732 | | hash, hash_len, sig, sig_size, sig_len); |
733 | | if (status == PSA_SUCCESS) { |
734 | | goto done; |
735 | | } else if (status != PSA_ERROR_NOT_PERMITTED) { |
736 | | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
737 | | } |
738 | | |
739 | | status = psa_sign_hash(key_id, |
740 | | PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)), |
741 | | hash, hash_len, sig, sig_size, sig_len); |
742 | | if (status != PSA_SUCCESS) { |
743 | | return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status); |
744 | | } |
745 | | |
746 | | done: |
747 | | ret = mbedtls_ecdsa_raw_to_der(key_bits, sig, *sig_len, sig, sig_size, sig_len); |
748 | | |
749 | | return ret; |
750 | | } |
751 | | |
752 | | static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk, |
753 | | mbedtls_md_type_t md_alg, |
754 | | const unsigned char *hash, size_t hash_len, |
755 | | unsigned char *sig, size_t sig_size, |
756 | | size_t *sig_len, |
757 | | int (*f_rng)(void *, unsigned char *, size_t), |
758 | | void *p_rng) |
759 | | { |
760 | | ((void) f_rng); |
761 | | ((void) p_rng); |
762 | | |
763 | | return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size, |
764 | | sig_len); |
765 | | } |
766 | | |
767 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
768 | | /* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up |
769 | | * using the same function. */ |
770 | | #define ecdsa_sign_wrap ecdsa_opaque_sign_wrap |
771 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
772 | | static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
773 | | const unsigned char *hash, size_t hash_len, |
774 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
775 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
776 | | { |
777 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
778 | | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
779 | | psa_status_t status; |
780 | | mbedtls_ecp_keypair *ctx = pk->pk_ctx; |
781 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
782 | | unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH]; |
783 | | size_t curve_bits; |
784 | | psa_ecc_family_t curve = |
785 | | mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits); |
786 | | size_t key_len = PSA_BITS_TO_BYTES(curve_bits); |
787 | | psa_algorithm_t psa_hash = mbedtls_md_psa_alg_from_type(md_alg); |
788 | | psa_algorithm_t psa_sig_md = MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(psa_hash); |
789 | | ((void) f_rng); |
790 | | ((void) p_rng); |
791 | | |
792 | | if (curve == 0) { |
793 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
794 | | } |
795 | | |
796 | | if (key_len > sizeof(buf)) { |
797 | | return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
798 | | } |
799 | | ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len); |
800 | | if (ret != 0) { |
801 | | goto cleanup; |
802 | | } |
803 | | |
804 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve)); |
805 | | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH); |
806 | | psa_set_key_algorithm(&attributes, psa_sig_md); |
807 | | |
808 | | status = psa_import_key(&attributes, buf, key_len, &key_id); |
809 | | if (status != PSA_SUCCESS) { |
810 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
811 | | goto cleanup; |
812 | | } |
813 | | |
814 | | ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len); |
815 | | |
816 | | cleanup: |
817 | | mbedtls_platform_zeroize(buf, sizeof(buf)); |
818 | | status = psa_destroy_key(key_id); |
819 | | if (ret == 0 && status != PSA_SUCCESS) { |
820 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
821 | | } |
822 | | |
823 | | return ret; |
824 | | } |
825 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
826 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
827 | | static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
828 | | const unsigned char *hash, size_t hash_len, |
829 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
830 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
831 | | { |
832 | | return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx, |
833 | | md_alg, hash, hash_len, |
834 | | sig, sig_size, sig_len, |
835 | | f_rng, p_rng); |
836 | | } |
837 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
838 | | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
839 | | |
840 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
841 | | /* Forward declarations */ |
842 | | static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
843 | | const unsigned char *hash, size_t hash_len, |
844 | | const unsigned char *sig, size_t sig_len, |
845 | | void *rs_ctx); |
846 | | |
847 | | static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg, |
848 | | const unsigned char *hash, size_t hash_len, |
849 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
850 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
851 | | void *rs_ctx); |
852 | | |
853 | | /* |
854 | | * Restart context for ECDSA operations with ECKEY context |
855 | | * |
856 | | * We need to store an actual ECDSA context, as we need to pass the same to |
857 | | * the underlying ecdsa function, so we can't create it on the fly every time. |
858 | | */ |
859 | | typedef struct { |
860 | | mbedtls_ecdsa_restart_ctx ecdsa_rs; |
861 | | mbedtls_ecdsa_context ecdsa_ctx; |
862 | | } eckey_restart_ctx; |
863 | | |
864 | | static void *eckey_rs_alloc(void) |
865 | | { |
866 | | eckey_restart_ctx *rs_ctx; |
867 | | |
868 | | void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx)); |
869 | | |
870 | | if (ctx != NULL) { |
871 | | rs_ctx = ctx; |
872 | | mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs); |
873 | | mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx); |
874 | | } |
875 | | |
876 | | return ctx; |
877 | | } |
878 | | |
879 | | static void eckey_rs_free(void *ctx) |
880 | | { |
881 | | eckey_restart_ctx *rs_ctx; |
882 | | |
883 | | if (ctx == NULL) { |
884 | | return; |
885 | | } |
886 | | |
887 | | rs_ctx = ctx; |
888 | | mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs); |
889 | | mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx); |
890 | | |
891 | | mbedtls_free(ctx); |
892 | | } |
893 | | |
894 | | static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
895 | | const unsigned char *hash, size_t hash_len, |
896 | | const unsigned char *sig, size_t sig_len, |
897 | | void *rs_ctx) |
898 | | { |
899 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
900 | | eckey_restart_ctx *rs = rs_ctx; |
901 | | |
902 | | /* Should never happen */ |
903 | | if (rs == NULL) { |
904 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
905 | | } |
906 | | |
907 | | /* set up our own sub-context if needed (that is, on first run) */ |
908 | | if (rs->ecdsa_ctx.grp.pbits == 0) { |
909 | | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx)); |
910 | | } |
911 | | |
912 | | MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk, |
913 | | md_alg, hash, hash_len, |
914 | | sig, sig_len, &rs->ecdsa_rs)); |
915 | | |
916 | | cleanup: |
917 | | return ret; |
918 | | } |
919 | | |
920 | | static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
921 | | const unsigned char *hash, size_t hash_len, |
922 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
923 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
924 | | void *rs_ctx) |
925 | | { |
926 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
927 | | eckey_restart_ctx *rs = rs_ctx; |
928 | | |
929 | | /* Should never happen */ |
930 | | if (rs == NULL) { |
931 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
932 | | } |
933 | | |
934 | | /* set up our own sub-context if needed (that is, on first run) */ |
935 | | if (rs->ecdsa_ctx.grp.pbits == 0) { |
936 | | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx)); |
937 | | } |
938 | | |
939 | | MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg, |
940 | | hash, hash_len, sig, sig_size, sig_len, |
941 | | f_rng, p_rng, &rs->ecdsa_rs)); |
942 | | |
943 | | cleanup: |
944 | | return ret; |
945 | | } |
946 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
947 | | |
948 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
949 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
950 | | static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv) |
951 | | { |
952 | | psa_status_t status; |
953 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
954 | | uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
955 | | size_t prv_key_len; |
956 | | mbedtls_svc_key_id_t key_id = prv->priv_id; |
957 | | |
958 | | status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf), |
959 | | &prv_key_len); |
960 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
961 | | if (ret != 0) { |
962 | | return ret; |
963 | | } |
964 | | |
965 | | if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) { |
966 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
967 | | } |
968 | | |
969 | | return 0; |
970 | | } |
971 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
972 | | static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv) |
973 | | { |
974 | | psa_status_t status; |
975 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
976 | | uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
977 | | size_t prv_key_len; |
978 | | psa_status_t destruction_status; |
979 | | mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT; |
980 | | psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT; |
981 | | uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; |
982 | | size_t pub_key_len; |
983 | | size_t curve_bits; |
984 | | const psa_ecc_family_t curve = |
985 | | mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits); |
986 | | const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits); |
987 | | |
988 | | if (curve == 0) { |
989 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
990 | | } |
991 | | |
992 | | psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve)); |
993 | | psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT); |
994 | | |
995 | | ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d, |
996 | | prv_key_buf, curve_bytes); |
997 | | if (ret != 0) { |
998 | | mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf)); |
999 | | return ret; |
1000 | | } |
1001 | | |
1002 | | status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id); |
1003 | | mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf)); |
1004 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
1005 | | if (ret != 0) { |
1006 | | return ret; |
1007 | | } |
1008 | | |
1009 | | // From now on prv_key_buf is used to store the public key of prv. |
1010 | | status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf), |
1011 | | &prv_key_len); |
1012 | | ret = PSA_PK_TO_MBEDTLS_ERR(status); |
1013 | | destruction_status = psa_destroy_key(key_id); |
1014 | | if (ret != 0) { |
1015 | | return ret; |
1016 | | } else if (destruction_status != PSA_SUCCESS) { |
1017 | | return PSA_PK_TO_MBEDTLS_ERR(destruction_status); |
1018 | | } |
1019 | | |
1020 | | ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp, |
1021 | | &mbedtls_pk_ec_rw(*pub)->Q, |
1022 | | MBEDTLS_ECP_PF_UNCOMPRESSED, |
1023 | | &pub_key_len, pub_key_buf, |
1024 | | sizeof(pub_key_buf)); |
1025 | | if (ret != 0) { |
1026 | | return ret; |
1027 | | } |
1028 | | |
1029 | | if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) { |
1030 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
1031 | | } |
1032 | | |
1033 | | return 0; |
1034 | | } |
1035 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1036 | | |
1037 | | static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv, |
1038 | | int (*f_rng)(void *, unsigned char *, size_t), |
1039 | | void *p_rng) |
1040 | | { |
1041 | | (void) f_rng; |
1042 | | (void) p_rng; |
1043 | | return eckey_check_pair_psa(pub, prv); |
1044 | | } |
1045 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
1046 | | static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv, |
1047 | | int (*f_rng)(void *, unsigned char *, size_t), |
1048 | | void *p_rng) |
1049 | | { |
1050 | | return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx, |
1051 | | (const mbedtls_ecp_keypair *) prv->pk_ctx, |
1052 | | f_rng, p_rng); |
1053 | | } |
1054 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
1055 | | |
1056 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
1057 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
1058 | | /* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up |
1059 | | * using the same function. */ |
1060 | | #define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap |
1061 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1062 | | static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub, |
1063 | | mbedtls_pk_context *prv, |
1064 | | int (*f_rng)(void *, unsigned char *, size_t), |
1065 | | void *p_rng) |
1066 | | { |
1067 | | psa_status_t status; |
1068 | | uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; |
1069 | | size_t exp_pub_key_len = 0; |
1070 | | uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN]; |
1071 | | size_t pub_key_len = 0; |
1072 | | int ret; |
1073 | | (void) f_rng; |
1074 | | (void) p_rng; |
1075 | | |
1076 | | status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key), |
1077 | | &exp_pub_key_len); |
1078 | | if (status != PSA_SUCCESS) { |
1079 | | ret = psa_pk_status_to_mbedtls(status); |
1080 | | return ret; |
1081 | | } |
1082 | | ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp), |
1083 | | &(mbedtls_pk_ec_ro(*pub)->Q), |
1084 | | MBEDTLS_ECP_PF_UNCOMPRESSED, |
1085 | | &pub_key_len, pub_key, sizeof(pub_key)); |
1086 | | if (ret != 0) { |
1087 | | return ret; |
1088 | | } |
1089 | | if ((exp_pub_key_len != pub_key_len) || |
1090 | | memcmp(exp_pub_key, pub_key, exp_pub_key_len)) { |
1091 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
1092 | | } |
1093 | | return 0; |
1094 | | } |
1095 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1096 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
1097 | | |
1098 | | #if !defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
1099 | | static void *eckey_alloc_wrap(void) |
1100 | | { |
1101 | | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); |
1102 | | |
1103 | | if (ctx != NULL) { |
1104 | | mbedtls_ecp_keypair_init(ctx); |
1105 | | } |
1106 | | |
1107 | | return ctx; |
1108 | | } |
1109 | | |
1110 | | static void eckey_free_wrap(void *ctx) |
1111 | | { |
1112 | | mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx); |
1113 | | mbedtls_free(ctx); |
1114 | | } |
1115 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1116 | | |
1117 | | static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items) |
1118 | | { |
1119 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
1120 | | items->type = MBEDTLS_PK_DEBUG_PSA_EC; |
1121 | | items->name = "eckey.Q"; |
1122 | | items->value = pk; |
1123 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1124 | | mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx; |
1125 | | items->type = MBEDTLS_PK_DEBUG_ECP; |
1126 | | items->name = "eckey.Q"; |
1127 | | items->value = &(ecp->Q); |
1128 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1129 | | } |
1130 | | |
1131 | | const mbedtls_pk_info_t mbedtls_eckey_info = { |
1132 | | /* .type = */ MBEDTLS_PK_ECKEY, |
1133 | | /* .name = */ "EC", |
1134 | | /* .get_bitlen = */ eckey_get_bitlen, |
1135 | | /* .can_do = */ eckey_can_do, |
1136 | | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
1137 | | /* .verify_func = */ ecdsa_verify_wrap, /* Compatible key structures */ |
1138 | | #else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
1139 | | /* .verify_func = */ NULL, |
1140 | | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
1141 | | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
1142 | | /* .sign_func = */ ecdsa_sign_wrap, /* Compatible key structures */ |
1143 | | #else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
1144 | | /* .sign_func = */ NULL, |
1145 | | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
1146 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1147 | | /* .verify_rs_func = */ eckey_verify_rs_wrap, |
1148 | | /* .sign_rs_func = */ eckey_sign_rs_wrap, |
1149 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1150 | | /* .decrypt_func = */ NULL, |
1151 | | /* .encrypt_func = */ NULL, |
1152 | | /* .check_pair_func = */ eckey_check_pair_wrap, |
1153 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
1154 | | /* .ctx_alloc_func = */ NULL, |
1155 | | /* .ctx_free_func = */ NULL, |
1156 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1157 | | /* .ctx_alloc_func = */ eckey_alloc_wrap, |
1158 | | /* .ctx_free_func = */ eckey_free_wrap, |
1159 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1160 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1161 | | /* .rs_alloc_func = */ eckey_rs_alloc, |
1162 | | /* .rs_free_func = */ eckey_rs_free, |
1163 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1164 | | /* .debug_func = */ eckey_debug, |
1165 | | }; |
1166 | | |
1167 | | /* |
1168 | | * EC key restricted to ECDH |
1169 | | */ |
1170 | | static int eckeydh_can_do(mbedtls_pk_type_t type) |
1171 | | { |
1172 | | return type == MBEDTLS_PK_ECKEY || |
1173 | | type == MBEDTLS_PK_ECKEY_DH; |
1174 | | } |
1175 | | |
1176 | | const mbedtls_pk_info_t mbedtls_eckeydh_info = { |
1177 | | /* .type = */ MBEDTLS_PK_ECKEY_DH, |
1178 | | /* .name = */ "EC_DH", |
1179 | | /* .get_bitlen = */ eckey_get_bitlen, /* Same underlying key structure */ |
1180 | | /* .can_do = */ eckeydh_can_do, |
1181 | | /* .verify_func = */ NULL, |
1182 | | /* .sign_func = */ NULL, |
1183 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1184 | | /* .verify_rs_func = */ NULL, |
1185 | | /* .sign_rs_func = */ NULL, |
1186 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1187 | | /* .decrypt_func = */ NULL, |
1188 | | /* .encrypt_func = */ NULL, |
1189 | | /* .check_pair_func = */ eckey_check_pair_wrap, |
1190 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
1191 | | /* .ctx_alloc_func = */ NULL, |
1192 | | /* .ctx_free_func = */ NULL, |
1193 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1194 | | /* .ctx_alloc_func = */ eckey_alloc_wrap, /* Same underlying key structure */ |
1195 | | /* .ctx_free_func = */ eckey_free_wrap, /* Same underlying key structure */ |
1196 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1197 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1198 | | /* .rs_alloc_func = */ NULL, |
1199 | | /* .rs_free_func = */ NULL, |
1200 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1201 | | /* .debug_func = */ eckey_debug, /* Same underlying key structure */ |
1202 | | }; |
1203 | | |
1204 | | #if defined(MBEDTLS_PK_CAN_ECDSA_SOME) |
1205 | | static int ecdsa_can_do(mbedtls_pk_type_t type) |
1206 | | { |
1207 | | return type == MBEDTLS_PK_ECDSA; |
1208 | | } |
1209 | | |
1210 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1211 | | static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
1212 | | const unsigned char *hash, size_t hash_len, |
1213 | | const unsigned char *sig, size_t sig_len, |
1214 | | void *rs_ctx) |
1215 | | { |
1216 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1217 | | ((void) md_alg); |
1218 | | |
1219 | | ret = mbedtls_ecdsa_read_signature_restartable( |
1220 | | (mbedtls_ecdsa_context *) pk->pk_ctx, |
1221 | | hash, hash_len, sig, sig_len, |
1222 | | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
1223 | | |
1224 | | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
1225 | | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
1226 | | } |
1227 | | |
1228 | | return ret; |
1229 | | } |
1230 | | |
1231 | | static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
1232 | | const unsigned char *hash, size_t hash_len, |
1233 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
1234 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
1235 | | void *rs_ctx) |
1236 | | { |
1237 | | return mbedtls_ecdsa_write_signature_restartable( |
1238 | | (mbedtls_ecdsa_context *) pk->pk_ctx, |
1239 | | md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng, |
1240 | | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
1241 | | |
1242 | | } |
1243 | | |
1244 | | static void *ecdsa_rs_alloc(void) |
1245 | | { |
1246 | | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx)); |
1247 | | |
1248 | | if (ctx != NULL) { |
1249 | | mbedtls_ecdsa_restart_init(ctx); |
1250 | | } |
1251 | | |
1252 | | return ctx; |
1253 | | } |
1254 | | |
1255 | | static void ecdsa_rs_free(void *ctx) |
1256 | | { |
1257 | | mbedtls_ecdsa_restart_free(ctx); |
1258 | | mbedtls_free(ctx); |
1259 | | } |
1260 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1261 | | |
1262 | | const mbedtls_pk_info_t mbedtls_ecdsa_info = { |
1263 | | /* .type = */ MBEDTLS_PK_ECDSA, |
1264 | | /* .name = */ "ECDSA", |
1265 | | /* .get_bitlen = */ eckey_get_bitlen, /* Compatible key structures */ |
1266 | | /* .can_do = */ ecdsa_can_do, |
1267 | | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
1268 | | /* .verify_func = */ ecdsa_verify_wrap, /* Compatible key structures */ |
1269 | | #else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
1270 | | /* .verify_func = */ NULL, |
1271 | | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
1272 | | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
1273 | | /* .sign_func = */ ecdsa_sign_wrap, /* Compatible key structures */ |
1274 | | #else /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
1275 | | /* .sign_func = */ NULL, |
1276 | | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
1277 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1278 | | /* .verify_rs_func = */ ecdsa_verify_rs_wrap, |
1279 | | /* .sign_rs_func = */ ecdsa_sign_rs_wrap, |
1280 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1281 | | /* .decrypt_func = */ NULL, |
1282 | | /* .encrypt_func = */ NULL, |
1283 | | /* .check_pair_func = */ eckey_check_pair_wrap, /* Compatible key structures */ |
1284 | | #if defined(MBEDTLS_PK_USE_PSA_EC_DATA) |
1285 | | /* .ctx_alloc_func = */ NULL, |
1286 | | /* .ctx_free_func = */ NULL, |
1287 | | #else /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1288 | | /* .ctx_alloc_func = */ eckey_alloc_wrap, /* Compatible key structures */ |
1289 | | /* .ctx_free_func = */ eckey_free_wrap, /* Compatible key structures */ |
1290 | | #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */ |
1291 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1292 | | /* .rs_alloc_func = */ ecdsa_rs_alloc, |
1293 | | /* .rs_free_func = */ ecdsa_rs_free, |
1294 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1295 | | /* .debug_func = */ eckey_debug, /* Compatible key structures */ |
1296 | | }; |
1297 | | #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */ |
1298 | | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
1299 | | |
1300 | | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
1301 | | /* |
1302 | | * Support for alternative RSA-private implementations |
1303 | | */ |
1304 | | |
1305 | | static int rsa_alt_can_do(mbedtls_pk_type_t type) |
1306 | | { |
1307 | | return type == MBEDTLS_PK_RSA; |
1308 | | } |
1309 | | |
1310 | | static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk) |
1311 | | { |
1312 | | const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx; |
1313 | | |
1314 | | return 8 * rsa_alt->key_len_func(rsa_alt->key); |
1315 | | } |
1316 | | |
1317 | | static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
1318 | | const unsigned char *hash, size_t hash_len, |
1319 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
1320 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
1321 | | { |
1322 | | mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx; |
1323 | | |
1324 | | #if SIZE_MAX > UINT_MAX |
1325 | | if (UINT_MAX < hash_len) { |
1326 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
1327 | | } |
1328 | | #endif |
1329 | | |
1330 | | *sig_len = rsa_alt->key_len_func(rsa_alt->key); |
1331 | | if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) { |
1332 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
1333 | | } |
1334 | | if (*sig_len > sig_size) { |
1335 | | return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL; |
1336 | | } |
1337 | | |
1338 | | return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng, |
1339 | | md_alg, (unsigned int) hash_len, hash, sig); |
1340 | | } |
1341 | | |
1342 | | static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk, |
1343 | | const unsigned char *input, size_t ilen, |
1344 | | unsigned char *output, size_t *olen, size_t osize, |
1345 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
1346 | | { |
1347 | | mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx; |
1348 | | |
1349 | | ((void) f_rng); |
1350 | | ((void) p_rng); |
1351 | | |
1352 | | if (ilen != rsa_alt->key_len_func(rsa_alt->key)) { |
1353 | | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
1354 | | } |
1355 | | |
1356 | | return rsa_alt->decrypt_func(rsa_alt->key, |
1357 | | olen, input, output, osize); |
1358 | | } |
1359 | | |
1360 | | #if defined(MBEDTLS_RSA_C) |
1361 | | static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv, |
1362 | | int (*f_rng)(void *, unsigned char *, size_t), |
1363 | | void *p_rng) |
1364 | | { |
1365 | | unsigned char hash[32]; |
1366 | | size_t sig_len = 0; |
1367 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
1368 | | |
1369 | | if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) { |
1370 | | return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
1371 | | } |
1372 | | |
1373 | | size_t sig_size = (rsa_get_bitlen(pub) + 7) / 8; |
1374 | | unsigned char *sig = mbedtls_calloc(1, sig_size); |
1375 | | if (sig == NULL) { |
1376 | | return MBEDTLS_ERR_PK_ALLOC_FAILED; |
1377 | | } |
1378 | | |
1379 | | memset(hash, 0x2a, sizeof(hash)); |
1380 | | |
1381 | | if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE, |
1382 | | hash, sizeof(hash), |
1383 | | sig, sig_size, &sig_len, |
1384 | | f_rng, p_rng)) != 0) { |
1385 | | goto cleanup; |
1386 | | } |
1387 | | |
1388 | | if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE, |
1389 | | hash, sizeof(hash), sig, sig_len) != 0) { |
1390 | | ret = MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
1391 | | } |
1392 | | |
1393 | | cleanup: |
1394 | | mbedtls_zeroize_and_free(sig, sig_size); |
1395 | | return ret; |
1396 | | } |
1397 | | #endif /* MBEDTLS_RSA_C */ |
1398 | | |
1399 | | static void *rsa_alt_alloc_wrap(void) |
1400 | | { |
1401 | | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context)); |
1402 | | |
1403 | | if (ctx != NULL) { |
1404 | | memset(ctx, 0, sizeof(mbedtls_rsa_alt_context)); |
1405 | | } |
1406 | | |
1407 | | return ctx; |
1408 | | } |
1409 | | |
1410 | | static void rsa_alt_free_wrap(void *ctx) |
1411 | | { |
1412 | | mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context)); |
1413 | | } |
1414 | | |
1415 | | const mbedtls_pk_info_t mbedtls_rsa_alt_info = { |
1416 | | /* .type = */ MBEDTLS_PK_RSA_ALT, |
1417 | | /* .name = */ "RSA-alt", |
1418 | | /* .get_bitlen = */ rsa_alt_get_bitlen, |
1419 | | /* .can_do = */ rsa_alt_can_do, |
1420 | | /* .verify_func = */ NULL, |
1421 | | /* .sign_func = */ rsa_alt_sign_wrap, |
1422 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1423 | | /* .verify_rs_func = */ NULL, |
1424 | | /* .sign_rs_func = */ NULL, |
1425 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1426 | | /* .decrypt_func = */ rsa_alt_decrypt_wrap, |
1427 | | /* .encrypt_func = */ NULL, |
1428 | | #if defined(MBEDTLS_RSA_C) |
1429 | | /* .check_pair_func = */ rsa_alt_check_pair, |
1430 | | #else |
1431 | | /* .check_pair_func = */ NULL, |
1432 | | #endif |
1433 | | /* .ctx_alloc_func = */ rsa_alt_alloc_wrap, |
1434 | | /* .ctx_free_func = */ rsa_alt_free_wrap, |
1435 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1436 | | /* .rs_alloc_func = */ NULL, |
1437 | | /* .rs_free_func = */ NULL, |
1438 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1439 | | /* .debug_func = */ NULL, |
1440 | | }; |
1441 | | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
1442 | | |
1443 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
1444 | | static size_t opaque_get_bitlen(mbedtls_pk_context *pk) |
1445 | | { |
1446 | | size_t bits; |
1447 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
1448 | | |
1449 | | if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) { |
1450 | | return 0; |
1451 | | } |
1452 | | |
1453 | | bits = psa_get_key_bits(&attributes); |
1454 | | psa_reset_key_attributes(&attributes); |
1455 | | return bits; |
1456 | | } |
1457 | | |
1458 | | #if defined(MBEDTLS_PK_HAVE_ECC_KEYS) |
1459 | | static int ecdsa_opaque_can_do(mbedtls_pk_type_t type) |
1460 | | { |
1461 | | return type == MBEDTLS_PK_ECKEY || |
1462 | | type == MBEDTLS_PK_ECDSA; |
1463 | | } |
1464 | | |
1465 | | const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = { |
1466 | | /* .type = */ MBEDTLS_PK_OPAQUE, |
1467 | | /* .name = */ "Opaque", |
1468 | | /* .get_bitlen = */ opaque_get_bitlen, |
1469 | | /* .can_do = */ ecdsa_opaque_can_do, |
1470 | | #if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY) |
1471 | | /* .verify_func = */ ecdsa_opaque_verify_wrap, |
1472 | | #else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
1473 | | /* .verify_func = */ NULL, |
1474 | | #endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */ |
1475 | | #if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) |
1476 | | /* .sign_func = */ ecdsa_opaque_sign_wrap, |
1477 | | #else /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
1478 | | /* .sign_func = */ NULL, |
1479 | | #endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */ |
1480 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1481 | | /* .verify_rs_func = */ NULL, |
1482 | | /* .sign_rs_func = */ NULL, |
1483 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1484 | | /* .decrypt_func = */ NULL, |
1485 | | /* .encrypt_func = */ NULL, |
1486 | | /* .check_pair_func = */ ecdsa_opaque_check_pair_wrap, |
1487 | | /* .ctx_alloc_func = */ NULL, |
1488 | | /* .ctx_free_func = */ NULL, |
1489 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1490 | | /* .rs_alloc_func = */ NULL, |
1491 | | /* .rs_free_func = */ NULL, |
1492 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1493 | | /* .debug_func = */ NULL, |
1494 | | }; |
1495 | | #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */ |
1496 | | |
1497 | | static int rsa_opaque_can_do(mbedtls_pk_type_t type) |
1498 | | { |
1499 | | return type == MBEDTLS_PK_RSA || |
1500 | | type == MBEDTLS_PK_RSASSA_PSS; |
1501 | | } |
1502 | | |
1503 | | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) |
1504 | | static int rsa_opaque_decrypt(mbedtls_pk_context *pk, |
1505 | | const unsigned char *input, size_t ilen, |
1506 | | unsigned char *output, size_t *olen, size_t osize, |
1507 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
1508 | | { |
1509 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
1510 | | psa_algorithm_t alg; |
1511 | | psa_key_type_t type; |
1512 | | psa_status_t status; |
1513 | | |
1514 | | /* PSA has its own RNG */ |
1515 | | (void) f_rng; |
1516 | | (void) p_rng; |
1517 | | |
1518 | | status = psa_get_key_attributes(pk->priv_id, &attributes); |
1519 | | if (status != PSA_SUCCESS) { |
1520 | | return PSA_PK_TO_MBEDTLS_ERR(status); |
1521 | | } |
1522 | | |
1523 | | type = psa_get_key_type(&attributes); |
1524 | | alg = psa_get_key_algorithm(&attributes); |
1525 | | psa_reset_key_attributes(&attributes); |
1526 | | |
1527 | | if (!PSA_KEY_TYPE_IS_RSA(type)) { |
1528 | | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
1529 | | } |
1530 | | |
1531 | | status = psa_asymmetric_decrypt(pk->priv_id, alg, input, ilen, NULL, 0, output, osize, olen); |
1532 | | #if defined(MBEDTLS_PKCS1_V15) |
1533 | | /* Translate error codes from PSA to legacy |
1534 | | * Success vs INVALID_PADDING vs BUFFER_TOO_SMALL is sensitive |
1535 | | * (padding oracle attack), so we take care to translate that |
1536 | | * part in constant time. |
1537 | | */ |
1538 | | int problem; |
1539 | | status = mbedtls_rsa_decrypt_decompose_ret( |
1540 | | PSA_ERROR_INVALID_PADDING, MBEDTLS_ERR_RSA_INVALID_PADDING, |
1541 | | PSA_ERROR_BUFFER_TOO_SMALL, MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE, |
1542 | | status, &problem); |
1543 | | return PSA_PK_RSA_TO_MBEDTLS_ERR(status) | problem; |
1544 | | #else |
1545 | | return PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
1546 | | #endif |
1547 | | } |
1548 | | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */ |
1549 | | |
1550 | | static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg, |
1551 | | const unsigned char *hash, size_t hash_len, |
1552 | | unsigned char *sig, size_t sig_size, size_t *sig_len, |
1553 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
1554 | | { |
1555 | | #if defined(MBEDTLS_RSA_C) |
1556 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
1557 | | psa_algorithm_t alg; |
1558 | | psa_key_type_t type; |
1559 | | psa_status_t status; |
1560 | | |
1561 | | /* PSA has its own RNG */ |
1562 | | (void) f_rng; |
1563 | | (void) p_rng; |
1564 | | |
1565 | | status = psa_get_key_attributes(pk->priv_id, &attributes); |
1566 | | if (status != PSA_SUCCESS) { |
1567 | | return PSA_PK_TO_MBEDTLS_ERR(status); |
1568 | | } |
1569 | | |
1570 | | type = psa_get_key_type(&attributes); |
1571 | | alg = psa_get_key_algorithm(&attributes); |
1572 | | psa_reset_key_attributes(&attributes); |
1573 | | |
1574 | | if (PSA_KEY_TYPE_IS_RSA(type)) { |
1575 | | alg = (alg & ~PSA_ALG_HASH_MASK) | mbedtls_md_psa_alg_from_type(md_alg); |
1576 | | } else { |
1577 | | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
1578 | | } |
1579 | | |
1580 | | status = psa_sign_hash(pk->priv_id, alg, hash, hash_len, sig, sig_size, sig_len); |
1581 | | if (status != PSA_SUCCESS) { |
1582 | | if (PSA_KEY_TYPE_IS_RSA(type)) { |
1583 | | return PSA_PK_RSA_TO_MBEDTLS_ERR(status); |
1584 | | } else { |
1585 | | return PSA_PK_TO_MBEDTLS_ERR(status); |
1586 | | } |
1587 | | } |
1588 | | |
1589 | | return 0; |
1590 | | #else /* !MBEDTLS_RSA_C */ |
1591 | | ((void) pk); |
1592 | | ((void) md_alg); |
1593 | | ((void) hash); |
1594 | | ((void) hash_len); |
1595 | | ((void) sig); |
1596 | | ((void) sig_size); |
1597 | | ((void) sig_len); |
1598 | | ((void) f_rng); |
1599 | | ((void) p_rng); |
1600 | | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
1601 | | #endif /* !MBEDTLS_RSA_C */ |
1602 | | } |
1603 | | |
1604 | | const mbedtls_pk_info_t mbedtls_rsa_opaque_info = { |
1605 | | /* .type = */ MBEDTLS_PK_OPAQUE, |
1606 | | /* .name = */ "Opaque", |
1607 | | /* .get_bitlen = */ opaque_get_bitlen, |
1608 | | /* .can_do = */ rsa_opaque_can_do, |
1609 | | /* .verify_func = */ NULL, |
1610 | | /* .sign_func = */ rsa_opaque_sign_wrap, |
1611 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1612 | | /* .verify_rs_func = */ NULL, |
1613 | | /* .sign_rs_func = */ NULL, |
1614 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1615 | | #if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC) |
1616 | | /* .decrypt_func = */ rsa_opaque_decrypt, |
1617 | | #else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */ |
1618 | | /* .decrypt_func = */ NULL, |
1619 | | #endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */ |
1620 | | /* .encrypt_func = */ NULL, |
1621 | | /* .check_pair_func = */ NULL, |
1622 | | /* .ctx_alloc_func = */ NULL, |
1623 | | /* .ctx_free_func = */ NULL, |
1624 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1625 | | /* .rs_alloc_func = */ NULL, |
1626 | | /* .rs_free_func = */ NULL, |
1627 | | #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */ |
1628 | | /* .debug_func = */ NULL, |
1629 | | }; |
1630 | | |
1631 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
1632 | | |
1633 | | #endif /* MBEDTLS_PK_C */ |