1 /*
2 * Public Key abstraction layer
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.h"
12 #include "pk_wrap.h"
13 #include "pkwrite.h"
14 #include "pk_internal.h"
15
16 #include "mbedtls/platform_util.h"
17 #include "mbedtls/error.h"
18
19 #if defined(MBEDTLS_RSA_C)
20 #include "mbedtls/rsa.h"
21 #endif
22 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
23 #include "mbedtls/ecp.h"
24 #endif
25 #if defined(MBEDTLS_ECDSA_C)
26 #include "mbedtls/ecdsa.h"
27 #endif
28
29 #if defined(MBEDTLS_PSA_CRYPTO_C)
30 #include "psa_util_internal.h"
31 #include "md_psa.h"
32 #endif
33
34 #include <limits.h>
35 #include <stdint.h>
36
37 /*
38 * Initialise a mbedtls_pk_context
39 */
mbedtls_pk_init(mbedtls_pk_context * ctx)40 void mbedtls_pk_init(mbedtls_pk_context *ctx)
41 {
42 ctx->pk_info = NULL;
43 ctx->pk_ctx = NULL;
44 #if defined(MBEDTLS_PSA_CRYPTO_C)
45 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
46 #endif /* MBEDTLS_PSA_CRYPTO_C */
47 #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
48 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
49 ctx->pub_raw_len = 0;
50 ctx->ec_family = 0;
51 ctx->ec_bits = 0;
52 #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
53 }
54
55 /*
56 * Free (the components of) a mbedtls_pk_context
57 */
mbedtls_pk_free(mbedtls_pk_context * ctx)58 void mbedtls_pk_free(mbedtls_pk_context *ctx)
59 {
60 if (ctx == NULL) {
61 return;
62 }
63
64 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
65 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
66 }
67
68 #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
69 /* The ownership of the priv_id key for opaque keys is external of the PK
70 * module. It's the user responsibility to clear it after use. */
71 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
72 psa_destroy_key(ctx->priv_id);
73 }
74 #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
75
76 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
77 }
78
79 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
80 /*
81 * Initialize a restart context
82 */
mbedtls_pk_restart_init(mbedtls_pk_restart_ctx * ctx)83 void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
84 {
85 ctx->pk_info = NULL;
86 ctx->rs_ctx = NULL;
87 }
88
89 /*
90 * Free the components of a restart context
91 */
mbedtls_pk_restart_free(mbedtls_pk_restart_ctx * ctx)92 void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
93 {
94 if (ctx == NULL || ctx->pk_info == NULL ||
95 ctx->pk_info->rs_free_func == NULL) {
96 return;
97 }
98
99 ctx->pk_info->rs_free_func(ctx->rs_ctx);
100
101 ctx->pk_info = NULL;
102 ctx->rs_ctx = NULL;
103 }
104 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
105
106 /*
107 * Get pk_info structure from type
108 */
mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)109 const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
110 {
111 switch (pk_type) {
112 #if defined(MBEDTLS_RSA_C)
113 case MBEDTLS_PK_RSA:
114 return &mbedtls_rsa_info;
115 #endif /* MBEDTLS_RSA_C */
116 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
117 case MBEDTLS_PK_ECKEY:
118 return &mbedtls_eckey_info;
119 case MBEDTLS_PK_ECKEY_DH:
120 return &mbedtls_eckeydh_info;
121 #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
122 #if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
123 case MBEDTLS_PK_ECDSA:
124 return &mbedtls_ecdsa_info;
125 #endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
126 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
127 default:
128 return NULL;
129 }
130 }
131
132 /*
133 * Initialise context
134 */
mbedtls_pk_setup(mbedtls_pk_context * ctx,const mbedtls_pk_info_t * info)135 int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
136 {
137 if (info == NULL || ctx->pk_info != NULL) {
138 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
139 }
140
141 if ((info->ctx_alloc_func != NULL) &&
142 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
143 return MBEDTLS_ERR_PK_ALLOC_FAILED;
144 }
145
146 ctx->pk_info = info;
147
148 return 0;
149 }
150
151 #if defined(MBEDTLS_USE_PSA_CRYPTO)
152 /*
153 * Initialise a PSA-wrapping context
154 */
mbedtls_pk_setup_opaque(mbedtls_pk_context * ctx,const mbedtls_svc_key_id_t key)155 int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
156 const mbedtls_svc_key_id_t key)
157 {
158 const mbedtls_pk_info_t *info = NULL;
159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
160 psa_key_type_t type;
161
162 if (ctx == NULL || ctx->pk_info != NULL) {
163 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
164 }
165
166 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
167 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
168 }
169 type = psa_get_key_type(&attributes);
170 psa_reset_key_attributes(&attributes);
171
172 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
173 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
174 info = &mbedtls_ecdsa_opaque_info;
175 } else
176 #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
177 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
178 info = &mbedtls_rsa_opaque_info;
179 } else {
180 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
181 }
182
183 ctx->pk_info = info;
184 ctx->priv_id = key;
185
186 return 0;
187 }
188 #endif /* MBEDTLS_USE_PSA_CRYPTO */
189
190 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
191 /*
192 * Initialize an RSA-alt context
193 */
mbedtls_pk_setup_rsa_alt(mbedtls_pk_context * ctx,void * key,mbedtls_pk_rsa_alt_decrypt_func decrypt_func,mbedtls_pk_rsa_alt_sign_func sign_func,mbedtls_pk_rsa_alt_key_len_func key_len_func)194 int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
195 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
196 mbedtls_pk_rsa_alt_sign_func sign_func,
197 mbedtls_pk_rsa_alt_key_len_func key_len_func)
198 {
199 mbedtls_rsa_alt_context *rsa_alt;
200 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
201
202 if (ctx->pk_info != NULL) {
203 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
204 }
205
206 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
207 return MBEDTLS_ERR_PK_ALLOC_FAILED;
208 }
209
210 ctx->pk_info = info;
211
212 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
213
214 rsa_alt->key = key;
215 rsa_alt->decrypt_func = decrypt_func;
216 rsa_alt->sign_func = sign_func;
217 rsa_alt->key_len_func = key_len_func;
218
219 return 0;
220 }
221 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
222
223 /*
224 * Tell if a PK can do the operations of the given type
225 */
mbedtls_pk_can_do(const mbedtls_pk_context * ctx,mbedtls_pk_type_t type)226 int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
227 {
228 /* A context with null pk_info is not set up yet and can't do anything.
229 * For backward compatibility, also accept NULL instead of a context
230 * pointer. */
231 if (ctx == NULL || ctx->pk_info == NULL) {
232 return 0;
233 }
234
235 return ctx->pk_info->can_do(type);
236 }
237
238 #if defined(MBEDTLS_USE_PSA_CRYPTO)
239 /*
240 * Tell if a PK can do the operations of the given PSA algorithm
241 */
mbedtls_pk_can_do_ext(const mbedtls_pk_context * ctx,psa_algorithm_t alg,psa_key_usage_t usage)242 int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
243 psa_key_usage_t usage)
244 {
245 psa_key_usage_t key_usage;
246
247 /* A context with null pk_info is not set up yet and can't do anything.
248 * For backward compatibility, also accept NULL instead of a context
249 * pointer. */
250 if (ctx == NULL || ctx->pk_info == NULL) {
251 return 0;
252 }
253
254 /* Filter out non allowed algorithms */
255 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
256 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
257 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
258 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
259 PSA_ALG_IS_ECDH(alg) == 0) {
260 return 0;
261 }
262
263 /* Filter out non allowed usage flags */
264 if (usage == 0 ||
265 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
266 PSA_KEY_USAGE_DECRYPT |
267 PSA_KEY_USAGE_DERIVE)) != 0) {
268 return 0;
269 }
270
271 /* Wildcard hash is not allowed */
272 if (PSA_ALG_IS_SIGN_HASH(alg) &&
273 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
274 return 0;
275 }
276
277 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
278 mbedtls_pk_type_t type;
279
280 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
281 type = MBEDTLS_PK_ECKEY;
282 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
283 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
284 type = MBEDTLS_PK_RSA;
285 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
286 type = MBEDTLS_PK_RSASSA_PSS;
287 } else {
288 return 0;
289 }
290
291 if (ctx->pk_info->can_do(type) == 0) {
292 return 0;
293 }
294
295 switch (type) {
296 case MBEDTLS_PK_ECKEY:
297 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
298 break;
299 case MBEDTLS_PK_RSA:
300 case MBEDTLS_PK_RSASSA_PSS:
301 key_usage = PSA_KEY_USAGE_SIGN_HASH |
302 PSA_KEY_USAGE_SIGN_MESSAGE |
303 PSA_KEY_USAGE_DECRYPT;
304 break;
305 default:
306 /* Should never happen */
307 return 0;
308 }
309
310 return (key_usage & usage) == usage;
311 }
312
313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
314 psa_algorithm_t key_alg, key_alg2;
315 psa_status_t status;
316
317 status = psa_get_key_attributes(ctx->priv_id, &attributes);
318 if (status != PSA_SUCCESS) {
319 return 0;
320 }
321
322 key_alg = psa_get_key_algorithm(&attributes);
323 key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
324 key_usage = psa_get_key_usage_flags(&attributes);
325 psa_reset_key_attributes(&attributes);
326
327 if ((key_usage & usage) != usage) {
328 return 0;
329 }
330
331 /*
332 * Common case: the key alg or alg2 only allows alg.
333 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
334 * directly.
335 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
336 * a fixed hash on key_alg/key_alg2.
337 */
338 if (alg == key_alg || alg == key_alg2) {
339 return 1;
340 }
341
342 /*
343 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
344 * and alg is the same hash-and-sign family with any hash,
345 * then alg is compliant with this key alg
346 */
347 if (PSA_ALG_IS_SIGN_HASH(alg)) {
348
349 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
350 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
351 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
352 return 1;
353 }
354
355 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
356 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
357 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
358 return 1;
359 }
360 }
361
362 return 0;
363 }
364 #endif /* MBEDTLS_USE_PSA_CRYPTO */
365
366 /*
367 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
368 */
pk_hashlen_helper(mbedtls_md_type_t md_alg,size_t * hash_len)369 static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
370 {
371 if (*hash_len != 0) {
372 return 0;
373 }
374
375 *hash_len = mbedtls_md_get_size_from_type(md_alg);
376
377 if (*hash_len == 0) {
378 return -1;
379 }
380
381 return 0;
382 }
383
384 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
385 /*
386 * Helper to set up a restart context if needed
387 */
pk_restart_setup(mbedtls_pk_restart_ctx * ctx,const mbedtls_pk_info_t * info)388 static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
389 const mbedtls_pk_info_t *info)
390 {
391 /* Don't do anything if already set up or invalid */
392 if (ctx == NULL || ctx->pk_info != NULL) {
393 return 0;
394 }
395
396 /* Should never happen when we're called */
397 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
398 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
399 }
400
401 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
402 return MBEDTLS_ERR_PK_ALLOC_FAILED;
403 }
404
405 ctx->pk_info = info;
406
407 return 0;
408 }
409 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
410
411 /*
412 * Verify a signature (restartable)
413 */
mbedtls_pk_verify_restartable(mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,const unsigned char * sig,size_t sig_len,mbedtls_pk_restart_ctx * rs_ctx)414 int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
415 mbedtls_md_type_t md_alg,
416 const unsigned char *hash, size_t hash_len,
417 const unsigned char *sig, size_t sig_len,
418 mbedtls_pk_restart_ctx *rs_ctx)
419 {
420 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
421 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
422 }
423
424 if (ctx->pk_info == NULL ||
425 pk_hashlen_helper(md_alg, &hash_len) != 0) {
426 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
427 }
428
429 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
430 /* optimization: use non-restartable version if restart disabled */
431 if (rs_ctx != NULL &&
432 mbedtls_ecp_restart_is_enabled() &&
433 ctx->pk_info->verify_rs_func != NULL) {
434 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
435
436 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
437 return ret;
438 }
439
440 ret = ctx->pk_info->verify_rs_func(ctx,
441 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
442
443 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
444 mbedtls_pk_restart_free(rs_ctx);
445 }
446
447 return ret;
448 }
449 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
450 (void) rs_ctx;
451 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
452
453 if (ctx->pk_info->verify_func == NULL) {
454 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
455 }
456
457 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
458 sig, sig_len);
459 }
460
461 /*
462 * Verify a signature
463 */
mbedtls_pk_verify(mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,const unsigned char * sig,size_t sig_len)464 int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
465 const unsigned char *hash, size_t hash_len,
466 const unsigned char *sig, size_t sig_len)
467 {
468 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
469 sig, sig_len, NULL);
470 }
471
472 /*
473 * Verify a signature with options
474 */
mbedtls_pk_verify_ext(mbedtls_pk_type_t type,const void * options,mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,const unsigned char * sig,size_t sig_len)475 int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
476 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
477 const unsigned char *hash, size_t hash_len,
478 const unsigned char *sig, size_t sig_len)
479 {
480 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
481 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
482 }
483
484 if (ctx->pk_info == NULL) {
485 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
486 }
487
488 if (!mbedtls_pk_can_do(ctx, type)) {
489 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
490 }
491
492 if (type != MBEDTLS_PK_RSASSA_PSS) {
493 /* General case: no options */
494 if (options != NULL) {
495 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
496 }
497
498 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
499 }
500
501 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
503 const mbedtls_pk_rsassa_pss_options *pss_opts;
504
505 #if SIZE_MAX > UINT_MAX
506 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
507 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
508 }
509 #endif
510
511 if (options == NULL) {
512 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
513 }
514
515 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
516
517 #if defined(MBEDTLS_USE_PSA_CRYPTO)
518 if (pss_opts->mgf1_hash_id == md_alg) {
519 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
520 unsigned char *p;
521 int key_len;
522 size_t signature_length;
523 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
524 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
525
526 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
527 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
528 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
529 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
530 p = buf + sizeof(buf);
531 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
532
533 if (key_len < 0) {
534 return key_len;
535 }
536
537 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
538 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
539 psa_set_key_algorithm(&attributes, psa_sig_alg);
540
541 status = psa_import_key(&attributes,
542 buf + sizeof(buf) - key_len, key_len,
543 &key_id);
544 if (status != PSA_SUCCESS) {
545 psa_destroy_key(key_id);
546 return PSA_PK_TO_MBEDTLS_ERR(status);
547 }
548
549 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
550 * on a valid signature with trailing data in a buffer, but
551 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
552 * so for this reason the passed sig_len is overwritten. Smaller
553 * signature lengths should not be accepted for verification. */
554 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
555 mbedtls_pk_get_len(ctx) : sig_len;
556 status = psa_verify_hash(key_id, psa_sig_alg, hash,
557 hash_len, sig, signature_length);
558 destruction_status = psa_destroy_key(key_id);
559
560 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
561 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
562 }
563
564 if (status == PSA_SUCCESS) {
565 status = destruction_status;
566 }
567
568 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
569 } else
570 #endif
571 {
572 if (sig_len < mbedtls_pk_get_len(ctx)) {
573 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
574 }
575
576 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
577 md_alg, (unsigned int) hash_len, hash,
578 pss_opts->mgf1_hash_id,
579 pss_opts->expected_salt_len,
580 sig);
581 if (ret != 0) {
582 return ret;
583 }
584
585 if (sig_len > mbedtls_pk_get_len(ctx)) {
586 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
587 }
588
589 return 0;
590 }
591 #else
592 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
593 #endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
594 }
595
596 /*
597 * Make a signature (restartable)
598 */
mbedtls_pk_sign_restartable(mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,unsigned char * sig,size_t sig_size,size_t * sig_len,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng,mbedtls_pk_restart_ctx * rs_ctx)599 int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
600 mbedtls_md_type_t md_alg,
601 const unsigned char *hash, size_t hash_len,
602 unsigned char *sig, size_t sig_size, size_t *sig_len,
603 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
604 mbedtls_pk_restart_ctx *rs_ctx)
605 {
606 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
607 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
608 }
609
610 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
611 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
612 }
613
614 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
615 /* optimization: use non-restartable version if restart disabled */
616 if (rs_ctx != NULL &&
617 mbedtls_ecp_restart_is_enabled() &&
618 ctx->pk_info->sign_rs_func != NULL) {
619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
620
621 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
622 return ret;
623 }
624
625 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
626 hash, hash_len,
627 sig, sig_size, sig_len,
628 f_rng, p_rng, rs_ctx->rs_ctx);
629
630 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
631 mbedtls_pk_restart_free(rs_ctx);
632 }
633
634 return ret;
635 }
636 #else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
637 (void) rs_ctx;
638 #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
639
640 if (ctx->pk_info->sign_func == NULL) {
641 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
642 }
643
644 return ctx->pk_info->sign_func(ctx, md_alg,
645 hash, hash_len,
646 sig, sig_size, sig_len,
647 f_rng, p_rng);
648 }
649
650 /*
651 * Make a signature
652 */
mbedtls_pk_sign(mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,unsigned char * sig,size_t sig_size,size_t * sig_len,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)653 int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
654 const unsigned char *hash, size_t hash_len,
655 unsigned char *sig, size_t sig_size, size_t *sig_len,
656 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
657 {
658 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
659 sig, sig_size, sig_len,
660 f_rng, p_rng, NULL);
661 }
662
663 #if defined(MBEDTLS_PSA_CRYPTO_C)
664 /*
665 * Make a signature given a signature type.
666 */
mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,mbedtls_pk_context * ctx,mbedtls_md_type_t md_alg,const unsigned char * hash,size_t hash_len,unsigned char * sig,size_t sig_size,size_t * sig_len,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)667 int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
668 mbedtls_pk_context *ctx,
669 mbedtls_md_type_t md_alg,
670 const unsigned char *hash, size_t hash_len,
671 unsigned char *sig, size_t sig_size, size_t *sig_len,
672 int (*f_rng)(void *, unsigned char *, size_t),
673 void *p_rng)
674 {
675 #if defined(MBEDTLS_RSA_C)
676 psa_algorithm_t psa_md_alg;
677 #endif /* MBEDTLS_RSA_C */
678 *sig_len = 0;
679
680 if (ctx->pk_info == NULL) {
681 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
682 }
683
684 if (!mbedtls_pk_can_do(ctx, pk_type)) {
685 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
686 }
687
688 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
689 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
690 sig, sig_size, sig_len, f_rng, p_rng);
691 }
692
693 #if defined(MBEDTLS_RSA_C)
694 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
695 if (psa_md_alg == 0) {
696 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
697 }
698
699 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
700 psa_status_t status;
701
702 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
703 hash, hash_len,
704 sig, sig_size, sig_len);
705 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
706 }
707
708 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
709 ctx->pk_ctx, hash, hash_len,
710 sig, sig_size, sig_len);
711 #else /* MBEDTLS_RSA_C */
712 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
713 #endif /* !MBEDTLS_RSA_C */
714
715 }
716 #endif /* MBEDTLS_PSA_CRYPTO_C */
717
718 /*
719 * Decrypt message
720 */
mbedtls_pk_decrypt(mbedtls_pk_context * ctx,const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen,size_t osize,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)721 int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
722 const unsigned char *input, size_t ilen,
723 unsigned char *output, size_t *olen, size_t osize,
724 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
725 {
726 if (ctx->pk_info == NULL) {
727 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
728 }
729
730 if (ctx->pk_info->decrypt_func == NULL) {
731 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
732 }
733
734 return ctx->pk_info->decrypt_func(ctx, input, ilen,
735 output, olen, osize, f_rng, p_rng);
736 }
737
738 /*
739 * Encrypt message
740 */
mbedtls_pk_encrypt(mbedtls_pk_context * ctx,const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen,size_t osize,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)741 int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
742 const unsigned char *input, size_t ilen,
743 unsigned char *output, size_t *olen, size_t osize,
744 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
745 {
746 if (ctx->pk_info == NULL) {
747 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
748 }
749
750 if (ctx->pk_info->encrypt_func == NULL) {
751 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
752 }
753
754 return ctx->pk_info->encrypt_func(ctx, input, ilen,
755 output, olen, osize, f_rng, p_rng);
756 }
757
758 /*
759 * Check public-private key pair
760 */
mbedtls_pk_check_pair(const mbedtls_pk_context * pub,const mbedtls_pk_context * prv,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)761 int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
762 const mbedtls_pk_context *prv,
763 int (*f_rng)(void *, unsigned char *, size_t),
764 void *p_rng)
765 {
766 if (pub->pk_info == NULL ||
767 prv->pk_info == NULL) {
768 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
769 }
770
771 if (f_rng == NULL) {
772 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
773 }
774
775 if (prv->pk_info->check_pair_func == NULL) {
776 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
777 }
778
779 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
780 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
781 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
782 }
783 } else {
784 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
785 (pub->pk_info != prv->pk_info)) {
786 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
787 }
788 }
789
790 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
791 (mbedtls_pk_context *) prv,
792 f_rng, p_rng);
793 }
794
795 /*
796 * Get key size in bits
797 */
mbedtls_pk_get_bitlen(const mbedtls_pk_context * ctx)798 size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
799 {
800 /* For backward compatibility, accept NULL or a context that
801 * isn't set up yet, and return a fake value that should be safe. */
802 if (ctx == NULL || ctx->pk_info == NULL) {
803 return 0;
804 }
805
806 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
807 }
808
809 /*
810 * Export debug information
811 */
mbedtls_pk_debug(const mbedtls_pk_context * ctx,mbedtls_pk_debug_item * items)812 int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
813 {
814 if (ctx->pk_info == NULL) {
815 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
816 }
817
818 if (ctx->pk_info->debug_func == NULL) {
819 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
820 }
821
822 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
823 return 0;
824 }
825
826 /*
827 * Access the PK type name
828 */
mbedtls_pk_get_name(const mbedtls_pk_context * ctx)829 const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
830 {
831 if (ctx == NULL || ctx->pk_info == NULL) {
832 return "invalid PK";
833 }
834
835 return ctx->pk_info->name;
836 }
837
838 /*
839 * Access the PK type
840 */
mbedtls_pk_get_type(const mbedtls_pk_context * ctx)841 mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
842 {
843 if (ctx == NULL || ctx->pk_info == NULL) {
844 return MBEDTLS_PK_NONE;
845 }
846
847 return ctx->pk_info->type;
848 }
849
850 #if defined(MBEDTLS_USE_PSA_CRYPTO)
851 /*
852 * Load the key to a PSA key slot,
853 * then turn the PK context into a wrapper for that key slot.
854 *
855 * Currently only works for EC & RSA private keys.
856 */
mbedtls_pk_wrap_as_opaque(mbedtls_pk_context * pk,mbedtls_svc_key_id_t * key,psa_algorithm_t alg,psa_key_usage_t usage,psa_algorithm_t alg2)857 int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
858 mbedtls_svc_key_id_t *key,
859 psa_algorithm_t alg,
860 psa_key_usage_t usage,
861 psa_algorithm_t alg2)
862 {
863 #if !defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_RSA_C)
864 ((void) pk);
865 ((void) key);
866 ((void) alg);
867 ((void) usage);
868 ((void) alg2);
869 #else /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
870 #if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
871 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
872 size_t d_len;
873 psa_ecc_family_t curve_id;
874 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
875 psa_key_type_t key_type;
876 size_t bits;
877 psa_status_t status;
878
879 /* export the private key material in the format PSA wants */
880 #if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
881 unsigned char d[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
882 status = psa_export_key(pk->priv_id, d, sizeof(d), &d_len);
883 if (status != PSA_SUCCESS) {
884 return psa_pk_status_to_mbedtls(status);
885 }
886
887 curve_id = pk->ec_family;
888 bits = pk->ec_bits;
889 #else /* MBEDTLS_PK_USE_PSA_EC_DATA */
890 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
891 mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*pk);
892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
893
894 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
895 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
896 return ret;
897 }
898
899 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
900 #endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
901 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
902
903 /* prepare the key attributes */
904 psa_set_key_type(&attributes, key_type);
905 psa_set_key_bits(&attributes, bits);
906 psa_set_key_usage_flags(&attributes, usage);
907 psa_set_key_algorithm(&attributes, alg);
908 if (alg2 != PSA_ALG_NONE) {
909 psa_set_key_enrollment_algorithm(&attributes, alg2);
910 }
911
912 /* import private key into PSA */
913 status = psa_import_key(&attributes, d, d_len, key);
914 mbedtls_platform_zeroize(d, sizeof(d));
915 if (status != PSA_SUCCESS) {
916 return PSA_PK_TO_MBEDTLS_ERR(status);
917 }
918
919 /* make PK context wrap the key slot */
920 mbedtls_pk_free(pk);
921 mbedtls_pk_init(pk);
922
923 return mbedtls_pk_setup_opaque(pk, *key);
924 } else
925 #endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
926 #if defined(MBEDTLS_RSA_C)
927 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
928 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
929 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
930 int key_len;
931 psa_status_t status;
932
933 /* export the private key material in the format PSA wants */
934 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
935 if (key_len <= 0) {
936 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
937 }
938
939 /* prepare the key attributes */
940 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
941 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
942 psa_set_key_usage_flags(&attributes, usage);
943 psa_set_key_algorithm(&attributes, alg);
944 if (alg2 != PSA_ALG_NONE) {
945 psa_set_key_enrollment_algorithm(&attributes, alg2);
946 }
947
948 /* import private key into PSA */
949 status = psa_import_key(&attributes,
950 buf + sizeof(buf) - key_len,
951 key_len, key);
952
953 mbedtls_platform_zeroize(buf, sizeof(buf));
954
955 if (status != PSA_SUCCESS) {
956 return PSA_PK_TO_MBEDTLS_ERR(status);
957 }
958
959 /* make PK context wrap the key slot */
960 mbedtls_pk_free(pk);
961 mbedtls_pk_init(pk);
962
963 return mbedtls_pk_setup_opaque(pk, *key);
964 } else
965 #endif /* MBEDTLS_RSA_C */
966 #endif /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
967 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
968 }
969 #endif /* MBEDTLS_USE_PSA_CRYPTO */
970 #endif /* MBEDTLS_PK_C */
971