blob: 03962104474318f494c3582be11e36ddb9f0e082 [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;
Neil Armstrong19915c22022-03-01 15:21:02 +0100122 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return mbedtls_pk_error_from_psa(status);
Neil Armstrong19915c22022-03-01 15:21:02 +0100124 }
125}
Neil Armstrong30beca32022-05-03 15:42:13 +0200126#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800127#endif /* MBEDTLS_PSA_CRYPTO_C */
128
129#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu07869e82022-03-16 16:40:50 +0800130#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100131int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800132{
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800134 case PSA_ERROR_NOT_PERMITTED:
135 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800137 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800139 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800141 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800143 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800145 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return mbedtls_pk_error_from_psa(status);
Jerry Yu07869e82022-03-16 16:40:50 +0800147 }
148}
Jerry Yu75339822022-03-23 12:06:31 +0800149#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu75339822022-03-23 12:06:31 +0800150#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500151#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Neil Armstrong19915c22022-03-01 15:21:02 +0100152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100154static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200155{
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return type == MBEDTLS_PK_RSA ||
157 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200158}
159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160static size_t rsa_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200161{
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx;
163 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200164}
165
Neil Armstrong52f41f82022-02-22 15:30:24 +0100166#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100167static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
168 const unsigned char *hash, size_t hash_len,
169 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200170{
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
174 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
175 psa_status_t status;
176 mbedtls_pk_context key;
177 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100178 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100179 psa_algorithm_t psa_alg_md =
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
181 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
184 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
185 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (sig_len < rsa_len) {
188 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
189 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100190
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100191 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100192 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100193 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100194 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
196 if (key_len <= 0) {
197 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
198 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
201 psa_set_key_algorithm(&attributes, psa_alg_md);
202 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 status = psa_import_key(&attributes,
205 buf + sizeof(buf) - key_len, key_len,
206 &key_id);
207 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500208 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100209 goto cleanup;
210 }
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
213 sig, sig_len);
214 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500215 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100216 goto cleanup;
217 }
218 ret = 0;
219
220cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 status = psa_destroy_key(key_id);
222 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500223 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100227}
228#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100229static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
230 const unsigned char *hash, size_t hash_len,
231 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100232{
Janos Follath24eed8d2019-11-22 13:21:35 +0000233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
235 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
238 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
239 }
Andres AG72849872017-01-19 11:24:33 +0000240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if (sig_len < rsa_len) {
242 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
243 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
246 (unsigned int) hash_len,
247 hash, sig)) != 0) {
248 return ret;
249 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200250
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200251 /* The buffer contains a valid signature followed by extra data.
252 * We have a special error code for that so that so that callers can
253 * use mbedtls_pk_verify() to check "Does the buffer start with a
254 * valid signature?" and not just "Does the buffer contain a valid
255 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (sig_len > rsa_len) {
257 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
258 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200261}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100262#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200263
Jerry Yub02ee182022-03-16 10:30:41 +0800264#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100265int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
266 mbedtls_rsa_context *rsa_ctx,
267 const unsigned char *hash, size_t hash_len,
268 unsigned char *sig, size_t sig_size,
269 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100270{
Neil Armstrong98545682022-02-22 16:12:51 +0100271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
272 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
273 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
274 psa_status_t status;
275 mbedtls_pk_context key;
276 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100277 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100278 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
281 if (sig_size < *sig_len) {
282 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
283 }
Neil Armstrong98545682022-02-22 16:12:51 +0100284
Neil Armstronge4f28682022-02-24 15:41:39 +0100285 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100286 * re-construct one to make it happy */
287 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800288 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
290 if (key_len <= 0) {
291 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
292 }
293 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
294 psa_set_key_algorithm(&attributes, alg);
295 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 status = psa_import_key(&attributes,
298 buf + sizeof(buf) - key_len, key_len,
299 &key_id);
300 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500301 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100302 goto cleanup;
303 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 status = psa_sign_hash(key_id, alg, hash, hash_len,
305 sig, sig_size, sig_len);
306 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500307 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100308 goto cleanup;
309 }
310
311 ret = 0;
312
313cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 status = psa_destroy_key(key_id);
315 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500316 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 }
318 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100319}
Jerry Yu406cf272022-03-22 11:33:42 +0800320#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800321
322#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
324 const unsigned char *hash, size_t hash_len,
325 unsigned char *sig, size_t sig_size, size_t *sig_len,
326 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800327{
Jerry Yu1d172a32022-03-12 19:12:05 +0800328 ((void) f_rng);
329 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800330
Jerry Yubd1b3272022-03-24 13:05:20 +0800331 psa_algorithm_t psa_md_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
333 if (psa_md_alg == 0) {
334 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
335 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
338 psa_md_alg),
339 ctx, hash, hash_len,
340 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800341}
Neil Armstrong98545682022-02-22 16:12:51 +0100342#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100343static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
344 const unsigned char *hash, size_t hash_len,
345 unsigned char *sig, size_t sig_size, size_t *sig_len,
346 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200347{
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
351 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
352 }
Andres AG72849872017-01-19 11:24:33 +0000353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 *sig_len = mbedtls_rsa_get_len(rsa);
355 if (sig_size < *sig_len) {
356 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
357 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
360 md_alg, (unsigned int) hash_len,
361 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200362}
Neil Armstrong98545682022-02-22 16:12:51 +0100363#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200364
Neil Armstrong18f43c72022-02-09 15:32:45 +0100365#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100366static int rsa_decrypt_wrap(void *ctx,
367 const unsigned char *input, size_t ilen,
368 unsigned char *output, size_t *olen, size_t osize,
369 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100370{
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
373 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
374 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
375 psa_status_t status;
376 mbedtls_pk_context key;
377 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100378 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100379
380 ((void) f_rng);
381 ((void) p_rng);
382
383#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
385 return MBEDTLS_ERR_RSA_INVALID_PADDING;
386 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100387#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (ilen != mbedtls_rsa_get_len(rsa)) {
390 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
391 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100392
393 /* mbedtls_pk_write_key_der() expects a full PK context;
394 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100395 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100396 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
398 if (key_len <= 0) {
399 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
400 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
403 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
404 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 status = psa_import_key(&attributes,
407 buf + sizeof(buf) - key_len, key_len,
408 &key_id);
409 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500410 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100411 goto cleanup;
412 }
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
415 input, ilen,
416 NULL, 0,
417 output, osize, olen);
418 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500419 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100420 goto cleanup;
421 }
422
423 ret = 0;
424
425cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_platform_zeroize(buf, sizeof(buf));
427 status = psa_destroy_key(key_id);
428 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500429 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100433}
434#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static int rsa_decrypt_wrap(void *ctx,
436 const unsigned char *input, size_t ilen,
437 unsigned char *output, size_t *olen, size_t osize,
438 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200439{
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (ilen != mbedtls_rsa_get_len(rsa)) {
443 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
444 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
447 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200448}
Neil Armstrong18f43c72022-02-09 15:32:45 +0100449#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200450
Neil Armstrong96a16a42022-02-10 10:40:11 +0100451#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100452static int rsa_encrypt_wrap(void *ctx,
453 const unsigned char *input, size_t ilen,
454 unsigned char *output, size_t *olen, size_t osize,
455 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100456{
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
459 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
460 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
461 psa_status_t status;
462 mbedtls_pk_context key;
463 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100464 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100465
466 ((void) f_rng);
467 ((void) p_rng);
468
469#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
471 return MBEDTLS_ERR_RSA_INVALID_PADDING;
472 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100473#endif
474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (mbedtls_rsa_get_len(rsa) > osize) {
476 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
477 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100478
Neil Armstrongac014ca2022-02-24 15:27:54 +0100479 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100480 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100481 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100482 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
484 if (key_len <= 0) {
485 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
486 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
489 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
490 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 status = psa_import_key(&attributes,
493 buf + sizeof(buf) - key_len, key_len,
494 &key_id);
495 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500496 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100497 goto cleanup;
498 }
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
501 input, ilen,
502 NULL, 0,
503 output, osize, olen);
504 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500505 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100506 goto cleanup;
507 }
508
509 ret = 0;
510
511cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 status = psa_destroy_key(key_id);
513 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500514 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100518}
519#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100520static int rsa_encrypt_wrap(void *ctx,
521 const unsigned char *input, size_t ilen,
522 unsigned char *output, size_t *olen, size_t osize,
523 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200524{
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
526 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (*olen > osize) {
529 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
530 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
533 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200534}
Neil Armstrong96a16a42022-02-10 10:40:11 +0100535#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537static int rsa_check_pair_wrap(const void *pub, const void *prv,
538 int (*f_rng)(void *, unsigned char *, size_t),
539 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100540{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200541 (void) f_rng;
542 (void) p_rng;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub,
544 (const mbedtls_rsa_context *) prv);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100545}
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200548{
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (ctx != NULL) {
552 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
553 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200556}
557
Gilles Peskine449bd832023-01-11 14:50:10 +0100558static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200559{
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
561 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200562}
563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564static void rsa_debug(const void *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200565{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200566#if defined(MBEDTLS_RSA_ALT)
567 /* Not supported */
568 (void) ctx;
569 (void) items;
570#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200572 items->name = "rsa.N";
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 items->value = &(((mbedtls_rsa_context *) ctx)->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200574
575 items++;
576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200578 items->name = "rsa.E";
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 items->value = &(((mbedtls_rsa_context *) ctx)->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200580#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200581}
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583const mbedtls_pk_info_t mbedtls_rsa_info = {
584 MBEDTLS_PK_RSA,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200585 "RSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200586 rsa_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200587 rsa_can_do,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200588 rsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200589 rsa_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200590#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200591 NULL,
592 NULL,
593#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200594 rsa_decrypt_wrap,
595 rsa_encrypt_wrap,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100596 rsa_check_pair_wrap,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200597 rsa_alloc_wrap,
598 rsa_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200599#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200600 NULL,
601 NULL,
602#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200603 rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200604};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200605#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200608/*
609 * Generic EC key
610 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100611static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200612{
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 return type == MBEDTLS_PK_ECKEY ||
614 type == MBEDTLS_PK_ECKEY_DH ||
615 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200616}
617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618static size_t eckey_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200619{
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 return ((mbedtls_ecp_keypair *) ctx)->grp.pbits;
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200621}
622
Valerio Setti1cdddac2023-02-02 13:55:57 +0100623#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400624#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400625/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500626 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
627 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500628 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100629static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
630 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500631{
Janos Follath24eed8d2019-11-22 13:21:35 +0000632 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500633 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
636 MBEDTLS_ASN1_INTEGER)) != 0) {
637 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500638 }
639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 while (unpadded_len > 0 && **from == 0x00) {
641 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500642 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500643 }
644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if (unpadded_len > to_len || unpadded_len == 0) {
646 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
647 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500648
Andrzej Kurek9241d182018-11-20 05:04:35 -0500649 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 memset(to, 0x00, padding_len);
651 memcpy(to + padding_len, *from, unpadded_len);
652 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500655}
656
657/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400658 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500659 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500660 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400661 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100662static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
663 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400664{
Janos Follath24eed8d2019-11-22 13:21:35 +0000665 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500666 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
669 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
670 return ret;
671 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400672
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500673 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
675 return ret;
676 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500677 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
679 return ret;
680 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400683}
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
686 const unsigned char *hash, size_t hash_len,
687 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400688{
Valerio Settiab363d92023-01-26 14:31:54 +0100689 mbedtls_ecp_keypair *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000690 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200691 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100692 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200693 psa_status_t status;
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200694 size_t key_len;
Valerio Setti5c032b52023-02-02 15:10:32 +0100695 /* This buffer will initially contain the public key and then the signature
696 * but at different points in time. For all curves except secp224k1, which
697 * is not currently supported in PSA, the public key is one byte longer
698 * (header byte + 2 numbers, while the signature is only 2 numbers),
699 * so use that as the buffer size. */
Valerio Setti1337a4f2023-01-30 15:54:55 +0100700 unsigned char buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Hanno Beckera9851112019-01-25 16:37:10 +0000701 unsigned char *p;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700702 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100703 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100704 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
706 const size_t signature_part_size = (ctx->grp.nbits + 7) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700707 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (curve == 0) {
710 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
711 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
714 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
715 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500716
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200717 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
718 MBEDTLS_ECP_PF_UNCOMPRESSED,
719 &key_len, buf, sizeof(buf));
720 if (ret != 0) {
721 goto cleanup;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500722 }
723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 status = psa_import_key(&attributes,
Gilles Peskineb4a87b02022-10-04 22:54:26 +0200725 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 &key_id);
727 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500728 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400729 goto cleanup;
730 }
731
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500732 /* We don't need the exported key anymore and can
733 * reuse its buffer for signature extraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (2 * signature_part_size > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500735 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
736 goto cleanup;
737 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 p = (unsigned char *) sig;
740 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
741 signature_part_size)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500742 goto cleanup;
743 }
744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 status = psa_verify_hash(key_id, psa_sig_md,
746 hash, hash_len,
747 buf, 2 * signature_part_size);
748 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500749 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400751 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500754 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
755 goto cleanup;
756 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400757 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400758
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500759cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 status = psa_destroy_key(key_id);
761 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500762 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400766}
767#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
769 const unsigned char *hash, size_t hash_len,
770 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200771{
Janos Follath24eed8d2019-11-22 13:21:35 +0000772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200773 ((void) md_alg);
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx,
776 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200777
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
779 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
780 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200783}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400784#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100785#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200786
Valerio Setti1cdddac2023-02-02 13:55:57 +0100787#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100788#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100789/*
790 * Simultaneously convert and move raw MPI from the beginning of a buffer
791 * to an ASN.1 MPI at the end of the buffer.
792 * See also mbedtls_asn1_write_mpi().
793 *
794 * p: pointer to the end of the output buffer
795 * start: start of the output buffer, and also of the mpi to write at the end
796 * n_len: length of the mpi to read from start
797 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
799 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100800{
801 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
802 size_t len = 0;
803
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if ((size_t) (*p - start) < n_len) {
805 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
806 }
Neil Armstrong15021652022-03-01 10:14:17 +0100807
808 len = n_len;
809 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100811
812 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
813 * Neither r nor s should be 0, but as a failsafe measure, still detect
814 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100816 ++(*p);
817 --len;
818 }
819
820 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 if (len == 0) {
822 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
823 }
Neil Armstrong15021652022-03-01 10:14:17 +0100824
825 /* if the msb is 1, ASN.1 requires that we prepend a 0.
826 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (**p & 0x80) {
828 if (*p - start < 1) {
829 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
830 }
Neil Armstrong15021652022-03-01 10:14:17 +0100831
832 *--(*p) = 0x00;
833 len += 1;
834 }
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
837 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
838 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100841}
842
843/* Transcode signature from PSA format to ASN.1 sequence.
844 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
845 * MPIs, and in-place.
846 *
847 * [in/out] sig: the signature pre- and post-transcoding
848 * [in/out] sig_len: signature length pre- and post-transcoding
849 * [int] buf_len: the available size the in/out buffer
850 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
852 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100853{
854 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
855 size_t len = 0;
856 const size_t rs_len = *sig_len / 2;
857 unsigned char *p = sig + buf_len;
858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
860 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
863 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
864 MBEDTLS_ASN1_CONSTRUCTED |
865 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100868 *sig_len = len;
869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100871}
Neil Armstronge9606902022-02-09 14:23:00 +0100872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
874 const unsigned char *hash, size_t hash_len,
875 unsigned char *sig, size_t sig_size, size_t *sig_len,
876 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100877{
Valerio Settiab363d92023-01-26 14:31:54 +0100878 mbedtls_ecp_keypair *ctx = ctx_arg;
Neil Armstronge9606902022-02-09 14:23:00 +0100879 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
880 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
881 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
882 psa_status_t status;
Valerio Setti1337a4f2023-01-30 15:54:55 +0100883 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100884#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Valerio Setti5bc52242023-01-30 15:48:28 +0100885 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100886 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100887#else
Gilles Peskine13caa942022-10-04 22:59:26 +0200888 psa_algorithm_t psa_sig_md =
Valerio Settib761b152023-01-31 14:56:04 +0100889 PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +0100890#endif
Neil Armstronge9606902022-02-09 14:23:00 +0100891 size_t curve_bits;
892 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200894 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +0100895
896 /* PSA has its own RNG */
897 ((void) f_rng);
898 ((void) p_rng);
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (curve == 0) {
901 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
902 }
Neil Armstronge9606902022-02-09 14:23:00 +0100903
Gilles Peskine13caa942022-10-04 22:59:26 +0200904 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100905 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200907 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100909 goto cleanup;
910 }
911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
913 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
914 psa_set_key_algorithm(&attributes, psa_sig_md);
915
916 status = psa_import_key(&attributes,
Gilles Peskine13caa942022-10-04 22:59:26 +0200917 buf, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100918 &key_id);
919 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500920 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100922 }
923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
925 sig, sig_size, sig_len);
926 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500927 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 goto cleanup;
929 }
930
931 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +0100932
933cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 mbedtls_platform_zeroize(buf, sizeof(buf));
935 status = psa_destroy_key(key_id);
936 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500937 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100941}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100942#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +0100943static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
944 const unsigned char *hash, size_t hash_len,
945 unsigned char *sig, size_t sig_size, size_t *sig_len,
946 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200947{
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
949 md_alg, hash, hash_len,
950 sig, sig_size, sig_len,
951 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200952}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100953#endif /* MBEDTLS_USE_PSA_CRYPTO */
954#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200955
Valerio Setti80d07982023-02-08 13:49:17 +0100956#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +0100957/* Forward declarations */
958static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
959 const unsigned char *hash, size_t hash_len,
960 const unsigned char *sig, size_t sig_len,
961 void *rs_ctx);
962
963static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
964 const unsigned char *hash, size_t hash_len,
965 unsigned char *sig, size_t sig_size, size_t *sig_len,
966 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
967 void *rs_ctx);
968
969/*
970 * Restart context for ECDSA operations with ECKEY context
971 *
972 * We need to store an actual ECDSA context, as we need to pass the same to
973 * the underlying ecdsa function, so we can't create it on the fly every time.
974 */
975typedef struct {
976 mbedtls_ecdsa_restart_ctx ecdsa_rs;
977 mbedtls_ecdsa_context ecdsa_ctx;
978} eckey_restart_ctx;
979
980static void *eckey_rs_alloc(void)
981{
982 eckey_restart_ctx *rs_ctx;
983
984 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
985
986 if (ctx != NULL) {
987 rs_ctx = ctx;
988 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
989 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
990 }
991
992 return ctx;
993}
994
995static void eckey_rs_free(void *ctx)
996{
997 eckey_restart_ctx *rs_ctx;
998
999 if (ctx == NULL) {
1000 return;
1001 }
1002
1003 rs_ctx = ctx;
1004 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1005 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1006
1007 mbedtls_free(ctx);
1008}
1009
1010static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1011 const unsigned char *hash, size_t hash_len,
1012 const unsigned char *sig, size_t sig_len,
1013 void *rs_ctx)
1014{
1015 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1016 eckey_restart_ctx *rs = rs_ctx;
1017
1018 /* Should never happen */
1019 if (rs == NULL) {
1020 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1021 }
1022
1023 /* set up our own sub-context if needed (that is, on first run) */
1024 if (rs->ecdsa_ctx.grp.pbits == 0) {
1025 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1026 }
1027
1028 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx,
1029 md_alg, hash, hash_len,
1030 sig, sig_len, &rs->ecdsa_rs));
1031
1032cleanup:
1033 return ret;
1034}
1035
1036static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1037 const unsigned char *hash, size_t hash_len,
1038 unsigned char *sig, size_t sig_size, size_t *sig_len,
1039 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1040 void *rs_ctx)
1041{
1042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1043 eckey_restart_ctx *rs = rs_ctx;
1044
1045 /* Should never happen */
1046 if (rs == NULL) {
1047 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1048 }
1049
1050 /* set up our own sub-context if needed (that is, on first run) */
1051 if (rs->ecdsa_ctx.grp.pbits == 0) {
1052 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
1053 }
1054
1055 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg,
1056 hash, hash_len, sig, sig_size, sig_len,
1057 f_rng, p_rng, &rs->ecdsa_rs));
1058
1059cleanup:
1060 return ret;
1061}
Valerio Setti80d07982023-02-08 13:49:17 +01001062#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001063
1064static int eckey_check_pair(const void *pub, const void *prv,
1065 int (*f_rng)(void *, unsigned char *, size_t),
1066 void *p_rng)
1067{
1068 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
1069 (const mbedtls_ecp_keypair *) prv,
1070 f_rng, p_rng);
1071}
1072
1073static void *eckey_alloc_wrap(void)
1074{
1075 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1076
1077 if (ctx != NULL) {
1078 mbedtls_ecp_keypair_init(ctx);
1079 }
1080
1081 return ctx;
1082}
1083
1084static void eckey_free_wrap(void *ctx)
1085{
1086 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1087 mbedtls_free(ctx);
1088}
1089
1090static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
1091{
1092 items->type = MBEDTLS_PK_DEBUG_ECP;
1093 items->name = "eckey.Q";
1094 items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
1095}
1096
1097const mbedtls_pk_info_t mbedtls_eckey_info = {
1098 MBEDTLS_PK_ECKEY,
1099 "EC",
1100 eckey_get_bitlen,
1101 eckey_can_do,
1102#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1103 ecdsa_verify_wrap, /* Compatible key structures */
1104#else
1105 NULL,
1106#endif
1107#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1108 ecdsa_sign_wrap, /* Compatible key structures */
1109#else
1110 NULL,
1111#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001112#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001113 eckey_verify_rs_wrap,
1114 eckey_sign_rs_wrap,
1115#endif
1116 NULL,
1117 NULL,
1118 eckey_check_pair,
1119 eckey_alloc_wrap,
1120 eckey_free_wrap,
1121#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1122 eckey_rs_alloc,
1123 eckey_rs_free,
1124#endif
1125 eckey_debug,
1126};
1127
1128/*
1129 * EC key restricted to ECDH
1130 */
1131static int eckeydh_can_do(mbedtls_pk_type_t type)
1132{
1133 return type == MBEDTLS_PK_ECKEY ||
1134 type == MBEDTLS_PK_ECKEY_DH;
1135}
1136
1137const mbedtls_pk_info_t mbedtls_eckeydh_info = {
1138 MBEDTLS_PK_ECKEY_DH,
1139 "EC_DH",
1140 eckey_get_bitlen, /* Same underlying key structure */
1141 eckeydh_can_do,
1142 NULL,
1143 NULL,
1144#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1145 NULL,
1146 NULL,
1147#endif
1148 NULL,
1149 NULL,
1150 eckey_check_pair,
1151 eckey_alloc_wrap, /* Same underlying key structure */
1152 eckey_free_wrap, /* Same underlying key structure */
1153#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1154 NULL,
1155 NULL,
1156#endif
1157 eckey_debug, /* Same underlying key structure */
1158};
1159#endif /* MBEDTLS_ECP_C */
1160
1161#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1162static int ecdsa_can_do(mbedtls_pk_type_t type)
1163{
1164 return type == MBEDTLS_PK_ECDSA;
1165}
1166
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001167#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001168static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1169 const unsigned char *hash, size_t hash_len,
1170 const unsigned char *sig, size_t sig_len,
1171 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001172{
Janos Follath24eed8d2019-11-22 13:21:35 +00001173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001174 ((void) md_alg);
1175
1176 ret = mbedtls_ecdsa_read_signature_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 (mbedtls_ecdsa_context *) ctx,
1178 hash, hash_len, sig, sig_len,
1179 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001180
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1182 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1183 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001184
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001186}
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1189 const unsigned char *hash, size_t hash_len,
1190 unsigned char *sig, size_t sig_size, size_t *sig_len,
1191 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1192 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001193{
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 return mbedtls_ecdsa_write_signature_restartable(
1195 (mbedtls_ecdsa_context *) ctx,
1196 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1197 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001198
1199}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001202{
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 if (ctx != NULL) {
1206 mbedtls_ecdsa_restart_init(ctx);
1207 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001210}
1211
Gilles Peskine449bd832023-01-11 14:50:10 +01001212static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001213{
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 mbedtls_ecdsa_restart_free(ctx);
1215 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001216}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001217#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001219const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1220 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001221 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001222 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001223 ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001224#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
1225 ecdsa_verify_wrap, /* Compatible key structures */
1226#else
1227 NULL,
1228#endif
1229#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
1230 ecdsa_sign_wrap, /* Compatible key structures */
1231#else
1232 NULL,
1233#endif
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001234#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001235 ecdsa_verify_rs_wrap,
1236 ecdsa_sign_rs_wrap,
1237#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001238 NULL,
1239 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001240 eckey_check_pair, /* Compatible key structures */
Valerio Setti24138d92023-01-27 14:24:09 +01001241 eckey_alloc_wrap, /* Compatible key structures */
1242 eckey_free_wrap, /* Compatible key structures */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001243#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001244 ecdsa_rs_alloc,
1245 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001246#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001247 eckey_debug, /* Compatible key structures */
1248};
Valerio Setti7ca13182023-01-27 13:22:42 +01001249#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001252/*
1253 * Support for alternative RSA-private implementations
1254 */
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001257{
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001259}
1260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261static size_t rsa_alt_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001262{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001266}
1267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1269 const unsigned char *hash, size_t hash_len,
1270 unsigned char *sig, size_t sig_size, size_t *sig_len,
1271 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (UINT_MAX < hash_len) {
1276 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1277 }
Andres AG72849872017-01-19 11:24:33 +00001278
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1280 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1281 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1282 }
1283 if (*sig_len > sig_size) {
1284 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1285 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001286
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1288 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001289}
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291static int rsa_alt_decrypt_wrap(void *ctx,
1292 const unsigned char *input, size_t ilen,
1293 unsigned char *output, size_t *olen, size_t osize,
1294 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001295{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001296 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001297
1298 ((void) f_rng);
1299 ((void) p_rng);
1300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1302 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1303 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 return rsa_alt->decrypt_func(rsa_alt->key,
1306 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001307}
1308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001310static int rsa_alt_check_pair(const void *pub, const void *prv,
1311 int (*f_rng)(void *, unsigned char *, size_t),
1312 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001313{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001314 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001315 unsigned char hash[32];
1316 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1320 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001321 }
1322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 memset(hash, 0x2a, sizeof(hash));
1324
1325 if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE,
1326 hash, sizeof(hash),
1327 sig, sizeof(sig), &sig_len,
1328 f_rng, p_rng)) != 0) {
1329 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001330 }
1331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE,
1333 hash, sizeof(hash), sig, sig_len) != 0) {
1334 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1335 }
1336
1337 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001338}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001342{
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (ctx != NULL) {
1346 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1347 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001350}
1351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001353{
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1355 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356}
1357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001358const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1359 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001360 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001361 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001362 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001363 NULL,
1364 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001365#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001366 NULL,
1367 NULL,
1368#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001369 rsa_alt_decrypt_wrap,
1370 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001372 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001373#else
1374 NULL,
1375#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001376 rsa_alt_alloc_wrap,
1377 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001378#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001379 NULL,
1380 NULL,
1381#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001382 NULL,
1383};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001386
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001387#if defined(MBEDTLS_USE_PSA_CRYPTO)
1388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389static void *pk_opaque_alloc_wrap(void)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001390{
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t));
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001392
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001393 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 return ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001396}
1397
Gilles Peskine449bd832023-01-11 14:50:10 +01001398static void pk_opaque_free_wrap(void *ctx)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001399{
Gilles Peskine449bd832023-01-11 14:50:10 +01001400 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t));
1401 mbedtls_free(ctx);
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001402}
1403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404static size_t pk_opaque_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001405{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001406 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001407 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001408 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) {
1411 return 0;
1412 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001413
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 bits = psa_get_key_bits(&attributes);
1415 psa_reset_key_attributes(&attributes);
1416 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001417}
1418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001420{
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 return type == MBEDTLS_PK_ECKEY ||
1422 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001423}
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001426{
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 return type == MBEDTLS_PK_RSA ||
1428 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001429}
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1432 const unsigned char *hash, size_t hash_len,
1433 unsigned char *sig, size_t sig_size, size_t *sig_len,
1434 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001435{
Valerio Settiab363d92023-01-26 14:31:54 +01001436#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001437 ((void) ctx);
1438 ((void) md_alg);
1439 ((void) hash);
1440 ((void) hash_len);
1441 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001442 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001443 ((void) sig_len);
1444 ((void) f_rng);
1445 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001447#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001448 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001449 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001450 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001451 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001452 psa_status_t status;
1453
1454 /* PSA has its own RNG */
1455 (void) f_rng;
1456 (void) p_rng;
1457
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 status = psa_get_key_attributes(*key, &attributes);
1459 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001460 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 type = psa_get_key_type(&attributes);
1464 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001465
Valerio Settiab363d92023-01-26 14:31:54 +01001466#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1468 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1469 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001470#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001471#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (PSA_KEY_TYPE_IS_RSA(type)) {
1473 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1474 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001475#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001477
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001478 /* make the signature */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 status = psa_sign_hash(*key, alg, hash, hash_len,
1480 sig, sig_size, sig_len);
1481 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001482#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001483 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001484 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001486#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001487#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001489 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001491#endif /* MBEDTLS_RSA_C */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001492 return PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001493 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001494
Valerio Settiab363d92023-01-26 14:31:54 +01001495#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001497 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1499 }
Valerio Settiab363d92023-01-26 14:31:54 +01001500#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001501
1502 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001503#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001504}
1505
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001506const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001507 MBEDTLS_PK_OPAQUE,
1508 "Opaque",
1509 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001510 pk_opaque_ecdsa_can_do,
1511 NULL, /* verify - will be done later */
1512 pk_opaque_sign_wrap,
1513#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1514 NULL, /* restartable verify - not relevant */
1515 NULL, /* restartable sign - not relevant */
1516#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001517 NULL, /* decrypt - not relevant */
1518 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001519 NULL, /* check_pair - could be done later or left NULL */
1520 pk_opaque_alloc_wrap,
1521 pk_opaque_free_wrap,
1522#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1523 NULL, /* restart alloc - not relevant */
1524 NULL, /* restart free - not relevant */
1525#endif
1526 NULL, /* debug - could be done later, or even left NULL */
1527};
1528
Neil Armstrong30beca32022-05-03 15:42:13 +02001529#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01001530static int pk_opaque_rsa_decrypt(void *ctx,
1531 const unsigned char *input, size_t ilen,
1532 unsigned char *output, size_t *olen, size_t osize,
1533 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001534{
1535 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1536 psa_status_t status;
1537
1538 /* PSA has its own RNG */
1539 (void) f_rng;
1540 (void) p_rng;
1541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1543 input, ilen,
1544 NULL, 0,
1545 output, osize, olen);
1546 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001547 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001548 }
1549
1550 return 0;
1551}
Neil Armstrong30beca32022-05-03 15:42:13 +02001552#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001553
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001554const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1555 MBEDTLS_PK_OPAQUE,
1556 "Opaque",
1557 pk_opaque_get_bitlen,
1558 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001559 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001560 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001561#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1562 NULL, /* restartable verify - not relevant */
1563 NULL, /* restartable sign - not relevant */
1564#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001565#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001566 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001567#else
1568 NULL, /* decrypt - not available */
1569#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001570 NULL, /* encrypt - will be done later */
1571 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001572 pk_opaque_alloc_wrap,
1573 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001574#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1575 NULL, /* restart alloc - not relevant */
1576 NULL, /* restart free - not relevant */
1577#endif
1578 NULL, /* debug - could be done later, or even left NULL */
1579};
1580
1581#endif /* MBEDTLS_USE_PSA_CRYPTO */
1582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_PK_C */