blob: c0a7fbde2c4a9fc81c5ef2da82c9fd2ed40ef500 [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)
46#include "mbedtls/psa_util.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 Setti8bb57632023-05-26 13:48:07 +0200100 defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
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 Setti8bb57632023-05-26 13:48:07 +0200137#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY */
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;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100305 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100306 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
309 if (sig_size < *sig_len) {
310 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
311 }
Neil Armstrong98545682022-02-22 16:12:51 +0100312
Neil Armstronge4f28682022-02-24 15:41:39 +0100313 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100314 * re-construct one to make it happy */
315 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800316 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
318 if (key_len <= 0) {
319 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
320 }
321 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
322 psa_set_key_algorithm(&attributes, alg);
323 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 status = psa_import_key(&attributes,
326 buf + sizeof(buf) - key_len, key_len,
327 &key_id);
328 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500329 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100330 goto cleanup;
331 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 status = psa_sign_hash(key_id, alg, hash, hash_len,
333 sig, sig_size, sig_len);
334 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500335 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100336 goto cleanup;
337 }
338
339 ret = 0;
340
341cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 status = psa_destroy_key(key_id);
343 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500344 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 }
346 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100347}
Jerry Yu406cf272022-03-22 11:33:42 +0800348#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800349
350#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200351static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 const unsigned char *hash, size_t hash_len,
353 unsigned char *sig, size_t sig_size, size_t *sig_len,
354 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800355{
Jerry Yu1d172a32022-03-12 19:12:05 +0800356 ((void) f_rng);
357 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800358
Jerry Yubd1b3272022-03-24 13:05:20 +0800359 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200360 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (psa_md_alg == 0) {
362 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
363 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
366 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200367 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800369}
Valerio Setti5c26b302023-06-21 19:47:01 +0200370#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200371static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 const unsigned char *hash, size_t hash_len,
373 unsigned char *sig, size_t sig_size, size_t *sig_len,
374 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200375{
valerio38992cb2023-04-20 09:56:30 +0200376 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
379 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
380 }
Andres AG72849872017-01-19 11:24:33 +0000381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 *sig_len = mbedtls_rsa_get_len(rsa);
383 if (sig_size < *sig_len) {
384 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
385 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
388 md_alg, (unsigned int) hash_len,
389 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200390}
Valerio Setti5c26b302023-06-21 19:47:01 +0200391#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200392
Neil Armstrong18f43c72022-02-09 15:32:45 +0100393#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200394static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 const unsigned char *input, size_t ilen,
396 unsigned char *output, size_t *olen, size_t osize,
397 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100398{
valerio38992cb2023-04-20 09:56:30 +0200399 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
402 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
403 psa_status_t status;
404 mbedtls_pk_context key;
405 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100406 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100407
408 ((void) f_rng);
409 ((void) p_rng);
410
411#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
413 return MBEDTLS_ERR_RSA_INVALID_PADDING;
414 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100415#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (ilen != mbedtls_rsa_get_len(rsa)) {
418 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
419 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100420
421 /* mbedtls_pk_write_key_der() expects a full PK context;
422 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100423 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200424 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
426 if (key_len <= 0) {
427 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
428 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
431 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
432 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 status = psa_import_key(&attributes,
435 buf + sizeof(buf) - key_len, key_len,
436 &key_id);
437 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500438 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100439 goto cleanup;
440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
443 input, ilen,
444 NULL, 0,
445 output, osize, olen);
446 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500447 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100448 goto cleanup;
449 }
450
451 ret = 0;
452
453cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_platform_zeroize(buf, sizeof(buf));
455 status = psa_destroy_key(key_id);
456 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500457 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100461}
Valerio Setti5c26b302023-06-21 19:47:01 +0200462#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200463static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 const unsigned char *input, size_t ilen,
465 unsigned char *output, size_t *olen, size_t osize,
466 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200467{
valerio38992cb2023-04-20 09:56:30 +0200468 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (ilen != mbedtls_rsa_get_len(rsa)) {
471 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
472 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
475 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200476}
Valerio Setti5c26b302023-06-21 19:47:01 +0200477#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200478
Neil Armstrong96a16a42022-02-10 10:40:11 +0100479#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200480static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 const unsigned char *input, size_t ilen,
482 unsigned char *output, size_t *olen, size_t osize,
483 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100484{
valerio38992cb2023-04-20 09:56:30 +0200485 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
488 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
489 psa_status_t status;
490 mbedtls_pk_context key;
491 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100492 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100493
494 ((void) f_rng);
495 ((void) p_rng);
496
497#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
499 return MBEDTLS_ERR_RSA_INVALID_PADDING;
500 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100501#endif
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (mbedtls_rsa_get_len(rsa) > osize) {
504 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
505 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100506
Neil Armstrongac014ca2022-02-24 15:27:54 +0100507 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100508 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100509 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200510 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
512 if (key_len <= 0) {
513 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
514 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
517 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
518 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 status = psa_import_key(&attributes,
521 buf + sizeof(buf) - key_len, key_len,
522 &key_id);
523 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500524 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100525 goto cleanup;
526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
529 input, ilen,
530 NULL, 0,
531 output, osize, olen);
532 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500533 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100534 goto cleanup;
535 }
536
537 ret = 0;
538
539cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 status = psa_destroy_key(key_id);
541 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500542 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100546}
Valerio Setti5c26b302023-06-21 19:47:01 +0200547#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200548static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 const unsigned char *input, size_t ilen,
550 unsigned char *output, size_t *olen, size_t osize,
551 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200552{
valerio38992cb2023-04-20 09:56:30 +0200553 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (*olen > osize) {
557 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
558 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
561 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200562}
Valerio Setti5c26b302023-06-21 19:47:01 +0200563#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200564
valerio38992cb2023-04-20 09:56:30 +0200565static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100568{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200569 (void) f_rng;
570 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200571 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
572 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100573}
574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200576{
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if (ctx != NULL) {
580 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
581 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200584}
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200587{
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
589 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200590}
591
valerio38992cb2023-04-20 09:56:30 +0200592static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200593{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200594#if defined(MBEDTLS_RSA_ALT)
595 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200596 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200597 (void) items;
598#else
valerio38992cb2023-04-20 09:56:30 +0200599 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200602 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200603 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200604
605 items++;
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200608 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200609 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200610#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200611}
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613const mbedtls_pk_info_t mbedtls_rsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +0200614 .type = MBEDTLS_PK_RSA,
615 .name = "RSA",
616 .get_bitlen = rsa_get_bitlen,
617 .can_do = rsa_can_do,
618 .verify_func = rsa_verify_wrap,
619 .sign_func = rsa_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +0200620#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
621 .verify_rs_func = NULL,
622 .sign_rs_func = NULL,
623 .rs_alloc_func = NULL,
624 .rs_free_func = NULL,
625#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +0200626 .decrypt_func = rsa_decrypt_wrap,
627 .encrypt_func = rsa_encrypt_wrap,
628 .check_pair_func = rsa_check_pair_wrap,
629 .ctx_alloc_func = rsa_alloc_wrap,
630 .ctx_free_func = rsa_free_wrap,
631 .debug_func = rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200632};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200634
Valerio Setti81d75122023-06-14 14:49:33 +0200635#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200636/*
637 * Generic EC key
638 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200640{
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 return type == MBEDTLS_PK_ECKEY ||
642 type == MBEDTLS_PK_ECKEY_DH ||
643 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200644}
645
valerio38992cb2023-04-20 09:56:30 +0200646static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200647{
Valerio Settia1b8af62023-05-17 15:34:57 +0200648#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
649 return pk->ec_bits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200650#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +0200651 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
652 return ecp->grp.pbits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200653#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200654}
655
Valerio Setti1cdddac2023-02-02 13:55:57 +0100656#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400657#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400658/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500659 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
660 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500661 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100662static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
663 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500664{
Janos Follath24eed8d2019-11-22 13:21:35 +0000665 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500666 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
669 MBEDTLS_ASN1_INTEGER)) != 0) {
670 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500671 }
672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 while (unpadded_len > 0 && **from == 0x00) {
674 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500675 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500676 }
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (unpadded_len > to_len || unpadded_len == 0) {
679 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
680 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500681
Andrzej Kurek9241d182018-11-20 05:04:35 -0500682 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 memset(to, 0x00, padding_len);
684 memcpy(to + padding_len, *from, unpadded_len);
685 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500688}
689
690/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400691 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500692 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500693 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400694 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100695static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
696 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400697{
Janos Follath24eed8d2019-11-22 13:21:35 +0000698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500699 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
702 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
703 return ret;
704 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400705
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500706 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
708 return ret;
709 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500710 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
712 return ret;
713 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400716}
717
Valerio Setti76d0f962023-06-23 13:32:54 +0200718/* Common helper for ECDSA verify using PSA functions. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200719static int ecdsa_verify_psa(unsigned char *key, size_t key_len,
720 psa_ecc_family_t curve, size_t curve_bits,
721 const unsigned char *hash, size_t hash_len,
722 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400723{
Janos Follath24eed8d2019-11-22 13:21:35 +0000724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100726 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settia1b8af62023-05-17 15:34:57 +0200727 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Valerio Settied7d6af2023-06-21 15:42:21 +0200728 size_t signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
729 unsigned char extracted_sig[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
730 unsigned char *p;
731 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (curve == 0) {
734 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
735 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
738 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
739 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500740
Valerio Settied7d6af2023-06-21 15:42:21 +0200741 status = psa_import_key(&attributes, key, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500743 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400744 goto cleanup;
745 }
746
Valerio Settied7d6af2023-06-21 15:42:21 +0200747 if (signature_len > sizeof(extracted_sig)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500748 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
749 goto cleanup;
750 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 p = (unsigned char *) sig;
Valerio Settia1b8af62023-05-17 15:34:57 +0200753 /* extract_ecdsa_sig's last parameter is the size
Valerio Settif57007d2023-05-19 13:54:39 +0200754 * of each integer to be parsed, so it's actually half
Valerio Settia1b8af62023-05-17 15:34:57 +0200755 * the size of the signature. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200756 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, extracted_sig,
Valerio Settia1b8af62023-05-17 15:34:57 +0200757 signature_len/2)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500758 goto cleanup;
759 }
760
Valerio Settied7d6af2023-06-21 15:42:21 +0200761 status = psa_verify_hash(key_id, psa_sig_md, hash, hash_len,
762 extracted_sig, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500764 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400766 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500769 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
770 goto cleanup;
771 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400772 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400773
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500774cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 status = psa_destroy_key(key_id);
776 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500777 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400781}
Valerio Settied7d6af2023-06-21 15:42:21 +0200782
Valerio Setti76d0f962023-06-23 13:32:54 +0200783static int ecdsa_opaque_verify_wrap(mbedtls_pk_context *pk,
784 mbedtls_md_type_t md_alg,
785 const unsigned char *hash, size_t hash_len,
786 const unsigned char *sig, size_t sig_len)
Valerio Settie7730772023-06-21 16:58:40 +0200787{
788 (void) md_alg;
789 unsigned char key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
790 size_t key_len;
791 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
792 psa_ecc_family_t curve;
793 size_t curve_bits;
794 psa_status_t status;
795
796 status = psa_get_key_attributes(pk->priv_id, &key_attr);
797 if (status != PSA_SUCCESS) {
798 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
799 }
800 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(&key_attr));
801 curve_bits = psa_get_key_bits(&key_attr);
802 psa_reset_key_attributes(&key_attr);
803
804 status = psa_export_public_key(pk->priv_id, key, sizeof(key), &key_len);
805 if (status != PSA_SUCCESS) {
806 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
807 }
808
809 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
810 hash, hash_len, sig, sig_len);
811}
812
Valerio Settied7d6af2023-06-21 15:42:21 +0200813#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
814static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
815 mbedtls_md_type_t md_alg,
816 const unsigned char *hash, size_t hash_len,
817 const unsigned char *sig, size_t sig_len)
818{
819 (void) md_alg;
820 psa_ecc_family_t curve = pk->ec_family;
821 size_t curve_bits = pk->ec_bits;
822
823 return ecdsa_verify_psa(pk->pub_raw, pk->pub_raw_len, curve, curve_bits,
824 hash, hash_len, sig, sig_len);
825}
826#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
827static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
828 mbedtls_md_type_t md_alg,
829 const unsigned char *hash, size_t hash_len,
830 const unsigned char *sig, size_t sig_len)
831{
832 (void) md_alg;
833 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
834 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
835 unsigned char key[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
836 size_t key_len;
837 size_t curve_bits;
838 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
839
840 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
841 MBEDTLS_ECP_PF_UNCOMPRESSED,
842 &key_len, key, sizeof(key));
843 if (ret != 0) {
844 return ret;
845 }
846
847 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
848 hash, hash_len, sig, sig_len);
849}
850#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400851#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200852static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 const unsigned char *hash, size_t hash_len,
854 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200855{
Janos Follath24eed8d2019-11-22 13:21:35 +0000856 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200857 ((void) md_alg);
858
valerio38992cb2023-04-20 09:56:30 +0200859 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
863 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
864 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200867}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400868#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100869#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200870
Valerio Setti1cdddac2023-02-02 13:55:57 +0100871#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100872#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100873/*
874 * Simultaneously convert and move raw MPI from the beginning of a buffer
875 * to an ASN.1 MPI at the end of the buffer.
876 * See also mbedtls_asn1_write_mpi().
877 *
878 * p: pointer to the end of the output buffer
879 * start: start of the output buffer, and also of the mpi to write at the end
880 * n_len: length of the mpi to read from start
881 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
883 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100884{
885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
886 size_t len = 0;
887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if ((size_t) (*p - start) < n_len) {
889 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
890 }
Neil Armstrong15021652022-03-01 10:14:17 +0100891
892 len = n_len;
893 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100895
896 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
897 * Neither r nor s should be 0, but as a failsafe measure, still detect
898 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100900 ++(*p);
901 --len;
902 }
903
904 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (len == 0) {
906 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
907 }
Neil Armstrong15021652022-03-01 10:14:17 +0100908
909 /* if the msb is 1, ASN.1 requires that we prepend a 0.
910 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 if (**p & 0x80) {
912 if (*p - start < 1) {
913 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
914 }
Neil Armstrong15021652022-03-01 10:14:17 +0100915
916 *--(*p) = 0x00;
917 len += 1;
918 }
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
921 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
922 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100925}
926
927/* Transcode signature from PSA format to ASN.1 sequence.
928 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
929 * MPIs, and in-place.
930 *
931 * [in/out] sig: the signature pre- and post-transcoding
932 * [in/out] sig_len: signature length pre- and post-transcoding
933 * [int] buf_len: the available size the in/out buffer
934 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
936 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100937{
938 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
939 size_t len = 0;
940 const size_t rs_len = *sig_len / 2;
941 unsigned char *p = sig + buf_len;
942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
944 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
947 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
948 MBEDTLS_ASN1_CONSTRUCTED |
949 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100952 *sig_len = len;
953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100955}
Neil Armstronge9606902022-02-09 14:23:00 +0100956
Valerio Setti76d0f962023-06-23 13:32:54 +0200957/* Common helper for ECDSA sign using PSA functions. */
Valerio Setti884c1ec2023-06-23 12:09:13 +0200958static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg,
Valerio Setti4657f102023-06-21 13:55:16 +0200959 const unsigned char *hash, size_t hash_len,
960 unsigned char *sig, size_t sig_size, size_t *sig_len)
961{
962 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
963 psa_status_t status;
Valerio Setti884c1ec2023-06-23 12:09:13 +0200964 psa_algorithm_t psa_sig_md;
965 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
966 psa_algorithm_t alg;
967
968 status = psa_get_key_attributes(key_id, &key_attr);
969 if (status != PSA_SUCCESS) {
970 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
971 }
972 alg = psa_get_key_algorithm(&key_attr);
Valerio Setti76d0f962023-06-23 13:32:54 +0200973 psa_reset_key_attributes(&key_attr);
Valerio Setti884c1ec2023-06-23 12:09:13 +0200974
975 if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) {
976 psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
977 } else {
978 psa_sig_md = PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
979 }
Valerio Setti4657f102023-06-21 13:55:16 +0200980
981 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
982 sig, sig_size, sig_len);
983 if (status != PSA_SUCCESS) {
984 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
985 }
986
987 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
988
989 return ret;
990}
991
Valerio Setti76d0f962023-06-23 13:32:54 +0200992static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk,
993 mbedtls_md_type_t md_alg,
994 const unsigned char *hash, size_t hash_len,
995 unsigned char *sig, size_t sig_size,
996 size_t *sig_len,
997 int (*f_rng)(void *, unsigned char *, size_t),
998 void *p_rng)
Valerio Setti4657f102023-06-21 13:55:16 +0200999{
1000 ((void) f_rng);
1001 ((void) p_rng);
Valerio Setti4657f102023-06-21 13:55:16 +02001002
Valerio Setti884c1ec2023-06-23 12:09:13 +02001003 return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size,
Valerio Setti4657f102023-06-21 13:55:16 +02001004 sig_len);
1005}
1006
1007#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti76d0f962023-06-23 13:32:54 +02001008/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1009 * using the same function. */
1010#define ecdsa_sign_wrap ecdsa_opaque_sign_wrap
Valerio Setti4657f102023-06-21 13:55:16 +02001011#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001012static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 const unsigned char *hash, size_t hash_len,
1014 unsigned char *sig, size_t sig_size, size_t *sig_len,
1015 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +01001016{
Neil Armstronge9606902022-02-09 14:23:00 +01001017 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronge9606902022-02-09 14:23:00 +01001018 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1019 psa_status_t status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001020 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
1021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1022 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Neil Armstronge9606902022-02-09 14:23:00 +01001023 size_t curve_bits;
1024 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +02001026 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti4657f102023-06-21 13:55:16 +02001027#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1028 psa_algorithm_t psa_sig_md =
1029 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
1030#else
1031 psa_algorithm_t psa_sig_md =
1032 PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
1033#endif
Neil Armstronge9606902022-02-09 14:23:00 +01001034 ((void) f_rng);
1035 ((void) p_rng);
1036
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (curve == 0) {
1038 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1039 }
Neil Armstronge9606902022-02-09 14:23:00 +01001040
Gilles Peskine13caa942022-10-04 22:59:26 +02001041 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +01001042 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 }
Gilles Peskine13caa942022-10-04 22:59:26 +02001044 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +01001046 goto cleanup;
1047 }
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1050 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
1051 psa_set_key_algorithm(&attributes, psa_sig_md);
1052
Valerio Setti4657f102023-06-21 13:55:16 +02001053 status = psa_import_key(&attributes, buf, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001055 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +01001057 }
1058
Valerio Setti884c1ec2023-06-23 12:09:13 +02001059 ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len);
Neil Armstronge9606902022-02-09 14:23:00 +01001060
1061cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 mbedtls_platform_zeroize(buf, sizeof(buf));
1063 status = psa_destroy_key(key_id);
1064 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001065 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001067
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +01001069}
Valerio Setti4657f102023-06-21 13:55:16 +02001070#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001071#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +02001072static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 const unsigned char *hash, size_t hash_len,
1074 unsigned char *sig, size_t sig_size, size_t *sig_len,
1075 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001076{
valerio38992cb2023-04-20 09:56:30 +02001077 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 md_alg, hash, hash_len,
1079 sig, sig_size, sig_len,
1080 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001081}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001082#endif /* MBEDTLS_USE_PSA_CRYPTO */
1083#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001084
Valerio Setti80d07982023-02-08 13:49:17 +01001085#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001086/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001087static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001088 const unsigned char *hash, size_t hash_len,
1089 const unsigned char *sig, size_t sig_len,
1090 void *rs_ctx);
1091
valerio38992cb2023-04-20 09:56:30 +02001092static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001093 const unsigned char *hash, size_t hash_len,
1094 unsigned char *sig, size_t sig_size, size_t *sig_len,
1095 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1096 void *rs_ctx);
1097
1098/*
1099 * Restart context for ECDSA operations with ECKEY context
1100 *
1101 * We need to store an actual ECDSA context, as we need to pass the same to
1102 * the underlying ecdsa function, so we can't create it on the fly every time.
1103 */
1104typedef struct {
1105 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1106 mbedtls_ecdsa_context ecdsa_ctx;
1107} eckey_restart_ctx;
1108
1109static void *eckey_rs_alloc(void)
1110{
1111 eckey_restart_ctx *rs_ctx;
1112
1113 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1114
1115 if (ctx != NULL) {
1116 rs_ctx = ctx;
1117 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1118 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1119 }
1120
1121 return ctx;
1122}
1123
1124static void eckey_rs_free(void *ctx)
1125{
1126 eckey_restart_ctx *rs_ctx;
1127
1128 if (ctx == NULL) {
1129 return;
1130 }
1131
1132 rs_ctx = ctx;
1133 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1134 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1135
1136 mbedtls_free(ctx);
1137}
1138
valerio38992cb2023-04-20 09:56:30 +02001139static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001140 const unsigned char *hash, size_t hash_len,
1141 const unsigned char *sig, size_t sig_len,
1142 void *rs_ctx)
1143{
1144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1145 eckey_restart_ctx *rs = rs_ctx;
1146
1147 /* Should never happen */
1148 if (rs == NULL) {
1149 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1150 }
1151
1152 /* set up our own sub-context if needed (that is, on first run) */
1153 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001154 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001155 }
1156
valerio38992cb2023-04-20 09:56:30 +02001157 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001158 md_alg, hash, hash_len,
1159 sig, sig_len, &rs->ecdsa_rs));
1160
1161cleanup:
1162 return ret;
1163}
1164
valerio38992cb2023-04-20 09:56:30 +02001165static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001166 const unsigned char *hash, size_t hash_len,
1167 unsigned char *sig, size_t sig_size, size_t *sig_len,
1168 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1169 void *rs_ctx)
1170{
1171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1172 eckey_restart_ctx *rs = rs_ctx;
1173
1174 /* Should never happen */
1175 if (rs == NULL) {
1176 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1177 }
1178
1179 /* set up our own sub-context if needed (that is, on first run) */
1180 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001181 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001182 }
1183
valerio38992cb2023-04-20 09:56:30 +02001184 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001185 hash, hash_len, sig, sig_size, sig_len,
1186 f_rng, p_rng, &rs->ecdsa_rs));
1187
1188cleanup:
1189 return ret;
1190}
Valerio Setti80d07982023-02-08 13:49:17 +01001191#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001192
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001193#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settibb7603a2023-06-21 18:34:54 +02001194#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerio38992cb2023-04-20 09:56:30 +02001195static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001196{
Valerio Setti9efa8c42023-05-19 13:27:30 +02001197 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001198 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001199 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001200 size_t prv_key_len;
Valerio Settiae8c6282023-05-18 18:57:57 +02001201 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001202
1203 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1204 &prv_key_len);
1205 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1206 if (ret != 0) {
1207 return ret;
1208 }
1209
1210 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1211 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1212 }
Valerio Settibb7603a2023-06-21 18:34:54 +02001213
1214 return 0;
1215}
1216#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1217static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
1218{
1219 psa_status_t status;
1220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1221 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1222 size_t prv_key_len;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001223 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001224 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1225 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001226 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1227 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001228 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001229 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001230 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +02001231 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001232
Valerio Settia7cb8452023-05-22 18:39:43 +02001233 if (curve == 0) {
1234 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1235 }
1236
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001237 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1238 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1239
Valerio Settia1b8af62023-05-17 15:34:57 +02001240 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1241 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001242 if (ret != 0) {
1243 return ret;
1244 }
1245
1246 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti1df94f82023-04-07 08:59:24 +02001247 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1248 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001249 return ret;
1250 }
1251
1252 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
1253
Valerio Setti1df94f82023-04-07 08:59:24 +02001254 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1255 &prv_key_len);
1256 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1257 destruction_status = psa_destroy_key(key_id);
1258 if (ret != 0) {
1259 return ret;
1260 } else if (destruction_status != PSA_SUCCESS) {
1261 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001262 }
1263
Valerio Settia1b8af62023-05-17 15:34:57 +02001264 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1265 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001266 MBEDTLS_ECP_PF_UNCOMPRESSED,
1267 &pub_key_len, pub_key_buf,
1268 sizeof(pub_key_buf));
1269 if (ret != 0) {
1270 return ret;
1271 }
1272
1273 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1274 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1275 }
1276
1277 return 0;
1278}
Valerio Settibb7603a2023-06-21 18:34:54 +02001279#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001280
Valerio Settibb7603a2023-06-21 18:34:54 +02001281static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1282 int (*f_rng)(void *, unsigned char *, size_t),
1283 void *p_rng)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001284{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001285 (void) f_rng;
1286 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001287 return eckey_check_pair_psa(pub, prv);
Valerio Settibb7603a2023-06-21 18:34:54 +02001288}
1289#else /* MBEDTLS_USE_PSA_CRYPTO */
1290static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1291 int (*f_rng)(void *, unsigned char *, size_t),
1292 void *p_rng)
1293{
valerio38992cb2023-04-20 09:56:30 +02001294 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1295 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001296 f_rng, p_rng);
1297}
Valerio Settibb7603a2023-06-21 18:34:54 +02001298#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001299
Valerio Settib5361262023-05-18 18:51:58 +02001300#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001301static void *eckey_alloc_wrap(void)
1302{
1303 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1304
1305 if (ctx != NULL) {
1306 mbedtls_ecp_keypair_init(ctx);
1307 }
1308
1309 return ctx;
1310}
1311
1312static void eckey_free_wrap(void *ctx)
1313{
1314 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1315 mbedtls_free(ctx);
1316}
Valerio Settib5361262023-05-18 18:51:58 +02001317#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001318
valerio38992cb2023-04-20 09:56:30 +02001319static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001320{
Valerio Settia1b8af62023-05-17 15:34:57 +02001321#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1322 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1323 items->name = "eckey.Q";
1324 items->value = pk;
Valerio Setti5c26b302023-06-21 19:47:01 +02001325#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001326 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001327 items->type = MBEDTLS_PK_DEBUG_ECP;
1328 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001329 items->value = &(ecp->Q);
Valerio Setti5c26b302023-06-21 19:47:01 +02001330#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001331}
1332
1333const mbedtls_pk_info_t mbedtls_eckey_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001334 .type = MBEDTLS_PK_ECKEY,
1335 .name = "EC",
1336 .get_bitlen = eckey_get_bitlen,
1337 .can_do = eckey_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001338#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001339 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001340#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1341 .verify_func = NULL,
1342#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001343#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001344 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001345#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1346 .sign_func = NULL,
1347#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001348#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001349 .verify_rs_func = eckey_verify_rs_wrap,
1350 .sign_rs_func = eckey_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001351 .rs_alloc_func = eckey_rs_alloc,
1352 .rs_free_func = eckey_rs_free,
1353#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1354 .decrypt_func = NULL,
1355 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001356 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001357#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1358 .ctx_alloc_func = NULL,
1359 .ctx_free_func = NULL,
1360#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001361 .ctx_alloc_func = eckey_alloc_wrap,
1362 .ctx_free_func = eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001363#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001364 .debug_func = eckey_debug,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001365};
1366
1367/*
1368 * EC key restricted to ECDH
1369 */
1370static int eckeydh_can_do(mbedtls_pk_type_t type)
1371{
1372 return type == MBEDTLS_PK_ECKEY ||
1373 type == MBEDTLS_PK_ECKEY_DH;
1374}
1375
1376const mbedtls_pk_info_t mbedtls_eckeydh_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001377 .type = MBEDTLS_PK_ECKEY_DH,
1378 .name = "EC_DH",
1379 .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */
1380 .can_do = eckeydh_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001381 .verify_func = NULL,
1382 .sign_func = NULL,
1383#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1384 .verify_rs_func = NULL,
1385 .sign_rs_func = NULL,
1386#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1387 .decrypt_func = NULL,
1388 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001389 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001390#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1391 .ctx_alloc_func = NULL,
1392 .ctx_free_func = NULL,
1393#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001394 .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */
1395 .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */
Valerio Settib5361262023-05-18 18:51:58 +02001396#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001397 .debug_func = eckey_debug, /* Same underlying key structure */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001398};
Valerio Setti81d75122023-06-14 14:49:33 +02001399#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001400
1401#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1402static int ecdsa_can_do(mbedtls_pk_type_t type)
1403{
1404 return type == MBEDTLS_PK_ECDSA;
1405}
1406
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001407#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001408static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 const unsigned char *hash, size_t hash_len,
1410 const unsigned char *sig, size_t sig_len,
1411 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001412{
Janos Follath24eed8d2019-11-22 13:21:35 +00001413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001414 ((void) md_alg);
1415
1416 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001417 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 hash, hash_len, sig, sig_len,
1419 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1422 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1423 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001426}
1427
valerio38992cb2023-04-20 09:56:30 +02001428static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 const unsigned char *hash, size_t hash_len,
1430 unsigned char *sig, size_t sig_size, size_t *sig_len,
1431 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1432 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001433{
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001435 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1437 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001438
1439}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001442{
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 if (ctx != NULL) {
1446 mbedtls_ecdsa_restart_init(ctx);
1447 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001450}
1451
Gilles Peskine449bd832023-01-11 14:50:10 +01001452static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001453{
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 mbedtls_ecdsa_restart_free(ctx);
1455 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001456}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001457#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001459const mbedtls_pk_info_t mbedtls_ecdsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001460 .type = MBEDTLS_PK_ECDSA,
1461 .name = "ECDSA",
1462 .get_bitlen = eckey_get_bitlen, /* Compatible key structures */
1463 .can_do = ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001464#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001465 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001466#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1467 .verify_func = NULL,
1468#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001469#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001470 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001471#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1472 .sign_func = NULL,
1473#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001474#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001475 .verify_rs_func = ecdsa_verify_rs_wrap,
1476 .sign_rs_func = ecdsa_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001477 .rs_alloc_func = ecdsa_rs_alloc,
1478 .rs_free_func = ecdsa_rs_free,
1479#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1480 .decrypt_func = NULL,
1481 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001482 .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001483#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1484 .ctx_alloc_func = NULL,
1485 .ctx_free_func = NULL,
1486#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001487 .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */
1488 .ctx_free_func = eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001489#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001490 .debug_func = eckey_debug, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001491};
Valerio Setti7ca13182023-01-27 13:22:42 +01001492#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001494#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001495/*
1496 * Support for alternative RSA-private implementations
1497 */
1498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001500{
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001502}
1503
valerio38992cb2023-04-20 09:56:30 +02001504static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001505{
valerio38992cb2023-04-20 09:56:30 +02001506 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001509}
1510
valerio38992cb2023-04-20 09:56:30 +02001511static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 const unsigned char *hash, size_t hash_len,
1513 unsigned char *sig, size_t sig_size, size_t *sig_len,
1514 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001515{
valerio38992cb2023-04-20 09:56:30 +02001516 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 if (UINT_MAX < hash_len) {
1519 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1520 }
Andres AG72849872017-01-19 11:24:33 +00001521
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1523 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1524 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1525 }
1526 if (*sig_len > sig_size) {
1527 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1528 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1531 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001532}
1533
valerio38992cb2023-04-20 09:56:30 +02001534static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 const unsigned char *input, size_t ilen,
1536 unsigned char *output, size_t *olen, size_t osize,
1537 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001538{
valerio38992cb2023-04-20 09:56:30 +02001539 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001540
1541 ((void) f_rng);
1542 ((void) p_rng);
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1545 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1546 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return rsa_alt->decrypt_func(rsa_alt->key,
1549 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001550}
1551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001553static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 int (*f_rng)(void *, unsigned char *, size_t),
1555 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001556{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001558 unsigned char hash[32];
1559 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001560 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1563 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001564 }
1565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 memset(hash, 0x2a, sizeof(hash));
1567
valerio38992cb2023-04-20 09:56:30 +02001568 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 hash, sizeof(hash),
1570 sig, sizeof(sig), &sig_len,
1571 f_rng, p_rng)) != 0) {
1572 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001573 }
1574
valerio38992cb2023-04-20 09:56:30 +02001575 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 hash, sizeof(hash), sig, sig_len) != 0) {
1577 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1578 }
1579
1580 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001581}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001583
Gilles Peskine449bd832023-01-11 14:50:10 +01001584static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001585{
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (ctx != NULL) {
1589 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1590 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001593}
1594
Gilles Peskine449bd832023-01-11 14:50:10 +01001595static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001596{
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1598 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001599}
1600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001602 .type = MBEDTLS_PK_RSA_ALT,
1603 .name = "RSA-alt",
1604 .get_bitlen = rsa_alt_get_bitlen,
1605 .can_do = rsa_alt_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001606 .verify_func = NULL,
Valerio Settif69514a2023-06-21 18:16:49 +02001607 .sign_func = rsa_alt_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001608#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1609 .verify_rs_func = NULL,
1610 .sign_rs_func = NULL,
1611 .rs_alloc_func = NULL,
1612 .rs_free_func = NULL,
1613#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +02001614 .decrypt_func = rsa_alt_decrypt_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001615 .encrypt_func = NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001616#if defined(MBEDTLS_RSA_C)
Valerio Settif69514a2023-06-21 18:16:49 +02001617 .check_pair_func = rsa_alt_check_pair,
Valerio Setti97976e32023-06-23 14:08:26 +02001618#else
1619 .check_pair_func = NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001620#endif
Valerio Settif69514a2023-06-21 18:16:49 +02001621 .ctx_alloc_func = rsa_alt_alloc_wrap,
1622 .ctx_free_func = rsa_alt_free_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001623 .debug_func = NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001624};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001626
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001627#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +02001628static size_t opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001629{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001630 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001631 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001632
Valerio Setti4f387ef2023-05-02 14:15:59 +02001633 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 return 0;
1635 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 bits = psa_get_key_bits(&attributes);
1638 psa_reset_key_attributes(&attributes);
1639 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001640}
1641
Valerio Setti76d0f962023-06-23 13:32:54 +02001642static int ecdsa_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001643{
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 return type == MBEDTLS_PK_ECKEY ||
1645 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001646}
1647
Valerio Setti574a00b2023-06-21 19:47:37 +02001648#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti76d0f962023-06-23 13:32:54 +02001649/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1650 * using the same function. */
1651#define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap
Valerio Setti574a00b2023-06-21 19:47:37 +02001652#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti4d1daf82023-06-26 13:31:18 +02001653#if defined(MBEDTLS_ECP_LIGHT)
Valerio Setti76d0f962023-06-23 13:32:54 +02001654static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub,
1655 mbedtls_pk_context *prv,
1656 int (*f_rng)(void *, unsigned char *, size_t),
1657 void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001658{
1659 psa_status_t status;
1660 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1661 size_t exp_pub_key_len = 0;
1662 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1663 size_t pub_key_len = 0;
1664 int ret;
1665 (void) f_rng;
1666 (void) p_rng;
1667
1668 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1669 &exp_pub_key_len);
1670 if (status != PSA_SUCCESS) {
1671 ret = psa_pk_status_to_mbedtls(status);
1672 return ret;
1673 }
1674 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1675 &(mbedtls_pk_ec_ro(*pub)->Q),
1676 MBEDTLS_ECP_PF_UNCOMPRESSED,
1677 &pub_key_len, pub_key, sizeof(pub_key));
1678 if (ret != 0) {
1679 return ret;
1680 }
1681 if ((exp_pub_key_len != pub_key_len) ||
1682 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1683 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1684 }
1685 return 0;
1686}
Valerio Setti4d1daf82023-06-26 13:31:18 +02001687#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti574a00b2023-06-21 19:47:37 +02001688#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1689
Valerio Setti76d0f962023-06-23 13:32:54 +02001690const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = {
Valerio Setti574a00b2023-06-21 19:47:37 +02001691 .type = MBEDTLS_PK_OPAQUE,
1692 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001693 .get_bitlen = opaque_get_bitlen,
1694 .can_do = ecdsa_opaque_can_do,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001695#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001696 .verify_func = ecdsa_opaque_verify_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001697#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1698 .verify_func = NULL,
1699#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1700#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Setti76d0f962023-06-23 13:32:54 +02001701 .sign_func = ecdsa_opaque_sign_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001702#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1703 .sign_func = NULL,
1704#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti97976e32023-06-23 14:08:26 +02001705#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1706 .verify_rs_func = NULL,
1707 .sign_rs_func = NULL,
1708 .rs_alloc_func = NULL,
1709 .rs_free_func = NULL,
1710#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1711 .decrypt_func = NULL,
1712 .encrypt_func = NULL,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001713#if defined(MBEDTLS_ECP_LIGHT)
Valerio Setti76d0f962023-06-23 13:32:54 +02001714 .check_pair_func = ecdsa_opaque_check_pair_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001715#else /* MBEDTLS_ECP_LIGHT */
1716 .check_pair_func = NULL,
1717#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti97976e32023-06-23 14:08:26 +02001718 .ctx_alloc_func = NULL,
1719 .ctx_free_func = NULL,
1720 .debug_func = NULL,
Valerio Setti574a00b2023-06-21 19:47:37 +02001721};
1722
Valerio Setti76d0f962023-06-23 13:32:54 +02001723static int rsa_opaque_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001724{
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 return type == MBEDTLS_PK_RSA ||
1726 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001727}
1728
Valerio Setti574a00b2023-06-21 19:47:37 +02001729#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001730static int rsa_opaque_decrypt(mbedtls_pk_context *pk,
1731 const unsigned char *input, size_t ilen,
1732 unsigned char *output, size_t *olen, size_t osize,
1733 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001734{
Valerio Setti574a00b2023-06-21 19:47:37 +02001735 psa_status_t status;
1736
1737 /* PSA has its own RNG */
1738 (void) f_rng;
1739 (void) p_rng;
1740
1741 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
1742 input, ilen,
1743 NULL, 0,
1744 output, osize, olen);
1745 if (status != PSA_SUCCESS) {
1746 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1747 }
1748
1749 return 0;
1750}
1751#endif /* MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY */
1752
Valerio Setti76d0f962023-06-23 13:32:54 +02001753static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
1754 const unsigned char *hash, size_t hash_len,
1755 unsigned char *sig, size_t sig_size, size_t *sig_len,
1756 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001757{
1758#if defined(MBEDTLS_RSA_C)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001759 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001760 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001761 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001762 psa_status_t status;
1763
1764 /* PSA has its own RNG */
1765 (void) f_rng;
1766 (void) p_rng;
1767
Valerio Setti4f387ef2023-05-02 14:15:59 +02001768 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001770 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 type = psa_get_key_type(&attributes);
1774 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001777 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Valerio Setti4657f102023-06-21 13:55:16 +02001778 } else {
1779 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1780 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001781
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001782 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001783 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 sig, sig_size, sig_len);
1785 if (status != PSA_SUCCESS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001786 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001787 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Valerio Setti4657f102023-06-21 13:55:16 +02001788 } else {
1789 return PSA_PK_TO_MBEDTLS_ERR(status);
1790 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001791 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001792
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001793 return 0;
Valerio Setti574a00b2023-06-21 19:47:37 +02001794#else /* !MBEDTLS_RSA_C */
1795 ((void) pk);
1796 ((void) md_alg);
1797 ((void) hash);
1798 ((void) hash_len);
1799 ((void) sig);
1800 ((void) sig_size);
1801 ((void) sig_len);
1802 ((void) f_rng);
1803 ((void) p_rng);
1804 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti4657f102023-06-21 13:55:16 +02001805#endif /* !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001806}
1807
Valerio Setti76d0f962023-06-23 13:32:54 +02001808const mbedtls_pk_info_t mbedtls_rsa_opaque_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001809 .type = MBEDTLS_PK_OPAQUE,
1810 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001811 .get_bitlen = opaque_get_bitlen,
1812 .can_do = rsa_opaque_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001813 .verify_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001814 .sign_func = rsa_opaque_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001815#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1816 .verify_rs_func = NULL,
1817 .sign_rs_func = NULL,
1818 .rs_alloc_func = NULL,
1819 .rs_free_func = NULL,
1820#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti8bb57632023-05-26 13:48:07 +02001821#if defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001822 .decrypt_func = rsa_opaque_decrypt,
Valerio Setti97976e32023-06-23 14:08:26 +02001823#else /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
1824 .decrypt_func = NULL,
Neil Armstrong30beca32022-05-03 15:42:13 +02001825#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Valerio Setti97976e32023-06-23 14:08:26 +02001826 .encrypt_func = NULL,
1827 .check_pair_func = NULL,
1828 .ctx_alloc_func = NULL,
1829 .ctx_free_func = NULL,
1830 .debug_func = NULL,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001831};
1832
1833#endif /* MBEDTLS_USE_PSA_CRYPTO */
1834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001835#endif /* MBEDTLS_PK_C */