/work/mbedtls-2.28.8/library/pk_wrap.c
Line | Count | Source (jump to first uncovered line) |
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 | | #if defined(MBEDTLS_PK_C) |
11 | | #include "mbedtls/pk_internal.h" |
12 | | #include "mbedtls/error.h" |
13 | | |
14 | | /* Even if RSA not activated, for the sake of RSA-alt */ |
15 | | #include "mbedtls/rsa.h" |
16 | | |
17 | | #include <string.h> |
18 | | |
19 | | #if defined(MBEDTLS_ECP_C) |
20 | | #include "mbedtls/ecp.h" |
21 | | #endif |
22 | | |
23 | | #if defined(MBEDTLS_ECDSA_C) |
24 | | #include "mbedtls/ecdsa.h" |
25 | | #endif |
26 | | |
27 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
28 | | #include "mbedtls/asn1write.h" |
29 | | #endif |
30 | | |
31 | | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
32 | | #include "mbedtls/platform_util.h" |
33 | | #endif |
34 | | |
35 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
36 | | #include "psa/crypto.h" |
37 | | #include "mbedtls/psa_util.h" |
38 | | #include "mbedtls/asn1.h" |
39 | | #endif |
40 | | |
41 | | #include "mbedtls/platform.h" |
42 | | |
43 | | #include <limits.h> |
44 | | #include <stdint.h> |
45 | | |
46 | | #if defined(MBEDTLS_RSA_C) |
47 | | static int rsa_can_do(mbedtls_pk_type_t type) |
48 | 0 | { |
49 | 0 | return type == MBEDTLS_PK_RSA || |
50 | 0 | type == MBEDTLS_PK_RSASSA_PSS; |
51 | 0 | } |
52 | | |
53 | | static size_t rsa_get_bitlen(const void *ctx) |
54 | 0 | { |
55 | 0 | const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx; |
56 | | /* Unfortunately, the rsa.h interface does not have a direct way |
57 | | * to access the bit-length that works with MBEDTLS_RSA_ALT. |
58 | | * So we have to do a little work here. |
59 | | */ |
60 | 0 | mbedtls_mpi N; |
61 | 0 | mbedtls_mpi_init(&N); |
62 | 0 | int ret = mbedtls_rsa_export(rsa, &N, NULL, NULL, NULL, NULL); |
63 | | /* If the export fails for some reason (e.g. the RSA_ALT implementation |
64 | | * does not support export, or there is not enough memory), |
65 | | * we have no way of returning an error from this function. |
66 | | * As a fallback, return the byte-length converted in bits, which is |
67 | | * the correct value if the modulus size is a multiple of 8 bits, which |
68 | | * is very often the case in practice. */ |
69 | 0 | size_t bitlen = (ret == 0 ? mbedtls_mpi_bitlen(&N) : |
70 | 0 | 8 * mbedtls_rsa_get_len(rsa)); |
71 | 0 | mbedtls_mpi_free(&N); |
72 | 0 | return bitlen; |
73 | 0 | } |
74 | | |
75 | | static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
76 | | const unsigned char *hash, size_t hash_len, |
77 | | const unsigned char *sig, size_t sig_len) |
78 | 0 | { |
79 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
80 | 0 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
81 | 0 | size_t rsa_len = mbedtls_rsa_get_len(rsa); |
82 | |
|
83 | 0 | #if SIZE_MAX > UINT_MAX |
84 | 0 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
85 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
86 | 0 | } |
87 | 0 | #endif /* SIZE_MAX > UINT_MAX */ |
88 | | |
89 | 0 | if (sig_len < rsa_len) { |
90 | 0 | return MBEDTLS_ERR_RSA_VERIFY_FAILED; |
91 | 0 | } |
92 | | |
93 | 0 | if ((ret = mbedtls_rsa_pkcs1_verify(rsa, NULL, NULL, |
94 | 0 | MBEDTLS_RSA_PUBLIC, md_alg, |
95 | 0 | (unsigned int) hash_len, hash, sig)) != 0) { |
96 | 0 | return ret; |
97 | 0 | } |
98 | | |
99 | | /* The buffer contains a valid signature followed by extra data. |
100 | | * We have a special error code for that so that so that callers can |
101 | | * use mbedtls_pk_verify() to check "Does the buffer start with a |
102 | | * valid signature?" and not just "Does the buffer contain a valid |
103 | | * signature?". */ |
104 | 0 | if (sig_len > rsa_len) { |
105 | 0 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
106 | 0 | } |
107 | | |
108 | 0 | return 0; |
109 | 0 | } |
110 | | |
111 | | static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
112 | | const unsigned char *hash, size_t hash_len, |
113 | | unsigned char *sig, size_t *sig_len, |
114 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
115 | 0 | { |
116 | 0 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
117 | |
|
118 | 0 | #if SIZE_MAX > UINT_MAX |
119 | 0 | if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) { |
120 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
121 | 0 | } |
122 | 0 | #endif /* SIZE_MAX > UINT_MAX */ |
123 | | |
124 | 0 | *sig_len = mbedtls_rsa_get_len(rsa); |
125 | |
|
126 | 0 | return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, |
127 | 0 | md_alg, (unsigned int) hash_len, hash, sig); |
128 | 0 | } |
129 | | |
130 | | static int rsa_decrypt_wrap(void *ctx, |
131 | | const unsigned char *input, size_t ilen, |
132 | | unsigned char *output, size_t *olen, size_t osize, |
133 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
134 | 0 | { |
135 | 0 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
136 | |
|
137 | 0 | if (ilen != mbedtls_rsa_get_len(rsa)) { |
138 | 0 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
139 | 0 | } |
140 | | |
141 | 0 | return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng, |
142 | 0 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize); |
143 | 0 | } |
144 | | |
145 | | static int rsa_encrypt_wrap(void *ctx, |
146 | | const unsigned char *input, size_t ilen, |
147 | | unsigned char *output, size_t *olen, size_t osize, |
148 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
149 | 0 | { |
150 | 0 | mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx; |
151 | 0 | *olen = mbedtls_rsa_get_len(rsa); |
152 | |
|
153 | 0 | if (*olen > osize) { |
154 | 0 | return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE; |
155 | 0 | } |
156 | | |
157 | 0 | return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng, MBEDTLS_RSA_PUBLIC, |
158 | 0 | ilen, input, output); |
159 | 0 | } |
160 | | |
161 | | static int rsa_check_pair_wrap(const void *pub, const void *prv) |
162 | 0 | { |
163 | 0 | return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub, |
164 | 0 | (const mbedtls_rsa_context *) prv); |
165 | 0 | } |
166 | | |
167 | | static void *rsa_alloc_wrap(void) |
168 | 0 | { |
169 | 0 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context)); |
170 | |
|
171 | 0 | if (ctx != NULL) { |
172 | 0 | mbedtls_rsa_init((mbedtls_rsa_context *) ctx, 0, 0); |
173 | 0 | } |
174 | |
|
175 | 0 | return ctx; |
176 | 0 | } |
177 | | |
178 | | static void rsa_free_wrap(void *ctx) |
179 | 0 | { |
180 | 0 | mbedtls_rsa_free((mbedtls_rsa_context *) ctx); |
181 | 0 | mbedtls_free(ctx); |
182 | 0 | } |
183 | | |
184 | | static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items) |
185 | 0 | { |
186 | 0 | items->type = MBEDTLS_PK_DEBUG_MPI; |
187 | 0 | items->name = "rsa.N"; |
188 | 0 | items->value = &(((mbedtls_rsa_context *) ctx)->N); |
189 | |
|
190 | 0 | items++; |
191 | |
|
192 | 0 | items->type = MBEDTLS_PK_DEBUG_MPI; |
193 | 0 | items->name = "rsa.E"; |
194 | 0 | items->value = &(((mbedtls_rsa_context *) ctx)->E); |
195 | 0 | } |
196 | | |
197 | | const mbedtls_pk_info_t mbedtls_rsa_info = { |
198 | | MBEDTLS_PK_RSA, |
199 | | "RSA", |
200 | | rsa_get_bitlen, |
201 | | rsa_can_do, |
202 | | rsa_verify_wrap, |
203 | | rsa_sign_wrap, |
204 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
205 | | NULL, |
206 | | NULL, |
207 | | #endif |
208 | | rsa_decrypt_wrap, |
209 | | rsa_encrypt_wrap, |
210 | | rsa_check_pair_wrap, |
211 | | rsa_alloc_wrap, |
212 | | rsa_free_wrap, |
213 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
214 | | NULL, |
215 | | NULL, |
216 | | #endif |
217 | | rsa_debug, |
218 | | }; |
219 | | #endif /* MBEDTLS_RSA_C */ |
220 | | |
221 | | #if defined(MBEDTLS_ECP_C) |
222 | | /* |
223 | | * Generic EC key |
224 | | */ |
225 | | static int eckey_can_do(mbedtls_pk_type_t type) |
226 | 0 | { |
227 | 0 | return type == MBEDTLS_PK_ECKEY || |
228 | 0 | type == MBEDTLS_PK_ECKEY_DH || |
229 | 0 | type == MBEDTLS_PK_ECDSA; |
230 | 0 | } |
231 | | |
232 | | static size_t eckey_get_bitlen(const void *ctx) |
233 | 0 | { |
234 | 0 | return ((mbedtls_ecp_keypair *) ctx)->grp.pbits; |
235 | 0 | } |
236 | | |
237 | | #if defined(MBEDTLS_ECDSA_C) |
238 | | /* Forward declarations */ |
239 | | static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
240 | | const unsigned char *hash, size_t hash_len, |
241 | | const unsigned char *sig, size_t sig_len); |
242 | | |
243 | | static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
244 | | const unsigned char *hash, size_t hash_len, |
245 | | unsigned char *sig, size_t *sig_len, |
246 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng); |
247 | | |
248 | | static int eckey_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
249 | | const unsigned char *hash, size_t hash_len, |
250 | | const unsigned char *sig, size_t sig_len) |
251 | 0 | { |
252 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
253 | 0 | mbedtls_ecdsa_context ecdsa; |
254 | |
|
255 | 0 | mbedtls_ecdsa_init(&ecdsa); |
256 | |
|
257 | 0 | if ((ret = mbedtls_ecdsa_from_keypair(&ecdsa, ctx)) == 0) { |
258 | 0 | ret = ecdsa_verify_wrap(&ecdsa, md_alg, hash, hash_len, sig, sig_len); |
259 | 0 | } |
260 | |
|
261 | 0 | mbedtls_ecdsa_free(&ecdsa); |
262 | |
|
263 | 0 | return ret; |
264 | 0 | } |
265 | | |
266 | | static int eckey_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
267 | | const unsigned char *hash, size_t hash_len, |
268 | | unsigned char *sig, size_t *sig_len, |
269 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
270 | 0 | { |
271 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
272 | 0 | mbedtls_ecdsa_context ecdsa; |
273 | |
|
274 | 0 | mbedtls_ecdsa_init(&ecdsa); |
275 | |
|
276 | 0 | if ((ret = mbedtls_ecdsa_from_keypair(&ecdsa, ctx)) == 0) { |
277 | 0 | ret = ecdsa_sign_wrap(&ecdsa, md_alg, hash, hash_len, sig, sig_len, |
278 | 0 | f_rng, p_rng); |
279 | 0 | } |
280 | |
|
281 | 0 | mbedtls_ecdsa_free(&ecdsa); |
282 | |
|
283 | 0 | return ret; |
284 | 0 | } |
285 | | |
286 | | #if defined(MBEDTLS_ECP_RESTARTABLE) |
287 | | /* Forward declarations */ |
288 | | static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
289 | | const unsigned char *hash, size_t hash_len, |
290 | | const unsigned char *sig, size_t sig_len, |
291 | | void *rs_ctx); |
292 | | |
293 | | static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
294 | | const unsigned char *hash, size_t hash_len, |
295 | | unsigned char *sig, size_t *sig_len, |
296 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
297 | | void *rs_ctx); |
298 | | |
299 | | /* |
300 | | * Restart context for ECDSA operations with ECKEY context |
301 | | * |
302 | | * We need to store an actual ECDSA context, as we need to pass the same to |
303 | | * the underlying ecdsa function, so we can't create it on the fly every time. |
304 | | */ |
305 | | typedef struct { |
306 | | mbedtls_ecdsa_restart_ctx ecdsa_rs; |
307 | | mbedtls_ecdsa_context ecdsa_ctx; |
308 | | } eckey_restart_ctx; |
309 | | |
310 | | static void *eckey_rs_alloc(void) |
311 | | { |
312 | | eckey_restart_ctx *rs_ctx; |
313 | | |
314 | | void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx)); |
315 | | |
316 | | if (ctx != NULL) { |
317 | | rs_ctx = ctx; |
318 | | mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs); |
319 | | mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx); |
320 | | } |
321 | | |
322 | | return ctx; |
323 | | } |
324 | | |
325 | | static void eckey_rs_free(void *ctx) |
326 | | { |
327 | | eckey_restart_ctx *rs_ctx; |
328 | | |
329 | | if (ctx == NULL) { |
330 | | return; |
331 | | } |
332 | | |
333 | | rs_ctx = ctx; |
334 | | mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs); |
335 | | mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx); |
336 | | |
337 | | mbedtls_free(ctx); |
338 | | } |
339 | | |
340 | | static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
341 | | const unsigned char *hash, size_t hash_len, |
342 | | const unsigned char *sig, size_t sig_len, |
343 | | void *rs_ctx) |
344 | | { |
345 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
346 | | eckey_restart_ctx *rs = rs_ctx; |
347 | | |
348 | | /* Should never happen */ |
349 | | if (rs == NULL) { |
350 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
351 | | } |
352 | | |
353 | | /* set up our own sub-context if needed (that is, on first run) */ |
354 | | if (rs->ecdsa_ctx.grp.pbits == 0) { |
355 | | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx)); |
356 | | } |
357 | | |
358 | | MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx, |
359 | | md_alg, hash, hash_len, |
360 | | sig, sig_len, &rs->ecdsa_rs)); |
361 | | |
362 | | cleanup: |
363 | | return ret; |
364 | | } |
365 | | |
366 | | static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
367 | | const unsigned char *hash, size_t hash_len, |
368 | | unsigned char *sig, size_t *sig_len, |
369 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
370 | | void *rs_ctx) |
371 | | { |
372 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
373 | | eckey_restart_ctx *rs = rs_ctx; |
374 | | |
375 | | /* Should never happen */ |
376 | | if (rs == NULL) { |
377 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
378 | | } |
379 | | |
380 | | /* set up our own sub-context if needed (that is, on first run) */ |
381 | | if (rs->ecdsa_ctx.grp.pbits == 0) { |
382 | | MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx)); |
383 | | } |
384 | | |
385 | | MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg, |
386 | | hash, hash_len, sig, sig_len, |
387 | | f_rng, p_rng, &rs->ecdsa_rs)); |
388 | | |
389 | | cleanup: |
390 | | return ret; |
391 | | } |
392 | | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
393 | | #endif /* MBEDTLS_ECDSA_C */ |
394 | | |
395 | | static int eckey_check_pair(const void *pub, const void *prv) |
396 | 0 | { |
397 | 0 | return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub, |
398 | 0 | (const mbedtls_ecp_keypair *) prv); |
399 | 0 | } |
400 | | |
401 | | static void *eckey_alloc_wrap(void) |
402 | 0 | { |
403 | 0 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair)); |
404 | |
|
405 | 0 | if (ctx != NULL) { |
406 | 0 | mbedtls_ecp_keypair_init(ctx); |
407 | 0 | } |
408 | |
|
409 | 0 | return ctx; |
410 | 0 | } |
411 | | |
412 | | static void eckey_free_wrap(void *ctx) |
413 | 0 | { |
414 | 0 | mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx); |
415 | 0 | mbedtls_free(ctx); |
416 | 0 | } |
417 | | |
418 | | static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items) |
419 | 0 | { |
420 | 0 | items->type = MBEDTLS_PK_DEBUG_ECP; |
421 | 0 | items->name = "eckey.Q"; |
422 | 0 | items->value = &(((mbedtls_ecp_keypair *) ctx)->Q); |
423 | 0 | } |
424 | | |
425 | | const mbedtls_pk_info_t mbedtls_eckey_info = { |
426 | | MBEDTLS_PK_ECKEY, |
427 | | "EC", |
428 | | eckey_get_bitlen, |
429 | | eckey_can_do, |
430 | | #if defined(MBEDTLS_ECDSA_C) |
431 | | eckey_verify_wrap, |
432 | | eckey_sign_wrap, |
433 | | #if defined(MBEDTLS_ECP_RESTARTABLE) |
434 | | eckey_verify_rs_wrap, |
435 | | eckey_sign_rs_wrap, |
436 | | #endif |
437 | | #else /* MBEDTLS_ECDSA_C */ |
438 | | NULL, |
439 | | NULL, |
440 | | #endif /* MBEDTLS_ECDSA_C */ |
441 | | NULL, |
442 | | NULL, |
443 | | eckey_check_pair, |
444 | | eckey_alloc_wrap, |
445 | | eckey_free_wrap, |
446 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
447 | | eckey_rs_alloc, |
448 | | eckey_rs_free, |
449 | | #endif |
450 | | eckey_debug, |
451 | | }; |
452 | | |
453 | | /* |
454 | | * EC key restricted to ECDH |
455 | | */ |
456 | | static int eckeydh_can_do(mbedtls_pk_type_t type) |
457 | 0 | { |
458 | 0 | return type == MBEDTLS_PK_ECKEY || |
459 | 0 | type == MBEDTLS_PK_ECKEY_DH; |
460 | 0 | } |
461 | | |
462 | | const mbedtls_pk_info_t mbedtls_eckeydh_info = { |
463 | | MBEDTLS_PK_ECKEY_DH, |
464 | | "EC_DH", |
465 | | eckey_get_bitlen, /* Same underlying key structure */ |
466 | | eckeydh_can_do, |
467 | | NULL, |
468 | | NULL, |
469 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
470 | | NULL, |
471 | | NULL, |
472 | | #endif |
473 | | NULL, |
474 | | NULL, |
475 | | eckey_check_pair, |
476 | | eckey_alloc_wrap, /* Same underlying key structure */ |
477 | | eckey_free_wrap, /* Same underlying key structure */ |
478 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
479 | | NULL, |
480 | | NULL, |
481 | | #endif |
482 | | eckey_debug, /* Same underlying key structure */ |
483 | | }; |
484 | | #endif /* MBEDTLS_ECP_C */ |
485 | | |
486 | | #if defined(MBEDTLS_ECDSA_C) |
487 | | static int ecdsa_can_do(mbedtls_pk_type_t type) |
488 | 0 | { |
489 | 0 | return type == MBEDTLS_PK_ECDSA; |
490 | 0 | } |
491 | | |
492 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
493 | | /* |
494 | | * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of |
495 | | * those integers and convert it to the fixed-length encoding expected by PSA. |
496 | | */ |
497 | | static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end, |
498 | | unsigned char *to, size_t to_len) |
499 | | { |
500 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
501 | | size_t unpadded_len, padding_len; |
502 | | |
503 | | if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len, |
504 | | MBEDTLS_ASN1_INTEGER)) != 0) { |
505 | | return ret; |
506 | | } |
507 | | |
508 | | while (unpadded_len > 0 && **from == 0x00) { |
509 | | (*from)++; |
510 | | unpadded_len--; |
511 | | } |
512 | | |
513 | | if (unpadded_len > to_len || unpadded_len == 0) { |
514 | | return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
515 | | } |
516 | | |
517 | | padding_len = to_len - unpadded_len; |
518 | | memset(to, 0x00, padding_len); |
519 | | memcpy(to + padding_len, *from, unpadded_len); |
520 | | (*from) += unpadded_len; |
521 | | |
522 | | return 0; |
523 | | } |
524 | | |
525 | | /* |
526 | | * Convert a signature from an ASN.1 sequence of two integers |
527 | | * to a raw {r,s} buffer. Note: the provided sig buffer must be at least |
528 | | * twice as big as int_size. |
529 | | */ |
530 | | static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end, |
531 | | unsigned char *sig, size_t int_size) |
532 | | { |
533 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
534 | | size_t tmp_size; |
535 | | |
536 | | if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size, |
537 | | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) { |
538 | | return ret; |
539 | | } |
540 | | |
541 | | /* Extract r */ |
542 | | if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) { |
543 | | return ret; |
544 | | } |
545 | | /* Extract s */ |
546 | | if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) { |
547 | | return ret; |
548 | | } |
549 | | |
550 | | return 0; |
551 | | } |
552 | | |
553 | | static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg, |
554 | | const unsigned char *hash, size_t hash_len, |
555 | | const unsigned char *sig, size_t sig_len) |
556 | | { |
557 | | mbedtls_ecdsa_context *ctx = ctx_arg; |
558 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
559 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
560 | | psa_key_id_t key_id = 0; |
561 | | psa_status_t status; |
562 | | mbedtls_pk_context key; |
563 | | int key_len; |
564 | | /* see ECP_PUB_DER_MAX_BYTES in pkwrite.c */ |
565 | | unsigned char buf[30 + 2 * MBEDTLS_ECP_MAX_BYTES]; |
566 | | unsigned char *p; |
567 | | mbedtls_pk_info_t pk_info = mbedtls_eckey_info; |
568 | | psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY; |
569 | | size_t curve_bits; |
570 | | psa_ecc_family_t curve = |
571 | | mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits); |
572 | | const size_t signature_part_size = (ctx->grp.nbits + 7) / 8; |
573 | | ((void) md_alg); |
574 | | |
575 | | if (curve == 0) { |
576 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
577 | | } |
578 | | |
579 | | /* mbedtls_pk_write_pubkey() expects a full PK context; |
580 | | * re-construct one to make it happy */ |
581 | | key.pk_info = &pk_info; |
582 | | key.pk_ctx = ctx; |
583 | | p = buf + sizeof(buf); |
584 | | key_len = mbedtls_pk_write_pubkey(&p, buf, &key); |
585 | | if (key_len <= 0) { |
586 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
587 | | } |
588 | | |
589 | | psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve)); |
590 | | psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH); |
591 | | psa_set_key_algorithm(&attributes, psa_sig_md); |
592 | | |
593 | | status = psa_import_key(&attributes, |
594 | | buf + sizeof(buf) - key_len, key_len, |
595 | | &key_id); |
596 | | if (status != PSA_SUCCESS) { |
597 | | ret = mbedtls_psa_err_translate_pk(status); |
598 | | goto cleanup; |
599 | | } |
600 | | |
601 | | /* We don't need the exported key anymore and can |
602 | | * reuse its buffer for signature extraction. */ |
603 | | if (2 * signature_part_size > sizeof(buf)) { |
604 | | ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
605 | | goto cleanup; |
606 | | } |
607 | | |
608 | | p = (unsigned char *) sig; |
609 | | if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf, |
610 | | signature_part_size)) != 0) { |
611 | | goto cleanup; |
612 | | } |
613 | | |
614 | | if (psa_verify_hash(key_id, psa_sig_md, |
615 | | hash, hash_len, |
616 | | buf, 2 * signature_part_size) |
617 | | != PSA_SUCCESS) { |
618 | | ret = MBEDTLS_ERR_ECP_VERIFY_FAILED; |
619 | | goto cleanup; |
620 | | } |
621 | | |
622 | | if (p != sig + sig_len) { |
623 | | ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
624 | | goto cleanup; |
625 | | } |
626 | | ret = 0; |
627 | | |
628 | | cleanup: |
629 | | psa_destroy_key(key_id); |
630 | | return ret; |
631 | | } |
632 | | #else /* MBEDTLS_USE_PSA_CRYPTO */ |
633 | | static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg, |
634 | | const unsigned char *hash, size_t hash_len, |
635 | | const unsigned char *sig, size_t sig_len) |
636 | 0 | { |
637 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
638 | 0 | ((void) md_alg); |
639 | |
|
640 | 0 | ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx, |
641 | 0 | hash, hash_len, sig, sig_len); |
642 | |
|
643 | 0 | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
644 | 0 | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
645 | 0 | } |
646 | | |
647 | 0 | return ret; |
648 | 0 | } |
649 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
650 | | |
651 | | static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
652 | | const unsigned char *hash, size_t hash_len, |
653 | | unsigned char *sig, size_t *sig_len, |
654 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
655 | 0 | { |
656 | 0 | return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx, |
657 | 0 | md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng); |
658 | 0 | } |
659 | | |
660 | | #if defined(MBEDTLS_ECP_RESTARTABLE) |
661 | | static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
662 | | const unsigned char *hash, size_t hash_len, |
663 | | const unsigned char *sig, size_t sig_len, |
664 | | void *rs_ctx) |
665 | | { |
666 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
667 | | ((void) md_alg); |
668 | | |
669 | | ret = mbedtls_ecdsa_read_signature_restartable( |
670 | | (mbedtls_ecdsa_context *) ctx, |
671 | | hash, hash_len, sig, sig_len, |
672 | | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
673 | | |
674 | | if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) { |
675 | | return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH; |
676 | | } |
677 | | |
678 | | return ret; |
679 | | } |
680 | | |
681 | | static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg, |
682 | | const unsigned char *hash, size_t hash_len, |
683 | | unsigned char *sig, size_t *sig_len, |
684 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng, |
685 | | void *rs_ctx) |
686 | | { |
687 | | return mbedtls_ecdsa_write_signature_restartable( |
688 | | (mbedtls_ecdsa_context *) ctx, |
689 | | md_alg, hash, hash_len, sig, sig_len, f_rng, p_rng, |
690 | | (mbedtls_ecdsa_restart_ctx *) rs_ctx); |
691 | | |
692 | | } |
693 | | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
694 | | |
695 | | static void *ecdsa_alloc_wrap(void) |
696 | 0 | { |
697 | 0 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_context)); |
698 | |
|
699 | 0 | if (ctx != NULL) { |
700 | 0 | mbedtls_ecdsa_init((mbedtls_ecdsa_context *) ctx); |
701 | 0 | } |
702 | |
|
703 | 0 | return ctx; |
704 | 0 | } |
705 | | |
706 | | static void ecdsa_free_wrap(void *ctx) |
707 | 0 | { |
708 | 0 | mbedtls_ecdsa_free((mbedtls_ecdsa_context *) ctx); |
709 | 0 | mbedtls_free(ctx); |
710 | 0 | } |
711 | | |
712 | | #if defined(MBEDTLS_ECP_RESTARTABLE) |
713 | | static void *ecdsa_rs_alloc(void) |
714 | | { |
715 | | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx)); |
716 | | |
717 | | if (ctx != NULL) { |
718 | | mbedtls_ecdsa_restart_init(ctx); |
719 | | } |
720 | | |
721 | | return ctx; |
722 | | } |
723 | | |
724 | | static void ecdsa_rs_free(void *ctx) |
725 | | { |
726 | | mbedtls_ecdsa_restart_free(ctx); |
727 | | mbedtls_free(ctx); |
728 | | } |
729 | | #endif /* MBEDTLS_ECP_RESTARTABLE */ |
730 | | |
731 | | const mbedtls_pk_info_t mbedtls_ecdsa_info = { |
732 | | MBEDTLS_PK_ECDSA, |
733 | | "ECDSA", |
734 | | eckey_get_bitlen, /* Compatible key structures */ |
735 | | ecdsa_can_do, |
736 | | ecdsa_verify_wrap, |
737 | | ecdsa_sign_wrap, |
738 | | #if defined(MBEDTLS_ECP_RESTARTABLE) |
739 | | ecdsa_verify_rs_wrap, |
740 | | ecdsa_sign_rs_wrap, |
741 | | #endif |
742 | | NULL, |
743 | | NULL, |
744 | | eckey_check_pair, /* Compatible key structures */ |
745 | | ecdsa_alloc_wrap, |
746 | | ecdsa_free_wrap, |
747 | | #if defined(MBEDTLS_ECP_RESTARTABLE) |
748 | | ecdsa_rs_alloc, |
749 | | ecdsa_rs_free, |
750 | | #endif |
751 | | eckey_debug, /* Compatible key structures */ |
752 | | }; |
753 | | #endif /* MBEDTLS_ECDSA_C */ |
754 | | |
755 | | #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT) |
756 | | /* |
757 | | * Support for alternative RSA-private implementations |
758 | | */ |
759 | | |
760 | | static int rsa_alt_can_do(mbedtls_pk_type_t type) |
761 | 0 | { |
762 | 0 | return type == MBEDTLS_PK_RSA; |
763 | 0 | } |
764 | | |
765 | | static size_t rsa_alt_get_bitlen(const void *ctx) |
766 | 0 | { |
767 | 0 | const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx; |
768 | |
|
769 | 0 | return 8 * rsa_alt->key_len_func(rsa_alt->key); |
770 | 0 | } |
771 | | |
772 | | static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
773 | | const unsigned char *hash, size_t hash_len, |
774 | | unsigned char *sig, size_t *sig_len, |
775 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
776 | 0 | { |
777 | 0 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
778 | |
|
779 | 0 | #if SIZE_MAX > UINT_MAX |
780 | 0 | if (UINT_MAX < hash_len) { |
781 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
782 | 0 | } |
783 | 0 | #endif /* SIZE_MAX > UINT_MAX */ |
784 | | |
785 | 0 | *sig_len = rsa_alt->key_len_func(rsa_alt->key); |
786 | 0 | if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) { |
787 | 0 | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
788 | 0 | } |
789 | | |
790 | 0 | return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng, MBEDTLS_RSA_PRIVATE, |
791 | 0 | md_alg, (unsigned int) hash_len, hash, sig); |
792 | 0 | } |
793 | | |
794 | | static int rsa_alt_decrypt_wrap(void *ctx, |
795 | | const unsigned char *input, size_t ilen, |
796 | | unsigned char *output, size_t *olen, size_t osize, |
797 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
798 | 0 | { |
799 | 0 | mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx; |
800 | |
|
801 | 0 | ((void) f_rng); |
802 | 0 | ((void) p_rng); |
803 | |
|
804 | 0 | if (ilen != rsa_alt->key_len_func(rsa_alt->key)) { |
805 | 0 | return MBEDTLS_ERR_RSA_BAD_INPUT_DATA; |
806 | 0 | } |
807 | | |
808 | 0 | return rsa_alt->decrypt_func(rsa_alt->key, |
809 | 0 | MBEDTLS_RSA_PRIVATE, olen, input, output, osize); |
810 | 0 | } |
811 | | |
812 | | #if defined(MBEDTLS_RSA_C) |
813 | | static int rsa_alt_check_pair(const void *pub, const void *prv) |
814 | 0 | { |
815 | 0 | unsigned char sig[MBEDTLS_MPI_MAX_SIZE]; |
816 | 0 | unsigned char hash[32]; |
817 | 0 | size_t sig_len = 0; |
818 | 0 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
819 | |
|
820 | 0 | if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) { |
821 | 0 | return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
822 | 0 | } |
823 | | |
824 | 0 | memset(hash, 0x2a, sizeof(hash)); |
825 | |
|
826 | 0 | if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE, |
827 | 0 | hash, sizeof(hash), |
828 | 0 | sig, &sig_len, NULL, NULL)) != 0) { |
829 | 0 | return ret; |
830 | 0 | } |
831 | | |
832 | 0 | if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE, |
833 | 0 | hash, sizeof(hash), sig, sig_len) != 0) { |
834 | 0 | return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED; |
835 | 0 | } |
836 | | |
837 | 0 | return 0; |
838 | 0 | } |
839 | | #endif /* MBEDTLS_RSA_C */ |
840 | | |
841 | | static void *rsa_alt_alloc_wrap(void) |
842 | 0 | { |
843 | 0 | void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context)); |
844 | |
|
845 | 0 | if (ctx != NULL) { |
846 | 0 | memset(ctx, 0, sizeof(mbedtls_rsa_alt_context)); |
847 | 0 | } |
848 | |
|
849 | 0 | return ctx; |
850 | 0 | } |
851 | | |
852 | | static void rsa_alt_free_wrap(void *ctx) |
853 | 0 | { |
854 | 0 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context)); |
855 | 0 | mbedtls_free(ctx); |
856 | 0 | } |
857 | | |
858 | | const mbedtls_pk_info_t mbedtls_rsa_alt_info = { |
859 | | MBEDTLS_PK_RSA_ALT, |
860 | | "RSA-alt", |
861 | | rsa_alt_get_bitlen, |
862 | | rsa_alt_can_do, |
863 | | NULL, |
864 | | rsa_alt_sign_wrap, |
865 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
866 | | NULL, |
867 | | NULL, |
868 | | #endif |
869 | | rsa_alt_decrypt_wrap, |
870 | | NULL, |
871 | | #if defined(MBEDTLS_RSA_C) |
872 | | rsa_alt_check_pair, |
873 | | #else |
874 | | NULL, |
875 | | #endif |
876 | | rsa_alt_alloc_wrap, |
877 | | rsa_alt_free_wrap, |
878 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
879 | | NULL, |
880 | | NULL, |
881 | | #endif |
882 | | NULL, |
883 | | }; |
884 | | |
885 | | #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */ |
886 | | |
887 | | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
888 | | |
889 | | static void *pk_opaque_alloc_wrap(void) |
890 | | { |
891 | | void *ctx = mbedtls_calloc(1, sizeof(psa_key_id_t)); |
892 | | |
893 | | /* no _init() function to call, as calloc() already zeroized */ |
894 | | |
895 | | return ctx; |
896 | | } |
897 | | |
898 | | static void pk_opaque_free_wrap(void *ctx) |
899 | | { |
900 | | mbedtls_platform_zeroize(ctx, sizeof(psa_key_id_t)); |
901 | | mbedtls_free(ctx); |
902 | | } |
903 | | |
904 | | static size_t pk_opaque_get_bitlen(const void *ctx) |
905 | | { |
906 | | const psa_key_id_t *key = (const psa_key_id_t *) ctx; |
907 | | size_t bits; |
908 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
909 | | |
910 | | if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) { |
911 | | return 0; |
912 | | } |
913 | | |
914 | | bits = psa_get_key_bits(&attributes); |
915 | | psa_reset_key_attributes(&attributes); |
916 | | return bits; |
917 | | } |
918 | | |
919 | | static int pk_opaque_can_do(mbedtls_pk_type_t type) |
920 | | { |
921 | | /* For now opaque PSA keys can only wrap ECC keypairs, |
922 | | * as checked by setup_psa(). |
923 | | * Also, ECKEY_DH does not really make sense with the current API. */ |
924 | | return type == MBEDTLS_PK_ECKEY || |
925 | | type == MBEDTLS_PK_ECDSA; |
926 | | } |
927 | | |
928 | | #if defined(MBEDTLS_ECDSA_C) |
929 | | |
930 | | /* |
931 | | * Simultaneously convert and move raw MPI from the beginning of a buffer |
932 | | * to an ASN.1 MPI at the end of the buffer. |
933 | | * See also mbedtls_asn1_write_mpi(). |
934 | | * |
935 | | * p: pointer to the end of the output buffer |
936 | | * start: start of the output buffer, and also of the mpi to write at the end |
937 | | * n_len: length of the mpi to read from start |
938 | | */ |
939 | | static int asn1_write_mpibuf(unsigned char **p, unsigned char *start, |
940 | | size_t n_len) |
941 | | { |
942 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
943 | | size_t len = 0; |
944 | | |
945 | | if ((size_t) (*p - start) < n_len) { |
946 | | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
947 | | } |
948 | | |
949 | | len = n_len; |
950 | | *p -= len; |
951 | | memmove(*p, start, len); |
952 | | |
953 | | /* ASN.1 DER encoding requires minimal length, so skip leading 0s. |
954 | | * Neither r nor s should be 0, but as a failsafe measure, still detect |
955 | | * that rather than overflowing the buffer in case of a PSA error. */ |
956 | | while (len > 0 && **p == 0x00) { |
957 | | ++(*p); |
958 | | --len; |
959 | | } |
960 | | |
961 | | /* this is only reached if the signature was invalid */ |
962 | | if (len == 0) { |
963 | | return MBEDTLS_ERR_PK_HW_ACCEL_FAILED; |
964 | | } |
965 | | |
966 | | /* if the msb is 1, ASN.1 requires that we prepend a 0. |
967 | | * Neither r nor s can be 0, so we can assume len > 0 at all times. */ |
968 | | if (**p & 0x80) { |
969 | | if (*p - start < 1) { |
970 | | return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL; |
971 | | } |
972 | | |
973 | | *--(*p) = 0x00; |
974 | | len += 1; |
975 | | } |
976 | | |
977 | | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
978 | | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
979 | | MBEDTLS_ASN1_INTEGER)); |
980 | | |
981 | | return (int) len; |
982 | | } |
983 | | |
984 | | /* Transcode signature from PSA format to ASN.1 sequence. |
985 | | * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of |
986 | | * MPIs, and in-place. |
987 | | * |
988 | | * [in/out] sig: the signature pre- and post-transcoding |
989 | | * [in/out] sig_len: signature length pre- and post-transcoding |
990 | | * [int] buf_len: the available size the in/out buffer |
991 | | */ |
992 | | static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len, |
993 | | size_t buf_len) |
994 | | { |
995 | | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
996 | | size_t len = 0; |
997 | | const size_t rs_len = *sig_len / 2; |
998 | | unsigned char *p = sig + buf_len; |
999 | | |
1000 | | MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len)); |
1001 | | MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len)); |
1002 | | |
1003 | | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len)); |
1004 | | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig, |
1005 | | MBEDTLS_ASN1_CONSTRUCTED | |
1006 | | MBEDTLS_ASN1_SEQUENCE)); |
1007 | | |
1008 | | memmove(sig, p, len); |
1009 | | *sig_len = len; |
1010 | | |
1011 | | return 0; |
1012 | | } |
1013 | | |
1014 | | #endif /* MBEDTLS_ECDSA_C */ |
1015 | | |
1016 | | static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg, |
1017 | | const unsigned char *hash, size_t hash_len, |
1018 | | unsigned char *sig, size_t *sig_len, |
1019 | | int (*f_rng)(void *, unsigned char *, size_t), void *p_rng) |
1020 | | { |
1021 | | #if !defined(MBEDTLS_ECDSA_C) |
1022 | | ((void) ctx); |
1023 | | ((void) md_alg); |
1024 | | ((void) hash); |
1025 | | ((void) hash_len); |
1026 | | ((void) sig); |
1027 | | ((void) sig_len); |
1028 | | ((void) f_rng); |
1029 | | ((void) p_rng); |
1030 | | return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE; |
1031 | | #else /* !MBEDTLS_ECDSA_C */ |
1032 | | const psa_key_id_t *key = (const psa_key_id_t *) ctx; |
1033 | | psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT; |
1034 | | psa_algorithm_t alg = PSA_ALG_ECDSA(mbedtls_psa_translate_md(md_alg)); |
1035 | | size_t buf_len; |
1036 | | psa_status_t status; |
1037 | | |
1038 | | /* PSA has its own RNG */ |
1039 | | (void) f_rng; |
1040 | | (void) p_rng; |
1041 | | |
1042 | | /* PSA needs an output buffer of known size, but our API doesn't provide |
1043 | | * that information. Assume that the buffer is large enough for a |
1044 | | * maximal-length signature with that key (otherwise the application is |
1045 | | * buggy anyway). */ |
1046 | | status = psa_get_key_attributes(*key, &attributes); |
1047 | | if (status != PSA_SUCCESS) { |
1048 | | return mbedtls_psa_err_translate_pk(status); |
1049 | | } |
1050 | | buf_len = MBEDTLS_ECDSA_MAX_SIG_LEN(psa_get_key_bits(&attributes)); |
1051 | | psa_reset_key_attributes(&attributes); |
1052 | | if (buf_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) { |
1053 | | return MBEDTLS_ERR_PK_BAD_INPUT_DATA; |
1054 | | } |
1055 | | |
1056 | | /* make the signature */ |
1057 | | status = psa_sign_hash(*key, alg, hash, hash_len, |
1058 | | sig, buf_len, sig_len); |
1059 | | if (status != PSA_SUCCESS) { |
1060 | | return mbedtls_psa_err_translate_pk(status); |
1061 | | } |
1062 | | |
1063 | | /* transcode it to ASN.1 sequence */ |
1064 | | return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, buf_len); |
1065 | | #endif /* !MBEDTLS_ECDSA_C */ |
1066 | | } |
1067 | | |
1068 | | const mbedtls_pk_info_t mbedtls_pk_opaque_info = { |
1069 | | MBEDTLS_PK_OPAQUE, |
1070 | | "Opaque", |
1071 | | pk_opaque_get_bitlen, |
1072 | | pk_opaque_can_do, |
1073 | | NULL, /* verify - will be done later */ |
1074 | | pk_opaque_sign_wrap, |
1075 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1076 | | NULL, /* restartable verify - not relevant */ |
1077 | | NULL, /* restartable sign - not relevant */ |
1078 | | #endif |
1079 | | NULL, /* decrypt - will be done later */ |
1080 | | NULL, /* encrypt - will be done later */ |
1081 | | NULL, /* check_pair - could be done later or left NULL */ |
1082 | | pk_opaque_alloc_wrap, |
1083 | | pk_opaque_free_wrap, |
1084 | | #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE) |
1085 | | NULL, /* restart alloc - not relevant */ |
1086 | | NULL, /* restart free - not relevant */ |
1087 | | #endif |
1088 | | NULL, /* debug - could be done later, or even left NULL */ |
1089 | | }; |
1090 | | |
1091 | | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
1092 | | |
1093 | | #endif /* MBEDTLS_PK_C */ |