blob: dc7a27f0eaaf8dce04f2879eaf66118170f029b6 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000023#include "pk_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020026/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Neil Armstrong0f49f832022-02-28 15:07:38 +010035#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
36#include "pkwrite.h"
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020041#endif
42
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "mbedtls/asn1write.h"
45#endif
46
Andres Amaya Garciae32df082017-10-25 09:37:04 +010047#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050048#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010049#endif
50
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040052#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010053#include "mbedtls/psa_util.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040054#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020055#include "hash_info.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010056#endif
57
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059
Andres AG72849872017-01-19 11:24:33 +000060#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010061#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020062
Jerry Yub02ee182022-03-16 10:30:41 +080063#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010064int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010065{
Gilles Peskine449bd832023-01-11 14:50:10 +010066 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010067 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010068 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010069 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010070 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010071 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010073 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010074 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010075 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010077 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010078 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010079 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010081 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010083 case PSA_ERROR_COMMUNICATION_FAILURE:
84 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010086 case PSA_ERROR_DATA_CORRUPT:
87 case PSA_ERROR_DATA_INVALID:
88 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010090 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010091 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +010092 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010094 }
95}
96
Neil Armstrong30beca32022-05-03 15:42:13 +020097#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
98 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +010099int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +0100100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +0100102 case PSA_ERROR_NOT_PERMITTED:
103 case PSA_ERROR_INVALID_ARGUMENT:
104 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +0100106 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +0100108 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100110 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100112 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Neil Armstrong19915c22022-03-01 15:21:02 +0100114 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return mbedtls_pk_error_from_psa(status);
Neil Armstrong19915c22022-03-01 15:21:02 +0100116 }
117}
Neil Armstrong30beca32022-05-03 15:42:13 +0200118#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800119
120#endif /* MBEDTLS_PSA_CRYPTO_C */
121
122#if defined(MBEDTLS_USE_PSA_CRYPTO)
123
124#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100125int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800126{
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800128 case PSA_ERROR_NOT_PERMITTED:
129 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800131 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800133 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800135 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800137 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800139 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return mbedtls_pk_error_from_psa(status);
Jerry Yu07869e82022-03-16 16:40:50 +0800141 }
142}
Jerry Yu75339822022-03-23 12:06:31 +0800143#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800144
Jerry Yu75339822022-03-23 12:06:31 +0800145#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200149{
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return type == MBEDTLS_PK_RSA ||
151 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200152}
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154static size_t rsa_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200155{
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx;
157 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200158}
159
Neil Armstrong52f41f82022-02-22 15:30:24 +0100160#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100161static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
162 const unsigned char *hash, size_t hash_len,
163 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200164{
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
168 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
169 psa_status_t status;
170 mbedtls_pk_context key;
171 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100172 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100173 psa_algorithm_t psa_alg_md =
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
175 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
178 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
179 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (sig_len < rsa_len) {
182 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
183 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100184
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100185 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100186 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100187 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100188 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
190 if (key_len <= 0) {
191 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
192 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
195 psa_set_key_algorithm(&attributes, psa_alg_md);
196 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 status = psa_import_key(&attributes,
199 buf + sizeof(buf) - key_len, key_len,
200 &key_id);
201 if (status != PSA_SUCCESS) {
202 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100203 goto cleanup;
204 }
205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
207 sig, sig_len);
208 if (status != PSA_SUCCESS) {
209 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100210 goto cleanup;
211 }
212 ret = 0;
213
214cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 status = psa_destroy_key(key_id);
216 if (ret == 0 && status != PSA_SUCCESS) {
217 ret = mbedtls_pk_error_from_psa(status);
218 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100221}
222#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100223static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
224 const unsigned char *hash, size_t hash_len,
225 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100226{
Janos Follath24eed8d2019-11-22 13:21:35 +0000227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
229 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
232 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
233 }
Andres AG72849872017-01-19 11:24:33 +0000234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (sig_len < rsa_len) {
236 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
237 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
240 (unsigned int) hash_len,
241 hash, sig)) != 0) {
242 return ret;
243 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200244
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200245 /* The buffer contains a valid signature followed by extra data.
246 * We have a special error code for that so that so that callers can
247 * use mbedtls_pk_verify() to check "Does the buffer start with a
248 * valid signature?" and not just "Does the buffer contain a valid
249 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (sig_len > rsa_len) {
251 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
252 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200255}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100256#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200257
Jerry Yub02ee182022-03-16 10:30:41 +0800258#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100259int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
260 mbedtls_rsa_context *rsa_ctx,
261 const unsigned char *hash, size_t hash_len,
262 unsigned char *sig, size_t sig_size,
263 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100264{
Neil Armstrong98545682022-02-22 16:12:51 +0100265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
266 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
267 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
268 psa_status_t status;
269 mbedtls_pk_context key;
270 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100271 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100272 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
275 if (sig_size < *sig_len) {
276 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
277 }
Neil Armstrong98545682022-02-22 16:12:51 +0100278
Neil Armstronge4f28682022-02-24 15:41:39 +0100279 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100280 * re-construct one to make it happy */
281 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800282 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
284 if (key_len <= 0) {
285 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
286 }
287 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
288 psa_set_key_algorithm(&attributes, alg);
289 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 status = psa_import_key(&attributes,
292 buf + sizeof(buf) - key_len, key_len,
293 &key_id);
294 if (status != PSA_SUCCESS) {
295 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100296 goto cleanup;
297 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 status = psa_sign_hash(key_id, alg, hash, hash_len,
299 sig, sig_size, sig_len);
300 if (status != PSA_SUCCESS) {
301 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100302 goto cleanup;
303 }
304
305 ret = 0;
306
307cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 status = psa_destroy_key(key_id);
309 if (ret == 0 && status != PSA_SUCCESS) {
310 ret = mbedtls_pk_error_from_psa(status);
311 }
312 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100313}
Jerry Yu406cf272022-03-22 11:33:42 +0800314#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800315
316#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100317static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
318 const unsigned char *hash, size_t hash_len,
319 unsigned char *sig, size_t sig_size, size_t *sig_len,
320 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800321{
Jerry Yu1d172a32022-03-12 19:12:05 +0800322 ((void) f_rng);
323 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800324
Jerry Yubd1b3272022-03-24 13:05:20 +0800325 psa_algorithm_t psa_md_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
327 if (psa_md_alg == 0) {
328 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
329 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
332 psa_md_alg),
333 ctx, hash, hash_len,
334 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800335}
Neil Armstrong98545682022-02-22 16:12:51 +0100336#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100337static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
338 const unsigned char *hash, size_t hash_len,
339 unsigned char *sig, size_t sig_size, size_t *sig_len,
340 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200341{
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
345 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
346 }
Andres AG72849872017-01-19 11:24:33 +0000347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 *sig_len = mbedtls_rsa_get_len(rsa);
349 if (sig_size < *sig_len) {
350 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
351 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
354 md_alg, (unsigned int) hash_len,
355 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200356}
Neil Armstrong98545682022-02-22 16:12:51 +0100357#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200358
Neil Armstrong18f43c72022-02-09 15:32:45 +0100359#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100360static int rsa_decrypt_wrap(void *ctx,
361 const unsigned char *input, size_t ilen,
362 unsigned char *output, size_t *olen, size_t osize,
363 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100364{
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100366 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
367 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
368 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
369 psa_status_t status;
370 mbedtls_pk_context key;
371 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100372 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100373
374 ((void) f_rng);
375 ((void) p_rng);
376
377#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
379 return MBEDTLS_ERR_RSA_INVALID_PADDING;
380 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100381#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (ilen != mbedtls_rsa_get_len(rsa)) {
384 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
385 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100386
387 /* mbedtls_pk_write_key_der() expects a full PK context;
388 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100389 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100390 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
392 if (key_len <= 0) {
393 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
394 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
397 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
398 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 status = psa_import_key(&attributes,
401 buf + sizeof(buf) - key_len, key_len,
402 &key_id);
403 if (status != PSA_SUCCESS) {
404 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100405 goto cleanup;
406 }
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
409 input, ilen,
410 NULL, 0,
411 output, osize, olen);
412 if (status != PSA_SUCCESS) {
413 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100414 goto cleanup;
415 }
416
417 ret = 0;
418
419cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 mbedtls_platform_zeroize(buf, sizeof(buf));
421 status = psa_destroy_key(key_id);
422 if (ret == 0 && status != PSA_SUCCESS) {
423 ret = mbedtls_pk_error_from_psa(status);
424 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100427}
428#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100429static int rsa_decrypt_wrap(void *ctx,
430 const unsigned char *input, size_t ilen,
431 unsigned char *output, size_t *olen, size_t osize,
432 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200433{
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (ilen != mbedtls_rsa_get_len(rsa)) {
437 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
438 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
441 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200442}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100443#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200444
Neil Armstrong96a16a42022-02-10 10:40:11 +0100445#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100446static int rsa_encrypt_wrap(void *ctx,
447 const unsigned char *input, size_t ilen,
448 unsigned char *output, size_t *olen, size_t osize,
449 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100450{
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
453 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
454 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
455 psa_status_t status;
456 mbedtls_pk_context key;
457 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100458 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100459
460 ((void) f_rng);
461 ((void) p_rng);
462
463#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
465 return MBEDTLS_ERR_RSA_INVALID_PADDING;
466 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100467#endif
468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (mbedtls_rsa_get_len(rsa) > osize) {
470 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
471 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100472
Neil Armstrongac014ca2022-02-24 15:27:54 +0100473 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100474 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100475 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100476 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
478 if (key_len <= 0) {
479 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
480 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
483 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
484 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100485
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 status = psa_import_key(&attributes,
487 buf + sizeof(buf) - key_len, key_len,
488 &key_id);
489 if (status != PSA_SUCCESS) {
490 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100491 goto cleanup;
492 }
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
495 input, ilen,
496 NULL, 0,
497 output, osize, olen);
498 if (status != PSA_SUCCESS) {
499 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100500 goto cleanup;
501 }
502
503 ret = 0;
504
505cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 status = psa_destroy_key(key_id);
507 if (ret == 0 && status != PSA_SUCCESS) {
508 ret = mbedtls_pk_error_from_psa(status);
509 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100512}
513#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100514static int rsa_encrypt_wrap(void *ctx,
515 const unsigned char *input, size_t ilen,
516 unsigned char *output, size_t *olen, size_t osize,
517 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200518{
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
520 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (*olen > osize) {
523 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
524 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
527 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200528}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100529#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531static int rsa_check_pair_wrap(const void *pub, const void *prv,
532 int (*f_rng)(void *, unsigned char *, size_t),
533 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100534{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200535 (void) f_rng;
536 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub,
538 (const mbedtls_rsa_context *) prv);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100539}
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200542{
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (ctx != NULL) {
546 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
547 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200550}
551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200553{
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
555 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200556}
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200559{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200560#if defined(MBEDTLS_RSA_ALT)
561 /* Not supported */
562 (void) ctx;
563 (void) items;
564#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200566 items->name = "rsa.N";
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 items->value = &(((mbedtls_rsa_context *) ctx)->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200568
569 items++;
570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200572 items->name = "rsa.E";
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 items->value = &(((mbedtls_rsa_context *) ctx)->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200574#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200575}
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577const mbedtls_pk_info_t mbedtls_rsa_info = {
578 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200579 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200580 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200581 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200582 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200583 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200584#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200585 NULL,
586 NULL,
587#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200588 rsa_decrypt_wrap,
589 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100590 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200591 rsa_alloc_wrap,
592 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200593#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200594 NULL,
595 NULL,
596#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200597 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200598};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200599#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200602/*
603 * Generic EC key
604 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100605static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200606{
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 return type == MBEDTLS_PK_ECKEY ||
608 type == MBEDTLS_PK_ECKEY_DH ||
609 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200610}
611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612static size_t eckey_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200613{
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 return ((mbedtls_ecp_keypair *) ctx)->grp.pbits;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200615}
616
Valerio Setti1cdddac2023-02-02 13:55:57 +0100617#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400618#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400619/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500620 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
621 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500622 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
624 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500625{
Janos Follath24eed8d2019-11-22 13:21:35 +0000626 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500627 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
630 MBEDTLS_ASN1_INTEGER)) != 0) {
631 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500632 }
633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 while (unpadded_len > 0 && **from == 0x00) {
635 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500636 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500637 }
638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (unpadded_len > to_len || unpadded_len == 0) {
640 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
641 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500642
Andrzej Kurek9241d182018-11-20 05:04:35 -0500643 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 memset(to, 0x00, padding_len);
645 memcpy(to + padding_len, *from, unpadded_len);
646 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500649}
650
651/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400652 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500653 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500654 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400655 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100656static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
657 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400658{
Janos Follath24eed8d2019-11-22 13:21:35 +0000659 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500660 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
663 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
664 return ret;
665 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400666
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500667 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
669 return ret;
670 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500671 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
673 return ret;
674 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400677}
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
680 const unsigned char *hash, size_t hash_len,
681 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400682{
Valerio Settiab363d92023-01-26 14:31:54 +0100683 mbedtls_ecp_keypair *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000684 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200685 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100686 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200687 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400688 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400689 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100690 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000691 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400692 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700693 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100694 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100695 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
697 const size_t signature_part_size = (ctx->grp.nbits + 7) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700698 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (curve == 0) {
701 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
702 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500703
Hanno Beckerccf574e2019-01-29 08:26:15 +0000704 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500705 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400706 key.pk_info = &pk_info;
707 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 p = buf + sizeof(buf);
709 key_len = mbedtls_pk_write_pubkey(&p, buf, &key);
710 if (key_len <= 0) {
711 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
712 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
715 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
716 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 status = psa_import_key(&attributes,
719 buf + sizeof(buf) - key_len, key_len,
720 &key_id);
721 if (status != PSA_SUCCESS) {
722 ret = mbedtls_pk_error_from_psa(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400723 goto cleanup;
724 }
725
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500726 /* We don't need the exported key anymore and can
727 * reuse its buffer for signature extraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (2 * signature_part_size > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500729 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
730 goto cleanup;
731 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 p = (unsigned char *) sig;
734 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
735 signature_part_size)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500736 goto cleanup;
737 }
738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 status = psa_verify_hash(key_id, psa_sig_md,
740 hash, hash_len,
741 buf, 2 * signature_part_size);
742 if (status != PSA_SUCCESS) {
743 ret = mbedtls_pk_error_from_psa_ecdsa(status);
744 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400745 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500748 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
749 goto cleanup;
750 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400751 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400752
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500753cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 status = psa_destroy_key(key_id);
755 if (ret == 0 && status != PSA_SUCCESS) {
756 ret = mbedtls_pk_error_from_psa(status);
757 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400760}
761#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100762static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
763 const unsigned char *hash, size_t hash_len,
764 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200765{
Janos Follath24eed8d2019-11-22 13:21:35 +0000766 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200767 ((void) md_alg);
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx,
770 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
773 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
774 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200777}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400778#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100779#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200780
Valerio Setti1cdddac2023-02-02 13:55:57 +0100781#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100782#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100783/*
784 * Simultaneously convert and move raw MPI from the beginning of a buffer
785 * to an ASN.1 MPI at the end of the buffer.
786 * See also mbedtls_asn1_write_mpi().
787 *
788 * p: pointer to the end of the output buffer
789 * start: start of the output buffer, and also of the mpi to write at the end
790 * n_len: length of the mpi to read from start
791 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100792static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
793 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100794{
795 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
796 size_t len = 0;
797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 if ((size_t) (*p - start) < n_len) {
799 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
800 }
Neil Armstrong15021652022-03-01 10:14:17 +0100801
802 len = n_len;
803 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100805
806 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
807 * Neither r nor s should be 0, but as a failsafe measure, still detect
808 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100810 ++(*p);
811 --len;
812 }
813
814 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (len == 0) {
816 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
817 }
Neil Armstrong15021652022-03-01 10:14:17 +0100818
819 /* if the msb is 1, ASN.1 requires that we prepend a 0.
820 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 if (**p & 0x80) {
822 if (*p - start < 1) {
823 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
824 }
Neil Armstrong15021652022-03-01 10:14:17 +0100825
826 *--(*p) = 0x00;
827 len += 1;
828 }
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
831 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
832 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100833
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100835}
836
837/* Transcode signature from PSA format to ASN.1 sequence.
838 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
839 * MPIs, and in-place.
840 *
841 * [in/out] sig: the signature pre- and post-transcoding
842 * [in/out] sig_len: signature length pre- and post-transcoding
843 * [int] buf_len: the available size the in/out buffer
844 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100845static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
846 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100847{
848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
849 size_t len = 0;
850 const size_t rs_len = *sig_len / 2;
851 unsigned char *p = sig + buf_len;
852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
854 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
857 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
858 MBEDTLS_ASN1_CONSTRUCTED |
859 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100862 *sig_len = len;
863
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100865}
Neil Armstronge9606902022-02-09 14:23:00 +0100866
Neil Armstrong17a06552022-03-18 15:27:38 +0100867/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
868 *
869 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
870 * [in] end: ASN.1 buffer end
871 * [out] key_len: the ECDSA privateKey length in bytes
872 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873static int find_ecdsa_private_key(unsigned char **buf, unsigned char *end,
874 size_t *key_len)
Neil Armstronge9606902022-02-09 14:23:00 +0100875{
876 size_t len;
877 int ret;
878
Neil Armstrong17a06552022-03-18 15:27:38 +0100879 /*
880 * RFC 5915, or SEC1 Appendix C.4
881 *
882 * ECPrivateKey ::= SEQUENCE {
883 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
884 * privateKey OCTET STRING,
885 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
886 * publicKey [1] BIT STRING OPTIONAL
887 * }
888 */
889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if ((ret = mbedtls_asn1_get_tag(buf, end, &len,
891 MBEDTLS_ASN1_CONSTRUCTED |
892 MBEDTLS_ASN1_SEQUENCE)) != 0) {
893 return ret;
894 }
Neil Armstronge9606902022-02-09 14:23:00 +0100895
896 /* version */
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if ((ret = mbedtls_asn1_get_tag(buf, end, &len,
898 MBEDTLS_ASN1_INTEGER)) != 0) {
899 return ret;
900 }
Neil Armstronge9606902022-02-09 14:23:00 +0100901
902 *buf += len;
903
904 /* privateKey */
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if ((ret = mbedtls_asn1_get_tag(buf, end, &len,
906 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
907 return ret;
908 }
Neil Armstronge9606902022-02-09 14:23:00 +0100909
910 *key_len = len;
911
912 return 0;
913}
914
Gilles Peskine449bd832023-01-11 14:50:10 +0100915static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
916 const unsigned char *hash, size_t hash_len,
917 unsigned char *sig, size_t sig_size, size_t *sig_len,
918 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100919{
Valerio Settiab363d92023-01-26 14:31:54 +0100920 mbedtls_ecp_keypair *ctx = ctx_arg;
Neil Armstronge9606902022-02-09 14:23:00 +0100921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
922 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
923 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
924 psa_status_t status;
925 mbedtls_pk_context key;
926 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +0100927 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +0100928 unsigned char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 psa_algorithm_t psa_hash = mbedtls_hash_info_psa_from_md(md_alg);
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100930#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 psa_algorithm_t psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(psa_hash);
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100932#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA(psa_hash);
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100934#endif
Neil Armstronge9606902022-02-09 14:23:00 +0100935 size_t curve_bits;
936 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100938
939 /* PSA has its own RNG */
940 ((void) f_rng);
941 ((void) p_rng);
942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 if (curve == 0) {
944 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
945 }
Neil Armstronge9606902022-02-09 14:23:00 +0100946
947 /* mbedtls_pk_write_key_der() expects a full PK context;
948 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +0100949 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +0100950 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
952 if (key_len <= 0) {
953 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
954 }
Neil Armstronge9606902022-02-09 14:23:00 +0100955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 p = buf + sizeof(buf) - key_len;
957 ret = find_ecdsa_private_key(&p, buf + sizeof(buf), &key_len);
958 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100959 goto cleanup;
960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
963 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
964 psa_set_key_algorithm(&attributes, psa_sig_md);
965
966 status = psa_import_key(&attributes,
967 p, key_len,
968 &key_id);
969 if (status != PSA_SUCCESS) {
970 ret = mbedtls_pk_error_from_psa(status);
971 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100972 }
973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
975 sig, sig_size, sig_len);
976 if (status != PSA_SUCCESS) {
977 ret = mbedtls_pk_error_from_psa_ecdsa(status);
978 goto cleanup;
979 }
980
981 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100982
983cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_platform_zeroize(buf, sizeof(buf));
985 status = psa_destroy_key(key_id);
986 if (ret == 0 && status != PSA_SUCCESS) {
987 ret = mbedtls_pk_error_from_psa(status);
988 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100989
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100991}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100992#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
994 const unsigned char *hash, size_t hash_len,
995 unsigned char *sig, size_t sig_size, size_t *sig_len,
996 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200997{
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
999 md_alg, hash, hash_len,
1000 sig, sig_size, sig_len,
1001 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001002}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001003#endif /* MBEDTLS_USE_PSA_CRYPTO */
1004#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001005
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001006#if defined(MBEDTLS_ECDSA_C)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001007#if defined(MBEDTLS_ECP_RESTARTABLE)
1008/* Forward declarations */
1009static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1010 const unsigned char *hash, size_t hash_len,
1011 const unsigned char *sig, size_t sig_len,
1012 void *rs_ctx);
1013
1014static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1015 const unsigned char *hash, size_t hash_len,
1016 unsigned char *sig, size_t sig_size, size_t *sig_len,
1017 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1018 void *rs_ctx);
1019
1020/*
1021 * Restart context for ECDSA operations with ECKEY context
1022 *
1023 * We need to store an actual ECDSA context, as we need to pass the same to
1024 * the underlying ecdsa function, so we can't create it on the fly every time.
1025 */
1026typedef struct {
1027 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1028 mbedtls_ecdsa_context ecdsa_ctx;
1029} eckey_restart_ctx;
1030
1031static void *eckey_rs_alloc(void)
1032{
1033 eckey_restart_ctx *rs_ctx;
1034
1035 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1036
1037 if (ctx != NULL) {
1038 rs_ctx = ctx;
1039 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1040 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1041 }
1042
1043 return ctx;
1044}
1045
1046static void eckey_rs_free(void *ctx)
1047{
1048 eckey_restart_ctx *rs_ctx;
1049
1050 if (ctx == NULL) {
1051 return;
1052 }
1053
1054 rs_ctx = ctx;
1055 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1056 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1057
1058 mbedtls_free(ctx);
1059}
1060
1061static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1062 const unsigned char *hash, size_t hash_len,
1063 const unsigned char *sig, size_t sig_len,
1064 void *rs_ctx)
1065{
1066 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1067 eckey_restart_ctx *rs = rs_ctx;
1068
1069 /* Should never happen */
1070 if (rs == NULL) {
1071 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1072 }
1073
1074 /* set up our own sub-context if needed (that is, on first run) */
1075 if (rs->ecdsa_ctx.grp.pbits == 0) {
1076 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1077 }
1078
1079 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx,
1080 md_alg, hash, hash_len,
1081 sig, sig_len, &rs->ecdsa_rs));
1082
1083cleanup:
1084 return ret;
1085}
1086
1087static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1088 const unsigned char *hash, size_t hash_len,
1089 unsigned char *sig, size_t sig_size, size_t *sig_len,
1090 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1091 void *rs_ctx)
1092{
1093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1094 eckey_restart_ctx *rs = rs_ctx;
1095
1096 /* Should never happen */
1097 if (rs == NULL) {
1098 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1099 }
1100
1101 /* set up our own sub-context if needed (that is, on first run) */
1102 if (rs->ecdsa_ctx.grp.pbits == 0) {
1103 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1104 }
1105
1106 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg,
1107 hash, hash_len, sig, sig_size, sig_len,
1108 f_rng, p_rng, &rs->ecdsa_rs));
1109
1110cleanup:
1111 return ret;
1112}
1113#endif /* MBEDTLS_ECP_RESTARTABLE */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001114#endif /* MBEDTLS_ECDSA_C */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001115
1116static int eckey_check_pair(const void *pub, const void *prv,
1117 int (*f_rng)(void *, unsigned char *, size_t),
1118 void *p_rng)
1119{
1120 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
1121 (const mbedtls_ecp_keypair *) prv,
1122 f_rng, p_rng);
1123}
1124
1125static void *eckey_alloc_wrap(void)
1126{
1127 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1128
1129 if (ctx != NULL) {
1130 mbedtls_ecp_keypair_init(ctx);
1131 }
1132
1133 return ctx;
1134}
1135
1136static void eckey_free_wrap(void *ctx)
1137{
1138 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1139 mbedtls_free(ctx);
1140}
1141
1142static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
1143{
1144 items->type = MBEDTLS_PK_DEBUG_ECP;
1145 items->name = "eckey.Q";
1146 items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
1147}
1148
1149const mbedtls_pk_info_t mbedtls_eckey_info = {
1150 MBEDTLS_PK_ECKEY,
1151 "EC",
1152 eckey_get_bitlen,
1153 eckey_can_do,
1154#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1155 ecdsa_verify_wrap, /* Compatible key structures */
1156#else
1157 NULL,
1158#endif
1159#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1160 ecdsa_sign_wrap, /* Compatible key structures */
1161#else
1162 NULL,
1163#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001164#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001165 eckey_verify_rs_wrap,
1166 eckey_sign_rs_wrap,
1167#endif
1168 NULL,
1169 NULL,
1170 eckey_check_pair,
1171 eckey_alloc_wrap,
1172 eckey_free_wrap,
1173#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1174 eckey_rs_alloc,
1175 eckey_rs_free,
1176#endif
1177 eckey_debug,
1178};
1179
1180/*
1181 * EC key restricted to ECDH
1182 */
1183static int eckeydh_can_do(mbedtls_pk_type_t type)
1184{
1185 return type == MBEDTLS_PK_ECKEY ||
1186 type == MBEDTLS_PK_ECKEY_DH;
1187}
1188
1189const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1190 MBEDTLS_PK_ECKEY_DH,
1191 "EC_DH",
1192 eckey_get_bitlen, /* Same underlying key structure */
1193 eckeydh_can_do,
1194 NULL,
1195 NULL,
1196#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1197 NULL,
1198 NULL,
1199#endif
1200 NULL,
1201 NULL,
1202 eckey_check_pair,
1203 eckey_alloc_wrap, /* Same underlying key structure */
1204 eckey_free_wrap, /* Same underlying key structure */
1205#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1206 NULL,
1207 NULL,
1208#endif
1209 eckey_debug, /* Same underlying key structure */
1210};
1211#endif /* MBEDTLS_ECP_C */
1212
1213#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1214static int ecdsa_can_do(mbedtls_pk_type_t type)
1215{
1216 return type == MBEDTLS_PK_ECDSA;
1217}
1218
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001219#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001220static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1221 const unsigned char *hash, size_t hash_len,
1222 const unsigned char *sig, size_t sig_len,
1223 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001224{
Janos Follath24eed8d2019-11-22 13:21:35 +00001225 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001226 ((void) md_alg);
1227
1228 ret = mbedtls_ecdsa_read_signature_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 (mbedtls_ecdsa_context *) ctx,
1230 hash, hash_len, sig, sig_len,
1231 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001232
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1234 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1235 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001236
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001238}
1239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1241 const unsigned char *hash, size_t hash_len,
1242 unsigned char *sig, size_t sig_size, size_t *sig_len,
1243 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1244 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001245{
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 return mbedtls_ecdsa_write_signature_restartable(
1247 (mbedtls_ecdsa_context *) ctx,
1248 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1249 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001250
1251}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001254{
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (ctx != NULL) {
1258 mbedtls_ecdsa_restart_init(ctx);
1259 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001262}
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001265{
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 mbedtls_ecdsa_restart_free(ctx);
1267 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001268}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001269#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001271const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1272 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001273 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001274 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001275 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001276#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1277 ecdsa_verify_wrap, /* Compatible key structures */
1278#else
1279 NULL,
1280#endif
1281#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1282 ecdsa_sign_wrap, /* Compatible key structures */
1283#else
1284 NULL,
1285#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001286#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001287 ecdsa_verify_rs_wrap,
1288 ecdsa_sign_rs_wrap,
1289#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001290 NULL,
1291 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001292 eckey_check_pair, /* Compatible key structures */
Valerio Setti24138d92023-01-27 14:24:09 +01001293 eckey_alloc_wrap, /* Compatible key structures */
1294 eckey_free_wrap, /* Compatible key structures */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001295#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001296 ecdsa_rs_alloc,
1297 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001298#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001299 eckey_debug, /* Compatible key structures */
1300};
Valerio Setti7ca13182023-01-27 13:22:42 +01001301#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001304/*
1305 * Support for alternative RSA-private implementations
1306 */
1307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001309{
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001311}
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313static size_t rsa_alt_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001314{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001318}
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1321 const unsigned char *hash, size_t hash_len,
1322 unsigned char *sig, size_t sig_size, size_t *sig_len,
1323 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001324{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 if (UINT_MAX < hash_len) {
1328 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1329 }
Andres AG72849872017-01-19 11:24:33 +00001330
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1332 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1333 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1334 }
1335 if (*sig_len > sig_size) {
1336 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1337 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1340 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001341}
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343static int rsa_alt_decrypt_wrap(void *ctx,
1344 const unsigned char *input, size_t ilen,
1345 unsigned char *output, size_t *olen, size_t osize,
1346 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001347{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001348 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001349
1350 ((void) f_rng);
1351 ((void) p_rng);
1352
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1354 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1355 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 return rsa_alt->decrypt_func(rsa_alt->key,
1358 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001359}
1360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001362static int rsa_alt_check_pair(const void *pub, const void *prv,
1363 int (*f_rng)(void *, unsigned char *, size_t),
1364 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001365{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001366 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001367 unsigned char hash[32];
1368 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001370
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1372 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001373 }
1374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 memset(hash, 0x2a, sizeof(hash));
1376
1377 if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE,
1378 hash, sizeof(hash),
1379 sig, sizeof(sig), &sig_len,
1380 f_rng, p_rng)) != 0) {
1381 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001382 }
1383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE,
1385 hash, sizeof(hash), sig, sig_len) != 0) {
1386 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1387 }
1388
1389 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001390}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001394{
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if (ctx != NULL) {
1398 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1399 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001402}
1403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001405{
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1407 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001408}
1409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001410const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1411 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001412 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001413 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001414 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001415 NULL,
1416 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001417#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001418 NULL,
1419 NULL,
1420#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001421 rsa_alt_decrypt_wrap,
1422 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001424 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001425#else
1426 NULL,
1427#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001428 rsa_alt_alloc_wrap,
1429 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001430#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001431 NULL,
1432 NULL,
1433#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001434 NULL,
1435};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001437#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001438
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001439#if defined(MBEDTLS_USE_PSA_CRYPTO)
1440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441static void *pk_opaque_alloc_wrap(void)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001442{
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t));
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001444
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001445 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 return ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001448}
1449
Gilles Peskine449bd832023-01-11 14:50:10 +01001450static void pk_opaque_free_wrap(void *ctx)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001451{
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t));
1453 mbedtls_free(ctx);
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001454}
1455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456static size_t pk_opaque_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001457{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001458 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001459 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001460 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) {
1463 return 0;
1464 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 bits = psa_get_key_bits(&attributes);
1467 psa_reset_key_attributes(&attributes);
1468 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001469}
1470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001472{
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return type == MBEDTLS_PK_ECKEY ||
1474 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001475}
1476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001478{
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 return type == MBEDTLS_PK_RSA ||
1480 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001481}
1482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1484 const unsigned char *hash, size_t hash_len,
1485 unsigned char *sig, size_t sig_size, size_t *sig_len,
1486 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001487{
Valerio Settiab363d92023-01-26 14:31:54 +01001488#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001489 ((void) ctx);
1490 ((void) md_alg);
1491 ((void) hash);
1492 ((void) hash_len);
1493 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001494 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001495 ((void) sig_len);
1496 ((void) f_rng);
1497 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001499#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001500 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001501 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001502 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001503 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001504 psa_status_t status;
1505
1506 /* PSA has its own RNG */
1507 (void) f_rng;
1508 (void) p_rng;
1509
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 status = psa_get_key_attributes(*key, &attributes);
1511 if (status != PSA_SUCCESS) {
1512 return mbedtls_pk_error_from_psa(status);
1513 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 type = psa_get_key_type(&attributes);
1516 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001517
Valerio Settiab363d92023-01-26 14:31:54 +01001518#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1520 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1521 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001522#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001523#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001524 if (PSA_KEY_TYPE_IS_RSA(type)) {
1525 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1526 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001527#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001529
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001530 /* make the signature */
Gilles Peskine449bd832023-01-11 14:50:10 +01001531 status = psa_sign_hash(*key, alg, hash, hash_len,
1532 sig, sig_size, sig_len);
1533 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001534#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1536 return mbedtls_pk_error_from_psa_ecdsa(status);
1537 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001538#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001539#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 if (PSA_KEY_TYPE_IS_RSA(type)) {
1541 return mbedtls_pk_error_from_psa_rsa(status);
1542 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001543#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 return mbedtls_pk_error_from_psa(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001545 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001546
Valerio Settiab363d92023-01-26 14:31:54 +01001547#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001549 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1551 }
Valerio Settiab363d92023-01-26 14:31:54 +01001552#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001553
1554 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001555#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001556}
1557
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001558const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001559 MBEDTLS_PK_OPAQUE,
1560 "Opaque",
1561 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001562 pk_opaque_ecdsa_can_do,
1563 NULL, /* verify - will be done later */
1564 pk_opaque_sign_wrap,
1565#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1566 NULL, /* restartable verify - not relevant */
1567 NULL, /* restartable sign - not relevant */
1568#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001569 NULL, /* decrypt - not relevant */
1570 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001571 NULL, /* check_pair - could be done later or left NULL */
1572 pk_opaque_alloc_wrap,
1573 pk_opaque_free_wrap,
1574#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1575 NULL, /* restart alloc - not relevant */
1576 NULL, /* restart free - not relevant */
1577#endif
1578 NULL, /* debug - could be done later, or even left NULL */
1579};
1580
Neil Armstrong30beca32022-05-03 15:42:13 +02001581#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01001582static int pk_opaque_rsa_decrypt(void *ctx,
1583 const unsigned char *input, size_t ilen,
1584 unsigned char *output, size_t *olen, size_t osize,
1585 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001586{
1587 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1588 psa_status_t status;
1589
1590 /* PSA has its own RNG */
1591 (void) f_rng;
1592 (void) p_rng;
1593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1595 input, ilen,
1596 NULL, 0,
1597 output, osize, olen);
1598 if (status != PSA_SUCCESS) {
1599 return mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001600 }
1601
1602 return 0;
1603}
Neil Armstrong30beca32022-05-03 15:42:13 +02001604#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001605
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001606const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1607 MBEDTLS_PK_OPAQUE,
1608 "Opaque",
1609 pk_opaque_get_bitlen,
1610 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001611 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001612 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001613#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1614 NULL, /* restartable verify - not relevant */
1615 NULL, /* restartable sign - not relevant */
1616#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001617#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001618 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001619#else
1620 NULL, /* decrypt - not available */
1621#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001622 NULL, /* encrypt - will be done later */
1623 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001624 pk_opaque_alloc_wrap,
1625 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001626#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1627 NULL, /* restart alloc - not relevant */
1628 NULL, /* restart free - not relevant */
1629#endif
1630 NULL, /* debug - could be done later, or even left NULL */
1631};
1632
1633#endif /* MBEDTLS_USE_PSA_CRYPTO */
1634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#endif /* MBEDTLS_PK_C */