blob: 957f64ed1e93e05605d2fa87fec57ad06b7be0d1 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
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é-Gonnard12e0ed92013-07-04 13:31:32 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000011#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000012#include "pk_wrap.h"
Neil Armstrongca5b55f2022-03-15 15:00:55 +010013#include "pkwrite.h"
Valerio Setti3f00b842023-05-15 12:57:06 +020014#include "pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020015
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050016#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000017#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020021#endif
Valerio Setti81d75122023-06-14 14:49:33 +020022#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020024#endif
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é-Gonnard7c5819e2013-07-10 12:29:57 +020027#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020028
Jerry Yub02ee182022-03-16 10:30:41 +080029#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020030#include "psa_util_internal.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020031#include "md_psa.h"
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010032#endif
33
Andres AG72849872017-01-19 11:24:33 +000034#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010035#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020036
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020037/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020039 */
Gilles Peskine449bd832023-01-11 14:50:10 +010040void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020041{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020042 ctx->pk_info = NULL;
43 ctx->pk_ctx = NULL;
Valerio Settie00954d2023-04-28 15:24:32 +020044#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti4f387ef2023-05-02 14:15:59 +020045 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settie00954d2023-04-28 15:24:32 +020046#endif /* MBEDTLS_PSA_CRYPTO_C */
Valerio Setti722f8f72023-05-17 15:31:21 +020047#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
48 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
49 ctx->pub_raw_len = 0;
50 ctx->ec_family = 0;
51 ctx->ec_bits = 0;
52#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053}
54
55/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020057 */
Gilles Peskine449bd832023-01-11 14:50:10 +010058void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020059{
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020061 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010062 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020063
Valerio Settie00954d2023-04-28 15:24:32 +020064 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010065 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
66 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020067
Valerio Settib5361262023-05-18 18:51:58 +020068#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
69 /* The ownership of the priv_id key for opaque keys is external of the PK
70 * module. It's the user responsibility to clear it after use. */
71 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
72 psa_destroy_key(ctx->priv_id);
73 }
74#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020077}
78
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020079#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020080/*
81 * Initialize a restart context
82 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020084{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020085 ctx->pk_info = NULL;
86 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020087}
88
89/*
90 * Free the components of a restart context
91 */
Gilles Peskine449bd832023-01-11 14:50:10 +010092void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020093{
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (ctx == NULL || ctx->pk_info == NULL ||
95 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020096 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020097 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200100
101 ctx->pk_info = NULL;
102 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200104#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200105
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200106/*
107 * Get pk_info structure from type
108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200110{
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112#if defined(MBEDTLS_RSA_C)
113 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200115#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200116#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return &mbedtls_eckeydh_info;
Valerio Setti81d75122023-06-14 14:49:33 +0200121#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti7ca13182023-01-27 13:22:42 +0100122#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200125#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200127 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200129 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200130}
131
132/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200133 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200136{
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (info == NULL || ctx->pk_info != NULL) {
138 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
139 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200140
Valerio Settib5361262023-05-18 18:51:58 +0200141 if ((info->ctx_alloc_func != NULL) &&
Valerio Settie00954d2023-04-28 15:24:32 +0200142 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return MBEDTLS_ERR_PK_ALLOC_FAILED;
144 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200145
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200146 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200149}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200150
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200151#if defined(MBEDTLS_USE_PSA_CRYPTO)
152/*
153 * Initialise a PSA-wrapping context
154 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
156 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200157{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100158 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200159 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100160 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if (ctx == NULL || ctx->pk_info != NULL) {
163 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
164 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
167 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
168 }
169 type = psa_get_key_type(&attributes);
170 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100171
Valerio Settif7cd4192023-06-30 18:11:29 +0200172#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200174 info = &mbedtls_ecdsa_opaque_info;
Valerio Settif7cd4192023-06-30 18:11:29 +0200175 } else
176#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
177 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200178 info = &mbedtls_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 } else {
180 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
181 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100182
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200183 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200184 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200187}
188#endif /* MBEDTLS_USE_PSA_CRYPTO */
189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100191/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200192 * Initialize an RSA-alt context
193 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
195 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
196 mbedtls_pk_rsa_alt_sign_func sign_func,
197 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200198{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_rsa_alt_context *rsa_alt;
200 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200201
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (ctx->pk_info != NULL) {
203 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
204 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
207 return MBEDTLS_ERR_PK_ALLOC_FAILED;
208 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200209
210 ctx->pk_info = info;
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200213
214 rsa_alt->key = key;
215 rsa_alt->decrypt_func = decrypt_func;
216 rsa_alt->sign_func = sign_func;
217 rsa_alt->key_len_func = key_len_func;
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200220}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200222
223/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200224 * Tell if a PK can do the operations of the given type
225 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100226int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200227{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500228 /* A context with null pk_info is not set up yet and can't do anything.
229 * For backward compatibility, also accept NULL instead of a context
230 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 if (ctx == NULL || ctx->pk_info == NULL) {
232 return 0;
233 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200236}
237
Neil Armstronga88b1582022-05-11 14:11:25 +0200238#if defined(MBEDTLS_USE_PSA_CRYPTO)
239/*
240 * Tell if a PK can do the operations of the given PSA algorithm
241 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100242int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
243 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200244{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200245 psa_key_usage_t key_usage;
246
Neil Armstronga88b1582022-05-11 14:11:25 +0200247 /* A context with null pk_info is not set up yet and can't do anything.
248 * For backward compatibility, also accept NULL instead of a context
249 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (ctx == NULL || ctx->pk_info == NULL) {
251 return 0;
252 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200253
254 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
256 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
257 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200258 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 PSA_ALG_IS_ECDH(alg) == 0) {
260 return 0;
261 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200262
Neil Armstrong408f6a62022-05-17 14:23:20 +0200263 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if (usage == 0 ||
265 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
266 PSA_KEY_USAGE_DECRYPT |
267 PSA_KEY_USAGE_DERIVE)) != 0) {
268 return 0;
269 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200270
Neil Armstronga88b1582022-05-11 14:11:25 +0200271 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (PSA_ALG_IS_SIGN_HASH(alg) &&
273 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
274 return 0;
275 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200276
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200278 mbedtls_pk_type_t type;
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200281 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
283 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200284 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200286 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 } else {
288 return 0;
289 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (ctx->pk_info->can_do(type) == 0) {
292 return 0;
293 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200296 case MBEDTLS_PK_ECKEY:
297 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
298 break;
299 case MBEDTLS_PK_RSA:
300 case MBEDTLS_PK_RSASSA_PSS:
301 key_usage = PSA_KEY_USAGE_SIGN_HASH |
302 PSA_KEY_USAGE_SIGN_MESSAGE |
303 PSA_KEY_USAGE_DECRYPT;
304 break;
305 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200306 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200308 }
309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200311 }
312
Neil Armstronga88b1582022-05-11 14:11:25 +0200313 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstronga88b1582022-05-11 14:11:25 +0200314 psa_status_t status;
315
Valerio Setti4f387ef2023-05-02 14:15:59 +0200316 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (status != PSA_SUCCESS) {
318 return 0;
319 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200320
Valerio Setti2bd53662023-12-05 10:14:06 +0100321 psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
322 /* Key's enrollment is available only when MBEDTLS_PSA_CRYPTO_CLIENT is
323 * defined, i.e. when the Mbed TLS implementation of PSA Crypto is being used.
324 * Even though we don't officially support using other implementations of PSA
325 * Crypto with TLS and X.509 (yet), we're still trying to simplify the life of
326 * people who would like to try it before it's officially supported. */
327#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
328 psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
329#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 key_usage = psa_get_key_usage_flags(&attributes);
331 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if ((key_usage & usage) != usage) {
334 return 0;
335 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200336
Neil Armstronga88b1582022-05-11 14:11:25 +0200337 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100338 * Common case: the key alg [or alg2] only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200339 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
340 * directly.
341 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
Valerio Setti2bd53662023-12-05 10:14:06 +0100342 * a fixed hash on key_alg [or key_alg2].
Neil Armstronga88b1582022-05-11 14:11:25 +0200343 */
Valerio Setti2bd53662023-12-05 10:14:06 +0100344 if (alg == key_alg) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return 1;
346 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100347#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
348 if (alg == key_alg2) {
349 return 1;
350 }
351#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200352
353 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100354 * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200355 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200356 * then alg is compliant with this key alg
357 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
361 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
362 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
363 return 1;
364 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100365#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
367 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
368 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
369 return 1;
370 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100371#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200375}
376#endif /* MBEDTLS_USE_PSA_CRYPTO */
377
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200378/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200380 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200382{
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (*hash_len != 0) {
384 return 0;
385 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200386
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200387 *hash_len = mbedtls_md_get_size_from_type(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (*hash_len == 0) {
390 return -1;
391 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200394}
395
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200396#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200397/*
398 * Helper to set up a restart context if needed
399 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
401 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200402{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200403 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (ctx == NULL || ctx->pk_info != NULL) {
405 return 0;
406 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200407
408 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
410 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
411 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
414 return MBEDTLS_ERR_PK_ALLOC_FAILED;
415 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200416
417 ctx->pk_info = info;
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200420}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200421#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200422
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200423/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200424 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200425 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
427 mbedtls_md_type_t md_alg,
428 const unsigned char *hash, size_t hash_len,
429 const unsigned char *sig, size_t sig_len,
430 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200431{
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100433 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (ctx->pk_info == NULL ||
437 pk_hashlen_helper(md_alg, &hash_len) != 0) {
438 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
439 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200440
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200441#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200442 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200444 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
449 return ret;
450 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200451
valerio38992cb2023-04-20 09:56:30 +0200452 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
456 mbedtls_pk_restart_free(rs_ctx);
457 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200460 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200461#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200462 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200463#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (ctx->pk_info->verify_func == NULL) {
466 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
467 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200468
valerio38992cb2023-04-20 09:56:30 +0200469 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200471}
472
473/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200474 * Verify a signature
475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
477 const unsigned char *hash, size_t hash_len,
478 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200479{
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
481 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200482}
483
484/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200485 * Verify a signature with options
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
488 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
489 const unsigned char *hash, size_t hash_len,
490 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200491{
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100493 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (ctx->pk_info == NULL) {
497 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
498 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (!mbedtls_pk_can_do(ctx, type)) {
501 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
502 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500505 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (options != NULL) {
507 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
508 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500511 }
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
515 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200516
Dave Rodgman02a53d72023-09-28 17:17:07 +0100517#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
519 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
520 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100521#endif
Andres AG72849872017-01-19 11:24:33 +0000522
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (options == NULL) {
524 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
525 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200526
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500527 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200528
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500529#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200531 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500532 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500533 int key_len;
534 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500535 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
536 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500537
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200538 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500539 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
540 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
542 p = buf + sizeof(buf);
543 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 if (key_len < 0) {
546 return key_len;
547 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
550 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
551 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 status = psa_import_key(&attributes,
554 buf + sizeof(buf) - key_len, key_len,
555 &key_id);
556 if (status != PSA_SUCCESS) {
557 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500558 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500559 }
560
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500561 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
562 * on a valid signature with trailing data in a buffer, but
563 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
564 * so for this reason the passed sig_len is overwritten. Smaller
565 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
567 mbedtls_pk_get_len(ctx) : sig_len;
568 status = psa_verify_hash(key_id, psa_sig_alg, hash,
569 hash_len, sig, signature_length);
570 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
573 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
574 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500577 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500579
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500580 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 } else
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500582#endif
583 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (sig_len < mbedtls_pk_get_len(ctx)) {
585 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
586 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
589 md_alg, (unsigned int) hash_len, hash,
590 pss_opts->mgf1_hash_id,
591 pss_opts->expected_salt_len,
592 sig);
593 if (ret != 0) {
594 return ret;
595 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200596
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 if (sig_len > mbedtls_pk_get_len(ctx)) {
598 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
599 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200602 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500603#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500605#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200606}
607
608/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200609 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200610 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100611int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
612 mbedtls_md_type_t md_alg,
613 const unsigned char *hash, size_t hash_len,
614 unsigned char *sig, size_t sig_size, size_t *sig_len,
615 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
616 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200617{
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100619 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
623 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
624 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200625
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200626#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200627 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200629 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
634 return ret;
635 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200636
valerio38992cb2023-04-20 09:56:30 +0200637 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 hash, hash_len,
639 sig, sig_size, sig_len,
640 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
643 mbedtls_pk_restart_free(rs_ctx);
644 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200647 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200648#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200649 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200650#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (ctx->pk_info->sign_func == NULL) {
653 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
654 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200655
valerio38992cb2023-04-20 09:56:30 +0200656 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 hash, hash_len,
658 sig, sig_size, sig_len,
659 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200660}
661
662/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200663 * Make a signature
664 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100665int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
666 const unsigned char *hash, size_t hash_len,
667 unsigned char *sig, size_t sig_size, size_t *sig_len,
668 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200669{
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
671 sig, sig_size, sig_len,
672 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200673}
674
Jerry Yu1d172a32022-03-12 19:12:05 +0800675#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200676/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800677 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800678 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100679int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
680 mbedtls_pk_context *ctx,
681 mbedtls_md_type_t md_alg,
682 const unsigned char *hash, size_t hash_len,
683 unsigned char *sig, size_t sig_size, size_t *sig_len,
684 int (*f_rng)(void *, unsigned char *, size_t),
685 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800686{
Jerry Yufb0621d2022-03-23 11:42:06 +0800687#if defined(MBEDTLS_RSA_C)
688 psa_algorithm_t psa_md_alg;
689#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800690 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (ctx->pk_info == NULL) {
693 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
694 }
Jerry Yud69439a2022-02-24 15:52:15 +0800695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (!mbedtls_pk_can_do(ctx, pk_type)) {
697 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
698 }
Jerry Yud69439a2022-02-24 15:52:15 +0800699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
701 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
702 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800703 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200704
Jerry Yu89107d12022-03-22 14:20:15 +0800705#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200706 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (psa_md_alg == 0) {
708 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
709 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200710
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200712 psa_status_t status;
713
Valerio Setti4f387ef2023-05-02 14:15:59 +0200714 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 hash, hash_len,
716 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500717 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200718 }
719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
721 ctx->pk_ctx, hash, hash_len,
722 sig, sig_size, sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800723#else /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Jerry Yu89107d12022-03-22 14:20:15 +0800725#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800726
727}
Jerry Yu1d172a32022-03-12 19:12:05 +0800728#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800729
730/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200731 * Decrypt message
732 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100733int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
734 const unsigned char *input, size_t ilen,
735 unsigned char *output, size_t *olen, size_t osize,
736 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200737{
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if (ctx->pk_info == NULL) {
739 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
740 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (ctx->pk_info->decrypt_func == NULL) {
743 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
744 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200745
valerio38992cb2023-04-20 09:56:30 +0200746 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200748}
749
750/*
751 * Encrypt message
752 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100753int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
754 const unsigned char *input, size_t ilen,
755 unsigned char *output, size_t *olen, size_t osize,
756 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200757{
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (ctx->pk_info == NULL) {
759 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
760 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (ctx->pk_info->encrypt_func == NULL) {
763 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
764 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200765
valerio38992cb2023-04-20 09:56:30 +0200766 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200768}
769
770/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100771 * Check public-private key pair
772 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100773int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
774 const mbedtls_pk_context *prv,
775 int (*f_rng)(void *, unsigned char *, size_t),
776 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100777{
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 if (pub->pk_info == NULL ||
779 prv->pk_info == NULL) {
780 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100781 }
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if (f_rng == NULL) {
784 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100785 }
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (prv->pk_info->check_pair_func == NULL) {
788 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
789 }
790
791 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
792 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
793 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
794 }
795 } else {
valerio8cbef4d2023-06-01 10:59:03 +0200796 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
797 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
799 }
800 }
801
valerio38992cb2023-04-20 09:56:30 +0200802 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
803 (mbedtls_pk_context *) prv,
804 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100805}
806
807/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200808 * Get key size in bits
809 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100810size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200811{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500812 /* For backward compatibility, accept NULL or a context that
813 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (ctx == NULL || ctx->pk_info == NULL) {
815 return 0;
816 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200817
valerio38992cb2023-04-20 09:56:30 +0200818 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200819}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200820
821/*
822 * Export debug information
823 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100824int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200825{
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (ctx->pk_info == NULL) {
827 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
828 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if (ctx->pk_info->debug_func == NULL) {
831 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
832 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200833
valerio38992cb2023-04-20 09:56:30 +0200834 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200836}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200837
838/*
839 * Access the PK type name
840 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100841const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200842{
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (ctx == NULL || ctx->pk_info == NULL) {
844 return "invalid PK";
845 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200848}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200849
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200850/*
851 * Access the PK type
852 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100853mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200854{
Gilles Peskine449bd832023-01-11 14:50:10 +0100855 if (ctx == NULL || ctx->pk_info == NULL) {
856 return MBEDTLS_PK_NONE;
857 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200860}
861
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100862#if defined(MBEDTLS_USE_PSA_CRYPTO)
863/*
864 * Load the key to a PSA key slot,
865 * then turn the PK context into a wrapper for that key slot.
866 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200867 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100868 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100869int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
870 mbedtls_svc_key_id_t *key,
871 psa_algorithm_t alg,
872 psa_key_usage_t usage,
873 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100874{
Valerio Setti81d75122023-06-14 14:49:33 +0200875#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700876 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200877 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200878 ((void) alg);
879 ((void) usage);
880 ((void) alg2);
Valerio Setti81d75122023-06-14 14:49:33 +0200881#else /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
882#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100884 size_t d_len;
885 psa_ecc_family_t curve_id;
886 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
887 psa_key_type_t key_type;
888 size_t bits;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100889 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100890
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100891 /* export the private key material in the format PSA wants */
Valerio Settiae8c6282023-05-18 18:57:57 +0200892#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1194ffa2023-05-24 13:15:58 +0200893 unsigned char d[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Valerio Settiae8c6282023-05-18 18:57:57 +0200894 status = psa_export_key(pk->priv_id, d, sizeof(d), &d_len);
895 if (status != PSA_SUCCESS) {
896 return psa_pk_status_to_mbedtls(status);
897 }
898
899 curve_id = pk->ec_family;
900 bits = pk->ec_bits;
901#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1194ffa2023-05-24 13:15:58 +0200902 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
Valerio Settiae8c6282023-05-18 18:57:57 +0200903 mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*pk);
904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
905
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200907 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 return ret;
909 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100910
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Valerio Settiae8c6282023-05-18 18:57:57 +0200912#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100914
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100915 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 psa_set_key_type(&attributes, key_type);
917 psa_set_key_bits(&attributes, bits);
918 psa_set_key_usage_flags(&attributes, usage);
919 psa_set_key_algorithm(&attributes, alg);
920 if (alg2 != PSA_ALG_NONE) {
921 psa_set_key_enrollment_algorithm(&attributes, alg2);
922 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100923
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100924 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 status = psa_import_key(&attributes, d, d_len, key);
Valerio Setti2d814992023-04-27 10:05:03 +0200926 mbedtls_platform_zeroize(d, sizeof(d));
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500928 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100930
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100931 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100932 mbedtls_pk_free(pk);
933 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100934
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 return mbedtls_pk_setup_opaque(pk, *key);
936 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200937#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100938#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100940 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
941 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
942 int key_len;
943 psa_status_t status;
944
945 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
947 if (key_len <= 0) {
948 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
949 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100950
951 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
953 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
954 psa_set_key_usage_flags(&attributes, usage);
955 psa_set_key_algorithm(&attributes, alg);
956 if (alg2 != PSA_ALG_NONE) {
957 psa_set_key_enrollment_algorithm(&attributes, alg2);
958 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100959
960 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 status = psa_import_key(&attributes,
962 buf + sizeof(buf) - key_len,
963 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500968 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100970
971 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 mbedtls_pk_free(pk);
973 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100974
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 return mbedtls_pk_setup_opaque(pk, *key);
976 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100977#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200978#endif /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100980}
981#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200982#endif /* MBEDTLS_PK_C */