blob: c05ea5d1b16943f272a39c973dd4f366ca1cd5ed [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"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020021#include "rsa_internal.h"
22#endif
Valerio Setti81d75122023-06-14 14:49:33 +020023#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020025#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020028#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020029
Gilles Peskine0b172552024-01-18 14:11:26 +010030#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020031#include "psa_util_internal.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010032#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010033#endif
34
Andres AG72849872017-01-19 11:24:33 +000035#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010036#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020037
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020038/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020040 */
Gilles Peskine449bd832023-01-11 14:50:10 +010041void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020042{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020043 ctx->pk_info = NULL;
44 ctx->pk_ctx = NULL;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020045#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4f387ef2023-05-02 14:15:59 +020046 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020047#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti722f8f72023-05-17 15:31:21 +020048#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
49 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
50 ctx->pub_raw_len = 0;
51 ctx->ec_family = 0;
52 ctx->ec_bits = 0;
53#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020054}
55
56/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060{
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010063 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020064
Valerio Settie00954d2023-04-28 15:24:32 +020065 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010066 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
67 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020068
Valerio Settib5361262023-05-18 18:51:58 +020069#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
70 /* The ownership of the priv_id key for opaque keys is external of the PK
71 * module. It's the user responsibility to clear it after use. */
72 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
73 psa_destroy_key(ctx->priv_id);
74 }
75#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020078}
79
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020080#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020081/*
82 * Initialize a restart context
83 */
Gilles Peskine449bd832023-01-11 14:50:10 +010084void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020085{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020086 ctx->pk_info = NULL;
87 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020088}
89
90/*
91 * Free the components of a restart context
92 */
Gilles Peskine449bd832023-01-11 14:50:10 +010093void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020094{
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if (ctx == NULL || ctx->pk_info == NULL ||
96 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020097 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020098 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200101
102 ctx->pk_info = NULL;
103 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200104}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200105#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200106
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200107/*
108 * Get pk_info structure from type
109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111{
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_RSA_C)
114 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200116#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200117#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return &mbedtls_eckeydh_info;
Valerio Setti81d75122023-06-14 14:49:33 +0200122#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti7ca13182023-01-27 13:22:42 +0100123#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200126#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200128 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200130 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200131}
132
133/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200134 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200137{
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (info == NULL || ctx->pk_info != NULL) {
139 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
140 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141
Valerio Settib5361262023-05-18 18:51:58 +0200142 if ((info->ctx_alloc_func != NULL) &&
Valerio Settie00954d2023-04-28 15:24:32 +0200143 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return MBEDTLS_ERR_PK_ALLOC_FAILED;
145 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200146
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200147 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200151
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200152#if defined(MBEDTLS_USE_PSA_CRYPTO)
153/*
154 * Initialise a PSA-wrapping context
155 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
157 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200158{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100159 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100161 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (ctx == NULL || ctx->pk_info != NULL) {
164 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
165 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
168 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
169 }
170 type = psa_get_key_type(&attributes);
171 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100172
Valerio Settif7cd4192023-06-30 18:11:29 +0200173#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200175 info = &mbedtls_ecdsa_opaque_info;
Valerio Settif7cd4192023-06-30 18:11:29 +0200176 } else
177#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
178 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200179 info = &mbedtls_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 } else {
181 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
182 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100183
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200184 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200185 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200188}
189#endif /* MBEDTLS_USE_PSA_CRYPTO */
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100192/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200193 * Initialize an RSA-alt context
194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
196 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
197 mbedtls_pk_rsa_alt_sign_func sign_func,
198 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_rsa_alt_context *rsa_alt;
201 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (ctx->pk_info != NULL) {
204 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
205 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
208 return MBEDTLS_ERR_PK_ALLOC_FAILED;
209 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200210
211 ctx->pk_info = info;
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200214
215 rsa_alt->key = key;
216 rsa_alt->decrypt_func = decrypt_func;
217 rsa_alt->sign_func = sign_func;
218 rsa_alt->key_len_func = key_len_func;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200223
224/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200225 * Tell if a PK can do the operations of the given type
226 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200228{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 /* A context with null pk_info is not set up yet and can't do anything.
230 * For backward compatibility, also accept NULL instead of a context
231 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (ctx == NULL || ctx->pk_info == NULL) {
233 return 0;
234 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200237}
238
Neil Armstronga88b1582022-05-11 14:11:25 +0200239#if defined(MBEDTLS_USE_PSA_CRYPTO)
240/*
241 * Tell if a PK can do the operations of the given PSA algorithm
242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
244 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200245{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200246 psa_key_usage_t key_usage;
247
Neil Armstronga88b1582022-05-11 14:11:25 +0200248 /* A context with null pk_info is not set up yet and can't do anything.
249 * For backward compatibility, also accept NULL instead of a context
250 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (ctx == NULL || ctx->pk_info == NULL) {
252 return 0;
253 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200254
255 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
257 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
258 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200259 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 PSA_ALG_IS_ECDH(alg) == 0) {
261 return 0;
262 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200263
Neil Armstrong408f6a62022-05-17 14:23:20 +0200264 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (usage == 0 ||
266 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
267 PSA_KEY_USAGE_DECRYPT |
268 PSA_KEY_USAGE_DERIVE)) != 0) {
269 return 0;
270 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200271
Neil Armstronga88b1582022-05-11 14:11:25 +0200272 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (PSA_ALG_IS_SIGN_HASH(alg) &&
274 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
275 return 0;
276 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200279 mbedtls_pk_type_t type;
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200282 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
284 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200285 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200287 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 } else {
289 return 0;
290 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (ctx->pk_info->can_do(type) == 0) {
293 return 0;
294 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200297 case MBEDTLS_PK_ECKEY:
298 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
299 break;
300 case MBEDTLS_PK_RSA:
301 case MBEDTLS_PK_RSASSA_PSS:
302 key_usage = PSA_KEY_USAGE_SIGN_HASH |
303 PSA_KEY_USAGE_SIGN_MESSAGE |
304 PSA_KEY_USAGE_DECRYPT;
305 break;
306 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200307 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200309 }
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200312 }
313
Neil Armstronga88b1582022-05-11 14:11:25 +0200314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstronga88b1582022-05-11 14:11:25 +0200315 psa_status_t status;
316
Valerio Setti4f387ef2023-05-02 14:15:59 +0200317 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (status != PSA_SUCCESS) {
319 return 0;
320 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200321
Valerio Setti2bd53662023-12-05 10:14:06 +0100322 psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
323 /* Key's enrollment is available only when MBEDTLS_PSA_CRYPTO_CLIENT is
324 * defined, i.e. when the Mbed TLS implementation of PSA Crypto is being used.
325 * Even though we don't officially support using other implementations of PSA
326 * Crypto with TLS and X.509 (yet), we're still trying to simplify the life of
327 * people who would like to try it before it's officially supported. */
328#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
329 psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
330#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 key_usage = psa_get_key_usage_flags(&attributes);
332 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if ((key_usage & usage) != usage) {
335 return 0;
336 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200337
Neil Armstronga88b1582022-05-11 14:11:25 +0200338 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100339 * Common case: the key alg [or alg2] only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200340 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
341 * directly.
342 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
Valerio Setti2bd53662023-12-05 10:14:06 +0100343 * a fixed hash on key_alg [or key_alg2].
Neil Armstronga88b1582022-05-11 14:11:25 +0200344 */
Valerio Setti2bd53662023-12-05 10:14:06 +0100345 if (alg == key_alg) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return 1;
347 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100348#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
349 if (alg == key_alg2) {
350 return 1;
351 }
352#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200353
354 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100355 * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200356 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200357 * then alg is compliant with this key alg
358 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
362 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
363 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
364 return 1;
365 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100366#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
368 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
369 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
370 return 1;
371 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100372#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200373 }
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200376}
377#endif /* MBEDTLS_USE_PSA_CRYPTO */
378
Gilles Peskine9cd2e9a2024-01-24 13:40:09 +0100379#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine6ea18362024-01-18 14:16:27 +0100380#if defined(MBEDTLS_RSA_C)
381static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa,
382 int want_crypt)
383{
384 if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) {
385 if (want_crypt) {
386 mbedtls_md_type_t md_type = mbedtls_rsa_get_md_alg(rsa);
387 return PSA_ALG_RSA_OAEP(mbedtls_md_psa_alg_from_type(md_type));
388 } else {
389 return PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH);
390 }
391 } else {
392 if (want_crypt) {
393 return PSA_ALG_RSA_PKCS1V15_CRYPT;
394 } else {
395 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
396 }
397 }
398}
399#endif /* MBEDTLS_RSA_C */
400
Gilles Peskine0b172552024-01-18 14:11:26 +0100401int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
402 psa_key_usage_t usage,
403 psa_key_attributes_t *attributes)
404{
405 mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
406
Gilles Peskineace7c772024-01-18 17:47:54 +0100407 psa_key_usage_t more_usage = usage;
408 if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) {
409 more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
410 } else if (usage == PSA_KEY_USAGE_SIGN_HASH) {
411 more_usage |= PSA_KEY_USAGE_VERIFY_HASH;
412 } else if (usage == PSA_KEY_USAGE_DECRYPT) {
413 more_usage |= PSA_KEY_USAGE_ENCRYPT;
414 }
415 more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
416
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100417 int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE ||
418 usage == PSA_KEY_USAGE_VERIFY_HASH ||
419 usage == PSA_KEY_USAGE_ENCRYPT);
420
Gilles Peskine0b172552024-01-18 14:11:26 +0100421 switch (pk_type) {
Gilles Peskine6ea18362024-01-18 14:16:27 +0100422#if defined(MBEDTLS_RSA_C)
423 case MBEDTLS_PK_RSA:
Gilles Peskineace7c772024-01-18 17:47:54 +0100424 {
Gilles Peskinee8209752024-02-01 21:00:33 +0100425 int want_crypt = 0; /* 0: sign/verify; 1: encrypt/decrypt */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100426 switch (usage) {
427 case PSA_KEY_USAGE_SIGN_MESSAGE:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100428 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100429 case PSA_KEY_USAGE_VERIFY_MESSAGE:
430 case PSA_KEY_USAGE_VERIFY_HASH:
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100431 /* Nothing to do. */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100432 break;
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100433 case PSA_KEY_USAGE_DECRYPT:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100434 case PSA_KEY_USAGE_ENCRYPT:
435 want_crypt = 1;
436 break;
437 default:
438 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
439 }
440 /* Detect the presence of a private key in a way that works both
441 * in CRT and non-CRT configurations. */
442 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk);
443 int has_private = (mbedtls_rsa_check_privkey(rsa) == 0);
444 if (want_private && !has_private) {
445 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
446 }
447 psa_set_key_type(attributes, (want_private ?
448 PSA_KEY_TYPE_RSA_KEY_PAIR :
449 PSA_KEY_TYPE_RSA_PUBLIC_KEY));
Gilles Peskine55effd92024-01-23 18:07:36 +0100450 psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk));
Gilles Peskine6ea18362024-01-18 14:16:27 +0100451 psa_set_key_algorithm(attributes,
452 psa_algorithm_for_rsa(rsa, want_crypt));
453 break;
Gilles Peskineace7c772024-01-18 17:47:54 +0100454 }
Gilles Peskine6ea18362024-01-18 14:16:27 +0100455#endif /* MBEDTLS_RSA_C */
456
Gilles Peskineace7c772024-01-18 17:47:54 +0100457#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
458 case MBEDTLS_PK_ECKEY:
459 case MBEDTLS_PK_ECKEY_DH:
460 case MBEDTLS_PK_ECDSA:
461 {
462 int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH);
463 int derive_ok = (pk_type != MBEDTLS_PK_ECDSA);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100464#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskinecb3b4ca2024-02-02 13:12:39 +0100465 psa_ecc_family_t family = pk->ec_family;
466 size_t bits = pk->ec_bits;
467 int has_private = 0;
468 if (pk->priv_id != MBEDTLS_SVC_KEY_ID_INIT) {
469 has_private = 1;
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100470 }
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100471#else
Gilles Peskineae2668b2024-02-01 20:48:04 +0100472 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskineace7c772024-01-18 17:47:54 +0100473 int has_private = (ec->d.n != 0);
474 size_t bits = 0;
475 psa_ecc_family_t family =
476 mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100477#endif
Gilles Peskineace7c772024-01-18 17:47:54 +0100478 psa_algorithm_t alg = 0;
479 switch (usage) {
480 case PSA_KEY_USAGE_SIGN_MESSAGE:
481 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskineace7c772024-01-18 17:47:54 +0100482 case PSA_KEY_USAGE_VERIFY_MESSAGE:
483 case PSA_KEY_USAGE_VERIFY_HASH:
484 if (!sign_ok) {
485 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
486 }
487#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
488 alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
489#else
490 alg = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
491#endif
492 break;
493 case PSA_KEY_USAGE_DERIVE:
Gilles Peskineace7c772024-01-18 17:47:54 +0100494 alg = PSA_ALG_ECDH;
495 if (!derive_ok) {
496 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
497 }
498 break;
499 default:
500 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
501 }
502 if (want_private && !has_private) {
503 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
504 }
505 psa_set_key_type(attributes, (want_private ?
506 PSA_KEY_TYPE_ECC_KEY_PAIR(family) :
507 PSA_KEY_TYPE_ECC_PUBLIC_KEY(family)));
508 psa_set_key_bits(attributes, bits);
509 psa_set_key_algorithm(attributes, alg);
510 break;
511 }
512#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
513
Gilles Peskine0b172552024-01-18 14:11:26 +0100514#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
515 case MBEDTLS_PK_RSA_ALT:
516 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
517#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
518
Gilles Peskine758d8c72024-01-22 20:53:21 +0100519#if defined(MBEDTLS_USE_PSA_CRYPTO)
520 case MBEDTLS_PK_OPAQUE:
521 {
522 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
523 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
524 status = psa_get_key_attributes(pk->priv_id, &old_attributes);
525 if (status != PSA_SUCCESS) {
526 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
527 }
528 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
529 switch (usage) {
530 case PSA_KEY_USAGE_SIGN_MESSAGE:
531 case PSA_KEY_USAGE_SIGN_HASH:
532 case PSA_KEY_USAGE_VERIFY_MESSAGE:
533 case PSA_KEY_USAGE_VERIFY_HASH:
534 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) ||
535 old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) {
536 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
537 }
538 break;
539 case PSA_KEY_USAGE_DECRYPT:
540 case PSA_KEY_USAGE_ENCRYPT:
541 if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) {
542 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
543 }
544 break;
545 case PSA_KEY_USAGE_DERIVE:
546 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) {
547 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
548 }
549 break;
Gilles Peskine758d8c72024-01-22 20:53:21 +0100550 default:
551 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
552 }
553 psa_key_type_t new_type = old_type;
554 /* Opaque keys are always key pairs, so we don't need a check
555 * on the input if the required usage is private. We just need
556 * to adjust the type correctly if the required usage is public. */
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100557 if (!want_private) {
Gilles Peskine758d8c72024-01-22 20:53:21 +0100558 new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type);
559 }
560 more_usage = psa_get_key_usage_flags(&old_attributes);
Gilles Peskine793920c2024-02-01 21:26:54 +0100561 if ((usage & more_usage) == 0) {
562 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
563 }
Gilles Peskine758d8c72024-01-22 20:53:21 +0100564 psa_set_key_type(attributes, new_type);
565 psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes));
566 psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes));
567 break;
568 }
569#endif /* MBEDTLS_USE_PSA_CRYPTO */
570
Gilles Peskine0b172552024-01-18 14:11:26 +0100571 default:
572 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
573 }
574
Gilles Peskineace7c772024-01-18 17:47:54 +0100575 psa_set_key_usage_flags(attributes, more_usage);
Gilles Peskine1f97e732024-01-18 14:14:24 +0100576 psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
Gilles Peskine0b172552024-01-18 14:11:26 +0100577
578 return 0;
579}
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100580
581#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_USE_PSA_CRYPTO)
582static psa_status_t export_import_into_psa(mbedtls_svc_key_id_t old_key_id,
583 const psa_key_attributes_t *attributes,
584 mbedtls_svc_key_id_t *new_key_id)
585{
586 unsigned char key_buffer[PSA_EXPORT_KEY_PAIR_MAX_SIZE];
587 size_t key_length = 0;
588 psa_status_t status = psa_export_key(old_key_id,
589 key_buffer, sizeof(key_buffer),
590 &key_length);
591 if (status != PSA_SUCCESS) {
592 return status;
593 }
594 status = psa_import_key(attributes, key_buffer, key_length, new_key_id);
595 mbedtls_platform_zeroize(key_buffer, key_length);
596 return status;
597}
598
599static int copy_into_psa(mbedtls_svc_key_id_t old_key_id,
600 const psa_key_attributes_t *attributes,
601 mbedtls_svc_key_id_t *new_key_id)
602{
603 /* Normally, we prefer copying: it's more efficient and works even
604 * for non-exportable keys. */
605 psa_status_t status = psa_copy_key(old_key_id, attributes, new_key_id);
606 if (status == PSA_ERROR_NOT_PERMITTED /*missing COPY usage*/ ||
607 status == PSA_ERROR_INVALID_ARGUMENT /*incompatible policy*/) {
608 /* There are edge cases where copying won't work, but export+import
609 * might:
610 * - If the old key does not allow PSA_KEY_USAGE_COPY.
611 * - If the old key's usage does not allow what attributes wants.
612 * Because the key was intended for use in the pk module, and may
613 * have had a policy chosen solely for what pk needs rather than
614 * based on a detailed understanding of PSA policies, we are a bit
615 * more liberal than psa_copy_key() here.
616 */
617 /* Here we need to check that the types match, otherwise we risk
618 * importing nonsensical data. */
619 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
620 status = psa_get_key_attributes(old_key_id, &old_attributes);
621 if (status != PSA_SUCCESS) {
622 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
623 }
624 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
625 psa_reset_key_attributes(&old_attributes);
626 if (old_type != psa_get_key_type(attributes)) {
627 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
628 }
629 status = export_import_into_psa(old_key_id, attributes, new_key_id);
630 }
631 return PSA_PK_TO_MBEDTLS_ERR(status);
632}
633#endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_USE_PSA_CRYPTO */
634
635static int import_pair_into_psa(const mbedtls_pk_context *pk,
636 const psa_key_attributes_t *attributes,
637 mbedtls_svc_key_id_t *key_id)
638{
639 switch (mbedtls_pk_get_type(pk)) {
640#if defined(MBEDTLS_RSA_C)
641 case MBEDTLS_PK_RSA:
642 {
643 if (psa_get_key_type(attributes) != PSA_KEY_TYPE_RSA_KEY_PAIR) {
644 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
645 }
646 unsigned char key_buffer[
647 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)];
648 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
649 unsigned char *key_data = key_end;
650 int ret = mbedtls_rsa_write_key(mbedtls_pk_rsa(*pk),
651 key_buffer, &key_data);
652 if (ret < 0) {
653 return ret;
654 }
655 size_t key_length = key_end - key_data;
656 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
657 key_data, key_length,
658 key_id));
659 mbedtls_platform_zeroize(key_data, key_length);
660 return ret;
661 }
662#endif /* MBEDTLS_RSA_C */
663
664#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
665 case MBEDTLS_PK_ECKEY:
666 case MBEDTLS_PK_ECKEY_DH:
667 case MBEDTLS_PK_ECDSA:
668 {
669 /* We need to check the curve family, otherwise the import could
670 * succeed with nonsensical data.
671 * We don't check the bit-size: it's optional in attributes,
672 * and if it's specified, psa_import_key() will know from the key
673 * data length and will check that the bit-size matches. */
674 psa_key_type_t to_type = psa_get_key_type(attributes);
675#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
676 psa_ecc_family_t from_family = pk->ec_family;
677#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
678 /* We're only reading the key, but mbedtls_ecp_write_key()
679 * is missing a const annotation on its key parameter, so
680 * we need the non-const accessor here. */
681 mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*pk);
682 size_t from_bits = 0;
683 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
684 &from_bits);
685#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
686 if (to_type != PSA_KEY_TYPE_ECC_KEY_PAIR(from_family)) {
687 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
688 }
689
690#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
691 if (mbedtls_svc_key_id_is_null(pk->priv_id)) {
692 /* We have a public key and want a key pair. */
693 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
694 }
695 return copy_into_psa(pk->priv_id, attributes, key_id);
696#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
697 if (ec->d.n == 0) {
698 /* Private key not set. Assume the input is a public key only.
699 * (The other possibility is that it's an incomplete object
700 * where the group is set but neither the public key nor
701 * the private key. This is not possible through ecp.h
702 * functions, so we don't bother reporting a more suitable
703 * error in that case.) */
704 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
705 }
706 unsigned char key_buffer[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100707 /* Make sure to pass the exact key length to
708 * mbedtls_ecp_write_key(), because it writes Montgomery keys
709 * at the start of the buffer but Weierstrass keys at the
710 * end of the buffer. */
711 size_t key_length = PSA_BITS_TO_BYTES(ec->grp.nbits);
712 int ret = mbedtls_ecp_write_key(ec, key_buffer, key_length);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100713 if (ret < 0) {
714 return ret;
715 }
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100716 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100717 key_buffer, key_length,
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100718 key_id));
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100719 mbedtls_platform_zeroize(key_buffer, key_length);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100720 return ret;
721#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
722 }
723#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
724
725#if defined(MBEDTLS_USE_PSA_CRYPTO)
726 case MBEDTLS_PK_OPAQUE:
727 return copy_into_psa(pk->priv_id, attributes, key_id);
728#endif /* MBEDTLS_USE_PSA_CRYPTO */
729
730 default:
731 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
732 }
733}
734
735static int import_public_into_psa(const mbedtls_pk_context *pk,
736 const psa_key_attributes_t *attributes,
737 mbedtls_svc_key_id_t *key_id)
738{
739 psa_key_type_t psa_type = psa_get_key_type(attributes);
740
741#if defined(MBEDTLS_RSA_C) || \
742 (defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)) || \
743 defined(MBEDTLS_USE_PSA_CRYPTO)
744 unsigned char key_buffer[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
745#endif
746 unsigned char *key_data = NULL;
747 size_t key_length = 0;
748
749 switch (mbedtls_pk_get_type(pk)) {
750#if defined(MBEDTLS_RSA_C)
751 case MBEDTLS_PK_RSA:
752 {
753 if (psa_type != PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
754 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
755 }
756 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
757 key_data = key_end;
758 int ret = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*pk),
759 key_buffer, &key_data);
760 if (ret < 0) {
761 return ret;
762 }
763 key_length = (size_t) ret;
764 break;
765 }
766#endif /*MBEDTLS_RSA_C */
767
768#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
769 case MBEDTLS_PK_ECKEY:
770 case MBEDTLS_PK_ECKEY_DH:
771 case MBEDTLS_PK_ECDSA:
772 {
773 /* We need to check the curve family, otherwise the import could
774 * succeed with nonsensical data.
775 * We don't check the bit-size: it's optional in attributes,
776 * and if it's specified, psa_import_key() will know from the key
777 * data length and will check that the bit-size matches. */
778#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
779 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)) {
780 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
781 }
782 key_data = (unsigned char *) pk->pub_raw;
783 key_length = pk->pub_raw_len;
784#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
785 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
786 size_t from_bits = 0;
787 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
788 &from_bits);
Gilles Peskine1d338762024-02-12 14:18:26 +0100789 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(from_family)) {
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100790 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
791 }
792 int ret = mbedtls_ecp_write_public_key(
793 ec, MBEDTLS_ECP_PF_UNCOMPRESSED,
794 &key_length, key_buffer, sizeof(key_buffer));
795 if (ret < 0) {
796 return ret;
797 }
798 key_data = key_buffer;
799#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
800 break;
801 }
802#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
803
804#if defined(MBEDTLS_USE_PSA_CRYPTO)
805 case MBEDTLS_PK_OPAQUE:
806 {
807 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
808 psa_status_t status =
809 psa_get_key_attributes(pk->priv_id, &old_attributes);
810 if (status != PSA_SUCCESS) {
811 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
812 }
813 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
814 psa_reset_key_attributes(&old_attributes);
815 if (psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(old_type)) {
816 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
817 }
818 status = psa_export_public_key(pk->priv_id,
819 key_buffer, sizeof(key_buffer),
820 &key_length);
821 if (status != PSA_SUCCESS) {
822 return PSA_PK_TO_MBEDTLS_ERR(status);
823 }
824 key_data = key_buffer;
825 break;
826 }
827#endif /* MBEDTLS_USE_PSA_CRYPTO */
828
829 default:
830 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
831 }
832
833 return PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
834 key_data, key_length,
835 key_id));
836}
837
838int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
839 const psa_key_attributes_t *attributes,
840 mbedtls_svc_key_id_t *key_id)
841{
842 /* Set the output immediately so that it won't contain garbage even
843 * if we error out before calling psa_import_key(). */
844 *key_id = MBEDTLS_SVC_KEY_ID_INIT;
845
846#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
847 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA_ALT) {
848 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
849 }
850#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
851
852 int want_public = PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(attributes));
853 if (want_public) {
854 return import_public_into_psa(pk, attributes, key_id);
855 } else {
856 return import_pair_into_psa(pk, attributes, key_id);
857 }
858}
Gilles Peskine9cd2e9a2024-01-24 13:40:09 +0100859#endif /* MBEDTLS_PSA_CRYPTO_C */
Gilles Peskine0b172552024-01-18 14:11:26 +0100860
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200861/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200862 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200863 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200865{
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (*hash_len != 0) {
867 return 0;
868 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200869
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200870 *hash_len = mbedtls_md_get_size_from_type(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200871
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (*hash_len == 0) {
873 return -1;
874 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200875
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200877}
878
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200879#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200880/*
881 * Helper to set up a restart context if needed
882 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
884 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200885{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200886 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (ctx == NULL || ctx->pk_info != NULL) {
888 return 0;
889 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200890
891 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
893 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
894 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200895
Gilles Peskine449bd832023-01-11 14:50:10 +0100896 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
897 return MBEDTLS_ERR_PK_ALLOC_FAILED;
898 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200899
900 ctx->pk_info = info;
901
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200903}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200904#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200905
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200906/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200907 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200908 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100909int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
910 mbedtls_md_type_t md_alg,
911 const unsigned char *hash, size_t hash_len,
912 const unsigned char *sig, size_t sig_len,
913 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200914{
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100916 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 if (ctx->pk_info == NULL ||
920 pk_hashlen_helper(md_alg, &hash_len) != 0) {
921 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
922 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200923
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200924#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200925 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200927 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000929 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
932 return ret;
933 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200934
valerio38992cb2023-04-20 09:56:30 +0200935 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
939 mbedtls_pk_restart_free(rs_ctx);
940 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200943 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200944#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200945 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200946#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 if (ctx->pk_info->verify_func == NULL) {
949 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
950 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200951
valerio38992cb2023-04-20 09:56:30 +0200952 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200954}
955
956/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200957 * Verify a signature
958 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
960 const unsigned char *hash, size_t hash_len,
961 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200962{
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
964 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200965}
966
967/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200968 * Verify a signature with options
969 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
971 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
972 const unsigned char *hash, size_t hash_len,
973 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200974{
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100976 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (ctx->pk_info == NULL) {
980 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
981 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200982
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 if (!mbedtls_pk_can_do(ctx, type)) {
984 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
985 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500988 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 if (options != NULL) {
990 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
991 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500992
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500994 }
995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500997 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
998 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200999
Dave Rodgman02a53d72023-09-28 17:17:07 +01001000#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
1002 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1003 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001004#endif
Andres AG72849872017-01-19 11:24:33 +00001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (options == NULL) {
1007 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1008 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001009
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001010 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001011
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001012#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001013 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +02001014 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001015 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -05001016 int key_len;
1017 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001018 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
1019 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -05001020
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001021 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001022 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
1025 p = buf + sizeof(buf);
1026 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 if (key_len < 0) {
1029 return key_len;
1030 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
1033 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1034 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 status = psa_import_key(&attributes,
1037 buf + sizeof(buf) - key_len, key_len,
1038 &key_id);
1039 if (status != PSA_SUCCESS) {
1040 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001041 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001042 }
1043
Andrzej Kurek4a953cd2022-02-16 06:13:35 -05001044 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
1045 * on a valid signature with trailing data in a buffer, but
1046 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
1047 * so for this reason the passed sig_len is overwritten. Smaller
1048 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
1050 mbedtls_pk_get_len(ctx) : sig_len;
1051 status = psa_verify_hash(key_id, psa_sig_alg, hash,
1052 hash_len, sig, signature_length);
1053 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
1056 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1057 }
Andrzej Kurek8666df62022-02-15 08:23:02 -05001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001060 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001062
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001063 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 } else
Tomi Fontanilles81746622023-07-16 13:06:06 +03001065#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001066 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (sig_len < mbedtls_pk_get_len(ctx)) {
1068 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
1069 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
1072 md_alg, (unsigned int) hash_len, hash,
1073 pss_opts->mgf1_hash_id,
1074 pss_opts->expected_salt_len,
1075 sig);
1076 if (ret != 0) {
1077 return ret;
1078 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (sig_len > mbedtls_pk_get_len(ctx)) {
1081 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1082 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -05001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001085 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001086#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001088#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001089}
1090
1091/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001092 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001093 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
1095 mbedtls_md_type_t md_alg,
1096 const unsigned char *hash, size_t hash_len,
1097 unsigned char *sig, size_t sig_size, size_t *sig_len,
1098 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1099 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001100{
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +01001102 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
1106 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1107 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001108
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001109#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001110 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001112 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001114 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
1117 return ret;
1118 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001119
valerio38992cb2023-04-20 09:56:30 +02001120 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 hash, hash_len,
1122 sig, sig_size, sig_len,
1123 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1126 mbedtls_pk_restart_free(rs_ctx);
1127 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001128
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001130 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001131#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001132 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001133#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (ctx->pk_info->sign_func == NULL) {
1136 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1137 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001138
valerio38992cb2023-04-20 09:56:30 +02001139 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 hash, hash_len,
1141 sig, sig_size, sig_len,
1142 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001143}
1144
1145/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001146 * Make a signature
1147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001148int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1149 const unsigned char *hash, size_t hash_len,
1150 unsigned char *sig, size_t sig_size, size_t *sig_len,
1151 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001152{
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
1154 sig, sig_size, sig_len,
1155 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001156}
1157
1158/*
Jerry Yufb0621d2022-03-23 11:42:06 +08001159 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +08001160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
1162 mbedtls_pk_context *ctx,
1163 mbedtls_md_type_t md_alg,
1164 const unsigned char *hash, size_t hash_len,
1165 unsigned char *sig, size_t sig_size, size_t *sig_len,
1166 int (*f_rng)(void *, unsigned char *, size_t),
1167 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +08001168{
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 if (ctx->pk_info == NULL) {
1170 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1171 }
Jerry Yud69439a2022-02-24 15:52:15 +08001172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 if (!mbedtls_pk_can_do(ctx, pk_type)) {
1174 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1175 }
Jerry Yud69439a2022-02-24 15:52:15 +08001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
1178 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
1179 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +08001180 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001181
Tomi Fontanilles81746622023-07-16 13:06:06 +03001182#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
1183
1184#if defined(MBEDTLS_USE_PSA_CRYPTO)
1185 const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001186 if (psa_md_alg == 0) {
1187 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1188 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +02001191 psa_status_t status;
1192
Valerio Setti4f387ef2023-05-02 14:15:59 +02001193 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 hash, hash_len,
1195 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001196 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +02001197 }
1198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
1200 ctx->pk_ctx, hash, hash_len,
1201 sig, sig_size, sig_len);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001202#else /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yud69439a2022-02-24 15:52:15 +08001203
Tomi Fontanilles81746622023-07-16 13:06:06 +03001204 if (sig_size < mbedtls_pk_get_len(ctx)) {
1205 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1206 }
1207
1208 if (pk_hashlen_helper(md_alg, &hash_len) != 0) {
1209 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1210 }
1211
1212 mbedtls_rsa_context *const rsa_ctx = mbedtls_pk_rsa(*ctx);
1213
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001214 const int ret = mbedtls_rsa_rsassa_pss_sign_no_mode_check(rsa_ctx, f_rng, p_rng, md_alg,
1215 (unsigned int) hash_len, hash, sig);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001216 if (ret == 0) {
1217 *sig_len = rsa_ctx->len;
1218 }
1219 return ret;
1220
1221#endif /* MBEDTLS_USE_PSA_CRYPTO */
1222
1223#else
1224 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1225#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Jerry Yud69439a2022-02-24 15:52:15 +08001226}
1227
1228/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001229 * Decrypt message
1230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001231int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
1232 const unsigned char *input, size_t ilen,
1233 unsigned char *output, size_t *olen, size_t osize,
1234 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001235{
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (ctx->pk_info == NULL) {
1237 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1238 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (ctx->pk_info->decrypt_func == NULL) {
1241 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1242 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001243
valerio38992cb2023-04-20 09:56:30 +02001244 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001246}
1247
1248/*
1249 * Encrypt message
1250 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
1252 const unsigned char *input, size_t ilen,
1253 unsigned char *output, size_t *olen, size_t osize,
1254 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001255{
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if (ctx->pk_info == NULL) {
1257 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1258 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (ctx->pk_info->encrypt_func == NULL) {
1261 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1262 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001263
valerio38992cb2023-04-20 09:56:30 +02001264 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001266}
1267
1268/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001269 * Check public-private key pair
1270 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001271int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
1272 const mbedtls_pk_context *prv,
1273 int (*f_rng)(void *, unsigned char *, size_t),
1274 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001275{
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 if (pub->pk_info == NULL ||
1277 prv->pk_info == NULL) {
1278 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001279 }
1280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (f_rng == NULL) {
1282 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001283 }
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (prv->pk_info->check_pair_func == NULL) {
1286 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1287 }
1288
1289 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
1290 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
1291 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1292 }
1293 } else {
valerio8cbef4d2023-06-01 10:59:03 +02001294 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
1295 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001296 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1297 }
1298 }
1299
valerio38992cb2023-04-20 09:56:30 +02001300 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
1301 (mbedtls_pk_context *) prv,
1302 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001303}
1304
1305/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001306 * Get key size in bits
1307 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001308size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001309{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001310 /* For backward compatibility, accept NULL or a context that
1311 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (ctx == NULL || ctx->pk_info == NULL) {
1313 return 0;
1314 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001315
valerio38992cb2023-04-20 09:56:30 +02001316 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001317}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001318
1319/*
1320 * Export debug information
1321 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001322int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001323{
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (ctx->pk_info == NULL) {
1325 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1326 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (ctx->pk_info->debug_func == NULL) {
1329 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1330 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001331
valerio38992cb2023-04-20 09:56:30 +02001332 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001334}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001335
1336/*
1337 * Access the PK type name
1338 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001339const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001340{
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 if (ctx == NULL || ctx->pk_info == NULL) {
1342 return "invalid PK";
1343 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001346}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001347
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001348/*
1349 * Access the PK type
1350 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001351mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001352{
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 if (ctx == NULL || ctx->pk_info == NULL) {
1354 return MBEDTLS_PK_NONE;
1355 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001356
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001358}
1359
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001360#if defined(MBEDTLS_USE_PSA_CRYPTO)
1361/*
1362 * Load the key to a PSA key slot,
1363 * then turn the PK context into a wrapper for that key slot.
1364 *
Neil Armstrong56e71d42022-04-08 15:12:42 +02001365 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001366 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001367int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
1368 mbedtls_svc_key_id_t *key,
1369 psa_algorithm_t alg,
1370 psa_key_usage_t usage,
1371 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001372{
Valerio Setti81d75122023-06-14 14:49:33 +02001373#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001374 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +02001375 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +02001376 ((void) alg);
1377 ((void) usage);
1378 ((void) alg2);
Valerio Setti81d75122023-06-14 14:49:33 +02001379#else /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
1380#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001382 size_t d_len;
1383 psa_ecc_family_t curve_id;
1384 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1385 psa_key_type_t key_type;
1386 size_t bits;
Neil Armstrongc1152e42022-03-22 10:29:06 +01001387 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001388
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001389 /* export the private key material in the format PSA wants */
Valerio Settiae8c6282023-05-18 18:57:57 +02001390#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1194ffa2023-05-24 13:15:58 +02001391 unsigned char d[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Valerio Settiae8c6282023-05-18 18:57:57 +02001392 status = psa_export_key(pk->priv_id, d, sizeof(d), &d_len);
1393 if (status != PSA_SUCCESS) {
1394 return psa_pk_status_to_mbedtls(status);
1395 }
1396
1397 curve_id = pk->ec_family;
1398 bits = pk->ec_bits;
1399#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1194ffa2023-05-24 13:15:58 +02001400 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
Valerio Settiae8c6282023-05-18 18:57:57 +02001401 mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*pk);
1402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1403
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
Jethro Beekman33a3ccd2023-05-03 18:25:27 +02001405 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 return ret;
1407 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Valerio Settiae8c6282023-05-18 18:57:57 +02001410#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001412
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001413 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 psa_set_key_type(&attributes, key_type);
1415 psa_set_key_bits(&attributes, bits);
1416 psa_set_key_usage_flags(&attributes, usage);
1417 psa_set_key_algorithm(&attributes, alg);
1418 if (alg2 != PSA_ALG_NONE) {
1419 psa_set_key_enrollment_algorithm(&attributes, alg2);
1420 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001421
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001422 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 status = psa_import_key(&attributes, d, d_len, key);
Valerio Setti2d814992023-04-27 10:05:03 +02001424 mbedtls_platform_zeroize(d, sizeof(d));
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001426 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001428
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001429 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 mbedtls_pk_free(pk);
1431 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 return mbedtls_pk_setup_opaque(pk, *key);
1434 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001435#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001436#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001438 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
1439 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1440 int key_len;
1441 psa_status_t status;
1442
1443 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
1445 if (key_len <= 0) {
1446 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1447 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001448
1449 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
1451 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
1452 psa_set_key_usage_flags(&attributes, usage);
1453 psa_set_key_algorithm(&attributes, alg);
1454 if (alg2 != PSA_ALG_NONE) {
1455 psa_set_key_enrollment_algorithm(&attributes, alg2);
1456 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001457
1458 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 status = psa_import_key(&attributes,
1460 buf + sizeof(buf) - key_len,
1461 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001466 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001468
1469 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 mbedtls_pk_free(pk);
1471 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001472
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return mbedtls_pk_setup_opaque(pk, *key);
1474 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001475#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001476#endif /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001478}
1479#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#endif /* MBEDTLS_PK_C */