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