blob: e33a2434b92aeac91349375d74d270eea2445c63 [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);
Valerio Setti864a50b2024-03-12 13:41:19 +0100327 /* Key's enrollment is available only when an Mbed TLS implementation of PSA
328 * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
Valerio Setti2bd53662023-12-05 10:14:06 +0100329 * Even though we don't officially support using other implementations of PSA
Valerio Setti864a50b2024-03-12 13:41:19 +0100330 * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
331 * separated. */
332#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti2bd53662023-12-05 10:14:06 +0100333 psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
Valerio Setti864a50b2024-03-12 13:41:19 +0100334#endif /* MBEDTLS_PSA_CRYPTO_C */
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 Setti864a50b2024-03-12 13:41:19 +0100352#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti2bd53662023-12-05 10:14:06 +0100353 if (alg == key_alg2) {
354 return 1;
355 }
Valerio Setti864a50b2024-03-12 13:41:19 +0100356#endif /* MBEDTLS_PSA_CRYPTO_C */
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)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
365 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
366 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
367 return 1;
368 }
Valerio Setti864a50b2024-03-12 13:41:19 +0100369#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
371 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
372 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
373 return 1;
374 }
Valerio Setti864a50b2024-03-12 13:41:19 +0100375#endif /* MBEDTLS_PSA_CRYPTO_C */
Neil Armstronga88b1582022-05-11 14:11:25 +0200376 }
377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200379}
380#endif /* MBEDTLS_USE_PSA_CRYPTO */
381
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100382#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine6ea18362024-01-18 14:16:27 +0100383#if defined(MBEDTLS_RSA_C)
384static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa,
385 int want_crypt)
386{
387 if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) {
388 if (want_crypt) {
Dave Rodgmanaa741652024-02-13 13:40:26 +0000389 mbedtls_md_type_t md_type = (mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa);
Gilles Peskine6ea18362024-01-18 14:16:27 +0100390 return PSA_ALG_RSA_OAEP(mbedtls_md_psa_alg_from_type(md_type));
391 } else {
392 return PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH);
393 }
394 } else {
395 if (want_crypt) {
396 return PSA_ALG_RSA_PKCS1V15_CRYPT;
397 } else {
398 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
399 }
400 }
401}
402#endif /* MBEDTLS_RSA_C */
403
Gilles Peskine0b172552024-01-18 14:11:26 +0100404int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
405 psa_key_usage_t usage,
406 psa_key_attributes_t *attributes)
407{
408 mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
409
Gilles Peskineace7c772024-01-18 17:47:54 +0100410 psa_key_usage_t more_usage = usage;
411 if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) {
412 more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
413 } else if (usage == PSA_KEY_USAGE_SIGN_HASH) {
414 more_usage |= PSA_KEY_USAGE_VERIFY_HASH;
415 } else if (usage == PSA_KEY_USAGE_DECRYPT) {
416 more_usage |= PSA_KEY_USAGE_ENCRYPT;
417 }
418 more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
419
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100420 int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE ||
421 usage == PSA_KEY_USAGE_VERIFY_HASH ||
422 usage == PSA_KEY_USAGE_ENCRYPT);
423
Gilles Peskine0b172552024-01-18 14:11:26 +0100424 switch (pk_type) {
Gilles Peskine6ea18362024-01-18 14:16:27 +0100425#if defined(MBEDTLS_RSA_C)
426 case MBEDTLS_PK_RSA:
Gilles Peskineace7c772024-01-18 17:47:54 +0100427 {
Gilles Peskinee8209752024-02-01 21:00:33 +0100428 int want_crypt = 0; /* 0: sign/verify; 1: encrypt/decrypt */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100429 switch (usage) {
430 case PSA_KEY_USAGE_SIGN_MESSAGE:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100431 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100432 case PSA_KEY_USAGE_VERIFY_MESSAGE:
433 case PSA_KEY_USAGE_VERIFY_HASH:
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100434 /* Nothing to do. */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100435 break;
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100436 case PSA_KEY_USAGE_DECRYPT:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100437 case PSA_KEY_USAGE_ENCRYPT:
438 want_crypt = 1;
439 break;
440 default:
441 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
442 }
443 /* Detect the presence of a private key in a way that works both
444 * in CRT and non-CRT configurations. */
445 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk);
446 int has_private = (mbedtls_rsa_check_privkey(rsa) == 0);
447 if (want_private && !has_private) {
448 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
449 }
450 psa_set_key_type(attributes, (want_private ?
451 PSA_KEY_TYPE_RSA_KEY_PAIR :
452 PSA_KEY_TYPE_RSA_PUBLIC_KEY));
Gilles Peskine55effd92024-01-23 18:07:36 +0100453 psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk));
Gilles Peskine6ea18362024-01-18 14:16:27 +0100454 psa_set_key_algorithm(attributes,
455 psa_algorithm_for_rsa(rsa, want_crypt));
456 break;
Gilles Peskineace7c772024-01-18 17:47:54 +0100457 }
Gilles Peskine6ea18362024-01-18 14:16:27 +0100458#endif /* MBEDTLS_RSA_C */
459
Gilles Peskineace7c772024-01-18 17:47:54 +0100460#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
461 case MBEDTLS_PK_ECKEY:
462 case MBEDTLS_PK_ECKEY_DH:
463 case MBEDTLS_PK_ECDSA:
464 {
465 int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH);
466 int derive_ok = (pk_type != MBEDTLS_PK_ECDSA);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100467#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskinecb3b4ca2024-02-02 13:12:39 +0100468 psa_ecc_family_t family = pk->ec_family;
469 size_t bits = pk->ec_bits;
470 int has_private = 0;
471 if (pk->priv_id != MBEDTLS_SVC_KEY_ID_INIT) {
472 has_private = 1;
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100473 }
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100474#else
Gilles Peskineae2668b2024-02-01 20:48:04 +0100475 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskineace7c772024-01-18 17:47:54 +0100476 int has_private = (ec->d.n != 0);
477 size_t bits = 0;
478 psa_ecc_family_t family =
479 mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100480#endif
Gilles Peskineace7c772024-01-18 17:47:54 +0100481 psa_algorithm_t alg = 0;
482 switch (usage) {
483 case PSA_KEY_USAGE_SIGN_MESSAGE:
484 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskineace7c772024-01-18 17:47:54 +0100485 case PSA_KEY_USAGE_VERIFY_MESSAGE:
486 case PSA_KEY_USAGE_VERIFY_HASH:
487 if (!sign_ok) {
488 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
489 }
490#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
491 alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
492#else
493 alg = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
494#endif
495 break;
496 case PSA_KEY_USAGE_DERIVE:
Gilles Peskineace7c772024-01-18 17:47:54 +0100497 alg = PSA_ALG_ECDH;
498 if (!derive_ok) {
499 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
500 }
501 break;
502 default:
503 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
504 }
505 if (want_private && !has_private) {
506 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
507 }
508 psa_set_key_type(attributes, (want_private ?
509 PSA_KEY_TYPE_ECC_KEY_PAIR(family) :
510 PSA_KEY_TYPE_ECC_PUBLIC_KEY(family)));
511 psa_set_key_bits(attributes, bits);
512 psa_set_key_algorithm(attributes, alg);
513 break;
514 }
515#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
516
Gilles Peskine0b172552024-01-18 14:11:26 +0100517#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
518 case MBEDTLS_PK_RSA_ALT:
519 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
520#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
521
Gilles Peskine758d8c72024-01-22 20:53:21 +0100522#if defined(MBEDTLS_USE_PSA_CRYPTO)
523 case MBEDTLS_PK_OPAQUE:
524 {
525 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
526 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
527 status = psa_get_key_attributes(pk->priv_id, &old_attributes);
528 if (status != PSA_SUCCESS) {
529 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
530 }
531 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
532 switch (usage) {
533 case PSA_KEY_USAGE_SIGN_MESSAGE:
534 case PSA_KEY_USAGE_SIGN_HASH:
535 case PSA_KEY_USAGE_VERIFY_MESSAGE:
536 case PSA_KEY_USAGE_VERIFY_HASH:
537 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) ||
538 old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) {
539 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
540 }
541 break;
542 case PSA_KEY_USAGE_DECRYPT:
543 case PSA_KEY_USAGE_ENCRYPT:
544 if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) {
545 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
546 }
547 break;
548 case PSA_KEY_USAGE_DERIVE:
549 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) {
550 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
551 }
552 break;
Gilles Peskine758d8c72024-01-22 20:53:21 +0100553 default:
554 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
555 }
556 psa_key_type_t new_type = old_type;
557 /* Opaque keys are always key pairs, so we don't need a check
558 * on the input if the required usage is private. We just need
559 * to adjust the type correctly if the required usage is public. */
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100560 if (!want_private) {
Gilles Peskine758d8c72024-01-22 20:53:21 +0100561 new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type);
562 }
563 more_usage = psa_get_key_usage_flags(&old_attributes);
Gilles Peskine793920c2024-02-01 21:26:54 +0100564 if ((usage & more_usage) == 0) {
565 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
566 }
Gilles Peskine758d8c72024-01-22 20:53:21 +0100567 psa_set_key_type(attributes, new_type);
568 psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes));
569 psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes));
570 break;
571 }
572#endif /* MBEDTLS_USE_PSA_CRYPTO */
573
Gilles Peskine0b172552024-01-18 14:11:26 +0100574 default:
575 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
576 }
577
Gilles Peskineace7c772024-01-18 17:47:54 +0100578 psa_set_key_usage_flags(attributes, more_usage);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100579 /* Key's enrollment is available only when an Mbed TLS implementation of PSA
580 * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
581 * Even though we don't officially support using other implementations of PSA
582 * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
583 * separated. */
584#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine1f97e732024-01-18 14:14:24 +0100585 psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100586#endif
Gilles Peskine0b172552024-01-18 14:11:26 +0100587
588 return 0;
589}
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100590
591#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_USE_PSA_CRYPTO)
592static psa_status_t export_import_into_psa(mbedtls_svc_key_id_t old_key_id,
593 const psa_key_attributes_t *attributes,
594 mbedtls_svc_key_id_t *new_key_id)
595{
596 unsigned char key_buffer[PSA_EXPORT_KEY_PAIR_MAX_SIZE];
597 size_t key_length = 0;
598 psa_status_t status = psa_export_key(old_key_id,
599 key_buffer, sizeof(key_buffer),
600 &key_length);
601 if (status != PSA_SUCCESS) {
602 return status;
603 }
604 status = psa_import_key(attributes, key_buffer, key_length, new_key_id);
605 mbedtls_platform_zeroize(key_buffer, key_length);
606 return status;
607}
608
609static int copy_into_psa(mbedtls_svc_key_id_t old_key_id,
610 const psa_key_attributes_t *attributes,
611 mbedtls_svc_key_id_t *new_key_id)
612{
613 /* Normally, we prefer copying: it's more efficient and works even
614 * for non-exportable keys. */
615 psa_status_t status = psa_copy_key(old_key_id, attributes, new_key_id);
616 if (status == PSA_ERROR_NOT_PERMITTED /*missing COPY usage*/ ||
617 status == PSA_ERROR_INVALID_ARGUMENT /*incompatible policy*/) {
618 /* There are edge cases where copying won't work, but export+import
619 * might:
620 * - If the old key does not allow PSA_KEY_USAGE_COPY.
621 * - If the old key's usage does not allow what attributes wants.
622 * Because the key was intended for use in the pk module, and may
623 * have had a policy chosen solely for what pk needs rather than
624 * based on a detailed understanding of PSA policies, we are a bit
625 * more liberal than psa_copy_key() here.
626 */
627 /* Here we need to check that the types match, otherwise we risk
628 * importing nonsensical data. */
629 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
630 status = psa_get_key_attributes(old_key_id, &old_attributes);
631 if (status != PSA_SUCCESS) {
632 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
633 }
634 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
635 psa_reset_key_attributes(&old_attributes);
636 if (old_type != psa_get_key_type(attributes)) {
637 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
638 }
639 status = export_import_into_psa(old_key_id, attributes, new_key_id);
640 }
641 return PSA_PK_TO_MBEDTLS_ERR(status);
642}
643#endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_USE_PSA_CRYPTO */
644
645static int import_pair_into_psa(const mbedtls_pk_context *pk,
646 const psa_key_attributes_t *attributes,
647 mbedtls_svc_key_id_t *key_id)
648{
649 switch (mbedtls_pk_get_type(pk)) {
650#if defined(MBEDTLS_RSA_C)
651 case MBEDTLS_PK_RSA:
652 {
653 if (psa_get_key_type(attributes) != PSA_KEY_TYPE_RSA_KEY_PAIR) {
654 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
655 }
656 unsigned char key_buffer[
657 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)];
658 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
659 unsigned char *key_data = key_end;
660 int ret = mbedtls_rsa_write_key(mbedtls_pk_rsa(*pk),
661 key_buffer, &key_data);
662 if (ret < 0) {
663 return ret;
664 }
665 size_t key_length = key_end - key_data;
666 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
667 key_data, key_length,
668 key_id));
669 mbedtls_platform_zeroize(key_data, key_length);
670 return ret;
671 }
672#endif /* MBEDTLS_RSA_C */
673
674#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
675 case MBEDTLS_PK_ECKEY:
676 case MBEDTLS_PK_ECKEY_DH:
677 case MBEDTLS_PK_ECDSA:
678 {
679 /* We need to check the curve family, otherwise the import could
680 * succeed with nonsensical data.
681 * We don't check the bit-size: it's optional in attributes,
682 * and if it's specified, psa_import_key() will know from the key
683 * data length and will check that the bit-size matches. */
684 psa_key_type_t to_type = psa_get_key_type(attributes);
685#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
686 psa_ecc_family_t from_family = pk->ec_family;
687#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine84a7bfb2024-02-28 14:21:32 +0100688 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100689 size_t from_bits = 0;
690 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
691 &from_bits);
692#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
693 if (to_type != PSA_KEY_TYPE_ECC_KEY_PAIR(from_family)) {
694 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
695 }
696
697#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
698 if (mbedtls_svc_key_id_is_null(pk->priv_id)) {
699 /* We have a public key and want a key pair. */
700 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
701 }
702 return copy_into_psa(pk->priv_id, attributes, key_id);
703#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
704 if (ec->d.n == 0) {
705 /* Private key not set. Assume the input is a public key only.
706 * (The other possibility is that it's an incomplete object
707 * where the group is set but neither the public key nor
708 * the private key. This is not possible through ecp.h
709 * functions, so we don't bother reporting a more suitable
710 * error in that case.) */
711 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
712 }
713 unsigned char key_buffer[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
Gilles Peskine84a7bfb2024-02-28 14:21:32 +0100714 size_t key_length = 0;
715 int ret = mbedtls_ecp_write_key_ext(ec, &key_length,
716 key_buffer, sizeof(key_buffer));
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100717 if (ret < 0) {
718 return ret;
719 }
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100720 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100721 key_buffer, key_length,
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100722 key_id));
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100723 mbedtls_platform_zeroize(key_buffer, key_length);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100724 return ret;
725#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
726 }
727#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
728
729#if defined(MBEDTLS_USE_PSA_CRYPTO)
730 case MBEDTLS_PK_OPAQUE:
731 return copy_into_psa(pk->priv_id, attributes, key_id);
732#endif /* MBEDTLS_USE_PSA_CRYPTO */
733
734 default:
735 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
736 }
737}
738
739static int import_public_into_psa(const mbedtls_pk_context *pk,
740 const psa_key_attributes_t *attributes,
741 mbedtls_svc_key_id_t *key_id)
742{
743 psa_key_type_t psa_type = psa_get_key_type(attributes);
744
745#if defined(MBEDTLS_RSA_C) || \
746 (defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)) || \
747 defined(MBEDTLS_USE_PSA_CRYPTO)
748 unsigned char key_buffer[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
749#endif
750 unsigned char *key_data = NULL;
751 size_t key_length = 0;
752
753 switch (mbedtls_pk_get_type(pk)) {
754#if defined(MBEDTLS_RSA_C)
755 case MBEDTLS_PK_RSA:
756 {
757 if (psa_type != PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
758 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
759 }
760 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
761 key_data = key_end;
762 int ret = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*pk),
763 key_buffer, &key_data);
764 if (ret < 0) {
765 return ret;
766 }
767 key_length = (size_t) ret;
768 break;
769 }
770#endif /*MBEDTLS_RSA_C */
771
772#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
773 case MBEDTLS_PK_ECKEY:
774 case MBEDTLS_PK_ECKEY_DH:
775 case MBEDTLS_PK_ECDSA:
776 {
777 /* We need to check the curve family, otherwise the import could
778 * succeed with nonsensical data.
779 * We don't check the bit-size: it's optional in attributes,
780 * and if it's specified, psa_import_key() will know from the key
781 * data length and will check that the bit-size matches. */
782#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
783 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)) {
784 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
785 }
786 key_data = (unsigned char *) pk->pub_raw;
787 key_length = pk->pub_raw_len;
788#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
789 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
790 size_t from_bits = 0;
791 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
792 &from_bits);
Gilles Peskine1d338762024-02-12 14:18:26 +0100793 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(from_family)) {
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100794 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
795 }
796 int ret = mbedtls_ecp_write_public_key(
797 ec, MBEDTLS_ECP_PF_UNCOMPRESSED,
798 &key_length, key_buffer, sizeof(key_buffer));
799 if (ret < 0) {
800 return ret;
801 }
802 key_data = key_buffer;
803#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
804 break;
805 }
806#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
807
808#if defined(MBEDTLS_USE_PSA_CRYPTO)
809 case MBEDTLS_PK_OPAQUE:
810 {
811 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
812 psa_status_t status =
813 psa_get_key_attributes(pk->priv_id, &old_attributes);
814 if (status != PSA_SUCCESS) {
815 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
816 }
817 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
818 psa_reset_key_attributes(&old_attributes);
819 if (psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(old_type)) {
820 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
821 }
822 status = psa_export_public_key(pk->priv_id,
823 key_buffer, sizeof(key_buffer),
824 &key_length);
825 if (status != PSA_SUCCESS) {
826 return PSA_PK_TO_MBEDTLS_ERR(status);
827 }
828 key_data = key_buffer;
829 break;
830 }
831#endif /* MBEDTLS_USE_PSA_CRYPTO */
832
833 default:
834 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
835 }
836
837 return PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
838 key_data, key_length,
839 key_id));
840}
841
842int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
843 const psa_key_attributes_t *attributes,
844 mbedtls_svc_key_id_t *key_id)
845{
846 /* Set the output immediately so that it won't contain garbage even
847 * if we error out before calling psa_import_key(). */
848 *key_id = MBEDTLS_SVC_KEY_ID_INIT;
849
850#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
851 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA_ALT) {
852 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
853 }
854#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
855
856 int want_public = PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(attributes));
857 if (want_public) {
858 return import_public_into_psa(pk, attributes, key_id);
859 } else {
860 return import_pair_into_psa(pk, attributes, key_id);
861 }
862}
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100863
864static int copy_from_psa(mbedtls_svc_key_id_t key_id,
865 mbedtls_pk_context *pk,
866 int public_only)
867{
868 psa_status_t status;
869 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
870 psa_key_type_t key_type;
871 psa_algorithm_t alg_type;
872 size_t key_bits;
873 /* Use a buffer size large enough to contain either a key pair or public key. */
874 unsigned char exp_key[PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE];
875 size_t exp_key_len;
876 int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
877
878 if (pk == NULL) {
879 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
880 }
881
882 status = psa_get_key_attributes(key_id, &key_attr);
883 if (status != PSA_SUCCESS) {
884 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
885 }
886
887 if (public_only) {
888 status = psa_export_public_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
889 } else {
890 status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
891 }
892 if (status != PSA_SUCCESS) {
893 ret = PSA_PK_TO_MBEDTLS_ERR(status);
894 goto exit;
895 }
896
897 key_type = psa_get_key_type(&key_attr);
898 if (public_only) {
899 key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
900 }
901 key_bits = psa_get_key_bits(&key_attr);
902 alg_type = psa_get_key_algorithm(&key_attr);
903
904#if defined(MBEDTLS_RSA_C)
905 if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) ||
906 (key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) {
907
908 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
909 if (ret != 0) {
910 goto exit;
911 }
912
913 if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
914 ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
915 } else {
916 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
917 }
918 if (ret != 0) {
919 goto exit;
920 }
921
922 mbedtls_md_type_t md_type = MBEDTLS_MD_NONE;
923 if (PSA_ALG_GET_HASH(alg_type) != PSA_ALG_ANY_HASH) {
924 md_type = mbedtls_md_type_from_psa_alg(alg_type);
925 }
926
927 if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) {
928 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type);
929 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg_type) ||
930 alg_type == PSA_ALG_RSA_PKCS1V15_CRYPT) {
931 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type);
932 }
933 if (ret != 0) {
934 goto exit;
935 }
936 } else
937#endif /* MBEDTLS_RSA_C */
938#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
939 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ||
940 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
941 mbedtls_ecp_group_id grp_id;
942
943 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
944 if (ret != 0) {
945 goto exit;
946 }
947
948 grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits);
949 ret = mbedtls_pk_ecc_set_group(pk, grp_id);
950 if (ret != 0) {
951 goto exit;
952 }
953
954 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
955 ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len);
956 if (ret != 0) {
957 goto exit;
958 }
959 ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, exp_key, exp_key_len,
960 mbedtls_psa_get_random,
961 MBEDTLS_PSA_RANDOM_STATE);
962 } else {
963 ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len);
964 }
965 if (ret != 0) {
966 goto exit;
967 }
968 } else
969#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
970 {
Valerio Setti4a350ca2024-04-02 11:31:33 +0200971 (void) key_bits;
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100972 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
973 }
974
975exit:
976 psa_reset_key_attributes(&key_attr);
977 mbedtls_platform_zeroize(exp_key, sizeof(exp_key));
978
979 return ret;
980}
981
982int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id,
983 mbedtls_pk_context *pk)
984{
985 return copy_from_psa(key_id, pk, 0);
986}
987
988int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id,
989 mbedtls_pk_context *pk)
990{
991 return copy_from_psa(key_id, pk, 1);
992}
993#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Gilles Peskine0b172552024-01-18 14:11:26 +0100994
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200995/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200997 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200999{
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (*hash_len != 0) {
1001 return 0;
1002 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001003
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +02001004 *hash_len = mbedtls_md_get_size_from_type(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +02001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (*hash_len == 0) {
1007 return -1;
1008 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001011}
1012
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001013#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001014/*
1015 * Helper to set up a restart context if needed
1016 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
1018 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001019{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001020 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if (ctx == NULL || ctx->pk_info != NULL) {
1022 return 0;
1023 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001024
1025 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
1027 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1028 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
1031 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1032 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001033
1034 ctx->pk_info = info;
1035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001037}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001038#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001039
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001040/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001041 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001042 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001043int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
1044 mbedtls_md_type_t md_alg,
1045 const unsigned char *hash, size_t hash_len,
1046 const unsigned char *sig, size_t sig_len,
1047 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001048{
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +01001050 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ctx->pk_info == NULL ||
1054 pk_hashlen_helper(md_alg, &hash_len) != 0) {
1055 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1056 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001057
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001058#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001059 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001061 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001063 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
1066 return ret;
1067 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001068
valerio38992cb2023-04-20 09:56:30 +02001069 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1073 mbedtls_pk_restart_free(rs_ctx);
1074 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001077 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001078#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001079 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001080#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (ctx->pk_info->verify_func == NULL) {
1083 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1084 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +02001085
valerio38992cb2023-04-20 09:56:30 +02001086 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001088}
1089
1090/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001091 * Verify a signature
1092 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001093int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1094 const unsigned char *hash, size_t hash_len,
1095 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001096{
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
1098 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001099}
1100
1101/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001102 * Verify a signature with options
1103 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001104int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
1105 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1106 const unsigned char *hash, size_t hash_len,
1107 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001108{
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +01001110 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if (ctx->pk_info == NULL) {
1114 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1115 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001116
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 if (!mbedtls_pk_can_do(ctx, type)) {
1118 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1119 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001120
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001122 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (options != NULL) {
1124 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1125 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001128 }
1129
Valerio Setti07500fd2024-03-18 16:22:33 +01001130 /* Ensure the PK context is of the right type otherwise mbedtls_pk_rsa()
1131 * below would return a NULL pointer. */
1132 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_RSA) {
1133 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1134 }
1135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001136#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001137 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1138 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001139
Dave Rodgman02a53d72023-09-28 17:17:07 +01001140#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
1142 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1143 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001144#endif
Andres AG72849872017-01-19 11:24:33 +00001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (options == NULL) {
1147 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1148 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001149
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001150 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001151
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001152#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +02001154 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001155 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -05001156 int key_len;
1157 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001158 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
1159 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -05001160
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001161 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001162 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1163 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
1165 p = buf + sizeof(buf);
Valerio Setti3a815cb2024-02-14 09:54:18 +01001166 key_len = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*ctx), buf, &p);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 if (key_len < 0) {
1169 return key_len;
1170 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001171
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
1173 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1174 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 status = psa_import_key(&attributes,
1177 buf + sizeof(buf) - key_len, key_len,
1178 &key_id);
1179 if (status != PSA_SUCCESS) {
1180 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001181 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001182 }
1183
Andrzej Kurek4a953cd2022-02-16 06:13:35 -05001184 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
1185 * on a valid signature with trailing data in a buffer, but
1186 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
1187 * so for this reason the passed sig_len is overwritten. Smaller
1188 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
1190 mbedtls_pk_get_len(ctx) : sig_len;
1191 status = psa_verify_hash(key_id, psa_sig_alg, hash,
1192 hash_len, sig, signature_length);
1193 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001194
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
1196 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1197 }
Andrzej Kurek8666df62022-02-15 08:23:02 -05001198
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001200 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001202
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001203 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 } else
Tomi Fontanilles81746622023-07-16 13:06:06 +03001205#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001206 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 if (sig_len < mbedtls_pk_get_len(ctx)) {
1208 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
1209 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001210
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
1212 md_alg, (unsigned int) hash_len, hash,
1213 pss_opts->mgf1_hash_id,
1214 pss_opts->expected_salt_len,
1215 sig);
1216 if (ret != 0) {
1217 return ret;
1218 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001219
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (sig_len > mbedtls_pk_get_len(ctx)) {
1221 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1222 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -05001223
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001225 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001226#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001228#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001229}
1230
1231/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001232 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001233 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001234int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
1235 mbedtls_md_type_t md_alg,
1236 const unsigned char *hash, size_t hash_len,
1237 unsigned char *sig, size_t sig_size, size_t *sig_len,
1238 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1239 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001240{
Gilles Peskine449bd832023-01-11 14:50:10 +01001241 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +01001242 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
1246 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1247 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001248
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001249#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001250 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001252 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
1257 return ret;
1258 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001259
valerio38992cb2023-04-20 09:56:30 +02001260 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001261 hash, hash_len,
1262 sig, sig_size, sig_len,
1263 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1266 mbedtls_pk_restart_free(rs_ctx);
1267 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001270 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001271#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001272 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001273#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 if (ctx->pk_info->sign_func == NULL) {
1276 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1277 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001278
valerio38992cb2023-04-20 09:56:30 +02001279 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 hash, hash_len,
1281 sig, sig_size, sig_len,
1282 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001283}
1284
1285/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001286 * Make a signature
1287 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001288int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1289 const unsigned char *hash, size_t hash_len,
1290 unsigned char *sig, size_t sig_size, size_t *sig_len,
1291 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001292{
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
1294 sig, sig_size, sig_len,
1295 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001296}
1297
1298/*
Jerry Yufb0621d2022-03-23 11:42:06 +08001299 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +08001300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001301int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
1302 mbedtls_pk_context *ctx,
1303 mbedtls_md_type_t md_alg,
1304 const unsigned char *hash, size_t hash_len,
1305 unsigned char *sig, size_t sig_size, size_t *sig_len,
1306 int (*f_rng)(void *, unsigned char *, size_t),
1307 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +08001308{
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 if (ctx->pk_info == NULL) {
1310 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1311 }
Jerry Yud69439a2022-02-24 15:52:15 +08001312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (!mbedtls_pk_can_do(ctx, pk_type)) {
1314 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1315 }
Jerry Yud69439a2022-02-24 15:52:15 +08001316
Gilles Peskine449bd832023-01-11 14:50:10 +01001317 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
1318 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
1319 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +08001320 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001321
Tomi Fontanilles81746622023-07-16 13:06:06 +03001322#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
1323
1324#if defined(MBEDTLS_USE_PSA_CRYPTO)
1325 const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if (psa_md_alg == 0) {
1327 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1328 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001329
Gilles Peskine449bd832023-01-11 14:50:10 +01001330 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +02001331 psa_status_t status;
1332
Valerio Settia53f5432024-03-21 16:23:34 +01001333 /* PSA_ALG_RSA_PSS() behaves the same as PSA_ALG_RSA_PSS_ANY_SALT() when
1334 * performing a signature, but they are encoded differently. Instead of
1335 * extracting the proper one from the wrapped key policy, just try both. */
1336 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 hash, hash_len,
1338 sig, sig_size, sig_len);
Valerio Settia53f5432024-03-21 16:23:34 +01001339 if (status == PSA_ERROR_NOT_PERMITTED) {
1340 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg),
1341 hash, hash_len,
1342 sig, sig_size, sig_len);
1343 }
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001344 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +02001345 }
1346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
1348 ctx->pk_ctx, hash, hash_len,
1349 sig, sig_size, sig_len);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001350#else /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yud69439a2022-02-24 15:52:15 +08001351
Tomi Fontanilles81746622023-07-16 13:06:06 +03001352 if (sig_size < mbedtls_pk_get_len(ctx)) {
1353 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1354 }
1355
1356 if (pk_hashlen_helper(md_alg, &hash_len) != 0) {
1357 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1358 }
1359
1360 mbedtls_rsa_context *const rsa_ctx = mbedtls_pk_rsa(*ctx);
1361
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001362 const int ret = mbedtls_rsa_rsassa_pss_sign_no_mode_check(rsa_ctx, f_rng, p_rng, md_alg,
1363 (unsigned int) hash_len, hash, sig);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001364 if (ret == 0) {
1365 *sig_len = rsa_ctx->len;
1366 }
1367 return ret;
1368
1369#endif /* MBEDTLS_USE_PSA_CRYPTO */
1370
1371#else
1372 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1373#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Jerry Yud69439a2022-02-24 15:52:15 +08001374}
1375
1376/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001377 * Decrypt message
1378 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001379int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
1380 const unsigned char *input, size_t ilen,
1381 unsigned char *output, size_t *olen, size_t osize,
1382 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001383{
Gilles Peskine449bd832023-01-11 14:50:10 +01001384 if (ctx->pk_info == NULL) {
1385 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1386 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 if (ctx->pk_info->decrypt_func == NULL) {
1389 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1390 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001391
valerio38992cb2023-04-20 09:56:30 +02001392 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001394}
1395
1396/*
1397 * Encrypt message
1398 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001399int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
1400 const unsigned char *input, size_t ilen,
1401 unsigned char *output, size_t *olen, size_t osize,
1402 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001403{
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 if (ctx->pk_info == NULL) {
1405 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1406 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001407
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 if (ctx->pk_info->encrypt_func == NULL) {
1409 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1410 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001411
valerio38992cb2023-04-20 09:56:30 +02001412 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001414}
1415
1416/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001417 * Check public-private key pair
1418 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001419int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
1420 const mbedtls_pk_context *prv,
1421 int (*f_rng)(void *, unsigned char *, size_t),
1422 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001423{
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (pub->pk_info == NULL ||
1425 prv->pk_info == NULL) {
1426 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001427 }
1428
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 if (f_rng == NULL) {
1430 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001431 }
1432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (prv->pk_info->check_pair_func == NULL) {
1434 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1435 }
1436
1437 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
1438 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
1439 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1440 }
1441 } else {
valerio8cbef4d2023-06-01 10:59:03 +02001442 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
1443 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1445 }
1446 }
1447
valerio38992cb2023-04-20 09:56:30 +02001448 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
1449 (mbedtls_pk_context *) prv,
1450 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001451}
1452
1453/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001454 * Get key size in bits
1455 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001457{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001458 /* For backward compatibility, accept NULL or a context that
1459 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 if (ctx == NULL || ctx->pk_info == NULL) {
1461 return 0;
1462 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001463
valerio38992cb2023-04-20 09:56:30 +02001464 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001465}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001466
1467/*
1468 * Export debug information
1469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001470int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001471{
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 if (ctx->pk_info == NULL) {
1473 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1474 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 if (ctx->pk_info->debug_func == NULL) {
1477 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1478 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001479
valerio38992cb2023-04-20 09:56:30 +02001480 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001482}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001483
1484/*
1485 * Access the PK type name
1486 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001488{
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 if (ctx == NULL || ctx->pk_info == NULL) {
1490 return "invalid PK";
1491 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001494}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001495
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001496/*
1497 * Access the PK type
1498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001499mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001500{
Gilles Peskine449bd832023-01-11 14:50:10 +01001501 if (ctx == NULL || ctx->pk_info == NULL) {
1502 return MBEDTLS_PK_NONE;
1503 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001504
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001506}
1507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001508#endif /* MBEDTLS_PK_C */