blob: 2e00d4a2583a832e46d4e58f28750b1c53858e6e [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"
Valerio Settibd5b9c62024-01-08 16:49:48 +010032#include "mbedtls/psa_util.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020033
Tomi Fontanilles9f417702023-12-16 15:28:51 +020034#if defined(MBEDTLS_RSA_C)
35#include "pkwrite.h"
Valerio Setti18dd0002024-01-23 17:59:10 +010036#include "rsa_internal.h"
Tomi Fontanilles9f417702023-12-16 15:28:51 +020037#endif
38
Valerio Setti80d07982023-02-08 13:49:17 +010039#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020040#include "mbedtls/asn1write.h"
41#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010042#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020043#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010044
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020046
Andres AG72849872017-01-19 11:24:33 +000047#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010048#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020049#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010052static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020053{
Gilles Peskine449bd832023-01-11 14:50:10 +010054 return type == MBEDTLS_PK_RSA ||
55 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +020056}
57
valerio38992cb2023-04-20 09:56:30 +020058static size_t rsa_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020059{
valerio38992cb2023-04-20 09:56:30 +020060 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +010061 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +020062}
63
Neil Armstrong52f41f82022-02-22 15:30:24 +010064#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +020065static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +010066 const unsigned char *hash, size_t hash_len,
67 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020068{
valerio38992cb2023-04-20 09:56:30 +020069 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +010070 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
71 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
72 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
73 psa_status_t status;
Neil Armstrong52f41f82022-02-22 15:30:24 +010074 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +010075 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Valerio Setti18dd0002024-01-23 17:59:10 +010076 unsigned char *p = buf + sizeof(buf);
Neil Armstrong82cf8042022-03-03 12:30:59 +010077 psa_algorithm_t psa_alg_md =
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020078 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Gilles Peskine449bd832023-01-11 14:50:10 +010079 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +010080
Dave Rodgman2eab4622023-10-05 13:30:37 +010081#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
83 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
84 }
Dave Rodgman2eab4622023-10-05 13:30:37 +010085#endif
Neil Armstrong52f41f82022-02-22 15:30:24 +010086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (sig_len < rsa_len) {
88 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
89 }
Neil Armstrong52f41f82022-02-22 15:30:24 +010090
Valerio Setti135ebde2024-02-01 17:00:29 +010091 key_len = mbedtls_rsa_write_pubkey(rsa, buf, &p);
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (key_len <= 0) {
93 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
94 }
Neil Armstrong52f41f82022-02-22 15:30:24 +010095
Gilles Peskine449bd832023-01-11 14:50:10 +010096 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
97 psa_set_key_algorithm(&attributes, psa_alg_md);
98 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +010099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 status = psa_import_key(&attributes,
101 buf + sizeof(buf) - key_len, key_len,
102 &key_id);
103 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500104 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100105 goto cleanup;
106 }
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
109 sig, sig_len);
110 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500111 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100112 goto cleanup;
113 }
114 ret = 0;
115
116cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 status = psa_destroy_key(key_id);
118 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500119 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100123}
Valerio Setti5c26b302023-06-21 19:47:01 +0200124#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200125static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 const unsigned char *hash, size_t hash_len,
127 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100128{
Janos Follath24eed8d2019-11-22 13:21:35 +0000129 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valerio38992cb2023-04-20 09:56:30 +0200130 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200132
Dave Rodgman02a53d72023-09-28 17:17:07 +0100133#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
135 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
136 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100137#endif
Andres AG72849872017-01-19 11:24:33 +0000138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (sig_len < rsa_len) {
140 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
141 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
144 (unsigned int) hash_len,
145 hash, sig)) != 0) {
146 return ret;
147 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200148
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200149 /* The buffer contains a valid signature followed by extra data.
150 * We have a special error code for that so that so that callers can
151 * use mbedtls_pk_verify() to check "Does the buffer start with a
152 * valid signature?" and not just "Does the buffer contain a valid
153 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (sig_len > rsa_len) {
155 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
156 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200159}
Valerio Setti5c26b302023-06-21 19:47:01 +0200160#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200161
Tomi Fontanilles81746622023-07-16 13:06:06 +0300162#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100163int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
164 mbedtls_rsa_context *rsa_ctx,
165 const unsigned char *hash, size_t hash_len,
166 unsigned char *sig, size_t sig_size,
167 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100168{
Neil Armstrong98545682022-02-22 16:12:51 +0100169 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
170 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
171 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
172 psa_status_t status;
Neil Armstrong98545682022-02-22 16:12:51 +0100173 int key_len;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530174 unsigned char *buf = NULL;
Valerio Setti18dd0002024-01-23 17:59:10 +0100175 unsigned char *p;
176
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530177 buf = mbedtls_calloc(1, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
178 if (buf == NULL) {
179 return MBEDTLS_ERR_PK_ALLOC_FAILED;
180 }
Valerio Setti18dd0002024-01-23 17:59:10 +0100181 p = buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES;
Neil Armstrong98545682022-02-22 16:12:51 +0100182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
184 if (sig_size < *sig_len) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530185 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
187 }
Neil Armstrong98545682022-02-22 16:12:51 +0100188
Valerio Setti135ebde2024-02-01 17:00:29 +0100189 key_len = mbedtls_rsa_write_key(rsa_ctx, buf, &p);
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if (key_len <= 0) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530191 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
193 }
194 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
195 psa_set_key_algorithm(&attributes, alg);
196 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 status = psa_import_key(&attributes,
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530199 buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES - key_len, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 &key_id);
201 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500202 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100203 goto cleanup;
204 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 status = psa_sign_hash(key_id, alg, hash, hash_len,
206 sig, sig_size, sig_len);
207 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500208 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100209 goto cleanup;
210 }
211
212 ret = 0;
213
214cleanup:
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530215 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 status = psa_destroy_key(key_id);
217 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500218 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 }
220 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100221}
Tomi Fontanilles81746622023-07-16 13:06:06 +0300222#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu1d172a32022-03-12 19:12:05 +0800223
224#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200225static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 const unsigned char *hash, size_t hash_len,
227 unsigned char *sig, size_t sig_size, size_t *sig_len,
228 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800229{
Jerry Yu1d172a32022-03-12 19:12:05 +0800230 ((void) f_rng);
231 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800232
Jerry Yubd1b3272022-03-24 13:05:20 +0800233 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200234 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 if (psa_md_alg == 0) {
236 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
237 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
240 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200241 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800243}
Valerio Setti5c26b302023-06-21 19:47:01 +0200244#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200245static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 const unsigned char *hash, size_t hash_len,
247 unsigned char *sig, size_t sig_size, size_t *sig_len,
248 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200249{
valerio38992cb2023-04-20 09:56:30 +0200250 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100251
Dave Rodgman02a53d72023-09-28 17:17:07 +0100252#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
254 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
255 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100256#endif
Andres AG72849872017-01-19 11:24:33 +0000257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 *sig_len = mbedtls_rsa_get_len(rsa);
259 if (sig_size < *sig_len) {
260 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
261 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
264 md_alg, (unsigned int) hash_len,
265 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200266}
Valerio Setti5c26b302023-06-21 19:47:01 +0200267#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200268
Neil Armstrong18f43c72022-02-09 15:32:45 +0100269#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200270static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 const unsigned char *input, size_t ilen,
272 unsigned char *output, size_t *olen, size_t osize,
273 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100274{
valerio38992cb2023-04-20 09:56:30 +0200275 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100276 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
277 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
278 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
279 psa_status_t status;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100280 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100281 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Valerio Setti18dd0002024-01-23 17:59:10 +0100282 unsigned char *p = buf + sizeof(buf);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100283
284 ((void) f_rng);
285 ((void) p_rng);
286
287#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
289 return MBEDTLS_ERR_RSA_INVALID_PADDING;
290 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100291#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (ilen != mbedtls_rsa_get_len(rsa)) {
294 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
295 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100296
Valerio Setti135ebde2024-02-01 17:00:29 +0100297 key_len = mbedtls_rsa_write_key(rsa, buf, &p);
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (key_len <= 0) {
299 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
300 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
303 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
304 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 status = psa_import_key(&attributes,
307 buf + sizeof(buf) - key_len, key_len,
308 &key_id);
309 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500310 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100311 goto cleanup;
312 }
313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
315 input, ilen,
316 NULL, 0,
317 output, osize, olen);
318 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500319 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100320 goto cleanup;
321 }
322
323 ret = 0;
324
325cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 mbedtls_platform_zeroize(buf, sizeof(buf));
327 status = psa_destroy_key(key_id);
328 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500329 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100333}
Valerio Setti5c26b302023-06-21 19:47:01 +0200334#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200335static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 const unsigned char *input, size_t ilen,
337 unsigned char *output, size_t *olen, size_t osize,
338 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200339{
valerio38992cb2023-04-20 09:56:30 +0200340 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (ilen != mbedtls_rsa_get_len(rsa)) {
343 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
344 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
347 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200348}
Valerio Setti5c26b302023-06-21 19:47:01 +0200349#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200350
Neil Armstrong96a16a42022-02-10 10:40:11 +0100351#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200352static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 const unsigned char *input, size_t ilen,
354 unsigned char *output, size_t *olen, size_t osize,
355 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100356{
valerio38992cb2023-04-20 09:56:30 +0200357 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
360 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
361 psa_status_t status;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100362 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100363 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Valerio Setti18dd0002024-01-23 17:59:10 +0100364 unsigned char *p = buf + sizeof(buf);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100365
366 ((void) f_rng);
367 ((void) p_rng);
368
369#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
371 return MBEDTLS_ERR_RSA_INVALID_PADDING;
372 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100373#endif
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (mbedtls_rsa_get_len(rsa) > osize) {
376 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
377 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100378
Valerio Setti135ebde2024-02-01 17:00:29 +0100379 key_len = mbedtls_rsa_write_pubkey(rsa, buf, &p);
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (key_len <= 0) {
381 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
382 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
385 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
386 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 status = psa_import_key(&attributes,
389 buf + sizeof(buf) - key_len, key_len,
390 &key_id);
391 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500392 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100393 goto cleanup;
394 }
395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
397 input, ilen,
398 NULL, 0,
399 output, osize, olen);
400 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500401 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100402 goto cleanup;
403 }
404
405 ret = 0;
406
407cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 status = psa_destroy_key(key_id);
409 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500410 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100414}
Valerio Setti5c26b302023-06-21 19:47:01 +0200415#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200416static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 const unsigned char *input, size_t ilen,
418 unsigned char *output, size_t *olen, size_t osize,
419 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200420{
valerio38992cb2023-04-20 09:56:30 +0200421 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (*olen > osize) {
425 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
426 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
429 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200430}
Valerio Setti5c26b302023-06-21 19:47:01 +0200431#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200432
valerio38992cb2023-04-20 09:56:30 +0200433static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 int (*f_rng)(void *, unsigned char *, size_t),
435 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100436{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200437 (void) f_rng;
438 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200439 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
440 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100441}
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200444{
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (ctx != NULL) {
448 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
449 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200452}
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200455{
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
457 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200458}
459
valerio38992cb2023-04-20 09:56:30 +0200460static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200461{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200462#if defined(MBEDTLS_RSA_ALT)
463 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200464 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200465 (void) items;
466#else
valerio38992cb2023-04-20 09:56:30 +0200467 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200470 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200471 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200472
473 items++;
474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200475 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200476 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200477 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200478#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200479}
480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481const mbedtls_pk_info_t mbedtls_rsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +0200482 .type = MBEDTLS_PK_RSA,
483 .name = "RSA",
484 .get_bitlen = rsa_get_bitlen,
485 .can_do = rsa_can_do,
486 .verify_func = rsa_verify_wrap,
487 .sign_func = rsa_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +0200488#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
489 .verify_rs_func = NULL,
490 .sign_rs_func = NULL,
491 .rs_alloc_func = NULL,
492 .rs_free_func = NULL,
493#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +0200494 .decrypt_func = rsa_decrypt_wrap,
495 .encrypt_func = rsa_encrypt_wrap,
496 .check_pair_func = rsa_check_pair_wrap,
497 .ctx_alloc_func = rsa_alloc_wrap,
498 .ctx_free_func = rsa_free_wrap,
499 .debug_func = rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200500};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200502
Valerio Setti81d75122023-06-14 14:49:33 +0200503#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200504/*
505 * Generic EC key
506 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100507static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200508{
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 return type == MBEDTLS_PK_ECKEY ||
510 type == MBEDTLS_PK_ECKEY_DH ||
511 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200512}
513
valerio38992cb2023-04-20 09:56:30 +0200514static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200515{
Valerio Settia1b8af62023-05-17 15:34:57 +0200516#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
517 return pk->ec_bits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200518#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +0200519 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
520 return ecp->grp.pbits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200521#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200522}
523
Valerio Setti1cdddac2023-02-02 13:55:57 +0100524#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400525#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +0200526/* Common helper for ECDSA verify using PSA functions. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200527static int ecdsa_verify_psa(unsigned char *key, size_t key_len,
528 psa_ecc_family_t curve, size_t curve_bits,
529 const unsigned char *hash, size_t hash_len,
530 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400531{
Janos Follath24eed8d2019-11-22 13:21:35 +0000532 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200533 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100534 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settia1b8af62023-05-17 15:34:57 +0200535 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Valerio Settied7d6af2023-06-21 15:42:21 +0200536 size_t signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
Valerio Settibd5b9c62024-01-08 16:49:48 +0100537 size_t converted_sig_len;
Valerio Settied7d6af2023-06-21 15:42:21 +0200538 unsigned char extracted_sig[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
539 unsigned char *p;
540 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (curve == 0) {
543 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
544 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
547 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
548 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500549
Valerio Settied7d6af2023-06-21 15:42:21 +0200550 status = psa_import_key(&attributes, key, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500552 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400553 goto cleanup;
554 }
555
Valerio Settied7d6af2023-06-21 15:42:21 +0200556 if (signature_len > sizeof(extracted_sig)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500557 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
558 goto cleanup;
559 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 p = (unsigned char *) sig;
Valerio Setti315e4af2024-02-05 10:09:15 +0100562 ret = mbedtls_ecdsa_der_to_raw(curve_bits, p, sig_len, extracted_sig,
563 sizeof(extracted_sig), &converted_sig_len);
Valerio Settibd5b9c62024-01-08 16:49:48 +0100564 if (ret != 0) {
565 goto cleanup;
566 }
567
568 if (converted_sig_len != signature_len) {
569 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500570 goto cleanup;
571 }
572
Valerio Settied7d6af2023-06-21 15:42:21 +0200573 status = psa_verify_hash(key_id, psa_sig_md, hash, hash_len,
574 extracted_sig, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500576 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400578 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500579
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400580 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400581
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500582cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 status = psa_destroy_key(key_id);
584 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500585 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400589}
Valerio Settied7d6af2023-06-21 15:42:21 +0200590
Valerio Setti76d0f962023-06-23 13:32:54 +0200591static int ecdsa_opaque_verify_wrap(mbedtls_pk_context *pk,
592 mbedtls_md_type_t md_alg,
593 const unsigned char *hash, size_t hash_len,
594 const unsigned char *sig, size_t sig_len)
Valerio Settie7730772023-06-21 16:58:40 +0200595{
596 (void) md_alg;
597 unsigned char key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
598 size_t key_len;
599 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
600 psa_ecc_family_t curve;
601 size_t curve_bits;
602 psa_status_t status;
603
604 status = psa_get_key_attributes(pk->priv_id, &key_attr);
605 if (status != PSA_SUCCESS) {
606 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
607 }
608 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(&key_attr));
609 curve_bits = psa_get_key_bits(&key_attr);
610 psa_reset_key_attributes(&key_attr);
611
612 status = psa_export_public_key(pk->priv_id, key, sizeof(key), &key_len);
613 if (status != PSA_SUCCESS) {
614 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
615 }
616
617 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
618 hash, hash_len, sig, sig_len);
619}
620
Valerio Settied7d6af2023-06-21 15:42:21 +0200621#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
622static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
623 mbedtls_md_type_t md_alg,
624 const unsigned char *hash, size_t hash_len,
625 const unsigned char *sig, size_t sig_len)
626{
627 (void) md_alg;
628 psa_ecc_family_t curve = pk->ec_family;
629 size_t curve_bits = pk->ec_bits;
630
631 return ecdsa_verify_psa(pk->pub_raw, pk->pub_raw_len, curve, curve_bits,
632 hash, hash_len, sig, sig_len);
633}
634#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
635static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
636 mbedtls_md_type_t md_alg,
637 const unsigned char *hash, size_t hash_len,
638 const unsigned char *sig, size_t sig_len)
639{
640 (void) md_alg;
641 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
642 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
643 unsigned char key[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
644 size_t key_len;
645 size_t curve_bits;
646 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
647
648 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
649 MBEDTLS_ECP_PF_UNCOMPRESSED,
650 &key_len, key, sizeof(key));
651 if (ret != 0) {
652 return ret;
653 }
654
655 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
656 hash, hash_len, sig, sig_len);
657}
658#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400659#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200660static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 const unsigned char *hash, size_t hash_len,
662 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200663{
Janos Follath24eed8d2019-11-22 13:21:35 +0000664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200665 ((void) md_alg);
666
valerio38992cb2023-04-20 09:56:30 +0200667 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
671 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
672 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200675}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400676#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100677#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200678
Valerio Setti1cdddac2023-02-02 13:55:57 +0100679#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100680#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4ac2c182023-12-05 07:59:01 +0100681/* Common helper for ECDSA sign using PSA functions.
682 * Instead of extracting key's properties in order to check which kind of ECDSA
683 * signature it supports, we try both deterministic and non-deterministic.
684 */
Valerio Setti884c1ec2023-06-23 12:09:13 +0200685static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg,
Valerio Setti4657f102023-06-21 13:55:16 +0200686 const unsigned char *hash, size_t hash_len,
687 unsigned char *sig, size_t sig_size, size_t *sig_len)
688{
689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
690 psa_status_t status;
Valerio Settibd5b9c62024-01-08 16:49:48 +0100691 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
692 size_t key_bits = 0;
693
694 status = psa_get_key_attributes(key_id, &key_attr);
695 if (status != PSA_SUCCESS) {
696 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
697 }
698 key_bits = psa_get_key_bits(&key_attr);
699 psa_reset_key_attributes(&key_attr);
Valerio Setti884c1ec2023-06-23 12:09:13 +0200700
Valerio Setti4ac2c182023-12-05 07:59:01 +0100701 status = psa_sign_hash(key_id,
702 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)),
703 hash, hash_len, sig, sig_size, sig_len);
704 if (status == PSA_SUCCESS) {
705 goto done;
706 } else if (status != PSA_ERROR_NOT_PERMITTED) {
Valerio Setti884c1ec2023-06-23 12:09:13 +0200707 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
708 }
Valerio Setti884c1ec2023-06-23 12:09:13 +0200709
Valerio Setti4ac2c182023-12-05 07:59:01 +0100710 status = psa_sign_hash(key_id,
711 PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)),
712 hash, hash_len, sig, sig_size, sig_len);
Valerio Setti4657f102023-06-21 13:55:16 +0200713 if (status != PSA_SUCCESS) {
714 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
715 }
716
Valerio Setti4ac2c182023-12-05 07:59:01 +0100717done:
Valerio Settibb76f802024-02-06 16:57:23 +0100718 ret = mbedtls_ecdsa_raw_to_der(key_bits, sig, *sig_len, sig, sig_size, sig_len);
Valerio Setti4657f102023-06-21 13:55:16 +0200719
720 return ret;
721}
722
Valerio Setti76d0f962023-06-23 13:32:54 +0200723static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk,
724 mbedtls_md_type_t md_alg,
725 const unsigned char *hash, size_t hash_len,
726 unsigned char *sig, size_t sig_size,
727 size_t *sig_len,
728 int (*f_rng)(void *, unsigned char *, size_t),
729 void *p_rng)
Valerio Setti4657f102023-06-21 13:55:16 +0200730{
731 ((void) f_rng);
732 ((void) p_rng);
Valerio Setti4657f102023-06-21 13:55:16 +0200733
Valerio Setti884c1ec2023-06-23 12:09:13 +0200734 return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size,
Valerio Setti4657f102023-06-21 13:55:16 +0200735 sig_len);
736}
737
738#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti76d0f962023-06-23 13:32:54 +0200739/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
740 * using the same function. */
741#define ecdsa_sign_wrap ecdsa_opaque_sign_wrap
Valerio Setti4657f102023-06-21 13:55:16 +0200742#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +0200743static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 const unsigned char *hash, size_t hash_len,
745 unsigned char *sig, size_t sig_size, size_t *sig_len,
746 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +0100747{
Neil Armstronge9606902022-02-09 14:23:00 +0100748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronge9606902022-02-09 14:23:00 +0100749 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
750 psa_status_t status;
Valerio Settiae8c6282023-05-18 18:57:57 +0200751 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
752 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
753 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Neil Armstronge9606902022-02-09 14:23:00 +0100754 size_t curve_bits;
755 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +0200757 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +0200758 psa_algorithm_t psa_hash = mbedtls_md_psa_alg_from_type(md_alg);
759 psa_algorithm_t psa_sig_md = MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(psa_hash);
Neil Armstronge9606902022-02-09 14:23:00 +0100760 ((void) f_rng);
761 ((void) p_rng);
762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 if (curve == 0) {
764 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
765 }
Neil Armstronge9606902022-02-09 14:23:00 +0100766
Gilles Peskine13caa942022-10-04 22:59:26 +0200767 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +0100768 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 }
Gilles Peskine13caa942022-10-04 22:59:26 +0200770 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +0100772 goto cleanup;
773 }
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
776 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
777 psa_set_key_algorithm(&attributes, psa_sig_md);
778
Valerio Setti4657f102023-06-21 13:55:16 +0200779 status = psa_import_key(&attributes, buf, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500781 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +0100783 }
784
Valerio Setti884c1ec2023-06-23 12:09:13 +0200785 ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len);
Neil Armstronge9606902022-02-09 14:23:00 +0100786
787cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 mbedtls_platform_zeroize(buf, sizeof(buf));
789 status = psa_destroy_key(key_id);
790 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500791 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +0100793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +0100795}
Valerio Setti4657f102023-06-21 13:55:16 +0200796#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100797#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200798static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 const unsigned char *hash, size_t hash_len,
800 unsigned char *sig, size_t sig_size, size_t *sig_len,
801 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200802{
valerio38992cb2023-04-20 09:56:30 +0200803 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 md_alg, hash, hash_len,
805 sig, sig_size, sig_len,
806 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200807}
Valerio Setti1cdddac2023-02-02 13:55:57 +0100808#endif /* MBEDTLS_USE_PSA_CRYPTO */
809#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200810
Valerio Setti80d07982023-02-08 13:49:17 +0100811#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +0100812/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +0200813static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +0100814 const unsigned char *hash, size_t hash_len,
815 const unsigned char *sig, size_t sig_len,
816 void *rs_ctx);
817
valerio38992cb2023-04-20 09:56:30 +0200818static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +0100819 const unsigned char *hash, size_t hash_len,
820 unsigned char *sig, size_t sig_size, size_t *sig_len,
821 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
822 void *rs_ctx);
823
824/*
825 * Restart context for ECDSA operations with ECKEY context
826 *
827 * We need to store an actual ECDSA context, as we need to pass the same to
828 * the underlying ecdsa function, so we can't create it on the fly every time.
829 */
830typedef struct {
831 mbedtls_ecdsa_restart_ctx ecdsa_rs;
832 mbedtls_ecdsa_context ecdsa_ctx;
833} eckey_restart_ctx;
834
835static void *eckey_rs_alloc(void)
836{
837 eckey_restart_ctx *rs_ctx;
838
839 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
840
841 if (ctx != NULL) {
842 rs_ctx = ctx;
843 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
844 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
845 }
846
847 return ctx;
848}
849
850static void eckey_rs_free(void *ctx)
851{
852 eckey_restart_ctx *rs_ctx;
853
854 if (ctx == NULL) {
855 return;
856 }
857
858 rs_ctx = ctx;
859 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
860 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
861
862 mbedtls_free(ctx);
863}
864
valerio38992cb2023-04-20 09:56:30 +0200865static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +0100866 const unsigned char *hash, size_t hash_len,
867 const unsigned char *sig, size_t sig_len,
868 void *rs_ctx)
869{
870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
871 eckey_restart_ctx *rs = rs_ctx;
872
873 /* Should never happen */
874 if (rs == NULL) {
875 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
876 }
877
878 /* set up our own sub-context if needed (that is, on first run) */
879 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +0200880 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +0100881 }
882
valerio38992cb2023-04-20 09:56:30 +0200883 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +0100884 md_alg, hash, hash_len,
885 sig, sig_len, &rs->ecdsa_rs));
886
887cleanup:
888 return ret;
889}
890
valerio38992cb2023-04-20 09:56:30 +0200891static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +0100892 const unsigned char *hash, size_t hash_len,
893 unsigned char *sig, size_t sig_size, size_t *sig_len,
894 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
895 void *rs_ctx)
896{
897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
898 eckey_restart_ctx *rs = rs_ctx;
899
900 /* Should never happen */
901 if (rs == NULL) {
902 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
903 }
904
905 /* set up our own sub-context if needed (that is, on first run) */
906 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +0200907 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +0100908 }
909
valerio38992cb2023-04-20 09:56:30 +0200910 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +0100911 hash, hash_len, sig, sig_size, sig_len,
912 f_rng, p_rng, &rs->ecdsa_rs));
913
914cleanup:
915 return ret;
916}
Valerio Setti80d07982023-02-08 13:49:17 +0100917#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100918
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200919#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settibb7603a2023-06-21 18:34:54 +0200920#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerio38992cb2023-04-20 09:56:30 +0200921static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200922{
Valerio Setti9efa8c42023-05-19 13:27:30 +0200923 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200924 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +0200925 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200926 size_t prv_key_len;
Valerio Settiae8c6282023-05-18 18:57:57 +0200927 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +0200928
929 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
930 &prv_key_len);
931 ret = PSA_PK_TO_MBEDTLS_ERR(status);
932 if (ret != 0) {
933 return ret;
934 }
935
936 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
937 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
938 }
Valerio Settibb7603a2023-06-21 18:34:54 +0200939
940 return 0;
941}
942#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
943static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
944{
945 psa_status_t status;
946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
947 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
948 size_t prv_key_len;
Valerio Setti9efa8c42023-05-19 13:27:30 +0200949 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +0200950 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
951 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200952 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
953 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200954 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +0200955 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +0200956 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +0200957 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200958
Valerio Settia7cb8452023-05-22 18:39:43 +0200959 if (curve == 0) {
960 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
961 }
962
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200963 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
964 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
965
Valerio Settia1b8af62023-05-17 15:34:57 +0200966 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
967 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200968 if (ret != 0) {
Valerio Setti35d1dac2023-06-30 18:04:16 +0200969 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200970 return ret;
971 }
972
973 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti35d1dac2023-06-30 18:04:16 +0200974 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti1df94f82023-04-07 08:59:24 +0200975 ret = PSA_PK_TO_MBEDTLS_ERR(status);
976 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200977 return ret;
978 }
979
Valerio Setti35d1dac2023-06-30 18:04:16 +0200980 // From now on prv_key_buf is used to store the public key of prv.
Valerio Setti1df94f82023-04-07 08:59:24 +0200981 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
982 &prv_key_len);
983 ret = PSA_PK_TO_MBEDTLS_ERR(status);
984 destruction_status = psa_destroy_key(key_id);
985 if (ret != 0) {
986 return ret;
987 } else if (destruction_status != PSA_SUCCESS) {
988 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200989 }
990
Valerio Settia1b8af62023-05-17 15:34:57 +0200991 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
992 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +0200993 MBEDTLS_ECP_PF_UNCOMPRESSED,
994 &pub_key_len, pub_key_buf,
995 sizeof(pub_key_buf));
996 if (ret != 0) {
997 return ret;
998 }
999
1000 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1001 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1002 }
1003
1004 return 0;
1005}
Valerio Settibb7603a2023-06-21 18:34:54 +02001006#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001007
Valerio Settibb7603a2023-06-21 18:34:54 +02001008static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1009 int (*f_rng)(void *, unsigned char *, size_t),
1010 void *p_rng)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001011{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001012 (void) f_rng;
1013 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001014 return eckey_check_pair_psa(pub, prv);
Valerio Settibb7603a2023-06-21 18:34:54 +02001015}
1016#else /* MBEDTLS_USE_PSA_CRYPTO */
1017static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1018 int (*f_rng)(void *, unsigned char *, size_t),
1019 void *p_rng)
1020{
valerio38992cb2023-04-20 09:56:30 +02001021 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1022 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001023 f_rng, p_rng);
1024}
Valerio Settibb7603a2023-06-21 18:34:54 +02001025#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001026
Valerio Setti88a3aee2023-06-29 15:01:10 +02001027#if defined(MBEDTLS_USE_PSA_CRYPTO)
1028#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1029/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1030 * using the same function. */
1031#define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap
1032#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1033static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub,
1034 mbedtls_pk_context *prv,
1035 int (*f_rng)(void *, unsigned char *, size_t),
1036 void *p_rng)
1037{
1038 psa_status_t status;
1039 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1040 size_t exp_pub_key_len = 0;
1041 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1042 size_t pub_key_len = 0;
1043 int ret;
1044 (void) f_rng;
1045 (void) p_rng;
1046
1047 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1048 &exp_pub_key_len);
1049 if (status != PSA_SUCCESS) {
1050 ret = psa_pk_status_to_mbedtls(status);
1051 return ret;
1052 }
1053 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1054 &(mbedtls_pk_ec_ro(*pub)->Q),
1055 MBEDTLS_ECP_PF_UNCOMPRESSED,
1056 &pub_key_len, pub_key, sizeof(pub_key));
1057 if (ret != 0) {
1058 return ret;
1059 }
1060 if ((exp_pub_key_len != pub_key_len) ||
1061 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1062 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1063 }
1064 return 0;
1065}
1066#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1067#endif /* MBEDTLS_USE_PSA_CRYPTO */
1068
Valerio Settib5361262023-05-18 18:51:58 +02001069#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001070static void *eckey_alloc_wrap(void)
1071{
1072 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1073
1074 if (ctx != NULL) {
1075 mbedtls_ecp_keypair_init(ctx);
1076 }
1077
1078 return ctx;
1079}
1080
1081static void eckey_free_wrap(void *ctx)
1082{
1083 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1084 mbedtls_free(ctx);
1085}
Valerio Settib5361262023-05-18 18:51:58 +02001086#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001087
valerio38992cb2023-04-20 09:56:30 +02001088static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001089{
Valerio Settia1b8af62023-05-17 15:34:57 +02001090#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1091 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1092 items->name = "eckey.Q";
1093 items->value = pk;
Valerio Setti5c26b302023-06-21 19:47:01 +02001094#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001095 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001096 items->type = MBEDTLS_PK_DEBUG_ECP;
1097 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001098 items->value = &(ecp->Q);
Valerio Setti5c26b302023-06-21 19:47:01 +02001099#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001100}
1101
1102const mbedtls_pk_info_t mbedtls_eckey_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001103 .type = MBEDTLS_PK_ECKEY,
1104 .name = "EC",
1105 .get_bitlen = eckey_get_bitlen,
1106 .can_do = eckey_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001107#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001108 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001109#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1110 .verify_func = NULL,
1111#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001112#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001113 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001114#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1115 .sign_func = NULL,
1116#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001117#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001118 .verify_rs_func = eckey_verify_rs_wrap,
1119 .sign_rs_func = eckey_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001120 .rs_alloc_func = eckey_rs_alloc,
1121 .rs_free_func = eckey_rs_free,
1122#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1123 .decrypt_func = NULL,
1124 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001125 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001126#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1127 .ctx_alloc_func = NULL,
1128 .ctx_free_func = NULL,
1129#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001130 .ctx_alloc_func = eckey_alloc_wrap,
1131 .ctx_free_func = eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001132#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001133 .debug_func = eckey_debug,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001134};
1135
1136/*
1137 * EC key restricted to ECDH
1138 */
1139static int eckeydh_can_do(mbedtls_pk_type_t type)
1140{
1141 return type == MBEDTLS_PK_ECKEY ||
1142 type == MBEDTLS_PK_ECKEY_DH;
1143}
1144
1145const mbedtls_pk_info_t mbedtls_eckeydh_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001146 .type = MBEDTLS_PK_ECKEY_DH,
1147 .name = "EC_DH",
1148 .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */
1149 .can_do = eckeydh_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001150 .verify_func = NULL,
1151 .sign_func = NULL,
1152#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1153 .verify_rs_func = NULL,
1154 .sign_rs_func = NULL,
1155#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1156 .decrypt_func = NULL,
1157 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001158 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001159#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1160 .ctx_alloc_func = NULL,
1161 .ctx_free_func = NULL,
1162#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001163 .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */
1164 .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */
Valerio Settib5361262023-05-18 18:51:58 +02001165#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001166 .debug_func = eckey_debug, /* Same underlying key structure */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001167};
Valerio Setti1cdddac2023-02-02 13:55:57 +01001168
1169#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1170static int ecdsa_can_do(mbedtls_pk_type_t type)
1171{
1172 return type == MBEDTLS_PK_ECDSA;
1173}
1174
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001175#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001176static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 const unsigned char *hash, size_t hash_len,
1178 const unsigned char *sig, size_t sig_len,
1179 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001180{
Janos Follath24eed8d2019-11-22 13:21:35 +00001181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001182 ((void) md_alg);
1183
1184 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001185 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 hash, hash_len, sig, sig_len,
1187 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1190 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1191 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001194}
1195
valerio38992cb2023-04-20 09:56:30 +02001196static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 const unsigned char *hash, size_t hash_len,
1198 unsigned char *sig, size_t sig_size, size_t *sig_len,
1199 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1200 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001201{
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001203 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1205 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001206
1207}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001210{
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 if (ctx != NULL) {
1214 mbedtls_ecdsa_restart_init(ctx);
1215 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001216
Gilles Peskine449bd832023-01-11 14:50:10 +01001217 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001218}
1219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001221{
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 mbedtls_ecdsa_restart_free(ctx);
1223 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001224}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001225#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001227const mbedtls_pk_info_t mbedtls_ecdsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001228 .type = MBEDTLS_PK_ECDSA,
1229 .name = "ECDSA",
1230 .get_bitlen = eckey_get_bitlen, /* Compatible key structures */
1231 .can_do = ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001232#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001233 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001234#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1235 .verify_func = NULL,
1236#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001237#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001238 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001239#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1240 .sign_func = NULL,
1241#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001242#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001243 .verify_rs_func = ecdsa_verify_rs_wrap,
1244 .sign_rs_func = ecdsa_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001245 .rs_alloc_func = ecdsa_rs_alloc,
1246 .rs_free_func = ecdsa_rs_free,
1247#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1248 .decrypt_func = NULL,
1249 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001250 .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001251#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1252 .ctx_alloc_func = NULL,
1253 .ctx_free_func = NULL,
1254#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001255 .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */
1256 .ctx_free_func = eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001257#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001258 .debug_func = eckey_debug, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001259};
Valerio Setti7ca13182023-01-27 13:22:42 +01001260#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Valerio Settid9d74c22023-06-29 15:00:02 +02001261#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001263#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001264/*
1265 * Support for alternative RSA-private implementations
1266 */
1267
Gilles Peskine449bd832023-01-11 14:50:10 +01001268static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001269{
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001271}
1272
valerio38992cb2023-04-20 09:56:30 +02001273static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001274{
valerio38992cb2023-04-20 09:56:30 +02001275 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001276
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001278}
1279
valerio38992cb2023-04-20 09:56:30 +02001280static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 const unsigned char *hash, size_t hash_len,
1282 unsigned char *sig, size_t sig_size, size_t *sig_len,
1283 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001284{
valerio38992cb2023-04-20 09:56:30 +02001285 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001286
Dave Rodgman02a53d72023-09-28 17:17:07 +01001287#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (UINT_MAX < hash_len) {
1289 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1290 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001291#endif
Andres AG72849872017-01-19 11:24:33 +00001292
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1294 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1295 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1296 }
1297 if (*sig_len > sig_size) {
1298 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1299 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1302 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001303}
1304
valerio38992cb2023-04-20 09:56:30 +02001305static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 const unsigned char *input, size_t ilen,
1307 unsigned char *output, size_t *olen, size_t osize,
1308 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001309{
valerio38992cb2023-04-20 09:56:30 +02001310 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001311
1312 ((void) f_rng);
1313 ((void) p_rng);
1314
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1316 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1317 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 return rsa_alt->decrypt_func(rsa_alt->key,
1320 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001321}
1322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001323#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001324static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 int (*f_rng)(void *, unsigned char *, size_t),
1326 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001327{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001328 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001329 unsigned char hash[32];
1330 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001332
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1334 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001335 }
1336
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 memset(hash, 0x2a, sizeof(hash));
1338
valerio38992cb2023-04-20 09:56:30 +02001339 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 hash, sizeof(hash),
1341 sig, sizeof(sig), &sig_len,
1342 f_rng, p_rng)) != 0) {
1343 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001344 }
1345
valerio38992cb2023-04-20 09:56:30 +02001346 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 hash, sizeof(hash), sig, sig_len) != 0) {
1348 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1349 }
1350
1351 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001352}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001353#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001356{
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 if (ctx != NULL) {
1360 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1361 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001362
Gilles Peskine449bd832023-01-11 14:50:10 +01001363 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001364}
1365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001367{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001368 mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001369}
1370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001371const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001372 .type = MBEDTLS_PK_RSA_ALT,
1373 .name = "RSA-alt",
1374 .get_bitlen = rsa_alt_get_bitlen,
1375 .can_do = rsa_alt_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001376 .verify_func = NULL,
Valerio Settif69514a2023-06-21 18:16:49 +02001377 .sign_func = rsa_alt_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001378#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1379 .verify_rs_func = NULL,
1380 .sign_rs_func = NULL,
1381 .rs_alloc_func = NULL,
1382 .rs_free_func = NULL,
1383#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +02001384 .decrypt_func = rsa_alt_decrypt_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001385 .encrypt_func = NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001386#if defined(MBEDTLS_RSA_C)
Valerio Settif69514a2023-06-21 18:16:49 +02001387 .check_pair_func = rsa_alt_check_pair,
Valerio Setti97976e32023-06-23 14:08:26 +02001388#else
1389 .check_pair_func = NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001390#endif
Valerio Settif69514a2023-06-21 18:16:49 +02001391 .ctx_alloc_func = rsa_alt_alloc_wrap,
1392 .ctx_free_func = rsa_alt_free_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001393 .debug_func = NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001394};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001396
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001397#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +02001398static size_t opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001399{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001400 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001401 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001402
Valerio Setti4f387ef2023-05-02 14:15:59 +02001403 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 return 0;
1405 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 bits = psa_get_key_bits(&attributes);
1408 psa_reset_key_attributes(&attributes);
1409 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001410}
1411
Valerio Setti38913c12023-06-30 16:18:33 +02001412#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti76d0f962023-06-23 13:32:54 +02001413static int ecdsa_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001414{
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 return type == MBEDTLS_PK_ECKEY ||
1416 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001417}
1418
Valerio Setti76d0f962023-06-23 13:32:54 +02001419const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = {
Valerio Setti574a00b2023-06-21 19:47:37 +02001420 .type = MBEDTLS_PK_OPAQUE,
1421 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001422 .get_bitlen = opaque_get_bitlen,
1423 .can_do = ecdsa_opaque_can_do,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001424#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001425 .verify_func = ecdsa_opaque_verify_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001426#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1427 .verify_func = NULL,
1428#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1429#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Setti76d0f962023-06-23 13:32:54 +02001430 .sign_func = ecdsa_opaque_sign_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001431#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1432 .sign_func = NULL,
1433#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti97976e32023-06-23 14:08:26 +02001434#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1435 .verify_rs_func = NULL,
1436 .sign_rs_func = NULL,
1437 .rs_alloc_func = NULL,
1438 .rs_free_func = NULL,
1439#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1440 .decrypt_func = NULL,
1441 .encrypt_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001442 .check_pair_func = ecdsa_opaque_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001443 .ctx_alloc_func = NULL,
1444 .ctx_free_func = NULL,
1445 .debug_func = NULL,
Valerio Setti574a00b2023-06-21 19:47:37 +02001446};
Valerio Setti38913c12023-06-30 16:18:33 +02001447#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti574a00b2023-06-21 19:47:37 +02001448
Valerio Setti76d0f962023-06-23 13:32:54 +02001449static int rsa_opaque_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001450{
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 return type == MBEDTLS_PK_RSA ||
1452 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001453}
1454
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001455#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001456static int rsa_opaque_decrypt(mbedtls_pk_context *pk,
1457 const unsigned char *input, size_t ilen,
1458 unsigned char *output, size_t *olen, size_t osize,
1459 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001460{
Valerio Setti574a00b2023-06-21 19:47:37 +02001461 psa_status_t status;
1462
1463 /* PSA has its own RNG */
1464 (void) f_rng;
1465 (void) p_rng;
1466
1467 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
1468 input, ilen,
1469 NULL, 0,
1470 output, osize, olen);
1471 if (status != PSA_SUCCESS) {
1472 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1473 }
1474
1475 return 0;
1476}
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001477#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti574a00b2023-06-21 19:47:37 +02001478
Valerio Setti76d0f962023-06-23 13:32:54 +02001479static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
1480 const unsigned char *hash, size_t hash_len,
1481 unsigned char *sig, size_t sig_size, size_t *sig_len,
1482 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001483{
1484#if defined(MBEDTLS_RSA_C)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001485 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001486 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001487 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001488 psa_status_t status;
1489
1490 /* PSA has its own RNG */
1491 (void) f_rng;
1492 (void) p_rng;
1493
Valerio Setti4f387ef2023-05-02 14:15:59 +02001494 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001496 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001498
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 type = psa_get_key_type(&attributes);
1500 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001503 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Valerio Setti4657f102023-06-21 13:55:16 +02001504 } else {
1505 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1506 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001507
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001508 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001509 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 sig, sig_size, sig_len);
1511 if (status != PSA_SUCCESS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001513 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Valerio Setti4657f102023-06-21 13:55:16 +02001514 } else {
1515 return PSA_PK_TO_MBEDTLS_ERR(status);
1516 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001517 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001518
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001519 return 0;
Valerio Setti574a00b2023-06-21 19:47:37 +02001520#else /* !MBEDTLS_RSA_C */
1521 ((void) pk);
1522 ((void) md_alg);
1523 ((void) hash);
1524 ((void) hash_len);
1525 ((void) sig);
1526 ((void) sig_size);
1527 ((void) sig_len);
1528 ((void) f_rng);
1529 ((void) p_rng);
1530 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti4657f102023-06-21 13:55:16 +02001531#endif /* !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001532}
1533
Valerio Setti76d0f962023-06-23 13:32:54 +02001534const mbedtls_pk_info_t mbedtls_rsa_opaque_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001535 .type = MBEDTLS_PK_OPAQUE,
1536 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001537 .get_bitlen = opaque_get_bitlen,
1538 .can_do = rsa_opaque_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001539 .verify_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001540 .sign_func = rsa_opaque_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001541#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1542 .verify_rs_func = NULL,
1543 .sign_rs_func = NULL,
1544 .rs_alloc_func = NULL,
1545 .rs_free_func = NULL,
1546#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001547#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001548 .decrypt_func = rsa_opaque_decrypt,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001549#else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001550 .decrypt_func = NULL,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001551#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001552 .encrypt_func = NULL,
1553 .check_pair_func = NULL,
1554 .ctx_alloc_func = NULL,
1555 .ctx_free_func = NULL,
1556 .debug_func = NULL,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001557};
1558
1559#endif /* MBEDTLS_USE_PSA_CRYPTO */
1560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561#endif /* MBEDTLS_PK_C */