blob: 087a7a386a7026dfd8883d10834e91a96ecf5cfb [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é-Gonnardd73b3c12013-08-12 17:06:05 +020028
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020029/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020038#endif
39
Gilles Peskine8a6022e2022-10-04 23:01:59 +020040#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PSA_CRYPTO_C)
41#include "pkwrite.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010042#endif
43
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050044#if defined(MBEDTLS_PSA_CRYPTO_C)
45#include "mbedtls/psa_util.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050046#endif
47
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010048#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040049#include "psa/crypto.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020050
Valerio Setti80d07982023-02-08 13:49:17 +010051#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020052#include "mbedtls/asn1write.h"
53#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010054#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020055#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010056
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000057#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020058
Andres AG72849872017-01-19 11:24:33 +000059#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010060#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020061#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020062
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050063#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yub02ee182022-03-16 10:30:41 +080064#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010065int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010066{
Gilles Peskine449bd832023-01-11 14:50:10 +010067 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010068 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010070 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010071 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010072 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010074 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010075 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010076 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010077 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010078 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010080 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010082 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010084 case PSA_ERROR_COMMUNICATION_FAILURE:
85 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010087 case PSA_ERROR_DATA_CORRUPT:
88 case PSA_ERROR_DATA_INVALID:
89 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010091 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +010093 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010095 }
96}
97
Neil Armstrong30beca32022-05-03 15:42:13 +020098#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
99 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +0100100int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +0100101{
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +0100103 case PSA_ERROR_NOT_PERMITTED:
104 case PSA_ERROR_INVALID_ARGUMENT:
105 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +0100107 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +0100109 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100111 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100113 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Andrzej Kurekba241382022-12-27 09:17:33 -0500115 case PSA_SUCCESS:
116 return 0;
117 case PSA_ERROR_NOT_SUPPORTED:
118 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
119 case PSA_ERROR_INSUFFICIENT_MEMORY:
120 return MBEDTLS_ERR_PK_ALLOC_FAILED;
121 case PSA_ERROR_BAD_STATE:
122 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
123 case PSA_ERROR_COMMUNICATION_FAILURE:
124 case PSA_ERROR_HARDWARE_FAILURE:
125 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
126 case PSA_ERROR_DATA_CORRUPT:
127 case PSA_ERROR_DATA_INVALID:
128 case PSA_ERROR_STORAGE_FAILURE:
129 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
130 case PSA_ERROR_CORRUPTION_DETECTED:
131 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100132 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500133 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +0100134 }
135}
Neil Armstrong30beca32022-05-03 15:42:13 +0200136#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800137#endif /* MBEDTLS_PSA_CRYPTO_C */
138
139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu07869e82022-03-16 16:40:50 +0800140#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100141int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800142{
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800144 case PSA_ERROR_NOT_PERMITTED:
145 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800147 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800149 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800151 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800153 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Andrzej Kurekba241382022-12-27 09:17:33 -0500155 case PSA_SUCCESS:
156 return 0;
157 case PSA_ERROR_NOT_SUPPORTED:
158 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
159 case PSA_ERROR_INSUFFICIENT_MEMORY:
160 return MBEDTLS_ERR_PK_ALLOC_FAILED;
161 case PSA_ERROR_BAD_STATE:
162 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
163 case PSA_ERROR_COMMUNICATION_FAILURE:
164 case PSA_ERROR_HARDWARE_FAILURE:
165 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
166 case PSA_ERROR_DATA_CORRUPT:
167 case PSA_ERROR_DATA_INVALID:
168 case PSA_ERROR_STORAGE_FAILURE:
169 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
170 case PSA_ERROR_CORRUPTION_DETECTED:
171 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu07869e82022-03-16 16:40:50 +0800172 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500173 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Jerry Yu07869e82022-03-16 16:40:50 +0800174 }
175}
Jerry Yu75339822022-03-23 12:06:31 +0800176#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu75339822022-03-23 12:06:31 +0800177#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500178#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Neil Armstrong19915c22022-03-01 15:21:02 +0100179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100181static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200182{
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 return type == MBEDTLS_PK_RSA ||
184 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200185}
186
valerio38992cb2023-04-20 09:56:30 +0200187static size_t rsa_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200188{
valerio38992cb2023-04-20 09:56:30 +0200189 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200191}
192
Neil Armstrong52f41f82022-02-22 15:30:24 +0100193#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200194static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 const unsigned char *hash, size_t hash_len,
196 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200197{
valerio38992cb2023-04-20 09:56:30 +0200198 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100199 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
200 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
201 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
202 psa_status_t status;
203 mbedtls_pk_context key;
204 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100205 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100206 psa_algorithm_t psa_alg_md =
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200207 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
211 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
212 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (sig_len < rsa_len) {
215 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
216 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100217
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100218 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100219 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100220 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200221 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
223 if (key_len <= 0) {
224 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
225 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
228 psa_set_key_algorithm(&attributes, psa_alg_md);
229 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 status = psa_import_key(&attributes,
232 buf + sizeof(buf) - key_len, key_len,
233 &key_id);
234 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500235 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100236 goto cleanup;
237 }
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
240 sig, sig_len);
241 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500242 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100243 goto cleanup;
244 }
245 ret = 0;
246
247cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 status = psa_destroy_key(key_id);
249 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500250 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100254}
255#else
valerio38992cb2023-04-20 09:56:30 +0200256static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 const unsigned char *hash, size_t hash_len,
258 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100259{
Janos Follath24eed8d2019-11-22 13:21:35 +0000260 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valerio38992cb2023-04-20 09:56:30 +0200261 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
265 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
266 }
Andres AG72849872017-01-19 11:24:33 +0000267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if (sig_len < rsa_len) {
269 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
270 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
273 (unsigned int) hash_len,
274 hash, sig)) != 0) {
275 return ret;
276 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200277
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200278 /* The buffer contains a valid signature followed by extra data.
279 * We have a special error code for that so that so that callers can
280 * use mbedtls_pk_verify() to check "Does the buffer start with a
281 * valid signature?" and not just "Does the buffer contain a valid
282 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (sig_len > rsa_len) {
284 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
285 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200288}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100289#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200290
Jerry Yub02ee182022-03-16 10:30:41 +0800291#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100292int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
293 mbedtls_rsa_context *rsa_ctx,
294 const unsigned char *hash, size_t hash_len,
295 unsigned char *sig, size_t sig_size,
296 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100297{
Neil Armstrong98545682022-02-22 16:12:51 +0100298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
299 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
300 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
301 psa_status_t status;
302 mbedtls_pk_context key;
303 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100304 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100305 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
308 if (sig_size < *sig_len) {
309 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
310 }
Neil Armstrong98545682022-02-22 16:12:51 +0100311
Neil Armstronge4f28682022-02-24 15:41:39 +0100312 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100313 * re-construct one to make it happy */
314 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800315 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
317 if (key_len <= 0) {
318 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
319 }
320 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
321 psa_set_key_algorithm(&attributes, alg);
322 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 status = psa_import_key(&attributes,
325 buf + sizeof(buf) - key_len, key_len,
326 &key_id);
327 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500328 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100329 goto cleanup;
330 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 status = psa_sign_hash(key_id, alg, hash, hash_len,
332 sig, sig_size, sig_len);
333 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500334 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100335 goto cleanup;
336 }
337
338 ret = 0;
339
340cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 status = psa_destroy_key(key_id);
342 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500343 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 }
345 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100346}
Jerry Yu406cf272022-03-22 11:33:42 +0800347#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800348
349#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200350static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 const unsigned char *hash, size_t hash_len,
352 unsigned char *sig, size_t sig_size, size_t *sig_len,
353 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800354{
Jerry Yu1d172a32022-03-12 19:12:05 +0800355 ((void) f_rng);
356 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800357
Jerry Yubd1b3272022-03-24 13:05:20 +0800358 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200359 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (psa_md_alg == 0) {
361 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
362 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
365 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200366 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800368}
Neil Armstrong98545682022-02-22 16:12:51 +0100369#else
valerio38992cb2023-04-20 09:56:30 +0200370static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 const unsigned char *hash, size_t hash_len,
372 unsigned char *sig, size_t sig_size, size_t *sig_len,
373 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200374{
valerio38992cb2023-04-20 09:56:30 +0200375 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
378 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
379 }
Andres AG72849872017-01-19 11:24:33 +0000380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 *sig_len = mbedtls_rsa_get_len(rsa);
382 if (sig_size < *sig_len) {
383 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
384 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
387 md_alg, (unsigned int) hash_len,
388 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200389}
Neil Armstrong98545682022-02-22 16:12:51 +0100390#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200391
Neil Armstrong18f43c72022-02-09 15:32:45 +0100392#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200393static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 const unsigned char *input, size_t ilen,
395 unsigned char *output, size_t *olen, size_t osize,
396 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100397{
valerio38992cb2023-04-20 09:56:30 +0200398 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
401 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
402 psa_status_t status;
403 mbedtls_pk_context key;
404 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100405 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100406
407 ((void) f_rng);
408 ((void) p_rng);
409
410#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
412 return MBEDTLS_ERR_RSA_INVALID_PADDING;
413 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100414#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (ilen != mbedtls_rsa_get_len(rsa)) {
417 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
418 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100419
420 /* mbedtls_pk_write_key_der() expects a full PK context;
421 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100422 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200423 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
425 if (key_len <= 0) {
426 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
427 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
430 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
431 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 status = psa_import_key(&attributes,
434 buf + sizeof(buf) - key_len, key_len,
435 &key_id);
436 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500437 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100438 goto cleanup;
439 }
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
442 input, ilen,
443 NULL, 0,
444 output, osize, olen);
445 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500446 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100447 goto cleanup;
448 }
449
450 ret = 0;
451
452cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 mbedtls_platform_zeroize(buf, sizeof(buf));
454 status = psa_destroy_key(key_id);
455 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500456 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100460}
461#else
valerio38992cb2023-04-20 09:56:30 +0200462static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 const unsigned char *input, size_t ilen,
464 unsigned char *output, size_t *olen, size_t osize,
465 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200466{
valerio38992cb2023-04-20 09:56:30 +0200467 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (ilen != mbedtls_rsa_get_len(rsa)) {
470 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
471 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
474 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200475}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100476#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200477
Neil Armstrong96a16a42022-02-10 10:40:11 +0100478#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200479static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 const unsigned char *input, size_t ilen,
481 unsigned char *output, size_t *olen, size_t osize,
482 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100483{
valerio38992cb2023-04-20 09:56:30 +0200484 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
487 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
488 psa_status_t status;
489 mbedtls_pk_context key;
490 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100491 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100492
493 ((void) f_rng);
494 ((void) p_rng);
495
496#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
498 return MBEDTLS_ERR_RSA_INVALID_PADDING;
499 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100500#endif
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (mbedtls_rsa_get_len(rsa) > osize) {
503 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
504 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100505
Neil Armstrongac014ca2022-02-24 15:27:54 +0100506 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100507 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100508 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200509 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
511 if (key_len <= 0) {
512 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
513 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
516 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
517 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 status = psa_import_key(&attributes,
520 buf + sizeof(buf) - key_len, key_len,
521 &key_id);
522 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500523 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100524 goto cleanup;
525 }
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
528 input, ilen,
529 NULL, 0,
530 output, osize, olen);
531 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500532 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100533 goto cleanup;
534 }
535
536 ret = 0;
537
538cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 status = psa_destroy_key(key_id);
540 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500541 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100545}
546#else
valerio38992cb2023-04-20 09:56:30 +0200547static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 const unsigned char *input, size_t ilen,
549 unsigned char *output, size_t *olen, size_t osize,
550 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200551{
valerio38992cb2023-04-20 09:56:30 +0200552 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (*olen > osize) {
556 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
557 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
560 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200561}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100562#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200563
valerio38992cb2023-04-20 09:56:30 +0200564static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 int (*f_rng)(void *, unsigned char *, size_t),
566 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100567{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200568 (void) f_rng;
569 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200570 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
571 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100572}
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200575{
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (ctx != NULL) {
579 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
580 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200583}
584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200586{
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
588 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200589}
590
valerio38992cb2023-04-20 09:56:30 +0200591static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200592{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200593#if defined(MBEDTLS_RSA_ALT)
594 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200595 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200596 (void) items;
597#else
valerio38992cb2023-04-20 09:56:30 +0200598 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200601 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200602 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200603
604 items++;
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200607 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200608 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200609#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200610}
611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612const mbedtls_pk_info_t mbedtls_rsa_info = {
613 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200614 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200615 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200616 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200617 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200618 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200619#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200620 NULL,
621 NULL,
622#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200623 rsa_decrypt_wrap,
624 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100625 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200626 rsa_alloc_wrap,
627 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200628#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200629 NULL,
630 NULL,
631#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200632 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200633};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200635
Valerio Setti0d2980f2023-04-05 18:17:13 +0200636#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200637/*
638 * Generic EC key
639 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100640static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200641{
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 return type == MBEDTLS_PK_ECKEY ||
643 type == MBEDTLS_PK_ECKEY_DH ||
644 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200645}
646
valerio38992cb2023-04-20 09:56:30 +0200647static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200648{
Valerio Settia1b8af62023-05-17 15:34:57 +0200649#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
650 return pk->ec_bits;
651#else
valerio38992cb2023-04-20 09:56:30 +0200652 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
653 return ecp->grp.pbits;
Valerio Settia1b8af62023-05-17 15:34:57 +0200654#endif
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200655}
656
Valerio Setti1cdddac2023-02-02 13:55:57 +0100657#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400658#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400659/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500660 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
661 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500662 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
664 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500665{
Janos Follath24eed8d2019-11-22 13:21:35 +0000666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500667 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
670 MBEDTLS_ASN1_INTEGER)) != 0) {
671 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500672 }
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 while (unpadded_len > 0 && **from == 0x00) {
675 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500676 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (unpadded_len > to_len || unpadded_len == 0) {
680 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
681 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500682
Andrzej Kurek9241d182018-11-20 05:04:35 -0500683 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 memset(to, 0x00, padding_len);
685 memcpy(to + padding_len, *from, unpadded_len);
686 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500689}
690
691/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400692 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500693 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500694 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400695 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
697 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400698{
Janos Follath24eed8d2019-11-22 13:21:35 +0000699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500700 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
703 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
704 return ret;
705 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400706
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500707 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
709 return ret;
710 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500711 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
713 return ret;
714 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400717}
718
valerio38992cb2023-04-20 09:56:30 +0200719static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
720 mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 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;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200727 psa_status_t status;
Valerio Settia1b8af62023-05-17 15:34:57 +0200728 unsigned char *p;
729 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
730 size_t signature_len;
731 ((void) md_alg);
732#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
733 unsigned char buf[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
734 psa_ecc_family_t curve = pk->ec_family;
735 size_t curve_bits = pk->ec_bits;
736#else
737 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200738 size_t key_len;
Valerio Setti5c032b52023-02-02 15:10:32 +0100739 /* This buffer will initially contain the public key and then the signature
740 * but at different points in time. For all curves except secp224k1, which
741 * is not currently supported in PSA, the public key is one byte longer
742 * (header byte + 2 numbers, while the signature is only 2 numbers),
743 * so use that as the buffer size. */
Valerio Setti1337a4f2023-01-30 15:54:55 +0100744 unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100745 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100746 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Valerio Settia1b8af62023-05-17 15:34:57 +0200748#endif
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (curve == 0) {
751 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
752 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
755 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
756 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500757
Valerio Settia1b8af62023-05-17 15:34:57 +0200758#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
759 status = psa_import_key(&attributes,
760 pk->pub_raw, pk->pub_raw_len,
761 &key_id);
762#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200763 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
764 MBEDTLS_ECP_PF_UNCOMPRESSED,
765 &key_len, buf, sizeof(buf));
766 if (ret != 0) {
767 goto cleanup;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500768 }
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 status = psa_import_key(&attributes,
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200771 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 &key_id);
Valerio Settia1b8af62023-05-17 15:34:57 +0200773#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500775 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400776 goto cleanup;
777 }
778
Valerio Settia1b8af62023-05-17 15:34:57 +0200779 signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
780 if (signature_len > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500781 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
782 goto cleanup;
783 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 p = (unsigned char *) sig;
Valerio Settia1b8af62023-05-17 15:34:57 +0200786 /* extract_ecdsa_sig's last parameter is the size
Valerio Settif57007d2023-05-19 13:54:39 +0200787 * of each integer to be parsed, so it's actually half
Valerio Settia1b8af62023-05-17 15:34:57 +0200788 * the size of the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
Valerio Settia1b8af62023-05-17 15:34:57 +0200790 signature_len/2)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500791 goto cleanup;
792 }
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 status = psa_verify_hash(key_id, psa_sig_md,
795 hash, hash_len,
Valerio Settia1b8af62023-05-17 15:34:57 +0200796 buf, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500798 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400800 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500803 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
804 goto cleanup;
805 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400806 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400807
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500808cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 status = psa_destroy_key(key_id);
810 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500811 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400815}
816#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200817static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 const unsigned char *hash, size_t hash_len,
819 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200820{
Janos Follath24eed8d2019-11-22 13:21:35 +0000821 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200822 ((void) md_alg);
823
valerio38992cb2023-04-20 09:56:30 +0200824 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
828 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
829 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200832}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400833#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100834#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200835
Valerio Setti1cdddac2023-02-02 13:55:57 +0100836#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100837#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100838/*
839 * Simultaneously convert and move raw MPI from the beginning of a buffer
840 * to an ASN.1 MPI at the end of the buffer.
841 * See also mbedtls_asn1_write_mpi().
842 *
843 * p: pointer to the end of the output buffer
844 * start: start of the output buffer, and also of the mpi to write at the end
845 * n_len: length of the mpi to read from start
846 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
848 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100849{
850 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
851 size_t len = 0;
852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if ((size_t) (*p - start) < n_len) {
854 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
855 }
Neil Armstrong15021652022-03-01 10:14:17 +0100856
857 len = n_len;
858 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100860
861 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
862 * Neither r nor s should be 0, but as a failsafe measure, still detect
863 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100865 ++(*p);
866 --len;
867 }
868
869 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (len == 0) {
871 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
872 }
Neil Armstrong15021652022-03-01 10:14:17 +0100873
874 /* if the msb is 1, ASN.1 requires that we prepend a 0.
875 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (**p & 0x80) {
877 if (*p - start < 1) {
878 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
879 }
Neil Armstrong15021652022-03-01 10:14:17 +0100880
881 *--(*p) = 0x00;
882 len += 1;
883 }
884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
886 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
887 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100890}
891
892/* Transcode signature from PSA format to ASN.1 sequence.
893 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
894 * MPIs, and in-place.
895 *
896 * [in/out] sig: the signature pre- and post-transcoding
897 * [in/out] sig_len: signature length pre- and post-transcoding
898 * [int] buf_len: the available size the in/out buffer
899 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
901 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100902{
903 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
904 size_t len = 0;
905 const size_t rs_len = *sig_len / 2;
906 unsigned char *p = sig + buf_len;
907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
909 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
912 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
913 MBEDTLS_ASN1_CONSTRUCTED |
914 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100917 *sig_len = len;
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100920}
Neil Armstronge9606902022-02-09 14:23:00 +0100921
valerio38992cb2023-04-20 09:56:30 +0200922static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 const unsigned char *hash, size_t hash_len,
924 unsigned char *sig, size_t sig_size, size_t *sig_len,
925 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100926{
Neil Armstronge9606902022-02-09 14:23:00 +0100927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronge9606902022-02-09 14:23:00 +0100928 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
929 psa_status_t status;
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100930#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Valerio Setti5bc52242023-01-30 15:48:28 +0100931 psa_algorithm_t psa_sig_md =
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200932 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100933#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200934 psa_algorithm_t psa_sig_md =
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200935 PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100936#endif
Valerio Settiae8c6282023-05-18 18:57:57 +0200937#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
938 psa_ecc_family_t curve = pk->ec_family;
939#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
940 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
942 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Neil Armstronge9606902022-02-09 14:23:00 +0100943 size_t curve_bits;
944 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200946 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Valerio Settiae8c6282023-05-18 18:57:57 +0200947#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Neil Armstronge9606902022-02-09 14:23:00 +0100948
949 /* PSA has its own RNG */
950 ((void) f_rng);
951 ((void) p_rng);
952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 if (curve == 0) {
954 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
955 }
Neil Armstronge9606902022-02-09 14:23:00 +0100956
Valerio Settiae8c6282023-05-18 18:57:57 +0200957#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
958 if (MBEDTLS_SVC_KEY_ID_GET_KEY_ID(pk->priv_id) == PSA_KEY_ID_NULL) {
959 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
960 }
961 key_id = pk->priv_id;
962#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200963 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100964 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200966 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100968 goto cleanup;
969 }
970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
972 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
973 psa_set_key_algorithm(&attributes, psa_sig_md);
974
975 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200976 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 &key_id);
978 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500979 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100981 }
Valerio Settiae8c6282023-05-18 18:57:57 +0200982#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Neil Armstronge9606902022-02-09 14:23:00 +0100983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
985 sig, sig_size, sig_len);
986 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500987 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 goto cleanup;
989 }
990
991 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100992
993cleanup:
Valerio Settiae8c6282023-05-18 18:57:57 +0200994
995#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_platform_zeroize(buf, sizeof(buf));
997 status = psa_destroy_key(key_id);
Valerio Settiae8c6282023-05-18 18:57:57 +0200998#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001000 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001002
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +01001004}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001005#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +02001006static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 const unsigned char *hash, size_t hash_len,
1008 unsigned char *sig, size_t sig_size, size_t *sig_len,
1009 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001010{
valerio38992cb2023-04-20 09:56:30 +02001011 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 md_alg, hash, hash_len,
1013 sig, sig_size, sig_len,
1014 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001015}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001016#endif /* MBEDTLS_USE_PSA_CRYPTO */
1017#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001018
Valerio Setti80d07982023-02-08 13:49:17 +01001019#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001020/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001021static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001022 const unsigned char *hash, size_t hash_len,
1023 const unsigned char *sig, size_t sig_len,
1024 void *rs_ctx);
1025
valerio38992cb2023-04-20 09:56:30 +02001026static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001027 const unsigned char *hash, size_t hash_len,
1028 unsigned char *sig, size_t sig_size, size_t *sig_len,
1029 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1030 void *rs_ctx);
1031
1032/*
1033 * Restart context for ECDSA operations with ECKEY context
1034 *
1035 * We need to store an actual ECDSA context, as we need to pass the same to
1036 * the underlying ecdsa function, so we can't create it on the fly every time.
1037 */
1038typedef struct {
1039 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1040 mbedtls_ecdsa_context ecdsa_ctx;
1041} eckey_restart_ctx;
1042
1043static void *eckey_rs_alloc(void)
1044{
1045 eckey_restart_ctx *rs_ctx;
1046
1047 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1048
1049 if (ctx != NULL) {
1050 rs_ctx = ctx;
1051 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1052 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1053 }
1054
1055 return ctx;
1056}
1057
1058static void eckey_rs_free(void *ctx)
1059{
1060 eckey_restart_ctx *rs_ctx;
1061
1062 if (ctx == NULL) {
1063 return;
1064 }
1065
1066 rs_ctx = ctx;
1067 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1068 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1069
1070 mbedtls_free(ctx);
1071}
1072
valerio38992cb2023-04-20 09:56:30 +02001073static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001074 const unsigned char *hash, size_t hash_len,
1075 const unsigned char *sig, size_t sig_len,
1076 void *rs_ctx)
1077{
1078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1079 eckey_restart_ctx *rs = rs_ctx;
1080
1081 /* Should never happen */
1082 if (rs == NULL) {
1083 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1084 }
1085
1086 /* set up our own sub-context if needed (that is, on first run) */
1087 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001088 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001089 }
1090
valerio38992cb2023-04-20 09:56:30 +02001091 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001092 md_alg, hash, hash_len,
1093 sig, sig_len, &rs->ecdsa_rs));
1094
1095cleanup:
1096 return ret;
1097}
1098
valerio38992cb2023-04-20 09:56:30 +02001099static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001100 const unsigned char *hash, size_t hash_len,
1101 unsigned char *sig, size_t sig_size, size_t *sig_len,
1102 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1103 void *rs_ctx)
1104{
1105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1106 eckey_restart_ctx *rs = rs_ctx;
1107
1108 /* Should never happen */
1109 if (rs == NULL) {
1110 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1111 }
1112
1113 /* set up our own sub-context if needed (that is, on first run) */
1114 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001115 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001116 }
1117
valerio38992cb2023-04-20 09:56:30 +02001118 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001119 hash, hash_len, sig, sig_size, sig_len,
1120 f_rng, p_rng, &rs->ecdsa_rs));
1121
1122cleanup:
1123 return ret;
1124}
Valerio Setti80d07982023-02-08 13:49:17 +01001125#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001126
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001127#if defined(MBEDTLS_USE_PSA_CRYPTO)
1128/*
1129 * Alternative function used to verify that the EC private/public key pair
1130 * is valid using PSA functions instead of ECP ones.
1131 * The flow is:
1132 * - import the private key "prv" to PSA and export its public part
1133 * - write the raw content of public key "pub" to a local buffer
1134 * - compare the two buffers
1135 */
valerio38992cb2023-04-20 09:56:30 +02001136static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001137{
Valerio Setti9efa8c42023-05-19 13:27:30 +02001138 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001140 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001141 size_t prv_key_len;
Valerio Settia1b8af62023-05-17 15:34:57 +02001142#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Settiae8c6282023-05-18 18:57:57 +02001143 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001144
1145 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1146 &prv_key_len);
1147 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1148 if (ret != 0) {
1149 return ret;
1150 }
1151
1152 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1153 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1154 }
Valerio Settia1b8af62023-05-17 15:34:57 +02001155#else /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti9efa8c42023-05-19 13:27:30 +02001156 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001157 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1158 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001159 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1160 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001161 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001162 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001163 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +02001164 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001165
Valerio Settia7cb8452023-05-22 18:39:43 +02001166 if (curve == 0) {
1167 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1168 }
1169
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001170 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1171 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1172
Valerio Settia1b8af62023-05-17 15:34:57 +02001173 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1174 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001175 if (ret != 0) {
1176 return ret;
1177 }
1178
1179 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti1df94f82023-04-07 08:59:24 +02001180 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1181 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001182 return ret;
1183 }
1184
1185 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
1186
Valerio Setti1df94f82023-04-07 08:59:24 +02001187 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1188 &prv_key_len);
1189 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1190 destruction_status = psa_destroy_key(key_id);
1191 if (ret != 0) {
1192 return ret;
1193 } else if (destruction_status != PSA_SUCCESS) {
1194 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001195 }
1196
Valerio Settia1b8af62023-05-17 15:34:57 +02001197 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1198 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001199 MBEDTLS_ECP_PF_UNCOMPRESSED,
1200 &pub_key_len, pub_key_buf,
1201 sizeof(pub_key_buf));
1202 if (ret != 0) {
1203 return ret;
1204 }
1205
1206 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1207 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1208 }
Valerio Settia1b8af62023-05-17 15:34:57 +02001209#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001210
1211 return 0;
1212}
1213#endif /* MBEDTLS_USE_PSA_CRYPTO */
1214
valerio38992cb2023-04-20 09:56:30 +02001215static int eckey_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001216 int (*f_rng)(void *, unsigned char *, size_t),
1217 void *p_rng)
1218{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001219#if defined(MBEDTLS_USE_PSA_CRYPTO)
1220 (void) f_rng;
1221 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001222 return eckey_check_pair_psa(pub, prv);
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001223#elif defined(MBEDTLS_ECP_C)
valerio38992cb2023-04-20 09:56:30 +02001224 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1225 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001226 f_rng, p_rng);
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001227#else
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001228 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001229#endif
Valerio Setti1cdddac2023-02-02 13:55:57 +01001230}
1231
Valerio Settib5361262023-05-18 18:51:58 +02001232#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001233static void *eckey_alloc_wrap(void)
1234{
1235 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1236
1237 if (ctx != NULL) {
1238 mbedtls_ecp_keypair_init(ctx);
1239 }
1240
1241 return ctx;
1242}
1243
1244static void eckey_free_wrap(void *ctx)
1245{
1246 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1247 mbedtls_free(ctx);
1248}
Valerio Settib5361262023-05-18 18:51:58 +02001249#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001250
valerio38992cb2023-04-20 09:56:30 +02001251static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001252{
Valerio Settia1b8af62023-05-17 15:34:57 +02001253#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1254 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1255 items->name = "eckey.Q";
1256 items->value = pk;
1257#else
valerio38992cb2023-04-20 09:56:30 +02001258 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001259 items->type = MBEDTLS_PK_DEBUG_ECP;
1260 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001261 items->value = &(ecp->Q);
Valerio Settia1b8af62023-05-17 15:34:57 +02001262#endif
Valerio Setti1cdddac2023-02-02 13:55:57 +01001263}
1264
1265const mbedtls_pk_info_t mbedtls_eckey_info = {
1266 MBEDTLS_PK_ECKEY,
1267 "EC",
1268 eckey_get_bitlen,
1269 eckey_can_do,
1270#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1271 ecdsa_verify_wrap, /* Compatible key structures */
1272#else
1273 NULL,
1274#endif
1275#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1276 ecdsa_sign_wrap, /* Compatible key structures */
1277#else
1278 NULL,
1279#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001280#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001281 eckey_verify_rs_wrap,
1282 eckey_sign_rs_wrap,
1283#endif
1284 NULL,
1285 NULL,
1286 eckey_check_pair,
Valerio Settib5361262023-05-18 18:51:58 +02001287#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1288 NULL,
1289 NULL,
1290#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001291 eckey_alloc_wrap,
1292 eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001293#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001294#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1295 eckey_rs_alloc,
1296 eckey_rs_free,
1297#endif
1298 eckey_debug,
1299};
1300
1301/*
1302 * EC key restricted to ECDH
1303 */
1304static int eckeydh_can_do(mbedtls_pk_type_t type)
1305{
1306 return type == MBEDTLS_PK_ECKEY ||
1307 type == MBEDTLS_PK_ECKEY_DH;
1308}
1309
1310const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1311 MBEDTLS_PK_ECKEY_DH,
1312 "EC_DH",
1313 eckey_get_bitlen, /* Same underlying key structure */
1314 eckeydh_can_do,
1315 NULL,
1316 NULL,
1317#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1318 NULL,
1319 NULL,
1320#endif
1321 NULL,
1322 NULL,
1323 eckey_check_pair,
Valerio Settib5361262023-05-18 18:51:58 +02001324#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1325 NULL,
1326 NULL,
1327#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1328 eckey_alloc_wrap, /* Same underlying key structure */
1329 eckey_free_wrap, /* Same underlying key structure */
1330#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001331#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1332 NULL,
1333 NULL,
1334#endif
1335 eckey_debug, /* Same underlying key structure */
1336};
Valerio Setti0d2980f2023-04-05 18:17:13 +02001337#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001338
1339#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1340static int ecdsa_can_do(mbedtls_pk_type_t type)
1341{
1342 return type == MBEDTLS_PK_ECDSA;
1343}
1344
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001345#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001346static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 const unsigned char *hash, size_t hash_len,
1348 const unsigned char *sig, size_t sig_len,
1349 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001350{
Janos Follath24eed8d2019-11-22 13:21:35 +00001351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001352 ((void) md_alg);
1353
1354 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001355 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 hash, hash_len, sig, sig_len,
1357 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1360 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1361 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001364}
1365
valerio38992cb2023-04-20 09:56:30 +02001366static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 const unsigned char *hash, size_t hash_len,
1368 unsigned char *sig, size_t sig_size, size_t *sig_len,
1369 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1370 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001371{
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001373 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1375 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001376
1377}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001380{
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (ctx != NULL) {
1384 mbedtls_ecdsa_restart_init(ctx);
1385 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001386
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001388}
1389
Gilles Peskine449bd832023-01-11 14:50:10 +01001390static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001391{
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 mbedtls_ecdsa_restart_free(ctx);
1393 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001394}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001395#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1398 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001399 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001400 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001401 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001402#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1403 ecdsa_verify_wrap, /* Compatible key structures */
1404#else
1405 NULL,
1406#endif
1407#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1408 ecdsa_sign_wrap, /* Compatible key structures */
1409#else
1410 NULL,
1411#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001412#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001413 ecdsa_verify_rs_wrap,
1414 ecdsa_sign_rs_wrap,
1415#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001416 NULL,
1417 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001418 eckey_check_pair, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001419#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1420 NULL,
1421 NULL,
1422#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti24138d92023-01-27 14:24:09 +01001423 eckey_alloc_wrap, /* Compatible key structures */
1424 eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001425#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001426#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001427 ecdsa_rs_alloc,
1428 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001429#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001430 eckey_debug, /* Compatible key structures */
1431};
Valerio Setti7ca13182023-01-27 13:22:42 +01001432#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001434#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001435/*
1436 * Support for alternative RSA-private implementations
1437 */
1438
Gilles Peskine449bd832023-01-11 14:50:10 +01001439static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001440{
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001442}
1443
valerio38992cb2023-04-20 09:56:30 +02001444static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001445{
valerio38992cb2023-04-20 09:56:30 +02001446 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001447
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001449}
1450
valerio38992cb2023-04-20 09:56:30 +02001451static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 const unsigned char *hash, size_t hash_len,
1453 unsigned char *sig, size_t sig_size, size_t *sig_len,
1454 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001455{
valerio38992cb2023-04-20 09:56:30 +02001456 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (UINT_MAX < hash_len) {
1459 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1460 }
Andres AG72849872017-01-19 11:24:33 +00001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1463 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1464 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1465 }
1466 if (*sig_len > sig_size) {
1467 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1468 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1471 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001472}
1473
valerio38992cb2023-04-20 09:56:30 +02001474static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 const unsigned char *input, size_t ilen,
1476 unsigned char *output, size_t *olen, size_t osize,
1477 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001478{
valerio38992cb2023-04-20 09:56:30 +02001479 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001480
1481 ((void) f_rng);
1482 ((void) p_rng);
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1485 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1486 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 return rsa_alt->decrypt_func(rsa_alt->key,
1489 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001490}
1491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001493static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 int (*f_rng)(void *, unsigned char *, size_t),
1495 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001498 unsigned char hash[32];
1499 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001500 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1503 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001504 }
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 memset(hash, 0x2a, sizeof(hash));
1507
valerio38992cb2023-04-20 09:56:30 +02001508 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 hash, sizeof(hash),
1510 sig, sizeof(sig), &sig_len,
1511 f_rng, p_rng)) != 0) {
1512 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001513 }
1514
valerio38992cb2023-04-20 09:56:30 +02001515 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 hash, sizeof(hash), sig, sig_len) != 0) {
1517 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1518 }
1519
1520 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001522#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001525{
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001527
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 if (ctx != NULL) {
1529 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1530 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001533}
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001536{
Gilles Peskine449bd832023-01-11 14:50:10 +01001537 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1538 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001539}
1540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1542 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001543 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001544 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001545 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001546 NULL,
1547 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001548#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001549 NULL,
1550 NULL,
1551#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001552 rsa_alt_decrypt_wrap,
1553 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001555 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001556#else
1557 NULL,
1558#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001559 rsa_alt_alloc_wrap,
1560 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001561#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001562 NULL,
1563 NULL,
1564#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001565 NULL,
1566};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001569
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001570#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +02001571static size_t pk_opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001572{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001573 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001574 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001575
Valerio Setti4f387ef2023-05-02 14:15:59 +02001576 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 return 0;
1578 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001579
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 bits = psa_get_key_bits(&attributes);
1581 psa_reset_key_attributes(&attributes);
1582 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001583}
1584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001586{
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 return type == MBEDTLS_PK_ECKEY ||
1588 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001589}
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001592{
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 return type == MBEDTLS_PK_RSA ||
1594 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001595}
1596
valerio38992cb2023-04-20 09:56:30 +02001597static int pk_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 const unsigned char *hash, size_t hash_len,
1599 unsigned char *sig, size_t sig_size, size_t *sig_len,
1600 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001601{
Valerio Settiab363d92023-01-26 14:31:54 +01001602#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001603 ((void) pk);
John Durkopf35069a2020-08-17 22:05:14 -07001604 ((void) md_alg);
1605 ((void) hash);
1606 ((void) hash_len);
1607 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001608 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001609 ((void) sig_len);
1610 ((void) f_rng);
1611 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001613#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001614 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001615 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001616 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001617 psa_status_t status;
1618
1619 /* PSA has its own RNG */
1620 (void) f_rng;
1621 (void) p_rng;
1622
Valerio Setti4f387ef2023-05-02 14:15:59 +02001623 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001625 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 type = psa_get_key_type(&attributes);
1629 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001630
Valerio Settiab363d92023-01-26 14:31:54 +01001631#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001633 alg = PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001635#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001636#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001637 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001638 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001640#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001641 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001642
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001643 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001644 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001645 sig, sig_size, sig_len);
1646 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001647#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001649 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001650 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001651#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001652#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001653 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001654 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001656#endif /* MBEDTLS_RSA_C */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001657 return PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001658 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001659
Valerio Settiab363d92023-01-26 14:31:54 +01001660#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001661 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001662 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1664 }
Valerio Settiab363d92023-01-26 14:31:54 +01001665#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001666
1667 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001668#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001669}
1670
valerioeab9a852023-06-01 10:58:19 +02001671static int pk_opaque_ec_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1672 int (*f_rng)(void *, unsigned char *, size_t),
1673 void *p_rng)
1674{
1675 /* The main difference between this function and eckey_check_pair_psa() is
1676 * that in the opaque case the private key is always stored in PSA side no
1677 * matter if MBEDTLS_PK_USE_PSA_EC_DATA is enabled or not.
1678 * When MBEDTLS_PK_USE_PSA_EC_DATA is enabled, we can simply use the
1679 * eckey_check_pair_psa(). */
1680 (void) f_rng;
1681 (void) p_rng;
1682
1683#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1684 return eckey_check_pair_psa(pub, prv);
1685#elif defined(MBEDTLS_ECP_LIGHT)
1686 psa_status_t status;
1687 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1688 size_t exp_pub_key_len = 0;
1689 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1690 size_t pub_key_len = 0;
1691 int ret;
1692
1693 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1694 &exp_pub_key_len);
1695 if (status != PSA_SUCCESS) {
1696 ret = psa_pk_status_to_mbedtls(status);
1697 return ret;
1698 }
1699 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1700 &(mbedtls_pk_ec_ro(*pub)->Q),
1701 MBEDTLS_ECP_PF_UNCOMPRESSED,
1702 &pub_key_len, pub_key, sizeof(pub_key));
1703 if (ret != 0) {
1704 return ret;
1705 }
1706 if ((exp_pub_key_len != pub_key_len) ||
1707 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1708 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1709 }
1710 return 0;
1711#else
1712 (void) pub;
1713 (void) prv;
1714 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1715#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
1716}
1717
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001718const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001719 MBEDTLS_PK_OPAQUE,
1720 "Opaque",
1721 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001722 pk_opaque_ecdsa_can_do,
1723 NULL, /* verify - will be done later */
1724 pk_opaque_sign_wrap,
1725#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1726 NULL, /* restartable verify - not relevant */
1727 NULL, /* restartable sign - not relevant */
1728#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001729 NULL, /* decrypt - not relevant */
1730 NULL, /* encrypt - not relevant */
valerioeab9a852023-06-01 10:58:19 +02001731 pk_opaque_ec_check_pair,
Valerio Settie00954d2023-04-28 15:24:32 +02001732 NULL, /* alloc - no need to allocate new data dynamically */
1733 NULL, /* free - as for the alloc, there is no data to free */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001734#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1735 NULL, /* restart alloc - not relevant */
1736 NULL, /* restart free - not relevant */
1737#endif
1738 NULL, /* debug - could be done later, or even left NULL */
1739};
1740
Neil Armstrong30beca32022-05-03 15:42:13 +02001741#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
valerio38992cb2023-04-20 09:56:30 +02001742static int pk_opaque_rsa_decrypt(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 const unsigned char *input, size_t ilen,
1744 unsigned char *output, size_t *olen, size_t osize,
1745 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001746{
Neil Armstrong10828182022-04-22 15:02:27 +02001747 psa_status_t status;
1748
1749 /* PSA has its own RNG */
1750 (void) f_rng;
1751 (void) p_rng;
1752
Valerio Setti4f387ef2023-05-02 14:15:59 +02001753 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 input, ilen,
1755 NULL, 0,
1756 output, osize, olen);
1757 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001758 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001759 }
1760
1761 return 0;
1762}
Neil Armstrong30beca32022-05-03 15:42:13 +02001763#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001764
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001765const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1766 MBEDTLS_PK_OPAQUE,
1767 "Opaque",
1768 pk_opaque_get_bitlen,
1769 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001770 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001771 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001772#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1773 NULL, /* restartable verify - not relevant */
1774 NULL, /* restartable sign - not relevant */
1775#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001776#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001777 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001778#else
1779 NULL, /* decrypt - not available */
1780#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001781 NULL, /* encrypt - will be done later */
1782 NULL, /* check_pair - could be done later or left NULL */
Valerio Settie00954d2023-04-28 15:24:32 +02001783 NULL, /* alloc - no need to allocate new data dynamically */
1784 NULL, /* free - as for the alloc, there is no data to free */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001785#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1786 NULL, /* restart alloc - not relevant */
1787 NULL, /* restart free - not relevant */
1788#endif
1789 NULL, /* debug - could be done later, or even left NULL */
1790};
1791
1792#endif /* MBEDTLS_USE_PSA_CRYPTO */
1793
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001794#endif /* MBEDTLS_PK_C */