blob: 51f0c240885f217251ca53a456cbfab8b79a2198 [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
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020038/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020040 */
Gilles Peskine449bd832023-01-11 14:50:10 +010041void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020042{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020043 ctx->pk_info = NULL;
44 ctx->pk_ctx = NULL;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020045#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4f387ef2023-05-02 14:15:59 +020046 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020047#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti722f8f72023-05-17 15:31:21 +020048#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
49 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
50 ctx->pub_raw_len = 0;
51 ctx->ec_family = 0;
52 ctx->ec_bits = 0;
53#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020054}
55
56/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060{
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010063 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020064
Valerio Settie00954d2023-04-28 15:24:32 +020065 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010066 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
67 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020068
Valerio Settib5361262023-05-18 18:51:58 +020069#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
70 /* The ownership of the priv_id key for opaque keys is external of the PK
71 * module. It's the user responsibility to clear it after use. */
72 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
73 psa_destroy_key(ctx->priv_id);
74 }
75#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020078}
79
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020080#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020081/*
82 * Initialize a restart context
83 */
Gilles Peskine449bd832023-01-11 14:50:10 +010084void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020085{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020086 ctx->pk_info = NULL;
87 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020088}
89
90/*
91 * Free the components of a restart context
92 */
Gilles Peskine449bd832023-01-11 14:50:10 +010093void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020094{
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if (ctx == NULL || ctx->pk_info == NULL ||
96 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020097 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020098 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200101
102 ctx->pk_info = NULL;
103 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200104}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200105#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200106
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200107/*
108 * Get pk_info structure from type
109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111{
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113#if defined(MBEDTLS_RSA_C)
114 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200116#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200117#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return &mbedtls_eckeydh_info;
Valerio Setti81d75122023-06-14 14:49:33 +0200122#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti7ca13182023-01-27 13:22:42 +0100123#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200126#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200128 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200130 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200131}
132
133/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200134 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200137{
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if (info == NULL || ctx->pk_info != NULL) {
139 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
140 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141
Valerio Settib5361262023-05-18 18:51:58 +0200142 if ((info->ctx_alloc_func != NULL) &&
Valerio Settie00954d2023-04-28 15:24:32 +0200143 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return MBEDTLS_ERR_PK_ALLOC_FAILED;
145 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200146
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200147 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200151
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200152#if defined(MBEDTLS_USE_PSA_CRYPTO)
153/*
154 * Initialise a PSA-wrapping context
155 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
157 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200158{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100159 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200160 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100161 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (ctx == NULL || ctx->pk_info != NULL) {
164 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
165 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
168 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
169 }
170 type = psa_get_key_type(&attributes);
171 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100172
Valerio Settif7cd4192023-06-30 18:11:29 +0200173#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200175 info = &mbedtls_ecdsa_opaque_info;
Valerio Settif7cd4192023-06-30 18:11:29 +0200176 } else
177#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
178 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200179 info = &mbedtls_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 } else {
181 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
182 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100183
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200184 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200185 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200188}
189#endif /* MBEDTLS_USE_PSA_CRYPTO */
190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100192/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200193 * Initialize an RSA-alt context
194 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
196 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
197 mbedtls_pk_rsa_alt_sign_func sign_func,
198 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200199{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_rsa_alt_context *rsa_alt;
201 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (ctx->pk_info != NULL) {
204 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
205 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
208 return MBEDTLS_ERR_PK_ALLOC_FAILED;
209 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200210
211 ctx->pk_info = info;
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200214
215 rsa_alt->key = key;
216 rsa_alt->decrypt_func = decrypt_func;
217 rsa_alt->sign_func = sign_func;
218 rsa_alt->key_len_func = key_len_func;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200223
224/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200225 * Tell if a PK can do the operations of the given type
226 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200228{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500229 /* A context with null pk_info is not set up yet and can't do anything.
230 * For backward compatibility, also accept NULL instead of a context
231 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 if (ctx == NULL || ctx->pk_info == NULL) {
233 return 0;
234 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200237}
238
Neil Armstronga88b1582022-05-11 14:11:25 +0200239#if defined(MBEDTLS_USE_PSA_CRYPTO)
240/*
241 * Tell if a PK can do the operations of the given PSA algorithm
242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
244 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200245{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200246 psa_key_usage_t key_usage;
247
Neil Armstronga88b1582022-05-11 14:11:25 +0200248 /* A context with null pk_info is not set up yet and can't do anything.
249 * For backward compatibility, also accept NULL instead of a context
250 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (ctx == NULL || ctx->pk_info == NULL) {
252 return 0;
253 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200254
255 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
257 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
258 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200259 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 PSA_ALG_IS_ECDH(alg) == 0) {
261 return 0;
262 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200263
Neil Armstrong408f6a62022-05-17 14:23:20 +0200264 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if (usage == 0 ||
266 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
267 PSA_KEY_USAGE_DECRYPT |
268 PSA_KEY_USAGE_DERIVE)) != 0) {
269 return 0;
270 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200271
Neil Armstronga88b1582022-05-11 14:11:25 +0200272 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 if (PSA_ALG_IS_SIGN_HASH(alg) &&
274 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
275 return 0;
276 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200279 mbedtls_pk_type_t type;
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200282 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
284 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200285 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200287 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 } else {
289 return 0;
290 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (ctx->pk_info->can_do(type) == 0) {
293 return 0;
294 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200297 case MBEDTLS_PK_ECKEY:
298 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
299 break;
300 case MBEDTLS_PK_RSA:
301 case MBEDTLS_PK_RSASSA_PSS:
302 key_usage = PSA_KEY_USAGE_SIGN_HASH |
303 PSA_KEY_USAGE_SIGN_MESSAGE |
304 PSA_KEY_USAGE_DECRYPT;
305 break;
306 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200307 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200309 }
310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200312 }
313
Neil Armstronga88b1582022-05-11 14:11:25 +0200314 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstronga88b1582022-05-11 14:11:25 +0200315 psa_status_t status;
316
Valerio Setti4f387ef2023-05-02 14:15:59 +0200317 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (status != PSA_SUCCESS) {
319 return 0;
320 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200321
Valerio Setti2bd53662023-12-05 10:14:06 +0100322 psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
Valerio Setti864a50b2024-03-12 13:41:19 +0100323 /* Key's enrollment is available only when an Mbed TLS implementation of PSA
324 * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
Valerio Setti2bd53662023-12-05 10:14:06 +0100325 * Even though we don't officially support using other implementations of PSA
Valerio Setti864a50b2024-03-12 13:41:19 +0100326 * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
327 * separated. */
328#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti2bd53662023-12-05 10:14:06 +0100329 psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
Valerio Setti864a50b2024-03-12 13:41:19 +0100330#endif /* MBEDTLS_PSA_CRYPTO_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 key_usage = psa_get_key_usage_flags(&attributes);
332 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if ((key_usage & usage) != usage) {
335 return 0;
336 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200337
Neil Armstronga88b1582022-05-11 14:11:25 +0200338 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100339 * Common case: the key alg [or alg2] only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200340 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
341 * directly.
342 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
Valerio Setti2bd53662023-12-05 10:14:06 +0100343 * a fixed hash on key_alg [or key_alg2].
Neil Armstronga88b1582022-05-11 14:11:25 +0200344 */
Valerio Setti2bd53662023-12-05 10:14:06 +0100345 if (alg == key_alg) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 return 1;
347 }
Valerio Setti864a50b2024-03-12 13:41:19 +0100348#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti2bd53662023-12-05 10:14:06 +0100349 if (alg == key_alg2) {
350 return 1;
351 }
Valerio Setti864a50b2024-03-12 13:41:19 +0100352#endif /* MBEDTLS_PSA_CRYPTO_C */
Neil Armstronga88b1582022-05-11 14:11:25 +0200353
354 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100355 * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200356 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200357 * then alg is compliant with this key alg
358 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
361 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
362 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
363 return 1;
364 }
Valerio Setti864a50b2024-03-12 13:41:19 +0100365#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
367 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
368 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
369 return 1;
370 }
Valerio Setti864a50b2024-03-12 13:41:19 +0100371#endif /* MBEDTLS_PSA_CRYPTO_C */
Neil Armstronga88b1582022-05-11 14:11:25 +0200372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200375}
376#endif /* MBEDTLS_USE_PSA_CRYPTO */
377
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100378#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine6ea18362024-01-18 14:16:27 +0100379#if defined(MBEDTLS_RSA_C)
380static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa,
381 int want_crypt)
382{
383 if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) {
384 if (want_crypt) {
Dave Rodgmanaa741652024-02-13 13:40:26 +0000385 mbedtls_md_type_t md_type = (mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa);
Gilles Peskine6ea18362024-01-18 14:16:27 +0100386 return PSA_ALG_RSA_OAEP(mbedtls_md_psa_alg_from_type(md_type));
387 } else {
388 return PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH);
389 }
390 } else {
391 if (want_crypt) {
392 return PSA_ALG_RSA_PKCS1V15_CRYPT;
393 } else {
394 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
395 }
396 }
397}
398#endif /* MBEDTLS_RSA_C */
399
Gilles Peskine0b172552024-01-18 14:11:26 +0100400int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
401 psa_key_usage_t usage,
402 psa_key_attributes_t *attributes)
403{
404 mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
405
Gilles Peskineace7c772024-01-18 17:47:54 +0100406 psa_key_usage_t more_usage = usage;
407 if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) {
408 more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
409 } else if (usage == PSA_KEY_USAGE_SIGN_HASH) {
410 more_usage |= PSA_KEY_USAGE_VERIFY_HASH;
411 } else if (usage == PSA_KEY_USAGE_DECRYPT) {
412 more_usage |= PSA_KEY_USAGE_ENCRYPT;
413 }
414 more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
415
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100416 int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE ||
417 usage == PSA_KEY_USAGE_VERIFY_HASH ||
418 usage == PSA_KEY_USAGE_ENCRYPT);
419
Gilles Peskine0b172552024-01-18 14:11:26 +0100420 switch (pk_type) {
Gilles Peskine6ea18362024-01-18 14:16:27 +0100421#if defined(MBEDTLS_RSA_C)
422 case MBEDTLS_PK_RSA:
Gilles Peskineace7c772024-01-18 17:47:54 +0100423 {
Gilles Peskinee8209752024-02-01 21:00:33 +0100424 int want_crypt = 0; /* 0: sign/verify; 1: encrypt/decrypt */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100425 switch (usage) {
426 case PSA_KEY_USAGE_SIGN_MESSAGE:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100427 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100428 case PSA_KEY_USAGE_VERIFY_MESSAGE:
429 case PSA_KEY_USAGE_VERIFY_HASH:
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100430 /* Nothing to do. */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100431 break;
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100432 case PSA_KEY_USAGE_DECRYPT:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100433 case PSA_KEY_USAGE_ENCRYPT:
434 want_crypt = 1;
435 break;
436 default:
437 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
438 }
439 /* Detect the presence of a private key in a way that works both
440 * in CRT and non-CRT configurations. */
441 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk);
442 int has_private = (mbedtls_rsa_check_privkey(rsa) == 0);
443 if (want_private && !has_private) {
444 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
445 }
446 psa_set_key_type(attributes, (want_private ?
447 PSA_KEY_TYPE_RSA_KEY_PAIR :
448 PSA_KEY_TYPE_RSA_PUBLIC_KEY));
Gilles Peskine55effd92024-01-23 18:07:36 +0100449 psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk));
Gilles Peskine6ea18362024-01-18 14:16:27 +0100450 psa_set_key_algorithm(attributes,
451 psa_algorithm_for_rsa(rsa, want_crypt));
452 break;
Gilles Peskineace7c772024-01-18 17:47:54 +0100453 }
Gilles Peskine6ea18362024-01-18 14:16:27 +0100454#endif /* MBEDTLS_RSA_C */
455
Gilles Peskineace7c772024-01-18 17:47:54 +0100456#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
457 case MBEDTLS_PK_ECKEY:
458 case MBEDTLS_PK_ECKEY_DH:
459 case MBEDTLS_PK_ECDSA:
460 {
461 int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH);
462 int derive_ok = (pk_type != MBEDTLS_PK_ECDSA);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100463#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskinecb3b4ca2024-02-02 13:12:39 +0100464 psa_ecc_family_t family = pk->ec_family;
465 size_t bits = pk->ec_bits;
466 int has_private = 0;
467 if (pk->priv_id != MBEDTLS_SVC_KEY_ID_INIT) {
468 has_private = 1;
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100469 }
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100470#else
Gilles Peskineae2668b2024-02-01 20:48:04 +0100471 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskineace7c772024-01-18 17:47:54 +0100472 int has_private = (ec->d.n != 0);
473 size_t bits = 0;
474 psa_ecc_family_t family =
475 mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100476#endif
Gilles Peskineace7c772024-01-18 17:47:54 +0100477 psa_algorithm_t alg = 0;
478 switch (usage) {
479 case PSA_KEY_USAGE_SIGN_MESSAGE:
480 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskineace7c772024-01-18 17:47:54 +0100481 case PSA_KEY_USAGE_VERIFY_MESSAGE:
482 case PSA_KEY_USAGE_VERIFY_HASH:
483 if (!sign_ok) {
484 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
485 }
486#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
487 alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
488#else
489 alg = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
490#endif
491 break;
492 case PSA_KEY_USAGE_DERIVE:
Gilles Peskineace7c772024-01-18 17:47:54 +0100493 alg = PSA_ALG_ECDH;
494 if (!derive_ok) {
495 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
496 }
497 break;
498 default:
499 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
500 }
501 if (want_private && !has_private) {
502 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
503 }
504 psa_set_key_type(attributes, (want_private ?
505 PSA_KEY_TYPE_ECC_KEY_PAIR(family) :
506 PSA_KEY_TYPE_ECC_PUBLIC_KEY(family)));
507 psa_set_key_bits(attributes, bits);
508 psa_set_key_algorithm(attributes, alg);
509 break;
510 }
511#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
512
Gilles Peskine0b172552024-01-18 14:11:26 +0100513#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
514 case MBEDTLS_PK_RSA_ALT:
515 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
516#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
517
Gilles Peskine758d8c72024-01-22 20:53:21 +0100518#if defined(MBEDTLS_USE_PSA_CRYPTO)
519 case MBEDTLS_PK_OPAQUE:
520 {
521 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
523 status = psa_get_key_attributes(pk->priv_id, &old_attributes);
524 if (status != PSA_SUCCESS) {
525 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
526 }
527 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
528 switch (usage) {
529 case PSA_KEY_USAGE_SIGN_MESSAGE:
530 case PSA_KEY_USAGE_SIGN_HASH:
531 case PSA_KEY_USAGE_VERIFY_MESSAGE:
532 case PSA_KEY_USAGE_VERIFY_HASH:
533 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) ||
534 old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) {
535 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
536 }
537 break;
538 case PSA_KEY_USAGE_DECRYPT:
539 case PSA_KEY_USAGE_ENCRYPT:
540 if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) {
541 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
542 }
543 break;
544 case PSA_KEY_USAGE_DERIVE:
545 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) {
546 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
547 }
548 break;
Gilles Peskine758d8c72024-01-22 20:53:21 +0100549 default:
550 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
551 }
552 psa_key_type_t new_type = old_type;
553 /* Opaque keys are always key pairs, so we don't need a check
554 * on the input if the required usage is private. We just need
555 * to adjust the type correctly if the required usage is public. */
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100556 if (!want_private) {
Gilles Peskine758d8c72024-01-22 20:53:21 +0100557 new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type);
558 }
559 more_usage = psa_get_key_usage_flags(&old_attributes);
Gilles Peskine793920c2024-02-01 21:26:54 +0100560 if ((usage & more_usage) == 0) {
561 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
562 }
Gilles Peskine758d8c72024-01-22 20:53:21 +0100563 psa_set_key_type(attributes, new_type);
564 psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes));
565 psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes));
566 break;
567 }
568#endif /* MBEDTLS_USE_PSA_CRYPTO */
569
Gilles Peskine0b172552024-01-18 14:11:26 +0100570 default:
571 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
572 }
573
Gilles Peskineace7c772024-01-18 17:47:54 +0100574 psa_set_key_usage_flags(attributes, more_usage);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100575 /* Key's enrollment is available only when an Mbed TLS implementation of PSA
576 * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
577 * Even though we don't officially support using other implementations of PSA
578 * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
579 * separated. */
580#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine1f97e732024-01-18 14:14:24 +0100581 psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100582#endif
Gilles Peskine0b172552024-01-18 14:11:26 +0100583
584 return 0;
585}
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100586
587#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_USE_PSA_CRYPTO)
588static psa_status_t export_import_into_psa(mbedtls_svc_key_id_t old_key_id,
589 const psa_key_attributes_t *attributes,
590 mbedtls_svc_key_id_t *new_key_id)
591{
592 unsigned char key_buffer[PSA_EXPORT_KEY_PAIR_MAX_SIZE];
593 size_t key_length = 0;
594 psa_status_t status = psa_export_key(old_key_id,
595 key_buffer, sizeof(key_buffer),
596 &key_length);
597 if (status != PSA_SUCCESS) {
598 return status;
599 }
600 status = psa_import_key(attributes, key_buffer, key_length, new_key_id);
601 mbedtls_platform_zeroize(key_buffer, key_length);
602 return status;
603}
604
605static int copy_into_psa(mbedtls_svc_key_id_t old_key_id,
606 const psa_key_attributes_t *attributes,
607 mbedtls_svc_key_id_t *new_key_id)
608{
609 /* Normally, we prefer copying: it's more efficient and works even
610 * for non-exportable keys. */
611 psa_status_t status = psa_copy_key(old_key_id, attributes, new_key_id);
612 if (status == PSA_ERROR_NOT_PERMITTED /*missing COPY usage*/ ||
613 status == PSA_ERROR_INVALID_ARGUMENT /*incompatible policy*/) {
614 /* There are edge cases where copying won't work, but export+import
615 * might:
616 * - If the old key does not allow PSA_KEY_USAGE_COPY.
617 * - If the old key's usage does not allow what attributes wants.
618 * Because the key was intended for use in the pk module, and may
619 * have had a policy chosen solely for what pk needs rather than
620 * based on a detailed understanding of PSA policies, we are a bit
621 * more liberal than psa_copy_key() here.
622 */
623 /* Here we need to check that the types match, otherwise we risk
624 * importing nonsensical data. */
625 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
626 status = psa_get_key_attributes(old_key_id, &old_attributes);
627 if (status != PSA_SUCCESS) {
628 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
629 }
630 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
631 psa_reset_key_attributes(&old_attributes);
632 if (old_type != psa_get_key_type(attributes)) {
633 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
634 }
635 status = export_import_into_psa(old_key_id, attributes, new_key_id);
636 }
637 return PSA_PK_TO_MBEDTLS_ERR(status);
638}
639#endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_USE_PSA_CRYPTO */
640
641static int import_pair_into_psa(const mbedtls_pk_context *pk,
642 const psa_key_attributes_t *attributes,
643 mbedtls_svc_key_id_t *key_id)
644{
645 switch (mbedtls_pk_get_type(pk)) {
646#if defined(MBEDTLS_RSA_C)
647 case MBEDTLS_PK_RSA:
648 {
649 if (psa_get_key_type(attributes) != PSA_KEY_TYPE_RSA_KEY_PAIR) {
650 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
651 }
652 unsigned char key_buffer[
653 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)];
654 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
655 unsigned char *key_data = key_end;
656 int ret = mbedtls_rsa_write_key(mbedtls_pk_rsa(*pk),
657 key_buffer, &key_data);
658 if (ret < 0) {
659 return ret;
660 }
661 size_t key_length = key_end - key_data;
662 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
663 key_data, key_length,
664 key_id));
665 mbedtls_platform_zeroize(key_data, key_length);
666 return ret;
667 }
668#endif /* MBEDTLS_RSA_C */
669
670#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
671 case MBEDTLS_PK_ECKEY:
672 case MBEDTLS_PK_ECKEY_DH:
673 case MBEDTLS_PK_ECDSA:
674 {
675 /* We need to check the curve family, otherwise the import could
676 * succeed with nonsensical data.
677 * We don't check the bit-size: it's optional in attributes,
678 * and if it's specified, psa_import_key() will know from the key
679 * data length and will check that the bit-size matches. */
680 psa_key_type_t to_type = psa_get_key_type(attributes);
681#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
682 psa_ecc_family_t from_family = pk->ec_family;
683#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine84a7bfb2024-02-28 14:21:32 +0100684 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100685 size_t from_bits = 0;
686 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
687 &from_bits);
688#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
689 if (to_type != PSA_KEY_TYPE_ECC_KEY_PAIR(from_family)) {
690 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
691 }
692
693#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
694 if (mbedtls_svc_key_id_is_null(pk->priv_id)) {
695 /* We have a public key and want a key pair. */
696 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
697 }
698 return copy_into_psa(pk->priv_id, attributes, key_id);
699#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
700 if (ec->d.n == 0) {
701 /* Private key not set. Assume the input is a public key only.
702 * (The other possibility is that it's an incomplete object
703 * where the group is set but neither the public key nor
704 * the private key. This is not possible through ecp.h
705 * functions, so we don't bother reporting a more suitable
706 * error in that case.) */
707 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
708 }
709 unsigned char key_buffer[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
Gilles Peskine84a7bfb2024-02-28 14:21:32 +0100710 size_t key_length = 0;
711 int ret = mbedtls_ecp_write_key_ext(ec, &key_length,
712 key_buffer, sizeof(key_buffer));
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100713 if (ret < 0) {
714 return ret;
715 }
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100716 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100717 key_buffer, key_length,
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100718 key_id));
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100719 mbedtls_platform_zeroize(key_buffer, key_length);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100720 return ret;
721#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
722 }
723#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
724
725#if defined(MBEDTLS_USE_PSA_CRYPTO)
726 case MBEDTLS_PK_OPAQUE:
727 return copy_into_psa(pk->priv_id, attributes, key_id);
728#endif /* MBEDTLS_USE_PSA_CRYPTO */
729
730 default:
731 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
732 }
733}
734
735static int import_public_into_psa(const mbedtls_pk_context *pk,
736 const psa_key_attributes_t *attributes,
737 mbedtls_svc_key_id_t *key_id)
738{
739 psa_key_type_t psa_type = psa_get_key_type(attributes);
740
741#if defined(MBEDTLS_RSA_C) || \
742 (defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)) || \
743 defined(MBEDTLS_USE_PSA_CRYPTO)
744 unsigned char key_buffer[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
745#endif
746 unsigned char *key_data = NULL;
747 size_t key_length = 0;
748
749 switch (mbedtls_pk_get_type(pk)) {
750#if defined(MBEDTLS_RSA_C)
751 case MBEDTLS_PK_RSA:
752 {
753 if (psa_type != PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
754 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
755 }
756 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
757 key_data = key_end;
758 int ret = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*pk),
759 key_buffer, &key_data);
760 if (ret < 0) {
761 return ret;
762 }
763 key_length = (size_t) ret;
764 break;
765 }
766#endif /*MBEDTLS_RSA_C */
767
768#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
769 case MBEDTLS_PK_ECKEY:
770 case MBEDTLS_PK_ECKEY_DH:
771 case MBEDTLS_PK_ECDSA:
772 {
773 /* We need to check the curve family, otherwise the import could
774 * succeed with nonsensical data.
775 * We don't check the bit-size: it's optional in attributes,
776 * and if it's specified, psa_import_key() will know from the key
777 * data length and will check that the bit-size matches. */
778#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
779 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)) {
780 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
781 }
782 key_data = (unsigned char *) pk->pub_raw;
783 key_length = pk->pub_raw_len;
784#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
785 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
786 size_t from_bits = 0;
787 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
788 &from_bits);
Gilles Peskine1d338762024-02-12 14:18:26 +0100789 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(from_family)) {
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100790 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
791 }
792 int ret = mbedtls_ecp_write_public_key(
793 ec, MBEDTLS_ECP_PF_UNCOMPRESSED,
794 &key_length, key_buffer, sizeof(key_buffer));
795 if (ret < 0) {
796 return ret;
797 }
798 key_data = key_buffer;
799#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
800 break;
801 }
802#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
803
804#if defined(MBEDTLS_USE_PSA_CRYPTO)
805 case MBEDTLS_PK_OPAQUE:
806 {
807 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
808 psa_status_t status =
809 psa_get_key_attributes(pk->priv_id, &old_attributes);
810 if (status != PSA_SUCCESS) {
811 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
812 }
813 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
814 psa_reset_key_attributes(&old_attributes);
815 if (psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(old_type)) {
816 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
817 }
818 status = psa_export_public_key(pk->priv_id,
819 key_buffer, sizeof(key_buffer),
820 &key_length);
821 if (status != PSA_SUCCESS) {
822 return PSA_PK_TO_MBEDTLS_ERR(status);
823 }
824 key_data = key_buffer;
825 break;
826 }
827#endif /* MBEDTLS_USE_PSA_CRYPTO */
828
829 default:
830 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
831 }
832
833 return PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
834 key_data, key_length,
835 key_id));
836}
837
838int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
839 const psa_key_attributes_t *attributes,
840 mbedtls_svc_key_id_t *key_id)
841{
842 /* Set the output immediately so that it won't contain garbage even
843 * if we error out before calling psa_import_key(). */
844 *key_id = MBEDTLS_SVC_KEY_ID_INIT;
845
846#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
847 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA_ALT) {
848 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
849 }
850#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
851
852 int want_public = PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(attributes));
853 if (want_public) {
854 return import_public_into_psa(pk, attributes, key_id);
855 } else {
856 return import_pair_into_psa(pk, attributes, key_id);
857 }
858}
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100859
860static int copy_from_psa(mbedtls_svc_key_id_t key_id,
861 mbedtls_pk_context *pk,
862 int public_only)
863{
864 psa_status_t status;
865 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
866 psa_key_type_t key_type;
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100867 size_t key_bits;
868 /* Use a buffer size large enough to contain either a key pair or public key. */
869 unsigned char exp_key[PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE];
870 size_t exp_key_len;
871 int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
872
873 if (pk == NULL) {
874 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
875 }
876
877 status = psa_get_key_attributes(key_id, &key_attr);
878 if (status != PSA_SUCCESS) {
879 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
880 }
881
882 if (public_only) {
883 status = psa_export_public_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
884 } else {
885 status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
886 }
887 if (status != PSA_SUCCESS) {
888 ret = PSA_PK_TO_MBEDTLS_ERR(status);
889 goto exit;
890 }
891
892 key_type = psa_get_key_type(&key_attr);
893 if (public_only) {
894 key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
895 }
896 key_bits = psa_get_key_bits(&key_attr);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100897
898#if defined(MBEDTLS_RSA_C)
899 if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) ||
900 (key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) {
901
902 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
903 if (ret != 0) {
904 goto exit;
905 }
906
907 if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
908 ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
909 } else {
910 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
911 }
912 if (ret != 0) {
913 goto exit;
914 }
915
Gilles Peskineb69757a2024-04-29 12:38:16 +0200916 psa_algorithm_t alg_type = psa_get_key_algorithm(&key_attr);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100917 mbedtls_md_type_t md_type = MBEDTLS_MD_NONE;
918 if (PSA_ALG_GET_HASH(alg_type) != PSA_ALG_ANY_HASH) {
919 md_type = mbedtls_md_type_from_psa_alg(alg_type);
920 }
921
922 if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) {
923 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type);
924 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg_type) ||
925 alg_type == PSA_ALG_RSA_PKCS1V15_CRYPT) {
926 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type);
927 }
928 if (ret != 0) {
929 goto exit;
930 }
931 } else
932#endif /* MBEDTLS_RSA_C */
933#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
934 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ||
935 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
936 mbedtls_ecp_group_id grp_id;
937
938 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
939 if (ret != 0) {
940 goto exit;
941 }
942
943 grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits);
944 ret = mbedtls_pk_ecc_set_group(pk, grp_id);
945 if (ret != 0) {
946 goto exit;
947 }
948
949 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
950 ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len);
951 if (ret != 0) {
952 goto exit;
953 }
954 ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, exp_key, exp_key_len,
955 mbedtls_psa_get_random,
956 MBEDTLS_PSA_RANDOM_STATE);
957 } else {
958 ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len);
959 }
960 if (ret != 0) {
961 goto exit;
962 }
963 } else
964#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
965 {
Valerio Setti4a350ca2024-04-02 11:31:33 +0200966 (void) key_bits;
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100967 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
968 }
969
970exit:
971 psa_reset_key_attributes(&key_attr);
972 mbedtls_platform_zeroize(exp_key, sizeof(exp_key));
973
974 return ret;
975}
976
977int mbedtls_pk_copy_from_psa(mbedtls_svc_key_id_t key_id,
978 mbedtls_pk_context *pk)
979{
980 return copy_from_psa(key_id, pk, 0);
981}
982
983int mbedtls_pk_copy_public_from_psa(mbedtls_svc_key_id_t key_id,
984 mbedtls_pk_context *pk)
985{
986 return copy_from_psa(key_id, pk, 1);
987}
988#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Gilles Peskine0b172552024-01-18 14:11:26 +0100989
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200990/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200991 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200992 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200994{
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 if (*hash_len != 0) {
996 return 0;
997 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200998
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200999 *hash_len = mbedtls_md_get_size_from_type(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +02001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (*hash_len == 0) {
1002 return -1;
1003 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001004
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001006}
1007
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001008#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001009/*
1010 * Helper to set up a restart context if needed
1011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
1013 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001014{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +02001015 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 if (ctx == NULL || ctx->pk_info != NULL) {
1017 return 0;
1018 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001019
1020 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
1022 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1023 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001024
Gilles Peskine449bd832023-01-11 14:50:10 +01001025 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
1026 return MBEDTLS_ERR_PK_ALLOC_FAILED;
1027 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001028
1029 ctx->pk_info = info;
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001032}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001033#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001034
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +02001035/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001036 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
1039 mbedtls_md_type_t md_alg,
1040 const unsigned char *hash, size_t hash_len,
1041 const unsigned char *sig, size_t sig_len,
1042 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001043{
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +01001045 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (ctx->pk_info == NULL ||
1049 pk_hashlen_helper(md_alg, &hash_len) != 0) {
1050 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1051 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001052
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001053#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001054 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001056 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001058 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001059
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
1061 return ret;
1062 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001063
valerio38992cb2023-04-20 09:56:30 +02001064 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1068 mbedtls_pk_restart_free(rs_ctx);
1069 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001072 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001073#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001074 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001075#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 if (ctx->pk_info->verify_func == NULL) {
1078 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1079 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +02001080
valerio38992cb2023-04-20 09:56:30 +02001081 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001083}
1084
1085/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001086 * Verify a signature
1087 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001088int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1089 const unsigned char *hash, size_t hash_len,
1090 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001091{
Gilles Peskine449bd832023-01-11 14:50:10 +01001092 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
1093 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001094}
1095
1096/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001097 * Verify a signature with options
1098 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001099int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
1100 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1101 const unsigned char *hash, size_t hash_len,
1102 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001103{
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +01001105 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001107
Gilles Peskine449bd832023-01-11 14:50:10 +01001108 if (ctx->pk_info == NULL) {
1109 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1110 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001111
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 if (!mbedtls_pk_can_do(ctx, type)) {
1113 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1114 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001117 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 if (options != NULL) {
1119 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1120 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001123 }
1124
Valerio Setti07500fd2024-03-18 16:22:33 +01001125 /* Ensure the PK context is of the right type otherwise mbedtls_pk_rsa()
1126 * below would return a NULL pointer. */
1127 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_RSA) {
1128 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1129 }
1130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001131#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1133 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001134
Dave Rodgman02a53d72023-09-28 17:17:07 +01001135#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
1137 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1138 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001139#endif
Andres AG72849872017-01-19 11:24:33 +00001140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 if (options == NULL) {
1142 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1143 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001144
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001145 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001146
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001147#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +02001149 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001150 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -05001151 int key_len;
1152 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001153 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
1154 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -05001155
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001156 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001157 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1158 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
1160 p = buf + sizeof(buf);
Valerio Setti3a815cb2024-02-14 09:54:18 +01001161 key_len = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*ctx), buf, &p);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001162
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (key_len < 0) {
1164 return key_len;
1165 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001166
Gilles Peskine449bd832023-01-11 14:50:10 +01001167 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
1168 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1169 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001170
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 status = psa_import_key(&attributes,
1172 buf + sizeof(buf) - key_len, key_len,
1173 &key_id);
1174 if (status != PSA_SUCCESS) {
1175 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001176 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001177 }
1178
Andrzej Kurek4a953cd2022-02-16 06:13:35 -05001179 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
1180 * on a valid signature with trailing data in a buffer, but
1181 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
1182 * so for this reason the passed sig_len is overwritten. Smaller
1183 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
1185 mbedtls_pk_get_len(ctx) : sig_len;
1186 status = psa_verify_hash(key_id, psa_sig_alg, hash,
1187 hash_len, sig, signature_length);
1188 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
1191 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1192 }
Andrzej Kurek8666df62022-02-15 08:23:02 -05001193
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001195 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001197
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001198 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 } else
Tomi Fontanilles81746622023-07-16 13:06:06 +03001200#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001201 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 if (sig_len < mbedtls_pk_get_len(ctx)) {
1203 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
1204 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
1207 md_alg, (unsigned int) hash_len, hash,
1208 pss_opts->mgf1_hash_id,
1209 pss_opts->expected_salt_len,
1210 sig);
1211 if (ret != 0) {
1212 return ret;
1213 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 if (sig_len > mbedtls_pk_get_len(ctx)) {
1216 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1217 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -05001218
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001220 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001221#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001223#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001224}
1225
1226/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001227 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001228 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001229int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
1230 mbedtls_md_type_t md_alg,
1231 const unsigned char *hash, size_t hash_len,
1232 unsigned char *sig, size_t sig_size, size_t *sig_len,
1233 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1234 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001235{
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +01001237 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001239
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
1241 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1242 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001243
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001244#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001245 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001247 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
1252 return ret;
1253 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001254
valerio38992cb2023-04-20 09:56:30 +02001255 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 hash, hash_len,
1257 sig, sig_size, sig_len,
1258 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1261 mbedtls_pk_restart_free(rs_ctx);
1262 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001265 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001266#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001267 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001268#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 if (ctx->pk_info->sign_func == NULL) {
1271 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1272 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001273
valerio38992cb2023-04-20 09:56:30 +02001274 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 hash, hash_len,
1276 sig, sig_size, sig_len,
1277 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001278}
1279
1280/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001281 * Make a signature
1282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1284 const unsigned char *hash, size_t hash_len,
1285 unsigned char *sig, size_t sig_size, size_t *sig_len,
1286 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001287{
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
1289 sig, sig_size, sig_len,
1290 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001291}
1292
1293/*
Jerry Yufb0621d2022-03-23 11:42:06 +08001294 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +08001295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
1297 mbedtls_pk_context *ctx,
1298 mbedtls_md_type_t md_alg,
1299 const unsigned char *hash, size_t hash_len,
1300 unsigned char *sig, size_t sig_size, size_t *sig_len,
1301 int (*f_rng)(void *, unsigned char *, size_t),
1302 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +08001303{
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 if (ctx->pk_info == NULL) {
1305 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1306 }
Jerry Yud69439a2022-02-24 15:52:15 +08001307
Gilles Peskine449bd832023-01-11 14:50:10 +01001308 if (!mbedtls_pk_can_do(ctx, pk_type)) {
1309 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1310 }
Jerry Yud69439a2022-02-24 15:52:15 +08001311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
1313 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
1314 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +08001315 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001316
Tomi Fontanilles81746622023-07-16 13:06:06 +03001317#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
1318
1319#if defined(MBEDTLS_USE_PSA_CRYPTO)
1320 const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 if (psa_md_alg == 0) {
1322 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1323 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001324
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +02001326 psa_status_t status;
1327
Valerio Settia53f5432024-03-21 16:23:34 +01001328 /* PSA_ALG_RSA_PSS() behaves the same as PSA_ALG_RSA_PSS_ANY_SALT() when
1329 * performing a signature, but they are encoded differently. Instead of
1330 * extracting the proper one from the wrapped key policy, just try both. */
1331 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 hash, hash_len,
1333 sig, sig_size, sig_len);
Valerio Settia53f5432024-03-21 16:23:34 +01001334 if (status == PSA_ERROR_NOT_PERMITTED) {
1335 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg),
1336 hash, hash_len,
1337 sig, sig_size, sig_len);
1338 }
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001339 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +02001340 }
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
1343 ctx->pk_ctx, hash, hash_len,
1344 sig, sig_size, sig_len);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001345#else /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yud69439a2022-02-24 15:52:15 +08001346
Tomi Fontanilles81746622023-07-16 13:06:06 +03001347 if (sig_size < mbedtls_pk_get_len(ctx)) {
1348 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1349 }
1350
1351 if (pk_hashlen_helper(md_alg, &hash_len) != 0) {
1352 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1353 }
1354
1355 mbedtls_rsa_context *const rsa_ctx = mbedtls_pk_rsa(*ctx);
1356
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001357 const int ret = mbedtls_rsa_rsassa_pss_sign_no_mode_check(rsa_ctx, f_rng, p_rng, md_alg,
1358 (unsigned int) hash_len, hash, sig);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001359 if (ret == 0) {
1360 *sig_len = rsa_ctx->len;
1361 }
1362 return ret;
1363
1364#endif /* MBEDTLS_USE_PSA_CRYPTO */
1365
1366#else
1367 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1368#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Jerry Yud69439a2022-02-24 15:52:15 +08001369}
1370
1371/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001372 * Decrypt message
1373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001374int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
1375 const unsigned char *input, size_t ilen,
1376 unsigned char *output, size_t *olen, size_t osize,
1377 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001378{
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 if (ctx->pk_info == NULL) {
1380 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1381 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001382
Gilles Peskine449bd832023-01-11 14:50:10 +01001383 if (ctx->pk_info->decrypt_func == NULL) {
1384 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1385 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001386
valerio38992cb2023-04-20 09:56:30 +02001387 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001389}
1390
1391/*
1392 * Encrypt message
1393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
1395 const unsigned char *input, size_t ilen,
1396 unsigned char *output, size_t *olen, size_t osize,
1397 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001398{
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 if (ctx->pk_info == NULL) {
1400 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1401 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001402
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 if (ctx->pk_info->encrypt_func == NULL) {
1404 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1405 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001406
valerio38992cb2023-04-20 09:56:30 +02001407 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001409}
1410
1411/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001412 * Check public-private key pair
1413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001414int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
1415 const mbedtls_pk_context *prv,
1416 int (*f_rng)(void *, unsigned char *, size_t),
1417 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001418{
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 if (pub->pk_info == NULL ||
1420 prv->pk_info == NULL) {
1421 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001422 }
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (f_rng == NULL) {
1425 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001426 }
1427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 if (prv->pk_info->check_pair_func == NULL) {
1429 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1430 }
1431
1432 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
1433 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
1434 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1435 }
1436 } else {
valerio8cbef4d2023-06-01 10:59:03 +02001437 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
1438 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1440 }
1441 }
1442
valerio38992cb2023-04-20 09:56:30 +02001443 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
1444 (mbedtls_pk_context *) prv,
1445 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001446}
1447
1448/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001449 * Get key size in bits
1450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001451size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001452{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001453 /* For backward compatibility, accept NULL or a context that
1454 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (ctx == NULL || ctx->pk_info == NULL) {
1456 return 0;
1457 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001458
valerio38992cb2023-04-20 09:56:30 +02001459 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001460}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001461
1462/*
1463 * Export debug information
1464 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001465int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001466{
Gilles Peskine449bd832023-01-11 14:50:10 +01001467 if (ctx->pk_info == NULL) {
1468 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1469 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 if (ctx->pk_info->debug_func == NULL) {
1472 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1473 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001474
valerio38992cb2023-04-20 09:56:30 +02001475 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001477}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001478
1479/*
1480 * Access the PK type name
1481 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001482const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001483{
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (ctx == NULL || ctx->pk_info == NULL) {
1485 return "invalid PK";
1486 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001489}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001490
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001491/*
1492 * Access the PK type
1493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001494mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001495{
Gilles Peskine449bd832023-01-11 14:50:10 +01001496 if (ctx == NULL || ctx->pk_info == NULL) {
1497 return MBEDTLS_PK_NONE;
1498 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001499
Gilles Peskine449bd832023-01-11 14:50:10 +01001500 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001501}
1502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001503#endif /* MBEDTLS_PK_C */