blob: e67138b26e23f3b59335635812807deb8218616f [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"
Valerio Settia1b8af62023-05-17 15:34:57 +020026#include "pk_internal.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000027#include "mbedtls/error.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020028#include "md_psa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020029
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020030/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020039#endif
40
Gilles Peskine8a6022e2022-10-04 23:01:59 +020041#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PSA_CRYPTO_C)
42#include "pkwrite.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010043#endif
44
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050045#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020046#include "psa_util_internal.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050047#endif
48
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010049#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040050#include "psa/crypto.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020051
Valerio Setti80d07982023-02-08 13:49:17 +010052#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020053#include "mbedtls/asn1write.h"
54#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010055#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020056#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010057
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059
Andres AG72849872017-01-19 11:24:33 +000060#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010061#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020062#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020063
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050064#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yub02ee182022-03-16 10:30:41 +080065#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010066int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010069 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010070 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010071 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010073 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010074 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010075 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010077 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010078 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010079 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010081 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010083 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010084 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010085 case PSA_ERROR_COMMUNICATION_FAILURE:
86 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010088 case PSA_ERROR_DATA_CORRUPT:
89 case PSA_ERROR_DATA_INVALID:
90 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010091 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010092 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +010094 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010096 }
97}
98
Neil Armstrong30beca32022-05-03 15:42:13 +020099#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
Valerio Settif6d4dfb2023-07-10 10:55:12 +0200100 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100101int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +0100102{
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +0100104 case PSA_ERROR_NOT_PERMITTED:
105 case PSA_ERROR_INVALID_ARGUMENT:
106 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +0100108 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +0100110 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100112 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100114 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Andrzej Kurekba241382022-12-27 09:17:33 -0500116 case PSA_SUCCESS:
117 return 0;
118 case PSA_ERROR_NOT_SUPPORTED:
119 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
120 case PSA_ERROR_INSUFFICIENT_MEMORY:
121 return MBEDTLS_ERR_PK_ALLOC_FAILED;
122 case PSA_ERROR_BAD_STATE:
123 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
124 case PSA_ERROR_COMMUNICATION_FAILURE:
125 case PSA_ERROR_HARDWARE_FAILURE:
126 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
127 case PSA_ERROR_DATA_CORRUPT:
128 case PSA_ERROR_DATA_INVALID:
129 case PSA_ERROR_STORAGE_FAILURE:
130 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
131 case PSA_ERROR_CORRUPTION_DETECTED:
132 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100133 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500134 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +0100135 }
136}
Valerio Settif6d4dfb2023-07-10 10:55:12 +0200137#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Jerry Yu07869e82022-03-16 16:40:50 +0800138#endif /* MBEDTLS_PSA_CRYPTO_C */
139
140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu07869e82022-03-16 16:40:50 +0800141#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100142int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800143{
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800145 case PSA_ERROR_NOT_PERMITTED:
146 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800148 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800150 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800152 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800154 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Andrzej Kurekba241382022-12-27 09:17:33 -0500156 case PSA_SUCCESS:
157 return 0;
158 case PSA_ERROR_NOT_SUPPORTED:
159 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
160 case PSA_ERROR_INSUFFICIENT_MEMORY:
161 return MBEDTLS_ERR_PK_ALLOC_FAILED;
162 case PSA_ERROR_BAD_STATE:
163 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
164 case PSA_ERROR_COMMUNICATION_FAILURE:
165 case PSA_ERROR_HARDWARE_FAILURE:
166 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
167 case PSA_ERROR_DATA_CORRUPT:
168 case PSA_ERROR_DATA_INVALID:
169 case PSA_ERROR_STORAGE_FAILURE:
170 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
171 case PSA_ERROR_CORRUPTION_DETECTED:
172 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu07869e82022-03-16 16:40:50 +0800173 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500174 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Jerry Yu07869e82022-03-16 16:40:50 +0800175 }
176}
Jerry Yu75339822022-03-23 12:06:31 +0800177#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu75339822022-03-23 12:06:31 +0800178#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500179#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Neil Armstrong19915c22022-03-01 15:21:02 +0100180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100182static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200183{
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return type == MBEDTLS_PK_RSA ||
185 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200186}
187
valerio38992cb2023-04-20 09:56:30 +0200188static size_t rsa_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200189{
valerio38992cb2023-04-20 09:56:30 +0200190 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200192}
193
Neil Armstrong52f41f82022-02-22 15:30:24 +0100194#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200195static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 const unsigned char *hash, size_t hash_len,
197 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200198{
valerio38992cb2023-04-20 09:56:30 +0200199 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
202 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
203 psa_status_t status;
204 mbedtls_pk_context key;
205 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100206 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100207 psa_algorithm_t psa_alg_md =
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200208 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
212 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
213 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (sig_len < rsa_len) {
216 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
217 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100218
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100219 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100220 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100221 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200222 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
224 if (key_len <= 0) {
225 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
226 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
229 psa_set_key_algorithm(&attributes, psa_alg_md);
230 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 status = psa_import_key(&attributes,
233 buf + sizeof(buf) - key_len, key_len,
234 &key_id);
235 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500236 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100237 goto cleanup;
238 }
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
241 sig, sig_len);
242 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500243 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100244 goto cleanup;
245 }
246 ret = 0;
247
248cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 status = psa_destroy_key(key_id);
250 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500251 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100255}
Valerio Setti5c26b302023-06-21 19:47:01 +0200256#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200257static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 const unsigned char *hash, size_t hash_len,
259 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100260{
Janos Follath24eed8d2019-11-22 13:21:35 +0000261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valerio38992cb2023-04-20 09:56:30 +0200262 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
266 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
267 }
Andres AG72849872017-01-19 11:24:33 +0000268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (sig_len < rsa_len) {
270 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
271 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
274 (unsigned int) hash_len,
275 hash, sig)) != 0) {
276 return ret;
277 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200278
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200279 /* The buffer contains a valid signature followed by extra data.
280 * We have a special error code for that so that so that callers can
281 * use mbedtls_pk_verify() to check "Does the buffer start with a
282 * valid signature?" and not just "Does the buffer contain a valid
283 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (sig_len > rsa_len) {
285 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
286 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200289}
Valerio Setti5c26b302023-06-21 19:47:01 +0200290#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200291
Jerry Yub02ee182022-03-16 10:30:41 +0800292#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
294 mbedtls_rsa_context *rsa_ctx,
295 const unsigned char *hash, size_t hash_len,
296 unsigned char *sig, size_t sig_size,
297 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100298{
Neil Armstrong98545682022-02-22 16:12:51 +0100299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
301 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
302 psa_status_t status;
303 mbedtls_pk_context key;
304 int key_len;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530305 unsigned char *buf = NULL;
306 buf = mbedtls_calloc(1, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
307 if (buf == NULL) {
308 return MBEDTLS_ERR_PK_ALLOC_FAILED;
309 }
Neil Armstrong98545682022-02-22 16:12:51 +0100310 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
313 if (sig_size < *sig_len) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530314 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
316 }
Neil Armstrong98545682022-02-22 16:12:51 +0100317
Neil Armstronge4f28682022-02-24 15:41:39 +0100318 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100319 * re-construct one to make it happy */
320 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800321 key.pk_ctx = rsa_ctx;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530322 key_len = mbedtls_pk_write_key_der(&key, buf, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (key_len <= 0) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530324 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
326 }
327 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
328 psa_set_key_algorithm(&attributes, alg);
329 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 status = psa_import_key(&attributes,
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530332 buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES - key_len, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 &key_id);
334 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500335 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100336 goto cleanup;
337 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 status = psa_sign_hash(key_id, alg, hash, hash_len,
339 sig, sig_size, sig_len);
340 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500341 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100342 goto cleanup;
343 }
344
345 ret = 0;
346
347cleanup:
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530348 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 status = psa_destroy_key(key_id);
350 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500351 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 }
353 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100354}
Jerry Yu406cf272022-03-22 11:33:42 +0800355#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800356
357#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200358static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 const unsigned char *hash, size_t hash_len,
360 unsigned char *sig, size_t sig_size, size_t *sig_len,
361 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800362{
Jerry Yu1d172a32022-03-12 19:12:05 +0800363 ((void) f_rng);
364 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800365
Jerry Yubd1b3272022-03-24 13:05:20 +0800366 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200367 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (psa_md_alg == 0) {
369 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
370 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
373 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200374 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800376}
Valerio Setti5c26b302023-06-21 19:47:01 +0200377#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200378static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 const unsigned char *hash, size_t hash_len,
380 unsigned char *sig, size_t sig_size, size_t *sig_len,
381 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200382{
valerio38992cb2023-04-20 09:56:30 +0200383 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
386 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
387 }
Andres AG72849872017-01-19 11:24:33 +0000388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 *sig_len = mbedtls_rsa_get_len(rsa);
390 if (sig_size < *sig_len) {
391 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
392 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
395 md_alg, (unsigned int) hash_len,
396 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200397}
Valerio Setti5c26b302023-06-21 19:47:01 +0200398#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200399
Neil Armstrong18f43c72022-02-09 15:32:45 +0100400#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200401static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 const unsigned char *input, size_t ilen,
403 unsigned char *output, size_t *olen, size_t osize,
404 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100405{
valerio38992cb2023-04-20 09:56:30 +0200406 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
409 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
410 psa_status_t status;
411 mbedtls_pk_context key;
412 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100413 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100414
415 ((void) f_rng);
416 ((void) p_rng);
417
418#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
420 return MBEDTLS_ERR_RSA_INVALID_PADDING;
421 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100422#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (ilen != mbedtls_rsa_get_len(rsa)) {
425 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
426 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100427
428 /* mbedtls_pk_write_key_der() expects a full PK context;
429 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100430 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200431 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
433 if (key_len <= 0) {
434 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
435 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
438 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
439 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 status = psa_import_key(&attributes,
442 buf + sizeof(buf) - key_len, key_len,
443 &key_id);
444 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500445 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100446 goto cleanup;
447 }
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
450 input, ilen,
451 NULL, 0,
452 output, osize, olen);
453 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500454 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100455 goto cleanup;
456 }
457
458 ret = 0;
459
460cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 mbedtls_platform_zeroize(buf, sizeof(buf));
462 status = psa_destroy_key(key_id);
463 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500464 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100468}
Valerio Setti5c26b302023-06-21 19:47:01 +0200469#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200470static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 const unsigned char *input, size_t ilen,
472 unsigned char *output, size_t *olen, size_t osize,
473 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200474{
valerio38992cb2023-04-20 09:56:30 +0200475 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (ilen != mbedtls_rsa_get_len(rsa)) {
478 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
479 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
482 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200483}
Valerio Setti5c26b302023-06-21 19:47:01 +0200484#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200485
Neil Armstrong96a16a42022-02-10 10:40:11 +0100486#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200487static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 const unsigned char *input, size_t ilen,
489 unsigned char *output, size_t *olen, size_t osize,
490 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100491{
valerio38992cb2023-04-20 09:56:30 +0200492 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
495 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
496 psa_status_t status;
497 mbedtls_pk_context key;
498 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100499 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100500
501 ((void) f_rng);
502 ((void) p_rng);
503
504#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
506 return MBEDTLS_ERR_RSA_INVALID_PADDING;
507 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100508#endif
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (mbedtls_rsa_get_len(rsa) > osize) {
511 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
512 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100513
Neil Armstrongac014ca2022-02-24 15:27:54 +0100514 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100515 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100516 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200517 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
519 if (key_len <= 0) {
520 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
521 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
524 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
525 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 status = psa_import_key(&attributes,
528 buf + sizeof(buf) - key_len, key_len,
529 &key_id);
530 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500531 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100532 goto cleanup;
533 }
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
536 input, ilen,
537 NULL, 0,
538 output, osize, olen);
539 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500540 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100541 goto cleanup;
542 }
543
544 ret = 0;
545
546cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 status = psa_destroy_key(key_id);
548 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500549 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100553}
Valerio Setti5c26b302023-06-21 19:47:01 +0200554#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200555static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 const unsigned char *input, size_t ilen,
557 unsigned char *output, size_t *olen, size_t osize,
558 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200559{
valerio38992cb2023-04-20 09:56:30 +0200560 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (*olen > osize) {
564 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
565 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
568 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200569}
Valerio Setti5c26b302023-06-21 19:47:01 +0200570#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200571
valerio38992cb2023-04-20 09:56:30 +0200572static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 int (*f_rng)(void *, unsigned char *, size_t),
574 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100575{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200576 (void) f_rng;
577 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200578 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
579 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100580}
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200583{
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ctx != NULL) {
587 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
588 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200591}
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200594{
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
596 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200597}
598
valerio38992cb2023-04-20 09:56:30 +0200599static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200600{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200601#if defined(MBEDTLS_RSA_ALT)
602 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200603 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200604 (void) items;
605#else
valerio38992cb2023-04-20 09:56:30 +0200606 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200609 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200610 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200611
612 items++;
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200615 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200616 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200617#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200618}
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620const mbedtls_pk_info_t mbedtls_rsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +0200621 .type = MBEDTLS_PK_RSA,
622 .name = "RSA",
623 .get_bitlen = rsa_get_bitlen,
624 .can_do = rsa_can_do,
625 .verify_func = rsa_verify_wrap,
626 .sign_func = rsa_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +0200627#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
628 .verify_rs_func = NULL,
629 .sign_rs_func = NULL,
630 .rs_alloc_func = NULL,
631 .rs_free_func = NULL,
632#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +0200633 .decrypt_func = rsa_decrypt_wrap,
634 .encrypt_func = rsa_encrypt_wrap,
635 .check_pair_func = rsa_check_pair_wrap,
636 .ctx_alloc_func = rsa_alloc_wrap,
637 .ctx_free_func = rsa_free_wrap,
638 .debug_func = rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200639};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200641
Valerio Setti81d75122023-06-14 14:49:33 +0200642#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200643/*
644 * Generic EC key
645 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200647{
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return type == MBEDTLS_PK_ECKEY ||
649 type == MBEDTLS_PK_ECKEY_DH ||
650 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200651}
652
valerio38992cb2023-04-20 09:56:30 +0200653static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200654{
Valerio Settia1b8af62023-05-17 15:34:57 +0200655#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
656 return pk->ec_bits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200657#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +0200658 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
659 return ecp->grp.pbits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200660#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200661}
662
Valerio Setti1cdddac2023-02-02 13:55:57 +0100663#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400664#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400665/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500666 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
667 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500668 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100669static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
670 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500671{
Janos Follath24eed8d2019-11-22 13:21:35 +0000672 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500673 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
676 MBEDTLS_ASN1_INTEGER)) != 0) {
677 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500678 }
679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 while (unpadded_len > 0 && **from == 0x00) {
681 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500682 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500683 }
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (unpadded_len > to_len || unpadded_len == 0) {
686 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
687 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500688
Andrzej Kurek9241d182018-11-20 05:04:35 -0500689 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 memset(to, 0x00, padding_len);
691 memcpy(to + padding_len, *from, unpadded_len);
692 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500695}
696
697/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400698 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500699 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500700 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400701 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
703 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400704{
Janos Follath24eed8d2019-11-22 13:21:35 +0000705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500706 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
709 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
710 return ret;
711 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400712
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500713 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
715 return ret;
716 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500717 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
719 return ret;
720 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400723}
724
Valerio Setti76d0f962023-06-23 13:32:54 +0200725/* Common helper for ECDSA verify using PSA functions. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200726static int ecdsa_verify_psa(unsigned char *key, size_t key_len,
727 psa_ecc_family_t curve, size_t curve_bits,
728 const unsigned char *hash, size_t hash_len,
729 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400730{
Janos Follath24eed8d2019-11-22 13:21:35 +0000731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200732 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100733 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settia1b8af62023-05-17 15:34:57 +0200734 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Valerio Settied7d6af2023-06-21 15:42:21 +0200735 size_t signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
736 unsigned char extracted_sig[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
737 unsigned char *p;
738 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (curve == 0) {
741 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
742 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
745 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
746 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500747
Valerio Settied7d6af2023-06-21 15:42:21 +0200748 status = psa_import_key(&attributes, key, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +0100749 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500750 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400751 goto cleanup;
752 }
753
Valerio Settied7d6af2023-06-21 15:42:21 +0200754 if (signature_len > sizeof(extracted_sig)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500755 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
756 goto cleanup;
757 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 p = (unsigned char *) sig;
Valerio Settia1b8af62023-05-17 15:34:57 +0200760 /* extract_ecdsa_sig's last parameter is the size
Valerio Settif57007d2023-05-19 13:54:39 +0200761 * of each integer to be parsed, so it's actually half
Valerio Settia1b8af62023-05-17 15:34:57 +0200762 * the size of the signature. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200763 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, extracted_sig,
Valerio Settia1b8af62023-05-17 15:34:57 +0200764 signature_len/2)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500765 goto cleanup;
766 }
767
Valerio Settied7d6af2023-06-21 15:42:21 +0200768 status = psa_verify_hash(key_id, psa_sig_md, hash, hash_len,
769 extracted_sig, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500771 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400773 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500776 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
777 goto cleanup;
778 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400779 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400780
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500781cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 status = psa_destroy_key(key_id);
783 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500784 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400788}
Valerio Settied7d6af2023-06-21 15:42:21 +0200789
Valerio Setti76d0f962023-06-23 13:32:54 +0200790static int ecdsa_opaque_verify_wrap(mbedtls_pk_context *pk,
791 mbedtls_md_type_t md_alg,
792 const unsigned char *hash, size_t hash_len,
793 const unsigned char *sig, size_t sig_len)
Valerio Settie7730772023-06-21 16:58:40 +0200794{
795 (void) md_alg;
796 unsigned char key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
797 size_t key_len;
798 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
799 psa_ecc_family_t curve;
800 size_t curve_bits;
801 psa_status_t status;
802
803 status = psa_get_key_attributes(pk->priv_id, &key_attr);
804 if (status != PSA_SUCCESS) {
805 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
806 }
807 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(&key_attr));
808 curve_bits = psa_get_key_bits(&key_attr);
809 psa_reset_key_attributes(&key_attr);
810
811 status = psa_export_public_key(pk->priv_id, key, sizeof(key), &key_len);
812 if (status != PSA_SUCCESS) {
813 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
814 }
815
816 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
817 hash, hash_len, sig, sig_len);
818}
819
Valerio Settied7d6af2023-06-21 15:42:21 +0200820#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
821static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
822 mbedtls_md_type_t md_alg,
823 const unsigned char *hash, size_t hash_len,
824 const unsigned char *sig, size_t sig_len)
825{
826 (void) md_alg;
827 psa_ecc_family_t curve = pk->ec_family;
828 size_t curve_bits = pk->ec_bits;
829
830 return ecdsa_verify_psa(pk->pub_raw, pk->pub_raw_len, curve, curve_bits,
831 hash, hash_len, sig, sig_len);
832}
833#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
834static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
835 mbedtls_md_type_t md_alg,
836 const unsigned char *hash, size_t hash_len,
837 const unsigned char *sig, size_t sig_len)
838{
839 (void) md_alg;
840 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
841 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
842 unsigned char key[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
843 size_t key_len;
844 size_t curve_bits;
845 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
846
847 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
848 MBEDTLS_ECP_PF_UNCOMPRESSED,
849 &key_len, key, sizeof(key));
850 if (ret != 0) {
851 return ret;
852 }
853
854 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
855 hash, hash_len, sig, sig_len);
856}
857#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400858#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200859static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 const unsigned char *hash, size_t hash_len,
861 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200862{
Janos Follath24eed8d2019-11-22 13:21:35 +0000863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200864 ((void) md_alg);
865
valerio38992cb2023-04-20 09:56:30 +0200866 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200868
Gilles Peskine449bd832023-01-11 14:50:10 +0100869 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
870 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
871 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200874}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400875#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100876#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200877
Valerio Setti1cdddac2023-02-02 13:55:57 +0100878#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100879#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100880/*
881 * Simultaneously convert and move raw MPI from the beginning of a buffer
882 * to an ASN.1 MPI at the end of the buffer.
883 * See also mbedtls_asn1_write_mpi().
884 *
885 * p: pointer to the end of the output buffer
886 * start: start of the output buffer, and also of the mpi to write at the end
887 * n_len: length of the mpi to read from start
888 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
890 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100891{
892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
893 size_t len = 0;
894
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 if ((size_t) (*p - start) < n_len) {
896 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
897 }
Neil Armstrong15021652022-03-01 10:14:17 +0100898
899 len = n_len;
900 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100902
903 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
904 * Neither r nor s should be 0, but as a failsafe measure, still detect
905 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100907 ++(*p);
908 --len;
909 }
910
911 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if (len == 0) {
913 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
914 }
Neil Armstrong15021652022-03-01 10:14:17 +0100915
916 /* if the msb is 1, ASN.1 requires that we prepend a 0.
917 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 if (**p & 0x80) {
919 if (*p - start < 1) {
920 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
921 }
Neil Armstrong15021652022-03-01 10:14:17 +0100922
923 *--(*p) = 0x00;
924 len += 1;
925 }
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
928 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
929 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100932}
933
934/* Transcode signature from PSA format to ASN.1 sequence.
935 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
936 * MPIs, and in-place.
937 *
938 * [in/out] sig: the signature pre- and post-transcoding
939 * [in/out] sig_len: signature length pre- and post-transcoding
940 * [int] buf_len: the available size the in/out buffer
941 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100942static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
943 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100944{
945 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
946 size_t len = 0;
947 const size_t rs_len = *sig_len / 2;
948 unsigned char *p = sig + buf_len;
949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
951 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
954 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
955 MBEDTLS_ASN1_CONSTRUCTED |
956 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100959 *sig_len = len;
960
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100962}
Neil Armstronge9606902022-02-09 14:23:00 +0100963
Valerio Setti76d0f962023-06-23 13:32:54 +0200964/* Common helper for ECDSA sign using PSA functions. */
Valerio Setti884c1ec2023-06-23 12:09:13 +0200965static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg,
Valerio Setti4657f102023-06-21 13:55:16 +0200966 const unsigned char *hash, size_t hash_len,
967 unsigned char *sig, size_t sig_size, size_t *sig_len)
968{
969 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
970 psa_status_t status;
Valerio Setti884c1ec2023-06-23 12:09:13 +0200971 psa_algorithm_t psa_sig_md;
972 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
973 psa_algorithm_t alg;
974
975 status = psa_get_key_attributes(key_id, &key_attr);
976 if (status != PSA_SUCCESS) {
977 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
978 }
979 alg = psa_get_key_algorithm(&key_attr);
Valerio Setti76d0f962023-06-23 13:32:54 +0200980 psa_reset_key_attributes(&key_attr);
Valerio Setti884c1ec2023-06-23 12:09:13 +0200981
982 if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) {
983 psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
984 } else {
985 psa_sig_md = PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
986 }
Valerio Setti4657f102023-06-21 13:55:16 +0200987
988 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
989 sig, sig_size, sig_len);
990 if (status != PSA_SUCCESS) {
991 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
992 }
993
994 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
995
996 return ret;
997}
998
Valerio Setti76d0f962023-06-23 13:32:54 +0200999static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk,
1000 mbedtls_md_type_t md_alg,
1001 const unsigned char *hash, size_t hash_len,
1002 unsigned char *sig, size_t sig_size,
1003 size_t *sig_len,
1004 int (*f_rng)(void *, unsigned char *, size_t),
1005 void *p_rng)
Valerio Setti4657f102023-06-21 13:55:16 +02001006{
1007 ((void) f_rng);
1008 ((void) p_rng);
Valerio Setti4657f102023-06-21 13:55:16 +02001009
Valerio Setti884c1ec2023-06-23 12:09:13 +02001010 return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size,
Valerio Setti4657f102023-06-21 13:55:16 +02001011 sig_len);
1012}
1013
1014#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti76d0f962023-06-23 13:32:54 +02001015/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1016 * using the same function. */
1017#define ecdsa_sign_wrap ecdsa_opaque_sign_wrap
Valerio Setti4657f102023-06-21 13:55:16 +02001018#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001019static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 const unsigned char *hash, size_t hash_len,
1021 unsigned char *sig, size_t sig_size, size_t *sig_len,
1022 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +01001023{
Neil Armstronge9606902022-02-09 14:23:00 +01001024 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronge9606902022-02-09 14:23:00 +01001025 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1026 psa_status_t status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001027 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
1028 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1029 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Neil Armstronge9606902022-02-09 14:23:00 +01001030 size_t curve_bits;
1031 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +02001033 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti4657f102023-06-21 13:55:16 +02001034#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1035 psa_algorithm_t psa_sig_md =
1036 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
1037#else
1038 psa_algorithm_t psa_sig_md =
1039 PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
1040#endif
Neil Armstronge9606902022-02-09 14:23:00 +01001041 ((void) f_rng);
1042 ((void) p_rng);
1043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if (curve == 0) {
1045 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1046 }
Neil Armstronge9606902022-02-09 14:23:00 +01001047
Gilles Peskine13caa942022-10-04 22:59:26 +02001048 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +01001049 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 }
Gilles Peskine13caa942022-10-04 22:59:26 +02001051 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +01001053 goto cleanup;
1054 }
1055
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1057 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
1058 psa_set_key_algorithm(&attributes, psa_sig_md);
1059
Valerio Setti4657f102023-06-21 13:55:16 +02001060 status = psa_import_key(&attributes, buf, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001062 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +01001064 }
1065
Valerio Setti884c1ec2023-06-23 12:09:13 +02001066 ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len);
Neil Armstronge9606902022-02-09 14:23:00 +01001067
1068cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 mbedtls_platform_zeroize(buf, sizeof(buf));
1070 status = psa_destroy_key(key_id);
1071 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001072 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001074
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +01001076}
Valerio Setti4657f102023-06-21 13:55:16 +02001077#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001078#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +02001079static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 const unsigned char *hash, size_t hash_len,
1081 unsigned char *sig, size_t sig_size, size_t *sig_len,
1082 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001083{
valerio38992cb2023-04-20 09:56:30 +02001084 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 md_alg, hash, hash_len,
1086 sig, sig_size, sig_len,
1087 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001088}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001089#endif /* MBEDTLS_USE_PSA_CRYPTO */
1090#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001091
Valerio Setti80d07982023-02-08 13:49:17 +01001092#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001093/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001094static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001095 const unsigned char *hash, size_t hash_len,
1096 const unsigned char *sig, size_t sig_len,
1097 void *rs_ctx);
1098
valerio38992cb2023-04-20 09:56:30 +02001099static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001100 const unsigned char *hash, size_t hash_len,
1101 unsigned char *sig, size_t sig_size, size_t *sig_len,
1102 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1103 void *rs_ctx);
1104
1105/*
1106 * Restart context for ECDSA operations with ECKEY context
1107 *
1108 * We need to store an actual ECDSA context, as we need to pass the same to
1109 * the underlying ecdsa function, so we can't create it on the fly every time.
1110 */
1111typedef struct {
1112 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1113 mbedtls_ecdsa_context ecdsa_ctx;
1114} eckey_restart_ctx;
1115
1116static void *eckey_rs_alloc(void)
1117{
1118 eckey_restart_ctx *rs_ctx;
1119
1120 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1121
1122 if (ctx != NULL) {
1123 rs_ctx = ctx;
1124 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1125 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1126 }
1127
1128 return ctx;
1129}
1130
1131static void eckey_rs_free(void *ctx)
1132{
1133 eckey_restart_ctx *rs_ctx;
1134
1135 if (ctx == NULL) {
1136 return;
1137 }
1138
1139 rs_ctx = ctx;
1140 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1141 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1142
1143 mbedtls_free(ctx);
1144}
1145
valerio38992cb2023-04-20 09:56:30 +02001146static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001147 const unsigned char *hash, size_t hash_len,
1148 const unsigned char *sig, size_t sig_len,
1149 void *rs_ctx)
1150{
1151 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1152 eckey_restart_ctx *rs = rs_ctx;
1153
1154 /* Should never happen */
1155 if (rs == NULL) {
1156 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1157 }
1158
1159 /* set up our own sub-context if needed (that is, on first run) */
1160 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001161 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001162 }
1163
valerio38992cb2023-04-20 09:56:30 +02001164 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001165 md_alg, hash, hash_len,
1166 sig, sig_len, &rs->ecdsa_rs));
1167
1168cleanup:
1169 return ret;
1170}
1171
valerio38992cb2023-04-20 09:56:30 +02001172static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001173 const unsigned char *hash, size_t hash_len,
1174 unsigned char *sig, size_t sig_size, size_t *sig_len,
1175 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1176 void *rs_ctx)
1177{
1178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1179 eckey_restart_ctx *rs = rs_ctx;
1180
1181 /* Should never happen */
1182 if (rs == NULL) {
1183 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1184 }
1185
1186 /* set up our own sub-context if needed (that is, on first run) */
1187 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001188 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001189 }
1190
valerio38992cb2023-04-20 09:56:30 +02001191 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001192 hash, hash_len, sig, sig_size, sig_len,
1193 f_rng, p_rng, &rs->ecdsa_rs));
1194
1195cleanup:
1196 return ret;
1197}
Valerio Setti80d07982023-02-08 13:49:17 +01001198#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001199
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001200#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settibb7603a2023-06-21 18:34:54 +02001201#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerio38992cb2023-04-20 09:56:30 +02001202static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001203{
Valerio Setti9efa8c42023-05-19 13:27:30 +02001204 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001205 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001206 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001207 size_t prv_key_len;
Valerio Settiae8c6282023-05-18 18:57:57 +02001208 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001209
1210 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1211 &prv_key_len);
1212 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1213 if (ret != 0) {
1214 return ret;
1215 }
1216
1217 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1218 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1219 }
Valerio Settibb7603a2023-06-21 18:34:54 +02001220
1221 return 0;
1222}
1223#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1224static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
1225{
1226 psa_status_t status;
1227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1228 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1229 size_t prv_key_len;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001230 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001231 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1232 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001233 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1234 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001235 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001236 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001237 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +02001238 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001239
Valerio Settia7cb8452023-05-22 18:39:43 +02001240 if (curve == 0) {
1241 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1242 }
1243
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001244 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1245 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1246
Valerio Settia1b8af62023-05-17 15:34:57 +02001247 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1248 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001249 if (ret != 0) {
Valerio Setti35d1dac2023-06-30 18:04:16 +02001250 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001251 return ret;
1252 }
1253
1254 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti35d1dac2023-06-30 18:04:16 +02001255 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti1df94f82023-04-07 08:59:24 +02001256 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1257 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001258 return ret;
1259 }
1260
Valerio Setti35d1dac2023-06-30 18:04:16 +02001261 // From now on prv_key_buf is used to store the public key of prv.
Valerio Setti1df94f82023-04-07 08:59:24 +02001262 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1263 &prv_key_len);
1264 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1265 destruction_status = psa_destroy_key(key_id);
1266 if (ret != 0) {
1267 return ret;
1268 } else if (destruction_status != PSA_SUCCESS) {
1269 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001270 }
1271
Valerio Settia1b8af62023-05-17 15:34:57 +02001272 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1273 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001274 MBEDTLS_ECP_PF_UNCOMPRESSED,
1275 &pub_key_len, pub_key_buf,
1276 sizeof(pub_key_buf));
1277 if (ret != 0) {
1278 return ret;
1279 }
1280
1281 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1282 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1283 }
1284
1285 return 0;
1286}
Valerio Settibb7603a2023-06-21 18:34:54 +02001287#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001288
Valerio Settibb7603a2023-06-21 18:34:54 +02001289static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1290 int (*f_rng)(void *, unsigned char *, size_t),
1291 void *p_rng)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001292{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001293 (void) f_rng;
1294 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001295 return eckey_check_pair_psa(pub, prv);
Valerio Settibb7603a2023-06-21 18:34:54 +02001296}
1297#else /* MBEDTLS_USE_PSA_CRYPTO */
1298static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1299 int (*f_rng)(void *, unsigned char *, size_t),
1300 void *p_rng)
1301{
valerio38992cb2023-04-20 09:56:30 +02001302 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1303 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001304 f_rng, p_rng);
1305}
Valerio Settibb7603a2023-06-21 18:34:54 +02001306#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001307
Valerio Setti88a3aee2023-06-29 15:01:10 +02001308#if defined(MBEDTLS_USE_PSA_CRYPTO)
1309#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1310/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1311 * using the same function. */
1312#define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap
1313#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1314static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub,
1315 mbedtls_pk_context *prv,
1316 int (*f_rng)(void *, unsigned char *, size_t),
1317 void *p_rng)
1318{
1319 psa_status_t status;
1320 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1321 size_t exp_pub_key_len = 0;
1322 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1323 size_t pub_key_len = 0;
1324 int ret;
1325 (void) f_rng;
1326 (void) p_rng;
1327
1328 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1329 &exp_pub_key_len);
1330 if (status != PSA_SUCCESS) {
1331 ret = psa_pk_status_to_mbedtls(status);
1332 return ret;
1333 }
1334 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1335 &(mbedtls_pk_ec_ro(*pub)->Q),
1336 MBEDTLS_ECP_PF_UNCOMPRESSED,
1337 &pub_key_len, pub_key, sizeof(pub_key));
1338 if (ret != 0) {
1339 return ret;
1340 }
1341 if ((exp_pub_key_len != pub_key_len) ||
1342 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1343 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1344 }
1345 return 0;
1346}
1347#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1348#endif /* MBEDTLS_USE_PSA_CRYPTO */
1349
Valerio Settib5361262023-05-18 18:51:58 +02001350#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001351static void *eckey_alloc_wrap(void)
1352{
1353 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1354
1355 if (ctx != NULL) {
1356 mbedtls_ecp_keypair_init(ctx);
1357 }
1358
1359 return ctx;
1360}
1361
1362static void eckey_free_wrap(void *ctx)
1363{
1364 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1365 mbedtls_free(ctx);
1366}
Valerio Settib5361262023-05-18 18:51:58 +02001367#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001368
valerio38992cb2023-04-20 09:56:30 +02001369static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001370{
Valerio Settia1b8af62023-05-17 15:34:57 +02001371#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1372 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1373 items->name = "eckey.Q";
1374 items->value = pk;
Valerio Setti5c26b302023-06-21 19:47:01 +02001375#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001376 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001377 items->type = MBEDTLS_PK_DEBUG_ECP;
1378 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001379 items->value = &(ecp->Q);
Valerio Setti5c26b302023-06-21 19:47:01 +02001380#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001381}
1382
1383const mbedtls_pk_info_t mbedtls_eckey_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001384 .type = MBEDTLS_PK_ECKEY,
1385 .name = "EC",
1386 .get_bitlen = eckey_get_bitlen,
1387 .can_do = eckey_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001388#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001389 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001390#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1391 .verify_func = NULL,
1392#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001393#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001394 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001395#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1396 .sign_func = NULL,
1397#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001398#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001399 .verify_rs_func = eckey_verify_rs_wrap,
1400 .sign_rs_func = eckey_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001401 .rs_alloc_func = eckey_rs_alloc,
1402 .rs_free_func = eckey_rs_free,
1403#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1404 .decrypt_func = NULL,
1405 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001406 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001407#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1408 .ctx_alloc_func = NULL,
1409 .ctx_free_func = NULL,
1410#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001411 .ctx_alloc_func = eckey_alloc_wrap,
1412 .ctx_free_func = eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001413#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001414 .debug_func = eckey_debug,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001415};
1416
1417/*
1418 * EC key restricted to ECDH
1419 */
1420static int eckeydh_can_do(mbedtls_pk_type_t type)
1421{
1422 return type == MBEDTLS_PK_ECKEY ||
1423 type == MBEDTLS_PK_ECKEY_DH;
1424}
1425
1426const mbedtls_pk_info_t mbedtls_eckeydh_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001427 .type = MBEDTLS_PK_ECKEY_DH,
1428 .name = "EC_DH",
1429 .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */
1430 .can_do = eckeydh_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001431 .verify_func = NULL,
1432 .sign_func = NULL,
1433#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1434 .verify_rs_func = NULL,
1435 .sign_rs_func = NULL,
1436#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1437 .decrypt_func = NULL,
1438 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001439 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001440#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1441 .ctx_alloc_func = NULL,
1442 .ctx_free_func = NULL,
1443#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001444 .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */
1445 .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */
Valerio Settib5361262023-05-18 18:51:58 +02001446#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001447 .debug_func = eckey_debug, /* Same underlying key structure */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001448};
Valerio Setti1cdddac2023-02-02 13:55:57 +01001449
1450#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1451static int ecdsa_can_do(mbedtls_pk_type_t type)
1452{
1453 return type == MBEDTLS_PK_ECDSA;
1454}
1455
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001456#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001457static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 const unsigned char *hash, size_t hash_len,
1459 const unsigned char *sig, size_t sig_len,
1460 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001461{
Janos Follath24eed8d2019-11-22 13:21:35 +00001462 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001463 ((void) md_alg);
1464
1465 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001466 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 hash, hash_len, sig, sig_len,
1468 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1471 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1472 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001475}
1476
valerio38992cb2023-04-20 09:56:30 +02001477static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001478 const unsigned char *hash, size_t hash_len,
1479 unsigned char *sig, size_t sig_size, size_t *sig_len,
1480 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1481 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001482{
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001484 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1486 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001487
1488}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001491{
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001493
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 if (ctx != NULL) {
1495 mbedtls_ecdsa_restart_init(ctx);
1496 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001497
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001499}
1500
Gilles Peskine449bd832023-01-11 14:50:10 +01001501static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001502{
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 mbedtls_ecdsa_restart_free(ctx);
1504 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001505}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001506#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508const mbedtls_pk_info_t mbedtls_ecdsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001509 .type = MBEDTLS_PK_ECDSA,
1510 .name = "ECDSA",
1511 .get_bitlen = eckey_get_bitlen, /* Compatible key structures */
1512 .can_do = ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001513#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001514 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001515#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1516 .verify_func = NULL,
1517#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001518#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001519 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001520#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1521 .sign_func = NULL,
1522#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001523#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001524 .verify_rs_func = ecdsa_verify_rs_wrap,
1525 .sign_rs_func = ecdsa_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001526 .rs_alloc_func = ecdsa_rs_alloc,
1527 .rs_free_func = ecdsa_rs_free,
1528#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1529 .decrypt_func = NULL,
1530 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001531 .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001532#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1533 .ctx_alloc_func = NULL,
1534 .ctx_free_func = NULL,
1535#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001536 .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */
1537 .ctx_free_func = eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001538#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001539 .debug_func = eckey_debug, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001540};
Valerio Setti7ca13182023-01-27 13:22:42 +01001541#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Valerio Settid9d74c22023-06-29 15:00:02 +02001542#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001545/*
1546 * Support for alternative RSA-private implementations
1547 */
1548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001550{
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001552}
1553
valerio38992cb2023-04-20 09:56:30 +02001554static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001555{
valerio38992cb2023-04-20 09:56:30 +02001556 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001557
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001559}
1560
valerio38992cb2023-04-20 09:56:30 +02001561static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 const unsigned char *hash, size_t hash_len,
1563 unsigned char *sig, size_t sig_size, size_t *sig_len,
1564 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001565{
valerio38992cb2023-04-20 09:56:30 +02001566 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001567
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 if (UINT_MAX < hash_len) {
1569 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1570 }
Andres AG72849872017-01-19 11:24:33 +00001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1573 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1574 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1575 }
1576 if (*sig_len > sig_size) {
1577 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1578 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1581 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001582}
1583
valerio38992cb2023-04-20 09:56:30 +02001584static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 const unsigned char *input, size_t ilen,
1586 unsigned char *output, size_t *olen, size_t osize,
1587 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001588{
valerio38992cb2023-04-20 09:56:30 +02001589 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001590
1591 ((void) f_rng);
1592 ((void) p_rng);
1593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1595 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1596 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 return rsa_alt->decrypt_func(rsa_alt->key,
1599 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001600}
1601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001603static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 int (*f_rng)(void *, unsigned char *, size_t),
1605 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001606{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001608 unsigned char hash[32];
1609 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001611
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1613 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001614 }
1615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 memset(hash, 0x2a, sizeof(hash));
1617
valerio38992cb2023-04-20 09:56:30 +02001618 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 hash, sizeof(hash),
1620 sig, sizeof(sig), &sig_len,
1621 f_rng, p_rng)) != 0) {
1622 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001623 }
1624
valerio38992cb2023-04-20 09:56:30 +02001625 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 hash, sizeof(hash), sig, sig_len) != 0) {
1627 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1628 }
1629
1630 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001635{
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (ctx != NULL) {
1639 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1640 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001643}
1644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001646{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001647 mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001648}
1649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001651 .type = MBEDTLS_PK_RSA_ALT,
1652 .name = "RSA-alt",
1653 .get_bitlen = rsa_alt_get_bitlen,
1654 .can_do = rsa_alt_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001655 .verify_func = NULL,
Valerio Settif69514a2023-06-21 18:16:49 +02001656 .sign_func = rsa_alt_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001657#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1658 .verify_rs_func = NULL,
1659 .sign_rs_func = NULL,
1660 .rs_alloc_func = NULL,
1661 .rs_free_func = NULL,
1662#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +02001663 .decrypt_func = rsa_alt_decrypt_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001664 .encrypt_func = NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001665#if defined(MBEDTLS_RSA_C)
Valerio Settif69514a2023-06-21 18:16:49 +02001666 .check_pair_func = rsa_alt_check_pair,
Valerio Setti97976e32023-06-23 14:08:26 +02001667#else
1668 .check_pair_func = NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001669#endif
Valerio Settif69514a2023-06-21 18:16:49 +02001670 .ctx_alloc_func = rsa_alt_alloc_wrap,
1671 .ctx_free_func = rsa_alt_free_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001672 .debug_func = NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001673};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001675
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001676#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +02001677static size_t opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001678{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001679 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001680 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001681
Valerio Setti4f387ef2023-05-02 14:15:59 +02001682 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 return 0;
1684 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001685
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 bits = psa_get_key_bits(&attributes);
1687 psa_reset_key_attributes(&attributes);
1688 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001689}
1690
Valerio Setti38913c12023-06-30 16:18:33 +02001691#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti76d0f962023-06-23 13:32:54 +02001692static int ecdsa_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001693{
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 return type == MBEDTLS_PK_ECKEY ||
1695 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001696}
1697
Valerio Setti76d0f962023-06-23 13:32:54 +02001698const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = {
Valerio Setti574a00b2023-06-21 19:47:37 +02001699 .type = MBEDTLS_PK_OPAQUE,
1700 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001701 .get_bitlen = opaque_get_bitlen,
1702 .can_do = ecdsa_opaque_can_do,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001703#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001704 .verify_func = ecdsa_opaque_verify_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001705#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1706 .verify_func = NULL,
1707#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1708#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Setti76d0f962023-06-23 13:32:54 +02001709 .sign_func = ecdsa_opaque_sign_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001710#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1711 .sign_func = NULL,
1712#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti97976e32023-06-23 14:08:26 +02001713#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1714 .verify_rs_func = NULL,
1715 .sign_rs_func = NULL,
1716 .rs_alloc_func = NULL,
1717 .rs_free_func = NULL,
1718#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1719 .decrypt_func = NULL,
1720 .encrypt_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001721 .check_pair_func = ecdsa_opaque_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001722 .ctx_alloc_func = NULL,
1723 .ctx_free_func = NULL,
1724 .debug_func = NULL,
Valerio Setti574a00b2023-06-21 19:47:37 +02001725};
Valerio Setti38913c12023-06-30 16:18:33 +02001726#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti574a00b2023-06-21 19:47:37 +02001727
Valerio Setti76d0f962023-06-23 13:32:54 +02001728static int rsa_opaque_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001729{
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 return type == MBEDTLS_PK_RSA ||
1731 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001732}
1733
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001734#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001735static int rsa_opaque_decrypt(mbedtls_pk_context *pk,
1736 const unsigned char *input, size_t ilen,
1737 unsigned char *output, size_t *olen, size_t osize,
1738 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001739{
Valerio Setti574a00b2023-06-21 19:47:37 +02001740 psa_status_t status;
1741
1742 /* PSA has its own RNG */
1743 (void) f_rng;
1744 (void) p_rng;
1745
1746 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
1747 input, ilen,
1748 NULL, 0,
1749 output, osize, olen);
1750 if (status != PSA_SUCCESS) {
1751 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1752 }
1753
1754 return 0;
1755}
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001756#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti574a00b2023-06-21 19:47:37 +02001757
Valerio Setti76d0f962023-06-23 13:32:54 +02001758static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
1759 const unsigned char *hash, size_t hash_len,
1760 unsigned char *sig, size_t sig_size, size_t *sig_len,
1761 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001762{
1763#if defined(MBEDTLS_RSA_C)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001764 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001765 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001766 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001767 psa_status_t status;
1768
1769 /* PSA has its own RNG */
1770 (void) f_rng;
1771 (void) p_rng;
1772
Valerio Setti4f387ef2023-05-02 14:15:59 +02001773 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001775 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001777
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 type = psa_get_key_type(&attributes);
1779 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001780
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001782 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Valerio Setti4657f102023-06-21 13:55:16 +02001783 } else {
1784 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1785 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001786
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001787 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001788 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 sig, sig_size, sig_len);
1790 if (status != PSA_SUCCESS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001791 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001792 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Valerio Setti4657f102023-06-21 13:55:16 +02001793 } else {
1794 return PSA_PK_TO_MBEDTLS_ERR(status);
1795 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001796 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001797
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001798 return 0;
Valerio Setti574a00b2023-06-21 19:47:37 +02001799#else /* !MBEDTLS_RSA_C */
1800 ((void) pk);
1801 ((void) md_alg);
1802 ((void) hash);
1803 ((void) hash_len);
1804 ((void) sig);
1805 ((void) sig_size);
1806 ((void) sig_len);
1807 ((void) f_rng);
1808 ((void) p_rng);
1809 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti4657f102023-06-21 13:55:16 +02001810#endif /* !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001811}
1812
Valerio Setti76d0f962023-06-23 13:32:54 +02001813const mbedtls_pk_info_t mbedtls_rsa_opaque_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001814 .type = MBEDTLS_PK_OPAQUE,
1815 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001816 .get_bitlen = opaque_get_bitlen,
1817 .can_do = rsa_opaque_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001818 .verify_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001819 .sign_func = rsa_opaque_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001820#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1821 .verify_rs_func = NULL,
1822 .sign_rs_func = NULL,
1823 .rs_alloc_func = NULL,
1824 .rs_free_func = NULL,
1825#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001826#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001827 .decrypt_func = rsa_opaque_decrypt,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001828#else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001829 .decrypt_func = NULL,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001830#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001831 .encrypt_func = NULL,
1832 .check_pair_func = NULL,
1833 .ctx_alloc_func = NULL,
1834 .ctx_free_func = NULL,
1835 .debug_func = NULL,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001836};
1837
1838#endif /* MBEDTLS_USE_PSA_CRYPTO */
1839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001840#endif /* MBEDTLS_PK_C */