2 * Public Key abstraction layer: wrapper functions
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #if defined(MBEDTLS_PK_C)
23 #include "mbedtls/pk_internal.h"
24 #include "mbedtls/error.h"
26 /* Even if RSA not activated, for the sake of RSA-alt */
27 #include "mbedtls/rsa.h"
31 #if defined(MBEDTLS_ECP_C)
32 #include "mbedtls/ecp.h"
35 #if defined(MBEDTLS_ECDSA_C)
36 #include "mbedtls/ecdsa.h"
39 #if defined(MBEDTLS_USE_PSA_CRYPTO)
40 #include "mbedtls/asn1write.h"
43 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
44 #include "mbedtls/platform_util.h"
47 #if defined(MBEDTLS_USE_PSA_CRYPTO)
48 #include "psa/crypto.h"
49 #include "mbedtls/psa_util.h"
50 #include "mbedtls/asn1.h"
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
57 #define mbedtls_calloc calloc
58 #define mbedtls_free free
64 #if defined(MBEDTLS_RSA_C)
65 static int rsa_can_do(mbedtls_pk_type_t type
) {
66 return (type
== MBEDTLS_PK_RSA
||
67 type
== MBEDTLS_PK_RSASSA_PSS
);
70 static size_t rsa_get_bitlen(const void *ctx
) {
71 const mbedtls_rsa_context
*rsa
= (const mbedtls_rsa_context
*) ctx
;
72 return (8 * mbedtls_rsa_get_len(rsa
));
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 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
79 mbedtls_rsa_context
*rsa
= (mbedtls_rsa_context
*) ctx
;
80 size_t rsa_len
= mbedtls_rsa_get_len(rsa
);
82 #if SIZE_MAX > UINT_MAX
83 if (md_alg
== MBEDTLS_MD_NONE
&& UINT_MAX
< hash_len
)
84 return (MBEDTLS_ERR_PK_BAD_INPUT_DATA
);
85 #endif /* SIZE_MAX > UINT_MAX */
87 if (sig_len
< rsa_len
)
88 return (MBEDTLS_ERR_RSA_VERIFY_FAILED
);
90 if ((ret
= mbedtls_rsa_pkcs1_verify(rsa
, NULL
, NULL
,
91 MBEDTLS_RSA_PUBLIC
, md_alg
,
92 (unsigned int) hash_len
, hash
, sig
)) != 0)
95 /* The buffer contains a valid signature followed by extra data.
96 * We have a special error code for that so that so that callers can
97 * use mbedtls_pk_verify() to check "Does the buffer start with a
98 * valid signature?" and not just "Does the buffer contain a valid
100 if (sig_len
> rsa_len
)
101 return (MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
);
106 static int rsa_sign_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
107 const unsigned char *hash
, size_t hash_len
,
108 unsigned char *sig
, size_t *sig_len
,
109 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
) {
110 mbedtls_rsa_context
*rsa
= (mbedtls_rsa_context
*) ctx
;
112 #if SIZE_MAX > UINT_MAX
113 if (md_alg
== MBEDTLS_MD_NONE
&& UINT_MAX
< hash_len
)
114 return (MBEDTLS_ERR_PK_BAD_INPUT_DATA
);
115 #endif /* SIZE_MAX > UINT_MAX */
117 *sig_len
= mbedtls_rsa_get_len(rsa
);
119 return (mbedtls_rsa_pkcs1_sign(rsa
, f_rng
, p_rng
, MBEDTLS_RSA_PRIVATE
,
120 md_alg
, (unsigned int) hash_len
, hash
, sig
));
123 static int rsa_decrypt_wrap(void *ctx
,
124 const unsigned char *input
, size_t ilen
,
125 unsigned char *output
, size_t *olen
, size_t osize
,
126 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
) {
127 mbedtls_rsa_context
*rsa
= (mbedtls_rsa_context
*) ctx
;
129 if (ilen
!= mbedtls_rsa_get_len(rsa
))
130 return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA
);
132 return (mbedtls_rsa_pkcs1_decrypt(rsa
, f_rng
, p_rng
,
133 MBEDTLS_RSA_PRIVATE
, olen
, input
, output
, osize
));
136 static int rsa_encrypt_wrap(void *ctx
,
137 const unsigned char *input
, size_t ilen
,
138 unsigned char *output
, size_t *olen
, size_t osize
,
139 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
) {
140 mbedtls_rsa_context
*rsa
= (mbedtls_rsa_context
*) ctx
;
141 *olen
= mbedtls_rsa_get_len(rsa
);
144 return (MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE
);
146 return (mbedtls_rsa_pkcs1_encrypt(rsa
, f_rng
, p_rng
, MBEDTLS_RSA_PUBLIC
,
147 ilen
, input
, output
));
150 static int rsa_check_pair_wrap(const void *pub
, const void *prv
) {
151 return (mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context
*) pub
,
152 (const mbedtls_rsa_context
*) prv
));
155 static void *rsa_alloc_wrap(void) {
156 void *ctx
= mbedtls_calloc(1, sizeof(mbedtls_rsa_context
));
159 mbedtls_rsa_init((mbedtls_rsa_context
*) ctx
, 0, 0);
164 static void rsa_free_wrap(void *ctx
) {
165 mbedtls_rsa_free((mbedtls_rsa_context
*) ctx
);
169 static void rsa_debug(const void *ctx
, mbedtls_pk_debug_item
*items
) {
170 items
->type
= MBEDTLS_PK_DEBUG_MPI
;
171 items
->name
= "rsa.N";
172 items
->value
= &(((mbedtls_rsa_context
*) ctx
)->N
);
176 items
->type
= MBEDTLS_PK_DEBUG_MPI
;
177 items
->name
= "rsa.E";
178 items
->value
= &(((mbedtls_rsa_context
*) ctx
)->E
);
181 const mbedtls_pk_info_t mbedtls_rsa_info
= {
188 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
197 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
203 #endif /* MBEDTLS_RSA_C */
205 #if defined(MBEDTLS_ECP_C)
209 static int eckey_can_do(mbedtls_pk_type_t type
) {
210 return (type
== MBEDTLS_PK_ECKEY
||
211 type
== MBEDTLS_PK_ECKEY_DH
||
212 type
== MBEDTLS_PK_ECDSA
);
215 static size_t eckey_get_bitlen(const void *ctx
) {
216 return (((mbedtls_ecp_keypair
*) ctx
)->grp
.pbits
);
219 #if defined(MBEDTLS_ECDSA_C)
220 /* Forward declarations */
221 static int ecdsa_verify_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
222 const unsigned char *hash
, size_t hash_len
,
223 const unsigned char *sig
, size_t sig_len
);
225 static int ecdsa_sign_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
226 const unsigned char *hash
, size_t hash_len
,
227 unsigned char *sig
, size_t *sig_len
,
228 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
);
230 static int eckey_verify_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
231 const unsigned char *hash
, size_t hash_len
,
232 const unsigned char *sig
, size_t sig_len
) {
233 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
234 mbedtls_ecdsa_context ecdsa
;
236 mbedtls_ecdsa_init(&ecdsa
);
238 if ((ret
= mbedtls_ecdsa_from_keypair(&ecdsa
, ctx
)) == 0)
239 ret
= ecdsa_verify_wrap(&ecdsa
, md_alg
, hash
, hash_len
, sig
, sig_len
);
241 mbedtls_ecdsa_free(&ecdsa
);
246 static int eckey_sign_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
247 const unsigned char *hash
, size_t hash_len
,
248 unsigned char *sig
, size_t *sig_len
,
249 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
) {
250 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
251 mbedtls_ecdsa_context ecdsa
;
253 mbedtls_ecdsa_init(&ecdsa
);
255 if ((ret
= mbedtls_ecdsa_from_keypair(&ecdsa
, ctx
)) == 0)
256 ret
= ecdsa_sign_wrap(&ecdsa
, md_alg
, hash
, hash_len
, sig
, sig_len
,
259 mbedtls_ecdsa_free(&ecdsa
);
264 #if defined(MBEDTLS_ECP_RESTARTABLE)
265 /* Forward declarations */
266 static int ecdsa_verify_rs_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
267 const unsigned char *hash
, size_t hash_len
,
268 const unsigned char *sig
, size_t sig_len
,
271 static int ecdsa_sign_rs_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
272 const unsigned char *hash
, size_t hash_len
,
273 unsigned char *sig
, size_t *sig_len
,
274 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
,
278 * Restart context for ECDSA operations with ECKEY context
280 * We need to store an actual ECDSA context, as we need to pass the same to
281 * the underlying ecdsa function, so we can't create it on the fly every time.
284 mbedtls_ecdsa_restart_ctx ecdsa_rs
;
285 mbedtls_ecdsa_context ecdsa_ctx
;
288 static void *eckey_rs_alloc(void) {
289 eckey_restart_ctx
*rs_ctx
;
291 void *ctx
= mbedtls_calloc(1, sizeof(eckey_restart_ctx
));
295 mbedtls_ecdsa_restart_init(&rs_ctx
->ecdsa_rs
);
296 mbedtls_ecdsa_init(&rs_ctx
->ecdsa_ctx
);
302 static void eckey_rs_free(void *ctx
) {
303 eckey_restart_ctx
*rs_ctx
;
309 mbedtls_ecdsa_restart_free(&rs_ctx
->ecdsa_rs
);
310 mbedtls_ecdsa_free(&rs_ctx
->ecdsa_ctx
);
315 static int eckey_verify_rs_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
316 const unsigned char *hash
, size_t hash_len
,
317 const unsigned char *sig
, size_t sig_len
,
319 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
320 eckey_restart_ctx
*rs
= rs_ctx
;
322 /* Should never happen */
324 return (MBEDTLS_ERR_PK_BAD_INPUT_DATA
);
326 /* set up our own sub-context if needed (that is, on first run) */
327 if (rs
->ecdsa_ctx
.grp
.pbits
== 0)
328 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs
->ecdsa_ctx
, ctx
));
330 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs
->ecdsa_ctx
,
331 md_alg
, hash
, hash_len
,
332 sig
, sig_len
, &rs
->ecdsa_rs
));
338 static int eckey_sign_rs_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
339 const unsigned char *hash
, size_t hash_len
,
340 unsigned char *sig
, size_t *sig_len
,
341 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
,
343 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
344 eckey_restart_ctx
*rs
= rs_ctx
;
346 /* Should never happen */
348 return (MBEDTLS_ERR_PK_BAD_INPUT_DATA
);
350 /* set up our own sub-context if needed (that is, on first run) */
351 if (rs
->ecdsa_ctx
.grp
.pbits
== 0)
352 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs
->ecdsa_ctx
, ctx
));
354 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs
->ecdsa_ctx
, md_alg
,
355 hash
, hash_len
, sig
, sig_len
,
356 f_rng
, p_rng
, &rs
->ecdsa_rs
));
361 #endif /* MBEDTLS_ECP_RESTARTABLE */
362 #endif /* MBEDTLS_ECDSA_C */
364 static int eckey_check_pair(const void *pub
, const void *prv
) {
365 return (mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair
*) pub
,
366 (const mbedtls_ecp_keypair
*) prv
));
369 static void *eckey_alloc_wrap(void) {
370 void *ctx
= mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair
));
373 mbedtls_ecp_keypair_init(ctx
);
378 static void eckey_free_wrap(void *ctx
) {
379 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair
*) ctx
);
383 static void eckey_debug(const void *ctx
, mbedtls_pk_debug_item
*items
) {
384 items
->type
= MBEDTLS_PK_DEBUG_ECP
;
385 items
->name
= "eckey.Q";
386 items
->value
= &(((mbedtls_ecp_keypair
*) ctx
)->Q
);
389 const mbedtls_pk_info_t mbedtls_eckey_info
= {
394 #if defined(MBEDTLS_ECDSA_C)
397 #if defined(MBEDTLS_ECP_RESTARTABLE)
398 eckey_verify_rs_wrap
,
401 #else /* MBEDTLS_ECDSA_C */
404 #endif /* MBEDTLS_ECDSA_C */
410 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
418 * EC key restricted to ECDH
420 static int eckeydh_can_do(mbedtls_pk_type_t type
) {
421 return (type
== MBEDTLS_PK_ECKEY
||
422 type
== MBEDTLS_PK_ECKEY_DH
);
425 const mbedtls_pk_info_t mbedtls_eckeydh_info
= {
428 eckey_get_bitlen
, /* Same underlying key structure */
432 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
439 eckey_alloc_wrap
, /* Same underlying key structure */
440 eckey_free_wrap
, /* Same underlying key structure */
441 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
445 eckey_debug
, /* Same underlying key structure */
447 #endif /* MBEDTLS_ECP_C */
449 #if defined(MBEDTLS_ECDSA_C)
450 static int ecdsa_can_do(mbedtls_pk_type_t type
) {
451 return (type
== MBEDTLS_PK_ECDSA
);
454 #if defined(MBEDTLS_USE_PSA_CRYPTO)
456 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
457 * those integers and convert it to the fixed-length encoding expected by PSA.
459 static int extract_ecdsa_sig_int(unsigned char **from
, const unsigned char *end
,
460 unsigned char *to
, size_t to_len
) {
461 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
462 size_t unpadded_len
, padding_len
;
464 if ((ret
= mbedtls_asn1_get_tag(from
, end
, &unpadded_len
,
465 MBEDTLS_ASN1_INTEGER
)) != 0) {
469 while (unpadded_len
> 0 && **from
== 0x00) {
474 if (unpadded_len
> to_len
|| unpadded_len
== 0)
475 return (MBEDTLS_ERR_ASN1_LENGTH_MISMATCH
);
477 padding_len
= to_len
- unpadded_len
;
478 memset(to
, 0x00, padding_len
);
479 memcpy(to
+ padding_len
, *from
, unpadded_len
);
480 (*from
) += unpadded_len
;
486 * Convert a signature from an ASN.1 sequence of two integers
487 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
488 * twice as big as int_size.
490 static int extract_ecdsa_sig(unsigned char **p
, const unsigned char *end
,
491 unsigned char *sig
, size_t int_size
) {
492 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
495 if ((ret
= mbedtls_asn1_get_tag(p
, end
, &tmp_size
,
496 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
)) != 0)
500 if ((ret
= extract_ecdsa_sig_int(p
, end
, sig
, int_size
)) != 0)
503 if ((ret
= extract_ecdsa_sig_int(p
, end
, sig
+ int_size
, int_size
)) != 0)
509 static int ecdsa_verify_wrap(void *ctx_arg
, mbedtls_md_type_t md_alg
,
510 const unsigned char *hash
, size_t hash_len
,
511 const unsigned char *sig
, size_t sig_len
) {
512 mbedtls_ecdsa_context
*ctx
= ctx_arg
;
513 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
514 psa_key_attributes_t attributes
= PSA_KEY_ATTRIBUTES_INIT
;
515 psa_key_id_t key_id
= 0;
517 mbedtls_pk_context key
;
519 /* see ECP_PUB_DER_MAX_BYTES in pkwrite.c */
520 unsigned char buf
[30 + 2 * MBEDTLS_ECP_MAX_BYTES
];
522 mbedtls_pk_info_t pk_info
= mbedtls_eckey_info
;
523 psa_algorithm_t psa_sig_md
= PSA_ALG_ECDSA_ANY
;
525 psa_ecc_family_t curve
=
526 mbedtls_ecc_group_to_psa(ctx
->grp
.id
, &curve_bits
);
527 const size_t signature_part_size
= (ctx
->grp
.nbits
+ 7) / 8;
531 return (MBEDTLS_ERR_PK_BAD_INPUT_DATA
);
533 /* mbedtls_pk_write_pubkey() expects a full PK context;
534 * re-construct one to make it happy */
535 key
.pk_info
= &pk_info
;
537 p
= buf
+ sizeof(buf
);
538 key_len
= mbedtls_pk_write_pubkey(&p
, buf
, &key
);
540 return (MBEDTLS_ERR_PK_BAD_INPUT_DATA
);
542 psa_set_key_type(&attributes
, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve
));
543 psa_set_key_usage_flags(&attributes
, PSA_KEY_USAGE_VERIFY_HASH
);
544 psa_set_key_algorithm(&attributes
, psa_sig_md
);
546 status
= psa_import_key(&attributes
,
547 buf
+ sizeof(buf
) - key_len
, key_len
,
549 if (status
!= PSA_SUCCESS
) {
550 ret
= mbedtls_psa_err_translate_pk(status
);
554 /* We don't need the exported key anymore and can
555 * reuse its buffer for signature extraction. */
556 if (2 * signature_part_size
> sizeof(buf
)) {
557 ret
= MBEDTLS_ERR_PK_BAD_INPUT_DATA
;
561 p
= (unsigned char *) sig
;
562 if ((ret
= extract_ecdsa_sig(&p
, sig
+ sig_len
, buf
,
563 signature_part_size
)) != 0) {
567 if (psa_verify_hash(key_id
, psa_sig_md
,
569 buf
, 2 * signature_part_size
)
571 ret
= MBEDTLS_ERR_ECP_VERIFY_FAILED
;
575 if (p
!= sig
+ sig_len
) {
576 ret
= MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
;
582 psa_destroy_key(key_id
);
585 #else /* MBEDTLS_USE_PSA_CRYPTO */
586 static int ecdsa_verify_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
587 const unsigned char *hash
, size_t hash_len
,
588 const unsigned char *sig
, size_t sig_len
) {
589 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
592 ret
= mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context
*) ctx
,
593 hash
, hash_len
, sig
, sig_len
);
595 if (ret
== MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH
)
596 return (MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
);
600 #endif /* MBEDTLS_USE_PSA_CRYPTO */
602 static int ecdsa_sign_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
603 const unsigned char *hash
, size_t hash_len
,
604 unsigned char *sig
, size_t *sig_len
,
605 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
) {
606 return (mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context
*) ctx
,
607 md_alg
, hash
, hash_len
, sig
, sig_len
, f_rng
, p_rng
));
610 #if defined(MBEDTLS_ECP_RESTARTABLE)
611 static int ecdsa_verify_rs_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
612 const unsigned char *hash
, size_t hash_len
,
613 const unsigned char *sig
, size_t sig_len
,
615 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
618 ret
= mbedtls_ecdsa_read_signature_restartable(
619 (mbedtls_ecdsa_context
*) ctx
,
620 hash
, hash_len
, sig
, sig_len
,
621 (mbedtls_ecdsa_restart_ctx
*) rs_ctx
);
623 if (ret
== MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH
)
624 return (MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
);
629 static int ecdsa_sign_rs_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
630 const unsigned char *hash
, size_t hash_len
,
631 unsigned char *sig
, size_t *sig_len
,
632 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
,
634 return (mbedtls_ecdsa_write_signature_restartable(
635 (mbedtls_ecdsa_context
*) ctx
,
636 md_alg
, hash
, hash_len
, sig
, sig_len
, f_rng
, p_rng
,
637 (mbedtls_ecdsa_restart_ctx
*) rs_ctx
));
640 #endif /* MBEDTLS_ECP_RESTARTABLE */
642 static void *ecdsa_alloc_wrap(void) {
643 void *ctx
= mbedtls_calloc(1, sizeof(mbedtls_ecdsa_context
));
646 mbedtls_ecdsa_init((mbedtls_ecdsa_context
*) ctx
);
651 static void ecdsa_free_wrap(void *ctx
) {
652 mbedtls_ecdsa_free((mbedtls_ecdsa_context
*) ctx
);
656 #if defined(MBEDTLS_ECP_RESTARTABLE)
657 static void *ecdsa_rs_alloc(void) {
658 void *ctx
= mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx
));
661 mbedtls_ecdsa_restart_init(ctx
);
666 static void ecdsa_rs_free(void *ctx
) {
667 mbedtls_ecdsa_restart_free(ctx
);
670 #endif /* MBEDTLS_ECP_RESTARTABLE */
672 const mbedtls_pk_info_t mbedtls_ecdsa_info
= {
675 eckey_get_bitlen
, /* Compatible key structures */
679 #if defined(MBEDTLS_ECP_RESTARTABLE)
680 ecdsa_verify_rs_wrap
,
685 eckey_check_pair
, /* Compatible key structures */
688 #if defined(MBEDTLS_ECP_RESTARTABLE)
692 eckey_debug
, /* Compatible key structures */
694 #endif /* MBEDTLS_ECDSA_C */
696 #if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
698 * Support for alternative RSA-private implementations
701 static int rsa_alt_can_do(mbedtls_pk_type_t type
) {
702 return (type
== MBEDTLS_PK_RSA
);
705 static size_t rsa_alt_get_bitlen(const void *ctx
) {
706 const mbedtls_rsa_alt_context
*rsa_alt
= (const mbedtls_rsa_alt_context
*) ctx
;
708 return (8 * rsa_alt
->key_len_func(rsa_alt
->key
));
711 static int rsa_alt_sign_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
712 const unsigned char *hash
, size_t hash_len
,
713 unsigned char *sig
, size_t *sig_len
,
714 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
) {
715 mbedtls_rsa_alt_context
*rsa_alt
= (mbedtls_rsa_alt_context
*) ctx
;
717 #if SIZE_MAX > UINT_MAX
718 if (UINT_MAX
< hash_len
)
719 return (MBEDTLS_ERR_PK_BAD_INPUT_DATA
);
720 #endif /* SIZE_MAX > UINT_MAX */
722 *sig_len
= rsa_alt
->key_len_func(rsa_alt
->key
);
723 if (*sig_len
> MBEDTLS_PK_SIGNATURE_MAX_SIZE
)
724 return (MBEDTLS_ERR_PK_BAD_INPUT_DATA
);
726 return (rsa_alt
->sign_func(rsa_alt
->key
, f_rng
, p_rng
, MBEDTLS_RSA_PRIVATE
,
727 md_alg
, (unsigned int) hash_len
, hash
, sig
));
730 static int rsa_alt_decrypt_wrap(void *ctx
,
731 const unsigned char *input
, size_t ilen
,
732 unsigned char *output
, size_t *olen
, size_t osize
,
733 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
) {
734 mbedtls_rsa_alt_context
*rsa_alt
= (mbedtls_rsa_alt_context
*) ctx
;
739 if (ilen
!= rsa_alt
->key_len_func(rsa_alt
->key
))
740 return (MBEDTLS_ERR_RSA_BAD_INPUT_DATA
);
742 return (rsa_alt
->decrypt_func(rsa_alt
->key
,
743 MBEDTLS_RSA_PRIVATE
, olen
, input
, output
, osize
));
746 #if defined(MBEDTLS_RSA_C)
747 static int rsa_alt_check_pair(const void *pub
, const void *prv
) {
748 unsigned char sig
[MBEDTLS_MPI_MAX_SIZE
];
749 unsigned char hash
[32];
751 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
753 if (rsa_alt_get_bitlen(prv
) != rsa_get_bitlen(pub
))
754 return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
);
756 memset(hash
, 0x2a, sizeof(hash
));
758 if ((ret
= rsa_alt_sign_wrap((void *) prv
, MBEDTLS_MD_NONE
,
760 sig
, &sig_len
, NULL
, NULL
)) != 0) {
764 if (rsa_verify_wrap((void *) pub
, MBEDTLS_MD_NONE
,
765 hash
, sizeof(hash
), sig
, sig_len
) != 0) {
766 return (MBEDTLS_ERR_RSA_KEY_CHECK_FAILED
);
771 #endif /* MBEDTLS_RSA_C */
773 static void *rsa_alt_alloc_wrap(void) {
774 void *ctx
= mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context
));
777 memset(ctx
, 0, sizeof(mbedtls_rsa_alt_context
));
782 static void rsa_alt_free_wrap(void *ctx
) {
783 mbedtls_platform_zeroize(ctx
, sizeof(mbedtls_rsa_alt_context
));
787 const mbedtls_pk_info_t mbedtls_rsa_alt_info
= {
794 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
798 rsa_alt_decrypt_wrap
,
800 #if defined(MBEDTLS_RSA_C)
807 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
814 #endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
816 #if defined(MBEDTLS_USE_PSA_CRYPTO)
818 static void *pk_opaque_alloc_wrap(void) {
819 void *ctx
= mbedtls_calloc(1, sizeof(psa_key_id_t
));
821 /* no _init() function to call, an calloc() already zeroized */
826 static void pk_opaque_free_wrap(void *ctx
) {
827 mbedtls_platform_zeroize(ctx
, sizeof(psa_key_id_t
));
831 static size_t pk_opaque_get_bitlen(const void *ctx
) {
832 const psa_key_id_t
*key
= (const psa_key_id_t
*) ctx
;
834 psa_key_attributes_t attributes
= PSA_KEY_ATTRIBUTES_INIT
;
836 if (PSA_SUCCESS
!= psa_get_key_attributes(*key
, &attributes
))
839 bits
= psa_get_key_bits(&attributes
);
840 psa_reset_key_attributes(&attributes
);
844 static int pk_opaque_can_do(mbedtls_pk_type_t type
) {
845 /* For now opaque PSA keys can only wrap ECC keypairs,
846 * as checked by setup_psa().
847 * Also, ECKEY_DH does not really make sense with the current API. */
848 return (type
== MBEDTLS_PK_ECKEY
||
849 type
== MBEDTLS_PK_ECDSA
);
852 #if defined(MBEDTLS_ECDSA_C)
855 * Simultaneously convert and move raw MPI from the beginning of a buffer
856 * to an ASN.1 MPI at the end of the buffer.
857 * See also mbedtls_asn1_write_mpi().
859 * p: pointer to the end of the output buffer
860 * start: start of the output buffer, and also of the mpi to write at the end
861 * n_len: length of the mpi to read from start
863 static int asn1_write_mpibuf(unsigned char **p
, unsigned char *start
,
865 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
868 if ((size_t)(*p
- start
) < n_len
)
869 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
873 memmove(*p
, start
, len
);
875 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
876 * Neither r nor s should be 0, but as a failsafe measure, still detect
877 * that rather than overflowing the buffer in case of a PSA error. */
878 while (len
> 0 && **p
== 0x00) {
883 /* this is only reached if the signature was invalid */
885 return (MBEDTLS_ERR_PK_HW_ACCEL_FAILED
);
887 /* if the msb is 1, ASN.1 requires that we prepend a 0.
888 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
891 return (MBEDTLS_ERR_ASN1_BUF_TOO_SMALL
);
897 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(p
, start
, len
));
898 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(p
, start
,
899 MBEDTLS_ASN1_INTEGER
));
904 /* Transcode signature from PSA format to ASN.1 sequence.
905 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
906 * MPIs, and in-place.
908 * [in/out] sig: the signature pre- and post-transcoding
909 * [in/out] sig_len: signature length pre- and post-transcoding
910 * [int] buf_len: the available size the in/out buffer
912 static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig
, size_t *sig_len
,
914 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
916 const size_t rs_len
= *sig_len
/ 2;
917 unsigned char *p
= sig
+ buf_len
;
919 MBEDTLS_ASN1_CHK_ADD(len
, asn1_write_mpibuf(&p
, sig
+ rs_len
, rs_len
));
920 MBEDTLS_ASN1_CHK_ADD(len
, asn1_write_mpibuf(&p
, sig
, rs_len
));
922 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_len(&p
, sig
, len
));
923 MBEDTLS_ASN1_CHK_ADD(len
, mbedtls_asn1_write_tag(&p
, sig
,
924 MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_SEQUENCE
));
926 memmove(sig
, p
, len
);
932 #endif /* MBEDTLS_ECDSA_C */
934 static int pk_opaque_sign_wrap(void *ctx
, mbedtls_md_type_t md_alg
,
935 const unsigned char *hash
, size_t hash_len
,
936 unsigned char *sig
, size_t *sig_len
,
937 int (*f_rng
)(void *, unsigned char *, size_t), void *p_rng
) {
938 #if !defined(MBEDTLS_ECDSA_C)
947 return (MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE
);
948 #else /* !MBEDTLS_ECDSA_C */
949 const psa_key_id_t
*key
= (const psa_key_id_t
*) ctx
;
950 psa_key_attributes_t attributes
= PSA_KEY_ATTRIBUTES_INIT
;
951 psa_algorithm_t alg
= PSA_ALG_ECDSA(mbedtls_psa_translate_md(md_alg
));
955 /* PSA has its own RNG */
959 /* PSA needs an output buffer of known size, but our API doesn't provide
960 * that information. Assume that the buffer is large enough for a
961 * maximal-length signature with that key (otherwise the application is
963 status
= psa_get_key_attributes(*key
, &attributes
);
964 if (status
!= PSA_SUCCESS
)
965 return (mbedtls_psa_err_translate_pk(status
));
966 buf_len
= MBEDTLS_ECDSA_MAX_SIG_LEN(psa_get_key_bits(&attributes
));
967 psa_reset_key_attributes(&attributes
);
968 if (buf_len
> MBEDTLS_PK_SIGNATURE_MAX_SIZE
)
969 return (MBEDTLS_ERR_PK_BAD_INPUT_DATA
);
971 /* make the signature */
972 status
= psa_sign_hash(*key
, alg
, hash
, hash_len
,
973 sig
, buf_len
, sig_len
);
974 if (status
!= PSA_SUCCESS
)
975 return (mbedtls_psa_err_translate_pk(status
));
977 /* transcode it to ASN.1 sequence */
978 return (pk_ecdsa_sig_asn1_from_psa(sig
, sig_len
, buf_len
));
979 #endif /* !MBEDTLS_ECDSA_C */
982 const mbedtls_pk_info_t mbedtls_pk_opaque_info
= {
985 pk_opaque_get_bitlen
,
987 NULL
, /* verify - will be done later */
989 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
990 NULL
, /* restartable verify - not relevant */
991 NULL
, /* restartable sign - not relevant */
993 NULL
, /* decrypt - will be done later */
994 NULL
, /* encrypt - will be done later */
995 NULL
, /* check_pair - could be done later or left NULL */
996 pk_opaque_alloc_wrap
,
998 #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
999 NULL
, /* restart alloc - not relevant */
1000 NULL
, /* restart free - not relevant */
1002 NULL
, /* debug - could be done later, or even left NULL */
1005 #endif /* MBEDTLS_USE_PSA_CRYPTO */
1007 #endif /* MBEDTLS_PK_C */