blob: 45cf807c66822738ee550cc1821da900b4867daf [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
Valerio Setti80d07982023-02-08 13:49:17 +010048#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020049#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
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
175 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
176 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (sig_len < rsa_len) {
179 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
180 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100181
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100182 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100183 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100184 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100185 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
187 if (key_len <= 0) {
188 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
189 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
192 psa_set_key_algorithm(&attributes, psa_alg_md);
193 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 status = psa_import_key(&attributes,
196 buf + sizeof(buf) - key_len, key_len,
197 &key_id);
198 if (status != PSA_SUCCESS) {
199 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100200 goto cleanup;
201 }
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
204 sig, sig_len);
205 if (status != PSA_SUCCESS) {
206 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100207 goto cleanup;
208 }
209 ret = 0;
210
211cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 status = psa_destroy_key(key_id);
213 if (ret == 0 && status != PSA_SUCCESS) {
214 ret = mbedtls_pk_error_from_psa(status);
215 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100218}
219#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100220static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
221 const unsigned char *hash, size_t hash_len,
222 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100223{
Janos Follath24eed8d2019-11-22 13:21:35 +0000224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
226 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
229 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
230 }
Andres AG72849872017-01-19 11:24:33 +0000231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (sig_len < rsa_len) {
233 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
234 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
237 (unsigned int) hash_len,
238 hash, sig)) != 0) {
239 return ret;
240 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200241
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200242 /* The buffer contains a valid signature followed by extra data.
243 * We have a special error code for that so that so that callers can
244 * use mbedtls_pk_verify() to check "Does the buffer start with a
245 * valid signature?" and not just "Does the buffer contain a valid
246 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (sig_len > rsa_len) {
248 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
249 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200252}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100253#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200254
Jerry Yub02ee182022-03-16 10:30:41 +0800255#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100256int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
257 mbedtls_rsa_context *rsa_ctx,
258 const unsigned char *hash, size_t hash_len,
259 unsigned char *sig, size_t sig_size,
260 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100261{
Neil Armstrong98545682022-02-22 16:12:51 +0100262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
263 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
264 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
265 psa_status_t status;
266 mbedtls_pk_context key;
267 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100268 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100269 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
272 if (sig_size < *sig_len) {
273 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
274 }
Neil Armstrong98545682022-02-22 16:12:51 +0100275
Neil Armstronge4f28682022-02-24 15:41:39 +0100276 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100277 * re-construct one to make it happy */
278 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800279 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
281 if (key_len <= 0) {
282 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
283 }
284 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
285 psa_set_key_algorithm(&attributes, alg);
286 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 status = psa_import_key(&attributes,
289 buf + sizeof(buf) - key_len, key_len,
290 &key_id);
291 if (status != PSA_SUCCESS) {
292 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100293 goto cleanup;
294 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 status = psa_sign_hash(key_id, alg, hash, hash_len,
296 sig, sig_size, sig_len);
297 if (status != PSA_SUCCESS) {
298 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100299 goto cleanup;
300 }
301
302 ret = 0;
303
304cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 status = psa_destroy_key(key_id);
306 if (ret == 0 && status != PSA_SUCCESS) {
307 ret = mbedtls_pk_error_from_psa(status);
308 }
309 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100310}
Jerry Yu406cf272022-03-22 11:33:42 +0800311#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800312
313#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100314static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
315 const unsigned char *hash, size_t hash_len,
316 unsigned char *sig, size_t sig_size, size_t *sig_len,
317 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800318{
Jerry Yu1d172a32022-03-12 19:12:05 +0800319 ((void) f_rng);
320 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800321
Jerry Yubd1b3272022-03-24 13:05:20 +0800322 psa_algorithm_t psa_md_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
324 if (psa_md_alg == 0) {
325 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
326 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
329 psa_md_alg),
330 ctx, hash, hash_len,
331 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800332}
Neil Armstrong98545682022-02-22 16:12:51 +0100333#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100334static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
335 const unsigned char *hash, size_t hash_len,
336 unsigned char *sig, size_t sig_size, size_t *sig_len,
337 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200338{
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
342 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
343 }
Andres AG72849872017-01-19 11:24:33 +0000344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 *sig_len = mbedtls_rsa_get_len(rsa);
346 if (sig_size < *sig_len) {
347 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
348 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
351 md_alg, (unsigned int) hash_len,
352 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200353}
Neil Armstrong98545682022-02-22 16:12:51 +0100354#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200355
Neil Armstrong18f43c72022-02-09 15:32:45 +0100356#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100357static int rsa_decrypt_wrap(void *ctx,
358 const unsigned char *input, size_t ilen,
359 unsigned char *output, size_t *olen, size_t osize,
360 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100361{
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100363 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
364 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
365 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
366 psa_status_t status;
367 mbedtls_pk_context key;
368 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100369 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100370
371 ((void) f_rng);
372 ((void) p_rng);
373
374#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
376 return MBEDTLS_ERR_RSA_INVALID_PADDING;
377 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100378#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (ilen != mbedtls_rsa_get_len(rsa)) {
381 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
382 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100383
384 /* mbedtls_pk_write_key_der() expects a full PK context;
385 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100386 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100387 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
389 if (key_len <= 0) {
390 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
391 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
394 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
395 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100396
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 status = psa_import_key(&attributes,
398 buf + sizeof(buf) - key_len, key_len,
399 &key_id);
400 if (status != PSA_SUCCESS) {
401 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100402 goto cleanup;
403 }
404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
406 input, ilen,
407 NULL, 0,
408 output, osize, olen);
409 if (status != PSA_SUCCESS) {
410 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100411 goto cleanup;
412 }
413
414 ret = 0;
415
416cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 mbedtls_platform_zeroize(buf, sizeof(buf));
418 status = psa_destroy_key(key_id);
419 if (ret == 0 && status != PSA_SUCCESS) {
420 ret = mbedtls_pk_error_from_psa(status);
421 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100424}
425#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100426static int rsa_decrypt_wrap(void *ctx,
427 const unsigned char *input, size_t ilen,
428 unsigned char *output, size_t *olen, size_t osize,
429 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200430{
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (ilen != mbedtls_rsa_get_len(rsa)) {
434 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
435 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
438 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200439}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100440#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200441
Neil Armstrong96a16a42022-02-10 10:40:11 +0100442#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100443static int rsa_encrypt_wrap(void *ctx,
444 const unsigned char *input, size_t ilen,
445 unsigned char *output, size_t *olen, size_t osize,
446 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100447{
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
450 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
451 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
452 psa_status_t status;
453 mbedtls_pk_context key;
454 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100455 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100456
457 ((void) f_rng);
458 ((void) p_rng);
459
460#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
462 return MBEDTLS_ERR_RSA_INVALID_PADDING;
463 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100464#endif
465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (mbedtls_rsa_get_len(rsa) > osize) {
467 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
468 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100469
Neil Armstrongac014ca2022-02-24 15:27:54 +0100470 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100471 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100472 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100473 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
475 if (key_len <= 0) {
476 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
477 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
480 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
481 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 status = psa_import_key(&attributes,
484 buf + sizeof(buf) - key_len, key_len,
485 &key_id);
486 if (status != PSA_SUCCESS) {
487 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100488 goto cleanup;
489 }
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
492 input, ilen,
493 NULL, 0,
494 output, osize, olen);
495 if (status != PSA_SUCCESS) {
496 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100497 goto cleanup;
498 }
499
500 ret = 0;
501
502cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 status = psa_destroy_key(key_id);
504 if (ret == 0 && status != PSA_SUCCESS) {
505 ret = mbedtls_pk_error_from_psa(status);
506 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100509}
510#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100511static int rsa_encrypt_wrap(void *ctx,
512 const unsigned char *input, size_t ilen,
513 unsigned char *output, size_t *olen, size_t osize,
514 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200515{
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
517 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (*olen > osize) {
520 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
521 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
524 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200525}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100526#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528static int rsa_check_pair_wrap(const void *pub, const void *prv,
529 int (*f_rng)(void *, unsigned char *, size_t),
530 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100531{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200532 (void) f_rng;
533 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub,
535 (const mbedtls_rsa_context *) prv);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100536}
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200539{
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (ctx != NULL) {
543 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
544 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200547}
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200550{
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
552 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200553}
554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200556{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200557#if defined(MBEDTLS_RSA_ALT)
558 /* Not supported */
559 (void) ctx;
560 (void) items;
561#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200563 items->name = "rsa.N";
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 items->value = &(((mbedtls_rsa_context *) ctx)->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200565
566 items++;
567
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.E";
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 items->value = &(((mbedtls_rsa_context *) ctx)->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200571#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200572}
573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574const mbedtls_pk_info_t mbedtls_rsa_info = {
575 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200576 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200577 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200578 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200579 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200580 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200581#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200582 NULL,
583 NULL,
584#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200585 rsa_decrypt_wrap,
586 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100587 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200588 rsa_alloc_wrap,
589 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200590#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200591 NULL,
592 NULL,
593#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200594 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200595};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200599/*
600 * Generic EC key
601 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200603{
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 return type == MBEDTLS_PK_ECKEY ||
605 type == MBEDTLS_PK_ECKEY_DH ||
606 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200607}
608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609static size_t eckey_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200610{
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 return ((mbedtls_ecp_keypair *) ctx)->grp.pbits;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200612}
613
Valerio Setti1cdddac2023-02-02 13:55:57 +0100614#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400615#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400616/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500617 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
618 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500619 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
621 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500622{
Janos Follath24eed8d2019-11-22 13:21:35 +0000623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500624 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
627 MBEDTLS_ASN1_INTEGER)) != 0) {
628 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500629 }
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 while (unpadded_len > 0 && **from == 0x00) {
632 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500633 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500634 }
635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 if (unpadded_len > to_len || unpadded_len == 0) {
637 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
638 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500639
Andrzej Kurek9241d182018-11-20 05:04:35 -0500640 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 memset(to, 0x00, padding_len);
642 memcpy(to + padding_len, *from, unpadded_len);
643 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500646}
647
648/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400649 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500650 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500651 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400652 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100653static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
654 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400655{
Janos Follath24eed8d2019-11-22 13:21:35 +0000656 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500657 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
660 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
661 return ret;
662 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400663
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500664 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
666 return ret;
667 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500668 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
670 return ret;
671 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400674}
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
677 const unsigned char *hash, size_t hash_len,
678 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400679{
Valerio Settiab363d92023-01-26 14:31:54 +0100680 mbedtls_ecp_keypair *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000681 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200682 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100683 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200684 psa_status_t status;
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200685 size_t key_len;
Valerio Setti5c032b52023-02-02 15:10:32 +0100686 /* This buffer will initially contain the public key and then the signature
687 * but at different points in time. For all curves except secp224k1, which
688 * is not currently supported in PSA, the public key is one byte longer
689 * (header byte + 2 numbers, while the signature is only 2 numbers),
690 * so use that as the buffer size. */
Valerio Setti1337a4f2023-01-30 15:54:55 +0100691 unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Hanno Beckera9851112019-01-25 16:37:10 +0000692 unsigned char *p;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700693 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100694 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100695 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
697 const size_t signature_part_size = (ctx->grp.nbits + 7) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700698 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (curve == 0) {
701 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
702 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
705 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
706 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500707
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200708 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
709 MBEDTLS_ECP_PF_UNCOMPRESSED,
710 &key_len, buf, sizeof(buf));
711 if (ret != 0) {
712 goto cleanup;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500713 }
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 status = psa_import_key(&attributes,
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200716 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 &key_id);
718 if (status != PSA_SUCCESS) {
719 ret = mbedtls_pk_error_from_psa(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400720 goto cleanup;
721 }
722
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500723 /* We don't need the exported key anymore and can
724 * reuse its buffer for signature extraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (2 * signature_part_size > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500726 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
727 goto cleanup;
728 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 p = (unsigned char *) sig;
731 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
732 signature_part_size)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500733 goto cleanup;
734 }
735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 status = psa_verify_hash(key_id, psa_sig_md,
737 hash, hash_len,
738 buf, 2 * signature_part_size);
739 if (status != PSA_SUCCESS) {
740 ret = mbedtls_pk_error_from_psa_ecdsa(status);
741 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400742 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500745 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
746 goto cleanup;
747 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400748 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400749
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500750cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 status = psa_destroy_key(key_id);
752 if (ret == 0 && status != PSA_SUCCESS) {
753 ret = mbedtls_pk_error_from_psa(status);
754 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400757}
758#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100759static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
760 const unsigned char *hash, size_t hash_len,
761 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200762{
Janos Follath24eed8d2019-11-22 13:21:35 +0000763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200764 ((void) md_alg);
765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx,
767 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
770 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
771 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200774}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400775#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100776#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200777
Valerio Setti1cdddac2023-02-02 13:55:57 +0100778#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100779#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100780/*
781 * Simultaneously convert and move raw MPI from the beginning of a buffer
782 * to an ASN.1 MPI at the end of the buffer.
783 * See also mbedtls_asn1_write_mpi().
784 *
785 * p: pointer to the end of the output buffer
786 * start: start of the output buffer, and also of the mpi to write at the end
787 * n_len: length of the mpi to read from start
788 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
790 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100791{
792 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
793 size_t len = 0;
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if ((size_t) (*p - start) < n_len) {
796 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
797 }
Neil Armstrong15021652022-03-01 10:14:17 +0100798
799 len = n_len;
800 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100802
803 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
804 * Neither r nor s should be 0, but as a failsafe measure, still detect
805 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100807 ++(*p);
808 --len;
809 }
810
811 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (len == 0) {
813 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
814 }
Neil Armstrong15021652022-03-01 10:14:17 +0100815
816 /* if the msb is 1, ASN.1 requires that we prepend a 0.
817 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (**p & 0x80) {
819 if (*p - start < 1) {
820 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
821 }
Neil Armstrong15021652022-03-01 10:14:17 +0100822
823 *--(*p) = 0x00;
824 len += 1;
825 }
826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
828 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
829 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100832}
833
834/* Transcode signature from PSA format to ASN.1 sequence.
835 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
836 * MPIs, and in-place.
837 *
838 * [in/out] sig: the signature pre- and post-transcoding
839 * [in/out] sig_len: signature length pre- and post-transcoding
840 * [int] buf_len: the available size the in/out buffer
841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
843 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100844{
845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
846 size_t len = 0;
847 const size_t rs_len = *sig_len / 2;
848 unsigned char *p = sig + buf_len;
849
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
851 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
854 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
855 MBEDTLS_ASN1_CONSTRUCTED |
856 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100857
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100859 *sig_len = len;
860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100862}
Neil Armstronge9606902022-02-09 14:23:00 +0100863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
865 const unsigned char *hash, size_t hash_len,
866 unsigned char *sig, size_t sig_size, size_t *sig_len,
867 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100868{
Valerio Settiab363d92023-01-26 14:31:54 +0100869 mbedtls_ecp_keypair *ctx = ctx_arg;
Neil Armstronge9606902022-02-09 14:23:00 +0100870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
871 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
872 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
873 psa_status_t status;
Valerio Setti1337a4f2023-01-30 15:54:55 +0100874 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100875#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Valerio Setti5bc52242023-01-30 15:48:28 +0100876 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100877 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100878#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200879 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100880 PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100881#endif
Neil Armstronge9606902022-02-09 14:23:00 +0100882 size_t curve_bits;
883 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200885 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100886
887 /* PSA has its own RNG */
888 ((void) f_rng);
889 ((void) p_rng);
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (curve == 0) {
892 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
893 }
Neil Armstronge9606902022-02-09 14:23:00 +0100894
Gilles Peskine13caa942022-10-04 22:59:26 +0200895 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100896 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200898 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100900 goto cleanup;
901 }
902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
904 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
905 psa_set_key_algorithm(&attributes, psa_sig_md);
906
907 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200908 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 &key_id);
910 if (status != PSA_SUCCESS) {
911 ret = mbedtls_pk_error_from_psa(status);
912 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100913 }
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
916 sig, sig_size, sig_len);
917 if (status != PSA_SUCCESS) {
918 ret = mbedtls_pk_error_from_psa_ecdsa(status);
919 goto cleanup;
920 }
921
922 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100923
924cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 mbedtls_platform_zeroize(buf, sizeof(buf));
926 status = psa_destroy_key(key_id);
927 if (ret == 0 && status != PSA_SUCCESS) {
928 ret = mbedtls_pk_error_from_psa(status);
929 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100932}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100933#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
935 const unsigned char *hash, size_t hash_len,
936 unsigned char *sig, size_t sig_size, size_t *sig_len,
937 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200938{
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
940 md_alg, hash, hash_len,
941 sig, sig_size, sig_len,
942 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200943}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100944#endif /* MBEDTLS_USE_PSA_CRYPTO */
945#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200946
Valerio Setti80d07982023-02-08 13:49:17 +0100947#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +0100948/* Forward declarations */
949static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
950 const unsigned char *hash, size_t hash_len,
951 const unsigned char *sig, size_t sig_len,
952 void *rs_ctx);
953
954static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
955 const unsigned char *hash, size_t hash_len,
956 unsigned char *sig, size_t sig_size, size_t *sig_len,
957 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
958 void *rs_ctx);
959
960/*
961 * Restart context for ECDSA operations with ECKEY context
962 *
963 * We need to store an actual ECDSA context, as we need to pass the same to
964 * the underlying ecdsa function, so we can't create it on the fly every time.
965 */
966typedef struct {
967 mbedtls_ecdsa_restart_ctx ecdsa_rs;
968 mbedtls_ecdsa_context ecdsa_ctx;
969} eckey_restart_ctx;
970
971static void *eckey_rs_alloc(void)
972{
973 eckey_restart_ctx *rs_ctx;
974
975 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
976
977 if (ctx != NULL) {
978 rs_ctx = ctx;
979 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
980 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
981 }
982
983 return ctx;
984}
985
986static void eckey_rs_free(void *ctx)
987{
988 eckey_restart_ctx *rs_ctx;
989
990 if (ctx == NULL) {
991 return;
992 }
993
994 rs_ctx = ctx;
995 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
996 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
997
998 mbedtls_free(ctx);
999}
1000
1001static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1002 const unsigned char *hash, size_t hash_len,
1003 const unsigned char *sig, size_t sig_len,
1004 void *rs_ctx)
1005{
1006 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1007 eckey_restart_ctx *rs = rs_ctx;
1008
1009 /* Should never happen */
1010 if (rs == NULL) {
1011 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1012 }
1013
1014 /* set up our own sub-context if needed (that is, on first run) */
1015 if (rs->ecdsa_ctx.grp.pbits == 0) {
1016 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1017 }
1018
1019 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx,
1020 md_alg, hash, hash_len,
1021 sig, sig_len, &rs->ecdsa_rs));
1022
1023cleanup:
1024 return ret;
1025}
1026
1027static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1028 const unsigned char *hash, size_t hash_len,
1029 unsigned char *sig, size_t sig_size, size_t *sig_len,
1030 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1031 void *rs_ctx)
1032{
1033 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1034 eckey_restart_ctx *rs = rs_ctx;
1035
1036 /* Should never happen */
1037 if (rs == NULL) {
1038 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1039 }
1040
1041 /* set up our own sub-context if needed (that is, on first run) */
1042 if (rs->ecdsa_ctx.grp.pbits == 0) {
1043 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1044 }
1045
1046 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg,
1047 hash, hash_len, sig, sig_size, sig_len,
1048 f_rng, p_rng, &rs->ecdsa_rs));
1049
1050cleanup:
1051 return ret;
1052}
Valerio Setti80d07982023-02-08 13:49:17 +01001053#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001054
1055static int eckey_check_pair(const void *pub, const void *prv,
1056 int (*f_rng)(void *, unsigned char *, size_t),
1057 void *p_rng)
1058{
1059 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
1060 (const mbedtls_ecp_keypair *) prv,
1061 f_rng, p_rng);
1062}
1063
1064static void *eckey_alloc_wrap(void)
1065{
1066 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1067
1068 if (ctx != NULL) {
1069 mbedtls_ecp_keypair_init(ctx);
1070 }
1071
1072 return ctx;
1073}
1074
1075static void eckey_free_wrap(void *ctx)
1076{
1077 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1078 mbedtls_free(ctx);
1079}
1080
1081static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
1082{
1083 items->type = MBEDTLS_PK_DEBUG_ECP;
1084 items->name = "eckey.Q";
1085 items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
1086}
1087
1088const mbedtls_pk_info_t mbedtls_eckey_info = {
1089 MBEDTLS_PK_ECKEY,
1090 "EC",
1091 eckey_get_bitlen,
1092 eckey_can_do,
1093#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1094 ecdsa_verify_wrap, /* Compatible key structures */
1095#else
1096 NULL,
1097#endif
1098#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1099 ecdsa_sign_wrap, /* Compatible key structures */
1100#else
1101 NULL,
1102#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001103#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001104 eckey_verify_rs_wrap,
1105 eckey_sign_rs_wrap,
1106#endif
1107 NULL,
1108 NULL,
1109 eckey_check_pair,
1110 eckey_alloc_wrap,
1111 eckey_free_wrap,
1112#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1113 eckey_rs_alloc,
1114 eckey_rs_free,
1115#endif
1116 eckey_debug,
1117};
1118
1119/*
1120 * EC key restricted to ECDH
1121 */
1122static int eckeydh_can_do(mbedtls_pk_type_t type)
1123{
1124 return type == MBEDTLS_PK_ECKEY ||
1125 type == MBEDTLS_PK_ECKEY_DH;
1126}
1127
1128const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1129 MBEDTLS_PK_ECKEY_DH,
1130 "EC_DH",
1131 eckey_get_bitlen, /* Same underlying key structure */
1132 eckeydh_can_do,
1133 NULL,
1134 NULL,
1135#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1136 NULL,
1137 NULL,
1138#endif
1139 NULL,
1140 NULL,
1141 eckey_check_pair,
1142 eckey_alloc_wrap, /* Same underlying key structure */
1143 eckey_free_wrap, /* Same underlying key structure */
1144#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1145 NULL,
1146 NULL,
1147#endif
1148 eckey_debug, /* Same underlying key structure */
1149};
1150#endif /* MBEDTLS_ECP_C */
1151
1152#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1153static int ecdsa_can_do(mbedtls_pk_type_t type)
1154{
1155 return type == MBEDTLS_PK_ECDSA;
1156}
1157
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001158#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001159static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1160 const unsigned char *hash, size_t hash_len,
1161 const unsigned char *sig, size_t sig_len,
1162 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001163{
Janos Follath24eed8d2019-11-22 13:21:35 +00001164 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001165 ((void) md_alg);
1166
1167 ret = mbedtls_ecdsa_read_signature_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 (mbedtls_ecdsa_context *) ctx,
1169 hash, hash_len, sig, sig_len,
1170 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1173 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1174 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001177}
1178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1180 const unsigned char *hash, size_t hash_len,
1181 unsigned char *sig, size_t sig_size, size_t *sig_len,
1182 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1183 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001184{
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 return mbedtls_ecdsa_write_signature_restartable(
1186 (mbedtls_ecdsa_context *) ctx,
1187 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1188 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001189
1190}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001193{
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (ctx != NULL) {
1197 mbedtls_ecdsa_restart_init(ctx);
1198 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001199
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001201}
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001204{
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 mbedtls_ecdsa_restart_free(ctx);
1206 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001207}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001208#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1211 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001212 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001213 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001214 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001215#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1216 ecdsa_verify_wrap, /* Compatible key structures */
1217#else
1218 NULL,
1219#endif
1220#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1221 ecdsa_sign_wrap, /* Compatible key structures */
1222#else
1223 NULL,
1224#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001225#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001226 ecdsa_verify_rs_wrap,
1227 ecdsa_sign_rs_wrap,
1228#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001229 NULL,
1230 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001231 eckey_check_pair, /* Compatible key structures */
Valerio Setti24138d92023-01-27 14:24:09 +01001232 eckey_alloc_wrap, /* Compatible key structures */
1233 eckey_free_wrap, /* Compatible key structures */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001234#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001235 ecdsa_rs_alloc,
1236 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001237#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001238 eckey_debug, /* Compatible key structures */
1239};
Valerio Setti7ca13182023-01-27 13:22:42 +01001240#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001243/*
1244 * Support for alternative RSA-private implementations
1245 */
1246
Gilles Peskine449bd832023-01-11 14:50:10 +01001247static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001248{
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001250}
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252static size_t rsa_alt_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001254 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001257}
1258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1260 const unsigned char *hash, size_t hash_len,
1261 unsigned char *sig, size_t sig_size, size_t *sig_len,
1262 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001263{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001265
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 if (UINT_MAX < hash_len) {
1267 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1268 }
Andres AG72849872017-01-19 11:24:33 +00001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1271 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1272 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1273 }
1274 if (*sig_len > sig_size) {
1275 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1276 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001277
Gilles Peskine449bd832023-01-11 14:50:10 +01001278 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1279 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001280}
1281
Gilles Peskine449bd832023-01-11 14:50:10 +01001282static int rsa_alt_decrypt_wrap(void *ctx,
1283 const unsigned char *input, size_t ilen,
1284 unsigned char *output, size_t *olen, size_t osize,
1285 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001286{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001287 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001288
1289 ((void) f_rng);
1290 ((void) p_rng);
1291
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1293 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1294 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001295
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 return rsa_alt->decrypt_func(rsa_alt->key,
1297 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001298}
1299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001301static int rsa_alt_check_pair(const void *pub, const void *prv,
1302 int (*f_rng)(void *, unsigned char *, size_t),
1303 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001306 unsigned char hash[32];
1307 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1311 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001312 }
1313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 memset(hash, 0x2a, sizeof(hash));
1315
1316 if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE,
1317 hash, sizeof(hash),
1318 sig, sizeof(sig), &sig_len,
1319 f_rng, p_rng)) != 0) {
1320 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001321 }
1322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE,
1324 hash, sizeof(hash), sig, sig_len) != 0) {
1325 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1326 }
1327
1328 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001329}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001330#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001333{
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001335
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 if (ctx != NULL) {
1337 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1338 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001339
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001341}
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001344{
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1346 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001347}
1348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1350 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001351 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001352 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001353 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001354 NULL,
1355 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001356#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001357 NULL,
1358 NULL,
1359#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001360 rsa_alt_decrypt_wrap,
1361 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001363 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001364#else
1365 NULL,
1366#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001367 rsa_alt_alloc_wrap,
1368 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001369#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001370 NULL,
1371 NULL,
1372#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001373 NULL,
1374};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001377
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001378#if defined(MBEDTLS_USE_PSA_CRYPTO)
1379
Gilles Peskine449bd832023-01-11 14:50:10 +01001380static void *pk_opaque_alloc_wrap(void)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001381{
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t));
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001383
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001384 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001385
Gilles Peskine449bd832023-01-11 14:50:10 +01001386 return ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001387}
1388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389static void pk_opaque_free_wrap(void *ctx)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001390{
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t));
1392 mbedtls_free(ctx);
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001393}
1394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395static size_t pk_opaque_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001396{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001397 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001398 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001399 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) {
1402 return 0;
1403 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 bits = psa_get_key_bits(&attributes);
1406 psa_reset_key_attributes(&attributes);
1407 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001408}
1409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001411{
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 return type == MBEDTLS_PK_ECKEY ||
1413 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001414}
1415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001417{
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 return type == MBEDTLS_PK_RSA ||
1419 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001420}
1421
Gilles Peskine449bd832023-01-11 14:50:10 +01001422static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1423 const unsigned char *hash, size_t hash_len,
1424 unsigned char *sig, size_t sig_size, size_t *sig_len,
1425 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001426{
Valerio Settiab363d92023-01-26 14:31:54 +01001427#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001428 ((void) ctx);
1429 ((void) md_alg);
1430 ((void) hash);
1431 ((void) hash_len);
1432 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001433 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001434 ((void) sig_len);
1435 ((void) f_rng);
1436 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001438#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001439 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001440 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001441 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001442 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001443 psa_status_t status;
1444
1445 /* PSA has its own RNG */
1446 (void) f_rng;
1447 (void) p_rng;
1448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 status = psa_get_key_attributes(*key, &attributes);
1450 if (status != PSA_SUCCESS) {
1451 return mbedtls_pk_error_from_psa(status);
1452 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 type = psa_get_key_type(&attributes);
1455 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001456
Valerio Settiab363d92023-01-26 14:31:54 +01001457#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1459 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1460 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001461#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001462#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (PSA_KEY_TYPE_IS_RSA(type)) {
1464 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1465 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001466#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001468
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001469 /* make the signature */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 status = psa_sign_hash(*key, alg, hash, hash_len,
1471 sig, sig_size, sig_len);
1472 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001473#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1475 return mbedtls_pk_error_from_psa_ecdsa(status);
1476 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001477#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001478#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 if (PSA_KEY_TYPE_IS_RSA(type)) {
1480 return mbedtls_pk_error_from_psa_rsa(status);
1481 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001482#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return mbedtls_pk_error_from_psa(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001484 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001485
Valerio Settiab363d92023-01-26 14:31:54 +01001486#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001488 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1490 }
Valerio Settiab363d92023-01-26 14:31:54 +01001491#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001492
1493 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001494#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001495}
1496
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001497const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001498 MBEDTLS_PK_OPAQUE,
1499 "Opaque",
1500 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001501 pk_opaque_ecdsa_can_do,
1502 NULL, /* verify - will be done later */
1503 pk_opaque_sign_wrap,
1504#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1505 NULL, /* restartable verify - not relevant */
1506 NULL, /* restartable sign - not relevant */
1507#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001508 NULL, /* decrypt - not relevant */
1509 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001510 NULL, /* check_pair - could be done later or left NULL */
1511 pk_opaque_alloc_wrap,
1512 pk_opaque_free_wrap,
1513#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1514 NULL, /* restart alloc - not relevant */
1515 NULL, /* restart free - not relevant */
1516#endif
1517 NULL, /* debug - could be done later, or even left NULL */
1518};
1519
Neil Armstrong30beca32022-05-03 15:42:13 +02001520#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01001521static int pk_opaque_rsa_decrypt(void *ctx,
1522 const unsigned char *input, size_t ilen,
1523 unsigned char *output, size_t *olen, size_t osize,
1524 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001525{
1526 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1527 psa_status_t status;
1528
1529 /* PSA has its own RNG */
1530 (void) f_rng;
1531 (void) p_rng;
1532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1534 input, ilen,
1535 NULL, 0,
1536 output, osize, olen);
1537 if (status != PSA_SUCCESS) {
1538 return mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001539 }
1540
1541 return 0;
1542}
Neil Armstrong30beca32022-05-03 15:42:13 +02001543#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001544
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001545const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1546 MBEDTLS_PK_OPAQUE,
1547 "Opaque",
1548 pk_opaque_get_bitlen,
1549 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001550 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001551 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001552#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1553 NULL, /* restartable verify - not relevant */
1554 NULL, /* restartable sign - not relevant */
1555#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001556#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001557 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001558#else
1559 NULL, /* decrypt - not available */
1560#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001561 NULL, /* encrypt - will be done later */
1562 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001563 pk_opaque_alloc_wrap,
1564 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001565#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1566 NULL, /* restart alloc - not relevant */
1567 NULL, /* restart free - not relevant */
1568#endif
1569 NULL, /* debug - could be done later, or even left NULL */
1570};
1571
1572#endif /* MBEDTLS_USE_PSA_CRYPTO */
1573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#endif /* MBEDTLS_PK_C */