blob: 6e6a6c2b7b4089d682d51c003b9f9f3296b34144 [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000023#include "pk_wrap.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000024#include "mbedtls/error.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020025
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020026/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020028
Rich Evans00ab4702015-02-06 13:43:58 +000029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020033#endif
34
Neil Armstrong0f49f832022-02-28 15:07:38 +010035#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
36#include "pkwrite.h"
37#endif
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020041#endif
42
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "mbedtls/asn1write.h"
45#endif
46
Andres Amaya Garciae32df082017-10-25 09:37:04 +010047#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050048#include "mbedtls/platform_util.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010049#endif
50
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010051#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040052#include "psa/crypto.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010053#include "mbedtls/psa_util.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040054#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +020055#include "hash_info.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010056#endif
57
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000058#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020059
Andres AG72849872017-01-19 11:24:33 +000060#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010061#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020062
Jerry Yub02ee182022-03-16 10:30:41 +080063#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010064int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010065{
Gilles Peskine449bd832023-01-11 14:50:10 +010066 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010067 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010068 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010069 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010070 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010071 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010073 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010074 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010075 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010076 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010077 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010078 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010079 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010080 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010081 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010083 case PSA_ERROR_COMMUNICATION_FAILURE:
84 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010086 case PSA_ERROR_DATA_CORRUPT:
87 case PSA_ERROR_DATA_INVALID:
88 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010089 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010090 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010091 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +010092 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010094 }
95}
96
Neil Armstrong30beca32022-05-03 15:42:13 +020097#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
98 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +010099int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +0100100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +0100102 case PSA_ERROR_NOT_PERMITTED:
103 case PSA_ERROR_INVALID_ARGUMENT:
104 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +0100106 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +0100108 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100110 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100112 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Neil Armstrong19915c22022-03-01 15:21:02 +0100114 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return mbedtls_pk_error_from_psa(status);
Neil Armstrong19915c22022-03-01 15:21:02 +0100116 }
117}
Neil Armstrong30beca32022-05-03 15:42:13 +0200118#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Jerry Yu07869e82022-03-16 16:40:50 +0800119
120#endif /* MBEDTLS_PSA_CRYPTO_C */
121
122#if defined(MBEDTLS_USE_PSA_CRYPTO)
123
124#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100125int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800126{
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800128 case PSA_ERROR_NOT_PERMITTED:
129 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800131 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800133 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800135 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800137 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800139 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return mbedtls_pk_error_from_psa(status);
Jerry Yu07869e82022-03-16 16:40:50 +0800141 }
142}
Jerry Yu75339822022-03-23 12:06:31 +0800143#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu07869e82022-03-16 16:40:50 +0800144
Jerry Yu75339822022-03-23 12:06:31 +0800145#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong19915c22022-03-01 15:21:02 +0100146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200149{
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 return type == MBEDTLS_PK_RSA ||
151 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200152}
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154static size_t rsa_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200155{
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) ctx;
157 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200158}
159
Neil Armstrong52f41f82022-02-22 15:30:24 +0100160#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100161static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
162 const unsigned char *hash, size_t hash_len,
163 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200164{
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
167 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
168 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
169 psa_status_t status;
170 mbedtls_pk_context key;
171 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100172 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100173 psa_algorithm_t psa_alg_md =
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
175 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100176
177#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
179 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
180 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100181#endif /* SIZE_MAX > UINT_MAX */
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if (sig_len < rsa_len) {
184 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
185 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100186
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100187 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100188 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100189 key.pk_info = &mbedtls_rsa_info;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100190 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
192 if (key_len <= 0) {
193 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
194 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
197 psa_set_key_algorithm(&attributes, psa_alg_md);
198 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 status = psa_import_key(&attributes,
201 buf + sizeof(buf) - key_len, key_len,
202 &key_id);
203 if (status != PSA_SUCCESS) {
204 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100205 goto cleanup;
206 }
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
209 sig, sig_len);
210 if (status != PSA_SUCCESS) {
211 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100212 goto cleanup;
213 }
214 ret = 0;
215
216cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 status = psa_destroy_key(key_id);
218 if (ret == 0 && status != PSA_SUCCESS) {
219 ret = mbedtls_pk_error_from_psa(status);
220 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100223}
224#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100225static int rsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
226 const unsigned char *hash, size_t hash_len,
227 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100228{
Janos Follath24eed8d2019-11-22 13:21:35 +0000229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
231 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200232
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100233#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
235 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
236 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100237#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 if (sig_len < rsa_len) {
240 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
241 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
244 (unsigned int) hash_len,
245 hash, sig)) != 0) {
246 return ret;
247 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200248
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200249 /* The buffer contains a valid signature followed by extra data.
250 * We have a special error code for that so that so that callers can
251 * use mbedtls_pk_verify() to check "Does the buffer start with a
252 * valid signature?" and not just "Does the buffer contain a valid
253 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (sig_len > rsa_len) {
255 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
256 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200259}
Neil Armstrong52f41f82022-02-22 15:30:24 +0100260#endif
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200261
Jerry Yub02ee182022-03-16 10:30:41 +0800262#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100263int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
264 mbedtls_rsa_context *rsa_ctx,
265 const unsigned char *hash, size_t hash_len,
266 unsigned char *sig, size_t sig_size,
267 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100268{
Neil Armstrong98545682022-02-22 16:12:51 +0100269 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
270 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
271 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
272 psa_status_t status;
273 mbedtls_pk_context key;
274 int key_len;
Neil Armstrong4b1a0592022-02-25 08:58:12 +0100275 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong98545682022-02-22 16:12:51 +0100276 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
279 if (sig_size < *sig_len) {
280 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
281 }
Neil Armstrong98545682022-02-22 16:12:51 +0100282
Neil Armstronge4f28682022-02-24 15:41:39 +0100283 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100284 * re-construct one to make it happy */
285 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800286 key.pk_ctx = rsa_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
288 if (key_len <= 0) {
289 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
290 }
291 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
292 psa_set_key_algorithm(&attributes, alg);
293 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 status = psa_import_key(&attributes,
296 buf + sizeof(buf) - key_len, key_len,
297 &key_id);
298 if (status != PSA_SUCCESS) {
299 ret = mbedtls_pk_error_from_psa(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100300 goto cleanup;
301 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 status = psa_sign_hash(key_id, alg, hash, hash_len,
303 sig, sig_size, sig_len);
304 if (status != PSA_SUCCESS) {
305 ret = mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100306 goto cleanup;
307 }
308
309 ret = 0;
310
311cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 status = psa_destroy_key(key_id);
313 if (ret == 0 && status != PSA_SUCCESS) {
314 ret = mbedtls_pk_error_from_psa(status);
315 }
316 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100317}
Jerry Yu406cf272022-03-22 11:33:42 +0800318#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800319
320#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100321static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
322 const unsigned char *hash, size_t hash_len,
323 unsigned char *sig, size_t sig_size, size_t *sig_len,
324 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800325{
Jerry Yu1d172a32022-03-12 19:12:05 +0800326 ((void) f_rng);
327 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800328
Jerry Yubd1b3272022-03-24 13:05:20 +0800329 psa_algorithm_t psa_md_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
331 if (psa_md_alg == 0) {
332 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
333 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
336 psa_md_alg),
337 ctx, hash, hash_len,
338 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800339}
Neil Armstrong98545682022-02-22 16:12:51 +0100340#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100341static int rsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
342 const unsigned char *hash, size_t hash_len,
343 unsigned char *sig, size_t sig_size, size_t *sig_len,
344 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200345{
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100347
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100348#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
350 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
351 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100352#endif /* SIZE_MAX > UINT_MAX */
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) {
410 ret = mbedtls_pk_error_from_psa(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) {
419 ret = mbedtls_pk_error_from_psa_rsa(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) {
429 ret = mbedtls_pk_error_from_psa(status);
430 }
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) {
496 ret = mbedtls_pk_error_from_psa(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) {
505 ret = mbedtls_pk_error_from_psa_rsa(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) {
514 ret = mbedtls_pk_error_from_psa(status);
515 }
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 Settiab363d92023-01-26 14:31:54 +0100623#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) || defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200624/* Forward declarations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100625static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
626 const unsigned char *hash, size_t hash_len,
627 const unsigned char *sig, size_t sig_len);
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
630 const unsigned char *hash, size_t hash_len,
631 unsigned char *sig, size_t sig_size, size_t *sig_len,
632 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634static int eckey_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
635 const unsigned char *hash, size_t hash_len,
636 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200637{
Janos Follath24eed8d2019-11-22 13:21:35 +0000638 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Settiab363d92023-01-26 14:31:54 +0100639 mbedtls_ecp_keypair *keypair_ctx = ctx;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200640
Valerio Settiab363d92023-01-26 14:31:54 +0100641 ret = ecdsa_verify_wrap(keypair_ctx, md_alg, hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return ret;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200644}
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646static int eckey_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
647 const unsigned char *hash, size_t hash_len,
648 unsigned char *sig, size_t sig_size, size_t *sig_len,
649 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200650{
Janos Follath24eed8d2019-11-22 13:21:35 +0000651 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Settiab363d92023-01-26 14:31:54 +0100652 mbedtls_ecp_keypair *keypair_ctx = ctx;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200653
Valerio Settiab363d92023-01-26 14:31:54 +0100654 ret = ecdsa_sign_wrap(keypair_ctx, md_alg, hash, hash_len,
655 sig, sig_size, sig_len,
656 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 return ret;
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200659}
660
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200661#if defined(MBEDTLS_ECP_RESTARTABLE)
662/* Forward declarations */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
664 const unsigned char *hash, size_t hash_len,
665 const unsigned char *sig, size_t sig_len,
666 void *rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
669 const unsigned char *hash, size_t hash_len,
670 unsigned char *sig, size_t sig_size, size_t *sig_len,
671 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
672 void *rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200673
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200674/*
675 * Restart context for ECDSA operations with ECKEY context
676 *
677 * We need to store an actual ECDSA context, as we need to pass the same to
678 * the underlying ecdsa function, so we can't create it on the fly every time.
679 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100680typedef struct {
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200681 mbedtls_ecdsa_restart_ctx ecdsa_rs;
682 mbedtls_ecdsa_context ecdsa_ctx;
683} eckey_restart_ctx;
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685static void *eckey_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200686{
687 eckey_restart_ctx *rs_ctx;
688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200690
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 if (ctx != NULL) {
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200692 rs_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
694 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200695 }
696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200698}
699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700static void eckey_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200701{
702 eckey_restart_ctx *rs_ctx;
703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 if (ctx == NULL) {
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200705 return;
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200707
708 rs_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
710 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200711
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200713}
714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715static int eckey_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
716 const unsigned char *hash, size_t hash_len,
717 const unsigned char *sig, size_t sig_len,
718 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200719{
Janos Follath24eed8d2019-11-22 13:21:35 +0000720 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200721 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200722
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200723 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (rs == NULL) {
725 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
726 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200727
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200728 /* set up our own sub-context if needed (that is, on first run) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if (rs->ecdsa_ctx.grp.pbits == 0) {
730 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
731 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(&rs->ecdsa_ctx,
734 md_alg, hash, hash_len,
735 sig, sig_len, &rs->ecdsa_rs));
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200736
737cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200739}
740
Gilles Peskine449bd832023-01-11 14:50:10 +0100741static int eckey_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
742 const unsigned char *hash, size_t hash_len,
743 unsigned char *sig, size_t sig_size, size_t *sig_len,
744 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
745 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200746{
Janos Follath24eed8d2019-11-22 13:21:35 +0000747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200748 eckey_restart_ctx *rs = rs_ctx;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200749
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +0200750 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (rs == NULL) {
752 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
753 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200754
Manuel Pégourié-Gonnardee68cff2018-10-15 15:27:49 +0200755 /* set up our own sub-context if needed (that is, on first run) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (rs->ecdsa_ctx.grp.pbits == 0) {
757 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, ctx));
758 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(&rs->ecdsa_ctx, md_alg,
761 hash, hash_len, sig, sig_size, sig_len,
762 f_rng, p_rng, &rs->ecdsa_rs));
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200763
764cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200766}
767#endif /* MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770static int eckey_check_pair(const void *pub, const void *prv,
771 int (*f_rng)(void *, unsigned char *, size_t),
772 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100773{
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub,
775 (const mbedtls_ecp_keypair *) prv,
776 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100777}
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779static void *eckey_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200780{
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (ctx != NULL) {
784 mbedtls_ecp_keypair_init(ctx);
785 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200788}
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790static void eckey_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200791{
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
793 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200794}
795
Gilles Peskine449bd832023-01-11 14:50:10 +0100796static void eckey_debug(const void *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200797{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798 items->type = MBEDTLS_PK_DEBUG_ECP;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200799 items->name = "eckey.Q";
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 items->value = &(((mbedtls_ecp_keypair *) ctx)->Q);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200801}
802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200803const mbedtls_pk_info_t mbedtls_eckey_info = {
804 MBEDTLS_PK_ECKEY,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200805 "EC",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200806 eckey_get_bitlen,
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200807 eckey_can_do,
Valerio Settiab363d92023-01-26 14:31:54 +0100808#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) || defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200809 eckey_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200810 eckey_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200811#if defined(MBEDTLS_ECP_RESTARTABLE)
812 eckey_verify_rs_wrap,
813 eckey_sign_rs_wrap,
814#endif
Valerio Settiab363d92023-01-26 14:31:54 +0100815#else /* MBEDTLS_PK_CAN_ECDSA_SIGN || MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200816 NULL,
817 NULL,
Valerio Settiab363d92023-01-26 14:31:54 +0100818#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN || MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200819 NULL,
820 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100821 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200822 eckey_alloc_wrap,
823 eckey_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200824#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200825 eckey_rs_alloc,
826 eckey_rs_free,
827#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200828 eckey_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200829};
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200830
831/*
Paul Bakker75342a62014-04-08 17:35:40 +0200832 * EC key restricted to ECDH
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200833 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100834static int eckeydh_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200835{
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 return type == MBEDTLS_PK_ECKEY ||
837 type == MBEDTLS_PK_ECKEY_DH;
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200838}
839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200840const mbedtls_pk_info_t mbedtls_eckeydh_info = {
841 MBEDTLS_PK_ECKEY_DH,
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200842 "EC_DH",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +0200843 eckey_get_bitlen, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200844 eckeydh_can_do,
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200845 NULL,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200846 NULL,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200847#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200848 NULL,
849 NULL,
850#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200851 NULL,
852 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100853 eckey_check_pair,
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200854 eckey_alloc_wrap, /* Same underlying key structure */
855 eckey_free_wrap, /* Same underlying key structure */
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200856#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200857 NULL,
858 NULL,
859#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200860 eckey_debug, /* Same underlying key structure */
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200861};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200863
Valerio Settiab363d92023-01-26 14:31:54 +0100864#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN) || defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100865static int ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200866{
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 return type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200868}
869
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400870#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400871/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500872 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
873 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500874 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
876 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500877{
Janos Follath24eed8d2019-11-22 13:21:35 +0000878 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500879 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500880
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
882 MBEDTLS_ASN1_INTEGER)) != 0) {
883 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500884 }
885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 while (unpadded_len > 0 && **from == 0x00) {
887 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500888 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500889 }
890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (unpadded_len > to_len || unpadded_len == 0) {
892 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
893 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500894
Andrzej Kurek9241d182018-11-20 05:04:35 -0500895 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 memset(to, 0x00, padding_len);
897 memcpy(to + padding_len, *from, unpadded_len);
898 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500901}
902
903/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400904 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500905 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500906 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400907 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100908static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
909 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400910{
Janos Follath24eed8d2019-11-22 13:21:35 +0000911 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500912 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500913
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
915 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
916 return ret;
917 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400918
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500919 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
921 return ret;
922 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500923 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
925 return ret;
926 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400929}
930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931static int ecdsa_verify_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
932 const unsigned char *hash, size_t hash_len,
933 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400934{
Valerio Settiab363d92023-01-26 14:31:54 +0100935 mbedtls_ecp_keypair *ctx = ctx_arg;
Janos Follath24eed8d2019-11-22 13:21:35 +0000936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200937 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100938 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200939 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400940 mbedtls_pk_context key;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400941 int key_len;
Neil Armstrong0f49f832022-02-28 15:07:38 +0100942 unsigned char buf[MBEDTLS_PK_ECP_PUB_DER_MAX_BYTES];
Hanno Beckera9851112019-01-25 16:37:10 +0000943 unsigned char *p;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400944 mbedtls_pk_info_t pk_info = mbedtls_eckey_info;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700945 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Gilles Peskinefc2459d2019-12-12 17:50:44 +0100946 size_t curve_bits;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100947 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
949 const size_t signature_part_size = (ctx->grp.nbits + 7) / 8;
John Durkop2ec2eaa2020-08-24 18:29:15 -0700950 ((void) md_alg);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 if (curve == 0) {
Valerio Settiab363d92023-01-26 14:31:54 +0100953 fprintf(stderr, "unkown curve!\n");
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
955 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500956
Hanno Beckerccf574e2019-01-29 08:26:15 +0000957 /* mbedtls_pk_write_pubkey() expects a full PK context;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500958 * re-construct one to make it happy */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400959 key.pk_info = &pk_info;
960 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 p = buf + sizeof(buf);
962 key_len = mbedtls_pk_write_pubkey(&p, buf, &key);
963 if (key_len <= 0) {
964 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
965 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
968 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
969 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 status = psa_import_key(&attributes,
972 buf + sizeof(buf) - key_len, key_len,
973 &key_id);
974 if (status != PSA_SUCCESS) {
975 ret = mbedtls_pk_error_from_psa(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400976 goto cleanup;
977 }
978
Andrzej Kurekeeac03b2018-11-20 06:39:06 -0500979 /* We don't need the exported key anymore and can
980 * reuse its buffer for signature extraction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (2 * signature_part_size > sizeof(buf)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500982 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
983 goto cleanup;
984 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 p = (unsigned char *) sig;
987 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, buf,
988 signature_part_size)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500989 goto cleanup;
990 }
991
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 status = psa_verify_hash(key_id, psa_sig_md,
993 hash, hash_len,
994 buf, 2 * signature_part_size);
995 if (status != PSA_SUCCESS) {
996 ret = mbedtls_pk_error_from_psa_ecdsa(status);
997 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400998 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -05001001 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1002 goto cleanup;
1003 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001004 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001005
Andrzej Kurekb7b04782018-11-19 17:01:16 -05001006cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 status = psa_destroy_key(key_id);
1008 if (ret == 0 && status != PSA_SUCCESS) {
1009 ret = mbedtls_pk_error_from_psa(status);
1010 }
Neil Armstrong9dccd862022-02-24 15:33:13 +01001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001013}
1014#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001015static int ecdsa_verify_wrap(void *ctx, mbedtls_md_type_t md_alg,
1016 const unsigned char *hash, size_t hash_len,
1017 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001018{
Janos Follath24eed8d2019-11-22 13:21:35 +00001019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +02001020 ((void) md_alg);
1021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) ctx,
1023 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1026 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1027 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +02001028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001030}
Andrzej Kurek8b036a62018-10-31 05:16:46 -04001031#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001032
Neil Armstronge9606902022-02-09 14:23:00 +01001033#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +01001034/*
1035 * Simultaneously convert and move raw MPI from the beginning of a buffer
1036 * to an ASN.1 MPI at the end of the buffer.
1037 * See also mbedtls_asn1_write_mpi().
1038 *
1039 * p: pointer to the end of the output buffer
1040 * start: start of the output buffer, and also of the mpi to write at the end
1041 * n_len: length of the mpi to read from start
1042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
1044 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +01001045{
1046 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1047 size_t len = 0;
1048
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((size_t) (*p - start) < n_len) {
1050 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
1051 }
Neil Armstrong15021652022-03-01 10:14:17 +01001052
1053 len = n_len;
1054 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +01001056
1057 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
1058 * Neither r nor s should be 0, but as a failsafe measure, still detect
1059 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +01001061 ++(*p);
1062 --len;
1063 }
1064
1065 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 if (len == 0) {
1067 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
1068 }
Neil Armstrong15021652022-03-01 10:14:17 +01001069
1070 /* if the msb is 1, ASN.1 requires that we prepend a 0.
1071 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (**p & 0x80) {
1073 if (*p - start < 1) {
1074 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
1075 }
Neil Armstrong15021652022-03-01 10:14:17 +01001076
1077 *--(*p) = 0x00;
1078 len += 1;
1079 }
1080
Gilles Peskine449bd832023-01-11 14:50:10 +01001081 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
1082 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
1083 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +01001084
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +01001086}
1087
1088/* Transcode signature from PSA format to ASN.1 sequence.
1089 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
1090 * MPIs, and in-place.
1091 *
1092 * [in/out] sig: the signature pre- and post-transcoding
1093 * [in/out] sig_len: signature length pre- and post-transcoding
1094 * [int] buf_len: the available size the in/out buffer
1095 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001096static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
1097 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +01001098{
1099 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1100 size_t len = 0;
1101 const size_t rs_len = *sig_len / 2;
1102 unsigned char *p = sig + buf_len;
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
1105 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +01001106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
1108 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
1109 MBEDTLS_ASN1_CONSTRUCTED |
1110 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +01001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +01001113 *sig_len = len;
1114
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +01001116}
Neil Armstronge9606902022-02-09 14:23:00 +01001117
Neil Armstrong17a06552022-03-18 15:27:38 +01001118/* Locate an ECDSA privateKey in a RFC 5915, or SEC1 Appendix C.4 ASN.1 buffer
1119 *
1120 * [in/out] buf: ASN.1 buffer start as input - ECDSA privateKey start as output
1121 * [in] end: ASN.1 buffer end
1122 * [out] key_len: the ECDSA privateKey length in bytes
1123 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001124static int find_ecdsa_private_key(unsigned char **buf, unsigned char *end,
1125 size_t *key_len)
Neil Armstronge9606902022-02-09 14:23:00 +01001126{
1127 size_t len;
1128 int ret;
1129
Neil Armstrong17a06552022-03-18 15:27:38 +01001130 /*
1131 * RFC 5915, or SEC1 Appendix C.4
1132 *
1133 * ECPrivateKey ::= SEQUENCE {
1134 * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
1135 * privateKey OCTET STRING,
1136 * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
1137 * publicKey [1] BIT STRING OPTIONAL
1138 * }
1139 */
1140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if ((ret = mbedtls_asn1_get_tag(buf, end, &len,
1142 MBEDTLS_ASN1_CONSTRUCTED |
1143 MBEDTLS_ASN1_SEQUENCE)) != 0) {
1144 return ret;
1145 }
Neil Armstronge9606902022-02-09 14:23:00 +01001146
1147 /* version */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 if ((ret = mbedtls_asn1_get_tag(buf, end, &len,
1149 MBEDTLS_ASN1_INTEGER)) != 0) {
1150 return ret;
1151 }
Neil Armstronge9606902022-02-09 14:23:00 +01001152
1153 *buf += len;
1154
1155 /* privateKey */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if ((ret = mbedtls_asn1_get_tag(buf, end, &len,
1157 MBEDTLS_ASN1_OCTET_STRING)) != 0) {
1158 return ret;
1159 }
Neil Armstronge9606902022-02-09 14:23:00 +01001160
1161 *key_len = len;
1162
1163 return 0;
1164}
1165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166static int ecdsa_sign_wrap(void *ctx_arg, mbedtls_md_type_t md_alg,
1167 const unsigned char *hash, size_t hash_len,
1168 unsigned char *sig, size_t sig_size, size_t *sig_len,
1169 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +01001170{
Valerio Settiab363d92023-01-26 14:31:54 +01001171 mbedtls_ecp_keypair *ctx = ctx_arg;
Neil Armstronge9606902022-02-09 14:23:00 +01001172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1174 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1175 psa_status_t status;
1176 mbedtls_pk_context key;
1177 size_t key_len;
Neil Armstrongdab14de2022-03-01 14:00:49 +01001178 unsigned char buf[MBEDTLS_PK_ECP_PRV_DER_MAX_BYTES];
Neil Armstronge9606902022-02-09 14:23:00 +01001179 unsigned char *p;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 psa_algorithm_t psa_hash = mbedtls_hash_info_psa_from_md(md_alg);
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +01001181#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 psa_algorithm_t psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(psa_hash);
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +01001183#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA(psa_hash);
Manuel Pégourié-Gonnard79ae7eb2022-12-05 12:55:51 +01001185#endif
Neil Armstronge9606902022-02-09 14:23:00 +01001186 size_t curve_bits;
1187 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Neil Armstronge9606902022-02-09 14:23:00 +01001189
1190 /* PSA has its own RNG */
1191 ((void) f_rng);
1192 ((void) p_rng);
1193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (curve == 0) {
1195 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1196 }
Neil Armstronge9606902022-02-09 14:23:00 +01001197
1198 /* mbedtls_pk_write_key_der() expects a full PK context;
1199 * re-construct one to make it happy */
Neil Armstrongcb753a62022-03-16 15:40:20 +01001200 key.pk_info = &mbedtls_eckey_info;
Neil Armstronge9606902022-02-09 14:23:00 +01001201 key.pk_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
1203 if (key_len <= 0) {
1204 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1205 }
Neil Armstronge9606902022-02-09 14:23:00 +01001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 p = buf + sizeof(buf) - key_len;
1208 ret = find_ecdsa_private_key(&p, buf + sizeof(buf), &key_len);
1209 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +01001210 goto cleanup;
1211 }
1212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1214 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
1215 psa_set_key_algorithm(&attributes, psa_sig_md);
1216
1217 status = psa_import_key(&attributes,
1218 p, key_len,
1219 &key_id);
1220 if (status != PSA_SUCCESS) {
1221 ret = mbedtls_pk_error_from_psa(status);
1222 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +01001223 }
1224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
1226 sig, sig_size, sig_len);
1227 if (status != PSA_SUCCESS) {
1228 ret = mbedtls_pk_error_from_psa_ecdsa(status);
1229 goto cleanup;
1230 }
1231
1232 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
Neil Armstronge9606902022-02-09 14:23:00 +01001233
1234cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 mbedtls_platform_zeroize(buf, sizeof(buf));
1236 status = psa_destroy_key(key_id);
1237 if (ret == 0 && status != PSA_SUCCESS) {
1238 ret = mbedtls_pk_error_from_psa(status);
1239 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001240
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +01001242}
1243#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001244static int ecdsa_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1245 const unsigned char *hash, size_t hash_len,
1246 unsigned char *sig, size_t sig_size, size_t *sig_len,
1247 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001248{
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) ctx,
1250 md_alg, hash, hash_len,
1251 sig, sig_size, sig_len,
1252 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001253}
Neil Armstronge9606902022-02-09 14:23:00 +01001254#endif
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001255
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001256#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001257static int ecdsa_verify_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1258 const unsigned char *hash, size_t hash_len,
1259 const unsigned char *sig, size_t sig_len,
1260 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001261{
Janos Follath24eed8d2019-11-22 13:21:35 +00001262 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001263 ((void) md_alg);
1264
1265 ret = mbedtls_ecdsa_read_signature_restartable(
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 (mbedtls_ecdsa_context *) ctx,
1267 hash, hash_len, sig, sig_len,
1268 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1271 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1272 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001273
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001275}
1276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277static int ecdsa_sign_rs_wrap(void *ctx, mbedtls_md_type_t md_alg,
1278 const unsigned char *hash, size_t hash_len,
1279 unsigned char *sig, size_t sig_size, size_t *sig_len,
1280 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1281 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001282{
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 return mbedtls_ecdsa_write_signature_restartable(
1284 (mbedtls_ecdsa_context *) ctx,
1285 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1286 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001287
1288}
1289#endif /* MBEDTLS_ECP_RESTARTABLE */
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291static void *ecdsa_alloc_wrap(void)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001292{
Valerio Settiab363d92023-01-26 14:31:54 +01001293 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 if (ctx != NULL) {
Valerio Settiab363d92023-01-26 14:31:54 +01001296 mbedtls_ecp_keypair_init((mbedtls_ecp_keypair *) ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 }
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 return ctx;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001300}
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302static void ecdsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001303{
Valerio Settiab363d92023-01-26 14:31:54 +01001304 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 mbedtls_free(ctx);
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001306}
1307
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001308#if defined(MBEDTLS_ECP_RESTARTABLE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001309static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001310{
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (ctx != NULL) {
1314 mbedtls_ecdsa_restart_init(ctx);
1315 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001318}
1319
Gilles Peskine449bd832023-01-11 14:50:10 +01001320static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001321{
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 mbedtls_ecdsa_restart_free(ctx);
1323 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001324}
1325#endif /* MBEDTLS_ECP_RESTARTABLE */
1326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001327const mbedtls_pk_info_t mbedtls_ecdsa_info = {
1328 MBEDTLS_PK_ECDSA,
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001329 "ECDSA",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001330 eckey_get_bitlen, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001331 ecdsa_can_do,
1332 ecdsa_verify_wrap,
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001333 ecdsa_sign_wrap,
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001334#if defined(MBEDTLS_ECP_RESTARTABLE)
1335 ecdsa_verify_rs_wrap,
1336 ecdsa_sign_rs_wrap,
1337#endif
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001338 NULL,
1339 NULL,
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001340 eckey_check_pair, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001341 ecdsa_alloc_wrap,
1342 ecdsa_free_wrap,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001343#if defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001344 ecdsa_rs_alloc,
1345 ecdsa_rs_free,
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001346#endif
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001347 eckey_debug, /* Compatible key structures */
1348};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001349#endif /* MBEDTLS_ECDSA_C */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001351#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001352/*
1353 * Support for alternative RSA-private implementations
1354 */
1355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001357{
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001359}
1360
Gilles Peskine449bd832023-01-11 14:50:10 +01001361static size_t rsa_alt_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001362{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363 const mbedtls_rsa_alt_context *rsa_alt = (const mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001364
Gilles Peskine449bd832023-01-11 14:50:10 +01001365 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001366}
1367
Gilles Peskine449bd832023-01-11 14:50:10 +01001368static int rsa_alt_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1369 const unsigned char *hash, size_t hash_len,
1370 unsigned char *sig, size_t sig_size, size_t *sig_len,
1371 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001373 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001374
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001375#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001376 if (UINT_MAX < hash_len) {
1377 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1378 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +01001379#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +00001380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1382 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1383 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1384 }
1385 if (*sig_len > sig_size) {
1386 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1387 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001388
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1390 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001391}
1392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393static int rsa_alt_decrypt_wrap(void *ctx,
1394 const unsigned char *input, size_t ilen,
1395 unsigned char *output, size_t *olen, size_t osize,
1396 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001397{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398 mbedtls_rsa_alt_context *rsa_alt = (mbedtls_rsa_alt_context *) ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001399
1400 ((void) f_rng);
1401 ((void) p_rng);
1402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1404 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1405 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 return rsa_alt->decrypt_func(rsa_alt->key,
1408 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001409}
1410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001411#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001412static int rsa_alt_check_pair(const void *pub, const void *prv,
1413 int (*f_rng)(void *, unsigned char *, size_t),
1414 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001415{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001417 unsigned char hash[32];
1418 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001419 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001420
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1422 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001423 }
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 memset(hash, 0x2a, sizeof(hash));
1426
1427 if ((ret = rsa_alt_sign_wrap((void *) prv, MBEDTLS_MD_NONE,
1428 hash, sizeof(hash),
1429 sig, sizeof(sig), &sig_len,
1430 f_rng, p_rng)) != 0) {
1431 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001432 }
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if (rsa_verify_wrap((void *) pub, MBEDTLS_MD_NONE,
1435 hash, sizeof(hash), sig, sig_len) != 0) {
1436 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1437 }
1438
1439 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001440}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001444{
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 if (ctx != NULL) {
1448 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1449 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001450
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001452}
1453
Gilles Peskine449bd832023-01-11 14:50:10 +01001454static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001455{
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_rsa_alt_context));
1457 mbedtls_free(ctx);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001458}
1459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
1461 MBEDTLS_PK_RSA_ALT,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001462 "RSA-alt",
Manuel Pégourié-Gonnard39a48f42015-06-18 16:06:55 +02001463 rsa_alt_get_bitlen,
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001464 rsa_alt_can_do,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001465 NULL,
1466 rsa_alt_sign_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001467#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001468 NULL,
1469 NULL,
1470#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001471 rsa_alt_decrypt_wrap,
1472 NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001473#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001474 rsa_alt_check_pair,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001475#else
1476 NULL,
1477#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001478 rsa_alt_alloc_wrap,
1479 rsa_alt_free_wrap,
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001480#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001481 NULL,
1482 NULL,
1483#endif
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001484 NULL,
1485};
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001488
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001489#if defined(MBEDTLS_USE_PSA_CRYPTO)
1490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491static void *pk_opaque_alloc_wrap(void)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001492{
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_svc_key_id_t));
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001494
Tom Cosgrovece7f18c2022-07-28 05:50:56 +01001495 /* no _init() function to call, as calloc() already zeroized */
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 return ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001498}
1499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500static void pk_opaque_free_wrap(void *ctx)
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001501{
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_svc_key_id_t));
1503 mbedtls_free(ctx);
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +01001504}
1505
Gilles Peskine449bd832023-01-11 14:50:10 +01001506static size_t pk_opaque_get_bitlen(const void *ctx)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001507{
Andrzej Kurek03e01462022-01-03 12:53:24 +01001508 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001509 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001510 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001511
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (PSA_SUCCESS != psa_get_key_attributes(*key, &attributes)) {
1513 return 0;
1514 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 bits = psa_get_key_bits(&attributes);
1517 psa_reset_key_attributes(&attributes);
1518 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001519}
1520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521static int pk_opaque_ecdsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001522{
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 return type == MBEDTLS_PK_ECKEY ||
1524 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001525}
1526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527static int pk_opaque_rsa_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001528{
Gilles Peskine449bd832023-01-11 14:50:10 +01001529 return type == MBEDTLS_PK_RSA ||
1530 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001531}
1532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533static int pk_opaque_sign_wrap(void *ctx, mbedtls_md_type_t md_alg,
1534 const unsigned char *hash, size_t hash_len,
1535 unsigned char *sig, size_t sig_size, size_t *sig_len,
1536 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001537{
Valerio Settiab363d92023-01-26 14:31:54 +01001538#if !defined(MBEDTLS_PK_CAN_ECDSA_SIGN) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001539 ((void) ctx);
1540 ((void) md_alg);
1541 ((void) hash);
1542 ((void) hash_len);
1543 ((void) sig);
Gilles Peskinef00f1522021-06-22 00:09:00 +02001544 ((void) sig_size);
John Durkopf35069a2020-08-17 22:05:14 -07001545 ((void) sig_len);
1546 ((void) f_rng);
1547 ((void) p_rng);
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Settiab363d92023-01-26 14:31:54 +01001549#else /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Andrzej Kurek03e01462022-01-03 12:53:24 +01001550 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001551 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001552 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001553 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001554 psa_status_t status;
1555
1556 /* PSA has its own RNG */
1557 (void) f_rng;
1558 (void) p_rng;
1559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 status = psa_get_key_attributes(*key, &attributes);
1561 if (status != PSA_SUCCESS) {
1562 return mbedtls_pk_error_from_psa(status);
1563 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001564
Gilles Peskine449bd832023-01-11 14:50:10 +01001565 type = psa_get_key_type(&attributes);
1566 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001567
Valerio Settiab363d92023-01-26 14:31:54 +01001568#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1570 alg = PSA_ALG_ECDSA(mbedtls_hash_info_psa_from_md(md_alg));
1571 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001572#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001573#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (PSA_KEY_TYPE_IS_RSA(type)) {
1575 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_hash_info_psa_from_md(md_alg));
1576 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001577#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001579
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001580 /* make the signature */
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 status = psa_sign_hash(*key, alg, hash, hash_len,
1582 sig, sig_size, sig_len);
1583 if (status != PSA_SUCCESS) {
Valerio Settiab363d92023-01-26 14:31:54 +01001584#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
1586 return mbedtls_pk_error_from_psa_ecdsa(status);
1587 } else
Valerio Settiab363d92023-01-26 14:31:54 +01001588#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001589#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (PSA_KEY_TYPE_IS_RSA(type)) {
1591 return mbedtls_pk_error_from_psa_rsa(status);
1592 } else
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001593#endif /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 return mbedtls_pk_error_from_psa(status);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001595 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001596
Valerio Settiab363d92023-01-26 14:31:54 +01001597#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001599 /* transcode it to ASN.1 sequence */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 return pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
1601 }
Valerio Settiab363d92023-01-26 14:31:54 +01001602#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001603
1604 return 0;
Valerio Settiab363d92023-01-26 14:31:54 +01001605#endif /* !MBEDTLS_PK_CAN_ECDSA_SIGN && !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001606}
1607
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001608const mbedtls_pk_info_t mbedtls_pk_ecdsa_opaque_info = {
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001609 MBEDTLS_PK_OPAQUE,
1610 "Opaque",
1611 pk_opaque_get_bitlen,
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001612 pk_opaque_ecdsa_can_do,
1613 NULL, /* verify - will be done later */
1614 pk_opaque_sign_wrap,
1615#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1616 NULL, /* restartable verify - not relevant */
1617 NULL, /* restartable sign - not relevant */
1618#endif
Neil Armstrong95a89232022-04-08 15:13:51 +02001619 NULL, /* decrypt - not relevant */
1620 NULL, /* encrypt - not relevant */
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001621 NULL, /* check_pair - could be done later or left NULL */
1622 pk_opaque_alloc_wrap,
1623 pk_opaque_free_wrap,
1624#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1625 NULL, /* restart alloc - not relevant */
1626 NULL, /* restart free - not relevant */
1627#endif
1628 NULL, /* debug - could be done later, or even left NULL */
1629};
1630
Neil Armstrong30beca32022-05-03 15:42:13 +02001631#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskine449bd832023-01-11 14:50:10 +01001632static int pk_opaque_rsa_decrypt(void *ctx,
1633 const unsigned char *input, size_t ilen,
1634 unsigned char *output, size_t *olen, size_t osize,
1635 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong10828182022-04-22 15:02:27 +02001636{
1637 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx;
1638 psa_status_t status;
1639
1640 /* PSA has its own RNG */
1641 (void) f_rng;
1642 (void) p_rng;
1643
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 status = psa_asymmetric_decrypt(*key, PSA_ALG_RSA_PKCS1V15_CRYPT,
1645 input, ilen,
1646 NULL, 0,
1647 output, osize, olen);
1648 if (status != PSA_SUCCESS) {
1649 return mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong10828182022-04-22 15:02:27 +02001650 }
1651
1652 return 0;
1653}
Neil Armstrong30beca32022-05-03 15:42:13 +02001654#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR */
Neil Armstrong10828182022-04-22 15:02:27 +02001655
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001656const mbedtls_pk_info_t mbedtls_pk_rsa_opaque_info = {
1657 MBEDTLS_PK_OPAQUE,
1658 "Opaque",
1659 pk_opaque_get_bitlen,
1660 pk_opaque_rsa_can_do,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001661 NULL, /* verify - will be done later */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001662 pk_opaque_sign_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001663#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1664 NULL, /* restartable verify - not relevant */
1665 NULL, /* restartable sign - not relevant */
1666#endif
Neil Armstrong30beca32022-05-03 15:42:13 +02001667#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Neil Armstrong10828182022-04-22 15:02:27 +02001668 pk_opaque_rsa_decrypt,
Neil Armstrong30beca32022-05-03 15:42:13 +02001669#else
1670 NULL, /* decrypt - not available */
1671#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001672 NULL, /* encrypt - will be done later */
1673 NULL, /* check_pair - could be done later or left NULL */
Manuel Pégourié-Gonnard69baf702018-11-06 09:34:30 +01001674 pk_opaque_alloc_wrap,
1675 pk_opaque_free_wrap,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001676#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1677 NULL, /* restart alloc - not relevant */
1678 NULL, /* restart free - not relevant */
1679#endif
1680 NULL, /* debug - could be done later, or even left NULL */
1681};
1682
1683#endif /* MBEDTLS_USE_PSA_CRYPTO */
1684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#endif /* MBEDTLS_PK_C */