Skip to content
0.2.0-dev Download ZIP
1464 lines ·47.6 KB Raw
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 "tf_psa_crypto_common.h"
9
10#if defined(MBEDTLS_PK_C)
11#include "mbedtls/pk.h"
12#if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
13#include <mbedtls/private/pk_private.h>
14#endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
15#include "pk_wrap.h"
16#include "pkwrite.h"
17#include "pk_internal.h"
18
19#include "mbedtls/platform_util.h"
20#include "mbedtls/private/error_common.h"
21
22#include "psa_util_internal.h"
23#include "mbedtls/psa_util.h"
24
25#include <limits.h>
26#include <stdint.h>
27
28#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
29#include "mbedtls/platform.h" // for calloc/free
30#endif
31
32
33/*
34 * Initialise a mbedtls_pk_context
35 */
36void mbedtls_pk_init(mbedtls_pk_context *ctx)
37{
38 /*
39 * Note: if any of the fields needs to be initialized to non-zero,
40 * we need to add a call to this as the end of mbedtls_pk_free()!
41 */
42 ctx->pk_info = NULL;
43 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
44 ctx->psa_type = PSA_KEY_TYPE_NONE;
45 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
46 ctx->pub_raw_len = 0;
47 ctx->bits = 0;
48#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
49 ctx->ec_family = 0;
50#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
51}
52
53/*
54 * Free (the components of) a mbedtls_pk_context
55 */
56void mbedtls_pk_free(mbedtls_pk_context *ctx)
57{
58 if (ctx == NULL) {
59 return;
60 }
61
62 /* The ownership of the priv_id key for opaque keys is external of the PK
63 * module. It's the user responsibility to clear it after use. */
64 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
65 psa_destroy_key(ctx->priv_id);
66 }
67
68 /* Leaves the context in the same state as mbedtls_pk_init(). */
69 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
70}
71
72#if defined(MBEDTLS_ECP_RESTARTABLE)
73/*
74 * Initialize a restart context
75 */
76void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
77{
78 ctx->pk_info = NULL;
79 ctx->rs_ctx = NULL;
80}
81
82/*
83 * Free the components of a restart context
84 */
85void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
86{
87 if (ctx == NULL || ctx->pk_info == NULL ||
88 ctx->pk_info->rs_free_func == NULL) {
89 return;
90 }
91
92 ctx->pk_info->rs_free_func(ctx->rs_ctx);
93
94 ctx->pk_info = NULL;
95 ctx->rs_ctx = NULL;
96}
97#endif /* MBEDTLS_ECP_RESTARTABLE */
98
99/*
100 * Get pk_info structure from type
101 */
102const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
103{
104 switch (pk_type) {
105#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
106 case MBEDTLS_PK_RSA:
107 return &mbedtls_rsa_info;
108#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
109#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
110 case MBEDTLS_PK_ECKEY:
111 return &mbedtls_eckey_info;
112 case MBEDTLS_PK_ECKEY_DH:
113 return &mbedtls_eckeydh_info;
114#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
115#if defined(PSA_HAVE_ALG_SOME_ECDSA)
116 case MBEDTLS_PK_ECDSA:
117 return &mbedtls_ecdsa_info;
118#endif /* PSA_HAVE_ALG_SOME_ECDSA */
119 default:
120 return NULL;
121 }
122}
123
124/*
125 * Initialise context
126 */
127int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
128{
129 if (info == NULL || ctx->pk_info != NULL) {
130 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
131 }
132
133 ctx->pk_info = info;
134
135 return 0;
136}
137
138/*
139 * Set the public key in PK context by exporting it from the private one.
140 */
141int mbedtls_pk_set_pubkey_from_prv(mbedtls_pk_context *pk)
142{
143 psa_status_t status;
144
145 /* Public key already available in the PK context. Nothing to do. */
146 if (pk->pub_raw_len > 0) {
147 return 0;
148 }
149
150 status = psa_export_public_key(pk->priv_id, pk->pub_raw, sizeof(pk->pub_raw),
151 &pk->pub_raw_len);
152 return psa_pk_status_to_mbedtls(status);
153}
154
155/*
156 * Initialise a PSA-wrapping context
157 */
158int mbedtls_pk_wrap_psa(mbedtls_pk_context *ctx,
159 const mbedtls_svc_key_id_t key)
160{
161 const mbedtls_pk_info_t *info = NULL;
162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
163 psa_key_type_t type;
164 size_t bits;
165 int ret;
166
167 if (ctx == NULL || ctx->pk_info != NULL) {
168 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
169 }
170
171 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
172 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
173 }
174 type = psa_get_key_type(&attributes);
175 bits = psa_get_key_bits(&attributes);
176 psa_reset_key_attributes(&attributes);
177
178#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
179 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
180 info = &mbedtls_ecdsa_opaque_info;
181 } else
182#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
183 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
184 info = &mbedtls_rsa_opaque_info;
185 } else {
186 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
187 }
188
189 ctx->priv_id = key;
190
191 ret = mbedtls_pk_set_pubkey_from_prv(ctx);
192 if (ret != 0) {
193 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
194 return ret;
195 }
196
197 ctx->pk_info = info;
198 ctx->psa_type = type;
199 ctx->bits = bits;
200
201 return 0;
202}
203
204/*
205 * Tell if a PK can do the operations of the given type
206 */
207int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
208{
209 /* A context with null pk_info is not set up yet and can't do anything.
210 * For backward compatibility, also accept NULL instead of a context
211 * pointer. */
212 if (ctx == NULL || ctx->pk_info == NULL) {
213 return 0;
214 }
215
216 return ctx->pk_info->can_do(type);
217}
218
219/*
220 * Tell if a PK can do the operations of the given PSA algorithm
221 */
222int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
223 psa_key_usage_t usage)
224{
225 psa_key_usage_t key_usage;
226
227 /* A context with null pk_info is not set up yet and can't do anything.
228 * For backward compatibility, also accept NULL instead of a context
229 * pointer. */
230 if (ctx == NULL || ctx->pk_info == NULL) {
231 return 0;
232 }
233
234 /* Filter out non allowed algorithms */
235 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
236 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
237 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
238 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
239 PSA_ALG_IS_ECDH(alg) == 0) {
240 return 0;
241 }
242
243 /* Filter out non allowed usage flags */
244 if (usage == 0 ||
245 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
246 PSA_KEY_USAGE_DECRYPT |
247 PSA_KEY_USAGE_DERIVE)) != 0) {
248 return 0;
249 }
250
251 /* Wildcard hash is not allowed */
252 if (PSA_ALG_IS_SIGN_HASH(alg) &&
253 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
254 return 0;
255 }
256
257 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
258 mbedtls_pk_type_t type;
259
260 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
261 type = MBEDTLS_PK_ECKEY;
262 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
263 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
264 type = MBEDTLS_PK_RSA;
265 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
266 type = MBEDTLS_PK_RSASSA_PSS;
267 } else {
268 return 0;
269 }
270
271 if (ctx->pk_info->can_do(type) == 0) {
272 return 0;
273 }
274
275 switch (type) {
276 case MBEDTLS_PK_ECKEY:
277 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
278 break;
279 case MBEDTLS_PK_RSA:
280 case MBEDTLS_PK_RSASSA_PSS:
281 key_usage = PSA_KEY_USAGE_SIGN_HASH |
282 PSA_KEY_USAGE_SIGN_MESSAGE |
283 PSA_KEY_USAGE_DECRYPT;
284 break;
285 default:
286 /* Should never happen */
287 return 0;
288 }
289
290 return (key_usage & usage) == usage;
291 }
292
293 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
294 psa_status_t status;
295
296 status = psa_get_key_attributes(ctx->priv_id, &attributes);
297 if (status != PSA_SUCCESS) {
298 return 0;
299 }
300
301 psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
302 /* Key's enrollment is available only when an Mbed TLS implementation of PSA
303 * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
304 * Even though we don't officially support using other implementations of PSA
305 * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
306 * separated. */
307#if defined(MBEDTLS_PSA_CRYPTO_C)
308 psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
309#endif /* MBEDTLS_PSA_CRYPTO_C */
310 key_usage = psa_get_key_usage_flags(&attributes);
311 psa_reset_key_attributes(&attributes);
312
313 if ((key_usage & usage) != usage) {
314 return 0;
315 }
316
317 /*
318 * Common case: the key alg [or alg2] only allows alg.
319 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
320 * directly.
321 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
322 * a fixed hash on key_alg [or key_alg2].
323 */
324 if (alg == key_alg) {
325 return 1;
326 }
327#if defined(MBEDTLS_PSA_CRYPTO_C)
328 if (alg == key_alg2) {
329 return 1;
330 }
331#endif /* MBEDTLS_PSA_CRYPTO_C */
332
333 /*
334 * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
335 * and alg is the same hash-and-sign family with any hash,
336 * then alg is compliant with this key alg
337 */
338 if (PSA_ALG_IS_SIGN_HASH(alg)) {
339 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
340 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
341 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
342 return 1;
343 }
344#if defined(MBEDTLS_PSA_CRYPTO_C)
345 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
346 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
347 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
348 return 1;
349 }
350#endif /* MBEDTLS_PSA_CRYPTO_C */
351 }
352
353 return 0;
354}
355
356/* Check that the specified check_alg is compatible with key's type and algorithm.
357 *
358 * check_alg: the algorithm to verify compatibility for.
359 * key_type: type of key being checked.
360 * key_alg: algorithm associated with the key. This can be the main algorithm or
361 * the enrollment one, depending on which of the 2 is passed when calling
362 * this function.
363 */
364static int is_alg_compatible_with_key(psa_algorithm_t check_alg,
365 psa_key_type_t key_type,
366 psa_algorithm_t key_alg)
367{
368 /* Ensure that check_alg is compatible with key type */
369 if (PSA_KEY_TYPE_IS_ECC(key_type)) {
370 psa_ecc_family_t key_ec_family = PSA_KEY_TYPE_ECC_GET_FAMILY(key_type);
371 if (PSA_ECC_FAMILY_IS_WEIERSTRASS(key_ec_family)) {
372 if (!(PSA_ALG_IS_ECDH(check_alg) || PSA_ALG_IS_ECDSA(check_alg))) {
373 return 0;
374 }
375 } else if (key_ec_family == PSA_ECC_FAMILY_MONTGOMERY) {
376 if (!PSA_ALG_IS_ECDH(check_alg)) {
377 return 0;
378 }
379 } else if (key_ec_family == PSA_ECC_FAMILY_TWISTED_EDWARDS) {
380 if (!(PSA_ALG_IS_HASH_EDDSA(check_alg) || check_alg == PSA_ALG_PURE_EDDSA)) {
381 return 0;
382 }
383 } else {
384 return 0;
385 }
386 } else if (PSA_KEY_TYPE_IS_RSA(key_type)) {
387 if (!(PSA_ALG_IS_RSA_PKCS1V15_SIGN(check_alg) || PSA_ALG_IS_RSA_PSS(check_alg) ||
388 PSA_ALG_IS_RSA_OAEP(check_alg) || (check_alg == PSA_ALG_RSA_PKCS1V15_CRYPT))) {
389 return 0;
390 }
391 } else {
392 /* Unsupported key type */
393 return 0;
394 }
395
396 /* Simplest case: perfect match */
397 if (check_alg == key_alg) {
398 return 1;
399 }
400
401 /* Check for PSA_ALG_ANY_HASH wildcard. */
402 if (PSA_ALG_IS_SIGN_HASH(key_alg) && PSA_ALG_IS_SIGN_HASH(check_alg)) {
403 if ((PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH) &&
404 (check_alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
405 return 1;
406 }
407 }
408
409 return 0;
410}
411
412static int is_psa_key_compatible_with_alg_usage(mbedtls_svc_key_id_t key_id,
413 psa_algorithm_t alg,
414 psa_key_usage_t usage)
415{
416 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
417 psa_key_type_t key_type;
418 int ret = 0;
419
420 if (psa_get_key_attributes(key_id, &key_attr) != PSA_SUCCESS) {
421 return 0;
422 }
423
424 key_type = psa_get_key_type(&key_attr);
425
426 /* PSA_KEY_USAGE_DERIVE_PUBLIC deserves a special treatment (see the
427 * definition of the symbol for further details). Therefore we skip normal
428 * checks and only verify that the key is an ECC one and that the requested
429 * algorithm is PSA_ALG_ECDH.
430 */
431 if ((usage == PSA_KEY_USAGE_DERIVE_PUBLIC) && (alg == PSA_ALG_ECDH) &&
432 PSA_KEY_TYPE_IS_ECC(key_type)) {
433 ret = 1;
434 goto exit;
435 }
436
437 ret = ((psa_get_key_usage_flags(&key_attr) & usage) == usage);
438 if (ret == 0) {
439 goto exit;
440 }
441
442 ret = is_alg_compatible_with_key(alg, key_type, psa_get_key_algorithm(&key_attr));
443#if defined(MBEDTLS_PSA_CRYPTO_C)
444 ret |= is_alg_compatible_with_key(alg, key_type, psa_get_key_enrollment_algorithm(&key_attr));
445#endif /* MBEDTLS_PSA_CRYPTO_C */
446
447exit:
448 psa_reset_key_attributes(&key_attr);
449
450 return ret;
451}
452
453int mbedtls_pk_can_do_psa(const mbedtls_pk_context *pk, psa_algorithm_t alg,
454 psa_key_usage_t usage)
455{
456 /* A context with null pk_info is not set up yet and can't do anything. */
457 if (pk == NULL || pk->pk_info == NULL) {
458 return 0;
459 }
460
461 /* Check algorithm <-> usage compatibility. */
462 switch (usage) {
463 case PSA_KEY_USAGE_SIGN_HASH:
464 case PSA_KEY_USAGE_VERIFY_HASH:
465 if (!PSA_ALG_IS_SIGN_HASH(alg)) {
466 return 0;
467 }
468 break;
469 case PSA_KEY_USAGE_DECRYPT:
470 case PSA_KEY_USAGE_ENCRYPT:
471 if (!((alg == PSA_ALG_RSA_PKCS1V15_CRYPT) || PSA_ALG_IS_RSA_OAEP(alg))) {
472 return 0;
473 }
474 break;
475 case PSA_KEY_USAGE_DERIVE:
476 case PSA_KEY_USAGE_DERIVE_PUBLIC:
477 if (!PSA_ALG_IS_ECDH(alg)) {
478 return 0;
479 }
480 break;
481 default:
482 /* Reject unknown usages or multiple flags */
483 return 0;
484 }
485
486 /* Basic checks on private and public keys availability */
487 int has_private = !mbedtls_svc_key_id_is_null(pk->priv_id);
488 int has_public = has_private || (pk->pub_raw_len > 0);
489 int want_private = ((usage & (PSA_KEY_USAGE_SIGN_HASH |
490 PSA_KEY_USAGE_DECRYPT |
491 PSA_KEY_USAGE_DERIVE)) != 0);
492 if ((!has_public && !has_private) ||
493 (want_private && !has_private)) {
494 return 0;
495 }
496
497 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_OPAQUE) {
498 return is_psa_key_compatible_with_alg_usage(pk->priv_id, alg, usage);
499 } else if (has_private) {
500 return is_psa_key_compatible_with_alg_usage(pk->priv_id, alg, usage);
501 } else {
502 mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
503 switch (pk_type) {
504#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
505 case MBEDTLS_PK_RSA:
506 case MBEDTLS_PK_RSASSA_PSS:
507 if (PSA_ALG_IS_RSA_OAEP(alg) ||
508 PSA_ALG_IS_RSA_PSS(alg) ||
509 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
510 (alg == PSA_ALG_RSA_PKCS1V15_CRYPT)) {
511 return 1;
512 }
513 break;
514#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
515
516#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
517 case MBEDTLS_PK_ECKEY:
518 if (PSA_ALG_IS_ECDH(alg) ||
519 (PSA_ALG_IS_ECDSA(alg) && pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY)) {
520 return 1;
521 }
522 break;
523
524 case MBEDTLS_PK_ECDSA:
525 if (PSA_ALG_IS_ECDSA(alg) && pk->ec_family != PSA_ECC_FAMILY_MONTGOMERY) {
526 return 1;
527 }
528 break;
529
530 case MBEDTLS_PK_ECKEY_DH:
531 if (PSA_ALG_IS_ECDH(alg)) {
532 return 1;
533 }
534 break;
535#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
536
537 default:
538 return 0;
539 }
540 }
541
542 return 0;
543}
544
545int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
546 psa_key_usage_t usage,
547 psa_key_attributes_t *attributes)
548{
549 mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
550
551 psa_key_usage_t more_usage = usage;
552 if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) {
553 more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
554 } else if (usage == PSA_KEY_USAGE_SIGN_HASH) {
555 more_usage |= PSA_KEY_USAGE_VERIFY_HASH;
556 } else if (usage == PSA_KEY_USAGE_DECRYPT) {
557 more_usage |= PSA_KEY_USAGE_ENCRYPT;
558 }
559 more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
560
561 int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE ||
562 usage == PSA_KEY_USAGE_VERIFY_HASH ||
563 usage == PSA_KEY_USAGE_ENCRYPT);
564
565 switch (pk_type) {
566#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
567 case MBEDTLS_PK_RSA:
568 {
569 psa_algorithm_t alg = 0;
570 switch (usage) {
571 case PSA_KEY_USAGE_SIGN_MESSAGE:
572 case PSA_KEY_USAGE_SIGN_HASH:
573 case PSA_KEY_USAGE_VERIFY_MESSAGE:
574 case PSA_KEY_USAGE_VERIFY_HASH:
575 alg = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
576 break;
577 case PSA_KEY_USAGE_DECRYPT:
578 case PSA_KEY_USAGE_ENCRYPT:
579 alg = PSA_ALG_RSA_PKCS1V15_CRYPT;
580 break;
581 default:
582 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
583 }
584 int has_private = !mbedtls_svc_key_id_is_null(pk->priv_id);
585 if (want_private && !has_private) {
586 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
587 }
588 psa_set_key_type(attributes, (want_private ?
589 PSA_KEY_TYPE_RSA_KEY_PAIR :
590 PSA_KEY_TYPE_RSA_PUBLIC_KEY));
591 psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk));
592 psa_set_key_algorithm(attributes, alg);
593 break;
594 }
595#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
596
597#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
598 case MBEDTLS_PK_ECKEY:
599 case MBEDTLS_PK_ECKEY_DH:
600 case MBEDTLS_PK_ECDSA:
601 {
602 int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH);
603 int derive_ok = (pk_type != MBEDTLS_PK_ECDSA);
604 psa_ecc_family_t family = pk->ec_family;
605 size_t bits = pk->bits;
606 int has_private = 0;
607 psa_algorithm_t alg = 0;
608
609 if (!mbedtls_svc_key_id_is_null(pk->priv_id)) {
610 has_private = 1;
611 }
612 switch (usage) {
613 case PSA_KEY_USAGE_SIGN_MESSAGE:
614 case PSA_KEY_USAGE_SIGN_HASH:
615 case PSA_KEY_USAGE_VERIFY_MESSAGE:
616 case PSA_KEY_USAGE_VERIFY_HASH:
617 if (!sign_ok) {
618 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
619 }
620 alg = MBEDTLS_PK_ALG_ECDSA(PSA_ALG_ANY_HASH);
621 break;
622 case PSA_KEY_USAGE_DERIVE:
623 alg = PSA_ALG_ECDH;
624 if (!derive_ok) {
625 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
626 }
627 break;
628 default:
629 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
630 }
631 if (want_private && !has_private) {
632 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
633 }
634 psa_set_key_type(attributes, (want_private ?
635 PSA_KEY_TYPE_ECC_KEY_PAIR(family) :
636 PSA_KEY_TYPE_ECC_PUBLIC_KEY(family)));
637 psa_set_key_bits(attributes, bits);
638 psa_set_key_algorithm(attributes, alg);
639 break;
640 }
641#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
642
643 case MBEDTLS_PK_OPAQUE:
644 {
645 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
646 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
647 status = psa_get_key_attributes(pk->priv_id, &old_attributes);
648 if (status != PSA_SUCCESS) {
649 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
650 }
651 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
652 switch (usage) {
653 case PSA_KEY_USAGE_SIGN_MESSAGE:
654 case PSA_KEY_USAGE_SIGN_HASH:
655 case PSA_KEY_USAGE_VERIFY_MESSAGE:
656 case PSA_KEY_USAGE_VERIFY_HASH:
657 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) ||
658 old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) {
659 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
660 }
661 break;
662 case PSA_KEY_USAGE_DECRYPT:
663 case PSA_KEY_USAGE_ENCRYPT:
664 if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) {
665 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
666 }
667 break;
668 case PSA_KEY_USAGE_DERIVE:
669 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) {
670 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
671 }
672 break;
673 default:
674 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
675 }
676 psa_key_type_t new_type = old_type;
677 /* Opaque keys are always key pairs, so we don't need a check
678 * on the input if the required usage is private. We just need
679 * to adjust the type correctly if the required usage is public. */
680 if (!want_private) {
681 new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type);
682 }
683 more_usage = psa_get_key_usage_flags(&old_attributes);
684 if ((usage & more_usage) == 0) {
685 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
686 }
687 psa_set_key_type(attributes, new_type);
688 psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes));
689 psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes));
690 break;
691 }
692
693 default:
694 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
695 }
696
697 psa_set_key_usage_flags(attributes, more_usage);
698 /* Key's enrollment is available only when an Mbed TLS implementation of PSA
699 * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
700 * Even though we don't officially support using other implementations of PSA
701 * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
702 * separated. */
703#if defined(MBEDTLS_PSA_CRYPTO_C)
704 psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
705#endif
706
707 return 0;
708}
709
710psa_key_type_t mbedtls_pk_get_key_type(const mbedtls_pk_context *pk)
711{
712 return pk->psa_type;
713}
714
715static psa_status_t export_import_into_psa(mbedtls_svc_key_id_t old_key_id,
716 psa_key_type_t old_type, size_t old_bits,
717 const psa_key_attributes_t *attributes,
718 mbedtls_svc_key_id_t *new_key_id)
719{
720#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
721 unsigned char *key_buffer = NULL;
722 size_t key_buffer_size = 0;
723#else
724 unsigned char key_buffer[PK_EXPORT_KEY_STACK_BUFFER_SIZE];
725 const size_t key_buffer_size = sizeof(key_buffer);
726#endif
727 size_t key_length = 0;
728
729 /* We are exporting from a PK object, so we know key type is valid for PK */
730#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
731 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(old_type, old_bits);
732 key_buffer = mbedtls_calloc(1, key_buffer_size);
733 if (key_buffer == NULL) {
734 return MBEDTLS_ERR_PK_ALLOC_FAILED;
735 }
736#else
737 (void) old_type;
738 (void) old_bits;
739#endif
740
741 psa_status_t status = psa_export_key(old_key_id,
742 key_buffer, key_buffer_size,
743 &key_length);
744 if (status != PSA_SUCCESS) {
745 goto cleanup;
746 }
747 status = psa_import_key(attributes, key_buffer, key_length, new_key_id);
748 mbedtls_platform_zeroize(key_buffer, key_length);
749
750cleanup:
751#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
752 mbedtls_free(key_buffer);
753#endif
754 return status;
755}
756
757static int copy_into_psa(mbedtls_svc_key_id_t old_key_id,
758 const psa_key_attributes_t *attributes,
759 mbedtls_svc_key_id_t *new_key_id)
760{
761 /* Normally, we prefer copying: it's more efficient and works even
762 * for non-exportable keys. */
763 psa_status_t status = psa_copy_key(old_key_id, attributes, new_key_id);
764 if (status == PSA_ERROR_NOT_PERMITTED /*missing COPY usage*/ ||
765 status == PSA_ERROR_INVALID_ARGUMENT /*incompatible policy*/) {
766 /* There are edge cases where copying won't work, but export+import
767 * might:
768 * - If the old key does not allow PSA_KEY_USAGE_COPY.
769 * - If the old key's usage does not allow what attributes wants.
770 * Because the key was intended for use in the pk module, and may
771 * have had a policy chosen solely for what pk needs rather than
772 * based on a detailed understanding of PSA policies, we are a bit
773 * more liberal than psa_copy_key() here.
774 */
775 /* Here we need to check that the types match, otherwise we risk
776 * importing nonsensical data. */
777 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
778 status = psa_get_key_attributes(old_key_id, &old_attributes);
779 if (status != PSA_SUCCESS) {
780 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
781 }
782 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
783 size_t old_bits = psa_get_key_bits(&old_attributes);
784 psa_reset_key_attributes(&old_attributes);
785 if (old_type != psa_get_key_type(attributes)) {
786 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
787 }
788 status = export_import_into_psa(old_key_id, old_type, old_bits,
789 attributes, new_key_id);
790 }
791 return PSA_PK_TO_MBEDTLS_ERR(status);
792}
793
794static int import_pair_into_psa(const mbedtls_pk_context *pk,
795 const psa_key_attributes_t *attributes,
796 mbedtls_svc_key_id_t *key_id)
797{
798 switch (mbedtls_pk_get_type(pk)) {
799#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
800 case MBEDTLS_PK_RSA:
801 {
802 if (psa_get_key_type(attributes) != PSA_KEY_TYPE_RSA_KEY_PAIR) {
803 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
804 }
805 if (mbedtls_svc_key_id_is_null(pk->priv_id)) {
806 /* We have a public key and want a key pair. */
807 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
808 }
809 return copy_into_psa(pk->priv_id, attributes, key_id);
810 }
811#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
812
813#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
814 case MBEDTLS_PK_ECKEY:
815 case MBEDTLS_PK_ECKEY_DH:
816 case MBEDTLS_PK_ECDSA:
817 {
818 /* We need to check the curve family, otherwise the import could
819 * succeed with nonsensical data.
820 * We don't check the bit-size: it's optional in attributes,
821 * and if it's specified, psa_import_key() will know from the key
822 * data length and will check that the bit-size matches. */
823 psa_key_type_t to_type = psa_get_key_type(attributes);
824 psa_ecc_family_t from_family = pk->ec_family;
825 if (to_type != PSA_KEY_TYPE_ECC_KEY_PAIR(from_family)) {
826 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
827 }
828
829 if (mbedtls_svc_key_id_is_null(pk->priv_id)) {
830 /* We have a public key and want a key pair. */
831 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
832 }
833 return copy_into_psa(pk->priv_id, attributes, key_id);
834 }
835#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
836
837 case MBEDTLS_PK_OPAQUE:
838 return copy_into_psa(pk->priv_id, attributes, key_id);
839
840 default:
841 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
842 }
843}
844
845static int import_public_into_psa(const mbedtls_pk_context *pk,
846 const psa_key_attributes_t *attributes,
847 mbedtls_svc_key_id_t *key_id)
848{
849 psa_key_type_t psa_type = psa_get_key_type(attributes);
850 unsigned char key_buffer[MBEDTLS_PK_MAX_PUBKEY_RAW_LEN];
851 unsigned char *key_data = NULL;
852 size_t key_length = 0;
853
854 switch (mbedtls_pk_get_type(pk)) {
855#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
856 case MBEDTLS_PK_RSA:
857 {
858 if (psa_type != PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
859 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
860 }
861 key_data = (unsigned char *) pk->pub_raw;
862 key_length = pk->pub_raw_len;
863 break;
864 }
865#endif /*PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
866
867#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
868 case MBEDTLS_PK_ECKEY:
869 case MBEDTLS_PK_ECKEY_DH:
870 case MBEDTLS_PK_ECDSA:
871 {
872 /* We need to check the curve family, otherwise the import could
873 * succeed with nonsensical data.
874 * We don't check the bit-size: it's optional in attributes,
875 * and if it's specified, psa_import_key() will know from the key
876 * data length and will check that the bit-size matches. */
877 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)) {
878 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
879 }
880 key_data = (unsigned char *) pk->pub_raw;
881 key_length = pk->pub_raw_len;
882 break;
883 }
884#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
885
886 case MBEDTLS_PK_OPAQUE:
887 {
888 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
889 psa_status_t status =
890 psa_get_key_attributes(pk->priv_id, &old_attributes);
891 if (status != PSA_SUCCESS) {
892 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
893 }
894 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
895 psa_reset_key_attributes(&old_attributes);
896 if (psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(old_type)) {
897 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
898 }
899 status = psa_export_public_key(pk->priv_id,
900 key_buffer, sizeof(key_buffer),
901 &key_length);
902 if (status != PSA_SUCCESS) {
903 return PSA_PK_TO_MBEDTLS_ERR(status);
904 }
905 key_data = key_buffer;
906 break;
907 }
908
909 default:
910 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
911 }
912
913 return PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
914 key_data, key_length,
915 key_id));
916}
917
918int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
919 const psa_key_attributes_t *attributes,
920 mbedtls_svc_key_id_t *key_id)
921{
922 /* Set the output immediately so that it won't contain garbage even
923 * if we error out before calling psa_import_key(). */
924 *key_id = MBEDTLS_SVC_KEY_ID_INIT;
925
926 int want_public = PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(attributes));
927 if (want_public) {
928 return import_public_into_psa(pk, attributes, key_id);
929 } else {
930 return import_pair_into_psa(pk, attributes, key_id);
931 }
932}
933
934static int is_valid_for_pk(psa_key_type_t key_type)
935{
936#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
937 if (PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
938 return 1;
939 }
940#endif
941#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC)
942 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
943 return 1;
944 }
945#endif
946#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
947 if (key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
948 return 1;
949 }
950#endif
951#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
952 if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
953 return 1;
954 }
955#endif
956 return 0;
957}
958
959static int copy_from_psa(mbedtls_svc_key_id_t key_id,
960 mbedtls_pk_context *pk,
961 int public_only)
962{
963 psa_status_t status;
964 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
965 psa_key_type_t key_type;
966 size_t key_bits;
967#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
968 unsigned char *exp_key = NULL;
969 size_t exp_key_size = 0;
970#else
971 unsigned char exp_key[PK_EXPORT_KEY_STACK_BUFFER_SIZE];
972 const size_t exp_key_size = sizeof(exp_key);
973#endif
974 size_t exp_key_len;
975 int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
976
977 if (pk == NULL) {
978 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
979 }
980
981 status = psa_get_key_attributes(key_id, &key_attr);
982 if (status != PSA_SUCCESS) {
983 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
984 }
985
986 key_type = psa_get_key_type(&key_attr);
987 if (!is_valid_for_pk(key_type)) {
988 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
989 }
990
991 if (public_only) {
992 key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
993 }
994 key_bits = psa_get_key_bits(&key_attr);
995
996#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
997 exp_key_size = PSA_EXPORT_KEY_OUTPUT_SIZE(key_type, key_bits);
998 exp_key = mbedtls_calloc(1, exp_key_size);
999 if (exp_key == NULL) {
1000 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1001 }
1002#endif
1003
1004 if (public_only) {
1005 status = psa_export_public_key(key_id, exp_key, exp_key_size, &exp_key_len);
1006 } else {
1007 status = psa_export_key(key_id, exp_key, exp_key_size, &exp_key_len);
1008 }
1009 if (status != PSA_SUCCESS) {
1010 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1011 goto exit;
1012 }
1013
1014 pk->psa_type = key_type;
1015
1016#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
1017 if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) ||
1018 (key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) {
1019
1020 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
1021 if (ret != 0) {
1022 goto exit;
1023 }
1024
1025 if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
1026 ret = mbedtls_pk_rsa_set_key(pk, exp_key, exp_key_len);
1027 if (ret != 0) {
1028 goto exit;
1029 }
1030 ret = mbedtls_pk_set_pubkey_from_prv(pk);
1031 } else {
1032 ret = mbedtls_pk_rsa_set_pubkey(pk, exp_key, exp_key_len);
1033 }
1034 if (ret != 0) {
1035 goto exit;
1036 }
1037 } else
1038#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
1039#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
1040 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ||
1041 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
1042 mbedtls_ecp_group_id grp_id;
1043
1044 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
1045 if (ret != 0) {
1046 goto exit;
1047 }
1048
1049 grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits);
1050 ret = mbedtls_pk_ecc_set_group(pk, grp_id);
1051 if (ret != 0) {
1052 goto exit;
1053 }
1054
1055 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
1056 ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len);
1057 if (ret != 0) {
1058 goto exit;
1059 }
1060 ret = mbedtls_pk_set_pubkey_from_prv(pk);
1061 } else {
1062 ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len);
1063 }
1064 if (ret != 0) {
1065 goto exit;
1066 }
1067 } else
1068#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
1069 {
1070 (void) key_bits;
1071 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1072 goto exit;
1073 }
1074
1075exit:
1076 mbedtls_platform_zeroize(exp_key, exp_key_size);
1077#if !defined(PK_EXPORT_KEYS_ON_THE_STACK)
1078 mbedtls_free(exp_key);
1079#endif
1080 psa_reset_key_attributes(&key_attr);
1081
1082 return ret;
1083}
1084
1085int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id,
1086 mbedtls_pk_context *pk)
1087{
1088 return copy_from_psa(key_id, pk, 0);
1089}
1090
1091int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id,
1092 mbedtls_pk_context *pk)
1093{
1094 return copy_from_psa(key_id, pk, 1);
1095}
1096
1097/*
1098 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
1099 */
1100static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
1101{
1102 if (*hash_len != 0) {
1103 return 0;
1104 }
1105
1106 *hash_len = mbedtls_md_get_size_from_type(md_alg);
1107
1108 if (*hash_len == 0) {
1109 return -1;
1110 }
1111
1112 return 0;
1113}
1114
1115#if defined(MBEDTLS_ECP_RESTARTABLE)
1116/*
1117 * Helper to set up a restart context if needed
1118 */
1119static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
1120 const mbedtls_pk_info_t *info,
1121 mbedtls_pk_rs_op_t rs_op)
1122{
1123 /* Don't do anything if already set up or invalid */
1124 if (ctx == NULL || ctx->pk_info != NULL) {
1125 return 0;
1126 }
1127
1128 /* Should never happen when we're called */
1129 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
1130 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1131 }
1132
1133 if ((ctx->rs_ctx = info->rs_alloc_func(rs_op)) == NULL) {
1134 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1135 }
1136
1137 ctx->pk_info = info;
1138
1139 return 0;
1140}
1141#endif /* MBEDTLS_ECP_RESTARTABLE */
1142
1143/*
1144 * Verify a signature (restartable)
1145 */
1146int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
1147 mbedtls_md_type_t md_alg,
1148 const unsigned char *hash, size_t hash_len,
1149 const unsigned char *sig, size_t sig_len,
1150 mbedtls_pk_restart_ctx *rs_ctx)
1151{
1152 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
1153 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1154 }
1155
1156 if (ctx->pk_info == NULL ||
1157 pk_hashlen_helper(md_alg, &hash_len) != 0) {
1158 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1159 }
1160
1161#if defined(MBEDTLS_ECP_RESTARTABLE)
1162 int is_restartable_enabled = psa_interruptible_get_max_ops() != 0;
1163 /* optimization: use non-restartable version if restart disabled */
1164 if (rs_ctx != NULL &&
1165 is_restartable_enabled &&
1166 ctx->pk_info->verify_rs_func != NULL) {
1167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1168
1169 ret = pk_restart_setup(rs_ctx, ctx->pk_info, MBEDTLS_PK_RS_OP_VERIFY);
1170 if (ret != 0) {
1171 return ret;
1172 }
1173
1174 ret = ctx->pk_info->verify_rs_func(ctx,
1175 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
1176
1177 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1178 mbedtls_pk_restart_free(rs_ctx);
1179 }
1180
1181 return ret;
1182 }
1183#else /* MBEDTLS_ECP_RESTARTABLE */
1184 (void) rs_ctx;
1185#endif /* MBEDTLS_ECP_RESTARTABLE */
1186
1187 if (ctx->pk_info->verify_func == NULL) {
1188 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1189 }
1190
1191 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
1192 sig, sig_len);
1193}
1194
1195/*
1196 * Verify a signature
1197 */
1198int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1199 const unsigned char *hash, size_t hash_len,
1200 const unsigned char *sig, size_t sig_len)
1201{
1202 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
1203 sig, sig_len, NULL);
1204}
1205
1206/*
1207 * Verify a signature, with explicit selection of the signature algorithm.
1208 */
1209int mbedtls_pk_verify_ext(mbedtls_pk_sigalg_t type,
1210 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1211 const unsigned char *hash, size_t hash_len,
1212 const unsigned char *sig, size_t sig_len)
1213{
1214 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
1215 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1216 }
1217
1218 if (ctx->pk_info == NULL) {
1219 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1220 }
1221
1222 if (!mbedtls_pk_can_do(ctx, (mbedtls_pk_type_t) type)) {
1223 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1224 }
1225
1226 if (type != MBEDTLS_PK_SIGALG_RSA_PSS) {
1227 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
1228 }
1229
1230 /* Ensure the PK context is of the right type. */
1231 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_RSA) {
1232 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1233 }
1234
1235#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
1236
1237#if SIZE_MAX > UINT_MAX
1238 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
1239 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1240 }
1241#endif
1242
1243 size_t signature_length;
1244 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
1245 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
1246
1247 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
1248 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1249 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1250 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
1251
1252 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
1253 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1254 psa_set_key_algorithm(&attributes, psa_sig_alg);
1255
1256 status = psa_import_key(&attributes,
1257 ctx->pub_raw, ctx->pub_raw_len,
1258 &key_id);
1259 if (status != PSA_SUCCESS) {
1260 psa_destroy_key(key_id);
1261 return PSA_PK_TO_MBEDTLS_ERR(status);
1262 }
1263
1264 /* This function must fail on a valid signature with trailing data in a
1265 * buffer (checked below). Moreover mbedtls_psa_rsa_verify_hash() requires
1266 * the sig_len to be exact. For this reason the passed sig_len is
1267 * overwritten. Smaller signature lengths should not be accepted for
1268 * verification. */
1269 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
1270 mbedtls_pk_get_len(ctx) : sig_len;
1271 status = psa_verify_hash(key_id, psa_sig_alg, hash,
1272 hash_len, sig, signature_length);
1273 destruction_status = psa_destroy_key(key_id);
1274
1275 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
1276 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1277 }
1278
1279 if (status == PSA_SUCCESS) {
1280 status = destruction_status;
1281 }
1282
1283 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1284#else
1285 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1286#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
1287}
1288
1289/*
1290 * Make a signature (restartable)
1291 */
1292int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
1293 mbedtls_md_type_t md_alg,
1294 const unsigned char *hash, size_t hash_len,
1295 unsigned char *sig, size_t sig_size, size_t *sig_len,
1296 mbedtls_pk_restart_ctx *rs_ctx)
1297{
1298 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
1299 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1300 }
1301
1302 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
1303 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1304 }
1305
1306#if defined(MBEDTLS_ECP_RESTARTABLE)
1307 int is_restartable_enabled = psa_interruptible_get_max_ops() != 0;
1308 /* optimization: use non-restartable version if restart disabled */
1309 if (rs_ctx != NULL &&
1310 is_restartable_enabled &&
1311 ctx->pk_info->sign_rs_func != NULL) {
1312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1313
1314 ret = pk_restart_setup(rs_ctx, ctx->pk_info, MBEDTLS_PK_RS_OP_SIGN);
1315 if (ret != 0) {
1316 return ret;
1317 }
1318
1319 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
1320 hash, hash_len,
1321 sig, sig_size, sig_len,
1322 rs_ctx->rs_ctx);
1323
1324 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1325 mbedtls_pk_restart_free(rs_ctx);
1326 }
1327
1328 return ret;
1329 }
1330#else /* MBEDTLS_ECP_RESTARTABLE */
1331 (void) rs_ctx;
1332#endif /* MBEDTLS_ECP_RESTARTABLE */
1333
1334 if (ctx->pk_info->sign_func == NULL) {
1335 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1336 }
1337
1338 return ctx->pk_info->sign_func(ctx, md_alg,
1339 hash, hash_len,
1340 sig, sig_size, sig_len);
1341}
1342
1343/*
1344 * Make a signature
1345 */
1346int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1347 const unsigned char *hash, size_t hash_len,
1348 unsigned char *sig, size_t sig_size, size_t *sig_len)
1349{
1350 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
1351 sig, sig_size, sig_len,
1352 NULL);
1353}
1354
1355/*
1356 * Make a signature given a signature type.
1357 */
1358int mbedtls_pk_sign_ext(mbedtls_pk_sigalg_t pk_type,
1359 mbedtls_pk_context *ctx,
1360 mbedtls_md_type_t md_alg,
1361 const unsigned char *hash, size_t hash_len,
1362 unsigned char *sig, size_t sig_size, size_t *sig_len)
1363{
1364 if (ctx->pk_info == NULL) {
1365 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1366 }
1367
1368 if (!mbedtls_pk_can_do(ctx, (mbedtls_pk_type_t) pk_type)) {
1369 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1370 }
1371
1372 if (pk_type != MBEDTLS_PK_SIGALG_RSA_PSS) {
1373 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
1374 sig, sig_size, sig_len);
1375 }
1376
1377#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY)
1378 const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
1379 if (psa_md_alg == 0) {
1380 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1381 }
1382
1383 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
1384 psa_status_t status;
1385
1386 /* PSA_ALG_RSA_PSS() behaves the same as PSA_ALG_RSA_PSS_ANY_SALT() when
1387 * performing a signature, but they are encoded differently. Instead of
1388 * extracting the proper one from the wrapped key policy, just try both. */
1389 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
1390 hash, hash_len,
1391 sig, sig_size, sig_len);
1392 if (status == PSA_ERROR_NOT_PERMITTED) {
1393 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg),
1394 hash, hash_len,
1395 sig, sig_size, sig_len);
1396 }
1397 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1398 }
1399
1400 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
1401 ctx, hash, hash_len,
1402 sig, sig_size, sig_len);
1403#else
1404 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1405#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
1406}
1407
1408/*
1409 * Check public-private key pair
1410 */
1411int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
1412 const mbedtls_pk_context *prv)
1413{
1414 /* Check for a valid context */
1415 if (pub->pk_info == NULL ||
1416 prv->pk_info == NULL ||
1417 pub->pub_raw_len == 0 ||
1418 prv->pub_raw_len == 0) {
1419 return PSA_ERROR_INVALID_ARGUMENT;
1420 }
1421
1422 /* Check types */
1423 if (!PSA_KEY_TYPE_IS_KEY_PAIR(prv->psa_type) ||
1424 pub->psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(prv->psa_type)) {
1425 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1426 }
1427
1428 /* Check input data */
1429 if ((mbedtls_pk_get_bitlen(pub) != mbedtls_pk_get_bitlen(prv)) ||
1430 prv->pub_raw_len != pub->pub_raw_len ||
1431 memcmp(prv->pub_raw, pub->pub_raw, prv->pub_raw_len) != 0) {
1432 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1433 }
1434
1435 /* return 0 on match */
1436 return 0;
1437}
1438
1439/*
1440 * Get key size in bits
1441 */
1442size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
1443{
1444 /* For backward compatibility, accept NULL or a context that
1445 * isn't set up yet, and return a fake value that should be safe. */
1446 if (ctx == NULL || ctx->pk_info == NULL) {
1447 return 0;
1448 }
1449 return ctx->bits;
1450}
1451
1452/*
1453 * Access the PK type
1454 */
1455mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
1456{
1457 if (ctx == NULL || ctx->pk_info == NULL) {
1458 return MBEDTLS_PK_NONE;
1459 }
1460
1461 return ctx->pk_info->type;
1462}
1463
1464#endif /* MBEDTLS_PK_C */