blob: 0e5e12049a3dd8405efb425b1545614826a0477d [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
valerio38992cb2023-04-20 09:56:30 +0200194static size_t rsa_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200195{
valerio38992cb2023-04-20 09:56:30 +0200196 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 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)
valerio38992cb2023-04-20 09:56:30 +0200201static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 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{
valerio38992cb2023-04-20 09:56:30 +0200205 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_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;
valerio38992cb2023-04-20 09:56:30 +0200228 key.pk_ctx = rsa;
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
valerio38992cb2023-04-20 09:56:30 +0200263static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 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;
valerio38992cb2023-04-20 09:56:30 +0200268 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 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)
valerio38992cb2023-04-20 09:56:30 +0200357static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 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),
valerio38992cb2023-04-20 09:56:30 +0200373 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800375}
Neil Armstrong98545682022-02-22 16:12:51 +0100376#else
valerio38992cb2023-04-20 09:56:30 +0200377static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 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{
valerio38992cb2023-04-20 09:56:30 +0200382 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_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)
valerio38992cb2023-04-20 09:56:30 +0200400static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 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{
valerio38992cb2023-04-20 09:56:30 +0200405 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_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;
valerio38992cb2023-04-20 09:56:30 +0200430 key.pk_ctx = rsa;
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
valerio38992cb2023-04-20 09:56:30 +0200469static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 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{
valerio38992cb2023-04-20 09:56:30 +0200474 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_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)
valerio38992cb2023-04-20 09:56:30 +0200486static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 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{
valerio38992cb2023-04-20 09:56:30 +0200491 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_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;
valerio38992cb2023-04-20 09:56:30 +0200516 key.pk_ctx = rsa;
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
valerio38992cb2023-04-20 09:56:30 +0200554static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 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{
valerio38992cb2023-04-20 09:56:30 +0200559 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 *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
valerio38992cb2023-04-20 09:56:30 +0200571static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 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;
valerio38992cb2023-04-20 09:56:30 +0200577 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
578 (const mbedtls_rsa_context *) prv->pk_ctx);
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
valerio38992cb2023-04-20 09:56:30 +0200598static void rsa_debug(mbedtls_pk_context *pk, 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 */
valerio38992cb2023-04-20 09:56:30 +0200602 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200603 (void) items;
604#else
valerio38992cb2023-04-20 09:56:30 +0200605 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200608 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200609 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200610
611 items++;
612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200614 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200615 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200616#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200617}
618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200619const mbedtls_pk_info_t mbedtls_rsa_info = {
620 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200621 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200622 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200623 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200624 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200625 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200626#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200627 NULL,
628 NULL,
629#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200630 rsa_decrypt_wrap,
631 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100632 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200633 rsa_alloc_wrap,
634 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200635#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200636 NULL,
637 NULL,
638#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200639 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200640};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200641#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200642
Valerio Setti0d2980f2023-04-05 18:17:13 +0200643#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200644/*
645 * Generic EC key
646 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200648{
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 return type == MBEDTLS_PK_ECKEY ||
650 type == MBEDTLS_PK_ECKEY_DH ||
651 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200652}
653
valerio38992cb2023-04-20 09:56:30 +0200654static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200655{
valerio38992cb2023-04-20 09:56:30 +0200656 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
657 return ecp->grp.pbits;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200658}
659
Valerio Setti1cdddac2023-02-02 13:55:57 +0100660#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400661#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400662/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500663 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
664 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
667 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500668{
Janos Follath24eed8d2019-11-22 13:21:35 +0000669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500670 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
673 MBEDTLS_ASN1_INTEGER)) != 0) {
674 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500675 }
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 while (unpadded_len > 0 && **from == 0x00) {
678 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500679 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500680 }
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (unpadded_len > to_len || unpadded_len == 0) {
683 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
684 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500685
Andrzej Kurek9241d182018-11-20 05:04:35 -0500686 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 memset(to, 0x00, padding_len);
688 memcpy(to + padding_len, *from, unpadded_len);
689 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500692}
693
694/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400695 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500696 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500697 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400698 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
700 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400701{
Janos Follath24eed8d2019-11-22 13:21:35 +0000702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500703 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
706 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
707 return ret;
708 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400709
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500710 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
712 return ret;
713 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500714 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
716 return ret;
717 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500718
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400720}
721
valerio38992cb2023-04-20 09:56:30 +0200722static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
723 mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 const unsigned char *hash, size_t hash_len,
725 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400726{
valerio38992cb2023-04-20 09:56:30 +0200727 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
Janos Follath24eed8d2019-11-22 13:21:35 +0000728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200729 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100730 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200731 psa_status_t status;
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200732 size_t key_len;
Valerio Setti5c032b52023-02-02 15:10:32 +0100733 /* This buffer will initially contain the public key and then the signature
734 * but at different points in time. For all curves except secp224k1, which
735 * is not currently supported in PSA, the public key is one byte longer
736 * (header byte + 2 numbers, while the signature is only 2 numbers),
737 * so use that as the buffer size. */
Valerio Setti1337a4f2023-01-30 15:54:55 +0100738 unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Hanno Beckera9851112019-01-25 16:37:10 +0000739 unsigned char *p;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700740 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100741 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100742 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
744 const size_t signature_part_size = (ctx->grp.nbits + 7) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700745 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 if (curve == 0) {
748 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
749 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
752 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
753 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500754
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200755 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
756 MBEDTLS_ECP_PF_UNCOMPRESSED,
757 &key_len, buf, sizeof(buf));
758 if (ret != 0) {
759 goto cleanup;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500760 }
761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 status = psa_import_key(&attributes,
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200763 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 &key_id);
765 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500766 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400767 goto cleanup;
768 }
769
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500770 /* We don't need the exported key anymore and can
771 * reuse its buffer for signature extraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (2 * signature_part_size > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500773 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
774 goto cleanup;
775 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 p = (unsigned char *) sig;
778 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
779 signature_part_size)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500780 goto cleanup;
781 }
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 status = psa_verify_hash(key_id, psa_sig_md,
784 hash, hash_len,
785 buf, 2 * signature_part_size);
786 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500787 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400789 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500792 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
793 goto cleanup;
794 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400795 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400796
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500797cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 status = psa_destroy_key(key_id);
799 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500800 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400804}
805#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200806static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 const unsigned char *hash, size_t hash_len,
808 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200809{
Janos Follath24eed8d2019-11-22 13:21:35 +0000810 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200811 ((void) md_alg);
812
valerio38992cb2023-04-20 09:56:30 +0200813 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
817 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
818 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200821}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400822#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100823#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200824
Valerio Setti1cdddac2023-02-02 13:55:57 +0100825#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100826#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100827/*
828 * Simultaneously convert and move raw MPI from the beginning of a buffer
829 * to an ASN.1 MPI at the end of the buffer.
830 * See also mbedtls_asn1_write_mpi().
831 *
832 * p: pointer to the end of the output buffer
833 * start: start of the output buffer, and also of the mpi to write at the end
834 * n_len: length of the mpi to read from start
835 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100836static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
837 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100838{
839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
840 size_t len = 0;
841
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 if ((size_t) (*p - start) < n_len) {
843 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
844 }
Neil Armstrong15021652022-03-01 10:14:17 +0100845
846 len = n_len;
847 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100849
850 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
851 * Neither r nor s should be 0, but as a failsafe measure, still detect
852 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100854 ++(*p);
855 --len;
856 }
857
858 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (len == 0) {
860 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
861 }
Neil Armstrong15021652022-03-01 10:14:17 +0100862
863 /* if the msb is 1, ASN.1 requires that we prepend a 0.
864 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if (**p & 0x80) {
866 if (*p - start < 1) {
867 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
868 }
Neil Armstrong15021652022-03-01 10:14:17 +0100869
870 *--(*p) = 0x00;
871 len += 1;
872 }
873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
875 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
876 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100877
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100879}
880
881/* Transcode signature from PSA format to ASN.1 sequence.
882 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
883 * MPIs, and in-place.
884 *
885 * [in/out] sig: the signature pre- and post-transcoding
886 * [in/out] sig_len: signature length pre- and post-transcoding
887 * [int] buf_len: the available size the in/out buffer
888 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100889static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
890 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100891{
892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
893 size_t len = 0;
894 const size_t rs_len = *sig_len / 2;
895 unsigned char *p = sig + buf_len;
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
898 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
901 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
902 MBEDTLS_ASN1_CONSTRUCTED |
903 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100904
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100906 *sig_len = len;
907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100909}
Neil Armstronge9606902022-02-09 14:23:00 +0100910
valerio38992cb2023-04-20 09:56:30 +0200911static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 const unsigned char *hash, size_t hash_len,
913 unsigned char *sig, size_t sig_size, size_t *sig_len,
914 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100915{
valerio38992cb2023-04-20 09:56:30 +0200916 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
Neil Armstronge9606902022-02-09 14:23:00 +0100917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
918 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
919 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
920 psa_status_t status;
Valerio Setti1337a4f2023-01-30 15:54:55 +0100921 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100922#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Valerio Setti5bc52242023-01-30 15:48:28 +0100923 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100924 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100925#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200926 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100927 PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100928#endif
Neil Armstronge9606902022-02-09 14:23:00 +0100929 size_t curve_bits;
930 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200932 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100933
934 /* PSA has its own RNG */
935 ((void) f_rng);
936 ((void) p_rng);
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (curve == 0) {
939 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
940 }
Neil Armstronge9606902022-02-09 14:23:00 +0100941
Gilles Peskine13caa942022-10-04 22:59:26 +0200942 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100943 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200945 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100947 goto cleanup;
948 }
949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
951 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
952 psa_set_key_algorithm(&attributes, psa_sig_md);
953
954 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200955 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 &key_id);
957 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500958 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100960 }
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
963 sig, sig_size, sig_len);
964 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500965 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 goto cleanup;
967 }
968
969 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100970
971cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 mbedtls_platform_zeroize(buf, sizeof(buf));
973 status = psa_destroy_key(key_id);
974 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500975 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100977
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100979}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100980#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200981static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 const unsigned char *hash, size_t hash_len,
983 unsigned char *sig, size_t sig_size, size_t *sig_len,
984 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200985{
valerio38992cb2023-04-20 09:56:30 +0200986 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 md_alg, hash, hash_len,
988 sig, sig_size, sig_len,
989 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200990}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100991#endif /* MBEDTLS_USE_PSA_CRYPTO */
992#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200993
Valerio Setti80d07982023-02-08 13:49:17 +0100994#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +0100995/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +0200996static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +0100997 const unsigned char *hash, size_t hash_len,
998 const unsigned char *sig, size_t sig_len,
999 void *rs_ctx);
1000
valerio38992cb2023-04-20 09:56:30 +02001001static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001002 const unsigned char *hash, size_t hash_len,
1003 unsigned char *sig, size_t sig_size, size_t *sig_len,
1004 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1005 void *rs_ctx);
1006
1007/*
1008 * Restart context for ECDSA operations with ECKEY context
1009 *
1010 * We need to store an actual ECDSA context, as we need to pass the same to
1011 * the underlying ecdsa function, so we can't create it on the fly every time.
1012 */
1013typedef struct {
1014 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1015 mbedtls_ecdsa_context ecdsa_ctx;
1016} eckey_restart_ctx;
1017
1018static void *eckey_rs_alloc(void)
1019{
1020 eckey_restart_ctx *rs_ctx;
1021
1022 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1023
1024 if (ctx != NULL) {
1025 rs_ctx = ctx;
1026 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1027 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1028 }
1029
1030 return ctx;
1031}
1032
1033static void eckey_rs_free(void *ctx)
1034{
1035 eckey_restart_ctx *rs_ctx;
1036
1037 if (ctx == NULL) {
1038 return;
1039 }
1040
1041 rs_ctx = ctx;
1042 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1043 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1044
1045 mbedtls_free(ctx);
1046}
1047
valerio38992cb2023-04-20 09:56:30 +02001048static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001049 const unsigned char *hash, size_t hash_len,
1050 const unsigned char *sig, size_t sig_len,
1051 void *rs_ctx)
1052{
1053 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1054 eckey_restart_ctx *rs = rs_ctx;
1055
1056 /* Should never happen */
1057 if (rs == NULL) {
1058 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1059 }
1060
1061 /* set up our own sub-context if needed (that is, on first run) */
1062 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001063 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001064 }
1065
valerio38992cb2023-04-20 09:56:30 +02001066 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001067 md_alg, hash, hash_len,
1068 sig, sig_len, &rs->ecdsa_rs));
1069
1070cleanup:
1071 return ret;
1072}
1073
valerio38992cb2023-04-20 09:56:30 +02001074static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001075 const unsigned char *hash, size_t hash_len,
1076 unsigned char *sig, size_t sig_size, size_t *sig_len,
1077 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1078 void *rs_ctx)
1079{
1080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1081 eckey_restart_ctx *rs = rs_ctx;
1082
1083 /* Should never happen */
1084 if (rs == NULL) {
1085 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1086 }
1087
1088 /* set up our own sub-context if needed (that is, on first run) */
1089 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001090 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001091 }
1092
valerio38992cb2023-04-20 09:56:30 +02001093 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001094 hash, hash_len, sig, sig_size, sig_len,
1095 f_rng, p_rng, &rs->ecdsa_rs));
1096
1097cleanup:
1098 return ret;
1099}
Valerio Setti80d07982023-02-08 13:49:17 +01001100#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001101
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001102#if defined(MBEDTLS_USE_PSA_CRYPTO)
1103/*
1104 * Alternative function used to verify that the EC private/public key pair
1105 * is valid using PSA functions instead of ECP ones.
1106 * The flow is:
1107 * - import the private key "prv" to PSA and export its public part
1108 * - write the raw content of public key "pub" to a local buffer
1109 * - compare the two buffers
1110 */
valerio38992cb2023-04-20 09:56:30 +02001111static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001112{
Valerio Setti1df94f82023-04-07 08:59:24 +02001113 psa_status_t status, destruction_status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001114 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
valerio38992cb2023-04-20 09:56:30 +02001115 mbedtls_ecp_keypair *prv_ctx = prv->pk_ctx;
1116 mbedtls_ecp_keypair *pub_ctx = pub->pk_ctx;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001118 /* We are using MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH for the size of this
1119 * buffer because it will be used to hold the private key at first and
1120 * then its public part (but not at the same time). */
1121 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001122 size_t prv_key_len;
1123 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1124 size_t pub_key_len;
1125 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1126 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001127 const psa_ecc_family_t curve =
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001128 mbedtls_ecc_group_to_psa(prv_ctx->grp.id, &curve_bits);
Valerio Settif2866642023-04-06 16:49:54 +02001129 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001130
1131 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1132 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1133
1134 ret = mbedtls_mpi_write_binary(&prv_ctx->d, prv_key_buf, curve_bytes);
1135 if (ret != 0) {
1136 return ret;
1137 }
1138
1139 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti1df94f82023-04-07 08:59:24 +02001140 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1141 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001142 return ret;
1143 }
1144
1145 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
1146
Valerio Setti1df94f82023-04-07 08:59:24 +02001147 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1148 &prv_key_len);
1149 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1150 destruction_status = psa_destroy_key(key_id);
1151 if (ret != 0) {
1152 return ret;
1153 } else if (destruction_status != PSA_SUCCESS) {
1154 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001155 }
1156
1157 ret = mbedtls_ecp_point_write_binary(&pub_ctx->grp, &pub_ctx->Q,
1158 MBEDTLS_ECP_PF_UNCOMPRESSED,
1159 &pub_key_len, pub_key_buf,
1160 sizeof(pub_key_buf));
1161 if (ret != 0) {
1162 return ret;
1163 }
1164
1165 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1166 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1167 }
1168
1169 return 0;
1170}
1171#endif /* MBEDTLS_USE_PSA_CRYPTO */
1172
valerio38992cb2023-04-20 09:56:30 +02001173static int eckey_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001174 int (*f_rng)(void *, unsigned char *, size_t),
1175 void *p_rng)
1176{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001177#if defined(MBEDTLS_USE_PSA_CRYPTO)
1178 (void) f_rng;
1179 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001180 return eckey_check_pair_psa(pub, prv);
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001181#elif defined(MBEDTLS_ECP_C)
valerio38992cb2023-04-20 09:56:30 +02001182 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1183 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001184 f_rng, p_rng);
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001185#else
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001186 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti3f8d23e2023-04-07 11:48:02 +02001187#endif
Valerio Setti1cdddac2023-02-02 13:55:57 +01001188}
1189
1190static void *eckey_alloc_wrap(void)
1191{
1192 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1193
1194 if (ctx != NULL) {
1195 mbedtls_ecp_keypair_init(ctx);
1196 }
1197
1198 return ctx;
1199}
1200
1201static void eckey_free_wrap(void *ctx)
1202{
1203 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1204 mbedtls_free(ctx);
1205}
1206
valerio38992cb2023-04-20 09:56:30 +02001207static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001208{
valerio38992cb2023-04-20 09:56:30 +02001209 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001210 items->type = MBEDTLS_PK_DEBUG_ECP;
1211 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001212 items->value = &(ecp->Q);
Valerio Setti1cdddac2023-02-02 13:55:57 +01001213}
1214
1215const mbedtls_pk_info_t mbedtls_eckey_info = {
1216 MBEDTLS_PK_ECKEY,
1217 "EC",
1218 eckey_get_bitlen,
1219 eckey_can_do,
1220#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1221 ecdsa_verify_wrap, /* Compatible key structures */
1222#else
1223 NULL,
1224#endif
1225#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1226 ecdsa_sign_wrap, /* Compatible key structures */
1227#else
1228 NULL,
1229#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001230#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001231 eckey_verify_rs_wrap,
1232 eckey_sign_rs_wrap,
1233#endif
1234 NULL,
1235 NULL,
1236 eckey_check_pair,
1237 eckey_alloc_wrap,
1238 eckey_free_wrap,
1239#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1240 eckey_rs_alloc,
1241 eckey_rs_free,
1242#endif
1243 eckey_debug,
1244};
1245
1246/*
1247 * EC key restricted to ECDH
1248 */
1249static int eckeydh_can_do(mbedtls_pk_type_t type)
1250{
1251 return type == MBEDTLS_PK_ECKEY ||
1252 type == MBEDTLS_PK_ECKEY_DH;
1253}
1254
1255const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1256 MBEDTLS_PK_ECKEY_DH,
1257 "EC_DH",
1258 eckey_get_bitlen, /* Same underlying key structure */
1259 eckeydh_can_do,
1260 NULL,
1261 NULL,
1262#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1263 NULL,
1264 NULL,
1265#endif
1266 NULL,
1267 NULL,
1268 eckey_check_pair,
1269 eckey_alloc_wrap, /* Same underlying key structure */
1270 eckey_free_wrap, /* Same underlying key structure */
1271#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1272 NULL,
1273 NULL,
1274#endif
1275 eckey_debug, /* Same underlying key structure */
1276};
Valerio Setti0d2980f2023-04-05 18:17:13 +02001277#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001278
1279#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1280static int ecdsa_can_do(mbedtls_pk_type_t type)
1281{
1282 return type == MBEDTLS_PK_ECDSA;
1283}
1284
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001285#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001286static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 const unsigned char *hash, size_t hash_len,
1288 const unsigned char *sig, size_t sig_len,
1289 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001290{
Janos Follath24eed8d2019-11-22 13:21:35 +00001291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001292 ((void) md_alg);
1293
1294 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001295 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 hash, hash_len, sig, sig_len,
1297 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1300 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1301 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001302
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001304}
1305
valerio38992cb2023-04-20 09:56:30 +02001306static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 const unsigned char *hash, size_t hash_len,
1308 unsigned char *sig, size_t sig_size, size_t *sig_len,
1309 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1310 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001311{
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001313 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1315 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001316
1317}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001320{
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (ctx != NULL) {
1324 mbedtls_ecdsa_restart_init(ctx);
1325 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001328}
1329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001331{
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 mbedtls_ecdsa_restart_free(ctx);
1333 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001334}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001335#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1338 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001339 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001340 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001341 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001342#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1343 ecdsa_verify_wrap, /* Compatible key structures */
1344#else
1345 NULL,
1346#endif
1347#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1348 ecdsa_sign_wrap, /* Compatible key structures */
1349#else
1350 NULL,
1351#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001352#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001353 ecdsa_verify_rs_wrap,
1354 ecdsa_sign_rs_wrap,
1355#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001356 NULL,
1357 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001358 eckey_check_pair, /* Compatible key structures */
Valerio Setti24138d92023-01-27 14:24:09 +01001359 eckey_alloc_wrap, /* Compatible key structures */
1360 eckey_free_wrap, /* Compatible key structures */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001361#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001362 ecdsa_rs_alloc,
1363 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001364#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001365 eckey_debug, /* Compatible key structures */
1366};
Valerio Setti7ca13182023-01-27 13:22:42 +01001367#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001369#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001370/*
1371 * Support for alternative RSA-private implementations
1372 */
1373
Gilles Peskine449bd832023-01-11 14:50:10 +01001374static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001375{
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001377}
1378
valerio38992cb2023-04-20 09:56:30 +02001379static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001380{
valerio38992cb2023-04-20 09:56:30 +02001381 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001384}
1385
valerio38992cb2023-04-20 09:56:30 +02001386static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001387 const unsigned char *hash, size_t hash_len,
1388 unsigned char *sig, size_t sig_size, size_t *sig_len,
1389 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001390{
valerio38992cb2023-04-20 09:56:30 +02001391 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if (UINT_MAX < hash_len) {
1394 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1395 }
Andres AG72849872017-01-19 11:24:33 +00001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1398 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1399 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1400 }
1401 if (*sig_len > sig_size) {
1402 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1403 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1406 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001407}
1408
valerio38992cb2023-04-20 09:56:30 +02001409static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 const unsigned char *input, size_t ilen,
1411 unsigned char *output, size_t *olen, size_t osize,
1412 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001413{
valerio38992cb2023-04-20 09:56:30 +02001414 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001415
1416 ((void) f_rng);
1417 ((void) p_rng);
1418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1420 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1421 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001422
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 return rsa_alt->decrypt_func(rsa_alt->key,
1424 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001425}
1426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001428static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 int (*f_rng)(void *, unsigned char *, size_t),
1430 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001431{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001433 unsigned char hash[32];
1434 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001436
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1438 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001439 }
1440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 memset(hash, 0x2a, sizeof(hash));
1442
valerio38992cb2023-04-20 09:56:30 +02001443 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 hash, sizeof(hash),
1445 sig, sizeof(sig), &sig_len,
1446 f_rng, p_rng)) != 0) {
1447 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001448 }
1449
valerio38992cb2023-04-20 09:56:30 +02001450 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 hash, sizeof(hash), sig, sig_len) != 0) {
1452 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1453 }
1454
1455 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001456}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001460{
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 if (ctx != NULL) {
1464 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1465 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001468}
1469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001471{
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1473 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001474}
1475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001476const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1477 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001478 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001479 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001480 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001481 NULL,
1482 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001483#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001484 NULL,
1485 NULL,
1486#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001487 rsa_alt_decrypt_wrap,
1488 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001489#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001490 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001491#else
1492 NULL,
1493#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001494 rsa_alt_alloc_wrap,
1495 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001496#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001497 NULL,
1498 NULL,
1499#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001500 NULL,
1501};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001504
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001505#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +02001506static size_t pk_opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001507{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001508 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001509 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001510
Valerio Setti4f387ef2023-05-02 14:15:59 +02001511 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 return 0;
1513 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001514
Gilles Peskine449bd832023-01-11 14:50:10 +01001515 bits = psa_get_key_bits(&attributes);
1516 psa_reset_key_attributes(&attributes);
1517 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001518}
1519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001521{
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 return type == MBEDTLS_PK_ECKEY ||
1523 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001524}
1525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001527{
Gilles Peskine449bd832023-01-11 14:50:10 +01001528 return type == MBEDTLS_PK_RSA ||
1529 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001530}
1531
valerio38992cb2023-04-20 09:56:30 +02001532static int pk_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001533 const unsigned char *hash, size_t hash_len,
1534 unsigned char *sig, size_t sig_size, size_t *sig_len,
1535 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001536{
Valerio Settiab363d92023-01-26 14:31:54 +01001537#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001538 ((void) pk);
John Durkopf35069a2020-08-17 22:05:14 -07001539 ((void) md_alg);
1540 ((void) hash);
1541 ((void) hash_len);
1542 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001543 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001544 ((void) sig_len);
1545 ((void) f_rng);
1546 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001547 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001548#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001549 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001550 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001551 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001552 psa_status_t status;
1553
1554 /* PSA has its own RNG */
1555 (void) f_rng;
1556 (void) p_rng;
1557
Valerio Setti4f387ef2023-05-02 14:15:59 +02001558 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001560 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 type = psa_get_key_type(&attributes);
1564 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001565
Valerio Settiab363d92023-01-26 14:31:54 +01001566#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1568 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1569 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001570#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001571#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 if (PSA_KEY_TYPE_IS_RSA(type)) {
1573 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1574 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001575#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001576 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001577
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001578 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001579 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 sig, sig_size, sig_len);
1581 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001582#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001584 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001586#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001587#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001589 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001591#endif /* MBEDTLS_RSA_C */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001592 return PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001593 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001594
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)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001597 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1599 }
Valerio Settiab363d92023-01-26 14:31:54 +01001600#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001601
1602 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001603#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001604}
1605
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001606const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001607 MBEDTLS_PK_OPAQUE,
1608 "Opaque",
1609 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001610 pk_opaque_ecdsa_can_do,
1611 NULL, /* verify - will be done later */
1612 pk_opaque_sign_wrap,
1613#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1614 NULL, /* restartable verify - not relevant */
1615 NULL, /* restartable sign - not relevant */
1616#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001617 NULL, /* decrypt - not relevant */
1618 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001619 NULL, /* check_pair - could be done later or left NULL */
Valerio Settie00954d2023-04-28 15:24:32 +02001620 NULL, /* alloc - no need to allocate new data dynamically */
1621 NULL, /* free - as for the alloc, there is no data to free */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001622#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1623 NULL, /* restart alloc - not relevant */
1624 NULL, /* restart free - not relevant */
1625#endif
1626 NULL, /* debug - could be done later, or even left NULL */
1627};
1628
Neil Armstrong30beca32022-05-03 15:42:13 +02001629#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
valerio38992cb2023-04-20 09:56:30 +02001630static int pk_opaque_rsa_decrypt(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 const unsigned char *input, size_t ilen,
1632 unsigned char *output, size_t *olen, size_t osize,
1633 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001634{
Neil Armstrong10828182022-04-22 15:02:27 +02001635 psa_status_t status;
1636
1637 /* PSA has its own RNG */
1638 (void) f_rng;
1639 (void) p_rng;
1640
Valerio Setti4f387ef2023-05-02 14:15:59 +02001641 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 input, ilen,
1643 NULL, 0,
1644 output, osize, olen);
1645 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001646 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001647 }
1648
1649 return 0;
1650}
Neil Armstrong30beca32022-05-03 15:42:13 +02001651#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001652
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001653const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1654 MBEDTLS_PK_OPAQUE,
1655 "Opaque",
1656 pk_opaque_get_bitlen,
1657 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001658 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001659 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001660#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1661 NULL, /* restartable verify - not relevant */
1662 NULL, /* restartable sign - not relevant */
1663#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001664#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001665 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001666#else
1667 NULL, /* decrypt - not available */
1668#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001669 NULL, /* encrypt - will be done later */
1670 NULL, /* check_pair - could be done later or left NULL */
Valerio Settie00954d2023-04-28 15:24:32 +02001671 NULL, /* alloc - no need to allocate new data dynamically */
1672 NULL, /* free - as for the alloc, there is no data to free */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001673#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1674 NULL, /* restart alloc - not relevant */
1675 NULL, /* restart free - not relevant */
1676#endif
1677 NULL, /* debug - could be done later, or even left NULL */
1678};
1679
1680#endif /* MBEDTLS_USE_PSA_CRYPTO */
1681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_PK_C */