blob: 2d5a0b7279821c631d49910825ff4a1b92b19f0b [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"
Janos Follath24eed8d2019-11-22 13:21:35 +000026#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020027
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020028/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020030
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020037#endif
38
Gilles Peskine8a6022e2022-10-04 23:01:59 +020039#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PSA_CRYPTO_C)
40#include "pkwrite.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010041#endif
42
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050043#if defined(MBEDTLS_PSA_CRYPTO_C)
44#include "mbedtls/psa_util.h"
45#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
46#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
47 psa_to_pk_rsa_errors, \
48 psa_pk_status_to_mbedtls)
49#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
50 psa_to_pk_ecdsa_errors, \
51 psa_pk_status_to_mbedtls)
52#endif
53
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010054#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040055#include "psa/crypto.h"
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020056#include "hash_info.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020057
Valerio Setti80d07982023-02-08 13:49:17 +010058#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020059#include "mbedtls/asn1write.h"
60#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010061#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020062#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010063
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020065
Andres AG72849872017-01-19 11:24:33 +000066#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010067#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020068#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020069
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050070#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yub02ee182022-03-16 10:30:41 +080071#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010072int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010073{
Gilles Peskine449bd832023-01-11 14:50:10 +010074 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010075 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010077 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010078 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010079 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010081 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010083 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010084 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010085 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010087 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010088 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010089 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010090 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010091 case PSA_ERROR_COMMUNICATION_FAILURE:
92 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010094 case PSA_ERROR_DATA_CORRUPT:
95 case PSA_ERROR_DATA_INVALID:
96 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010098 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100100 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +0100102 }
103}
104
Neil Armstrong30beca32022-05-03 15:42:13 +0200105#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
106 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +0100107int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +0100108{
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +0100110 case PSA_ERROR_NOT_PERMITTED:
111 case PSA_ERROR_INVALID_ARGUMENT:
112 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +0100114 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +0100116 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100118 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100120 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Andrzej Kurekba241382022-12-27 09:17:33 -0500122 case PSA_SUCCESS:
123 return 0;
124 case PSA_ERROR_NOT_SUPPORTED:
125 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
126 case PSA_ERROR_INSUFFICIENT_MEMORY:
127 return MBEDTLS_ERR_PK_ALLOC_FAILED;
128 case PSA_ERROR_BAD_STATE:
129 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
130 case PSA_ERROR_COMMUNICATION_FAILURE:
131 case PSA_ERROR_HARDWARE_FAILURE:
132 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
133 case PSA_ERROR_DATA_CORRUPT:
134 case PSA_ERROR_DATA_INVALID:
135 case PSA_ERROR_STORAGE_FAILURE:
136 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
137 case PSA_ERROR_CORRUPTION_DETECTED:
138 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100139 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500140 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +0100141 }
142}
Neil Armstrong30beca32022-05-03 15:42:13 +0200143#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800144#endif /* MBEDTLS_PSA_CRYPTO_C */
145
146#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu07869e82022-03-16 16:40:50 +0800147#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800149{
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800151 case PSA_ERROR_NOT_PERMITTED:
152 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800154 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800156 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800158 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800160 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Andrzej Kurekba241382022-12-27 09:17:33 -0500162 case PSA_SUCCESS:
163 return 0;
164 case PSA_ERROR_NOT_SUPPORTED:
165 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
166 case PSA_ERROR_INSUFFICIENT_MEMORY:
167 return MBEDTLS_ERR_PK_ALLOC_FAILED;
168 case PSA_ERROR_BAD_STATE:
169 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
170 case PSA_ERROR_COMMUNICATION_FAILURE:
171 case PSA_ERROR_HARDWARE_FAILURE:
172 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
173 case PSA_ERROR_DATA_CORRUPT:
174 case PSA_ERROR_DATA_INVALID:
175 case PSA_ERROR_STORAGE_FAILURE:
176 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
177 case PSA_ERROR_CORRUPTION_DETECTED:
178 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu07869e82022-03-16 16:40:50 +0800179 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500180 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Jerry Yu07869e82022-03-16 16:40:50 +0800181 }
182}
Jerry Yu75339822022-03-23 12:06:31 +0800183#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu75339822022-03-23 12:06:31 +0800184#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500185#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Neil Armstrong19915c22022-03-01 15:21:02 +0100186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100188static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200189{
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return type == MBEDTLS_PK_RSA ||
191 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200192}
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194static size_t rsa_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200195{
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx;
197 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200198}
199
Neil Armstrong52f41f82022-02-22 15:30:24 +0100200#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100201static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
202 const unsigned char *hash, size_t hash_len,
203 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200204{
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
207 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
208 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
209 psa_status_t status;
210 mbedtls_pk_context key;
211 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100212 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100213 psa_algorithm_t psa_alg_md =
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
215 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
218 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
219 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (sig_len < rsa_len) {
222 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
223 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100224
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100225 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100226 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100227 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100228 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
230 if (key_len <= 0) {
231 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
232 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
235 psa_set_key_algorithm(&attributes, psa_alg_md);
236 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 status = psa_import_key(&attributes,
239 buf + sizeof(buf) - key_len, key_len,
240 &key_id);
241 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500242 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100243 goto cleanup;
244 }
245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
247 sig, sig_len);
248 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500249 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100250 goto cleanup;
251 }
252 ret = 0;
253
254cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 status = psa_destroy_key(key_id);
256 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500257 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100261}
262#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100263static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
264 const unsigned char *hash, size_t hash_len,
265 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100266{
Janos Follath24eed8d2019-11-22 13:21:35 +0000267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
269 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200270
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
272 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
273 }
Andres AG72849872017-01-19 11:24:33 +0000274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (sig_len < rsa_len) {
276 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
277 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
280 (unsigned int) hash_len,
281 hash, sig)) != 0) {
282 return ret;
283 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200284
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200285 /* The buffer contains a valid signature followed by extra data.
286 * We have a special error code for that so that so that callers can
287 * use mbedtls_pk_verify() to check "Does the buffer start with a
288 * valid signature?" and not just "Does the buffer contain a valid
289 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (sig_len > rsa_len) {
291 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
292 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200295}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100296#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200297
Jerry Yub02ee182022-03-16 10:30:41 +0800298#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100299int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
300 mbedtls_rsa_context *rsa_ctx,
301 const unsigned char *hash, size_t hash_len,
302 unsigned char *sig, size_t sig_size,
303 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100304{
Neil Armstrong98545682022-02-22 16:12:51 +0100305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
306 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
307 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
308 psa_status_t status;
309 mbedtls_pk_context key;
310 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100311 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100312 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
315 if (sig_size < *sig_len) {
316 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
317 }
Neil Armstrong98545682022-02-22 16:12:51 +0100318
Neil Armstronge4f28682022-02-24 15:41:39 +0100319 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100320 * re-construct one to make it happy */
321 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800322 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
324 if (key_len <= 0) {
325 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
326 }
327 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
328 psa_set_key_algorithm(&attributes, alg);
329 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 status = psa_import_key(&attributes,
332 buf + sizeof(buf) - key_len, key_len,
333 &key_id);
334 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500335 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100336 goto cleanup;
337 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 status = psa_sign_hash(key_id, alg, hash, hash_len,
339 sig, sig_size, sig_len);
340 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500341 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100342 goto cleanup;
343 }
344
345 ret = 0;
346
347cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 status = psa_destroy_key(key_id);
349 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500350 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 }
352 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100353}
Jerry Yu406cf272022-03-22 11:33:42 +0800354#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800355
356#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100357static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
358 const unsigned char *hash, size_t hash_len,
359 unsigned char *sig, size_t sig_size, size_t *sig_len,
360 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800361{
Jerry Yu1d172a32022-03-12 19:12:05 +0800362 ((void) f_rng);
363 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800364
Jerry Yubd1b3272022-03-24 13:05:20 +0800365 psa_algorithm_t psa_md_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
367 if (psa_md_alg == 0) {
368 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
369 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
372 psa_md_alg),
373 ctx, hash, hash_len,
374 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800375}
Neil Armstrong98545682022-02-22 16:12:51 +0100376#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100377static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
378 const unsigned char *hash, size_t hash_len,
379 unsigned char *sig, size_t sig_size, size_t *sig_len,
380 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200381{
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
385 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
386 }
Andres AG72849872017-01-19 11:24:33 +0000387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 *sig_len = mbedtls_rsa_get_len(rsa);
389 if (sig_size < *sig_len) {
390 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
391 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
394 md_alg, (unsigned int) hash_len,
395 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200396}
Neil Armstrong98545682022-02-22 16:12:51 +0100397#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200398
Neil Armstrong18f43c72022-02-09 15:32:45 +0100399#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100400static int rsa_decrypt_wrap(void *ctx,
401 const unsigned char *input, size_t ilen,
402 unsigned char *output, size_t *olen, size_t osize,
403 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100404{
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
407 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
408 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
409 psa_status_t status;
410 mbedtls_pk_context key;
411 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100412 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100413
414 ((void) f_rng);
415 ((void) p_rng);
416
417#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
419 return MBEDTLS_ERR_RSA_INVALID_PADDING;
420 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100421#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (ilen != mbedtls_rsa_get_len(rsa)) {
424 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
425 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100426
427 /* mbedtls_pk_write_key_der() expects a full PK context;
428 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100429 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100430 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
432 if (key_len <= 0) {
433 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
434 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
437 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
438 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 status = psa_import_key(&attributes,
441 buf + sizeof(buf) - key_len, key_len,
442 &key_id);
443 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500444 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100445 goto cleanup;
446 }
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
449 input, ilen,
450 NULL, 0,
451 output, osize, olen);
452 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500453 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100454 goto cleanup;
455 }
456
457 ret = 0;
458
459cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 mbedtls_platform_zeroize(buf, sizeof(buf));
461 status = psa_destroy_key(key_id);
462 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500463 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100467}
468#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100469static int rsa_decrypt_wrap(void *ctx,
470 const unsigned char *input, size_t ilen,
471 unsigned char *output, size_t *olen, size_t osize,
472 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200473{
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (ilen != mbedtls_rsa_get_len(rsa)) {
477 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
478 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
481 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200482}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100483#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200484
Neil Armstrong96a16a42022-02-10 10:40:11 +0100485#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486static int rsa_encrypt_wrap(void *ctx,
487 const unsigned char *input, size_t ilen,
488 unsigned char *output, size_t *olen, size_t osize,
489 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100490{
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
493 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
494 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
495 psa_status_t status;
496 mbedtls_pk_context key;
497 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100498 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100499
500 ((void) f_rng);
501 ((void) p_rng);
502
503#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
505 return MBEDTLS_ERR_RSA_INVALID_PADDING;
506 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100507#endif
508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 if (mbedtls_rsa_get_len(rsa) > osize) {
510 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
511 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100512
Neil Armstrongac014ca2022-02-24 15:27:54 +0100513 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100514 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100515 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100516 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
518 if (key_len <= 0) {
519 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
520 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
523 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
524 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 status = psa_import_key(&attributes,
527 buf + sizeof(buf) - key_len, key_len,
528 &key_id);
529 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500530 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100531 goto cleanup;
532 }
533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
535 input, ilen,
536 NULL, 0,
537 output, osize, olen);
538 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500539 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100540 goto cleanup;
541 }
542
543 ret = 0;
544
545cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 status = psa_destroy_key(key_id);
547 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500548 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100552}
553#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100554static int rsa_encrypt_wrap(void *ctx,
555 const unsigned char *input, size_t ilen,
556 unsigned char *output, size_t *olen, size_t osize,
557 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200558{
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
560 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (*olen > osize) {
563 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
564 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
567 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200568}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100569#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571static int rsa_check_pair_wrap(const void *pub, const void *prv,
572 int (*f_rng)(void *, unsigned char *, size_t),
573 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100574{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200575 (void) f_rng;
576 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub,
578 (const mbedtls_rsa_context *) prv);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100579}
580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200582{
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 if (ctx != NULL) {
586 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
587 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200590}
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200593{
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
595 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200596}
597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200599{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200600#if defined(MBEDTLS_RSA_ALT)
601 /* Not supported */
602 (void) ctx;
603 (void) items;
604#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200606 items->name = "rsa.N";
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 items->value = &(((mbedtls_rsa_context *) ctx)->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200608
609 items++;
610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200611 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200612 items->name = "rsa.E";
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 items->value = &(((mbedtls_rsa_context *) ctx)->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200614#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200615}
616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617const mbedtls_pk_info_t mbedtls_rsa_info = {
618 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200619 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200620 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200621 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200622 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200623 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200624#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200625 NULL,
626 NULL,
627#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200628 rsa_decrypt_wrap,
629 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100630 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200631 rsa_alloc_wrap,
632 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200633#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200634 NULL,
635 NULL,
636#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200637 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200638};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200642/*
643 * Generic EC key
644 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100645static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200646{
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return type == MBEDTLS_PK_ECKEY ||
648 type == MBEDTLS_PK_ECKEY_DH ||
649 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200650}
651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652static size_t eckey_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200653{
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 return ((mbedtls_ecp_keypair *) ctx)->grp.pbits;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200655}
656
Valerio Setti1cdddac2023-02-02 13:55:57 +0100657#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400658#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400659/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500660 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
661 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500662 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
664 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500665{
Janos Follath24eed8d2019-11-22 13:21:35 +0000666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500667 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
670 MBEDTLS_ASN1_INTEGER)) != 0) {
671 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500672 }
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 while (unpadded_len > 0 && **from == 0x00) {
675 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500676 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (unpadded_len > to_len || unpadded_len == 0) {
680 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
681 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500682
Andrzej Kurek9241d182018-11-20 05:04:35 -0500683 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 memset(to, 0x00, padding_len);
685 memcpy(to + padding_len, *from, unpadded_len);
686 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500689}
690
691/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400692 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500693 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500694 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400695 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
697 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400698{
Janos Follath24eed8d2019-11-22 13:21:35 +0000699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500700 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
703 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
704 return ret;
705 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400706
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500707 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
709 return ret;
710 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500711 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
713 return ret;
714 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400717}
718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
720 const unsigned char *hash, size_t hash_len,
721 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400722{
Valerio Settiab363d92023-01-26 14:31:54 +0100723 mbedtls_ecp_keypair *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000724 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200725 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100726 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200727 psa_status_t status;
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200728 size_t key_len;
Valerio Setti5c032b52023-02-02 15:10:32 +0100729 /* This buffer will initially contain the public key and then the signature
730 * but at different points in time. For all curves except secp224k1, which
731 * is not currently supported in PSA, the public key is one byte longer
732 * (header byte + 2 numbers, while the signature is only 2 numbers),
733 * so use that as the buffer size. */
Valerio Setti1337a4f2023-01-30 15:54:55 +0100734 unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Hanno Beckera9851112019-01-25 16:37:10 +0000735 unsigned char *p;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700736 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100737 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100738 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
740 const size_t signature_part_size = (ctx->grp.nbits + 7) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700741 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (curve == 0) {
744 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
745 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
748 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
749 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500750
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200751 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
752 MBEDTLS_ECP_PF_UNCOMPRESSED,
753 &key_len, buf, sizeof(buf));
754 if (ret != 0) {
755 goto cleanup;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500756 }
757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 status = psa_import_key(&attributes,
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200759 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 &key_id);
761 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500762 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400763 goto cleanup;
764 }
765
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500766 /* We don't need the exported key anymore and can
767 * reuse its buffer for signature extraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (2 * signature_part_size > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500769 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
770 goto cleanup;
771 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 p = (unsigned char *) sig;
774 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
775 signature_part_size)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500776 goto cleanup;
777 }
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 status = psa_verify_hash(key_id, psa_sig_md,
780 hash, hash_len,
781 buf, 2 * signature_part_size);
782 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500783 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400785 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500788 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
789 goto cleanup;
790 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400791 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400792
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500793cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 status = psa_destroy_key(key_id);
795 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500796 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400800}
801#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
803 const unsigned char *hash, size_t hash_len,
804 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200805{
Janos Follath24eed8d2019-11-22 13:21:35 +0000806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200807 ((void) md_alg);
808
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx,
810 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200811
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
813 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
814 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200817}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400818#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100819#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200820
Valerio Setti1cdddac2023-02-02 13:55:57 +0100821#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100822#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100823/*
824 * Simultaneously convert and move raw MPI from the beginning of a buffer
825 * to an ASN.1 MPI at the end of the buffer.
826 * See also mbedtls_asn1_write_mpi().
827 *
828 * p: pointer to the end of the output buffer
829 * start: start of the output buffer, and also of the mpi to write at the end
830 * n_len: length of the mpi to read from start
831 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100832static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
833 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100834{
835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
836 size_t len = 0;
837
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 if ((size_t) (*p - start) < n_len) {
839 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
840 }
Neil Armstrong15021652022-03-01 10:14:17 +0100841
842 len = n_len;
843 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100845
846 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
847 * Neither r nor s should be 0, but as a failsafe measure, still detect
848 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100850 ++(*p);
851 --len;
852 }
853
854 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (len == 0) {
856 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
857 }
Neil Armstrong15021652022-03-01 10:14:17 +0100858
859 /* if the msb is 1, ASN.1 requires that we prepend a 0.
860 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (**p & 0x80) {
862 if (*p - start < 1) {
863 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
864 }
Neil Armstrong15021652022-03-01 10:14:17 +0100865
866 *--(*p) = 0x00;
867 len += 1;
868 }
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
871 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
872 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100875}
876
877/* Transcode signature from PSA format to ASN.1 sequence.
878 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
879 * MPIs, and in-place.
880 *
881 * [in/out] sig: the signature pre- and post-transcoding
882 * [in/out] sig_len: signature length pre- and post-transcoding
883 * [int] buf_len: the available size the in/out buffer
884 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
886 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100887{
888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
889 size_t len = 0;
890 const size_t rs_len = *sig_len / 2;
891 unsigned char *p = sig + buf_len;
892
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
894 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
897 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
898 MBEDTLS_ASN1_CONSTRUCTED |
899 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100900
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100902 *sig_len = len;
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100905}
Neil Armstronge9606902022-02-09 14:23:00 +0100906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
908 const unsigned char *hash, size_t hash_len,
909 unsigned char *sig, size_t sig_size, size_t *sig_len,
910 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100911{
Valerio Settiab363d92023-01-26 14:31:54 +0100912 mbedtls_ecp_keypair *ctx = ctx_arg;
Neil Armstronge9606902022-02-09 14:23:00 +0100913 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
915 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
916 psa_status_t status;
Valerio Setti1337a4f2023-01-30 15:54:55 +0100917 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100918#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Valerio Setti5bc52242023-01-30 15:48:28 +0100919 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100920 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100921#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200922 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100923 PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100924#endif
Neil Armstronge9606902022-02-09 14:23:00 +0100925 size_t curve_bits;
926 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200928 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100929
930 /* PSA has its own RNG */
931 ((void) f_rng);
932 ((void) p_rng);
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (curve == 0) {
935 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
936 }
Neil Armstronge9606902022-02-09 14:23:00 +0100937
Gilles Peskine13caa942022-10-04 22:59:26 +0200938 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100939 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200941 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100943 goto cleanup;
944 }
945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
947 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
948 psa_set_key_algorithm(&attributes, psa_sig_md);
949
950 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200951 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 &key_id);
953 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500954 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100956 }
957
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
959 sig, sig_size, sig_len);
960 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500961 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 goto cleanup;
963 }
964
965 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100966
967cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 mbedtls_platform_zeroize(buf, sizeof(buf));
969 status = psa_destroy_key(key_id);
970 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500971 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100975}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100976#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
978 const unsigned char *hash, size_t hash_len,
979 unsigned char *sig, size_t sig_size, size_t *sig_len,
980 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200981{
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
983 md_alg, hash, hash_len,
984 sig, sig_size, sig_len,
985 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200986}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100987#endif /* MBEDTLS_USE_PSA_CRYPTO */
988#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200989
Valerio Setti80d07982023-02-08 13:49:17 +0100990#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +0100991/* Forward declarations */
992static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
993 const unsigned char *hash, size_t hash_len,
994 const unsigned char *sig, size_t sig_len,
995 void *rs_ctx);
996
997static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
998 const unsigned char *hash, size_t hash_len,
999 unsigned char *sig, size_t sig_size, size_t *sig_len,
1000 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1001 void *rs_ctx);
1002
1003/*
1004 * Restart context for ECDSA operations with ECKEY context
1005 *
1006 * We need to store an actual ECDSA context, as we need to pass the same to
1007 * the underlying ecdsa function, so we can't create it on the fly every time.
1008 */
1009typedef struct {
1010 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1011 mbedtls_ecdsa_context ecdsa_ctx;
1012} eckey_restart_ctx;
1013
1014static void *eckey_rs_alloc(void)
1015{
1016 eckey_restart_ctx *rs_ctx;
1017
1018 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1019
1020 if (ctx != NULL) {
1021 rs_ctx = ctx;
1022 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1023 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1024 }
1025
1026 return ctx;
1027}
1028
1029static void eckey_rs_free(void *ctx)
1030{
1031 eckey_restart_ctx *rs_ctx;
1032
1033 if (ctx == NULL) {
1034 return;
1035 }
1036
1037 rs_ctx = ctx;
1038 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1039 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1040
1041 mbedtls_free(ctx);
1042}
1043
1044static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1045 const unsigned char *hash, size_t hash_len,
1046 const unsigned char *sig, size_t sig_len,
1047 void *rs_ctx)
1048{
1049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1050 eckey_restart_ctx *rs = rs_ctx;
1051
1052 /* Should never happen */
1053 if (rs == NULL) {
1054 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1055 }
1056
1057 /* set up our own sub-context if needed (that is, on first run) */
1058 if (rs->ecdsa_ctx.grp.pbits == 0) {
1059 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1060 }
1061
1062 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx,
1063 md_alg, hash, hash_len,
1064 sig, sig_len, &rs->ecdsa_rs));
1065
1066cleanup:
1067 return ret;
1068}
1069
1070static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1071 const unsigned char *hash, size_t hash_len,
1072 unsigned char *sig, size_t sig_size, size_t *sig_len,
1073 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1074 void *rs_ctx)
1075{
1076 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1077 eckey_restart_ctx *rs = rs_ctx;
1078
1079 /* Should never happen */
1080 if (rs == NULL) {
1081 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1082 }
1083
1084 /* set up our own sub-context if needed (that is, on first run) */
1085 if (rs->ecdsa_ctx.grp.pbits == 0) {
1086 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1087 }
1088
1089 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg,
1090 hash, hash_len, sig, sig_size, sig_len,
1091 f_rng, p_rng, &rs->ecdsa_rs));
1092
1093cleanup:
1094 return ret;
1095}
Valerio Setti80d07982023-02-08 13:49:17 +01001096#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001097
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001098#if defined(MBEDTLS_USE_PSA_CRYPTO)
1099/*
1100 * Alternative function used to verify that the EC private/public key pair
1101 * is valid using PSA functions instead of ECP ones.
1102 * The flow is:
1103 * - import the private key "prv" to PSA and export its public part
1104 * - write the raw content of public key "pub" to a local buffer
1105 * - compare the two buffers
1106 */
1107static int eckey_check_pair_psa(const void *pub, const void *prv)
1108{
1109 psa_status_t status;
1110 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
1111 mbedtls_ecp_keypair *prv_ctx = (mbedtls_ecp_keypair *) prv;
1112 mbedtls_ecp_keypair *pub_ctx = (mbedtls_ecp_keypair *) pub;
1113 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1114 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
1115 size_t prv_key_len;
1116 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1117 size_t pub_key_len;
1118 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1119 size_t curve_bits;
1120 psa_ecc_family_t curve =
1121 mbedtls_ecc_group_to_psa(prv_ctx->grp.id, &curve_bits);
1122 size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
1123
1124 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1125 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1126
1127 ret = mbedtls_mpi_write_binary(&prv_ctx->d, prv_key_buf, curve_bytes);
1128 if (ret != 0) {
1129 return ret;
1130 }
1131
1132 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
1133 if (status != PSA_SUCCESS) {
1134 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1135 return ret;
1136 }
1137
1138 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
1139
1140 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1141 &prv_key_len);
1142 if (status != PSA_SUCCESS) {
1143 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1144 status = psa_destroy_key(key_id);
1145 return (status != PSA_SUCCESS) ? PSA_PK_TO_MBEDTLS_ERR(status) : ret;
1146 }
1147
1148 status = psa_destroy_key(key_id);
1149 if (status != PSA_SUCCESS) {
1150 return PSA_PK_TO_MBEDTLS_ERR(status);
1151 }
1152
1153 ret = mbedtls_ecp_point_write_binary(&pub_ctx->grp, &pub_ctx->Q,
1154 MBEDTLS_ECP_PF_UNCOMPRESSED,
1155 &pub_key_len, pub_key_buf,
1156 sizeof(pub_key_buf));
1157 if (ret != 0) {
1158 return ret;
1159 }
1160
1161 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1162 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1163 }
1164
1165 return 0;
1166}
1167#endif /* MBEDTLS_USE_PSA_CRYPTO */
1168
Valerio Setti1cdddac2023-02-02 13:55:57 +01001169static int eckey_check_pair(const void *pub, const void *prv,
1170 int (*f_rng)(void *, unsigned char *, size_t),
1171 void *p_rng)
1172{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001173#if defined(MBEDTLS_USE_PSA_CRYPTO)
1174 (void) f_rng;
1175 (void) p_rng;
1176 return eckey_check_pair_psa((const mbedtls_ecp_keypair *) pub,
1177 (const mbedtls_ecp_keypair *) prv);
1178#else /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001179 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
1180 (const mbedtls_ecp_keypair *) prv,
1181 f_rng, p_rng);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001182#endif /* MBEDTLS_USE_PSA_CRYPTO */
1183 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001184}
1185
1186static void *eckey_alloc_wrap(void)
1187{
1188 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1189
1190 if (ctx != NULL) {
1191 mbedtls_ecp_keypair_init(ctx);
1192 }
1193
1194 return ctx;
1195}
1196
1197static void eckey_free_wrap(void *ctx)
1198{
1199 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1200 mbedtls_free(ctx);
1201}
1202
1203static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
1204{
1205 items->type = MBEDTLS_PK_DEBUG_ECP;
1206 items->name = "eckey.Q";
1207 items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
1208}
1209
1210const mbedtls_pk_info_t mbedtls_eckey_info = {
1211 MBEDTLS_PK_ECKEY,
1212 "EC",
1213 eckey_get_bitlen,
1214 eckey_can_do,
1215#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1216 ecdsa_verify_wrap, /* Compatible key structures */
1217#else
1218 NULL,
1219#endif
1220#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1221 ecdsa_sign_wrap, /* Compatible key structures */
1222#else
1223 NULL,
1224#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001225#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001226 eckey_verify_rs_wrap,
1227 eckey_sign_rs_wrap,
1228#endif
1229 NULL,
1230 NULL,
1231 eckey_check_pair,
1232 eckey_alloc_wrap,
1233 eckey_free_wrap,
1234#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1235 eckey_rs_alloc,
1236 eckey_rs_free,
1237#endif
1238 eckey_debug,
1239};
1240
1241/*
1242 * EC key restricted to ECDH
1243 */
1244static int eckeydh_can_do(mbedtls_pk_type_t type)
1245{
1246 return type == MBEDTLS_PK_ECKEY ||
1247 type == MBEDTLS_PK_ECKEY_DH;
1248}
1249
1250const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1251 MBEDTLS_PK_ECKEY_DH,
1252 "EC_DH",
1253 eckey_get_bitlen, /* Same underlying key structure */
1254 eckeydh_can_do,
1255 NULL,
1256 NULL,
1257#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1258 NULL,
1259 NULL,
1260#endif
1261 NULL,
1262 NULL,
1263 eckey_check_pair,
1264 eckey_alloc_wrap, /* Same underlying key structure */
1265 eckey_free_wrap, /* Same underlying key structure */
1266#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1267 NULL,
1268 NULL,
1269#endif
1270 eckey_debug, /* Same underlying key structure */
1271};
1272#endif /* MBEDTLS_ECP_C */
1273
1274#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1275static int ecdsa_can_do(mbedtls_pk_type_t type)
1276{
1277 return type == MBEDTLS_PK_ECDSA;
1278}
1279
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001280#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001281static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1282 const unsigned char *hash, size_t hash_len,
1283 const unsigned char *sig, size_t sig_len,
1284 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001285{
Janos Follath24eed8d2019-11-22 13:21:35 +00001286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001287 ((void) md_alg);
1288
1289 ret = mbedtls_ecdsa_read_signature_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 (mbedtls_ecdsa_context *) ctx,
1291 hash, hash_len, sig, sig_len,
1292 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1295 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1296 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001299}
1300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1302 const unsigned char *hash, size_t hash_len,
1303 unsigned char *sig, size_t sig_size, size_t *sig_len,
1304 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1305 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001306{
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 return mbedtls_ecdsa_write_signature_restartable(
1308 (mbedtls_ecdsa_context *) ctx,
1309 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1310 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001311
1312}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001315{
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if (ctx != NULL) {
1319 mbedtls_ecdsa_restart_init(ctx);
1320 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001323}
1324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001326{
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 mbedtls_ecdsa_restart_free(ctx);
1328 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001329}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001330#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001332const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1333 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001334 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001335 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001336 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001337#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1338 ecdsa_verify_wrap, /* Compatible key structures */
1339#else
1340 NULL,
1341#endif
1342#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1343 ecdsa_sign_wrap, /* Compatible key structures */
1344#else
1345 NULL,
1346#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001347#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001348 ecdsa_verify_rs_wrap,
1349 ecdsa_sign_rs_wrap,
1350#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001351 NULL,
1352 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001353 eckey_check_pair, /* Compatible key structures */
Valerio Setti24138d92023-01-27 14:24:09 +01001354 eckey_alloc_wrap, /* Compatible key structures */
1355 eckey_free_wrap, /* Compatible key structures */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001356#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001357 ecdsa_rs_alloc,
1358 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001359#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001360 eckey_debug, /* Compatible key structures */
1361};
Valerio Setti7ca13182023-01-27 13:22:42 +01001362#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001364#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001365/*
1366 * Support for alternative RSA-private implementations
1367 */
1368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001370{
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001372}
1373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374static size_t rsa_alt_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001375{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001379}
1380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1382 const unsigned char *hash, size_t hash_len,
1383 unsigned char *sig, size_t sig_size, size_t *sig_len,
1384 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 if (UINT_MAX < hash_len) {
1389 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1390 }
Andres AG72849872017-01-19 11:24:33 +00001391
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1393 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1394 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1395 }
1396 if (*sig_len > sig_size) {
1397 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1398 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001399
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1401 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001402}
1403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404static int rsa_alt_decrypt_wrap(void *ctx,
1405 const unsigned char *input, size_t ilen,
1406 unsigned char *output, size_t *olen, size_t osize,
1407 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001409 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001410
1411 ((void) f_rng);
1412 ((void) p_rng);
1413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1415 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1416 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001417
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 return rsa_alt->decrypt_func(rsa_alt->key,
1419 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001420}
1421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001422#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001423static int rsa_alt_check_pair(const void *pub, const void *prv,
1424 int (*f_rng)(void *, unsigned char *, size_t),
1425 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001426{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001428 unsigned char hash[32];
1429 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001430 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1433 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001434 }
1435
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 memset(hash, 0x2a, sizeof(hash));
1437
1438 if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE,
1439 hash, sizeof(hash),
1440 sig, sizeof(sig), &sig_len,
1441 f_rng, p_rng)) != 0) {
1442 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001443 }
1444
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE,
1446 hash, sizeof(hash), sig, sig_len) != 0) {
1447 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1448 }
1449
1450 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001451}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001455{
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 if (ctx != NULL) {
1459 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1460 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001463}
1464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001466{
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1468 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001469}
1470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001471const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1472 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001473 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001474 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001475 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001476 NULL,
1477 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001478#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001479 NULL,
1480 NULL,
1481#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001482 rsa_alt_decrypt_wrap,
1483 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001484#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001485 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001486#else
1487 NULL,
1488#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001489 rsa_alt_alloc_wrap,
1490 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001491#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001492 NULL,
1493 NULL,
1494#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001495 NULL,
1496};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001499
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001500#if defined(MBEDTLS_USE_PSA_CRYPTO)
1501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502static void *pk_opaque_alloc_wrap(void)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001503{
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t));
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001505
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001506 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 return ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001509}
1510
Gilles Peskine449bd832023-01-11 14:50:10 +01001511static void pk_opaque_free_wrap(void *ctx)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001512{
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t));
1514 mbedtls_free(ctx);
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001515}
1516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517static size_t pk_opaque_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001518{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001519 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001520 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001521 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) {
1524 return 0;
1525 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 bits = psa_get_key_bits(&attributes);
1528 psa_reset_key_attributes(&attributes);
1529 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001530}
1531
Gilles Peskine449bd832023-01-11 14:50:10 +01001532static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001533{
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 return type == MBEDTLS_PK_ECKEY ||
1535 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001536}
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001539{
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 return type == MBEDTLS_PK_RSA ||
1541 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001542}
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1545 const unsigned char *hash, size_t hash_len,
1546 unsigned char *sig, size_t sig_size, size_t *sig_len,
1547 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001548{
Valerio Settiab363d92023-01-26 14:31:54 +01001549#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001550 ((void) ctx);
1551 ((void) md_alg);
1552 ((void) hash);
1553 ((void) hash_len);
1554 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001555 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001556 ((void) sig_len);
1557 ((void) f_rng);
1558 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001560#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001561 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001562 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001563 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001564 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001565 psa_status_t status;
1566
1567 /* PSA has its own RNG */
1568 (void) f_rng;
1569 (void) p_rng;
1570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 status = psa_get_key_attributes(*key, &attributes);
1572 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001573 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001575
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 type = psa_get_key_type(&attributes);
1577 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001578
Valerio Settiab363d92023-01-26 14:31:54 +01001579#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1581 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1582 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001583#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001584#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (PSA_KEY_TYPE_IS_RSA(type)) {
1586 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1587 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001588#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001590
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001591 /* make the signature */
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 status = psa_sign_hash(*key, alg, hash, hash_len,
1593 sig, sig_size, sig_len);
1594 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001595#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001597 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001599#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001600#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001602 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001603 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001604#endif /* MBEDTLS_RSA_C */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001605 return PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001606 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001607
Valerio Settiab363d92023-01-26 14:31:54 +01001608#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001610 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1612 }
Valerio Settiab363d92023-01-26 14:31:54 +01001613#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001614
1615 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001616#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001617}
1618
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001619const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001620 MBEDTLS_PK_OPAQUE,
1621 "Opaque",
1622 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001623 pk_opaque_ecdsa_can_do,
1624 NULL, /* verify - will be done later */
1625 pk_opaque_sign_wrap,
1626#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1627 NULL, /* restartable verify - not relevant */
1628 NULL, /* restartable sign - not relevant */
1629#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001630 NULL, /* decrypt - not relevant */
1631 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001632 NULL, /* check_pair - could be done later or left NULL */
1633 pk_opaque_alloc_wrap,
1634 pk_opaque_free_wrap,
1635#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1636 NULL, /* restart alloc - not relevant */
1637 NULL, /* restart free - not relevant */
1638#endif
1639 NULL, /* debug - could be done later, or even left NULL */
1640};
1641
Neil Armstrong30beca32022-05-03 15:42:13 +02001642#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01001643static int pk_opaque_rsa_decrypt(void *ctx,
1644 const unsigned char *input, size_t ilen,
1645 unsigned char *output, size_t *olen, size_t osize,
1646 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001647{
1648 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1649 psa_status_t status;
1650
1651 /* PSA has its own RNG */
1652 (void) f_rng;
1653 (void) p_rng;
1654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1656 input, ilen,
1657 NULL, 0,
1658 output, osize, olen);
1659 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001660 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001661 }
1662
1663 return 0;
1664}
Neil Armstrong30beca32022-05-03 15:42:13 +02001665#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001666
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001667const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1668 MBEDTLS_PK_OPAQUE,
1669 "Opaque",
1670 pk_opaque_get_bitlen,
1671 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001672 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001673 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001674#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1675 NULL, /* restartable verify - not relevant */
1676 NULL, /* restartable sign - not relevant */
1677#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001678#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001679 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001680#else
1681 NULL, /* decrypt - not available */
1682#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001683 NULL, /* encrypt - will be done later */
1684 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001685 pk_opaque_alloc_wrap,
1686 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001687#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1688 NULL, /* restart alloc - not relevant */
1689 NULL, /* restart free - not relevant */
1690#endif
1691 NULL, /* debug - could be done later, or even left NULL */
1692};
1693
1694#endif /* MBEDTLS_USE_PSA_CRYPTO */
1695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001696#endif /* MBEDTLS_PK_C */