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