blob: 525f6bc90c2adb92d45268506699695ddccb74c1 [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 )];
Valerio Setti5bc52242023-01-30 15:48:28 +0100881#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
882 psa_algorithm_t psa_sig_md =
883 PSA_ALG_DETERMINISTIC_ECDSA( mbedtls_hash_info_psa_from_md( md_alg ) );
884#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200885 psa_algorithm_t psa_sig_md =
886 PSA_ALG_ECDSA( mbedtls_hash_info_psa_from_md( md_alg ) );
Valerio Setti5bc52242023-01-30 15:48:28 +0100887#endif
Neil Armstronge9606902022-02-09 14:23:00 +0100888 size_t curve_bits;
889 psa_ecc_family_t curve =
Gilles Peskine13caa942022-10-04 22:59:26 +0200890 mbedtls_ecc_group_to_psa( ctx->grp.id, &curve_bits );
891 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100892
893 /* PSA has its own RNG */
894 ((void) f_rng);
895 ((void) p_rng);
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (curve == 0) {
898 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
899 }
Neil Armstronge9606902022-02-09 14:23:00 +0100900
Gilles Peskine13caa942022-10-04 22:59:26 +0200901 if (key_len > sizeof(buf)) {
902 return( MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED );
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200904 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
905 if( ret != 0 ) {
Neil Armstronge9606902022-02-09 14:23:00 +0100906 goto cleanup;
907 }
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
910 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
911 psa_set_key_algorithm(&attributes, psa_sig_md);
912
913 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200914 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 &key_id);
916 if (status != PSA_SUCCESS) {
917 ret = mbedtls_pk_error_from_psa(status);
918 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100919 }
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
922 sig, sig_size, sig_len);
923 if (status != PSA_SUCCESS) {
924 ret = mbedtls_pk_error_from_psa_ecdsa(status);
925 goto cleanup;
926 }
927
928 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100929
930cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 mbedtls_platform_zeroize(buf, sizeof(buf));
932 status = psa_destroy_key(key_id);
933 if (ret == 0 && status != PSA_SUCCESS) {
934 ret = mbedtls_pk_error_from_psa(status);
935 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100936
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100938}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100939#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100940static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
941 const unsigned char *hash, size_t hash_len,
942 unsigned char *sig, size_t sig_size, size_t *sig_len,
943 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200944{
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
946 md_alg, hash, hash_len,
947 sig, sig_size, sig_len,
948 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200949}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100950#endif /* MBEDTLS_USE_PSA_CRYPTO */
951#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200952
Valerio Setti5b16e9e2023-02-07 08:08:53 +0100953#if defined(MBEDTLS_ECDSA_C)
Valerio Setti1cdddac2023-02-02 13:55:57 +0100954#if defined(MBEDTLS_ECP_RESTARTABLE)
955/* Forward declarations */
956static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
957 const unsigned char *hash, size_t hash_len,
958 const unsigned char *sig, size_t sig_len,
959 void *rs_ctx);
960
961static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
962 const unsigned char *hash, size_t hash_len,
963 unsigned char *sig, size_t sig_size, size_t *sig_len,
964 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
965 void *rs_ctx);
966
967/*
968 * Restart context for ECDSA operations with ECKEY context
969 *
970 * We need to store an actual ECDSA context, as we need to pass the same to
971 * the underlying ecdsa function, so we can't create it on the fly every time.
972 */
973typedef struct {
974 mbedtls_ecdsa_restart_ctx ecdsa_rs;
975 mbedtls_ecdsa_context ecdsa_ctx;
976} eckey_restart_ctx;
977
978static void *eckey_rs_alloc(void)
979{
980 eckey_restart_ctx *rs_ctx;
981
982 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
983
984 if (ctx != NULL) {
985 rs_ctx = ctx;
986 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
987 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
988 }
989
990 return ctx;
991}
992
993static void eckey_rs_free(void *ctx)
994{
995 eckey_restart_ctx *rs_ctx;
996
997 if (ctx == NULL) {
998 return;
999 }
1000
1001 rs_ctx = ctx;
1002 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1003 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1004
1005 mbedtls_free(ctx);
1006}
1007
1008static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1009 const unsigned char *hash, size_t hash_len,
1010 const unsigned char *sig, size_t sig_len,
1011 void *rs_ctx)
1012{
1013 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1014 eckey_restart_ctx *rs = rs_ctx;
1015
1016 /* Should never happen */
1017 if (rs == NULL) {
1018 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1019 }
1020
1021 /* set up our own sub-context if needed (that is, on first run) */
1022 if (rs->ecdsa_ctx.grp.pbits == 0) {
1023 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1024 }
1025
1026 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx,
1027 md_alg, hash, hash_len,
1028 sig, sig_len, &rs->ecdsa_rs));
1029
1030cleanup:
1031 return ret;
1032}
1033
1034static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1035 const unsigned char *hash, size_t hash_len,
1036 unsigned char *sig, size_t sig_size, size_t *sig_len,
1037 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1038 void *rs_ctx)
1039{
1040 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1041 eckey_restart_ctx *rs = rs_ctx;
1042
1043 /* Should never happen */
1044 if (rs == NULL) {
1045 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1046 }
1047
1048 /* set up our own sub-context if needed (that is, on first run) */
1049 if (rs->ecdsa_ctx.grp.pbits == 0) {
1050 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1051 }
1052
1053 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg,
1054 hash, hash_len, sig, sig_size, sig_len,
1055 f_rng, p_rng, &rs->ecdsa_rs));
1056
1057cleanup:
1058 return ret;
1059}
1060#endif /* MBEDTLS_ECP_RESTARTABLE */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001061#endif /* MBEDTLS_ECDSA_C */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001062
1063static int eckey_check_pair(const void *pub, const void *prv,
1064 int (*f_rng)(void *, unsigned char *, size_t),
1065 void *p_rng)
1066{
1067 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
1068 (const mbedtls_ecp_keypair *) prv,
1069 f_rng, p_rng);
1070}
1071
1072static void *eckey_alloc_wrap(void)
1073{
1074 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1075
1076 if (ctx != NULL) {
1077 mbedtls_ecp_keypair_init(ctx);
1078 }
1079
1080 return ctx;
1081}
1082
1083static void eckey_free_wrap(void *ctx)
1084{
1085 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1086 mbedtls_free(ctx);
1087}
1088
1089static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
1090{
1091 items->type = MBEDTLS_PK_DEBUG_ECP;
1092 items->name = "eckey.Q";
1093 items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
1094}
1095
1096const mbedtls_pk_info_t mbedtls_eckey_info = {
1097 MBEDTLS_PK_ECKEY,
1098 "EC",
1099 eckey_get_bitlen,
1100 eckey_can_do,
1101#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1102 ecdsa_verify_wrap, /* Compatible key structures */
1103#else
1104 NULL,
1105#endif
1106#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1107 ecdsa_sign_wrap, /* Compatible key structures */
1108#else
1109 NULL,
1110#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001111#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001112 eckey_verify_rs_wrap,
1113 eckey_sign_rs_wrap,
1114#endif
1115 NULL,
1116 NULL,
1117 eckey_check_pair,
1118 eckey_alloc_wrap,
1119 eckey_free_wrap,
1120#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1121 eckey_rs_alloc,
1122 eckey_rs_free,
1123#endif
1124 eckey_debug,
1125};
1126
1127/*
1128 * EC key restricted to ECDH
1129 */
1130static int eckeydh_can_do(mbedtls_pk_type_t type)
1131{
1132 return type == MBEDTLS_PK_ECKEY ||
1133 type == MBEDTLS_PK_ECKEY_DH;
1134}
1135
1136const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1137 MBEDTLS_PK_ECKEY_DH,
1138 "EC_DH",
1139 eckey_get_bitlen, /* Same underlying key structure */
1140 eckeydh_can_do,
1141 NULL,
1142 NULL,
1143#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1144 NULL,
1145 NULL,
1146#endif
1147 NULL,
1148 NULL,
1149 eckey_check_pair,
1150 eckey_alloc_wrap, /* Same underlying key structure */
1151 eckey_free_wrap, /* Same underlying key structure */
1152#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1153 NULL,
1154 NULL,
1155#endif
1156 eckey_debug, /* Same underlying key structure */
1157};
1158#endif /* MBEDTLS_ECP_C */
1159
1160#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1161static int ecdsa_can_do(mbedtls_pk_type_t type)
1162{
1163 return type == MBEDTLS_PK_ECDSA;
1164}
1165
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001166#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001167static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1168 const unsigned char *hash, size_t hash_len,
1169 const unsigned char *sig, size_t sig_len,
1170 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001171{
Janos Follath24eed8d2019-11-22 13:21:35 +00001172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001173 ((void) md_alg);
1174
1175 ret = mbedtls_ecdsa_read_signature_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 (mbedtls_ecdsa_context *) ctx,
1177 hash, hash_len, sig, sig_len,
1178 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001179
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1181 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1182 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001183
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001185}
1186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1188 const unsigned char *hash, size_t hash_len,
1189 unsigned char *sig, size_t sig_size, size_t *sig_len,
1190 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1191 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001192{
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 return mbedtls_ecdsa_write_signature_restartable(
1194 (mbedtls_ecdsa_context *) ctx,
1195 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1196 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001197
1198}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001201{
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001203
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 if (ctx != NULL) {
1205 mbedtls_ecdsa_restart_init(ctx);
1206 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001207
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001209}
1210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001212{
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 mbedtls_ecdsa_restart_free(ctx);
1214 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001215}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001216#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001218const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1219 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001220 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001221 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001222 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001223#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1224 ecdsa_verify_wrap, /* Compatible key structures */
1225#else
1226 NULL,
1227#endif
1228#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1229 ecdsa_sign_wrap, /* Compatible key structures */
1230#else
1231 NULL,
1232#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001233#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001234 ecdsa_verify_rs_wrap,
1235 ecdsa_sign_rs_wrap,
1236#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001237 NULL,
1238 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001239 eckey_check_pair, /* Compatible key structures */
Valerio Setti24138d92023-01-27 14:24:09 +01001240 eckey_alloc_wrap, /* Compatible key structures */
1241 eckey_free_wrap, /* Compatible key structures */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001242#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001243 ecdsa_rs_alloc,
1244 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001245#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001246 eckey_debug, /* Compatible key structures */
1247};
Valerio Setti7ca13182023-01-27 13:22:42 +01001248#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001251/*
1252 * Support for alternative RSA-private implementations
1253 */
1254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001256{
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001258}
1259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260static size_t rsa_alt_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001261{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001265}
1266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1268 const unsigned char *hash, size_t hash_len,
1269 unsigned char *sig, size_t sig_size, size_t *sig_len,
1270 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001271{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001273
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001274#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (UINT_MAX < hash_len) {
1276 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1277 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001278#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1281 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1282 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1283 }
1284 if (*sig_len > sig_size) {
1285 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1286 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001287
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1289 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001290}
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292static int rsa_alt_decrypt_wrap(void *ctx,
1293 const unsigned char *input, size_t ilen,
1294 unsigned char *output, size_t *olen, size_t osize,
1295 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001296{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001297 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001298
1299 ((void) f_rng);
1300 ((void) p_rng);
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1303 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1304 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 return rsa_alt->decrypt_func(rsa_alt->key,
1307 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001308}
1309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001310#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001311static int rsa_alt_check_pair(const void *pub, const void *prv,
1312 int (*f_rng)(void *, unsigned char *, size_t),
1313 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001316 unsigned char hash[32];
1317 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1321 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001322 }
1323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 memset(hash, 0x2a, sizeof(hash));
1325
1326 if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE,
1327 hash, sizeof(hash),
1328 sig, sizeof(sig), &sig_len,
1329 f_rng, p_rng)) != 0) {
1330 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001331 }
1332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE,
1334 hash, sizeof(hash), sig, sig_len) != 0) {
1335 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1336 }
1337
1338 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001339}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001343{
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001345
Gilles Peskine449bd832023-01-11 14:50:10 +01001346 if (ctx != NULL) {
1347 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1348 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001349
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001351}
1352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001354{
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1356 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001357}
1358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1360 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001361 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001362 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001363 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001364 NULL,
1365 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001366#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001367 NULL,
1368 NULL,
1369#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001370 rsa_alt_decrypt_wrap,
1371 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001373 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001374#else
1375 NULL,
1376#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001377 rsa_alt_alloc_wrap,
1378 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001379#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001380 NULL,
1381 NULL,
1382#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001383 NULL,
1384};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001387
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001388#if defined(MBEDTLS_USE_PSA_CRYPTO)
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390static void *pk_opaque_alloc_wrap(void)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001391{
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t));
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001393
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001394 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001395
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 return ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001397}
1398
Gilles Peskine449bd832023-01-11 14:50:10 +01001399static void pk_opaque_free_wrap(void *ctx)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001400{
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t));
1402 mbedtls_free(ctx);
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001403}
1404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405static size_t pk_opaque_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001406{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001407 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001408 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001409 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) {
1412 return 0;
1413 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 bits = psa_get_key_bits(&attributes);
1416 psa_reset_key_attributes(&attributes);
1417 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001418}
1419
Gilles Peskine449bd832023-01-11 14:50:10 +01001420static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001421{
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 return type == MBEDTLS_PK_ECKEY ||
1423 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001424}
1425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001427{
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 return type == MBEDTLS_PK_RSA ||
1429 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001430}
1431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1433 const unsigned char *hash, size_t hash_len,
1434 unsigned char *sig, size_t sig_size, size_t *sig_len,
1435 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001436{
Valerio Settiab363d92023-01-26 14:31:54 +01001437#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001438 ((void) ctx);
1439 ((void) md_alg);
1440 ((void) hash);
1441 ((void) hash_len);
1442 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001443 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001444 ((void) sig_len);
1445 ((void) f_rng);
1446 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001448#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001449 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001451 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001452 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001453 psa_status_t status;
1454
1455 /* PSA has its own RNG */
1456 (void) f_rng;
1457 (void) p_rng;
1458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 status = psa_get_key_attributes(*key, &attributes);
1460 if (status != PSA_SUCCESS) {
1461 return mbedtls_pk_error_from_psa(status);
1462 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 type = psa_get_key_type(&attributes);
1465 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001466
Valerio Settiab363d92023-01-26 14:31:54 +01001467#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1469 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1470 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001471#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001472#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (PSA_KEY_TYPE_IS_RSA(type)) {
1474 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1475 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001476#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001478
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001479 /* make the signature */
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 status = psa_sign_hash(*key, alg, hash, hash_len,
1481 sig, sig_size, sig_len);
1482 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001483#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1485 return mbedtls_pk_error_from_psa_ecdsa(status);
1486 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001487#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001488#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (PSA_KEY_TYPE_IS_RSA(type)) {
1490 return mbedtls_pk_error_from_psa_rsa(status);
1491 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001492#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 return mbedtls_pk_error_from_psa(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001494 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001495
Valerio Settiab363d92023-01-26 14:31:54 +01001496#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001498 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1500 }
Valerio Settiab363d92023-01-26 14:31:54 +01001501#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001502
1503 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001504#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001505}
1506
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001507const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001508 MBEDTLS_PK_OPAQUE,
1509 "Opaque",
1510 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001511 pk_opaque_ecdsa_can_do,
1512 NULL, /* verify - will be done later */
1513 pk_opaque_sign_wrap,
1514#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1515 NULL, /* restartable verify - not relevant */
1516 NULL, /* restartable sign - not relevant */
1517#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001518 NULL, /* decrypt - not relevant */
1519 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001520 NULL, /* check_pair - could be done later or left NULL */
1521 pk_opaque_alloc_wrap,
1522 pk_opaque_free_wrap,
1523#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1524 NULL, /* restart alloc - not relevant */
1525 NULL, /* restart free - not relevant */
1526#endif
1527 NULL, /* debug - could be done later, or even left NULL */
1528};
1529
Neil Armstrong30beca32022-05-03 15:42:13 +02001530#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01001531static int pk_opaque_rsa_decrypt(void *ctx,
1532 const unsigned char *input, size_t ilen,
1533 unsigned char *output, size_t *olen, size_t osize,
1534 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001535{
1536 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1537 psa_status_t status;
1538
1539 /* PSA has its own RNG */
1540 (void) f_rng;
1541 (void) p_rng;
1542
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1544 input, ilen,
1545 NULL, 0,
1546 output, osize, olen);
1547 if (status != PSA_SUCCESS) {
1548 return mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001549 }
1550
1551 return 0;
1552}
Neil Armstrong30beca32022-05-03 15:42:13 +02001553#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001554
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001555const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1556 MBEDTLS_PK_OPAQUE,
1557 "Opaque",
1558 pk_opaque_get_bitlen,
1559 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001560 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001561 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001562#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1563 NULL, /* restartable verify - not relevant */
1564 NULL, /* restartable sign - not relevant */
1565#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001566#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001567 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001568#else
1569 NULL, /* decrypt - not available */
1570#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001571 NULL, /* encrypt - will be done later */
1572 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001573 pk_opaque_alloc_wrap,
1574 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001575#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1576 NULL, /* restart alloc - not relevant */
1577 NULL, /* restart free - not relevant */
1578#endif
1579 NULL, /* debug - could be done later, or even left NULL */
1580};
1581
1582#endif /* MBEDTLS_USE_PSA_CRYPTO */
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_PK_C */