blob: ff8eeb14cc6ed7a85043e8bbec6e3f48b806ced9 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02009
Gilles Peskine8a6022e2022-10-04 23:01:59 +020010#include "mbedtls/platform_util.h"
11
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000013#include "pk_wrap.h"
Valerio Settia1b8af62023-05-17 15:34:57 +020014#include "pk_internal.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000015#include "mbedtls/error.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010016#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020017
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020018/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020023#endif
24
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020027#endif
28
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010029#if defined(MBEDTLS_USE_PSA_CRYPTO)
Tomi Fontanilles1941af02023-12-14 21:48:52 +020030#include "psa_util_internal.h"
Andrzej Kurek8b036a62018-10-31 05:16:46 -040031#include "psa/crypto.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020032
Tomi Fontanilles9f417702023-12-16 15:28:51 +020033#if defined(MBEDTLS_RSA_C)
34#include "pkwrite.h"
Valerio Setti18dd0002024-01-23 17:59:10 +010035#include "rsa_internal.h"
Tomi Fontanilles9f417702023-12-16 15:28:51 +020036#endif
37
Valerio Setti80d07982023-02-08 13:49:17 +010038#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020039#include "mbedtls/asn1write.h"
40#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010041#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020042#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020045
Andres AG72849872017-01-19 11:24:33 +000046#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010047#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020048#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010051static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020052{
Gilles Peskine449bd832023-01-11 14:50:10 +010053 return type == MBEDTLS_PK_RSA ||
54 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020055}
56
valerio38992cb2023-04-20 09:56:30 +020057static size_t rsa_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020058{
valerio38992cb2023-04-20 09:56:30 +020059 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +010060 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020061}
62
Neil Armstrong52f41f82022-02-22 15:30:24 +010063#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +020064static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +010065 const unsigned char *hash, size_t hash_len,
66 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020067{
valerio38992cb2023-04-20 09:56:30 +020068 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +010069 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
70 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
71 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
72 psa_status_t status;
Neil Armstrong52f41f82022-02-22 15:30:24 +010073 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +010074 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Valerio Setti18dd0002024-01-23 17:59:10 +010075 unsigned char *p = buf + sizeof(buf);
Neil Armstrong82cf8042022-03-03 12:30:59 +010076 psa_algorithm_t psa_alg_md =
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020077 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Gilles Peskine449bd832023-01-11 14:50:10 +010078 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +010079
Dave Rodgman2eab4622023-10-05 13:30:37 +010080#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
82 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
83 }
Dave Rodgman2eab4622023-10-05 13:30:37 +010084#endif
Neil Armstrong52f41f82022-02-22 15:30:24 +010085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (sig_len < rsa_len) {
87 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
88 }
Neil Armstrong52f41f82022-02-22 15:30:24 +010089
Valerio Setti18dd0002024-01-23 17:59:10 +010090 key_len = mbedtls_rsa_pubkey_write(rsa, buf, &p);
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (key_len <= 0) {
92 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
93 }
Neil Armstrong52f41f82022-02-22 15:30:24 +010094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
96 psa_set_key_algorithm(&attributes, psa_alg_md);
97 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +010098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 status = psa_import_key(&attributes,
100 buf + sizeof(buf) - key_len, key_len,
101 &key_id);
102 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500103 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100104 goto cleanup;
105 }
106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
108 sig, sig_len);
109 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500110 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100111 goto cleanup;
112 }
113 ret = 0;
114
115cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 status = psa_destroy_key(key_id);
117 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500118 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100122}
Valerio Setti5c26b302023-06-21 19:47:01 +0200123#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200124static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 const unsigned char *hash, size_t hash_len,
126 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100127{
Janos Follath24eed8d2019-11-22 13:21:35 +0000128 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valerio38992cb2023-04-20 09:56:30 +0200129 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200131
Dave Rodgman02a53d72023-09-28 17:17:07 +0100132#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
134 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
135 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100136#endif
Andres AG72849872017-01-19 11:24:33 +0000137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (sig_len < rsa_len) {
139 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
140 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
143 (unsigned int) hash_len,
144 hash, sig)) != 0) {
145 return ret;
146 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200147
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200148 /* The buffer contains a valid signature followed by extra data.
149 * We have a special error code for that so that so that callers can
150 * use mbedtls_pk_verify() to check "Does the buffer start with a
151 * valid signature?" and not just "Does the buffer contain a valid
152 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (sig_len > rsa_len) {
154 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
155 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200158}
Valerio Setti5c26b302023-06-21 19:47:01 +0200159#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200160
Tomi Fontanilles81746622023-07-16 13:06:06 +0300161#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100162int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
163 mbedtls_rsa_context *rsa_ctx,
164 const unsigned char *hash, size_t hash_len,
165 unsigned char *sig, size_t sig_size,
166 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100167{
Neil Armstrong98545682022-02-22 16:12:51 +0100168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
170 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
171 psa_status_t status;
Neil Armstrong98545682022-02-22 16:12:51 +0100172 int key_len;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530173 unsigned char *buf = NULL;
Valerio Setti18dd0002024-01-23 17:59:10 +0100174 unsigned char *p;
175
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530176 buf = mbedtls_calloc(1, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
177 if (buf == NULL) {
178 return MBEDTLS_ERR_PK_ALLOC_FAILED;
179 }
Valerio Setti18dd0002024-01-23 17:59:10 +0100180 p = buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES;
Neil Armstrong98545682022-02-22 16:12:51 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
183 if (sig_size < *sig_len) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530184 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
186 }
Neil Armstrong98545682022-02-22 16:12:51 +0100187
Valerio Setti18dd0002024-01-23 17:59:10 +0100188 key_len = mbedtls_rsa_key_write(rsa_ctx, buf, &p);
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (key_len <= 0) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530190 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
192 }
193 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
194 psa_set_key_algorithm(&attributes, alg);
195 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 status = psa_import_key(&attributes,
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530198 buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES - key_len, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 &key_id);
200 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500201 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100202 goto cleanup;
203 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 status = psa_sign_hash(key_id, alg, hash, hash_len,
205 sig, sig_size, sig_len);
206 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500207 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100208 goto cleanup;
209 }
210
211 ret = 0;
212
213cleanup:
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530214 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 status = psa_destroy_key(key_id);
216 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500217 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 }
219 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100220}
Tomi Fontanilles81746622023-07-16 13:06:06 +0300221#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu1d172a32022-03-12 19:12:05 +0800222
223#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200224static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 const unsigned char *hash, size_t hash_len,
226 unsigned char *sig, size_t sig_size, size_t *sig_len,
227 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800228{
Jerry Yu1d172a32022-03-12 19:12:05 +0800229 ((void) f_rng);
230 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800231
Jerry Yubd1b3272022-03-24 13:05:20 +0800232 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200233 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (psa_md_alg == 0) {
235 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
236 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
239 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200240 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800242}
Valerio Setti5c26b302023-06-21 19:47:01 +0200243#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200244static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 const unsigned char *hash, size_t hash_len,
246 unsigned char *sig, size_t sig_size, size_t *sig_len,
247 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200248{
valerio38992cb2023-04-20 09:56:30 +0200249 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100250
Dave Rodgman02a53d72023-09-28 17:17:07 +0100251#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
253 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
254 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100255#endif
Andres AG72849872017-01-19 11:24:33 +0000256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 *sig_len = mbedtls_rsa_get_len(rsa);
258 if (sig_size < *sig_len) {
259 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
260 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
263 md_alg, (unsigned int) hash_len,
264 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200265}
Valerio Setti5c26b302023-06-21 19:47:01 +0200266#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200267
Neil Armstrong18f43c72022-02-09 15:32:45 +0100268#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200269static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 const unsigned char *input, size_t ilen,
271 unsigned char *output, size_t *olen, size_t osize,
272 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100273{
valerio38992cb2023-04-20 09:56:30 +0200274 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
276 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
277 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
278 psa_status_t status;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100279 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100280 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Valerio Setti18dd0002024-01-23 17:59:10 +0100281 unsigned char *p = buf + sizeof(buf);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100282
283 ((void) f_rng);
284 ((void) p_rng);
285
286#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
288 return MBEDTLS_ERR_RSA_INVALID_PADDING;
289 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100290#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (ilen != mbedtls_rsa_get_len(rsa)) {
293 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
294 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100295
Valerio Setti18dd0002024-01-23 17:59:10 +0100296 key_len = mbedtls_rsa_key_write(rsa, buf, &p);
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (key_len <= 0) {
298 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
299 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
302 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
303 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 status = psa_import_key(&attributes,
306 buf + sizeof(buf) - key_len, key_len,
307 &key_id);
308 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500309 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100310 goto cleanup;
311 }
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
314 input, ilen,
315 NULL, 0,
316 output, osize, olen);
317 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500318 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100319 goto cleanup;
320 }
321
322 ret = 0;
323
324cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 mbedtls_platform_zeroize(buf, sizeof(buf));
326 status = psa_destroy_key(key_id);
327 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500328 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100332}
Valerio Setti5c26b302023-06-21 19:47:01 +0200333#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200334static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 const unsigned char *input, size_t ilen,
336 unsigned char *output, size_t *olen, size_t osize,
337 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200338{
valerio38992cb2023-04-20 09:56:30 +0200339 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (ilen != mbedtls_rsa_get_len(rsa)) {
342 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
343 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
346 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200347}
Valerio Setti5c26b302023-06-21 19:47:01 +0200348#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200349
Neil Armstrong96a16a42022-02-10 10:40:11 +0100350#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200351static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 const unsigned char *input, size_t ilen,
353 unsigned char *output, size_t *olen, size_t osize,
354 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100355{
valerio38992cb2023-04-20 09:56:30 +0200356 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
358 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
359 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
360 psa_status_t status;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100361 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100362 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Valerio Setti18dd0002024-01-23 17:59:10 +0100363 unsigned char *p = buf + sizeof(buf);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100364
365 ((void) f_rng);
366 ((void) p_rng);
367
368#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
370 return MBEDTLS_ERR_RSA_INVALID_PADDING;
371 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100372#endif
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (mbedtls_rsa_get_len(rsa) > osize) {
375 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
376 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100377
Valerio Setti18dd0002024-01-23 17:59:10 +0100378 key_len = mbedtls_rsa_pubkey_write(rsa, buf, &p);
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (key_len <= 0) {
380 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
381 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
384 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
385 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 status = psa_import_key(&attributes,
388 buf + sizeof(buf) - key_len, key_len,
389 &key_id);
390 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500391 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100392 goto cleanup;
393 }
394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
396 input, ilen,
397 NULL, 0,
398 output, osize, olen);
399 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500400 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100401 goto cleanup;
402 }
403
404 ret = 0;
405
406cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 status = psa_destroy_key(key_id);
408 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500409 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100413}
Valerio Setti5c26b302023-06-21 19:47:01 +0200414#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200415static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 const unsigned char *input, size_t ilen,
417 unsigned char *output, size_t *olen, size_t osize,
418 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200419{
valerio38992cb2023-04-20 09:56:30 +0200420 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (*olen > osize) {
424 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
425 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
428 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200429}
Valerio Setti5c26b302023-06-21 19:47:01 +0200430#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200431
valerio38992cb2023-04-20 09:56:30 +0200432static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 int (*f_rng)(void *, unsigned char *, size_t),
434 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100435{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200436 (void) f_rng;
437 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200438 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
439 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100440}
441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200443{
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (ctx != NULL) {
447 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
448 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200451}
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200454{
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
456 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200457}
458
valerio38992cb2023-04-20 09:56:30 +0200459static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200460{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200461#if defined(MBEDTLS_RSA_ALT)
462 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200463 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200464 (void) items;
465#else
valerio38992cb2023-04-20 09:56:30 +0200466 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200469 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200470 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200471
472 items++;
473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200474 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200475 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200476 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200477#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200478}
479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480const mbedtls_pk_info_t mbedtls_rsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +0200481 .type = MBEDTLS_PK_RSA,
482 .name = "RSA",
483 .get_bitlen = rsa_get_bitlen,
484 .can_do = rsa_can_do,
485 .verify_func = rsa_verify_wrap,
486 .sign_func = rsa_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +0200487#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
488 .verify_rs_func = NULL,
489 .sign_rs_func = NULL,
490 .rs_alloc_func = NULL,
491 .rs_free_func = NULL,
492#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +0200493 .decrypt_func = rsa_decrypt_wrap,
494 .encrypt_func = rsa_encrypt_wrap,
495 .check_pair_func = rsa_check_pair_wrap,
496 .ctx_alloc_func = rsa_alloc_wrap,
497 .ctx_free_func = rsa_free_wrap,
498 .debug_func = rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200499};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200501
Valerio Setti81d75122023-06-14 14:49:33 +0200502#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200503/*
504 * Generic EC key
505 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 return type == MBEDTLS_PK_ECKEY ||
509 type == MBEDTLS_PK_ECKEY_DH ||
510 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200511}
512
valerio38992cb2023-04-20 09:56:30 +0200513static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200514{
Valerio Settia1b8af62023-05-17 15:34:57 +0200515#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
516 return pk->ec_bits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200517#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +0200518 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
519 return ecp->grp.pbits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200520#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200521}
522
Valerio Setti1cdddac2023-02-02 13:55:57 +0100523#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400524#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400525/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500526 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
527 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500528 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100529static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
530 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500531{
Janos Follath24eed8d2019-11-22 13:21:35 +0000532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500533 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
536 MBEDTLS_ASN1_INTEGER)) != 0) {
537 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 while (unpadded_len > 0 && **from == 0x00) {
541 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500542 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500543 }
544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (unpadded_len > to_len || unpadded_len == 0) {
546 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
547 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500548
Andrzej Kurek9241d182018-11-20 05:04:35 -0500549 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 memset(to, 0x00, padding_len);
551 memcpy(to + padding_len, *from, unpadded_len);
552 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500555}
556
557/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400558 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500559 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500560 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400561 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
563 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400564{
Janos Follath24eed8d2019-11-22 13:21:35 +0000565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500566 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
569 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
570 return ret;
571 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400572
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500573 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
575 return ret;
576 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500577 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
579 return ret;
580 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400583}
584
Valerio Setti76d0f962023-06-23 13:32:54 +0200585/* Common helper for ECDSA verify using PSA functions. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200586static int ecdsa_verify_psa(unsigned char *key, size_t key_len,
587 psa_ecc_family_t curve, size_t curve_bits,
588 const unsigned char *hash, size_t hash_len,
589 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400590{
Janos Follath24eed8d2019-11-22 13:21:35 +0000591 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200592 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100593 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settia1b8af62023-05-17 15:34:57 +0200594 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Valerio Settied7d6af2023-06-21 15:42:21 +0200595 size_t signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
596 unsigned char extracted_sig[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
597 unsigned char *p;
598 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (curve == 0) {
601 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
602 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
605 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
606 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500607
Valerio Settied7d6af2023-06-21 15:42:21 +0200608 status = psa_import_key(&attributes, key, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500610 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400611 goto cleanup;
612 }
613
Valerio Settied7d6af2023-06-21 15:42:21 +0200614 if (signature_len > sizeof(extracted_sig)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500615 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
616 goto cleanup;
617 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 p = (unsigned char *) sig;
Valerio Settia1b8af62023-05-17 15:34:57 +0200620 /* extract_ecdsa_sig's last parameter is the size
Valerio Settif57007d2023-05-19 13:54:39 +0200621 * of each integer to be parsed, so it's actually half
Valerio Settia1b8af62023-05-17 15:34:57 +0200622 * the size of the signature. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200623 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, extracted_sig,
Valerio Settia1b8af62023-05-17 15:34:57 +0200624 signature_len/2)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500625 goto cleanup;
626 }
627
Valerio Settied7d6af2023-06-21 15:42:21 +0200628 status = psa_verify_hash(key_id, psa_sig_md, hash, hash_len,
629 extracted_sig, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500631 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400633 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500636 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
637 goto cleanup;
638 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400639 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400640
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500641cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 status = psa_destroy_key(key_id);
643 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500644 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400648}
Valerio Settied7d6af2023-06-21 15:42:21 +0200649
Valerio Setti76d0f962023-06-23 13:32:54 +0200650static int ecdsa_opaque_verify_wrap(mbedtls_pk_context *pk,
651 mbedtls_md_type_t md_alg,
652 const unsigned char *hash, size_t hash_len,
653 const unsigned char *sig, size_t sig_len)
Valerio Settie7730772023-06-21 16:58:40 +0200654{
655 (void) md_alg;
656 unsigned char key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
657 size_t key_len;
658 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
659 psa_ecc_family_t curve;
660 size_t curve_bits;
661 psa_status_t status;
662
663 status = psa_get_key_attributes(pk->priv_id, &key_attr);
664 if (status != PSA_SUCCESS) {
665 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
666 }
667 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(&key_attr));
668 curve_bits = psa_get_key_bits(&key_attr);
669 psa_reset_key_attributes(&key_attr);
670
671 status = psa_export_public_key(pk->priv_id, key, sizeof(key), &key_len);
672 if (status != PSA_SUCCESS) {
673 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
674 }
675
676 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
677 hash, hash_len, sig, sig_len);
678}
679
Valerio Settied7d6af2023-06-21 15:42:21 +0200680#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
681static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
682 mbedtls_md_type_t md_alg,
683 const unsigned char *hash, size_t hash_len,
684 const unsigned char *sig, size_t sig_len)
685{
686 (void) md_alg;
687 psa_ecc_family_t curve = pk->ec_family;
688 size_t curve_bits = pk->ec_bits;
689
690 return ecdsa_verify_psa(pk->pub_raw, pk->pub_raw_len, curve, curve_bits,
691 hash, hash_len, sig, sig_len);
692}
693#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
694static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
695 mbedtls_md_type_t md_alg,
696 const unsigned char *hash, size_t hash_len,
697 const unsigned char *sig, size_t sig_len)
698{
699 (void) md_alg;
700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
701 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
702 unsigned char key[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
703 size_t key_len;
704 size_t curve_bits;
705 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
706
707 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
708 MBEDTLS_ECP_PF_UNCOMPRESSED,
709 &key_len, key, sizeof(key));
710 if (ret != 0) {
711 return ret;
712 }
713
714 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
715 hash, hash_len, sig, sig_len);
716}
717#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400718#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200719static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 const unsigned char *hash, size_t hash_len,
721 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200722{
Janos Follath24eed8d2019-11-22 13:21:35 +0000723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200724 ((void) md_alg);
725
valerio38992cb2023-04-20 09:56:30 +0200726 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
730 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
731 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200734}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400735#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100736#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200737
Valerio Setti1cdddac2023-02-02 13:55:57 +0100738#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100739#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100740/*
741 * Simultaneously convert and move raw MPI from the beginning of a buffer
742 * to an ASN.1 MPI at the end of the buffer.
743 * See also mbedtls_asn1_write_mpi().
744 *
745 * p: pointer to the end of the output buffer
746 * start: start of the output buffer, and also of the mpi to write at the end
747 * n_len: length of the mpi to read from start
748 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100749static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
750 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100751{
752 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
753 size_t len = 0;
754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if ((size_t) (*p - start) < n_len) {
756 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
757 }
Neil Armstrong15021652022-03-01 10:14:17 +0100758
759 len = n_len;
760 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100762
763 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
764 * Neither r nor s should be 0, but as a failsafe measure, still detect
765 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100767 ++(*p);
768 --len;
769 }
770
771 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 if (len == 0) {
773 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
774 }
Neil Armstrong15021652022-03-01 10:14:17 +0100775
776 /* if the msb is 1, ASN.1 requires that we prepend a 0.
777 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (**p & 0x80) {
779 if (*p - start < 1) {
780 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
781 }
Neil Armstrong15021652022-03-01 10:14:17 +0100782
783 *--(*p) = 0x00;
784 len += 1;
785 }
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
788 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
789 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100790
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100792}
793
794/* Transcode signature from PSA format to ASN.1 sequence.
795 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
796 * MPIs, and in-place.
797 *
798 * [in/out] sig: the signature pre- and post-transcoding
799 * [in/out] sig_len: signature length pre- and post-transcoding
800 * [int] buf_len: the available size the in/out buffer
801 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100802static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
803 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100804{
805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
806 size_t len = 0;
807 const size_t rs_len = *sig_len / 2;
808 unsigned char *p = sig + buf_len;
809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
811 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100812
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
814 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
815 MBEDTLS_ASN1_CONSTRUCTED |
816 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100819 *sig_len = len;
820
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100822}
Neil Armstronge9606902022-02-09 14:23:00 +0100823
Valerio Setti4ac2c182023-12-05 07:59:01 +0100824/* Common helper for ECDSA sign using PSA functions.
825 * Instead of extracting key's properties in order to check which kind of ECDSA
826 * signature it supports, we try both deterministic and non-deterministic.
827 */
Valerio Setti884c1ec2023-06-23 12:09:13 +0200828static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg,
Valerio Setti4657f102023-06-21 13:55:16 +0200829 const unsigned char *hash, size_t hash_len,
830 unsigned char *sig, size_t sig_size, size_t *sig_len)
831{
832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
833 psa_status_t status;
Valerio Setti884c1ec2023-06-23 12:09:13 +0200834
Valerio Setti4ac2c182023-12-05 07:59:01 +0100835 status = psa_sign_hash(key_id,
836 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)),
837 hash, hash_len, sig, sig_size, sig_len);
838 if (status == PSA_SUCCESS) {
839 goto done;
840 } else if (status != PSA_ERROR_NOT_PERMITTED) {
Valerio Setti884c1ec2023-06-23 12:09:13 +0200841 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
842 }
Valerio Setti884c1ec2023-06-23 12:09:13 +0200843
Valerio Setti4ac2c182023-12-05 07:59:01 +0100844 status = psa_sign_hash(key_id,
845 PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)),
846 hash, hash_len, sig, sig_size, sig_len);
Valerio Setti4657f102023-06-21 13:55:16 +0200847 if (status != PSA_SUCCESS) {
848 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
849 }
850
Valerio Setti4ac2c182023-12-05 07:59:01 +0100851done:
Valerio Setti4657f102023-06-21 13:55:16 +0200852 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
853
854 return ret;
855}
856
Valerio Setti76d0f962023-06-23 13:32:54 +0200857static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk,
858 mbedtls_md_type_t md_alg,
859 const unsigned char *hash, size_t hash_len,
860 unsigned char *sig, size_t sig_size,
861 size_t *sig_len,
862 int (*f_rng)(void *, unsigned char *, size_t),
863 void *p_rng)
Valerio Setti4657f102023-06-21 13:55:16 +0200864{
865 ((void) f_rng);
866 ((void) p_rng);
Valerio Setti4657f102023-06-21 13:55:16 +0200867
Valerio Setti884c1ec2023-06-23 12:09:13 +0200868 return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size,
Valerio Setti4657f102023-06-21 13:55:16 +0200869 sig_len);
870}
871
872#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti76d0f962023-06-23 13:32:54 +0200873/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
874 * using the same function. */
875#define ecdsa_sign_wrap ecdsa_opaque_sign_wrap
Valerio Setti4657f102023-06-21 13:55:16 +0200876#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +0200877static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 const unsigned char *hash, size_t hash_len,
879 unsigned char *sig, size_t sig_size, size_t *sig_len,
880 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100881{
Neil Armstronge9606902022-02-09 14:23:00 +0100882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronge9606902022-02-09 14:23:00 +0100883 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
884 psa_status_t status;
Valerio Settiae8c6282023-05-18 18:57:57 +0200885 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
887 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Neil Armstronge9606902022-02-09 14:23:00 +0100888 size_t curve_bits;
889 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200891 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200892 psa_algorithm_t psa_hash = mbedtls_md_psa_alg_from_type(md_alg);
893 psa_algorithm_t psa_sig_md = MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(psa_hash);
Neil Armstronge9606902022-02-09 14:23:00 +0100894 ((void) f_rng);
895 ((void) p_rng);
896
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 if (curve == 0) {
898 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
899 }
Neil Armstronge9606902022-02-09 14:23:00 +0100900
Gilles Peskine13caa942022-10-04 22:59:26 +0200901 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100902 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200904 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100906 goto cleanup;
907 }
908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
910 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
911 psa_set_key_algorithm(&attributes, psa_sig_md);
912
Valerio Setti4657f102023-06-21 13:55:16 +0200913 status = psa_import_key(&attributes, buf, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500915 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100917 }
918
Valerio Setti884c1ec2023-06-23 12:09:13 +0200919 ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len);
Neil Armstronge9606902022-02-09 14:23:00 +0100920
921cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100922 mbedtls_platform_zeroize(buf, sizeof(buf));
923 status = psa_destroy_key(key_id);
924 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500925 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100927
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100929}
Valerio Setti4657f102023-06-21 13:55:16 +0200930#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100931#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200932static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 const unsigned char *hash, size_t hash_len,
934 unsigned char *sig, size_t sig_size, size_t *sig_len,
935 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200936{
valerio38992cb2023-04-20 09:56:30 +0200937 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 md_alg, hash, hash_len,
939 sig, sig_size, sig_len,
940 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200941}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100942#endif /* MBEDTLS_USE_PSA_CRYPTO */
943#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200944
Valerio Setti80d07982023-02-08 13:49:17 +0100945#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +0100946/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +0200947static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +0100948 const unsigned char *hash, size_t hash_len,
949 const unsigned char *sig, size_t sig_len,
950 void *rs_ctx);
951
valerio38992cb2023-04-20 09:56:30 +0200952static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +0100953 const unsigned char *hash, size_t hash_len,
954 unsigned char *sig, size_t sig_size, size_t *sig_len,
955 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
956 void *rs_ctx);
957
958/*
959 * Restart context for ECDSA operations with ECKEY context
960 *
961 * We need to store an actual ECDSA context, as we need to pass the same to
962 * the underlying ecdsa function, so we can't create it on the fly every time.
963 */
964typedef struct {
965 mbedtls_ecdsa_restart_ctx ecdsa_rs;
966 mbedtls_ecdsa_context ecdsa_ctx;
967} eckey_restart_ctx;
968
969static void *eckey_rs_alloc(void)
970{
971 eckey_restart_ctx *rs_ctx;
972
973 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
974
975 if (ctx != NULL) {
976 rs_ctx = ctx;
977 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
978 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
979 }
980
981 return ctx;
982}
983
984static void eckey_rs_free(void *ctx)
985{
986 eckey_restart_ctx *rs_ctx;
987
988 if (ctx == NULL) {
989 return;
990 }
991
992 rs_ctx = ctx;
993 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
994 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
995
996 mbedtls_free(ctx);
997}
998
valerio38992cb2023-04-20 09:56:30 +0200999static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001000 const unsigned char *hash, size_t hash_len,
1001 const unsigned char *sig, size_t sig_len,
1002 void *rs_ctx)
1003{
1004 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1005 eckey_restart_ctx *rs = rs_ctx;
1006
1007 /* Should never happen */
1008 if (rs == NULL) {
1009 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1010 }
1011
1012 /* set up our own sub-context if needed (that is, on first run) */
1013 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001014 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001015 }
1016
valerio38992cb2023-04-20 09:56:30 +02001017 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001018 md_alg, hash, hash_len,
1019 sig, sig_len, &rs->ecdsa_rs));
1020
1021cleanup:
1022 return ret;
1023}
1024
valerio38992cb2023-04-20 09:56:30 +02001025static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001026 const unsigned char *hash, size_t hash_len,
1027 unsigned char *sig, size_t sig_size, size_t *sig_len,
1028 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1029 void *rs_ctx)
1030{
1031 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1032 eckey_restart_ctx *rs = rs_ctx;
1033
1034 /* Should never happen */
1035 if (rs == NULL) {
1036 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1037 }
1038
1039 /* set up our own sub-context if needed (that is, on first run) */
1040 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001041 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001042 }
1043
valerio38992cb2023-04-20 09:56:30 +02001044 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001045 hash, hash_len, sig, sig_size, sig_len,
1046 f_rng, p_rng, &rs->ecdsa_rs));
1047
1048cleanup:
1049 return ret;
1050}
Valerio Setti80d07982023-02-08 13:49:17 +01001051#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001052
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001053#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settibb7603a2023-06-21 18:34:54 +02001054#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerio38992cb2023-04-20 09:56:30 +02001055static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001056{
Valerio Setti9efa8c42023-05-19 13:27:30 +02001057 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001059 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001060 size_t prv_key_len;
Valerio Settiae8c6282023-05-18 18:57:57 +02001061 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001062
1063 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1064 &prv_key_len);
1065 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1066 if (ret != 0) {
1067 return ret;
1068 }
1069
1070 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1071 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1072 }
Valerio Settibb7603a2023-06-21 18:34:54 +02001073
1074 return 0;
1075}
1076#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1077static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
1078{
1079 psa_status_t status;
1080 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1081 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1082 size_t prv_key_len;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001083 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001084 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1085 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001086 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1087 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001088 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001089 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001090 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +02001091 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001092
Valerio Settia7cb8452023-05-22 18:39:43 +02001093 if (curve == 0) {
1094 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1095 }
1096
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001097 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1098 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1099
Valerio Settia1b8af62023-05-17 15:34:57 +02001100 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1101 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001102 if (ret != 0) {
Valerio Setti35d1dac2023-06-30 18:04:16 +02001103 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001104 return ret;
1105 }
1106
1107 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti35d1dac2023-06-30 18:04:16 +02001108 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti1df94f82023-04-07 08:59:24 +02001109 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1110 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001111 return ret;
1112 }
1113
Valerio Setti35d1dac2023-06-30 18:04:16 +02001114 // From now on prv_key_buf is used to store the public key of prv.
Valerio Setti1df94f82023-04-07 08:59:24 +02001115 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1116 &prv_key_len);
1117 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1118 destruction_status = psa_destroy_key(key_id);
1119 if (ret != 0) {
1120 return ret;
1121 } else if (destruction_status != PSA_SUCCESS) {
1122 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001123 }
1124
Valerio Settia1b8af62023-05-17 15:34:57 +02001125 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1126 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001127 MBEDTLS_ECP_PF_UNCOMPRESSED,
1128 &pub_key_len, pub_key_buf,
1129 sizeof(pub_key_buf));
1130 if (ret != 0) {
1131 return ret;
1132 }
1133
1134 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1135 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1136 }
1137
1138 return 0;
1139}
Valerio Settibb7603a2023-06-21 18:34:54 +02001140#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001141
Valerio Settibb7603a2023-06-21 18:34:54 +02001142static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1143 int (*f_rng)(void *, unsigned char *, size_t),
1144 void *p_rng)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001145{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001146 (void) f_rng;
1147 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001148 return eckey_check_pair_psa(pub, prv);
Valerio Settibb7603a2023-06-21 18:34:54 +02001149}
1150#else /* MBEDTLS_USE_PSA_CRYPTO */
1151static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1152 int (*f_rng)(void *, unsigned char *, size_t),
1153 void *p_rng)
1154{
valerio38992cb2023-04-20 09:56:30 +02001155 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1156 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001157 f_rng, p_rng);
1158}
Valerio Settibb7603a2023-06-21 18:34:54 +02001159#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001160
Valerio Setti88a3aee2023-06-29 15:01:10 +02001161#if defined(MBEDTLS_USE_PSA_CRYPTO)
1162#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1163/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1164 * using the same function. */
1165#define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap
1166#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1167static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub,
1168 mbedtls_pk_context *prv,
1169 int (*f_rng)(void *, unsigned char *, size_t),
1170 void *p_rng)
1171{
1172 psa_status_t status;
1173 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1174 size_t exp_pub_key_len = 0;
1175 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1176 size_t pub_key_len = 0;
1177 int ret;
1178 (void) f_rng;
1179 (void) p_rng;
1180
1181 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1182 &exp_pub_key_len);
1183 if (status != PSA_SUCCESS) {
1184 ret = psa_pk_status_to_mbedtls(status);
1185 return ret;
1186 }
1187 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1188 &(mbedtls_pk_ec_ro(*pub)->Q),
1189 MBEDTLS_ECP_PF_UNCOMPRESSED,
1190 &pub_key_len, pub_key, sizeof(pub_key));
1191 if (ret != 0) {
1192 return ret;
1193 }
1194 if ((exp_pub_key_len != pub_key_len) ||
1195 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1196 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1197 }
1198 return 0;
1199}
1200#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1201#endif /* MBEDTLS_USE_PSA_CRYPTO */
1202
Valerio Settib5361262023-05-18 18:51:58 +02001203#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001204static void *eckey_alloc_wrap(void)
1205{
1206 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1207
1208 if (ctx != NULL) {
1209 mbedtls_ecp_keypair_init(ctx);
1210 }
1211
1212 return ctx;
1213}
1214
1215static void eckey_free_wrap(void *ctx)
1216{
1217 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1218 mbedtls_free(ctx);
1219}
Valerio Settib5361262023-05-18 18:51:58 +02001220#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001221
valerio38992cb2023-04-20 09:56:30 +02001222static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001223{
Valerio Settia1b8af62023-05-17 15:34:57 +02001224#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1225 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1226 items->name = "eckey.Q";
1227 items->value = pk;
Valerio Setti5c26b302023-06-21 19:47:01 +02001228#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001229 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001230 items->type = MBEDTLS_PK_DEBUG_ECP;
1231 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001232 items->value = &(ecp->Q);
Valerio Setti5c26b302023-06-21 19:47:01 +02001233#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001234}
1235
1236const mbedtls_pk_info_t mbedtls_eckey_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001237 .type = MBEDTLS_PK_ECKEY,
1238 .name = "EC",
1239 .get_bitlen = eckey_get_bitlen,
1240 .can_do = eckey_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001241#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001242 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001243#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1244 .verify_func = NULL,
1245#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001246#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001247 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001248#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1249 .sign_func = NULL,
1250#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001251#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001252 .verify_rs_func = eckey_verify_rs_wrap,
1253 .sign_rs_func = eckey_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001254 .rs_alloc_func = eckey_rs_alloc,
1255 .rs_free_func = eckey_rs_free,
1256#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1257 .decrypt_func = NULL,
1258 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001259 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001260#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1261 .ctx_alloc_func = NULL,
1262 .ctx_free_func = NULL,
1263#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001264 .ctx_alloc_func = eckey_alloc_wrap,
1265 .ctx_free_func = eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001266#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001267 .debug_func = eckey_debug,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001268};
1269
1270/*
1271 * EC key restricted to ECDH
1272 */
1273static int eckeydh_can_do(mbedtls_pk_type_t type)
1274{
1275 return type == MBEDTLS_PK_ECKEY ||
1276 type == MBEDTLS_PK_ECKEY_DH;
1277}
1278
1279const mbedtls_pk_info_t mbedtls_eckeydh_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001280 .type = MBEDTLS_PK_ECKEY_DH,
1281 .name = "EC_DH",
1282 .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */
1283 .can_do = eckeydh_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001284 .verify_func = NULL,
1285 .sign_func = NULL,
1286#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1287 .verify_rs_func = NULL,
1288 .sign_rs_func = NULL,
1289#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1290 .decrypt_func = NULL,
1291 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001292 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001293#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1294 .ctx_alloc_func = NULL,
1295 .ctx_free_func = NULL,
1296#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001297 .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */
1298 .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */
Valerio Settib5361262023-05-18 18:51:58 +02001299#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001300 .debug_func = eckey_debug, /* Same underlying key structure */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001301};
Valerio Setti1cdddac2023-02-02 13:55:57 +01001302
1303#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1304static int ecdsa_can_do(mbedtls_pk_type_t type)
1305{
1306 return type == MBEDTLS_PK_ECDSA;
1307}
1308
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001309#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001310static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 const unsigned char *hash, size_t hash_len,
1312 const unsigned char *sig, size_t sig_len,
1313 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001314{
Janos Follath24eed8d2019-11-22 13:21:35 +00001315 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001316 ((void) md_alg);
1317
1318 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001319 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 hash, hash_len, sig, sig_len,
1321 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1324 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1325 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001326
Gilles Peskine449bd832023-01-11 14:50:10 +01001327 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001328}
1329
valerio38992cb2023-04-20 09:56:30 +02001330static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001331 const unsigned char *hash, size_t hash_len,
1332 unsigned char *sig, size_t sig_size, size_t *sig_len,
1333 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1334 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001335{
Gilles Peskine449bd832023-01-11 14:50:10 +01001336 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001337 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1339 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001340
1341}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001344{
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (ctx != NULL) {
1348 mbedtls_ecdsa_restart_init(ctx);
1349 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001352}
1353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001355{
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 mbedtls_ecdsa_restart_free(ctx);
1357 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001358}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001359#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361const mbedtls_pk_info_t mbedtls_ecdsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001362 .type = MBEDTLS_PK_ECDSA,
1363 .name = "ECDSA",
1364 .get_bitlen = eckey_get_bitlen, /* Compatible key structures */
1365 .can_do = ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001366#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001367 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001368#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1369 .verify_func = NULL,
1370#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001371#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001372 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001373#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1374 .sign_func = NULL,
1375#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001376#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001377 .verify_rs_func = ecdsa_verify_rs_wrap,
1378 .sign_rs_func = ecdsa_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001379 .rs_alloc_func = ecdsa_rs_alloc,
1380 .rs_free_func = ecdsa_rs_free,
1381#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1382 .decrypt_func = NULL,
1383 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001384 .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001385#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1386 .ctx_alloc_func = NULL,
1387 .ctx_free_func = NULL,
1388#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001389 .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */
1390 .ctx_free_func = eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001391#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001392 .debug_func = eckey_debug, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001393};
Valerio Setti7ca13182023-01-27 13:22:42 +01001394#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Valerio Settid9d74c22023-06-29 15:00:02 +02001395#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001398/*
1399 * Support for alternative RSA-private implementations
1400 */
1401
Gilles Peskine449bd832023-01-11 14:50:10 +01001402static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001403{
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001405}
1406
valerio38992cb2023-04-20 09:56:30 +02001407static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001408{
valerio38992cb2023-04-20 09:56:30 +02001409 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001410
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001412}
1413
valerio38992cb2023-04-20 09:56:30 +02001414static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 const unsigned char *hash, size_t hash_len,
1416 unsigned char *sig, size_t sig_size, size_t *sig_len,
1417 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001418{
valerio38992cb2023-04-20 09:56:30 +02001419 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001420
Dave Rodgman02a53d72023-09-28 17:17:07 +01001421#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 if (UINT_MAX < hash_len) {
1423 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1424 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001425#endif
Andres AG72849872017-01-19 11:24:33 +00001426
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1428 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1429 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1430 }
1431 if (*sig_len > sig_size) {
1432 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1433 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1436 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001437}
1438
valerio38992cb2023-04-20 09:56:30 +02001439static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 const unsigned char *input, size_t ilen,
1441 unsigned char *output, size_t *olen, size_t osize,
1442 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001443{
valerio38992cb2023-04-20 09:56:30 +02001444 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001445
1446 ((void) f_rng);
1447 ((void) p_rng);
1448
Gilles Peskine449bd832023-01-11 14:50:10 +01001449 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1450 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1451 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001452
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return rsa_alt->decrypt_func(rsa_alt->key,
1454 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001455}
1456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001457#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001458static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 int (*f_rng)(void *, unsigned char *, size_t),
1460 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001461{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001462 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001463 unsigned char hash[32];
1464 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001465 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001466
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1468 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001469 }
1470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 memset(hash, 0x2a, sizeof(hash));
1472
valerio38992cb2023-04-20 09:56:30 +02001473 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 hash, sizeof(hash),
1475 sig, sizeof(sig), &sig_len,
1476 f_rng, p_rng)) != 0) {
1477 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001478 }
1479
valerio38992cb2023-04-20 09:56:30 +02001480 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 hash, sizeof(hash), sig, sig_len) != 0) {
1482 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1483 }
1484
1485 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001490{
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 if (ctx != NULL) {
1494 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1495 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001496
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001498}
1499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001501{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001502 mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001503}
1504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001505const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001506 .type = MBEDTLS_PK_RSA_ALT,
1507 .name = "RSA-alt",
1508 .get_bitlen = rsa_alt_get_bitlen,
1509 .can_do = rsa_alt_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001510 .verify_func = NULL,
Valerio Settif69514a2023-06-21 18:16:49 +02001511 .sign_func = rsa_alt_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001512#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1513 .verify_rs_func = NULL,
1514 .sign_rs_func = NULL,
1515 .rs_alloc_func = NULL,
1516 .rs_free_func = NULL,
1517#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +02001518 .decrypt_func = rsa_alt_decrypt_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001519 .encrypt_func = NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001520#if defined(MBEDTLS_RSA_C)
Valerio Settif69514a2023-06-21 18:16:49 +02001521 .check_pair_func = rsa_alt_check_pair,
Valerio Setti97976e32023-06-23 14:08:26 +02001522#else
1523 .check_pair_func = NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001524#endif
Valerio Settif69514a2023-06-21 18:16:49 +02001525 .ctx_alloc_func = rsa_alt_alloc_wrap,
1526 .ctx_free_func = rsa_alt_free_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001527 .debug_func = NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001528};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001529#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001530
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001531#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +02001532static size_t opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001533{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001534 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001535 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001536
Valerio Setti4f387ef2023-05-02 14:15:59 +02001537 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 return 0;
1539 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 bits = psa_get_key_bits(&attributes);
1542 psa_reset_key_attributes(&attributes);
1543 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001544}
1545
Valerio Setti38913c12023-06-30 16:18:33 +02001546#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti76d0f962023-06-23 13:32:54 +02001547static int ecdsa_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001548{
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 return type == MBEDTLS_PK_ECKEY ||
1550 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001551}
1552
Valerio Setti76d0f962023-06-23 13:32:54 +02001553const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = {
Valerio Setti574a00b2023-06-21 19:47:37 +02001554 .type = MBEDTLS_PK_OPAQUE,
1555 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001556 .get_bitlen = opaque_get_bitlen,
1557 .can_do = ecdsa_opaque_can_do,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001558#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001559 .verify_func = ecdsa_opaque_verify_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001560#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1561 .verify_func = NULL,
1562#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1563#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Setti76d0f962023-06-23 13:32:54 +02001564 .sign_func = ecdsa_opaque_sign_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001565#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1566 .sign_func = NULL,
1567#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti97976e32023-06-23 14:08:26 +02001568#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1569 .verify_rs_func = NULL,
1570 .sign_rs_func = NULL,
1571 .rs_alloc_func = NULL,
1572 .rs_free_func = NULL,
1573#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1574 .decrypt_func = NULL,
1575 .encrypt_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001576 .check_pair_func = ecdsa_opaque_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001577 .ctx_alloc_func = NULL,
1578 .ctx_free_func = NULL,
1579 .debug_func = NULL,
Valerio Setti574a00b2023-06-21 19:47:37 +02001580};
Valerio Setti38913c12023-06-30 16:18:33 +02001581#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti574a00b2023-06-21 19:47:37 +02001582
Valerio Setti76d0f962023-06-23 13:32:54 +02001583static int rsa_opaque_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001584{
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 return type == MBEDTLS_PK_RSA ||
1586 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001587}
1588
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001589#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001590static int rsa_opaque_decrypt(mbedtls_pk_context *pk,
1591 const unsigned char *input, size_t ilen,
1592 unsigned char *output, size_t *olen, size_t osize,
1593 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001594{
Valerio Setti574a00b2023-06-21 19:47:37 +02001595 psa_status_t status;
1596
1597 /* PSA has its own RNG */
1598 (void) f_rng;
1599 (void) p_rng;
1600
1601 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
1602 input, ilen,
1603 NULL, 0,
1604 output, osize, olen);
1605 if (status != PSA_SUCCESS) {
1606 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1607 }
1608
1609 return 0;
1610}
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001611#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti574a00b2023-06-21 19:47:37 +02001612
Valerio Setti76d0f962023-06-23 13:32:54 +02001613static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
1614 const unsigned char *hash, size_t hash_len,
1615 unsigned char *sig, size_t sig_size, size_t *sig_len,
1616 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001617{
1618#if defined(MBEDTLS_RSA_C)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001619 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001620 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001621 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001622 psa_status_t status;
1623
1624 /* PSA has its own RNG */
1625 (void) f_rng;
1626 (void) p_rng;
1627
Valerio Setti4f387ef2023-05-02 14:15:59 +02001628 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001630 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 type = psa_get_key_type(&attributes);
1634 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001637 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Valerio Setti4657f102023-06-21 13:55:16 +02001638 } else {
1639 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1640 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001641
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001642 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001643 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 sig, sig_size, sig_len);
1645 if (status != PSA_SUCCESS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001646 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001647 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Valerio Setti4657f102023-06-21 13:55:16 +02001648 } else {
1649 return PSA_PK_TO_MBEDTLS_ERR(status);
1650 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001651 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001652
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001653 return 0;
Valerio Setti574a00b2023-06-21 19:47:37 +02001654#else /* !MBEDTLS_RSA_C */
1655 ((void) pk);
1656 ((void) md_alg);
1657 ((void) hash);
1658 ((void) hash_len);
1659 ((void) sig);
1660 ((void) sig_size);
1661 ((void) sig_len);
1662 ((void) f_rng);
1663 ((void) p_rng);
1664 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti4657f102023-06-21 13:55:16 +02001665#endif /* !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001666}
1667
Valerio Setti76d0f962023-06-23 13:32:54 +02001668const mbedtls_pk_info_t mbedtls_rsa_opaque_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001669 .type = MBEDTLS_PK_OPAQUE,
1670 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001671 .get_bitlen = opaque_get_bitlen,
1672 .can_do = rsa_opaque_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001673 .verify_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001674 .sign_func = rsa_opaque_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001675#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1676 .verify_rs_func = NULL,
1677 .sign_rs_func = NULL,
1678 .rs_alloc_func = NULL,
1679 .rs_free_func = NULL,
1680#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001681#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001682 .decrypt_func = rsa_opaque_decrypt,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001683#else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001684 .decrypt_func = NULL,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001685#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001686 .encrypt_func = NULL,
1687 .check_pair_func = NULL,
1688 .ctx_alloc_func = NULL,
1689 .ctx_free_func = NULL,
1690 .debug_func = NULL,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001691};
1692
1693#endif /* MBEDTLS_USE_PSA_CRYPTO */
1694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#endif /* MBEDTLS_PK_C */