blob: ab19a47af36024c39d05d9dc3bf53cbe3afa6eb0 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021
Gilles Peskine8a6022e2022-10-04 23:01:59 +020022#include "mbedtls/platform_util.h"
23
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000025#include "pk_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000026#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020027
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020028/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020037#endif
38
Gilles Peskine8a6022e2022-10-04 23:01:59 +020039#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PSA_CRYPTO_C)
40#include "pkwrite.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010041#endif
42
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040044#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010045#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020046#include "hash_info.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020047
48#if defined(MBEDTLS_ECDSA_C)
49#include "mbedtls/asn1write.h"
50#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020052#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010053
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020055
Andres AG72849872017-01-19 11:24:33 +000056#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010057#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020058#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020059
Jerry Yub02ee182022-03-16 10:30:41 +080060#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010061int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010062{
Gilles Peskine449bd832023-01-11 14:50:10 +010063 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010064 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010065 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010066 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010067 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010068 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010070 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010071 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010072 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010074 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010075 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010076 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010077 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010078 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010080 case PSA_ERROR_COMMUNICATION_FAILURE:
81 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010083 case PSA_ERROR_DATA_CORRUPT:
84 case PSA_ERROR_DATA_INVALID:
85 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010087 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +010089 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010091 }
92}
93
Neil Armstrong30beca32022-05-03 15:42:13 +020094#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
95 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +010096int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010097{
Gilles Peskine449bd832023-01-11 14:50:10 +010098 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010099 case PSA_ERROR_NOT_PERMITTED:
100 case PSA_ERROR_INVALID_ARGUMENT:
101 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +0100103 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +0100105 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100107 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100109 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Neil Armstrong19915c22022-03-01 15:21:02 +0100111 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return mbedtls_pk_error_from_psa(status);
Neil Armstrong19915c22022-03-01 15:21:02 +0100113 }
114}
Neil Armstrong30beca32022-05-03 15:42:13 +0200115#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800116
117#endif /* MBEDTLS_PSA_CRYPTO_C */
118
119#if defined(MBEDTLS_USE_PSA_CRYPTO)
120
121#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100122int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800123{
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800125 case PSA_ERROR_NOT_PERMITTED:
126 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800128 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800130 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800132 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800134 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800136 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return mbedtls_pk_error_from_psa(status);
Jerry Yu07869e82022-03-16 16:40:50 +0800138 }
139}
Jerry Yu75339822022-03-23 12:06:31 +0800140#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800141
Jerry Yu75339822022-03-23 12:06:31 +0800142#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100145static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200146{
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return type == MBEDTLS_PK_RSA ||
148 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200149}
150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151static size_t rsa_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200152{
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx;
154 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200155}
156
Neil Armstrong52f41f82022-02-22 15:30:24 +0100157#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100158static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
159 const unsigned char *hash, size_t hash_len,
160 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200161{
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
165 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
166 psa_status_t status;
167 mbedtls_pk_context key;
168 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100169 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100170 psa_algorithm_t psa_alg_md =
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
172 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100173
174#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
176 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
177 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100178#endif /* SIZE_MAX > UINT_MAX */
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (sig_len < rsa_len) {
181 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
182 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100183
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100184 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100185 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100186 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100187 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
189 if (key_len <= 0) {
190 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
191 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
194 psa_set_key_algorithm(&attributes, psa_alg_md);
195 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 status = psa_import_key(&attributes,
198 buf + sizeof(buf) - key_len, key_len,
199 &key_id);
200 if (status != PSA_SUCCESS) {
201 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100202 goto cleanup;
203 }
204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
206 sig, sig_len);
207 if (status != PSA_SUCCESS) {
208 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100209 goto cleanup;
210 }
211 ret = 0;
212
213cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 status = psa_destroy_key(key_id);
215 if (ret == 0 && status != PSA_SUCCESS) {
216 ret = mbedtls_pk_error_from_psa(status);
217 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100220}
221#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100222static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
223 const unsigned char *hash, size_t hash_len,
224 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100225{
Janos Follath24eed8d2019-11-22 13:21:35 +0000226 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
228 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200229
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100230#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
232 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
233 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100234#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (sig_len < rsa_len) {
237 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
238 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
241 (unsigned int) hash_len,
242 hash, sig)) != 0) {
243 return ret;
244 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200245
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200246 /* The buffer contains a valid signature followed by extra data.
247 * We have a special error code for that so that so that callers can
248 * use mbedtls_pk_verify() to check "Does the buffer start with a
249 * valid signature?" and not just "Does the buffer contain a valid
250 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (sig_len > rsa_len) {
252 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
253 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200256}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100257#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200258
Jerry Yub02ee182022-03-16 10:30:41 +0800259#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100260int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
261 mbedtls_rsa_context *rsa_ctx,
262 const unsigned char *hash, size_t hash_len,
263 unsigned char *sig, size_t sig_size,
264 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100265{
Neil Armstrong98545682022-02-22 16:12:51 +0100266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
267 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
268 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
269 psa_status_t status;
270 mbedtls_pk_context key;
271 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100272 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100273 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
276 if (sig_size < *sig_len) {
277 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
278 }
Neil Armstrong98545682022-02-22 16:12:51 +0100279
Neil Armstronge4f28682022-02-24 15:41:39 +0100280 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100281 * re-construct one to make it happy */
282 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800283 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
285 if (key_len <= 0) {
286 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
287 }
288 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
289 psa_set_key_algorithm(&attributes, alg);
290 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 status = psa_import_key(&attributes,
293 buf + sizeof(buf) - key_len, key_len,
294 &key_id);
295 if (status != PSA_SUCCESS) {
296 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100297 goto cleanup;
298 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 status = psa_sign_hash(key_id, alg, hash, hash_len,
300 sig, sig_size, sig_len);
301 if (status != PSA_SUCCESS) {
302 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100303 goto cleanup;
304 }
305
306 ret = 0;
307
308cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 status = psa_destroy_key(key_id);
310 if (ret == 0 && status != PSA_SUCCESS) {
311 ret = mbedtls_pk_error_from_psa(status);
312 }
313 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100314}
Jerry Yu406cf272022-03-22 11:33:42 +0800315#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800316
317#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100318static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
319 const unsigned char *hash, size_t hash_len,
320 unsigned char *sig, size_t sig_size, size_t *sig_len,
321 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800322{
Jerry Yu1d172a32022-03-12 19:12:05 +0800323 ((void) f_rng);
324 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800325
Jerry Yubd1b3272022-03-24 13:05:20 +0800326 psa_algorithm_t psa_md_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
328 if (psa_md_alg == 0) {
329 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
330 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
333 psa_md_alg),
334 ctx, hash, hash_len,
335 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800336}
Neil Armstrong98545682022-02-22 16:12:51 +0100337#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100338static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
339 const unsigned char *hash, size_t hash_len,
340 unsigned char *sig, size_t sig_size, size_t *sig_len,
341 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200342{
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100344
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100345#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
347 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
348 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100349#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 *sig_len = mbedtls_rsa_get_len(rsa);
352 if (sig_size < *sig_len) {
353 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
354 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
357 md_alg, (unsigned int) hash_len,
358 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200359}
Neil Armstrong98545682022-02-22 16:12:51 +0100360#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200361
Neil Armstrong18f43c72022-02-09 15:32:45 +0100362#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100363static int rsa_decrypt_wrap(void *ctx,
364 const unsigned char *input, size_t ilen,
365 unsigned char *output, size_t *olen, size_t osize,
366 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100367{
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
370 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
371 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
372 psa_status_t status;
373 mbedtls_pk_context key;
374 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100375 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100376
377 ((void) f_rng);
378 ((void) p_rng);
379
380#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
382 return MBEDTLS_ERR_RSA_INVALID_PADDING;
383 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100384#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (ilen != mbedtls_rsa_get_len(rsa)) {
387 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
388 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100389
390 /* mbedtls_pk_write_key_der() expects a full PK context;
391 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100392 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100393 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
395 if (key_len <= 0) {
396 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
397 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
400 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
401 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100402
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 status = psa_import_key(&attributes,
404 buf + sizeof(buf) - key_len, key_len,
405 &key_id);
406 if (status != PSA_SUCCESS) {
407 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100408 goto cleanup;
409 }
410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
412 input, ilen,
413 NULL, 0,
414 output, osize, olen);
415 if (status != PSA_SUCCESS) {
416 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100417 goto cleanup;
418 }
419
420 ret = 0;
421
422cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 mbedtls_platform_zeroize(buf, sizeof(buf));
424 status = psa_destroy_key(key_id);
425 if (ret == 0 && status != PSA_SUCCESS) {
426 ret = mbedtls_pk_error_from_psa(status);
427 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100430}
431#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100432static int rsa_decrypt_wrap(void *ctx,
433 const unsigned char *input, size_t ilen,
434 unsigned char *output, size_t *olen, size_t osize,
435 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200436{
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (ilen != mbedtls_rsa_get_len(rsa)) {
440 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
441 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
444 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200445}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100446#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200447
Neil Armstrong96a16a42022-02-10 10:40:11 +0100448#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100449static int rsa_encrypt_wrap(void *ctx,
450 const unsigned char *input, size_t ilen,
451 unsigned char *output, size_t *olen, size_t osize,
452 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100453{
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
456 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
457 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
458 psa_status_t status;
459 mbedtls_pk_context key;
460 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100461 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100462
463 ((void) f_rng);
464 ((void) p_rng);
465
466#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
468 return MBEDTLS_ERR_RSA_INVALID_PADDING;
469 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100470#endif
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (mbedtls_rsa_get_len(rsa) > osize) {
473 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
474 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100475
Neil Armstrongac014ca2022-02-24 15:27:54 +0100476 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100477 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100478 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100479 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
481 if (key_len <= 0) {
482 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
483 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
486 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
487 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 status = psa_import_key(&attributes,
490 buf + sizeof(buf) - key_len, key_len,
491 &key_id);
492 if (status != PSA_SUCCESS) {
493 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100494 goto cleanup;
495 }
496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
498 input, ilen,
499 NULL, 0,
500 output, osize, olen);
501 if (status != PSA_SUCCESS) {
502 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100503 goto cleanup;
504 }
505
506 ret = 0;
507
508cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 status = psa_destroy_key(key_id);
510 if (ret == 0 && status != PSA_SUCCESS) {
511 ret = mbedtls_pk_error_from_psa(status);
512 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100515}
516#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100517static int rsa_encrypt_wrap(void *ctx,
518 const unsigned char *input, size_t ilen,
519 unsigned char *output, size_t *olen, size_t osize,
520 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200521{
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
523 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if (*olen > osize) {
526 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
527 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
530 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200531}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100532#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534static int rsa_check_pair_wrap(const void *pub, const void *prv,
535 int (*f_rng)(void *, unsigned char *, size_t),
536 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100537{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200538 (void) f_rng;
539 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub,
541 (const mbedtls_rsa_context *) prv);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100542}
543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200545{
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200547
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 if (ctx != NULL) {
549 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
550 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200553}
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200556{
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
558 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200559}
560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200562{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200563#if defined(MBEDTLS_RSA_ALT)
564 /* Not supported */
565 (void) ctx;
566 (void) items;
567#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200569 items->name = "rsa.N";
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 items->value = &(((mbedtls_rsa_context *) ctx)->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200571
572 items++;
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200575 items->name = "rsa.E";
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 items->value = &(((mbedtls_rsa_context *) ctx)->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200577#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200578}
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580const mbedtls_pk_info_t mbedtls_rsa_info = {
581 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200582 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200583 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200584 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200585 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200586 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200587#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200588 NULL,
589 NULL,
590#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200591 rsa_decrypt_wrap,
592 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100593 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200594 rsa_alloc_wrap,
595 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200596#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200597 NULL,
598 NULL,
599#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200600 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200601};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200604#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200605/*
606 * Generic EC key
607 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200609{
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return type == MBEDTLS_PK_ECKEY ||
611 type == MBEDTLS_PK_ECKEY_DH ||
612 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200613}
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615static size_t eckey_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200616{
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 return ((mbedtls_ecp_keypair *) ctx)->grp.pbits;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200618}
619
Valerio Setti1cdddac2023-02-02 13:55:57 +0100620#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400621#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400622/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500623 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
624 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500625 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
627 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500628{
Janos Follath24eed8d2019-11-22 13:21:35 +0000629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500630 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
633 MBEDTLS_ASN1_INTEGER)) != 0) {
634 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500635 }
636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 while (unpadded_len > 0 && **from == 0x00) {
638 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500639 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500640 }
641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (unpadded_len > to_len || unpadded_len == 0) {
643 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
644 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500645
Andrzej Kurek9241d182018-11-20 05:04:35 -0500646 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 memset(to, 0x00, padding_len);
648 memcpy(to + padding_len, *from, unpadded_len);
649 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500652}
653
654/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400655 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500656 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500657 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400658 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
660 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400661{
Janos Follath24eed8d2019-11-22 13:21:35 +0000662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500663 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
666 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
667 return ret;
668 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400669
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500670 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
672 return ret;
673 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500674 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
676 return ret;
677 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400680}
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
683 const unsigned char *hash, size_t hash_len,
684 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400685{
Valerio Settiab363d92023-01-26 14:31:54 +0100686 mbedtls_ecp_keypair *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000687 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200688 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100689 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200690 psa_status_t status;
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200691 size_t key_len;
692 /* This buffer contains first the public key (consisting of two public
693 * points plus a header byte), then the signature (consisting of two
694 * public points). Size it for the public key which is one byte larger. */
695 unsigned char buf[PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(
696 PSA_VENDOR_ECC_MAX_CURVE_BITS )];
Hanno Beckera9851112019-01-25 16:37:10 +0000697 unsigned char *p;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700698 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100699 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100700 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
702 const size_t signature_part_size = (ctx->grp.nbits + 7) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700703 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (curve == 0) {
706 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
707 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500708
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200709 psa_set_key_type( &attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY( curve ) );
710 psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_VERIFY_HASH );
711 psa_set_key_algorithm( &attributes, psa_sig_md );
712
713 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
714 MBEDTLS_ECP_PF_UNCOMPRESSED,
715 &key_len, buf, sizeof(buf));
716 if (ret != 0) {
717 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 status = psa_import_key(&attributes,
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200721 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 &key_id);
723 if (status != PSA_SUCCESS) {
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200724 ret = mbedtls_pk_error_from_psa( status );
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400725 goto cleanup;
726 }
727
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500728 /* We don't need the exported key anymore and can
729 * reuse its buffer for signature extraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (2 * signature_part_size > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500731 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
732 goto cleanup;
733 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 p = (unsigned char *) sig;
736 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
737 signature_part_size)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500738 goto cleanup;
739 }
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 status = psa_verify_hash(key_id, psa_sig_md,
742 hash, hash_len,
743 buf, 2 * signature_part_size);
744 if (status != PSA_SUCCESS) {
745 ret = mbedtls_pk_error_from_psa_ecdsa(status);
746 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400747 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500748
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500750 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
751 goto cleanup;
752 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400753 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400754
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500755cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 status = psa_destroy_key(key_id);
757 if (ret == 0 && status != PSA_SUCCESS) {
758 ret = mbedtls_pk_error_from_psa(status);
759 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400762}
763#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100764static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
765 const unsigned char *hash, size_t hash_len,
766 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200767{
Janos Follath24eed8d2019-11-22 13:21:35 +0000768 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200769 ((void) md_alg);
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx,
772 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
775 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
776 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200779}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400780#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100781#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200782
Valerio Setti1cdddac2023-02-02 13:55:57 +0100783#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100784#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100785/*
786 * Simultaneously convert and move raw MPI from the beginning of a buffer
787 * to an ASN.1 MPI at the end of the buffer.
788 * See also mbedtls_asn1_write_mpi().
789 *
790 * p: pointer to the end of the output buffer
791 * start: start of the output buffer, and also of the mpi to write at the end
792 * n_len: length of the mpi to read from start
793 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100794static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
795 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100796{
797 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
798 size_t len = 0;
799
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 if ((size_t) (*p - start) < n_len) {
801 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
802 }
Neil Armstrong15021652022-03-01 10:14:17 +0100803
804 len = n_len;
805 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100807
808 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
809 * Neither r nor s should be 0, but as a failsafe measure, still detect
810 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100812 ++(*p);
813 --len;
814 }
815
816 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 if (len == 0) {
818 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
819 }
Neil Armstrong15021652022-03-01 10:14:17 +0100820
821 /* if the msb is 1, ASN.1 requires that we prepend a 0.
822 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if (**p & 0x80) {
824 if (*p - start < 1) {
825 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
826 }
Neil Armstrong15021652022-03-01 10:14:17 +0100827
828 *--(*p) = 0x00;
829 len += 1;
830 }
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
833 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
834 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100837}
838
839/* Transcode signature from PSA format to ASN.1 sequence.
840 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
841 * MPIs, and in-place.
842 *
843 * [in/out] sig: the signature pre- and post-transcoding
844 * [in/out] sig_len: signature length pre- and post-transcoding
845 * [int] buf_len: the available size the in/out buffer
846 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
848 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100849{
850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
851 size_t len = 0;
852 const size_t rs_len = *sig_len / 2;
853 unsigned char *p = sig + buf_len;
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
856 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
859 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
860 MBEDTLS_ASN1_CONSTRUCTED |
861 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100864 *sig_len = len;
865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100867}
Neil Armstronge9606902022-02-09 14:23:00 +0100868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
870 const unsigned char *hash, size_t hash_len,
871 unsigned char *sig, size_t sig_size, size_t *sig_len,
872 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100873{
Valerio Settiab363d92023-01-26 14:31:54 +0100874 mbedtls_ecp_keypair *ctx = ctx_arg;
Neil Armstronge9606902022-02-09 14:23:00 +0100875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
876 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
877 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
878 psa_status_t status;
Gilles Peskine13caa942022-10-04 22:59:26 +0200879 unsigned char buf[PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(
880 PSA_VENDOR_ECC_MAX_CURVE_BITS )];
881 psa_algorithm_t psa_sig_md =
882 PSA_ALG_ECDSA( mbedtls_hash_info_psa_from_md( md_alg ) );
Neil Armstronge9606902022-02-09 14:23:00 +0100883 size_t curve_bits;
884 psa_ecc_family_t curve =
Gilles Peskine13caa942022-10-04 22:59:26 +0200885 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
886 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100887
888 /* PSA has its own RNG */
889 ((void) f_rng);
890 ((void) p_rng);
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (curve == 0) {
893 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
894 }
Neil Armstronge9606902022-02-09 14:23:00 +0100895
Gilles Peskine13caa942022-10-04 22:59:26 +0200896 if (key_len > sizeof(buf)) {
897 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200899 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
900 if( ret != 0 ) {
Neil Armstronge9606902022-02-09 14:23:00 +0100901 goto cleanup;
902 }
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
905 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
906 psa_set_key_algorithm(&attributes, psa_sig_md);
907
908 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200909 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 &key_id);
911 if (status != PSA_SUCCESS) {
912 ret = mbedtls_pk_error_from_psa(status);
913 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100914 }
915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
917 sig, sig_size, sig_len);
918 if (status != PSA_SUCCESS) {
919 ret = mbedtls_pk_error_from_psa_ecdsa(status);
920 goto cleanup;
921 }
922
923 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100924
925cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 mbedtls_platform_zeroize(buf, sizeof(buf));
927 status = psa_destroy_key(key_id);
928 if (ret == 0 && status != PSA_SUCCESS) {
929 ret = mbedtls_pk_error_from_psa(status);
930 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100931
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100933}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100934#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
936 const unsigned char *hash, size_t hash_len,
937 unsigned char *sig, size_t sig_size, size_t *sig_len,
938 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200939{
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
941 md_alg, hash, hash_len,
942 sig, sig_size, sig_len,
943 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200944}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100945#endif /* MBEDTLS_USE_PSA_CRYPTO */
946#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200947
Valerio Setti5b16e9e2023-02-07 08:08:53 +0100948#if defined(MBEDTLS_ECDSA_C)
Valerio Setti1cdddac2023-02-02 13:55:57 +0100949#if defined(MBEDTLS_ECP_RESTARTABLE)
950/* Forward declarations */
951static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
952 const unsigned char *hash, size_t hash_len,
953 const unsigned char *sig, size_t sig_len,
954 void *rs_ctx);
955
956static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
957 const unsigned char *hash, size_t hash_len,
958 unsigned char *sig, size_t sig_size, size_t *sig_len,
959 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
960 void *rs_ctx);
961
962/*
963 * Restart context for ECDSA operations with ECKEY context
964 *
965 * We need to store an actual ECDSA context, as we need to pass the same to
966 * the underlying ecdsa function, so we can't create it on the fly every time.
967 */
968typedef struct {
969 mbedtls_ecdsa_restart_ctx ecdsa_rs;
970 mbedtls_ecdsa_context ecdsa_ctx;
971} eckey_restart_ctx;
972
973static void *eckey_rs_alloc(void)
974{
975 eckey_restart_ctx *rs_ctx;
976
977 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
978
979 if (ctx != NULL) {
980 rs_ctx = ctx;
981 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
982 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
983 }
984
985 return ctx;
986}
987
988static void eckey_rs_free(void *ctx)
989{
990 eckey_restart_ctx *rs_ctx;
991
992 if (ctx == NULL) {
993 return;
994 }
995
996 rs_ctx = ctx;
997 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
998 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
999
1000 mbedtls_free(ctx);
1001}
1002
1003static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1004 const unsigned char *hash, size_t hash_len,
1005 const unsigned char *sig, size_t sig_len,
1006 void *rs_ctx)
1007{
1008 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1009 eckey_restart_ctx *rs = rs_ctx;
1010
1011 /* Should never happen */
1012 if (rs == NULL) {
1013 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1014 }
1015
1016 /* set up our own sub-context if needed (that is, on first run) */
1017 if (rs->ecdsa_ctx.grp.pbits == 0) {
1018 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1019 }
1020
1021 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx,
1022 md_alg, hash, hash_len,
1023 sig, sig_len, &rs->ecdsa_rs));
1024
1025cleanup:
1026 return ret;
1027}
1028
1029static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1030 const unsigned char *hash, size_t hash_len,
1031 unsigned char *sig, size_t sig_size, size_t *sig_len,
1032 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1033 void *rs_ctx)
1034{
1035 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1036 eckey_restart_ctx *rs = rs_ctx;
1037
1038 /* Should never happen */
1039 if (rs == NULL) {
1040 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1041 }
1042
1043 /* set up our own sub-context if needed (that is, on first run) */
1044 if (rs->ecdsa_ctx.grp.pbits == 0) {
1045 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1046 }
1047
1048 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg,
1049 hash, hash_len, sig, sig_size, sig_len,
1050 f_rng, p_rng, &rs->ecdsa_rs));
1051
1052cleanup:
1053 return ret;
1054}
1055#endif /* MBEDTLS_ECP_RESTARTABLE */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001056#endif /* MBEDTLS_ECDSA_C */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001057
1058static int eckey_check_pair(const void *pub, const void *prv,
1059 int (*f_rng)(void *, unsigned char *, size_t),
1060 void *p_rng)
1061{
1062 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
1063 (const mbedtls_ecp_keypair *) prv,
1064 f_rng, p_rng);
1065}
1066
1067static void *eckey_alloc_wrap(void)
1068{
1069 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1070
1071 if (ctx != NULL) {
1072 mbedtls_ecp_keypair_init(ctx);
1073 }
1074
1075 return ctx;
1076}
1077
1078static void eckey_free_wrap(void *ctx)
1079{
1080 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1081 mbedtls_free(ctx);
1082}
1083
1084static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
1085{
1086 items->type = MBEDTLS_PK_DEBUG_ECP;
1087 items->name = "eckey.Q";
1088 items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
1089}
1090
1091const mbedtls_pk_info_t mbedtls_eckey_info = {
1092 MBEDTLS_PK_ECKEY,
1093 "EC",
1094 eckey_get_bitlen,
1095 eckey_can_do,
1096#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1097 ecdsa_verify_wrap, /* Compatible key structures */
1098#else
1099 NULL,
1100#endif
1101#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1102 ecdsa_sign_wrap, /* Compatible key structures */
1103#else
1104 NULL,
1105#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001106#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001107 eckey_verify_rs_wrap,
1108 eckey_sign_rs_wrap,
1109#endif
1110 NULL,
1111 NULL,
1112 eckey_check_pair,
1113 eckey_alloc_wrap,
1114 eckey_free_wrap,
1115#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1116 eckey_rs_alloc,
1117 eckey_rs_free,
1118#endif
1119 eckey_debug,
1120};
1121
1122/*
1123 * EC key restricted to ECDH
1124 */
1125static int eckeydh_can_do(mbedtls_pk_type_t type)
1126{
1127 return type == MBEDTLS_PK_ECKEY ||
1128 type == MBEDTLS_PK_ECKEY_DH;
1129}
1130
1131const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1132 MBEDTLS_PK_ECKEY_DH,
1133 "EC_DH",
1134 eckey_get_bitlen, /* Same underlying key structure */
1135 eckeydh_can_do,
1136 NULL,
1137 NULL,
1138#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1139 NULL,
1140 NULL,
1141#endif
1142 NULL,
1143 NULL,
1144 eckey_check_pair,
1145 eckey_alloc_wrap, /* Same underlying key structure */
1146 eckey_free_wrap, /* Same underlying key structure */
1147#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1148 NULL,
1149 NULL,
1150#endif
1151 eckey_debug, /* Same underlying key structure */
1152};
1153#endif /* MBEDTLS_ECP_C */
1154
1155#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1156static int ecdsa_can_do(mbedtls_pk_type_t type)
1157{
1158 return type == MBEDTLS_PK_ECDSA;
1159}
1160
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001161#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001162static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1163 const unsigned char *hash, size_t hash_len,
1164 const unsigned char *sig, size_t sig_len,
1165 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001166{
Janos Follath24eed8d2019-11-22 13:21:35 +00001167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001168 ((void) md_alg);
1169
1170 ret = mbedtls_ecdsa_read_signature_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 (mbedtls_ecdsa_context *) ctx,
1172 hash, hash_len, sig, sig_len,
1173 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1176 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1177 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001180}
1181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1183 const unsigned char *hash, size_t hash_len,
1184 unsigned char *sig, size_t sig_size, size_t *sig_len,
1185 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1186 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001187{
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 return mbedtls_ecdsa_write_signature_restartable(
1189 (mbedtls_ecdsa_context *) ctx,
1190 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1191 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001192
1193}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001196{
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if (ctx != NULL) {
1200 mbedtls_ecdsa_restart_init(ctx);
1201 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001204}
1205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001207{
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 mbedtls_ecdsa_restart_free(ctx);
1209 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001210}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001211#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1214 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001215 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001216 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001217 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001218#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1219 ecdsa_verify_wrap, /* Compatible key structures */
1220#else
1221 NULL,
1222#endif
1223#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1224 ecdsa_sign_wrap, /* Compatible key structures */
1225#else
1226 NULL,
1227#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001228#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001229 ecdsa_verify_rs_wrap,
1230 ecdsa_sign_rs_wrap,
1231#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001232 NULL,
1233 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001234 eckey_check_pair, /* Compatible key structures */
Valerio Setti24138d92023-01-27 14:24:09 +01001235 eckey_alloc_wrap, /* Compatible key structures */
1236 eckey_free_wrap, /* Compatible key structures */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001237#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001238 ecdsa_rs_alloc,
1239 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001240#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001241 eckey_debug, /* Compatible key structures */
1242};
Valerio Setti7ca13182023-01-27 13:22:42 +01001243#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001246/*
1247 * Support for alternative RSA-private implementations
1248 */
1249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001251{
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001253}
1254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255static size_t rsa_alt_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001257 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001260}
1261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1263 const unsigned char *hash, size_t hash_len,
1264 unsigned char *sig, size_t sig_size, size_t *sig_len,
1265 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001266{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001268
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001269#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (UINT_MAX < hash_len) {
1271 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1272 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001273#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1276 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1277 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1278 }
1279 if (*sig_len > sig_size) {
1280 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1281 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001282
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1284 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001285}
1286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287static int rsa_alt_decrypt_wrap(void *ctx,
1288 const unsigned char *input, size_t ilen,
1289 unsigned char *output, size_t *olen, size_t osize,
1290 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001291{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001293
1294 ((void) f_rng);
1295 ((void) p_rng);
1296
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1298 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1299 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 return rsa_alt->decrypt_func(rsa_alt->key,
1302 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001303}
1304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001306static int rsa_alt_check_pair(const void *pub, const void *prv,
1307 int (*f_rng)(void *, unsigned char *, size_t),
1308 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001309{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001311 unsigned char hash[32];
1312 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001313 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1316 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001317 }
1318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 memset(hash, 0x2a, sizeof(hash));
1320
1321 if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE,
1322 hash, sizeof(hash),
1323 sig, sizeof(sig), &sig_len,
1324 f_rng, p_rng)) != 0) {
1325 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001326 }
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE,
1329 hash, sizeof(hash), sig, sig_len) != 0) {
1330 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1331 }
1332
1333 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001334}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001335#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001338{
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if (ctx != NULL) {
1342 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1343 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001346}
1347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001349{
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1351 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001352}
1353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001354const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1355 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001357 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001358 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001359 NULL,
1360 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001361#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001362 NULL,
1363 NULL,
1364#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001365 rsa_alt_decrypt_wrap,
1366 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001368 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001369#else
1370 NULL,
1371#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001372 rsa_alt_alloc_wrap,
1373 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001374#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001375 NULL,
1376 NULL,
1377#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001378 NULL,
1379};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001382
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001383#if defined(MBEDTLS_USE_PSA_CRYPTO)
1384
Gilles Peskine449bd832023-01-11 14:50:10 +01001385static void *pk_opaque_alloc_wrap(void)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001386{
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t));
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001388
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001389 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 return ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001392}
1393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394static void pk_opaque_free_wrap(void *ctx)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001395{
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t));
1397 mbedtls_free(ctx);
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001398}
1399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400static size_t pk_opaque_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001401{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001402 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001403 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001404 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) {
1407 return 0;
1408 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 bits = psa_get_key_bits(&attributes);
1411 psa_reset_key_attributes(&attributes);
1412 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001413}
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001416{
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 return type == MBEDTLS_PK_ECKEY ||
1418 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001419}
1420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001422{
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 return type == MBEDTLS_PK_RSA ||
1424 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001425}
1426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1428 const unsigned char *hash, size_t hash_len,
1429 unsigned char *sig, size_t sig_size, size_t *sig_len,
1430 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001431{
Valerio Settiab363d92023-01-26 14:31:54 +01001432#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001433 ((void) ctx);
1434 ((void) md_alg);
1435 ((void) hash);
1436 ((void) hash_len);
1437 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001438 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001439 ((void) sig_len);
1440 ((void) f_rng);
1441 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001443#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001444 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001445 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001446 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001447 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001448 psa_status_t status;
1449
1450 /* PSA has its own RNG */
1451 (void) f_rng;
1452 (void) p_rng;
1453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 status = psa_get_key_attributes(*key, &attributes);
1455 if (status != PSA_SUCCESS) {
1456 return mbedtls_pk_error_from_psa(status);
1457 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 type = psa_get_key_type(&attributes);
1460 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001461
Valerio Settiab363d92023-01-26 14:31:54 +01001462#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1464 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1465 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001466#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001467#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (PSA_KEY_TYPE_IS_RSA(type)) {
1469 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1470 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001471#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001473
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001474 /* make the signature */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 status = psa_sign_hash(*key, alg, hash, hash_len,
1476 sig, sig_size, sig_len);
1477 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001478#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1480 return mbedtls_pk_error_from_psa_ecdsa(status);
1481 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001482#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001483#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (PSA_KEY_TYPE_IS_RSA(type)) {
1485 return mbedtls_pk_error_from_psa_rsa(status);
1486 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001487#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 return mbedtls_pk_error_from_psa(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001489 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001490
Valerio Settiab363d92023-01-26 14:31:54 +01001491#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001493 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1495 }
Valerio Settiab363d92023-01-26 14:31:54 +01001496#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001497
1498 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001499#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001500}
1501
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001502const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001503 MBEDTLS_PK_OPAQUE,
1504 "Opaque",
1505 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001506 pk_opaque_ecdsa_can_do,
1507 NULL, /* verify - will be done later */
1508 pk_opaque_sign_wrap,
1509#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1510 NULL, /* restartable verify - not relevant */
1511 NULL, /* restartable sign - not relevant */
1512#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001513 NULL, /* decrypt - not relevant */
1514 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001515 NULL, /* check_pair - could be done later or left NULL */
1516 pk_opaque_alloc_wrap,
1517 pk_opaque_free_wrap,
1518#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1519 NULL, /* restart alloc - not relevant */
1520 NULL, /* restart free - not relevant */
1521#endif
1522 NULL, /* debug - could be done later, or even left NULL */
1523};
1524
Neil Armstrong30beca32022-05-03 15:42:13 +02001525#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526static int pk_opaque_rsa_decrypt(void *ctx,
1527 const unsigned char *input, size_t ilen,
1528 unsigned char *output, size_t *olen, size_t osize,
1529 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001530{
1531 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1532 psa_status_t status;
1533
1534 /* PSA has its own RNG */
1535 (void) f_rng;
1536 (void) p_rng;
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1539 input, ilen,
1540 NULL, 0,
1541 output, osize, olen);
1542 if (status != PSA_SUCCESS) {
1543 return mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001544 }
1545
1546 return 0;
1547}
Neil Armstrong30beca32022-05-03 15:42:13 +02001548#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001549
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001550const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1551 MBEDTLS_PK_OPAQUE,
1552 "Opaque",
1553 pk_opaque_get_bitlen,
1554 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001555 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001556 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001557#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1558 NULL, /* restartable verify - not relevant */
1559 NULL, /* restartable sign - not relevant */
1560#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001561#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001562 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001563#else
1564 NULL, /* decrypt - not available */
1565#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001566 NULL, /* encrypt - will be done later */
1567 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001568 pk_opaque_alloc_wrap,
1569 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001570#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1571 NULL, /* restart alloc - not relevant */
1572 NULL, /* restart free - not relevant */
1573#endif
1574 NULL, /* debug - could be done later, or even left NULL */
1575};
1576
1577#endif /* MBEDTLS_USE_PSA_CRYPTO */
1578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#endif /* MBEDTLS_PK_C */