blob: 32d697ac03a9fb8fe6e0c9bbe2891173cbd6d340 [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"
46#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
47#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
48 psa_to_pk_rsa_errors, \
49 psa_pk_status_to_mbedtls)
50#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
51 psa_to_pk_ecdsa_errors, \
52 psa_pk_status_to_mbedtls)
53#endif
54
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010055#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040056#include "psa/crypto.h"
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020057#include "hash_info.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020058
Valerio Setti80d07982023-02-08 13:49:17 +010059#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020060#include "mbedtls/asn1write.h"
61#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010062#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020063#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010064
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000065#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020066
Andres AG72849872017-01-19 11:24:33 +000067#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010068#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020069#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020070
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050071#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yub02ee182022-03-16 10:30:41 +080072#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010073int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010074{
Gilles Peskine449bd832023-01-11 14:50:10 +010075 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010076 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010077 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010078 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010080 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010082 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010084 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010086 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010087 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010088 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010090 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010091 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010092 case PSA_ERROR_COMMUNICATION_FAILURE:
93 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010095 case PSA_ERROR_DATA_CORRUPT:
96 case PSA_ERROR_DATA_INVALID:
97 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010099 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100101 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +0100103 }
104}
105
Neil Armstrong30beca32022-05-03 15:42:13 +0200106#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
107 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +0100108int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +0100109{
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +0100111 case PSA_ERROR_NOT_PERMITTED:
112 case PSA_ERROR_INVALID_ARGUMENT:
113 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +0100115 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +0100117 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100119 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100121 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Andrzej Kurekba241382022-12-27 09:17:33 -0500123 case PSA_SUCCESS:
124 return 0;
125 case PSA_ERROR_NOT_SUPPORTED:
126 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
127 case PSA_ERROR_INSUFFICIENT_MEMORY:
128 return MBEDTLS_ERR_PK_ALLOC_FAILED;
129 case PSA_ERROR_BAD_STATE:
130 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
131 case PSA_ERROR_COMMUNICATION_FAILURE:
132 case PSA_ERROR_HARDWARE_FAILURE:
133 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
134 case PSA_ERROR_DATA_CORRUPT:
135 case PSA_ERROR_DATA_INVALID:
136 case PSA_ERROR_STORAGE_FAILURE:
137 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
138 case PSA_ERROR_CORRUPTION_DETECTED:
139 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100140 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500141 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +0100142 }
143}
Neil Armstrong30beca32022-05-03 15:42:13 +0200144#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800145#endif /* MBEDTLS_PSA_CRYPTO_C */
146
147#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu07869e82022-03-16 16:40:50 +0800148#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100149int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800150{
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800152 case PSA_ERROR_NOT_PERMITTED:
153 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800155 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800157 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800159 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800161 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Andrzej Kurekba241382022-12-27 09:17:33 -0500163 case PSA_SUCCESS:
164 return 0;
165 case PSA_ERROR_NOT_SUPPORTED:
166 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
167 case PSA_ERROR_INSUFFICIENT_MEMORY:
168 return MBEDTLS_ERR_PK_ALLOC_FAILED;
169 case PSA_ERROR_BAD_STATE:
170 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
171 case PSA_ERROR_COMMUNICATION_FAILURE:
172 case PSA_ERROR_HARDWARE_FAILURE:
173 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
174 case PSA_ERROR_DATA_CORRUPT:
175 case PSA_ERROR_DATA_INVALID:
176 case PSA_ERROR_STORAGE_FAILURE:
177 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
178 case PSA_ERROR_CORRUPTION_DETECTED:
179 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu07869e82022-03-16 16:40:50 +0800180 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500181 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Jerry Yu07869e82022-03-16 16:40:50 +0800182 }
183}
Jerry Yu75339822022-03-23 12:06:31 +0800184#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu75339822022-03-23 12:06:31 +0800185#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500186#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Neil Armstrong19915c22022-03-01 15:21:02 +0100187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100189static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200190{
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return type == MBEDTLS_PK_RSA ||
192 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200193}
194
valerio38992cb2023-04-20 09:56:30 +0200195static size_t rsa_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200196{
valerio38992cb2023-04-20 09:56:30 +0200197 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200199}
200
Neil Armstrong52f41f82022-02-22 15:30:24 +0100201#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200202static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 const unsigned char *hash, size_t hash_len,
204 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200205{
valerio38992cb2023-04-20 09:56:30 +0200206 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100207 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
208 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
209 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
210 psa_status_t status;
211 mbedtls_pk_context key;
212 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100213 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100214 psa_algorithm_t psa_alg_md =
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
216 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
219 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
220 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 if (sig_len < rsa_len) {
223 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
224 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100225
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100226 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100227 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100228 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200229 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
231 if (key_len <= 0) {
232 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
233 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
236 psa_set_key_algorithm(&attributes, psa_alg_md);
237 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 status = psa_import_key(&attributes,
240 buf + sizeof(buf) - key_len, key_len,
241 &key_id);
242 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500243 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100244 goto cleanup;
245 }
246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
248 sig, sig_len);
249 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500250 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100251 goto cleanup;
252 }
253 ret = 0;
254
255cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 status = psa_destroy_key(key_id);
257 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500258 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100262}
263#else
valerio38992cb2023-04-20 09:56:30 +0200264static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 const unsigned char *hash, size_t hash_len,
266 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100267{
Janos Follath24eed8d2019-11-22 13:21:35 +0000268 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valerio38992cb2023-04-20 09:56:30 +0200269 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
273 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
274 }
Andres AG72849872017-01-19 11:24:33 +0000275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (sig_len < rsa_len) {
277 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
278 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
281 (unsigned int) hash_len,
282 hash, sig)) != 0) {
283 return ret;
284 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200285
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200286 /* The buffer contains a valid signature followed by extra data.
287 * We have a special error code for that so that so that callers can
288 * use mbedtls_pk_verify() to check "Does the buffer start with a
289 * valid signature?" and not just "Does the buffer contain a valid
290 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (sig_len > rsa_len) {
292 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
293 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200296}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100297#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200298
Jerry Yub02ee182022-03-16 10:30:41 +0800299#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100300int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
301 mbedtls_rsa_context *rsa_ctx,
302 const unsigned char *hash, size_t hash_len,
303 unsigned char *sig, size_t sig_size,
304 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100305{
Neil Armstrong98545682022-02-22 16:12:51 +0100306 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
307 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
308 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
309 psa_status_t status;
310 mbedtls_pk_context key;
311 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100312 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100313 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
316 if (sig_size < *sig_len) {
317 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
318 }
Neil Armstrong98545682022-02-22 16:12:51 +0100319
Neil Armstronge4f28682022-02-24 15:41:39 +0100320 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100321 * re-construct one to make it happy */
322 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800323 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
325 if (key_len <= 0) {
326 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
327 }
328 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
329 psa_set_key_algorithm(&attributes, alg);
330 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 status = psa_import_key(&attributes,
333 buf + sizeof(buf) - key_len, key_len,
334 &key_id);
335 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500336 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100337 goto cleanup;
338 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 status = psa_sign_hash(key_id, alg, hash, hash_len,
340 sig, sig_size, sig_len);
341 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500342 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100343 goto cleanup;
344 }
345
346 ret = 0;
347
348cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 status = psa_destroy_key(key_id);
350 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500351 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 }
353 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100354}
Jerry Yu406cf272022-03-22 11:33:42 +0800355#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800356
357#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200358static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 const unsigned char *hash, size_t hash_len,
360 unsigned char *sig, size_t sig_size, size_t *sig_len,
361 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800362{
Jerry Yu1d172a32022-03-12 19:12:05 +0800363 ((void) f_rng);
364 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800365
Jerry Yubd1b3272022-03-24 13:05:20 +0800366 psa_algorithm_t psa_md_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
368 if (psa_md_alg == 0) {
369 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
370 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
373 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200374 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800376}
Neil Armstrong98545682022-02-22 16:12:51 +0100377#else
valerio38992cb2023-04-20 09:56:30 +0200378static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 const unsigned char *hash, size_t hash_len,
380 unsigned char *sig, size_t sig_size, size_t *sig_len,
381 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200382{
valerio38992cb2023-04-20 09:56:30 +0200383 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
386 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
387 }
Andres AG72849872017-01-19 11:24:33 +0000388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 *sig_len = mbedtls_rsa_get_len(rsa);
390 if (sig_size < *sig_len) {
391 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
392 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
395 md_alg, (unsigned int) hash_len,
396 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200397}
Neil Armstrong98545682022-02-22 16:12:51 +0100398#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200399
Neil Armstrong18f43c72022-02-09 15:32:45 +0100400#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200401static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 const unsigned char *input, size_t ilen,
403 unsigned char *output, size_t *olen, size_t osize,
404 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100405{
valerio38992cb2023-04-20 09:56:30 +0200406 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100407 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
409 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
410 psa_status_t status;
411 mbedtls_pk_context key;
412 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100413 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100414
415 ((void) f_rng);
416 ((void) p_rng);
417
418#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
420 return MBEDTLS_ERR_RSA_INVALID_PADDING;
421 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100422#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (ilen != mbedtls_rsa_get_len(rsa)) {
425 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
426 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100427
428 /* mbedtls_pk_write_key_der() expects a full PK context;
429 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100430 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200431 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
433 if (key_len <= 0) {
434 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
435 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100436
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
438 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
439 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 status = psa_import_key(&attributes,
442 buf + sizeof(buf) - key_len, key_len,
443 &key_id);
444 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500445 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100446 goto cleanup;
447 }
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
450 input, ilen,
451 NULL, 0,
452 output, osize, olen);
453 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500454 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100455 goto cleanup;
456 }
457
458 ret = 0;
459
460cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 mbedtls_platform_zeroize(buf, sizeof(buf));
462 status = psa_destroy_key(key_id);
463 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500464 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100468}
469#else
valerio38992cb2023-04-20 09:56:30 +0200470static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 const unsigned char *input, size_t ilen,
472 unsigned char *output, size_t *olen, size_t osize,
473 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200474{
valerio38992cb2023-04-20 09:56:30 +0200475 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100476
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (ilen != mbedtls_rsa_get_len(rsa)) {
478 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
479 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
482 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200483}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100484#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200485
Neil Armstrong96a16a42022-02-10 10:40:11 +0100486#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200487static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 const unsigned char *input, size_t ilen,
489 unsigned char *output, size_t *olen, size_t osize,
490 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100491{
valerio38992cb2023-04-20 09:56:30 +0200492 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
494 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
495 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
496 psa_status_t status;
497 mbedtls_pk_context key;
498 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100499 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100500
501 ((void) f_rng);
502 ((void) p_rng);
503
504#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
506 return MBEDTLS_ERR_RSA_INVALID_PADDING;
507 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100508#endif
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (mbedtls_rsa_get_len(rsa) > osize) {
511 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
512 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100513
Neil Armstrongac014ca2022-02-24 15:27:54 +0100514 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100515 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100516 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200517 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
519 if (key_len <= 0) {
520 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
521 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
524 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
525 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 status = psa_import_key(&attributes,
528 buf + sizeof(buf) - key_len, key_len,
529 &key_id);
530 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500531 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100532 goto cleanup;
533 }
534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
536 input, ilen,
537 NULL, 0,
538 output, osize, olen);
539 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500540 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100541 goto cleanup;
542 }
543
544 ret = 0;
545
546cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 status = psa_destroy_key(key_id);
548 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500549 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100553}
554#else
valerio38992cb2023-04-20 09:56:30 +0200555static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 const unsigned char *input, size_t ilen,
557 unsigned char *output, size_t *olen, size_t osize,
558 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200559{
valerio38992cb2023-04-20 09:56:30 +0200560 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (*olen > osize) {
564 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
565 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
568 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200569}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100570#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200571
valerio38992cb2023-04-20 09:56:30 +0200572static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 int (*f_rng)(void *, unsigned char *, size_t),
574 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100575{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200576 (void) f_rng;
577 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200578 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
579 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100580}
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200583{
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (ctx != NULL) {
587 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
588 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200591}
592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200594{
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
596 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200597}
598
valerio38992cb2023-04-20 09:56:30 +0200599static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200600{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200601#if defined(MBEDTLS_RSA_ALT)
602 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200603 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200604 (void) items;
605#else
valerio38992cb2023-04-20 09:56:30 +0200606 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200609 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200610 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200611
612 items++;
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200615 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200616 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200617#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200618}
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620const mbedtls_pk_info_t mbedtls_rsa_info = {
621 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200622 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200623 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200624 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200625 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200626 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200627#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200628 NULL,
629 NULL,
630#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200631 rsa_decrypt_wrap,
632 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100633 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200634 rsa_alloc_wrap,
635 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200636#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200637 NULL,
638 NULL,
639#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200640 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200641};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200643
Valerio Setti0d2980f2023-04-05 18:17:13 +0200644#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200645/*
646 * Generic EC key
647 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200649{
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 return type == MBEDTLS_PK_ECKEY ||
651 type == MBEDTLS_PK_ECKEY_DH ||
652 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200653}
654
valerio38992cb2023-04-20 09:56:30 +0200655static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200656{
Valerio Settia1b8af62023-05-17 15:34:57 +0200657#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
658 return pk->ec_bits;
659#else
valerio38992cb2023-04-20 09:56:30 +0200660 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
661 return ecp->grp.pbits;
Valerio Settia1b8af62023-05-17 15:34:57 +0200662#endif
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200663}
664
Valerio Setti1cdddac2023-02-02 13:55:57 +0100665#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400666#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400667/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500668 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
669 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500670 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
672 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500673{
Janos Follath24eed8d2019-11-22 13:21:35 +0000674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500675 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
678 MBEDTLS_ASN1_INTEGER)) != 0) {
679 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500680 }
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 while (unpadded_len > 0 && **from == 0x00) {
683 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500684 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500685 }
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (unpadded_len > to_len || unpadded_len == 0) {
688 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
689 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500690
Andrzej Kurek9241d182018-11-20 05:04:35 -0500691 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 memset(to, 0x00, padding_len);
693 memcpy(to + padding_len, *from, unpadded_len);
694 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500697}
698
699/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400700 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500701 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500702 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400703 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
705 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400706{
Janos Follath24eed8d2019-11-22 13:21:35 +0000707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500708 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
711 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
712 return ret;
713 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400714
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500715 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
717 return ret;
718 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500719 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
721 return ret;
722 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400725}
726
valerio38992cb2023-04-20 09:56:30 +0200727static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
728 mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 const unsigned char *hash, size_t hash_len,
730 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400731{
Janos Follath24eed8d2019-11-22 13:21:35 +0000732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200733 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100734 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200735 psa_status_t status;
Valerio Settia1b8af62023-05-17 15:34:57 +0200736 unsigned char *p;
737 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
738 size_t signature_len;
739 ((void) md_alg);
740#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
741 unsigned char buf[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
742 psa_ecc_family_t curve = pk->ec_family;
743 size_t curve_bits = pk->ec_bits;
744#else
745 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200746 size_t key_len;
Valerio Setti5c032b52023-02-02 15:10:32 +0100747 /* This buffer will initially contain the public key and then the signature
748 * but at different points in time. For all curves except secp224k1, which
749 * is not currently supported in PSA, the public key is one byte longer
750 * (header byte + 2 numbers, while the signature is only 2 numbers),
751 * so use that as the buffer size. */
Valerio Setti1337a4f2023-01-30 15:54:55 +0100752 unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100753 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100754 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Valerio Settia1b8af62023-05-17 15:34:57 +0200756#endif
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (curve == 0) {
759 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
760 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
763 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
764 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500765
Valerio Settia1b8af62023-05-17 15:34:57 +0200766#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
767 status = psa_import_key(&attributes,
768 pk->pub_raw, pk->pub_raw_len,
769 &key_id);
770#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200771 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
772 MBEDTLS_ECP_PF_UNCOMPRESSED,
773 &key_len, buf, sizeof(buf));
774 if (ret != 0) {
775 goto cleanup;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500776 }
777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 status = psa_import_key(&attributes,
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200779 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 &key_id);
Valerio Settia1b8af62023-05-17 15:34:57 +0200781#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500783 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400784 goto cleanup;
785 }
786
Valerio Settia1b8af62023-05-17 15:34:57 +0200787 signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
788 if (signature_len > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500789 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
790 goto cleanup;
791 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 p = (unsigned char *) sig;
Valerio Settia1b8af62023-05-17 15:34:57 +0200794 /* extract_ecdsa_sig's last parameter is the size
795 * of each integer to be parse, so it's actually half
796 * the size of the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
Valerio Settia1b8af62023-05-17 15:34:57 +0200798 signature_len/2)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500799 goto cleanup;
800 }
801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 status = psa_verify_hash(key_id, psa_sig_md,
803 hash, hash_len,
Valerio Settia1b8af62023-05-17 15:34:57 +0200804 buf, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500806 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400808 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500811 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
812 goto cleanup;
813 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400814 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400815
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500816cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 status = psa_destroy_key(key_id);
818 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500819 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100821
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400823}
824#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200825static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 const unsigned char *hash, size_t hash_len,
827 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200828{
Janos Follath24eed8d2019-11-22 13:21:35 +0000829 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200830 ((void) md_alg);
831
valerio38992cb2023-04-20 09:56:30 +0200832 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
836 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
837 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200840}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400841#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100842#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200843
Valerio Setti1cdddac2023-02-02 13:55:57 +0100844#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100845#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100846/*
847 * Simultaneously convert and move raw MPI from the beginning of a buffer
848 * to an ASN.1 MPI at the end of the buffer.
849 * See also mbedtls_asn1_write_mpi().
850 *
851 * p: pointer to the end of the output buffer
852 * start: start of the output buffer, and also of the mpi to write at the end
853 * n_len: length of the mpi to read from start
854 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
856 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100857{
858 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
859 size_t len = 0;
860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if ((size_t) (*p - start) < n_len) {
862 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
863 }
Neil Armstrong15021652022-03-01 10:14:17 +0100864
865 len = n_len;
866 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100868
869 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
870 * Neither r nor s should be 0, but as a failsafe measure, still detect
871 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100873 ++(*p);
874 --len;
875 }
876
877 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (len == 0) {
879 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
880 }
Neil Armstrong15021652022-03-01 10:14:17 +0100881
882 /* if the msb is 1, ASN.1 requires that we prepend a 0.
883 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 if (**p & 0x80) {
885 if (*p - start < 1) {
886 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
887 }
Neil Armstrong15021652022-03-01 10:14:17 +0100888
889 *--(*p) = 0x00;
890 len += 1;
891 }
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
894 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
895 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100898}
899
900/* Transcode signature from PSA format to ASN.1 sequence.
901 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
902 * MPIs, and in-place.
903 *
904 * [in/out] sig: the signature pre- and post-transcoding
905 * [in/out] sig_len: signature length pre- and post-transcoding
906 * [int] buf_len: the available size the in/out buffer
907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
909 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100910{
911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
912 size_t len = 0;
913 const size_t rs_len = *sig_len / 2;
914 unsigned char *p = sig + buf_len;
915
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
917 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
920 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
921 MBEDTLS_ASN1_CONSTRUCTED |
922 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100925 *sig_len = len;
926
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100928}
Neil Armstronge9606902022-02-09 14:23:00 +0100929
valerio38992cb2023-04-20 09:56:30 +0200930static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 const unsigned char *hash, size_t hash_len,
932 unsigned char *sig, size_t sig_size, size_t *sig_len,
933 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100934{
valerio38992cb2023-04-20 09:56:30 +0200935 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
Neil Armstronge9606902022-02-09 14:23:00 +0100936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
938 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
939 psa_status_t status;
Valerio Setti1337a4f2023-01-30 15:54:55 +0100940 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100941#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Valerio Setti5bc52242023-01-30 15:48:28 +0100942 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100943 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100944#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200945 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100946 PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100947#endif
Neil Armstronge9606902022-02-09 14:23:00 +0100948 size_t curve_bits;
949 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200951 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100952
953 /* PSA has its own RNG */
954 ((void) f_rng);
955 ((void) p_rng);
956
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 if (curve == 0) {
958 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
959 }
Neil Armstronge9606902022-02-09 14:23:00 +0100960
Gilles Peskine13caa942022-10-04 22:59:26 +0200961 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100962 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200964 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100966 goto cleanup;
967 }
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
970 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
971 psa_set_key_algorithm(&attributes, psa_sig_md);
972
973 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200974 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 &key_id);
976 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500977 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100979 }
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
982 sig, sig_size, sig_len);
983 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500984 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 goto cleanup;
986 }
987
988 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100989
990cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 mbedtls_platform_zeroize(buf, sizeof(buf));
992 status = psa_destroy_key(key_id);
993 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500994 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100998}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100999#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +02001000static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 const unsigned char *hash, size_t hash_len,
1002 unsigned char *sig, size_t sig_size, size_t *sig_len,
1003 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001004{
valerio38992cb2023-04-20 09:56:30 +02001005 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 md_alg, hash, hash_len,
1007 sig, sig_size, sig_len,
1008 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001009}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001010#endif /* MBEDTLS_USE_PSA_CRYPTO */
1011#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001012
Valerio Setti80d07982023-02-08 13:49:17 +01001013#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001014/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001015static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001016 const unsigned char *hash, size_t hash_len,
1017 const unsigned char *sig, size_t sig_len,
1018 void *rs_ctx);
1019
valerio38992cb2023-04-20 09:56:30 +02001020static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001021 const unsigned char *hash, size_t hash_len,
1022 unsigned char *sig, size_t sig_size, size_t *sig_len,
1023 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1024 void *rs_ctx);
1025
1026/*
1027 * Restart context for ECDSA operations with ECKEY context
1028 *
1029 * We need to store an actual ECDSA context, as we need to pass the same to
1030 * the underlying ecdsa function, so we can't create it on the fly every time.
1031 */
1032typedef struct {
1033 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1034 mbedtls_ecdsa_context ecdsa_ctx;
1035} eckey_restart_ctx;
1036
1037static void *eckey_rs_alloc(void)
1038{
1039 eckey_restart_ctx *rs_ctx;
1040
1041 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1042
1043 if (ctx != NULL) {
1044 rs_ctx = ctx;
1045 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1046 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1047 }
1048
1049 return ctx;
1050}
1051
1052static void eckey_rs_free(void *ctx)
1053{
1054 eckey_restart_ctx *rs_ctx;
1055
1056 if (ctx == NULL) {
1057 return;
1058 }
1059
1060 rs_ctx = ctx;
1061 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1062 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1063
1064 mbedtls_free(ctx);
1065}
1066
valerio38992cb2023-04-20 09:56:30 +02001067static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001068 const unsigned char *hash, size_t hash_len,
1069 const unsigned char *sig, size_t sig_len,
1070 void *rs_ctx)
1071{
1072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1073 eckey_restart_ctx *rs = rs_ctx;
1074
1075 /* Should never happen */
1076 if (rs == NULL) {
1077 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1078 }
1079
1080 /* set up our own sub-context if needed (that is, on first run) */
1081 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001082 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001083 }
1084
valerio38992cb2023-04-20 09:56:30 +02001085 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001086 md_alg, hash, hash_len,
1087 sig, sig_len, &rs->ecdsa_rs));
1088
1089cleanup:
1090 return ret;
1091}
1092
valerio38992cb2023-04-20 09:56:30 +02001093static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001094 const unsigned char *hash, size_t hash_len,
1095 unsigned char *sig, size_t sig_size, size_t *sig_len,
1096 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1097 void *rs_ctx)
1098{
1099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1100 eckey_restart_ctx *rs = rs_ctx;
1101
1102 /* Should never happen */
1103 if (rs == NULL) {
1104 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1105 }
1106
1107 /* set up our own sub-context if needed (that is, on first run) */
1108 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001109 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001110 }
1111
valerio38992cb2023-04-20 09:56:30 +02001112 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001113 hash, hash_len, sig, sig_size, sig_len,
1114 f_rng, p_rng, &rs->ecdsa_rs));
1115
1116cleanup:
1117 return ret;
1118}
Valerio Setti80d07982023-02-08 13:49:17 +01001119#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001120
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001121#if defined(MBEDTLS_USE_PSA_CRYPTO)
1122/*
1123 * Alternative function used to verify that the EC private/public key pair
1124 * is valid using PSA functions instead of ECP ones.
1125 * The flow is:
1126 * - import the private key "prv" to PSA and export its public part
1127 * - write the raw content of public key "pub" to a local buffer
1128 * - compare the two buffers
1129 */
valerio38992cb2023-04-20 09:56:30 +02001130static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001131{
Valerio Setti1df94f82023-04-07 08:59:24 +02001132 psa_status_t status, destruction_status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001133 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001135 /* We are using MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH for the size of this
1136 * buffer because it will be used to hold the private key at first and
1137 * then its public part (but not at the same time). */
1138 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001139 size_t prv_key_len;
Valerio Settia1b8af62023-05-17 15:34:57 +02001140 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1141#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1142 const psa_ecc_family_t curve = prv->ec_family;
1143 const size_t curve_bits = PSA_BITS_TO_BYTES(prv->ec_bits);
1144#else /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001145 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1146 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001147 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001148 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001149 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settif2866642023-04-06 16:49:54 +02001150 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Settia1b8af62023-05-17 15:34:57 +02001151#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001152
1153 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1154 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1155
Valerio Settia1b8af62023-05-17 15:34:57 +02001156 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1157 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001158 if (ret != 0) {
1159 return ret;
1160 }
1161
1162 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti1df94f82023-04-07 08:59:24 +02001163 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1164 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001165 return ret;
1166 }
1167
1168 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
1169
Valerio Setti1df94f82023-04-07 08:59:24 +02001170 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1171 &prv_key_len);
1172 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1173 destruction_status = psa_destroy_key(key_id);
1174 if (ret != 0) {
1175 return ret;
1176 } else if (destruction_status != PSA_SUCCESS) {
1177 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001178 }
1179
Valerio Settia1b8af62023-05-17 15:34:57 +02001180#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1181 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1182 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1183 }
1184#else
1185 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1186 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001187 MBEDTLS_ECP_PF_UNCOMPRESSED,
1188 &pub_key_len, pub_key_buf,
1189 sizeof(pub_key_buf));
1190 if (ret != 0) {
1191 return ret;
1192 }
1193
1194 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1195 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1196 }
Valerio Settia1b8af62023-05-17 15:34:57 +02001197#endif /* !MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001198
1199 return 0;
1200}
1201#endif /* MBEDTLS_USE_PSA_CRYPTO */
1202
valerio38992cb2023-04-20 09:56:30 +02001203static int eckey_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001204 int (*f_rng)(void *, unsigned char *, size_t),
1205 void *p_rng)
1206{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001207#if defined(MBEDTLS_USE_PSA_CRYPTO)
1208 (void) f_rng;
1209 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001210 return eckey_check_pair_psa(pub, prv);
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001211#elif defined(MBEDTLS_ECP_C)
valerio38992cb2023-04-20 09:56:30 +02001212 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1213 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001214 f_rng, p_rng);
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001215#else
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001216 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001217#endif
Valerio Setti1cdddac2023-02-02 13:55:57 +01001218}
1219
1220static void *eckey_alloc_wrap(void)
1221{
1222 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1223
1224 if (ctx != NULL) {
1225 mbedtls_ecp_keypair_init(ctx);
1226 }
1227
1228 return ctx;
1229}
1230
1231static void eckey_free_wrap(void *ctx)
1232{
1233 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1234 mbedtls_free(ctx);
1235}
1236
valerio38992cb2023-04-20 09:56:30 +02001237static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001238{
Valerio Settia1b8af62023-05-17 15:34:57 +02001239#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1240 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1241 items->name = "eckey.Q";
1242 items->value = pk;
1243#else
valerio38992cb2023-04-20 09:56:30 +02001244 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001245 items->type = MBEDTLS_PK_DEBUG_ECP;
1246 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001247 items->value = &(ecp->Q);
Valerio Settia1b8af62023-05-17 15:34:57 +02001248#endif
Valerio Setti1cdddac2023-02-02 13:55:57 +01001249}
1250
1251const mbedtls_pk_info_t mbedtls_eckey_info = {
1252 MBEDTLS_PK_ECKEY,
1253 "EC",
1254 eckey_get_bitlen,
1255 eckey_can_do,
1256#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1257 ecdsa_verify_wrap, /* Compatible key structures */
1258#else
1259 NULL,
1260#endif
1261#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1262 ecdsa_sign_wrap, /* Compatible key structures */
1263#else
1264 NULL,
1265#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001266#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001267 eckey_verify_rs_wrap,
1268 eckey_sign_rs_wrap,
1269#endif
1270 NULL,
1271 NULL,
1272 eckey_check_pair,
1273 eckey_alloc_wrap,
1274 eckey_free_wrap,
1275#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1276 eckey_rs_alloc,
1277 eckey_rs_free,
1278#endif
1279 eckey_debug,
1280};
1281
1282/*
1283 * EC key restricted to ECDH
1284 */
1285static int eckeydh_can_do(mbedtls_pk_type_t type)
1286{
1287 return type == MBEDTLS_PK_ECKEY ||
1288 type == MBEDTLS_PK_ECKEY_DH;
1289}
1290
1291const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1292 MBEDTLS_PK_ECKEY_DH,
1293 "EC_DH",
1294 eckey_get_bitlen, /* Same underlying key structure */
1295 eckeydh_can_do,
1296 NULL,
1297 NULL,
1298#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1299 NULL,
1300 NULL,
1301#endif
1302 NULL,
1303 NULL,
1304 eckey_check_pair,
1305 eckey_alloc_wrap, /* Same underlying key structure */
1306 eckey_free_wrap, /* Same underlying key structure */
1307#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1308 NULL,
1309 NULL,
1310#endif
1311 eckey_debug, /* Same underlying key structure */
1312};
Valerio Setti0d2980f2023-04-05 18:17:13 +02001313#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001314
1315#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1316static int ecdsa_can_do(mbedtls_pk_type_t type)
1317{
1318 return type == MBEDTLS_PK_ECDSA;
1319}
1320
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001321#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001322static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 const unsigned char *hash, size_t hash_len,
1324 const unsigned char *sig, size_t sig_len,
1325 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001326{
Janos Follath24eed8d2019-11-22 13:21:35 +00001327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001328 ((void) md_alg);
1329
1330 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001331 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 hash, hash_len, sig, sig_len,
1333 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001334
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1336 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1337 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001340}
1341
valerio38992cb2023-04-20 09:56:30 +02001342static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 const unsigned char *hash, size_t hash_len,
1344 unsigned char *sig, size_t sig_size, size_t *sig_len,
1345 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1346 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001347{
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001349 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1351 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001352
1353}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001356{
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (ctx != NULL) {
1360 mbedtls_ecdsa_restart_init(ctx);
1361 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001364}
1365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001367{
Gilles Peskine449bd832023-01-11 14:50:10 +01001368 mbedtls_ecdsa_restart_free(ctx);
1369 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001370}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001371#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1374 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001375 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001376 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001377 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001378#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1379 ecdsa_verify_wrap, /* Compatible key structures */
1380#else
1381 NULL,
1382#endif
1383#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1384 ecdsa_sign_wrap, /* Compatible key structures */
1385#else
1386 NULL,
1387#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001388#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001389 ecdsa_verify_rs_wrap,
1390 ecdsa_sign_rs_wrap,
1391#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001392 NULL,
1393 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001394 eckey_check_pair, /* Compatible key structures */
Valerio Setti24138d92023-01-27 14:24:09 +01001395 eckey_alloc_wrap, /* Compatible key structures */
1396 eckey_free_wrap, /* Compatible key structures */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001397#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001398 ecdsa_rs_alloc,
1399 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001400#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001401 eckey_debug, /* Compatible key structures */
1402};
Valerio Setti7ca13182023-01-27 13:22:42 +01001403#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001406/*
1407 * Support for alternative RSA-private implementations
1408 */
1409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001411{
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001413}
1414
valerio38992cb2023-04-20 09:56:30 +02001415static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001416{
valerio38992cb2023-04-20 09:56:30 +02001417 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001420}
1421
valerio38992cb2023-04-20 09:56:30 +02001422static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 const unsigned char *hash, size_t hash_len,
1424 unsigned char *sig, size_t sig_size, size_t *sig_len,
1425 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001426{
valerio38992cb2023-04-20 09:56:30 +02001427 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if (UINT_MAX < hash_len) {
1430 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1431 }
Andres AG72849872017-01-19 11:24:33 +00001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1434 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1435 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1436 }
1437 if (*sig_len > sig_size) {
1438 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1439 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1442 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001443}
1444
valerio38992cb2023-04-20 09:56:30 +02001445static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 const unsigned char *input, size_t ilen,
1447 unsigned char *output, size_t *olen, size_t osize,
1448 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001449{
valerio38992cb2023-04-20 09:56:30 +02001450 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001451
1452 ((void) f_rng);
1453 ((void) p_rng);
1454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1456 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1457 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 return rsa_alt->decrypt_func(rsa_alt->key,
1460 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001461}
1462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001463#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001464static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 int (*f_rng)(void *, unsigned char *, size_t),
1466 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001467{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001469 unsigned char hash[32];
1470 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001471 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1474 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001475 }
1476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 memset(hash, 0x2a, sizeof(hash));
1478
valerio38992cb2023-04-20 09:56:30 +02001479 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 hash, sizeof(hash),
1481 sig, sizeof(sig), &sig_len,
1482 f_rng, p_rng)) != 0) {
1483 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001484 }
1485
valerio38992cb2023-04-20 09:56:30 +02001486 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 hash, sizeof(hash), sig, sig_len) != 0) {
1488 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1489 }
1490
1491 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001493#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001494
Gilles Peskine449bd832023-01-11 14:50:10 +01001495static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001496{
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 if (ctx != NULL) {
1500 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1501 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001502
Gilles Peskine449bd832023-01-11 14:50:10 +01001503 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001504}
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001507{
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1509 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001510}
1511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001512const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1513 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001514 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001515 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001516 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001517 NULL,
1518 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001519#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001520 NULL,
1521 NULL,
1522#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001523 rsa_alt_decrypt_wrap,
1524 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001526 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001527#else
1528 NULL,
1529#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001530 rsa_alt_alloc_wrap,
1531 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001532#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001533 NULL,
1534 NULL,
1535#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001536 NULL,
1537};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001539#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001540
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001541#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +02001542static size_t pk_opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001543{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001544 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001545 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001546
Valerio Setti4f387ef2023-05-02 14:15:59 +02001547 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return 0;
1549 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001550
Gilles Peskine449bd832023-01-11 14:50:10 +01001551 bits = psa_get_key_bits(&attributes);
1552 psa_reset_key_attributes(&attributes);
1553 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001554}
1555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001557{
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 return type == MBEDTLS_PK_ECKEY ||
1559 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001560}
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001563{
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 return type == MBEDTLS_PK_RSA ||
1565 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001566}
1567
valerio38992cb2023-04-20 09:56:30 +02001568static int pk_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 const unsigned char *hash, size_t hash_len,
1570 unsigned char *sig, size_t sig_size, size_t *sig_len,
1571 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001572{
Valerio Settiab363d92023-01-26 14:31:54 +01001573#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001574 ((void) pk);
John Durkopf35069a2020-08-17 22:05:14 -07001575 ((void) md_alg);
1576 ((void) hash);
1577 ((void) hash_len);
1578 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001579 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001580 ((void) sig_len);
1581 ((void) f_rng);
1582 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001584#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001585 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001586 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001587 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001588 psa_status_t status;
1589
1590 /* PSA has its own RNG */
1591 (void) f_rng;
1592 (void) p_rng;
1593
Valerio Setti4f387ef2023-05-02 14:15:59 +02001594 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001595 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001596 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001598
Gilles Peskine449bd832023-01-11 14:50:10 +01001599 type = psa_get_key_type(&attributes);
1600 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001601
Valerio Settiab363d92023-01-26 14:31:54 +01001602#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1604 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1605 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001606#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001607#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 if (PSA_KEY_TYPE_IS_RSA(type)) {
1609 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1610 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001611#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001612 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001613
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001614 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001615 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001616 sig, sig_size, sig_len);
1617 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001618#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001619 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001620 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001621 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001622#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001623#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001624 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001625 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001627#endif /* MBEDTLS_RSA_C */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001628 return PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001629 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001630
Valerio Settiab363d92023-01-26 14:31:54 +01001631#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001633 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1635 }
Valerio Settiab363d92023-01-26 14:31:54 +01001636#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001637
1638 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001639#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001640}
1641
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001642const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001643 MBEDTLS_PK_OPAQUE,
1644 "Opaque",
1645 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001646 pk_opaque_ecdsa_can_do,
1647 NULL, /* verify - will be done later */
1648 pk_opaque_sign_wrap,
1649#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1650 NULL, /* restartable verify - not relevant */
1651 NULL, /* restartable sign - not relevant */
1652#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001653 NULL, /* decrypt - not relevant */
1654 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001655 NULL, /* check_pair - could be done later or left NULL */
Valerio Settie00954d2023-04-28 15:24:32 +02001656 NULL, /* alloc - no need to allocate new data dynamically */
1657 NULL, /* free - as for the alloc, there is no data to free */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001658#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1659 NULL, /* restart alloc - not relevant */
1660 NULL, /* restart free - not relevant */
1661#endif
1662 NULL, /* debug - could be done later, or even left NULL */
1663};
1664
Neil Armstrong30beca32022-05-03 15:42:13 +02001665#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
valerio38992cb2023-04-20 09:56:30 +02001666static int pk_opaque_rsa_decrypt(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 const unsigned char *input, size_t ilen,
1668 unsigned char *output, size_t *olen, size_t osize,
1669 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001670{
Neil Armstrong10828182022-04-22 15:02:27 +02001671 psa_status_t status;
1672
1673 /* PSA has its own RNG */
1674 (void) f_rng;
1675 (void) p_rng;
1676
Valerio Setti4f387ef2023-05-02 14:15:59 +02001677 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 input, ilen,
1679 NULL, 0,
1680 output, osize, olen);
1681 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001682 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001683 }
1684
1685 return 0;
1686}
Neil Armstrong30beca32022-05-03 15:42:13 +02001687#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001688
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001689const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1690 MBEDTLS_PK_OPAQUE,
1691 "Opaque",
1692 pk_opaque_get_bitlen,
1693 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001694 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001695 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001696#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1697 NULL, /* restartable verify - not relevant */
1698 NULL, /* restartable sign - not relevant */
1699#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001700#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001701 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001702#else
1703 NULL, /* decrypt - not available */
1704#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001705 NULL, /* encrypt - will be done later */
1706 NULL, /* check_pair - could be done later or left NULL */
Valerio Settie00954d2023-04-28 15:24:32 +02001707 NULL, /* alloc - no need to allocate new data dynamically */
1708 NULL, /* free - as for the alloc, there is no data to free */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001709#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1710 NULL, /* restart alloc - not relevant */
1711 NULL, /* restart free - not relevant */
1712#endif
1713 NULL, /* debug - could be done later, or even left NULL */
1714};
1715
1716#endif /* MBEDTLS_USE_PSA_CRYPTO */
1717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#endif /* MBEDTLS_PK_C */