blob: 7f5e751a9b449c40c00e0c43a43cd287995ed585 [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"
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020050#include "hash_info.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020051
Valerio Setti80d07982023-02-08 13:49:17 +010052#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020053#include "mbedtls/asn1write.h"
54#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010055#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020056#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010057
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059
Andres AG72849872017-01-19 11:24:33 +000060#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010061#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020062#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020063
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050064#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yub02ee182022-03-16 10:30:41 +080065#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010066int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010069 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010070 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010071 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010073 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010074 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010075 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010077 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010078 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010079 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010081 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010083 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010084 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010085 case PSA_ERROR_COMMUNICATION_FAILURE:
86 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010088 case PSA_ERROR_DATA_CORRUPT:
89 case PSA_ERROR_DATA_INVALID:
90 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010091 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010092 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +010094 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010096 }
97}
98
Neil Armstrong30beca32022-05-03 15:42:13 +020099#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
100 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +0100101int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +0100102{
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +0100104 case PSA_ERROR_NOT_PERMITTED:
105 case PSA_ERROR_INVALID_ARGUMENT:
106 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +0100108 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +0100110 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100112 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100114 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Andrzej Kurekba241382022-12-27 09:17:33 -0500116 case PSA_SUCCESS:
117 return 0;
118 case PSA_ERROR_NOT_SUPPORTED:
119 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
120 case PSA_ERROR_INSUFFICIENT_MEMORY:
121 return MBEDTLS_ERR_PK_ALLOC_FAILED;
122 case PSA_ERROR_BAD_STATE:
123 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
124 case PSA_ERROR_COMMUNICATION_FAILURE:
125 case PSA_ERROR_HARDWARE_FAILURE:
126 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
127 case PSA_ERROR_DATA_CORRUPT:
128 case PSA_ERROR_DATA_INVALID:
129 case PSA_ERROR_STORAGE_FAILURE:
130 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
131 case PSA_ERROR_CORRUPTION_DETECTED:
132 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100133 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500134 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +0100135 }
136}
Neil Armstrong30beca32022-05-03 15:42:13 +0200137#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800138#endif /* MBEDTLS_PSA_CRYPTO_C */
139
140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu07869e82022-03-16 16:40:50 +0800141#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100142int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800143{
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800145 case PSA_ERROR_NOT_PERMITTED:
146 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800148 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800150 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800152 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800154 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Andrzej Kurekba241382022-12-27 09:17:33 -0500156 case PSA_SUCCESS:
157 return 0;
158 case PSA_ERROR_NOT_SUPPORTED:
159 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
160 case PSA_ERROR_INSUFFICIENT_MEMORY:
161 return MBEDTLS_ERR_PK_ALLOC_FAILED;
162 case PSA_ERROR_BAD_STATE:
163 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
164 case PSA_ERROR_COMMUNICATION_FAILURE:
165 case PSA_ERROR_HARDWARE_FAILURE:
166 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
167 case PSA_ERROR_DATA_CORRUPT:
168 case PSA_ERROR_DATA_INVALID:
169 case PSA_ERROR_STORAGE_FAILURE:
170 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
171 case PSA_ERROR_CORRUPTION_DETECTED:
172 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu07869e82022-03-16 16:40:50 +0800173 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500174 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Jerry Yu07869e82022-03-16 16:40:50 +0800175 }
176}
Jerry Yu75339822022-03-23 12:06:31 +0800177#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu75339822022-03-23 12:06:31 +0800178#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500179#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Neil Armstrong19915c22022-03-01 15:21:02 +0100180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100182static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200183{
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return type == MBEDTLS_PK_RSA ||
185 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200186}
187
valerio38992cb2023-04-20 09:56:30 +0200188static size_t rsa_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200189{
valerio38992cb2023-04-20 09:56:30 +0200190 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200192}
193
Neil Armstrong52f41f82022-02-22 15:30:24 +0100194#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200195static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 const unsigned char *hash, size_t hash_len,
197 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200198{
valerio38992cb2023-04-20 09:56:30 +0200199 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
201 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
202 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
203 psa_status_t status;
204 mbedtls_pk_context key;
205 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100206 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100207 psa_algorithm_t psa_alg_md =
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
209 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
212 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
213 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 if (sig_len < rsa_len) {
216 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
217 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100218
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100219 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100220 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100221 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200222 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
224 if (key_len <= 0) {
225 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
226 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
229 psa_set_key_algorithm(&attributes, psa_alg_md);
230 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 status = psa_import_key(&attributes,
233 buf + sizeof(buf) - key_len, key_len,
234 &key_id);
235 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500236 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100237 goto cleanup;
238 }
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
241 sig, sig_len);
242 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500243 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100244 goto cleanup;
245 }
246 ret = 0;
247
248cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 status = psa_destroy_key(key_id);
250 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500251 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100255}
256#else
valerio38992cb2023-04-20 09:56:30 +0200257static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 const unsigned char *hash, size_t hash_len,
259 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100260{
Janos Follath24eed8d2019-11-22 13:21:35 +0000261 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valerio38992cb2023-04-20 09:56:30 +0200262 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
266 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
267 }
Andres AG72849872017-01-19 11:24:33 +0000268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (sig_len < rsa_len) {
270 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
271 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
274 (unsigned int) hash_len,
275 hash, sig)) != 0) {
276 return ret;
277 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200278
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200279 /* The buffer contains a valid signature followed by extra data.
280 * We have a special error code for that so that so that callers can
281 * use mbedtls_pk_verify() to check "Does the buffer start with a
282 * valid signature?" and not just "Does the buffer contain a valid
283 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (sig_len > rsa_len) {
285 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
286 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200289}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100290#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200291
Jerry Yub02ee182022-03-16 10:30:41 +0800292#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
294 mbedtls_rsa_context *rsa_ctx,
295 const unsigned char *hash, size_t hash_len,
296 unsigned char *sig, size_t sig_size,
297 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100298{
Neil Armstrong98545682022-02-22 16:12:51 +0100299 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
300 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
301 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
302 psa_status_t status;
303 mbedtls_pk_context key;
304 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100305 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100306 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
309 if (sig_size < *sig_len) {
310 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
311 }
Neil Armstrong98545682022-02-22 16:12:51 +0100312
Neil Armstronge4f28682022-02-24 15:41:39 +0100313 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100314 * re-construct one to make it happy */
315 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800316 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
318 if (key_len <= 0) {
319 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
320 }
321 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
322 psa_set_key_algorithm(&attributes, alg);
323 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 status = psa_import_key(&attributes,
326 buf + sizeof(buf) - key_len, key_len,
327 &key_id);
328 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500329 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100330 goto cleanup;
331 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 status = psa_sign_hash(key_id, alg, hash, hash_len,
333 sig, sig_size, sig_len);
334 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500335 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100336 goto cleanup;
337 }
338
339 ret = 0;
340
341cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 status = psa_destroy_key(key_id);
343 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500344 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 }
346 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100347}
Jerry Yu406cf272022-03-22 11:33:42 +0800348#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800349
350#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200351static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 const unsigned char *hash, size_t hash_len,
353 unsigned char *sig, size_t sig_size, size_t *sig_len,
354 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800355{
Jerry Yu1d172a32022-03-12 19:12:05 +0800356 ((void) f_rng);
357 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800358
Jerry Yubd1b3272022-03-24 13:05:20 +0800359 psa_algorithm_t psa_md_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
361 if (psa_md_alg == 0) {
362 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
363 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
366 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200367 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800369}
Neil Armstrong98545682022-02-22 16:12:51 +0100370#else
valerio38992cb2023-04-20 09:56:30 +0200371static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 const unsigned char *hash, size_t hash_len,
373 unsigned char *sig, size_t sig_size, size_t *sig_len,
374 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200375{
valerio38992cb2023-04-20 09:56:30 +0200376 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
379 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
380 }
Andres AG72849872017-01-19 11:24:33 +0000381
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 *sig_len = mbedtls_rsa_get_len(rsa);
383 if (sig_size < *sig_len) {
384 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
385 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
388 md_alg, (unsigned int) hash_len,
389 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200390}
Neil Armstrong98545682022-02-22 16:12:51 +0100391#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200392
Neil Armstrong18f43c72022-02-09 15:32:45 +0100393#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200394static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 const unsigned char *input, size_t ilen,
396 unsigned char *output, size_t *olen, size_t osize,
397 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100398{
valerio38992cb2023-04-20 09:56:30 +0200399 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100400 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
402 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
403 psa_status_t status;
404 mbedtls_pk_context key;
405 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100406 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100407
408 ((void) f_rng);
409 ((void) p_rng);
410
411#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
413 return MBEDTLS_ERR_RSA_INVALID_PADDING;
414 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100415#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (ilen != mbedtls_rsa_get_len(rsa)) {
418 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
419 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100420
421 /* mbedtls_pk_write_key_der() expects a full PK context;
422 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100423 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200424 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
426 if (key_len <= 0) {
427 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
428 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
431 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
432 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 status = psa_import_key(&attributes,
435 buf + sizeof(buf) - key_len, key_len,
436 &key_id);
437 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500438 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100439 goto cleanup;
440 }
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
443 input, ilen,
444 NULL, 0,
445 output, osize, olen);
446 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500447 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100448 goto cleanup;
449 }
450
451 ret = 0;
452
453cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_platform_zeroize(buf, sizeof(buf));
455 status = psa_destroy_key(key_id);
456 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500457 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100461}
462#else
valerio38992cb2023-04-20 09:56:30 +0200463static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 const unsigned char *input, size_t ilen,
465 unsigned char *output, size_t *olen, size_t osize,
466 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200467{
valerio38992cb2023-04-20 09:56:30 +0200468 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (ilen != mbedtls_rsa_get_len(rsa)) {
471 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
472 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
475 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200476}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100477#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200478
Neil Armstrong96a16a42022-02-10 10:40:11 +0100479#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200480static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 const unsigned char *input, size_t ilen,
482 unsigned char *output, size_t *olen, size_t osize,
483 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100484{
valerio38992cb2023-04-20 09:56:30 +0200485 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100486 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
487 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
488 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
489 psa_status_t status;
490 mbedtls_pk_context key;
491 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100492 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100493
494 ((void) f_rng);
495 ((void) p_rng);
496
497#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
499 return MBEDTLS_ERR_RSA_INVALID_PADDING;
500 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100501#endif
502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (mbedtls_rsa_get_len(rsa) > osize) {
504 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
505 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100506
Neil Armstrongac014ca2022-02-24 15:27:54 +0100507 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100508 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100509 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200510 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
512 if (key_len <= 0) {
513 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
514 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
517 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
518 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 status = psa_import_key(&attributes,
521 buf + sizeof(buf) - key_len, key_len,
522 &key_id);
523 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500524 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100525 goto cleanup;
526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
529 input, ilen,
530 NULL, 0,
531 output, osize, olen);
532 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500533 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100534 goto cleanup;
535 }
536
537 ret = 0;
538
539cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 status = psa_destroy_key(key_id);
541 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500542 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100546}
547#else
valerio38992cb2023-04-20 09:56:30 +0200548static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 const unsigned char *input, size_t ilen,
550 unsigned char *output, size_t *olen, size_t osize,
551 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200552{
valerio38992cb2023-04-20 09:56:30 +0200553 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (*olen > osize) {
557 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
558 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
561 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200562}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100563#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200564
valerio38992cb2023-04-20 09:56:30 +0200565static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 int (*f_rng)(void *, unsigned char *, size_t),
567 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100568{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200569 (void) f_rng;
570 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200571 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
572 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100573}
574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200576{
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if (ctx != NULL) {
580 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
581 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200582
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200584}
585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200587{
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
589 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200590}
591
valerio38992cb2023-04-20 09:56:30 +0200592static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200593{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200594#if defined(MBEDTLS_RSA_ALT)
595 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200596 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200597 (void) items;
598#else
valerio38992cb2023-04-20 09:56:30 +0200599 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200602 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200603 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200604
605 items++;
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200608 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200609 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200610#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200611}
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613const mbedtls_pk_info_t mbedtls_rsa_info = {
614 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200615 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200616 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200617 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200618 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200619 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200620#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200621 NULL,
622 NULL,
623#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200624 rsa_decrypt_wrap,
625 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100626 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200627 rsa_alloc_wrap,
628 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200629#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200630 NULL,
631 NULL,
632#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200633 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200634};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200636
Valerio Setti0d2980f2023-04-05 18:17:13 +0200637#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200638/*
639 * Generic EC key
640 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100641static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200642{
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return type == MBEDTLS_PK_ECKEY ||
644 type == MBEDTLS_PK_ECKEY_DH ||
645 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200646}
647
valerio38992cb2023-04-20 09:56:30 +0200648static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200649{
Valerio Settia1b8af62023-05-17 15:34:57 +0200650#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
651 return pk->ec_bits;
652#else
valerio38992cb2023-04-20 09:56:30 +0200653 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
654 return ecp->grp.pbits;
Valerio Settia1b8af62023-05-17 15:34:57 +0200655#endif
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200656}
657
Valerio Setti1cdddac2023-02-02 13:55:57 +0100658#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400659#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400660/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500661 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
662 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500663 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
665 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500666{
Janos Follath24eed8d2019-11-22 13:21:35 +0000667 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500668 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
671 MBEDTLS_ASN1_INTEGER)) != 0) {
672 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500673 }
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 while (unpadded_len > 0 && **from == 0x00) {
676 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500677 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500678 }
679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (unpadded_len > to_len || unpadded_len == 0) {
681 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
682 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500683
Andrzej Kurek9241d182018-11-20 05:04:35 -0500684 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 memset(to, 0x00, padding_len);
686 memcpy(to + padding_len, *from, unpadded_len);
687 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500690}
691
692/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400693 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500694 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500695 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400696 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
698 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400699{
Janos Follath24eed8d2019-11-22 13:21:35 +0000700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500701 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
704 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
705 return ret;
706 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400707
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500708 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
710 return ret;
711 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500712 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
714 return ret;
715 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400718}
719
valerio38992cb2023-04-20 09:56:30 +0200720static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
721 mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 const unsigned char *hash, size_t hash_len,
723 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400724{
Janos Follath24eed8d2019-11-22 13:21:35 +0000725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100727 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200728 psa_status_t status;
Valerio Settia1b8af62023-05-17 15:34:57 +0200729 unsigned char *p;
730 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
731 size_t signature_len;
732 ((void) md_alg);
733#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
734 unsigned char buf[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
735 psa_ecc_family_t curve = pk->ec_family;
736 size_t curve_bits = pk->ec_bits;
737#else
738 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200739 size_t key_len;
Valerio Setti5c032b52023-02-02 15:10:32 +0100740 /* This buffer will initially contain the public key and then the signature
741 * but at different points in time. For all curves except secp224k1, which
742 * is not currently supported in PSA, the public key is one byte longer
743 * (header byte + 2 numbers, while the signature is only 2 numbers),
744 * so use that as the buffer size. */
Valerio Setti1337a4f2023-01-30 15:54:55 +0100745 unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100746 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100747 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Valerio Settia1b8af62023-05-17 15:34:57 +0200749#endif
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (curve == 0) {
752 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
753 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
756 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
757 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500758
Valerio Settia1b8af62023-05-17 15:34:57 +0200759#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
760 status = psa_import_key(&attributes,
761 pk->pub_raw, pk->pub_raw_len,
762 &key_id);
763#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200764 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
765 MBEDTLS_ECP_PF_UNCOMPRESSED,
766 &key_len, buf, sizeof(buf));
767 if (ret != 0) {
768 goto cleanup;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500769 }
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 status = psa_import_key(&attributes,
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200772 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 &key_id);
Valerio Settia1b8af62023-05-17 15:34:57 +0200774#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500776 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400777 goto cleanup;
778 }
779
Valerio Settia1b8af62023-05-17 15:34:57 +0200780 signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
781 if (signature_len > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500782 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
783 goto cleanup;
784 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 p = (unsigned char *) sig;
Valerio Settia1b8af62023-05-17 15:34:57 +0200787 /* extract_ecdsa_sig's last parameter is the size
Valerio Settif57007d2023-05-19 13:54:39 +0200788 * of each integer to be parsed, so it's actually half
Valerio Settia1b8af62023-05-17 15:34:57 +0200789 * the size of the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
Valerio Settia1b8af62023-05-17 15:34:57 +0200791 signature_len/2)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500792 goto cleanup;
793 }
794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 status = psa_verify_hash(key_id, psa_sig_md,
796 hash, hash_len,
Valerio Settia1b8af62023-05-17 15:34:57 +0200797 buf, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500799 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400801 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500804 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
805 goto cleanup;
806 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400807 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400808
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500809cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 status = psa_destroy_key(key_id);
811 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500812 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400816}
817#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200818static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 const unsigned char *hash, size_t hash_len,
820 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200821{
Janos Follath24eed8d2019-11-22 13:21:35 +0000822 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200823 ((void) md_alg);
824
valerio38992cb2023-04-20 09:56:30 +0200825 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
829 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
830 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200833}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400834#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100835#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200836
Valerio Setti1cdddac2023-02-02 13:55:57 +0100837#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100838#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100839/*
840 * Simultaneously convert and move raw MPI from the beginning of a buffer
841 * to an ASN.1 MPI at the end of the buffer.
842 * See also mbedtls_asn1_write_mpi().
843 *
844 * p: pointer to the end of the output buffer
845 * start: start of the output buffer, and also of the mpi to write at the end
846 * n_len: length of the mpi to read from start
847 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100848static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
849 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100850{
851 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
852 size_t len = 0;
853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if ((size_t) (*p - start) < n_len) {
855 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
856 }
Neil Armstrong15021652022-03-01 10:14:17 +0100857
858 len = n_len;
859 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100861
862 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
863 * Neither r nor s should be 0, but as a failsafe measure, still detect
864 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100866 ++(*p);
867 --len;
868 }
869
870 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 if (len == 0) {
872 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
873 }
Neil Armstrong15021652022-03-01 10:14:17 +0100874
875 /* if the msb is 1, ASN.1 requires that we prepend a 0.
876 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 if (**p & 0x80) {
878 if (*p - start < 1) {
879 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
880 }
Neil Armstrong15021652022-03-01 10:14:17 +0100881
882 *--(*p) = 0x00;
883 len += 1;
884 }
885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
887 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
888 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100891}
892
893/* Transcode signature from PSA format to ASN.1 sequence.
894 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
895 * MPIs, and in-place.
896 *
897 * [in/out] sig: the signature pre- and post-transcoding
898 * [in/out] sig_len: signature length pre- and post-transcoding
899 * [int] buf_len: the available size the in/out buffer
900 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
902 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100903{
904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
905 size_t len = 0;
906 const size_t rs_len = *sig_len / 2;
907 unsigned char *p = sig + buf_len;
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
910 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
913 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
914 MBEDTLS_ASN1_CONSTRUCTED |
915 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100918 *sig_len = len;
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100921}
Neil Armstronge9606902022-02-09 14:23:00 +0100922
valerio38992cb2023-04-20 09:56:30 +0200923static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 const unsigned char *hash, size_t hash_len,
925 unsigned char *sig, size_t sig_size, size_t *sig_len,
926 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100927{
valerio38992cb2023-04-20 09:56:30 +0200928 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
Neil Armstronge9606902022-02-09 14:23:00 +0100929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
930 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
931 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
932 psa_status_t status;
Valerio Setti1337a4f2023-01-30 15:54:55 +0100933 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100934#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Valerio Setti5bc52242023-01-30 15:48:28 +0100935 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100936 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100937#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200938 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100939 PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100940#endif
Neil Armstronge9606902022-02-09 14:23:00 +0100941 size_t curve_bits;
942 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200944 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100945
946 /* PSA has its own RNG */
947 ((void) f_rng);
948 ((void) p_rng);
949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (curve == 0) {
951 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
952 }
Neil Armstronge9606902022-02-09 14:23:00 +0100953
Gilles Peskine13caa942022-10-04 22:59:26 +0200954 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100955 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200957 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100959 goto cleanup;
960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
963 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
964 psa_set_key_algorithm(&attributes, psa_sig_md);
965
966 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200967 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 &key_id);
969 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500970 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100972 }
973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
975 sig, sig_size, sig_len);
976 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500977 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 goto cleanup;
979 }
980
981 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100982
983cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_platform_zeroize(buf, sizeof(buf));
985 status = psa_destroy_key(key_id);
986 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500987 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100991}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100992#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200993static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 const unsigned char *hash, size_t hash_len,
995 unsigned char *sig, size_t sig_size, size_t *sig_len,
996 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200997{
valerio38992cb2023-04-20 09:56:30 +0200998 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 md_alg, hash, hash_len,
1000 sig, sig_size, sig_len,
1001 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001002}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001003#endif /* MBEDTLS_USE_PSA_CRYPTO */
1004#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001005
Valerio Setti80d07982023-02-08 13:49:17 +01001006#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001007/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001008static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001009 const unsigned char *hash, size_t hash_len,
1010 const unsigned char *sig, size_t sig_len,
1011 void *rs_ctx);
1012
valerio38992cb2023-04-20 09:56:30 +02001013static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001014 const unsigned char *hash, size_t hash_len,
1015 unsigned char *sig, size_t sig_size, size_t *sig_len,
1016 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1017 void *rs_ctx);
1018
1019/*
1020 * Restart context for ECDSA operations with ECKEY context
1021 *
1022 * We need to store an actual ECDSA context, as we need to pass the same to
1023 * the underlying ecdsa function, so we can't create it on the fly every time.
1024 */
1025typedef struct {
1026 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1027 mbedtls_ecdsa_context ecdsa_ctx;
1028} eckey_restart_ctx;
1029
1030static void *eckey_rs_alloc(void)
1031{
1032 eckey_restart_ctx *rs_ctx;
1033
1034 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1035
1036 if (ctx != NULL) {
1037 rs_ctx = ctx;
1038 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1039 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1040 }
1041
1042 return ctx;
1043}
1044
1045static void eckey_rs_free(void *ctx)
1046{
1047 eckey_restart_ctx *rs_ctx;
1048
1049 if (ctx == NULL) {
1050 return;
1051 }
1052
1053 rs_ctx = ctx;
1054 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1055 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1056
1057 mbedtls_free(ctx);
1058}
1059
valerio38992cb2023-04-20 09:56:30 +02001060static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001061 const unsigned char *hash, size_t hash_len,
1062 const unsigned char *sig, size_t sig_len,
1063 void *rs_ctx)
1064{
1065 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1066 eckey_restart_ctx *rs = rs_ctx;
1067
1068 /* Should never happen */
1069 if (rs == NULL) {
1070 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1071 }
1072
1073 /* set up our own sub-context if needed (that is, on first run) */
1074 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001075 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001076 }
1077
valerio38992cb2023-04-20 09:56:30 +02001078 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001079 md_alg, hash, hash_len,
1080 sig, sig_len, &rs->ecdsa_rs));
1081
1082cleanup:
1083 return ret;
1084}
1085
valerio38992cb2023-04-20 09:56:30 +02001086static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001087 const unsigned char *hash, size_t hash_len,
1088 unsigned char *sig, size_t sig_size, size_t *sig_len,
1089 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1090 void *rs_ctx)
1091{
1092 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1093 eckey_restart_ctx *rs = rs_ctx;
1094
1095 /* Should never happen */
1096 if (rs == NULL) {
1097 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1098 }
1099
1100 /* set up our own sub-context if needed (that is, on first run) */
1101 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001102 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001103 }
1104
valerio38992cb2023-04-20 09:56:30 +02001105 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001106 hash, hash_len, sig, sig_size, sig_len,
1107 f_rng, p_rng, &rs->ecdsa_rs));
1108
1109cleanup:
1110 return ret;
1111}
Valerio Setti80d07982023-02-08 13:49:17 +01001112#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001113
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001114#if defined(MBEDTLS_USE_PSA_CRYPTO)
1115/*
1116 * Alternative function used to verify that the EC private/public key pair
1117 * is valid using PSA functions instead of ECP ones.
1118 * The flow is:
1119 * - import the private key "prv" to PSA and export its public part
1120 * - write the raw content of public key "pub" to a local buffer
1121 * - compare the two buffers
1122 */
valerio38992cb2023-04-20 09:56:30 +02001123static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001124{
Valerio Setti1df94f82023-04-07 08:59:24 +02001125 psa_status_t status, destruction_status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001126 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001127 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001128 /* We are using MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH for the size of this
1129 * buffer because it will be used to hold the private key at first and
1130 * then its public part (but not at the same time). */
1131 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001132 size_t prv_key_len;
Valerio Settia1b8af62023-05-17 15:34:57 +02001133 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1134#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1135 const psa_ecc_family_t curve = prv->ec_family;
Valerio Settic1541cb2023-05-17 15:49:55 +02001136 const size_t curve_bits = prv->ec_bits;
Valerio Settia1b8af62023-05-17 15:34:57 +02001137#else /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001138 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1139 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001140 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001141 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001142 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settia1b8af62023-05-17 15:34:57 +02001143#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settic1541cb2023-05-17 15:49:55 +02001144 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001145
Valerio Settia7cb8452023-05-22 18:39:43 +02001146 if (curve == 0) {
1147 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1148 }
1149
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001150 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1151 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1152
Valerio Settia1b8af62023-05-17 15:34:57 +02001153 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1154 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001155 if (ret != 0) {
1156 return ret;
1157 }
1158
1159 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti1df94f82023-04-07 08:59:24 +02001160 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1161 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001162 return ret;
1163 }
1164
1165 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
1166
Valerio Setti1df94f82023-04-07 08:59:24 +02001167 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1168 &prv_key_len);
1169 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1170 destruction_status = psa_destroy_key(key_id);
1171 if (ret != 0) {
1172 return ret;
1173 } else if (destruction_status != PSA_SUCCESS) {
1174 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001175 }
1176
Valerio Settia1b8af62023-05-17 15:34:57 +02001177#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1178 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1179 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1180 }
1181#else
1182 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1183 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001184 MBEDTLS_ECP_PF_UNCOMPRESSED,
1185 &pub_key_len, pub_key_buf,
1186 sizeof(pub_key_buf));
1187 if (ret != 0) {
1188 return ret;
1189 }
1190
1191 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1192 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1193 }
Valerio Settia1b8af62023-05-17 15:34:57 +02001194#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001195
1196 return 0;
1197}
1198#endif /* MBEDTLS_USE_PSA_CRYPTO */
1199
valerio38992cb2023-04-20 09:56:30 +02001200static int eckey_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001201 int (*f_rng)(void *, unsigned char *, size_t),
1202 void *p_rng)
1203{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001204#if defined(MBEDTLS_USE_PSA_CRYPTO)
1205 (void) f_rng;
1206 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001207 return eckey_check_pair_psa(pub, prv);
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001208#elif defined(MBEDTLS_ECP_C)
valerio38992cb2023-04-20 09:56:30 +02001209 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1210 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001211 f_rng, p_rng);
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001212#else
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001213 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001214#endif
Valerio Setti1cdddac2023-02-02 13:55:57 +01001215}
1216
Valerio Settib5361262023-05-18 18:51:58 +02001217#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001218static void *eckey_alloc_wrap(void)
1219{
1220 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1221
1222 if (ctx != NULL) {
1223 mbedtls_ecp_keypair_init(ctx);
1224 }
1225
1226 return ctx;
1227}
1228
1229static void eckey_free_wrap(void *ctx)
1230{
1231 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1232 mbedtls_free(ctx);
1233}
Valerio Settib5361262023-05-18 18:51:58 +02001234#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001235
valerio38992cb2023-04-20 09:56:30 +02001236static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001237{
Valerio Settia1b8af62023-05-17 15:34:57 +02001238#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1239 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1240 items->name = "eckey.Q";
1241 items->value = pk;
1242#else
valerio38992cb2023-04-20 09:56:30 +02001243 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001244 items->type = MBEDTLS_PK_DEBUG_ECP;
1245 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001246 items->value = &(ecp->Q);
Valerio Settia1b8af62023-05-17 15:34:57 +02001247#endif
Valerio Setti1cdddac2023-02-02 13:55:57 +01001248}
1249
1250const mbedtls_pk_info_t mbedtls_eckey_info = {
1251 MBEDTLS_PK_ECKEY,
1252 "EC",
1253 eckey_get_bitlen,
1254 eckey_can_do,
1255#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1256 ecdsa_verify_wrap, /* Compatible key structures */
1257#else
1258 NULL,
1259#endif
1260#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1261 ecdsa_sign_wrap, /* Compatible key structures */
1262#else
1263 NULL,
1264#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001265#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001266 eckey_verify_rs_wrap,
1267 eckey_sign_rs_wrap,
1268#endif
1269 NULL,
1270 NULL,
1271 eckey_check_pair,
Valerio Settib5361262023-05-18 18:51:58 +02001272#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1273 NULL,
1274 NULL,
1275#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001276 eckey_alloc_wrap,
1277 eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001278#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001279#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1280 eckey_rs_alloc,
1281 eckey_rs_free,
1282#endif
1283 eckey_debug,
1284};
1285
1286/*
1287 * EC key restricted to ECDH
1288 */
1289static int eckeydh_can_do(mbedtls_pk_type_t type)
1290{
1291 return type == MBEDTLS_PK_ECKEY ||
1292 type == MBEDTLS_PK_ECKEY_DH;
1293}
1294
1295const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1296 MBEDTLS_PK_ECKEY_DH,
1297 "EC_DH",
1298 eckey_get_bitlen, /* Same underlying key structure */
1299 eckeydh_can_do,
1300 NULL,
1301 NULL,
1302#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1303 NULL,
1304 NULL,
1305#endif
1306 NULL,
1307 NULL,
1308 eckey_check_pair,
Valerio Settib5361262023-05-18 18:51:58 +02001309#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1310 NULL,
1311 NULL,
1312#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1313 eckey_alloc_wrap, /* Same underlying key structure */
1314 eckey_free_wrap, /* Same underlying key structure */
1315#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001316#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1317 NULL,
1318 NULL,
1319#endif
1320 eckey_debug, /* Same underlying key structure */
1321};
Valerio Setti0d2980f2023-04-05 18:17:13 +02001322#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001323
1324#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1325static int ecdsa_can_do(mbedtls_pk_type_t type)
1326{
1327 return type == MBEDTLS_PK_ECDSA;
1328}
1329
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001330#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001331static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 const unsigned char *hash, size_t hash_len,
1333 const unsigned char *sig, size_t sig_len,
1334 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001335{
Janos Follath24eed8d2019-11-22 13:21:35 +00001336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001337 ((void) md_alg);
1338
1339 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001340 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 hash, hash_len, sig, sig_len,
1342 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001343
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1345 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1346 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001349}
1350
valerio38992cb2023-04-20 09:56:30 +02001351static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 const unsigned char *hash, size_t hash_len,
1353 unsigned char *sig, size_t sig_size, size_t *sig_len,
1354 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1355 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001356{
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001358 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1360 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001361
1362}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001365{
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 if (ctx != NULL) {
1369 mbedtls_ecdsa_restart_init(ctx);
1370 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001371
Gilles Peskine449bd832023-01-11 14:50:10 +01001372 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001373}
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001376{
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 mbedtls_ecdsa_restart_free(ctx);
1378 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001379}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001380#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1383 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001384 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001385 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001386 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001387#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1388 ecdsa_verify_wrap, /* Compatible key structures */
1389#else
1390 NULL,
1391#endif
1392#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1393 ecdsa_sign_wrap, /* Compatible key structures */
1394#else
1395 NULL,
1396#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001397#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001398 ecdsa_verify_rs_wrap,
1399 ecdsa_sign_rs_wrap,
1400#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001401 NULL,
1402 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001403 eckey_check_pair, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001404#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1405 NULL,
1406 NULL,
1407#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti24138d92023-01-27 14:24:09 +01001408 eckey_alloc_wrap, /* Compatible key structures */
1409 eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001410#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001411#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001412 ecdsa_rs_alloc,
1413 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001414#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001415 eckey_debug, /* Compatible key structures */
1416};
Valerio Setti7ca13182023-01-27 13:22:42 +01001417#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001420/*
1421 * Support for alternative RSA-private implementations
1422 */
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001425{
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001427}
1428
valerio38992cb2023-04-20 09:56:30 +02001429static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001430{
valerio38992cb2023-04-20 09:56:30 +02001431 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001434}
1435
valerio38992cb2023-04-20 09:56:30 +02001436static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 const unsigned char *hash, size_t hash_len,
1438 unsigned char *sig, size_t sig_size, size_t *sig_len,
1439 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001440{
valerio38992cb2023-04-20 09:56:30 +02001441 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (UINT_MAX < hash_len) {
1444 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1445 }
Andres AG72849872017-01-19 11:24:33 +00001446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1448 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1449 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1450 }
1451 if (*sig_len > sig_size) {
1452 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1453 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1456 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001457}
1458
valerio38992cb2023-04-20 09:56:30 +02001459static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 const unsigned char *input, size_t ilen,
1461 unsigned char *output, size_t *olen, size_t osize,
1462 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001463{
valerio38992cb2023-04-20 09:56:30 +02001464 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001465
1466 ((void) f_rng);
1467 ((void) p_rng);
1468
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1470 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1471 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return rsa_alt->decrypt_func(rsa_alt->key,
1474 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001475}
1476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001477#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001478static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 int (*f_rng)(void *, unsigned char *, size_t),
1480 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001481{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001482 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001483 unsigned char hash[32];
1484 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001486
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1488 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001489 }
1490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 memset(hash, 0x2a, sizeof(hash));
1492
valerio38992cb2023-04-20 09:56:30 +02001493 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001494 hash, sizeof(hash),
1495 sig, sizeof(sig), &sig_len,
1496 f_rng, p_rng)) != 0) {
1497 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001498 }
1499
valerio38992cb2023-04-20 09:56:30 +02001500 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 hash, sizeof(hash), sig, sig_len) != 0) {
1502 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1503 }
1504
1505 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001507#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001510{
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if (ctx != NULL) {
1514 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1515 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001518}
1519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001521{
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1523 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001524}
1525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001526const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1527 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001528 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001529 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001530 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001531 NULL,
1532 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001533#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001534 NULL,
1535 NULL,
1536#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001537 rsa_alt_decrypt_wrap,
1538 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001540 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001541#else
1542 NULL,
1543#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001544 rsa_alt_alloc_wrap,
1545 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001546#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001547 NULL,
1548 NULL,
1549#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001550 NULL,
1551};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001554
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001555#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +02001556static size_t pk_opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001557{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001558 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001559 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001560
Valerio Setti4f387ef2023-05-02 14:15:59 +02001561 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001562 return 0;
1563 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 bits = psa_get_key_bits(&attributes);
1566 psa_reset_key_attributes(&attributes);
1567 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001568}
1569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001571{
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 return type == MBEDTLS_PK_ECKEY ||
1573 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001574}
1575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001577{
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 return type == MBEDTLS_PK_RSA ||
1579 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001580}
1581
valerio38992cb2023-04-20 09:56:30 +02001582static int pk_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 const unsigned char *hash, size_t hash_len,
1584 unsigned char *sig, size_t sig_size, size_t *sig_len,
1585 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001586{
Valerio Settiab363d92023-01-26 14:31:54 +01001587#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001588 ((void) pk);
John Durkopf35069a2020-08-17 22:05:14 -07001589 ((void) md_alg);
1590 ((void) hash);
1591 ((void) hash_len);
1592 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001593 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001594 ((void) sig_len);
1595 ((void) f_rng);
1596 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001598#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001599 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001600 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001601 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001602 psa_status_t status;
1603
1604 /* PSA has its own RNG */
1605 (void) f_rng;
1606 (void) p_rng;
1607
Valerio Setti4f387ef2023-05-02 14:15:59 +02001608 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001610 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001612
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 type = psa_get_key_type(&attributes);
1614 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001615
Valerio Settiab363d92023-01-26 14:31:54 +01001616#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001617 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1618 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1619 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001620#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001621#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001622 if (PSA_KEY_TYPE_IS_RSA(type)) {
1623 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1624 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001625#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001627
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001628 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001629 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 sig, sig_size, sig_len);
1631 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001632#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001634 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001636#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001637#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001639 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001641#endif /* MBEDTLS_RSA_C */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001642 return PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001643 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001644
Valerio Settiab363d92023-01-26 14:31:54 +01001645#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001647 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1649 }
Valerio Settiab363d92023-01-26 14:31:54 +01001650#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001651
1652 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001653#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001654}
1655
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001656const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001657 MBEDTLS_PK_OPAQUE,
1658 "Opaque",
1659 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001660 pk_opaque_ecdsa_can_do,
1661 NULL, /* verify - will be done later */
1662 pk_opaque_sign_wrap,
1663#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1664 NULL, /* restartable verify - not relevant */
1665 NULL, /* restartable sign - not relevant */
1666#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001667 NULL, /* decrypt - not relevant */
1668 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001669 NULL, /* check_pair - could be done later or left NULL */
Valerio Settie00954d2023-04-28 15:24:32 +02001670 NULL, /* alloc - no need to allocate new data dynamically */
1671 NULL, /* free - as for the alloc, there is no data to free */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001672#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1673 NULL, /* restart alloc - not relevant */
1674 NULL, /* restart free - not relevant */
1675#endif
1676 NULL, /* debug - could be done later, or even left NULL */
1677};
1678
Neil Armstrong30beca32022-05-03 15:42:13 +02001679#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
valerio38992cb2023-04-20 09:56:30 +02001680static int pk_opaque_rsa_decrypt(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 const unsigned char *input, size_t ilen,
1682 unsigned char *output, size_t *olen, size_t osize,
1683 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001684{
Neil Armstrong10828182022-04-22 15:02:27 +02001685 psa_status_t status;
1686
1687 /* PSA has its own RNG */
1688 (void) f_rng;
1689 (void) p_rng;
1690
Valerio Setti4f387ef2023-05-02 14:15:59 +02001691 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 input, ilen,
1693 NULL, 0,
1694 output, osize, olen);
1695 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001696 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001697 }
1698
1699 return 0;
1700}
Neil Armstrong30beca32022-05-03 15:42:13 +02001701#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001702
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001703const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1704 MBEDTLS_PK_OPAQUE,
1705 "Opaque",
1706 pk_opaque_get_bitlen,
1707 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001708 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001709 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001710#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1711 NULL, /* restartable verify - not relevant */
1712 NULL, /* restartable sign - not relevant */
1713#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001714#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001715 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001716#else
1717 NULL, /* decrypt - not available */
1718#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001719 NULL, /* encrypt - will be done later */
1720 NULL, /* check_pair - could be done later or left NULL */
Valerio Settie00954d2023-04-28 15:24:32 +02001721 NULL, /* alloc - no need to allocate new data dynamically */
1722 NULL, /* free - as for the alloc, there is no data to free */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001723#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1724 NULL, /* restart alloc - not relevant */
1725 NULL, /* restart free - not relevant */
1726#endif
1727 NULL, /* debug - could be done later, or even left NULL */
1728};
1729
1730#endif /* MBEDTLS_USE_PSA_CRYPTO */
1731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732#endif /* MBEDTLS_PK_C */