blob: b25dd28c0bd5e88a8a44a392ad2db2f6644794ac [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
Valerio Settia657ae32024-02-23 17:55:28 +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
Valerio Setti070d95e2024-02-01 11:29:15 +010038#define PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE \
39 (PSA_EXPORT_KEY_PAIR_MAX_SIZE > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) ? \
40 PSA_EXPORT_KEY_PAIR_MAX_SIZE : PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
41
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020042/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020044 */
Gilles Peskine449bd832023-01-11 14:50:10 +010045void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020046{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020047 ctx->pk_info = NULL;
48 ctx->pk_ctx = NULL;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020049#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4f387ef2023-05-02 14:15:59 +020050 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020051#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti722f8f72023-05-17 15:31:21 +020052#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
53 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
54 ctx->pub_raw_len = 0;
55 ctx->ec_family = 0;
56 ctx->ec_bits = 0;
57#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058}
59
60/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020064{
Gilles Peskine449bd832023-01-11 14:50:10 +010065 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010067 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020068
Valerio Settie00954d2023-04-28 15:24:32 +020069 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010070 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
71 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020072
Valerio Settib5361262023-05-18 18:51:58 +020073#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
74 /* The ownership of the priv_id key for opaque keys is external of the PK
75 * module. It's the user responsibility to clear it after use. */
76 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
77 psa_destroy_key(ctx->priv_id);
78 }
79#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020082}
83
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020084#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020085/*
86 * Initialize a restart context
87 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020089{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020090 ctx->pk_info = NULL;
91 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020092}
93
94/*
95 * Free the components of a restart context
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (ctx == NULL || ctx->pk_info == NULL ||
100 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200101 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200102 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200105
106 ctx->pk_info = NULL;
107 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200108}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200109#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200110
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111/*
112 * Get pk_info structure from type
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200115{
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_RSA_C)
118 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200120#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200121#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return &mbedtls_eckeydh_info;
Valerio Setti81d75122023-06-14 14:49:33 +0200126#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti7ca13182023-01-27 13:22:42 +0100127#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200130#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200132 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200134 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135}
136
137/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200138 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200139 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141{
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (info == NULL || ctx->pk_info != NULL) {
143 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
144 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200145
Valerio Settib5361262023-05-18 18:51:58 +0200146 if ((info->ctx_alloc_func != NULL) &&
Valerio Settie00954d2023-04-28 15:24:32 +0200147 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return MBEDTLS_ERR_PK_ALLOC_FAILED;
149 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200151 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200154}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200155
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200156#if defined(MBEDTLS_USE_PSA_CRYPTO)
157/*
158 * Initialise a PSA-wrapping context
159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
161 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200162{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100163 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100165 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (ctx == NULL || ctx->pk_info != NULL) {
168 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
169 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
172 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
173 }
174 type = psa_get_key_type(&attributes);
175 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100176
Valerio Settif7cd4192023-06-30 18:11:29 +0200177#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200179 info = &mbedtls_ecdsa_opaque_info;
Valerio Settif7cd4192023-06-30 18:11:29 +0200180 } else
181#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
182 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200183 info = &mbedtls_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 } else {
185 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
186 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100187
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200188 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200189 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200192}
193#endif /* MBEDTLS_USE_PSA_CRYPTO */
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100196/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200197 * Initialize an RSA-alt context
198 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
200 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
201 mbedtls_pk_rsa_alt_sign_func sign_func,
202 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_rsa_alt_context *rsa_alt;
205 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (ctx->pk_info != NULL) {
208 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
209 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
212 return MBEDTLS_ERR_PK_ALLOC_FAILED;
213 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200214
215 ctx->pk_info = info;
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200218
219 rsa_alt->key = key;
220 rsa_alt->decrypt_func = decrypt_func;
221 rsa_alt->sign_func = sign_func;
222 rsa_alt->key_len_func = key_len_func;
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200227
228/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200229 * Tell if a PK can do the operations of the given type
230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200232{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500233 /* A context with null pk_info is not set up yet and can't do anything.
234 * For backward compatibility, also accept NULL instead of a context
235 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (ctx == NULL || ctx->pk_info == NULL) {
237 return 0;
238 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200241}
242
Neil Armstronga88b1582022-05-11 14:11:25 +0200243#if defined(MBEDTLS_USE_PSA_CRYPTO)
244/*
245 * Tell if a PK can do the operations of the given PSA algorithm
246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
248 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200249{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200250 psa_key_usage_t key_usage;
251
Neil Armstronga88b1582022-05-11 14:11:25 +0200252 /* A context with null pk_info is not set up yet and can't do anything.
253 * For backward compatibility, also accept NULL instead of a context
254 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (ctx == NULL || ctx->pk_info == NULL) {
256 return 0;
257 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200258
259 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
261 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
262 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200263 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 PSA_ALG_IS_ECDH(alg) == 0) {
265 return 0;
266 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200267
Neil Armstrong408f6a62022-05-17 14:23:20 +0200268 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (usage == 0 ||
270 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
271 PSA_KEY_USAGE_DECRYPT |
272 PSA_KEY_USAGE_DERIVE)) != 0) {
273 return 0;
274 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200275
Neil Armstronga88b1582022-05-11 14:11:25 +0200276 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (PSA_ALG_IS_SIGN_HASH(alg) &&
278 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
279 return 0;
280 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200283 mbedtls_pk_type_t type;
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200286 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
288 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200289 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200291 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 } else {
293 return 0;
294 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (ctx->pk_info->can_do(type) == 0) {
297 return 0;
298 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200301 case MBEDTLS_PK_ECKEY:
302 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
303 break;
304 case MBEDTLS_PK_RSA:
305 case MBEDTLS_PK_RSASSA_PSS:
306 key_usage = PSA_KEY_USAGE_SIGN_HASH |
307 PSA_KEY_USAGE_SIGN_MESSAGE |
308 PSA_KEY_USAGE_DECRYPT;
309 break;
310 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200311 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200316 }
317
Neil Armstronga88b1582022-05-11 14:11:25 +0200318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstronga88b1582022-05-11 14:11:25 +0200319 psa_status_t status;
320
Valerio Setti4f387ef2023-05-02 14:15:59 +0200321 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (status != PSA_SUCCESS) {
323 return 0;
324 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200325
Valerio Setti2bd53662023-12-05 10:14:06 +0100326 psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
327 /* Key's enrollment is available only when MBEDTLS_PSA_CRYPTO_CLIENT is
328 * defined, i.e. when the Mbed TLS implementation of PSA Crypto is being used.
329 * Even though we don't officially support using other implementations of PSA
330 * Crypto with TLS and X.509 (yet), we're still trying to simplify the life of
331 * people who would like to try it before it's officially supported. */
332#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
333 psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
334#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 key_usage = psa_get_key_usage_flags(&attributes);
336 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if ((key_usage & usage) != usage) {
339 return 0;
340 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200341
Neil Armstronga88b1582022-05-11 14:11:25 +0200342 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100343 * Common case: the key alg [or alg2] only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200344 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
345 * directly.
346 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
Valerio Setti2bd53662023-12-05 10:14:06 +0100347 * a fixed hash on key_alg [or key_alg2].
Neil Armstronga88b1582022-05-11 14:11:25 +0200348 */
Valerio Setti2bd53662023-12-05 10:14:06 +0100349 if (alg == key_alg) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return 1;
351 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100352#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
353 if (alg == key_alg2) {
354 return 1;
355 }
356#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200357
358 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100359 * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200360 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200361 * then alg is compliant with this key alg
362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
366 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
367 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
368 return 1;
369 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100370#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
372 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
373 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
374 return 1;
375 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100376#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200377 }
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200380}
381#endif /* MBEDTLS_USE_PSA_CRYPTO */
382
Gilles Peskine9cd2e9a2024-01-24 13:40:09 +0100383#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine6ea18362024-01-18 14:16:27 +0100384#if defined(MBEDTLS_RSA_C)
385static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa,
386 int want_crypt)
387{
388 if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) {
389 if (want_crypt) {
Dave Rodgmanaa741652024-02-13 13:40:26 +0000390 mbedtls_md_type_t md_type = (mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa);
Gilles Peskine6ea18362024-01-18 14:16:27 +0100391 return PSA_ALG_RSA_OAEP(mbedtls_md_psa_alg_from_type(md_type));
392 } else {
393 return PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH);
394 }
395 } else {
396 if (want_crypt) {
397 return PSA_ALG_RSA_PKCS1V15_CRYPT;
398 } else {
399 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
400 }
401 }
402}
403#endif /* MBEDTLS_RSA_C */
404
Gilles Peskine0b172552024-01-18 14:11:26 +0100405int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
406 psa_key_usage_t usage,
407 psa_key_attributes_t *attributes)
408{
409 mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
410
Gilles Peskineace7c772024-01-18 17:47:54 +0100411 psa_key_usage_t more_usage = usage;
412 if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) {
413 more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
414 } else if (usage == PSA_KEY_USAGE_SIGN_HASH) {
415 more_usage |= PSA_KEY_USAGE_VERIFY_HASH;
416 } else if (usage == PSA_KEY_USAGE_DECRYPT) {
417 more_usage |= PSA_KEY_USAGE_ENCRYPT;
418 }
419 more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
420
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100421 int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE ||
422 usage == PSA_KEY_USAGE_VERIFY_HASH ||
423 usage == PSA_KEY_USAGE_ENCRYPT);
424
Gilles Peskine0b172552024-01-18 14:11:26 +0100425 switch (pk_type) {
Gilles Peskine6ea18362024-01-18 14:16:27 +0100426#if defined(MBEDTLS_RSA_C)
427 case MBEDTLS_PK_RSA:
Gilles Peskineace7c772024-01-18 17:47:54 +0100428 {
Gilles Peskinee8209752024-02-01 21:00:33 +0100429 int want_crypt = 0; /* 0: sign/verify; 1: encrypt/decrypt */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100430 switch (usage) {
431 case PSA_KEY_USAGE_SIGN_MESSAGE:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100432 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100433 case PSA_KEY_USAGE_VERIFY_MESSAGE:
434 case PSA_KEY_USAGE_VERIFY_HASH:
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100435 /* Nothing to do. */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100436 break;
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100437 case PSA_KEY_USAGE_DECRYPT:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100438 case PSA_KEY_USAGE_ENCRYPT:
439 want_crypt = 1;
440 break;
441 default:
442 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
443 }
444 /* Detect the presence of a private key in a way that works both
445 * in CRT and non-CRT configurations. */
446 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk);
447 int has_private = (mbedtls_rsa_check_privkey(rsa) == 0);
448 if (want_private && !has_private) {
449 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
450 }
451 psa_set_key_type(attributes, (want_private ?
452 PSA_KEY_TYPE_RSA_KEY_PAIR :
453 PSA_KEY_TYPE_RSA_PUBLIC_KEY));
Gilles Peskine55effd92024-01-23 18:07:36 +0100454 psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk));
Gilles Peskine6ea18362024-01-18 14:16:27 +0100455 psa_set_key_algorithm(attributes,
456 psa_algorithm_for_rsa(rsa, want_crypt));
457 break;
Gilles Peskineace7c772024-01-18 17:47:54 +0100458 }
Gilles Peskine6ea18362024-01-18 14:16:27 +0100459#endif /* MBEDTLS_RSA_C */
460
Gilles Peskineace7c772024-01-18 17:47:54 +0100461#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
462 case MBEDTLS_PK_ECKEY:
463 case MBEDTLS_PK_ECKEY_DH:
464 case MBEDTLS_PK_ECDSA:
465 {
466 int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH);
467 int derive_ok = (pk_type != MBEDTLS_PK_ECDSA);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100468#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskinecb3b4ca2024-02-02 13:12:39 +0100469 psa_ecc_family_t family = pk->ec_family;
470 size_t bits = pk->ec_bits;
471 int has_private = 0;
472 if (pk->priv_id != MBEDTLS_SVC_KEY_ID_INIT) {
473 has_private = 1;
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100474 }
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100475#else
Gilles Peskineae2668b2024-02-01 20:48:04 +0100476 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskineace7c772024-01-18 17:47:54 +0100477 int has_private = (ec->d.n != 0);
478 size_t bits = 0;
479 psa_ecc_family_t family =
480 mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100481#endif
Gilles Peskineace7c772024-01-18 17:47:54 +0100482 psa_algorithm_t alg = 0;
483 switch (usage) {
484 case PSA_KEY_USAGE_SIGN_MESSAGE:
485 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskineace7c772024-01-18 17:47:54 +0100486 case PSA_KEY_USAGE_VERIFY_MESSAGE:
487 case PSA_KEY_USAGE_VERIFY_HASH:
488 if (!sign_ok) {
489 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
490 }
491#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
492 alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
493#else
494 alg = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
495#endif
496 break;
497 case PSA_KEY_USAGE_DERIVE:
Gilles Peskineace7c772024-01-18 17:47:54 +0100498 alg = PSA_ALG_ECDH;
499 if (!derive_ok) {
500 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
501 }
502 break;
503 default:
504 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
505 }
506 if (want_private && !has_private) {
507 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
508 }
509 psa_set_key_type(attributes, (want_private ?
510 PSA_KEY_TYPE_ECC_KEY_PAIR(family) :
511 PSA_KEY_TYPE_ECC_PUBLIC_KEY(family)));
512 psa_set_key_bits(attributes, bits);
513 psa_set_key_algorithm(attributes, alg);
514 break;
515 }
516#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
517
Gilles Peskine0b172552024-01-18 14:11:26 +0100518#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
519 case MBEDTLS_PK_RSA_ALT:
520 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
521#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
522
Gilles Peskine758d8c72024-01-22 20:53:21 +0100523#if defined(MBEDTLS_USE_PSA_CRYPTO)
524 case MBEDTLS_PK_OPAQUE:
525 {
526 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
528 status = psa_get_key_attributes(pk->priv_id, &old_attributes);
529 if (status != PSA_SUCCESS) {
530 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
531 }
532 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
533 switch (usage) {
534 case PSA_KEY_USAGE_SIGN_MESSAGE:
535 case PSA_KEY_USAGE_SIGN_HASH:
536 case PSA_KEY_USAGE_VERIFY_MESSAGE:
537 case PSA_KEY_USAGE_VERIFY_HASH:
538 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) ||
539 old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) {
540 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
541 }
542 break;
543 case PSA_KEY_USAGE_DECRYPT:
544 case PSA_KEY_USAGE_ENCRYPT:
545 if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) {
546 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
547 }
548 break;
549 case PSA_KEY_USAGE_DERIVE:
550 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) {
551 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
552 }
553 break;
Gilles Peskine758d8c72024-01-22 20:53:21 +0100554 default:
555 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
556 }
557 psa_key_type_t new_type = old_type;
558 /* Opaque keys are always key pairs, so we don't need a check
559 * on the input if the required usage is private. We just need
560 * to adjust the type correctly if the required usage is public. */
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100561 if (!want_private) {
Gilles Peskine758d8c72024-01-22 20:53:21 +0100562 new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type);
563 }
564 more_usage = psa_get_key_usage_flags(&old_attributes);
Gilles Peskine793920c2024-02-01 21:26:54 +0100565 if ((usage & more_usage) == 0) {
566 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
567 }
Gilles Peskine758d8c72024-01-22 20:53:21 +0100568 psa_set_key_type(attributes, new_type);
569 psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes));
570 psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes));
571 break;
572 }
573#endif /* MBEDTLS_USE_PSA_CRYPTO */
574
Gilles Peskine0b172552024-01-18 14:11:26 +0100575 default:
576 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
577 }
578
Gilles Peskineace7c772024-01-18 17:47:54 +0100579 psa_set_key_usage_flags(attributes, more_usage);
Gilles Peskine1f97e732024-01-18 14:14:24 +0100580 psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
Gilles Peskine0b172552024-01-18 14:11:26 +0100581
582 return 0;
583}
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100584
585#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_USE_PSA_CRYPTO)
586static psa_status_t export_import_into_psa(mbedtls_svc_key_id_t old_key_id,
587 const psa_key_attributes_t *attributes,
588 mbedtls_svc_key_id_t *new_key_id)
589{
590 unsigned char key_buffer[PSA_EXPORT_KEY_PAIR_MAX_SIZE];
591 size_t key_length = 0;
592 psa_status_t status = psa_export_key(old_key_id,
593 key_buffer, sizeof(key_buffer),
594 &key_length);
595 if (status != PSA_SUCCESS) {
596 return status;
597 }
598 status = psa_import_key(attributes, key_buffer, key_length, new_key_id);
599 mbedtls_platform_zeroize(key_buffer, key_length);
600 return status;
601}
602
603static int copy_into_psa(mbedtls_svc_key_id_t old_key_id,
604 const psa_key_attributes_t *attributes,
605 mbedtls_svc_key_id_t *new_key_id)
606{
607 /* Normally, we prefer copying: it's more efficient and works even
608 * for non-exportable keys. */
609 psa_status_t status = psa_copy_key(old_key_id, attributes, new_key_id);
610 if (status == PSA_ERROR_NOT_PERMITTED /*missing COPY usage*/ ||
611 status == PSA_ERROR_INVALID_ARGUMENT /*incompatible policy*/) {
612 /* There are edge cases where copying won't work, but export+import
613 * might:
614 * - If the old key does not allow PSA_KEY_USAGE_COPY.
615 * - If the old key's usage does not allow what attributes wants.
616 * Because the key was intended for use in the pk module, and may
617 * have had a policy chosen solely for what pk needs rather than
618 * based on a detailed understanding of PSA policies, we are a bit
619 * more liberal than psa_copy_key() here.
620 */
621 /* Here we need to check that the types match, otherwise we risk
622 * importing nonsensical data. */
623 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
624 status = psa_get_key_attributes(old_key_id, &old_attributes);
625 if (status != PSA_SUCCESS) {
626 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
627 }
628 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
629 psa_reset_key_attributes(&old_attributes);
630 if (old_type != psa_get_key_type(attributes)) {
631 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
632 }
633 status = export_import_into_psa(old_key_id, attributes, new_key_id);
634 }
635 return PSA_PK_TO_MBEDTLS_ERR(status);
636}
637#endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_USE_PSA_CRYPTO */
638
639static int import_pair_into_psa(const mbedtls_pk_context *pk,
640 const psa_key_attributes_t *attributes,
641 mbedtls_svc_key_id_t *key_id)
642{
643 switch (mbedtls_pk_get_type(pk)) {
644#if defined(MBEDTLS_RSA_C)
645 case MBEDTLS_PK_RSA:
646 {
647 if (psa_get_key_type(attributes) != PSA_KEY_TYPE_RSA_KEY_PAIR) {
648 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
649 }
650 unsigned char key_buffer[
651 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)];
652 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
653 unsigned char *key_data = key_end;
654 int ret = mbedtls_rsa_write_key(mbedtls_pk_rsa(*pk),
655 key_buffer, &key_data);
656 if (ret < 0) {
657 return ret;
658 }
659 size_t key_length = key_end - key_data;
660 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
661 key_data, key_length,
662 key_id));
663 mbedtls_platform_zeroize(key_data, key_length);
664 return ret;
665 }
666#endif /* MBEDTLS_RSA_C */
667
668#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
669 case MBEDTLS_PK_ECKEY:
670 case MBEDTLS_PK_ECKEY_DH:
671 case MBEDTLS_PK_ECDSA:
672 {
673 /* We need to check the curve family, otherwise the import could
674 * succeed with nonsensical data.
675 * We don't check the bit-size: it's optional in attributes,
676 * and if it's specified, psa_import_key() will know from the key
677 * data length and will check that the bit-size matches. */
678 psa_key_type_t to_type = psa_get_key_type(attributes);
679#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
680 psa_ecc_family_t from_family = pk->ec_family;
681#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine84a7bfb2024-02-28 14:21:32 +0100682 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100683 size_t from_bits = 0;
684 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
685 &from_bits);
686#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
687 if (to_type != PSA_KEY_TYPE_ECC_KEY_PAIR(from_family)) {
688 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
689 }
690
691#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
692 if (mbedtls_svc_key_id_is_null(pk->priv_id)) {
693 /* We have a public key and want a key pair. */
694 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
695 }
696 return copy_into_psa(pk->priv_id, attributes, key_id);
697#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
698 if (ec->d.n == 0) {
699 /* Private key not set. Assume the input is a public key only.
700 * (The other possibility is that it's an incomplete object
701 * where the group is set but neither the public key nor
702 * the private key. This is not possible through ecp.h
703 * functions, so we don't bother reporting a more suitable
704 * error in that case.) */
705 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
706 }
707 unsigned char key_buffer[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
Gilles Peskine84a7bfb2024-02-28 14:21:32 +0100708 size_t key_length = 0;
709 int ret = mbedtls_ecp_write_key_ext(ec, &key_length,
710 key_buffer, sizeof(key_buffer));
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100711 if (ret < 0) {
712 return ret;
713 }
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100714 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100715 key_buffer, key_length,
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100716 key_id));
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100717 mbedtls_platform_zeroize(key_buffer, key_length);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100718 return ret;
719#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
720 }
721#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
722
723#if defined(MBEDTLS_USE_PSA_CRYPTO)
724 case MBEDTLS_PK_OPAQUE:
725 return copy_into_psa(pk->priv_id, attributes, key_id);
726#endif /* MBEDTLS_USE_PSA_CRYPTO */
727
728 default:
729 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
730 }
731}
732
733static int import_public_into_psa(const mbedtls_pk_context *pk,
734 const psa_key_attributes_t *attributes,
735 mbedtls_svc_key_id_t *key_id)
736{
737 psa_key_type_t psa_type = psa_get_key_type(attributes);
738
739#if defined(MBEDTLS_RSA_C) || \
740 (defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)) || \
741 defined(MBEDTLS_USE_PSA_CRYPTO)
742 unsigned char key_buffer[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
743#endif
744 unsigned char *key_data = NULL;
745 size_t key_length = 0;
746
747 switch (mbedtls_pk_get_type(pk)) {
748#if defined(MBEDTLS_RSA_C)
749 case MBEDTLS_PK_RSA:
750 {
751 if (psa_type != PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
752 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
753 }
754 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
755 key_data = key_end;
756 int ret = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*pk),
757 key_buffer, &key_data);
758 if (ret < 0) {
759 return ret;
760 }
761 key_length = (size_t) ret;
762 break;
763 }
764#endif /*MBEDTLS_RSA_C */
765
766#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
767 case MBEDTLS_PK_ECKEY:
768 case MBEDTLS_PK_ECKEY_DH:
769 case MBEDTLS_PK_ECDSA:
770 {
771 /* We need to check the curve family, otherwise the import could
772 * succeed with nonsensical data.
773 * We don't check the bit-size: it's optional in attributes,
774 * and if it's specified, psa_import_key() will know from the key
775 * data length and will check that the bit-size matches. */
776#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
777 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)) {
778 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
779 }
780 key_data = (unsigned char *) pk->pub_raw;
781 key_length = pk->pub_raw_len;
782#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
783 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
784 size_t from_bits = 0;
785 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
786 &from_bits);
Gilles Peskine1d338762024-02-12 14:18:26 +0100787 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(from_family)) {
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100788 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
789 }
790 int ret = mbedtls_ecp_write_public_key(
791 ec, MBEDTLS_ECP_PF_UNCOMPRESSED,
792 &key_length, key_buffer, sizeof(key_buffer));
793 if (ret < 0) {
794 return ret;
795 }
796 key_data = key_buffer;
797#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
798 break;
799 }
800#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
801
802#if defined(MBEDTLS_USE_PSA_CRYPTO)
803 case MBEDTLS_PK_OPAQUE:
804 {
805 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
806 psa_status_t status =
807 psa_get_key_attributes(pk->priv_id, &old_attributes);
808 if (status != PSA_SUCCESS) {
809 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
810 }
811 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
812 psa_reset_key_attributes(&old_attributes);
813 if (psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(old_type)) {
814 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
815 }
816 status = psa_export_public_key(pk->priv_id,
817 key_buffer, sizeof(key_buffer),
818 &key_length);
819 if (status != PSA_SUCCESS) {
820 return PSA_PK_TO_MBEDTLS_ERR(status);
821 }
822 key_data = key_buffer;
823 break;
824 }
825#endif /* MBEDTLS_USE_PSA_CRYPTO */
826
827 default:
828 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
829 }
830
831 return PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
832 key_data, key_length,
833 key_id));
834}
835
836int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
837 const psa_key_attributes_t *attributes,
838 mbedtls_svc_key_id_t *key_id)
839{
840 /* Set the output immediately so that it won't contain garbage even
841 * if we error out before calling psa_import_key(). */
842 *key_id = MBEDTLS_SVC_KEY_ID_INIT;
843
844#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
845 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA_ALT) {
846 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
847 }
848#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
849
850 int want_public = PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(attributes));
851 if (want_public) {
852 return import_public_into_psa(pk, attributes, key_id);
853 } else {
854 return import_pair_into_psa(pk, attributes, key_id);
855 }
856}
Gilles Peskine9cd2e9a2024-01-24 13:40:09 +0100857#endif /* MBEDTLS_PSA_CRYPTO_C */
Gilles Peskine0b172552024-01-18 14:11:26 +0100858
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200859/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200860 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200863{
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 if (*hash_len != 0) {
865 return 0;
866 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200867
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200868 *hash_len = mbedtls_md_get_size_from_type(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 if (*hash_len == 0) {
871 return -1;
872 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200875}
876
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200877#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200878/*
879 * Helper to set up a restart context if needed
880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
882 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200883{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200884 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (ctx == NULL || ctx->pk_info != NULL) {
886 return 0;
887 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200888
889 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
891 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
892 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
895 return MBEDTLS_ERR_PK_ALLOC_FAILED;
896 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200897
898 ctx->pk_info = info;
899
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200901}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200902#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200903
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200904/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200905 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200906 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100907int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
908 mbedtls_md_type_t md_alg,
909 const unsigned char *hash, size_t hash_len,
910 const unsigned char *sig, size_t sig_len,
911 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200912{
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100914 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500916
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (ctx->pk_info == NULL ||
918 pk_hashlen_helper(md_alg, &hash_len) != 0) {
919 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
920 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200921
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200922#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200923 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200925 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000927 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200928
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
930 return ret;
931 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200932
valerio38992cb2023-04-20 09:56:30 +0200933 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
937 mbedtls_pk_restart_free(rs_ctx);
938 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200941 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200942#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200943 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200944#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (ctx->pk_info->verify_func == NULL) {
947 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
948 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200949
valerio38992cb2023-04-20 09:56:30 +0200950 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200952}
953
954/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200955 * Verify a signature
956 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
958 const unsigned char *hash, size_t hash_len,
959 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200960{
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
962 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200963}
964
965/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200966 * Verify a signature with options
967 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100968int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
969 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
970 const unsigned char *hash, size_t hash_len,
971 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200972{
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100974 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 if (ctx->pk_info == NULL) {
978 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
979 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (!mbedtls_pk_can_do(ctx, type)) {
982 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
983 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200984
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500986 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 if (options != NULL) {
988 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
989 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500990
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500992 }
993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
996 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200997
Dave Rodgman02a53d72023-09-28 17:17:07 +0100998#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
1000 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1001 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001002#endif
Andres AG72849872017-01-19 11:24:33 +00001003
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 if (options == NULL) {
1005 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1006 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001007
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001008 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001009
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001010#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +02001012 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001013 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -05001014 int key_len;
1015 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001016 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
1017 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -05001018
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001019 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001020 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1021 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
1023 p = buf + sizeof(buf);
Valerio Setti3a815cb2024-02-14 09:54:18 +01001024 key_len = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*ctx), buf, &p);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (key_len < 0) {
1027 return key_len;
1028 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
1031 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1032 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 status = psa_import_key(&attributes,
1035 buf + sizeof(buf) - key_len, key_len,
1036 &key_id);
1037 if (status != PSA_SUCCESS) {
1038 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001039 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001040 }
1041
Andrzej Kurek4a953cd2022-02-16 06:13:35 -05001042 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
1043 * on a valid signature with trailing data in a buffer, but
1044 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
1045 * so for this reason the passed sig_len is overwritten. Smaller
1046 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
1048 mbedtls_pk_get_len(ctx) : sig_len;
1049 status = psa_verify_hash(key_id, psa_sig_alg, hash,
1050 hash_len, sig, signature_length);
1051 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
1054 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1055 }
Andrzej Kurek8666df62022-02-15 08:23:02 -05001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001058 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001060
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001061 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 } else
Tomi Fontanilles81746622023-07-16 13:06:06 +03001063#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001064 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if (sig_len < mbedtls_pk_get_len(ctx)) {
1066 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
1067 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001068
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
1070 md_alg, (unsigned int) hash_len, hash,
1071 pss_opts->mgf1_hash_id,
1072 pss_opts->expected_salt_len,
1073 sig);
1074 if (ret != 0) {
1075 return ret;
1076 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (sig_len > mbedtls_pk_get_len(ctx)) {
1079 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1080 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -05001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001083 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001084#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001086#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001087}
1088
1089/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001090 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001091 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001092int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
1093 mbedtls_md_type_t md_alg,
1094 const unsigned char *hash, size_t hash_len,
1095 unsigned char *sig, size_t sig_size, size_t *sig_len,
1096 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1097 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001098{
Gilles Peskine449bd832023-01-11 14:50:10 +01001099 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +01001100 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001102
Gilles Peskine449bd832023-01-11 14:50:10 +01001103 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
1104 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1105 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001106
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001107#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001108 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001110 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001112 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001113
Gilles Peskine449bd832023-01-11 14:50:10 +01001114 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
1115 return ret;
1116 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001117
valerio38992cb2023-04-20 09:56:30 +02001118 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 hash, hash_len,
1120 sig, sig_size, sig_len,
1121 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1124 mbedtls_pk_restart_free(rs_ctx);
1125 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001128 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001129#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001130 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001131#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001132
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (ctx->pk_info->sign_func == NULL) {
1134 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1135 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001136
valerio38992cb2023-04-20 09:56:30 +02001137 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 hash, hash_len,
1139 sig, sig_size, sig_len,
1140 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001141}
1142
1143/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001144 * Make a signature
1145 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001146int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1147 const unsigned char *hash, size_t hash_len,
1148 unsigned char *sig, size_t sig_size, size_t *sig_len,
1149 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001150{
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
1152 sig, sig_size, sig_len,
1153 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001154}
1155
1156/*
Jerry Yufb0621d2022-03-23 11:42:06 +08001157 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +08001158 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001159int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
1160 mbedtls_pk_context *ctx,
1161 mbedtls_md_type_t md_alg,
1162 const unsigned char *hash, size_t hash_len,
1163 unsigned char *sig, size_t sig_size, size_t *sig_len,
1164 int (*f_rng)(void *, unsigned char *, size_t),
1165 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +08001166{
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 if (ctx->pk_info == NULL) {
1168 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1169 }
Jerry Yud69439a2022-02-24 15:52:15 +08001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 if (!mbedtls_pk_can_do(ctx, pk_type)) {
1172 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1173 }
Jerry Yud69439a2022-02-24 15:52:15 +08001174
Gilles Peskine449bd832023-01-11 14:50:10 +01001175 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
1176 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
1177 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +08001178 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001179
Tomi Fontanilles81746622023-07-16 13:06:06 +03001180#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
1181
1182#if defined(MBEDTLS_USE_PSA_CRYPTO)
1183 const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 if (psa_md_alg == 0) {
1185 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1186 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Valerio Settib484e372024-02-27 16:21:12 +01001189 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
1190 psa_algorithm_t psa_alg, psa_enrollment_alg, sign_alg;
Neil Armstrong13e76be2022-04-21 12:08:52 +02001191 psa_status_t status;
1192
Valerio Settib484e372024-02-27 16:21:12 +01001193 status = psa_get_key_attributes(ctx->priv_id, &key_attr);
1194 if (status != PSA_SUCCESS) {
1195 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1196 }
1197 psa_alg = psa_get_key_algorithm(&key_attr);
1198 psa_enrollment_alg = psa_get_key_enrollment_algorithm(&key_attr);
1199 psa_reset_key_attributes(&key_attr);
1200
1201 /* Since we're PK type is MBEDTLS_PK_RSASSA_PSS at least one between
1202 * alg and enrollment alg should be of type RSA_PSS. */
1203 if (PSA_ALG_IS_RSA_PSS(psa_alg)) {
1204 sign_alg = psa_alg;
1205 } else if (PSA_ALG_IS_RSA_PSS(psa_enrollment_alg)) {
1206 sign_alg = psa_enrollment_alg;
1207 } else {
1208 /* The opaque key has no RSA PSS algorithm associated. */
1209 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1210 }
1211 /* Adjust the hashing algorithm. */
1212 sign_alg = (sign_alg & ~PSA_ALG_HASH_MASK) | PSA_ALG_GET_HASH(psa_md_alg);
1213
1214 status = psa_sign_hash(ctx->priv_id, sign_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 hash, hash_len,
1216 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001217 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +02001218 }
1219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
1221 ctx->pk_ctx, hash, hash_len,
1222 sig, sig_size, sig_len);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001223#else /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yud69439a2022-02-24 15:52:15 +08001224
Tomi Fontanilles81746622023-07-16 13:06:06 +03001225 if (sig_size < mbedtls_pk_get_len(ctx)) {
1226 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1227 }
1228
1229 if (pk_hashlen_helper(md_alg, &hash_len) != 0) {
1230 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1231 }
1232
1233 mbedtls_rsa_context *const rsa_ctx = mbedtls_pk_rsa(*ctx);
1234
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001235 const int ret = mbedtls_rsa_rsassa_pss_sign_no_mode_check(rsa_ctx, f_rng, p_rng, md_alg,
1236 (unsigned int) hash_len, hash, sig);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001237 if (ret == 0) {
1238 *sig_len = rsa_ctx->len;
1239 }
1240 return ret;
1241
1242#endif /* MBEDTLS_USE_PSA_CRYPTO */
1243
1244#else
1245 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1246#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Jerry Yud69439a2022-02-24 15:52:15 +08001247}
1248
1249/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001250 * Decrypt message
1251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001252int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
1253 const unsigned char *input, size_t ilen,
1254 unsigned char *output, size_t *olen, size_t osize,
1255 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001256{
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 if (ctx->pk_info == NULL) {
1258 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1259 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001260
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 if (ctx->pk_info->decrypt_func == NULL) {
1262 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1263 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001264
valerio38992cb2023-04-20 09:56:30 +02001265 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001266 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001267}
1268
1269/*
1270 * Encrypt message
1271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001272int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
1273 const unsigned char *input, size_t ilen,
1274 unsigned char *output, size_t *olen, size_t osize,
1275 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001276{
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 if (ctx->pk_info == NULL) {
1278 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1279 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if (ctx->pk_info->encrypt_func == NULL) {
1282 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1283 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001284
valerio38992cb2023-04-20 09:56:30 +02001285 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001287}
1288
1289/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001290 * Check public-private key pair
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
1293 const mbedtls_pk_context *prv,
1294 int (*f_rng)(void *, unsigned char *, size_t),
1295 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001296{
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (pub->pk_info == NULL ||
1298 prv->pk_info == NULL) {
1299 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001300 }
1301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302 if (f_rng == NULL) {
1303 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001304 }
1305
Gilles Peskine449bd832023-01-11 14:50:10 +01001306 if (prv->pk_info->check_pair_func == NULL) {
1307 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1308 }
1309
1310 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
1311 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
1312 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1313 }
1314 } else {
valerio8cbef4d2023-06-01 10:59:03 +02001315 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
1316 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1318 }
1319 }
1320
valerio38992cb2023-04-20 09:56:30 +02001321 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
1322 (mbedtls_pk_context *) prv,
1323 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001324}
1325
1326/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001327 * Get key size in bits
1328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001329size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001330{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001331 /* For backward compatibility, accept NULL or a context that
1332 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (ctx == NULL || ctx->pk_info == NULL) {
1334 return 0;
1335 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001336
valerio38992cb2023-04-20 09:56:30 +02001337 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001338}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001339
1340/*
1341 * Export debug information
1342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001343int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001344{
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 if (ctx->pk_info == NULL) {
1346 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1347 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 if (ctx->pk_info->debug_func == NULL) {
1350 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1351 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001352
valerio38992cb2023-04-20 09:56:30 +02001353 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001355}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001356
1357/*
1358 * Access the PK type name
1359 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001360const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001361{
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (ctx == NULL || ctx->pk_info == NULL) {
1363 return "invalid PK";
1364 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001367}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001368
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001369/*
1370 * Access the PK type
1371 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001372mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001373{
Gilles Peskine449bd832023-01-11 14:50:10 +01001374 if (ctx == NULL || ctx->pk_info == NULL) {
1375 return MBEDTLS_PK_NONE;
1376 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001379}
1380
Valerio Setti01ba66d2024-02-09 11:35:42 +01001381#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti070d95e2024-02-01 11:29:15 +01001382int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id, mbedtls_pk_context *pk)
1383{
1384 psa_status_t status;
1385 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
1386 psa_key_type_t key_type;
1387 psa_algorithm_t alg_type;
1388 size_t key_bits;
1389 /* Use a buffer size large enough to contain either a key pair or public key. */
1390 unsigned char exp_key[PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE];
1391 size_t exp_key_len;
1392 int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1393
1394 if (pk == NULL) {
1395 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1396 }
1397
1398 status = psa_get_key_attributes(key_id, &key_attr);
1399 if (status != PSA_SUCCESS) {
1400 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1401 }
1402
Valerio Setti070d95e2024-02-01 11:29:15 +01001403 status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
1404 if (status != PSA_SUCCESS) {
Valerio Setti5ac511b2024-02-14 10:17:08 +01001405 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Valerio Setti070d95e2024-02-01 11:29:15 +01001406 goto exit;
1407 }
1408
1409 key_type = psa_get_key_type(&key_attr);
1410 key_bits = psa_get_key_bits(&key_attr);
1411 alg_type = psa_get_key_algorithm(&key_attr);
1412
1413#if defined(MBEDTLS_RSA_C)
1414 if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) ||
1415 (key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) {
Valerio Setti070d95e2024-02-01 11:29:15 +01001416
1417 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
1418 if (ret != 0) {
1419 goto exit;
1420 }
1421
1422 if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
1423 ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
1424 } else {
1425 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
1426 }
1427 if (ret != 0) {
1428 goto exit;
1429 }
1430
1431 mbedtls_md_type_t md_type = MBEDTLS_MD_NONE;
Valerio Settia657ae32024-02-23 17:55:28 +01001432 if ((PSA_ALG_GET_HASH(alg_type) != PSA_ALG_NONE) &&
Valerio Setti070d95e2024-02-01 11:29:15 +01001433 (PSA_ALG_GET_HASH(alg_type) != PSA_ALG_ANY_HASH)) {
1434 md_type = mbedtls_md_type_from_psa_alg(alg_type);
1435 }
1436
1437 if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) {
1438 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type);
1439 } else {
1440 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type);
1441 }
1442 if (ret != 0) {
1443 goto exit;
1444 }
1445 } else
1446#endif /* MBEDTLS_RSA_C */
1447#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
1448 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ||
1449 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
1450 mbedtls_ecp_group_id grp_id;
1451
Valerio Setti070d95e2024-02-01 11:29:15 +01001452 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
1453 if (ret != 0) {
1454 goto exit;
1455 }
1456
1457 grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits);
1458 ret = mbedtls_pk_ecc_set_group(pk, grp_id);
1459 if (ret != 0) {
1460 goto exit;
1461 }
1462
1463 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
1464 ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len);
1465 if (ret != 0) {
1466 goto exit;
1467 }
1468 ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, exp_key, exp_key_len,
1469 mbedtls_psa_get_random,
1470 MBEDTLS_PSA_RANDOM_STATE);
1471 } else {
1472 ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len);
1473 }
1474 if (ret != 0) {
1475 goto exit;
1476 }
1477 } else
1478#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
1479 {
1480 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1481 }
1482
1483exit:
1484 psa_reset_key_attributes(&key_attr);
1485 mbedtls_platform_zeroize(exp_key, sizeof(exp_key));
1486
1487 return ret;
1488}
Valerio Setti01ba66d2024-02-09 11:35:42 +01001489#endif /* MBEDTLS_PSA_CRYPTO_C */
Valerio Setti070d95e2024-02-01 11:29:15 +01001490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491#endif /* MBEDTLS_PK_C */