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