blob: 2a71bd8529eea6430ab285b4077b0e5339aabc64 [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. */
Valerio Setti1337a4f2023-01-30 15:54:55 +0100695 unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Hanno Beckera9851112019-01-25 16:37:10 +0000696 unsigned char *p;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700697 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100698 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100699 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
701 const size_t signature_part_size = (ctx->grp.nbits + 7) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700702 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (curve == 0) {
705 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
706 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500707
Valerio Settib761b152023-01-31 14:56:04 +0100708 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
709 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
710 psa_set_key_algorithm(&attributes, psa_sig_md);
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200711
712 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
713 MBEDTLS_ECP_PF_UNCOMPRESSED,
714 &key_len, buf, sizeof(buf));
715 if (ret != 0) {
716 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 status = psa_import_key(&attributes,
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200720 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 &key_id);
722 if (status != PSA_SUCCESS) {
Valerio Settib761b152023-01-31 14:56:04 +0100723 ret = mbedtls_pk_error_from_psa(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400724 goto cleanup;
725 }
726
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500727 /* We don't need the exported key anymore and can
728 * reuse its buffer for signature extraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if (2 * signature_part_size > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500730 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
731 goto cleanup;
732 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 p = (unsigned char *) sig;
735 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
736 signature_part_size)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500737 goto cleanup;
738 }
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 status = psa_verify_hash(key_id, psa_sig_md,
741 hash, hash_len,
742 buf, 2 * signature_part_size);
743 if (status != PSA_SUCCESS) {
744 ret = mbedtls_pk_error_from_psa_ecdsa(status);
745 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400746 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500749 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
750 goto cleanup;
751 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400752 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400753
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500754cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 status = psa_destroy_key(key_id);
756 if (ret == 0 && status != PSA_SUCCESS) {
757 ret = mbedtls_pk_error_from_psa(status);
758 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400761}
762#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
764 const unsigned char *hash, size_t hash_len,
765 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200766{
Janos Follath24eed8d2019-11-22 13:21:35 +0000767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200768 ((void) md_alg);
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx,
771 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
774 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
775 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200778}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400779#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100780#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200781
Valerio Setti1cdddac2023-02-02 13:55:57 +0100782#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100783#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100784/*
785 * Simultaneously convert and move raw MPI from the beginning of a buffer
786 * to an ASN.1 MPI at the end of the buffer.
787 * See also mbedtls_asn1_write_mpi().
788 *
789 * p: pointer to the end of the output buffer
790 * start: start of the output buffer, and also of the mpi to write at the end
791 * n_len: length of the mpi to read from start
792 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100793static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
794 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100795{
796 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
797 size_t len = 0;
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if ((size_t) (*p - start) < n_len) {
800 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
801 }
Neil Armstrong15021652022-03-01 10:14:17 +0100802
803 len = n_len;
804 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100806
807 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
808 * Neither r nor s should be 0, but as a failsafe measure, still detect
809 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100811 ++(*p);
812 --len;
813 }
814
815 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (len == 0) {
817 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
818 }
Neil Armstrong15021652022-03-01 10:14:17 +0100819
820 /* if the msb is 1, ASN.1 requires that we prepend a 0.
821 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 if (**p & 0x80) {
823 if (*p - start < 1) {
824 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
825 }
Neil Armstrong15021652022-03-01 10:14:17 +0100826
827 *--(*p) = 0x00;
828 len += 1;
829 }
830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
832 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
833 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100836}
837
838/* Transcode signature from PSA format to ASN.1 sequence.
839 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
840 * MPIs, and in-place.
841 *
842 * [in/out] sig: the signature pre- and post-transcoding
843 * [in/out] sig_len: signature length pre- and post-transcoding
844 * [int] buf_len: the available size the in/out buffer
845 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100846static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
847 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100848{
849 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
850 size_t len = 0;
851 const size_t rs_len = *sig_len / 2;
852 unsigned char *p = sig + buf_len;
853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
855 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
858 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
859 MBEDTLS_ASN1_CONSTRUCTED |
860 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100863 *sig_len = len;
864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100866}
Neil Armstronge9606902022-02-09 14:23:00 +0100867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
869 const unsigned char *hash, size_t hash_len,
870 unsigned char *sig, size_t sig_size, size_t *sig_len,
871 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100872{
Valerio Settiab363d92023-01-26 14:31:54 +0100873 mbedtls_ecp_keypair *ctx = ctx_arg;
Neil Armstronge9606902022-02-09 14:23:00 +0100874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
875 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
876 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
877 psa_status_t status;
Valerio Setti1337a4f2023-01-30 15:54:55 +0100878 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Valerio Setti5bc52242023-01-30 15:48:28 +0100879#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
880 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100881 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Valerio Setti5bc52242023-01-30 15:48:28 +0100882#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200883 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100884 PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Valerio Setti5bc52242023-01-30 15:48:28 +0100885#endif
Neil Armstronge9606902022-02-09 14:23:00 +0100886 size_t curve_bits;
887 psa_ecc_family_t curve =
Valerio Settib761b152023-01-31 14:56:04 +0100888 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200889 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100890
891 /* PSA has its own RNG */
892 ((void) f_rng);
893 ((void) p_rng);
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if (curve == 0) {
896 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
897 }
Neil Armstronge9606902022-02-09 14:23:00 +0100898
Gilles Peskine13caa942022-10-04 22:59:26 +0200899 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100900 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200902 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Valerio Settib761b152023-01-31 14:56:04 +0100903 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100904 goto cleanup;
905 }
906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
908 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
909 psa_set_key_algorithm(&attributes, psa_sig_md);
910
911 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200912 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 &key_id);
914 if (status != PSA_SUCCESS) {
915 ret = mbedtls_pk_error_from_psa(status);
916 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100917 }
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
920 sig, sig_size, sig_len);
921 if (status != PSA_SUCCESS) {
922 ret = mbedtls_pk_error_from_psa_ecdsa(status);
923 goto cleanup;
924 }
925
926 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100927
928cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 mbedtls_platform_zeroize(buf, sizeof(buf));
930 status = psa_destroy_key(key_id);
931 if (ret == 0 && status != PSA_SUCCESS) {
932 ret = mbedtls_pk_error_from_psa(status);
933 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100936}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100937#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100938static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
939 const unsigned char *hash, size_t hash_len,
940 unsigned char *sig, size_t sig_size, size_t *sig_len,
941 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200942{
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
944 md_alg, hash, hash_len,
945 sig, sig_size, sig_len,
946 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200947}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100948#endif /* MBEDTLS_USE_PSA_CRYPTO */
949#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200950
Valerio Setti5b16e9e2023-02-07 08:08:53 +0100951#if defined(MBEDTLS_ECDSA_C)
Valerio Setti1cdddac2023-02-02 13:55:57 +0100952#if defined(MBEDTLS_ECP_RESTARTABLE)
953/* Forward declarations */
954static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
955 const unsigned char *hash, size_t hash_len,
956 const unsigned char *sig, size_t sig_len,
957 void *rs_ctx);
958
959static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
960 const unsigned char *hash, size_t hash_len,
961 unsigned char *sig, size_t sig_size, size_t *sig_len,
962 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
963 void *rs_ctx);
964
965/*
966 * Restart context for ECDSA operations with ECKEY context
967 *
968 * We need to store an actual ECDSA context, as we need to pass the same to
969 * the underlying ecdsa function, so we can't create it on the fly every time.
970 */
971typedef struct {
972 mbedtls_ecdsa_restart_ctx ecdsa_rs;
973 mbedtls_ecdsa_context ecdsa_ctx;
974} eckey_restart_ctx;
975
976static void *eckey_rs_alloc(void)
977{
978 eckey_restart_ctx *rs_ctx;
979
980 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
981
982 if (ctx != NULL) {
983 rs_ctx = ctx;
984 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
985 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
986 }
987
988 return ctx;
989}
990
991static void eckey_rs_free(void *ctx)
992{
993 eckey_restart_ctx *rs_ctx;
994
995 if (ctx == NULL) {
996 return;
997 }
998
999 rs_ctx = ctx;
1000 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1001 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1002
1003 mbedtls_free(ctx);
1004}
1005
1006static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1007 const unsigned char *hash, size_t hash_len,
1008 const unsigned char *sig, size_t sig_len,
1009 void *rs_ctx)
1010{
1011 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1012 eckey_restart_ctx *rs = rs_ctx;
1013
1014 /* Should never happen */
1015 if (rs == NULL) {
1016 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1017 }
1018
1019 /* set up our own sub-context if needed (that is, on first run) */
1020 if (rs->ecdsa_ctx.grp.pbits == 0) {
1021 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1022 }
1023
1024 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx,
1025 md_alg, hash, hash_len,
1026 sig, sig_len, &rs->ecdsa_rs));
1027
1028cleanup:
1029 return ret;
1030}
1031
1032static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1033 const unsigned char *hash, size_t hash_len,
1034 unsigned char *sig, size_t sig_size, size_t *sig_len,
1035 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1036 void *rs_ctx)
1037{
1038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1039 eckey_restart_ctx *rs = rs_ctx;
1040
1041 /* Should never happen */
1042 if (rs == NULL) {
1043 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1044 }
1045
1046 /* set up our own sub-context if needed (that is, on first run) */
1047 if (rs->ecdsa_ctx.grp.pbits == 0) {
1048 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1049 }
1050
1051 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg,
1052 hash, hash_len, sig, sig_size, sig_len,
1053 f_rng, p_rng, &rs->ecdsa_rs));
1054
1055cleanup:
1056 return ret;
1057}
1058#endif /* MBEDTLS_ECP_RESTARTABLE */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001059#endif /* MBEDTLS_ECDSA_C */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001060
1061static int eckey_check_pair(const void *pub, const void *prv,
1062 int (*f_rng)(void *, unsigned char *, size_t),
1063 void *p_rng)
1064{
1065 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
1066 (const mbedtls_ecp_keypair *) prv,
1067 f_rng, p_rng);
1068}
1069
1070static void *eckey_alloc_wrap(void)
1071{
1072 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1073
1074 if (ctx != NULL) {
1075 mbedtls_ecp_keypair_init(ctx);
1076 }
1077
1078 return ctx;
1079}
1080
1081static void eckey_free_wrap(void *ctx)
1082{
1083 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1084 mbedtls_free(ctx);
1085}
1086
1087static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
1088{
1089 items->type = MBEDTLS_PK_DEBUG_ECP;
1090 items->name = "eckey.Q";
1091 items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
1092}
1093
1094const mbedtls_pk_info_t mbedtls_eckey_info = {
1095 MBEDTLS_PK_ECKEY,
1096 "EC",
1097 eckey_get_bitlen,
1098 eckey_can_do,
1099#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1100 ecdsa_verify_wrap, /* Compatible key structures */
1101#else
1102 NULL,
1103#endif
1104#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1105 ecdsa_sign_wrap, /* Compatible key structures */
1106#else
1107 NULL,
1108#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001109#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001110 eckey_verify_rs_wrap,
1111 eckey_sign_rs_wrap,
1112#endif
1113 NULL,
1114 NULL,
1115 eckey_check_pair,
1116 eckey_alloc_wrap,
1117 eckey_free_wrap,
1118#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1119 eckey_rs_alloc,
1120 eckey_rs_free,
1121#endif
1122 eckey_debug,
1123};
1124
1125/*
1126 * EC key restricted to ECDH
1127 */
1128static int eckeydh_can_do(mbedtls_pk_type_t type)
1129{
1130 return type == MBEDTLS_PK_ECKEY ||
1131 type == MBEDTLS_PK_ECKEY_DH;
1132}
1133
1134const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1135 MBEDTLS_PK_ECKEY_DH,
1136 "EC_DH",
1137 eckey_get_bitlen, /* Same underlying key structure */
1138 eckeydh_can_do,
1139 NULL,
1140 NULL,
1141#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1142 NULL,
1143 NULL,
1144#endif
1145 NULL,
1146 NULL,
1147 eckey_check_pair,
1148 eckey_alloc_wrap, /* Same underlying key structure */
1149 eckey_free_wrap, /* Same underlying key structure */
1150#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1151 NULL,
1152 NULL,
1153#endif
1154 eckey_debug, /* Same underlying key structure */
1155};
1156#endif /* MBEDTLS_ECP_C */
1157
1158#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1159static int ecdsa_can_do(mbedtls_pk_type_t type)
1160{
1161 return type == MBEDTLS_PK_ECDSA;
1162}
1163
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001164#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001165static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1166 const unsigned char *hash, size_t hash_len,
1167 const unsigned char *sig, size_t sig_len,
1168 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001169{
Janos Follath24eed8d2019-11-22 13:21:35 +00001170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001171 ((void) md_alg);
1172
1173 ret = mbedtls_ecdsa_read_signature_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 (mbedtls_ecdsa_context *) ctx,
1175 hash, hash_len, sig, sig_len,
1176 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001177
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1179 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1180 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001181
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001183}
1184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1186 const unsigned char *hash, size_t hash_len,
1187 unsigned char *sig, size_t sig_size, size_t *sig_len,
1188 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1189 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001190{
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 return mbedtls_ecdsa_write_signature_restartable(
1192 (mbedtls_ecdsa_context *) ctx,
1193 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1194 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001195
1196}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001197
Gilles Peskine449bd832023-01-11 14:50:10 +01001198static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001199{
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (ctx != NULL) {
1203 mbedtls_ecdsa_restart_init(ctx);
1204 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001207}
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001210{
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 mbedtls_ecdsa_restart_free(ctx);
1212 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001213}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001214#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1217 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001218 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001219 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001220 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001221#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1222 ecdsa_verify_wrap, /* Compatible key structures */
1223#else
1224 NULL,
1225#endif
1226#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1227 ecdsa_sign_wrap, /* Compatible key structures */
1228#else
1229 NULL,
1230#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001231#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001232 ecdsa_verify_rs_wrap,
1233 ecdsa_sign_rs_wrap,
1234#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001235 NULL,
1236 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001237 eckey_check_pair, /* Compatible key structures */
Valerio Setti24138d92023-01-27 14:24:09 +01001238 eckey_alloc_wrap, /* Compatible key structures */
1239 eckey_free_wrap, /* Compatible key structures */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001240#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001241 ecdsa_rs_alloc,
1242 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001243#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001244 eckey_debug, /* Compatible key structures */
1245};
Valerio Setti7ca13182023-01-27 13:22:42 +01001246#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001249/*
1250 * Support for alternative RSA-private implementations
1251 */
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001254{
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001256}
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258static size_t rsa_alt_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001259{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001261
Gilles Peskine449bd832023-01-11 14:50:10 +01001262 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001263}
1264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1266 const unsigned char *hash, size_t hash_len,
1267 unsigned char *sig, size_t sig_size, size_t *sig_len,
1268 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001271
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001272#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001273 if (UINT_MAX < hash_len) {
1274 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1275 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001276#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1279 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1280 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1281 }
1282 if (*sig_len > sig_size) {
1283 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1284 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001285
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1287 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001288}
1289
Gilles Peskine449bd832023-01-11 14:50:10 +01001290static int rsa_alt_decrypt_wrap(void *ctx,
1291 const unsigned char *input, size_t ilen,
1292 unsigned char *output, size_t *olen, size_t osize,
1293 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001294{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001296
1297 ((void) f_rng);
1298 ((void) p_rng);
1299
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1301 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1302 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001303
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 return rsa_alt->decrypt_func(rsa_alt->key,
1305 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001306}
1307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001308#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001309static int rsa_alt_check_pair(const void *pub, const void *prv,
1310 int (*f_rng)(void *, unsigned char *, size_t),
1311 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001312{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001314 unsigned char hash[32];
1315 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1319 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001320 }
1321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 memset(hash, 0x2a, sizeof(hash));
1323
1324 if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE,
1325 hash, sizeof(hash),
1326 sig, sizeof(sig), &sig_len,
1327 f_rng, p_rng)) != 0) {
1328 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001329 }
1330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE,
1332 hash, sizeof(hash), sig, sig_len) != 0) {
1333 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1334 }
1335
1336 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001337}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001338#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001341{
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (ctx != NULL) {
1345 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1346 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001349}
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001352{
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1354 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001355}
1356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001357const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1358 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001359 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001360 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001361 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001362 NULL,
1363 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001364#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001365 NULL,
1366 NULL,
1367#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001368 rsa_alt_decrypt_wrap,
1369 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001370#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001371 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001372#else
1373 NULL,
1374#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001375 rsa_alt_alloc_wrap,
1376 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001377#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001378 NULL,
1379 NULL,
1380#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001381 NULL,
1382};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001385
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001386#if defined(MBEDTLS_USE_PSA_CRYPTO)
1387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388static void *pk_opaque_alloc_wrap(void)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001389{
Gilles Peskine449bd832023-01-11 14:50:10 +01001390 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t));
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001391
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001392 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001393
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 return ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001395}
1396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397static void pk_opaque_free_wrap(void *ctx)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001398{
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t));
1400 mbedtls_free(ctx);
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001401}
1402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403static size_t pk_opaque_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001404{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001405 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001406 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) {
1410 return 0;
1411 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001412
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 bits = psa_get_key_bits(&attributes);
1414 psa_reset_key_attributes(&attributes);
1415 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001416}
1417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001419{
Gilles Peskine449bd832023-01-11 14:50:10 +01001420 return type == MBEDTLS_PK_ECKEY ||
1421 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001422}
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001425{
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 return type == MBEDTLS_PK_RSA ||
1427 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001428}
1429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1431 const unsigned char *hash, size_t hash_len,
1432 unsigned char *sig, size_t sig_size, size_t *sig_len,
1433 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001434{
Valerio Settiab363d92023-01-26 14:31:54 +01001435#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001436 ((void) ctx);
1437 ((void) md_alg);
1438 ((void) hash);
1439 ((void) hash_len);
1440 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001441 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001442 ((void) sig_len);
1443 ((void) f_rng);
1444 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001446#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001447 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001448 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001449 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001450 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001451 psa_status_t status;
1452
1453 /* PSA has its own RNG */
1454 (void) f_rng;
1455 (void) p_rng;
1456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 status = psa_get_key_attributes(*key, &attributes);
1458 if (status != PSA_SUCCESS) {
1459 return mbedtls_pk_error_from_psa(status);
1460 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 type = psa_get_key_type(&attributes);
1463 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001464
Valerio Settiab363d92023-01-26 14:31:54 +01001465#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1467 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1468 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001469#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001470#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (PSA_KEY_TYPE_IS_RSA(type)) {
1472 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1473 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001474#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001476
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001477 /* make the signature */
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 status = psa_sign_hash(*key, alg, hash, hash_len,
1479 sig, sig_size, sig_len);
1480 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001481#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1483 return mbedtls_pk_error_from_psa_ecdsa(status);
1484 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001485#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001486#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (PSA_KEY_TYPE_IS_RSA(type)) {
1488 return mbedtls_pk_error_from_psa_rsa(status);
1489 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001490#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 return mbedtls_pk_error_from_psa(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001492 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001493
Valerio Settiab363d92023-01-26 14:31:54 +01001494#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001496 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1498 }
Valerio Settiab363d92023-01-26 14:31:54 +01001499#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001500
1501 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001502#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001503}
1504
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001505const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001506 MBEDTLS_PK_OPAQUE,
1507 "Opaque",
1508 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001509 pk_opaque_ecdsa_can_do,
1510 NULL, /* verify - will be done later */
1511 pk_opaque_sign_wrap,
1512#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1513 NULL, /* restartable verify - not relevant */
1514 NULL, /* restartable sign - not relevant */
1515#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001516 NULL, /* decrypt - not relevant */
1517 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001518 NULL, /* check_pair - could be done later or left NULL */
1519 pk_opaque_alloc_wrap,
1520 pk_opaque_free_wrap,
1521#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1522 NULL, /* restart alloc - not relevant */
1523 NULL, /* restart free - not relevant */
1524#endif
1525 NULL, /* debug - could be done later, or even left NULL */
1526};
1527
Neil Armstrong30beca32022-05-03 15:42:13 +02001528#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01001529static int pk_opaque_rsa_decrypt(void *ctx,
1530 const unsigned char *input, size_t ilen,
1531 unsigned char *output, size_t *olen, size_t osize,
1532 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001533{
1534 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1535 psa_status_t status;
1536
1537 /* PSA has its own RNG */
1538 (void) f_rng;
1539 (void) p_rng;
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1542 input, ilen,
1543 NULL, 0,
1544 output, osize, olen);
1545 if (status != PSA_SUCCESS) {
1546 return mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001547 }
1548
1549 return 0;
1550}
Neil Armstrong30beca32022-05-03 15:42:13 +02001551#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001552
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001553const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1554 MBEDTLS_PK_OPAQUE,
1555 "Opaque",
1556 pk_opaque_get_bitlen,
1557 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001558 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001559 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001560#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1561 NULL, /* restartable verify - not relevant */
1562 NULL, /* restartable sign - not relevant */
1563#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001564#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001565 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001566#else
1567 NULL, /* decrypt - not available */
1568#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001569 NULL, /* encrypt - will be done later */
1570 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001571 pk_opaque_alloc_wrap,
1572 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001573#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1574 NULL, /* restart alloc - not relevant */
1575 NULL, /* restart free - not relevant */
1576#endif
1577 NULL, /* debug - could be done later, or even left NULL */
1578};
1579
1580#endif /* MBEDTLS_USE_PSA_CRYPTO */
1581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#endif /* MBEDTLS_PK_C */