blob: ccc2eac6d08b737d5854a68c84fecde03d90e095 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000011#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000012#include "pk_wrap.h"
Neil Armstrongca5b55f2022-03-15 15:00:55 +010013#include "pkwrite.h"
Valerio Setti3f00b842023-05-15 12:57:06 +020014#include "pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020015
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050016#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000017#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020#include "mbedtls/rsa.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020021#include "rsa_internal.h"
22#endif
Valerio Setti81d75122023-06-14 14:49:33 +020023#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020025#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020028#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020029
Valerio Settia657ae32024-02-23 17:55:28 +010030#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020031#include "psa_util_internal.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010032#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010033#endif
34
Andres AG72849872017-01-19 11:24:33 +000035#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010036#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020037
Valerio Setti070d95e2024-02-01 11:29:15 +010038#define PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE \
39 (PSA_EXPORT_KEY_PAIR_MAX_SIZE > PSA_EXPORT_PUBLIC_KEY_MAX_SIZE) ? \
40 PSA_EXPORT_KEY_PAIR_MAX_SIZE : PSA_EXPORT_PUBLIC_KEY_MAX_SIZE
41
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020042/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020044 */
Gilles Peskine449bd832023-01-11 14:50:10 +010045void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020046{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020047 ctx->pk_info = NULL;
48 ctx->pk_ctx = NULL;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020049#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4f387ef2023-05-02 14:15:59 +020050 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020051#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti722f8f72023-05-17 15:31:21 +020052#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
53 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
54 ctx->pub_raw_len = 0;
55 ctx->ec_family = 0;
56 ctx->ec_bits = 0;
57#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058}
59
60/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020064{
Gilles Peskine449bd832023-01-11 14:50:10 +010065 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010067 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020068
Valerio Settie00954d2023-04-28 15:24:32 +020069 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010070 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
71 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020072
Valerio Settib5361262023-05-18 18:51:58 +020073#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
74 /* The ownership of the priv_id key for opaque keys is external of the PK
75 * module. It's the user responsibility to clear it after use. */
76 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
77 psa_destroy_key(ctx->priv_id);
78 }
79#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020082}
83
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020084#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020085/*
86 * Initialize a restart context
87 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020089{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020090 ctx->pk_info = NULL;
91 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020092}
93
94/*
95 * Free the components of a restart context
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (ctx == NULL || ctx->pk_info == NULL ||
100 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200101 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200102 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200105
106 ctx->pk_info = NULL;
107 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200108}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200109#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200110
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111/*
112 * Get pk_info structure from type
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200115{
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_RSA_C)
118 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200120#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200121#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return &mbedtls_eckeydh_info;
Valerio Setti81d75122023-06-14 14:49:33 +0200126#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti7ca13182023-01-27 13:22:42 +0100127#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200130#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200132 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200134 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135}
136
137/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200138 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200139 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141{
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (info == NULL || ctx->pk_info != NULL) {
143 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
144 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200145
Valerio Settib5361262023-05-18 18:51:58 +0200146 if ((info->ctx_alloc_func != NULL) &&
Valerio Settie00954d2023-04-28 15:24:32 +0200147 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return MBEDTLS_ERR_PK_ALLOC_FAILED;
149 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200151 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200154}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200155
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200156#if defined(MBEDTLS_USE_PSA_CRYPTO)
157/*
158 * Initialise a PSA-wrapping context
159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
161 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200162{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100163 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100165 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (ctx == NULL || ctx->pk_info != NULL) {
168 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
169 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
172 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
173 }
174 type = psa_get_key_type(&attributes);
175 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100176
Valerio Settif7cd4192023-06-30 18:11:29 +0200177#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200179 info = &mbedtls_ecdsa_opaque_info;
Valerio Settif7cd4192023-06-30 18:11:29 +0200180 } else
181#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
182 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200183 info = &mbedtls_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 } else {
185 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
186 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100187
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200188 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200189 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200192}
193#endif /* MBEDTLS_USE_PSA_CRYPTO */
194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100196/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200197 * Initialize an RSA-alt context
198 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
200 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
201 mbedtls_pk_rsa_alt_sign_func sign_func,
202 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200203{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_rsa_alt_context *rsa_alt;
205 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 if (ctx->pk_info != NULL) {
208 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
209 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
212 return MBEDTLS_ERR_PK_ALLOC_FAILED;
213 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200214
215 ctx->pk_info = info;
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200218
219 rsa_alt->key = key;
220 rsa_alt->decrypt_func = decrypt_func;
221 rsa_alt->sign_func = sign_func;
222 rsa_alt->key_len_func = key_len_func;
223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200225}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200227
228/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200229 * Tell if a PK can do the operations of the given type
230 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100231int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200232{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500233 /* A context with null pk_info is not set up yet and can't do anything.
234 * For backward compatibility, also accept NULL instead of a context
235 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (ctx == NULL || ctx->pk_info == NULL) {
237 return 0;
238 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200241}
242
Neil Armstronga88b1582022-05-11 14:11:25 +0200243#if defined(MBEDTLS_USE_PSA_CRYPTO)
244/*
245 * Tell if a PK can do the operations of the given PSA algorithm
246 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
248 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200249{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200250 psa_key_usage_t key_usage;
251
Neil Armstronga88b1582022-05-11 14:11:25 +0200252 /* A context with null pk_info is not set up yet and can't do anything.
253 * For backward compatibility, also accept NULL instead of a context
254 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if (ctx == NULL || ctx->pk_info == NULL) {
256 return 0;
257 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200258
259 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
261 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
262 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200263 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 PSA_ALG_IS_ECDH(alg) == 0) {
265 return 0;
266 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200267
Neil Armstrong408f6a62022-05-17 14:23:20 +0200268 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (usage == 0 ||
270 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
271 PSA_KEY_USAGE_DECRYPT |
272 PSA_KEY_USAGE_DERIVE)) != 0) {
273 return 0;
274 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200275
Neil Armstronga88b1582022-05-11 14:11:25 +0200276 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (PSA_ALG_IS_SIGN_HASH(alg) &&
278 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
279 return 0;
280 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200283 mbedtls_pk_type_t type;
284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200286 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
288 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200289 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200291 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 } else {
293 return 0;
294 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (ctx->pk_info->can_do(type) == 0) {
297 return 0;
298 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200301 case MBEDTLS_PK_ECKEY:
302 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
303 break;
304 case MBEDTLS_PK_RSA:
305 case MBEDTLS_PK_RSASSA_PSS:
306 key_usage = PSA_KEY_USAGE_SIGN_HASH |
307 PSA_KEY_USAGE_SIGN_MESSAGE |
308 PSA_KEY_USAGE_DECRYPT;
309 break;
310 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200311 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200313 }
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200316 }
317
Neil Armstronga88b1582022-05-11 14:11:25 +0200318 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstronga88b1582022-05-11 14:11:25 +0200319 psa_status_t status;
320
Valerio Setti4f387ef2023-05-02 14:15:59 +0200321 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (status != PSA_SUCCESS) {
323 return 0;
324 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200325
Valerio Setti2bd53662023-12-05 10:14:06 +0100326 psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
327 /* Key's enrollment is available only when MBEDTLS_PSA_CRYPTO_CLIENT is
328 * defined, i.e. when the Mbed TLS implementation of PSA Crypto is being used.
329 * Even though we don't officially support using other implementations of PSA
330 * Crypto with TLS and X.509 (yet), we're still trying to simplify the life of
331 * people who would like to try it before it's officially supported. */
332#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
333 psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
334#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 key_usage = psa_get_key_usage_flags(&attributes);
336 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if ((key_usage & usage) != usage) {
339 return 0;
340 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200341
Neil Armstronga88b1582022-05-11 14:11:25 +0200342 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100343 * Common case: the key alg [or alg2] only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200344 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
345 * directly.
346 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
Valerio Setti2bd53662023-12-05 10:14:06 +0100347 * a fixed hash on key_alg [or key_alg2].
Neil Armstronga88b1582022-05-11 14:11:25 +0200348 */
Valerio Setti2bd53662023-12-05 10:14:06 +0100349 if (alg == key_alg) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 return 1;
351 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100352#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
353 if (alg == key_alg2) {
354 return 1;
355 }
356#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200357
358 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100359 * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200360 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200361 * then alg is compliant with this key alg
362 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
366 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
367 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
368 return 1;
369 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100370#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
372 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
373 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
374 return 1;
375 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100376#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200377 }
378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200380}
381#endif /* MBEDTLS_USE_PSA_CRYPTO */
382
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100383#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine6ea18362024-01-18 14:16:27 +0100384#if defined(MBEDTLS_RSA_C)
385static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa,
386 int want_crypt)
387{
388 if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) {
389 if (want_crypt) {
Dave Rodgmanaa741652024-02-13 13:40:26 +0000390 mbedtls_md_type_t md_type = (mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa);
Gilles Peskine6ea18362024-01-18 14:16:27 +0100391 return PSA_ALG_RSA_OAEP(mbedtls_md_psa_alg_from_type(md_type));
392 } else {
393 return PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH);
394 }
395 } else {
396 if (want_crypt) {
397 return PSA_ALG_RSA_PKCS1V15_CRYPT;
398 } else {
399 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
400 }
401 }
402}
403#endif /* MBEDTLS_RSA_C */
404
Gilles Peskine0b172552024-01-18 14:11:26 +0100405int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
406 psa_key_usage_t usage,
407 psa_key_attributes_t *attributes)
408{
409 mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
410
Gilles Peskineace7c772024-01-18 17:47:54 +0100411 psa_key_usage_t more_usage = usage;
412 if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) {
413 more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
414 } else if (usage == PSA_KEY_USAGE_SIGN_HASH) {
415 more_usage |= PSA_KEY_USAGE_VERIFY_HASH;
416 } else if (usage == PSA_KEY_USAGE_DECRYPT) {
417 more_usage |= PSA_KEY_USAGE_ENCRYPT;
418 }
419 more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
420
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100421 int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE ||
422 usage == PSA_KEY_USAGE_VERIFY_HASH ||
423 usage == PSA_KEY_USAGE_ENCRYPT);
424
Gilles Peskine0b172552024-01-18 14:11:26 +0100425 switch (pk_type) {
Gilles Peskine6ea18362024-01-18 14:16:27 +0100426#if defined(MBEDTLS_RSA_C)
427 case MBEDTLS_PK_RSA:
Gilles Peskineace7c772024-01-18 17:47:54 +0100428 {
Gilles Peskinee8209752024-02-01 21:00:33 +0100429 int want_crypt = 0; /* 0: sign/verify; 1: encrypt/decrypt */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100430 switch (usage) {
431 case PSA_KEY_USAGE_SIGN_MESSAGE:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100432 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100433 case PSA_KEY_USAGE_VERIFY_MESSAGE:
434 case PSA_KEY_USAGE_VERIFY_HASH:
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100435 /* Nothing to do. */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100436 break;
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100437 case PSA_KEY_USAGE_DECRYPT:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100438 case PSA_KEY_USAGE_ENCRYPT:
439 want_crypt = 1;
440 break;
441 default:
442 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
443 }
444 /* Detect the presence of a private key in a way that works both
445 * in CRT and non-CRT configurations. */
446 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk);
447 int has_private = (mbedtls_rsa_check_privkey(rsa) == 0);
448 if (want_private && !has_private) {
449 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
450 }
451 psa_set_key_type(attributes, (want_private ?
452 PSA_KEY_TYPE_RSA_KEY_PAIR :
453 PSA_KEY_TYPE_RSA_PUBLIC_KEY));
Gilles Peskine55effd92024-01-23 18:07:36 +0100454 psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk));
Gilles Peskine6ea18362024-01-18 14:16:27 +0100455 psa_set_key_algorithm(attributes,
456 psa_algorithm_for_rsa(rsa, want_crypt));
457 break;
Gilles Peskineace7c772024-01-18 17:47:54 +0100458 }
Gilles Peskine6ea18362024-01-18 14:16:27 +0100459#endif /* MBEDTLS_RSA_C */
460
Gilles Peskineace7c772024-01-18 17:47:54 +0100461#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
462 case MBEDTLS_PK_ECKEY:
463 case MBEDTLS_PK_ECKEY_DH:
464 case MBEDTLS_PK_ECDSA:
465 {
466 int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH);
467 int derive_ok = (pk_type != MBEDTLS_PK_ECDSA);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100468#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskinecb3b4ca2024-02-02 13:12:39 +0100469 psa_ecc_family_t family = pk->ec_family;
470 size_t bits = pk->ec_bits;
471 int has_private = 0;
472 if (pk->priv_id != MBEDTLS_SVC_KEY_ID_INIT) {
473 has_private = 1;
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100474 }
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100475#else
Gilles Peskineae2668b2024-02-01 20:48:04 +0100476 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskineace7c772024-01-18 17:47:54 +0100477 int has_private = (ec->d.n != 0);
478 size_t bits = 0;
479 psa_ecc_family_t family =
480 mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100481#endif
Gilles Peskineace7c772024-01-18 17:47:54 +0100482 psa_algorithm_t alg = 0;
483 switch (usage) {
484 case PSA_KEY_USAGE_SIGN_MESSAGE:
485 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskineace7c772024-01-18 17:47:54 +0100486 case PSA_KEY_USAGE_VERIFY_MESSAGE:
487 case PSA_KEY_USAGE_VERIFY_HASH:
488 if (!sign_ok) {
489 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
490 }
491#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
492 alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
493#else
494 alg = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
495#endif
496 break;
497 case PSA_KEY_USAGE_DERIVE:
Gilles Peskineace7c772024-01-18 17:47:54 +0100498 alg = PSA_ALG_ECDH;
499 if (!derive_ok) {
500 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
501 }
502 break;
503 default:
504 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
505 }
506 if (want_private && !has_private) {
507 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
508 }
509 psa_set_key_type(attributes, (want_private ?
510 PSA_KEY_TYPE_ECC_KEY_PAIR(family) :
511 PSA_KEY_TYPE_ECC_PUBLIC_KEY(family)));
512 psa_set_key_bits(attributes, bits);
513 psa_set_key_algorithm(attributes, alg);
514 break;
515 }
516#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
517
Gilles Peskine0b172552024-01-18 14:11:26 +0100518#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
519 case MBEDTLS_PK_RSA_ALT:
520 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
521#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
522
Gilles Peskine758d8c72024-01-22 20:53:21 +0100523#if defined(MBEDTLS_USE_PSA_CRYPTO)
524 case MBEDTLS_PK_OPAQUE:
525 {
526 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
528 status = psa_get_key_attributes(pk->priv_id, &old_attributes);
529 if (status != PSA_SUCCESS) {
530 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
531 }
532 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
533 switch (usage) {
534 case PSA_KEY_USAGE_SIGN_MESSAGE:
535 case PSA_KEY_USAGE_SIGN_HASH:
536 case PSA_KEY_USAGE_VERIFY_MESSAGE:
537 case PSA_KEY_USAGE_VERIFY_HASH:
538 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) ||
539 old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) {
540 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
541 }
542 break;
543 case PSA_KEY_USAGE_DECRYPT:
544 case PSA_KEY_USAGE_ENCRYPT:
545 if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) {
546 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
547 }
548 break;
549 case PSA_KEY_USAGE_DERIVE:
550 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) {
551 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
552 }
553 break;
Gilles Peskine758d8c72024-01-22 20:53:21 +0100554 default:
555 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
556 }
557 psa_key_type_t new_type = old_type;
558 /* Opaque keys are always key pairs, so we don't need a check
559 * on the input if the required usage is private. We just need
560 * to adjust the type correctly if the required usage is public. */
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100561 if (!want_private) {
Gilles Peskine758d8c72024-01-22 20:53:21 +0100562 new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type);
563 }
564 more_usage = psa_get_key_usage_flags(&old_attributes);
Gilles Peskine793920c2024-02-01 21:26:54 +0100565 if ((usage & more_usage) == 0) {
566 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
567 }
Gilles Peskine758d8c72024-01-22 20:53:21 +0100568 psa_set_key_type(attributes, new_type);
569 psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes));
570 psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes));
571 break;
572 }
573#endif /* MBEDTLS_USE_PSA_CRYPTO */
574
Gilles Peskine0b172552024-01-18 14:11:26 +0100575 default:
576 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
577 }
578
Gilles Peskineace7c772024-01-18 17:47:54 +0100579 psa_set_key_usage_flags(attributes, more_usage);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100580 /* Key's enrollment is available only when an Mbed TLS implementation of PSA
581 * Crypto is being used, i.e. when MBEDTLS_PSA_CRYPTO_C is defined.
582 * Even though we don't officially support using other implementations of PSA
583 * Crypto with TLS and X.509 (yet), we try to keep vendor's customizations
584 * separated. */
585#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine1f97e732024-01-18 14:14:24 +0100586 psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100587#endif
Gilles Peskine0b172552024-01-18 14:11:26 +0100588
589 return 0;
590}
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100591
592#if defined(MBEDTLS_PK_USE_PSA_EC_DATA) || defined(MBEDTLS_USE_PSA_CRYPTO)
593static psa_status_t export_import_into_psa(mbedtls_svc_key_id_t old_key_id,
594 const psa_key_attributes_t *attributes,
595 mbedtls_svc_key_id_t *new_key_id)
596{
597 unsigned char key_buffer[PSA_EXPORT_KEY_PAIR_MAX_SIZE];
598 size_t key_length = 0;
599 psa_status_t status = psa_export_key(old_key_id,
600 key_buffer, sizeof(key_buffer),
601 &key_length);
602 if (status != PSA_SUCCESS) {
603 return status;
604 }
605 status = psa_import_key(attributes, key_buffer, key_length, new_key_id);
606 mbedtls_platform_zeroize(key_buffer, key_length);
607 return status;
608}
609
610static int copy_into_psa(mbedtls_svc_key_id_t old_key_id,
611 const psa_key_attributes_t *attributes,
612 mbedtls_svc_key_id_t *new_key_id)
613{
614 /* Normally, we prefer copying: it's more efficient and works even
615 * for non-exportable keys. */
616 psa_status_t status = psa_copy_key(old_key_id, attributes, new_key_id);
617 if (status == PSA_ERROR_NOT_PERMITTED /*missing COPY usage*/ ||
618 status == PSA_ERROR_INVALID_ARGUMENT /*incompatible policy*/) {
619 /* There are edge cases where copying won't work, but export+import
620 * might:
621 * - If the old key does not allow PSA_KEY_USAGE_COPY.
622 * - If the old key's usage does not allow what attributes wants.
623 * Because the key was intended for use in the pk module, and may
624 * have had a policy chosen solely for what pk needs rather than
625 * based on a detailed understanding of PSA policies, we are a bit
626 * more liberal than psa_copy_key() here.
627 */
628 /* Here we need to check that the types match, otherwise we risk
629 * importing nonsensical data. */
630 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
631 status = psa_get_key_attributes(old_key_id, &old_attributes);
632 if (status != PSA_SUCCESS) {
633 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
634 }
635 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
636 psa_reset_key_attributes(&old_attributes);
637 if (old_type != psa_get_key_type(attributes)) {
638 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
639 }
640 status = export_import_into_psa(old_key_id, attributes, new_key_id);
641 }
642 return PSA_PK_TO_MBEDTLS_ERR(status);
643}
644#endif /* MBEDTLS_PK_USE_PSA_EC_DATA || MBEDTLS_USE_PSA_CRYPTO */
645
646static int import_pair_into_psa(const mbedtls_pk_context *pk,
647 const psa_key_attributes_t *attributes,
648 mbedtls_svc_key_id_t *key_id)
649{
650 switch (mbedtls_pk_get_type(pk)) {
651#if defined(MBEDTLS_RSA_C)
652 case MBEDTLS_PK_RSA:
653 {
654 if (psa_get_key_type(attributes) != PSA_KEY_TYPE_RSA_KEY_PAIR) {
655 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
656 }
657 unsigned char key_buffer[
658 PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE(PSA_VENDOR_RSA_MAX_KEY_BITS)];
659 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
660 unsigned char *key_data = key_end;
661 int ret = mbedtls_rsa_write_key(mbedtls_pk_rsa(*pk),
662 key_buffer, &key_data);
663 if (ret < 0) {
664 return ret;
665 }
666 size_t key_length = key_end - key_data;
667 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
668 key_data, key_length,
669 key_id));
670 mbedtls_platform_zeroize(key_data, key_length);
671 return ret;
672 }
673#endif /* MBEDTLS_RSA_C */
674
675#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
676 case MBEDTLS_PK_ECKEY:
677 case MBEDTLS_PK_ECKEY_DH:
678 case MBEDTLS_PK_ECDSA:
679 {
680 /* We need to check the curve family, otherwise the import could
681 * succeed with nonsensical data.
682 * We don't check the bit-size: it's optional in attributes,
683 * and if it's specified, psa_import_key() will know from the key
684 * data length and will check that the bit-size matches. */
685 psa_key_type_t to_type = psa_get_key_type(attributes);
686#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
687 psa_ecc_family_t from_family = pk->ec_family;
688#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine84a7bfb2024-02-28 14:21:32 +0100689 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100690 size_t from_bits = 0;
691 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
692 &from_bits);
693#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
694 if (to_type != PSA_KEY_TYPE_ECC_KEY_PAIR(from_family)) {
695 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
696 }
697
698#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
699 if (mbedtls_svc_key_id_is_null(pk->priv_id)) {
700 /* We have a public key and want a key pair. */
701 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
702 }
703 return copy_into_psa(pk->priv_id, attributes, key_id);
704#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
705 if (ec->d.n == 0) {
706 /* Private key not set. Assume the input is a public key only.
707 * (The other possibility is that it's an incomplete object
708 * where the group is set but neither the public key nor
709 * the private key. This is not possible through ecp.h
710 * functions, so we don't bother reporting a more suitable
711 * error in that case.) */
712 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
713 }
714 unsigned char key_buffer[PSA_BITS_TO_BYTES(PSA_VENDOR_ECC_MAX_CURVE_BITS)];
Gilles Peskine84a7bfb2024-02-28 14:21:32 +0100715 size_t key_length = 0;
716 int ret = mbedtls_ecp_write_key_ext(ec, &key_length,
717 key_buffer, sizeof(key_buffer));
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100718 if (ret < 0) {
719 return ret;
720 }
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100721 ret = PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100722 key_buffer, key_length,
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100723 key_id));
Gilles Peskine83b8baf2024-02-15 17:26:07 +0100724 mbedtls_platform_zeroize(key_buffer, key_length);
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100725 return ret;
726#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
727 }
728#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
729
730#if defined(MBEDTLS_USE_PSA_CRYPTO)
731 case MBEDTLS_PK_OPAQUE:
732 return copy_into_psa(pk->priv_id, attributes, key_id);
733#endif /* MBEDTLS_USE_PSA_CRYPTO */
734
735 default:
736 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
737 }
738}
739
740static int import_public_into_psa(const mbedtls_pk_context *pk,
741 const psa_key_attributes_t *attributes,
742 mbedtls_svc_key_id_t *key_id)
743{
744 psa_key_type_t psa_type = psa_get_key_type(attributes);
745
746#if defined(MBEDTLS_RSA_C) || \
747 (defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_PK_USE_PSA_EC_DATA)) || \
748 defined(MBEDTLS_USE_PSA_CRYPTO)
749 unsigned char key_buffer[PSA_EXPORT_PUBLIC_KEY_MAX_SIZE];
750#endif
751 unsigned char *key_data = NULL;
752 size_t key_length = 0;
753
754 switch (mbedtls_pk_get_type(pk)) {
755#if defined(MBEDTLS_RSA_C)
756 case MBEDTLS_PK_RSA:
757 {
758 if (psa_type != PSA_KEY_TYPE_RSA_PUBLIC_KEY) {
759 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
760 }
761 unsigned char *const key_end = key_buffer + sizeof(key_buffer);
762 key_data = key_end;
763 int ret = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*pk),
764 key_buffer, &key_data);
765 if (ret < 0) {
766 return ret;
767 }
768 key_length = (size_t) ret;
769 break;
770 }
771#endif /*MBEDTLS_RSA_C */
772
773#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
774 case MBEDTLS_PK_ECKEY:
775 case MBEDTLS_PK_ECKEY_DH:
776 case MBEDTLS_PK_ECDSA:
777 {
778 /* We need to check the curve family, otherwise the import could
779 * succeed with nonsensical data.
780 * We don't check the bit-size: it's optional in attributes,
781 * and if it's specified, psa_import_key() will know from the key
782 * data length and will check that the bit-size matches. */
783#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
784 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(pk->ec_family)) {
785 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
786 }
787 key_data = (unsigned char *) pk->pub_raw;
788 key_length = pk->pub_raw_len;
789#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
790 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
791 size_t from_bits = 0;
792 psa_ecc_family_t from_family = mbedtls_ecc_group_to_psa(ec->grp.id,
793 &from_bits);
Gilles Peskine1d338762024-02-12 14:18:26 +0100794 if (psa_type != PSA_KEY_TYPE_ECC_PUBLIC_KEY(from_family)) {
Gilles Peskinefc3d8662024-02-09 19:26:37 +0100795 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
796 }
797 int ret = mbedtls_ecp_write_public_key(
798 ec, MBEDTLS_ECP_PF_UNCOMPRESSED,
799 &key_length, key_buffer, sizeof(key_buffer));
800 if (ret < 0) {
801 return ret;
802 }
803 key_data = key_buffer;
804#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
805 break;
806 }
807#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
808
809#if defined(MBEDTLS_USE_PSA_CRYPTO)
810 case MBEDTLS_PK_OPAQUE:
811 {
812 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
813 psa_status_t status =
814 psa_get_key_attributes(pk->priv_id, &old_attributes);
815 if (status != PSA_SUCCESS) {
816 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
817 }
818 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
819 psa_reset_key_attributes(&old_attributes);
820 if (psa_type != PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(old_type)) {
821 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
822 }
823 status = psa_export_public_key(pk->priv_id,
824 key_buffer, sizeof(key_buffer),
825 &key_length);
826 if (status != PSA_SUCCESS) {
827 return PSA_PK_TO_MBEDTLS_ERR(status);
828 }
829 key_data = key_buffer;
830 break;
831 }
832#endif /* MBEDTLS_USE_PSA_CRYPTO */
833
834 default:
835 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
836 }
837
838 return PSA_PK_TO_MBEDTLS_ERR(psa_import_key(attributes,
839 key_data, key_length,
840 key_id));
841}
842
843int mbedtls_pk_import_into_psa(const mbedtls_pk_context *pk,
844 const psa_key_attributes_t *attributes,
845 mbedtls_svc_key_id_t *key_id)
846{
847 /* Set the output immediately so that it won't contain garbage even
848 * if we error out before calling psa_import_key(). */
849 *key_id = MBEDTLS_SVC_KEY_ID_INIT;
850
851#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
852 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA_ALT) {
853 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
854 }
855#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
856
857 int want_public = PSA_KEY_TYPE_IS_PUBLIC_KEY(psa_get_key_type(attributes));
858 if (want_public) {
859 return import_public_into_psa(pk, attributes, key_id);
860 } else {
861 return import_pair_into_psa(pk, attributes, key_id);
862 }
863}
Valerio Settic4c1d3a2024-03-12 13:09:23 +0100864
865static int copy_from_psa(mbedtls_svc_key_id_t key_id,
866 mbedtls_pk_context *pk,
867 int public_only)
868{
869 psa_status_t status;
870 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
871 psa_key_type_t key_type;
872 psa_algorithm_t alg_type;
873 size_t key_bits;
874 /* Use a buffer size large enough to contain either a key pair or public key. */
875 unsigned char exp_key[PSA_EXPORT_KEY_PAIR_OR_PUBLIC_MAX_SIZE];
876 size_t exp_key_len;
877 int ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
878
879 if (pk == NULL) {
880 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
881 }
882
883 status = psa_get_key_attributes(key_id, &key_attr);
884 if (status != PSA_SUCCESS) {
885 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
886 }
887
888 if (public_only) {
889 status = psa_export_public_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
890 } else {
891 status = psa_export_key(key_id, exp_key, sizeof(exp_key), &exp_key_len);
892 }
893 if (status != PSA_SUCCESS) {
894 ret = PSA_PK_TO_MBEDTLS_ERR(status);
895 goto exit;
896 }
897
898 key_type = psa_get_key_type(&key_attr);
899 if (public_only) {
900 key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(key_type);
901 }
902 key_bits = psa_get_key_bits(&key_attr);
903 alg_type = psa_get_key_algorithm(&key_attr);
904
905#if defined(MBEDTLS_RSA_C)
906 if ((key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) ||
907 (key_type == PSA_KEY_TYPE_RSA_PUBLIC_KEY)) {
908
909 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_RSA));
910 if (ret != 0) {
911 goto exit;
912 }
913
914 if (key_type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
915 ret = mbedtls_rsa_parse_key(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
916 } else {
917 ret = mbedtls_rsa_parse_pubkey(mbedtls_pk_rsa(*pk), exp_key, exp_key_len);
918 }
919 if (ret != 0) {
920 goto exit;
921 }
922
923 mbedtls_md_type_t md_type = MBEDTLS_MD_NONE;
924 if (PSA_ALG_GET_HASH(alg_type) != PSA_ALG_ANY_HASH) {
925 md_type = mbedtls_md_type_from_psa_alg(alg_type);
926 }
927
928 if (PSA_ALG_IS_RSA_OAEP(alg_type) || PSA_ALG_IS_RSA_PSS(alg_type)) {
929 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V21, md_type);
930 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg_type) ||
931 alg_type == PSA_ALG_RSA_PKCS1V15_CRYPT) {
932 ret = mbedtls_rsa_set_padding(mbedtls_pk_rsa(*pk), MBEDTLS_RSA_PKCS_V15, md_type);
933 }
934 if (ret != 0) {
935 goto exit;
936 }
937 } else
938#endif /* MBEDTLS_RSA_C */
939#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
940 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type) ||
941 PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY(key_type)) {
942 mbedtls_ecp_group_id grp_id;
943
944 ret = mbedtls_pk_setup(pk, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY));
945 if (ret != 0) {
946 goto exit;
947 }
948
949 grp_id = mbedtls_ecc_group_from_psa(PSA_KEY_TYPE_ECC_GET_FAMILY(key_type), key_bits);
950 ret = mbedtls_pk_ecc_set_group(pk, grp_id);
951 if (ret != 0) {
952 goto exit;
953 }
954
955 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(key_type)) {
956 ret = mbedtls_pk_ecc_set_key(pk, exp_key, exp_key_len);
957 if (ret != 0) {
958 goto exit;
959 }
960 ret = mbedtls_pk_ecc_set_pubkey_from_prv(pk, exp_key, exp_key_len,
961 mbedtls_psa_get_random,
962 MBEDTLS_PSA_RANDOM_STATE);
963 } else {
964 ret = mbedtls_pk_ecc_set_pubkey(pk, exp_key, exp_key_len);
965 }
966 if (ret != 0) {
967 goto exit;
968 }
969 } else
970#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
971 {
972 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1132 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001133
Dave Rodgman02a53d72023-09-28 17:17:07 +01001134#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
1136 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1137 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001138#endif
Andres AG72849872017-01-19 11:24:33 +00001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 if (options == NULL) {
1141 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1142 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001143
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001144 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001145
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001146#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +02001148 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001149 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -05001150 int key_len;
1151 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001152 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
1153 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -05001154
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001155 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001156 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1157 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
1159 p = buf + sizeof(buf);
Valerio Setti3a815cb2024-02-14 09:54:18 +01001160 key_len = mbedtls_rsa_write_pubkey(mbedtls_pk_rsa(*ctx), buf, &p);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001161
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 if (key_len < 0) {
1163 return key_len;
1164 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
1167 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
1168 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 status = psa_import_key(&attributes,
1171 buf + sizeof(buf) - key_len, key_len,
1172 &key_id);
1173 if (status != PSA_SUCCESS) {
1174 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001175 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001176 }
1177
Andrzej Kurek4a953cd2022-02-16 06:13:35 -05001178 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
1179 * on a valid signature with trailing data in a buffer, but
1180 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
1181 * so for this reason the passed sig_len is overwritten. Smaller
1182 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001183 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
1184 mbedtls_pk_get_len(ctx) : sig_len;
1185 status = psa_verify_hash(key_id, psa_sig_alg, hash,
1186 hash_len, sig, signature_length);
1187 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
1190 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1191 }
Andrzej Kurek8666df62022-02-15 08:23:02 -05001192
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001194 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -05001196
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001197 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 } else
Tomi Fontanilles81746622023-07-16 13:06:06 +03001199#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001200 {
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if (sig_len < mbedtls_pk_get_len(ctx)) {
1202 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
1203 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
1206 md_alg, (unsigned int) hash_len, hash,
1207 pss_opts->mgf1_hash_id,
1208 pss_opts->expected_salt_len,
1209 sig);
1210 if (ret != 0) {
1211 return ret;
1212 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001213
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 if (sig_len > mbedtls_pk_get_len(ctx)) {
1215 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1216 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -05001217
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001219 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001220#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -05001222#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001223}
1224
1225/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001226 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001227 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001228int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
1229 mbedtls_md_type_t md_alg,
1230 const unsigned char *hash, size_t hash_len,
1231 unsigned char *sig, size_t sig_size, size_t *sig_len,
1232 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1233 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001234{
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +01001236 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
1240 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1241 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001242
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001243#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +02001244 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +02001246 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001247 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +00001248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001249
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
1251 return ret;
1252 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001253
valerio38992cb2023-04-20 09:56:30 +02001254 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 hash, hash_len,
1256 sig, sig_size, sig_len,
1257 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001258
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
1260 mbedtls_pk_restart_free(rs_ctx);
1261 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +02001262
Gilles Peskine449bd832023-01-11 14:50:10 +01001263 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001264 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001265#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001266 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +02001267#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 if (ctx->pk_info->sign_func == NULL) {
1270 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1271 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001272
valerio38992cb2023-04-20 09:56:30 +02001273 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 hash, hash_len,
1275 sig, sig_size, sig_len,
1276 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001277}
1278
1279/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001280 * Make a signature
1281 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001282int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
1283 const unsigned char *hash, size_t hash_len,
1284 unsigned char *sig, size_t sig_size, size_t *sig_len,
1285 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001286{
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
1288 sig, sig_size, sig_len,
1289 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +02001290}
1291
1292/*
Jerry Yufb0621d2022-03-23 11:42:06 +08001293 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +08001294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001295int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
1296 mbedtls_pk_context *ctx,
1297 mbedtls_md_type_t md_alg,
1298 const unsigned char *hash, size_t hash_len,
1299 unsigned char *sig, size_t sig_size, size_t *sig_len,
1300 int (*f_rng)(void *, unsigned char *, size_t),
1301 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +08001302{
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (ctx->pk_info == NULL) {
1304 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1305 }
Jerry Yud69439a2022-02-24 15:52:15 +08001306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (!mbedtls_pk_can_do(ctx, pk_type)) {
1308 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1309 }
Jerry Yud69439a2022-02-24 15:52:15 +08001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
1312 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
1313 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +08001314 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001315
Tomi Fontanilles81746622023-07-16 13:06:06 +03001316#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
1317
1318#if defined(MBEDTLS_USE_PSA_CRYPTO)
1319 const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 if (psa_md_alg == 0) {
1321 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1322 }
Neil Armstrong13e76be2022-04-21 12:08:52 +02001323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Valerio Settib484e372024-02-27 16:21:12 +01001325 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
1326 psa_algorithm_t psa_alg, psa_enrollment_alg, sign_alg;
Neil Armstrong13e76be2022-04-21 12:08:52 +02001327 psa_status_t status;
1328
Valerio Settib484e372024-02-27 16:21:12 +01001329 status = psa_get_key_attributes(ctx->priv_id, &key_attr);
1330 if (status != PSA_SUCCESS) {
1331 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1332 }
1333 psa_alg = psa_get_key_algorithm(&key_attr);
1334 psa_enrollment_alg = psa_get_key_enrollment_algorithm(&key_attr);
1335 psa_reset_key_attributes(&key_attr);
1336
1337 /* Since we're PK type is MBEDTLS_PK_RSASSA_PSS at least one between
1338 * alg and enrollment alg should be of type RSA_PSS. */
1339 if (PSA_ALG_IS_RSA_PSS(psa_alg)) {
1340 sign_alg = psa_alg;
1341 } else if (PSA_ALG_IS_RSA_PSS(psa_enrollment_alg)) {
1342 sign_alg = psa_enrollment_alg;
1343 } else {
1344 /* The opaque key has no RSA PSS algorithm associated. */
1345 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1346 }
1347 /* Adjust the hashing algorithm. */
1348 sign_alg = (sign_alg & ~PSA_ALG_HASH_MASK) | PSA_ALG_GET_HASH(psa_md_alg);
1349
1350 status = psa_sign_hash(ctx->priv_id, sign_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 hash, hash_len,
1352 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001353 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +02001354 }
1355
Gilles Peskine449bd832023-01-11 14:50:10 +01001356 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
1357 ctx->pk_ctx, hash, hash_len,
1358 sig, sig_size, sig_len);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001359#else /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yud69439a2022-02-24 15:52:15 +08001360
Tomi Fontanilles81746622023-07-16 13:06:06 +03001361 if (sig_size < mbedtls_pk_get_len(ctx)) {
1362 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1363 }
1364
1365 if (pk_hashlen_helper(md_alg, &hash_len) != 0) {
1366 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1367 }
1368
1369 mbedtls_rsa_context *const rsa_ctx = mbedtls_pk_rsa(*ctx);
1370
Tomi Fontanilles573dc232023-12-10 14:57:51 +02001371 const int ret = mbedtls_rsa_rsassa_pss_sign_no_mode_check(rsa_ctx, f_rng, p_rng, md_alg,
1372 (unsigned int) hash_len, hash, sig);
Tomi Fontanilles81746622023-07-16 13:06:06 +03001373 if (ret == 0) {
1374 *sig_len = rsa_ctx->len;
1375 }
1376 return ret;
1377
1378#endif /* MBEDTLS_USE_PSA_CRYPTO */
1379
1380#else
1381 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1382#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Jerry Yud69439a2022-02-24 15:52:15 +08001383}
1384
1385/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001386 * Decrypt message
1387 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
1389 const unsigned char *input, size_t ilen,
1390 unsigned char *output, size_t *olen, size_t osize,
1391 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001392{
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if (ctx->pk_info == NULL) {
1394 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1395 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if (ctx->pk_info->decrypt_func == NULL) {
1398 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1399 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001400
valerio38992cb2023-04-20 09:56:30 +02001401 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001403}
1404
1405/*
1406 * Encrypt message
1407 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001408int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
1409 const unsigned char *input, size_t ilen,
1410 unsigned char *output, size_t *olen, size_t osize,
1411 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001412{
Gilles Peskine449bd832023-01-11 14:50:10 +01001413 if (ctx->pk_info == NULL) {
1414 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1415 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001416
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 if (ctx->pk_info->encrypt_func == NULL) {
1418 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1419 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001420
valerio38992cb2023-04-20 09:56:30 +02001421 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +02001423}
1424
1425/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001426 * Check public-private key pair
1427 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001428int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
1429 const mbedtls_pk_context *prv,
1430 int (*f_rng)(void *, unsigned char *, size_t),
1431 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001432{
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 if (pub->pk_info == NULL ||
1434 prv->pk_info == NULL) {
1435 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001436 }
1437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 if (f_rng == NULL) {
1439 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001440 }
1441
Gilles Peskine449bd832023-01-11 14:50:10 +01001442 if (prv->pk_info->check_pair_func == NULL) {
1443 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1444 }
1445
1446 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
1447 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
1448 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1449 }
1450 } else {
valerio8cbef4d2023-06-01 10:59:03 +02001451 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
1452 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001453 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1454 }
1455 }
1456
valerio38992cb2023-04-20 09:56:30 +02001457 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
1458 (mbedtls_pk_context *) prv,
1459 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001460}
1461
1462/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001463 * Get key size in bits
1464 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001465size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001466{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001467 /* For backward compatibility, accept NULL or a context that
1468 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 if (ctx == NULL || ctx->pk_info == NULL) {
1470 return 0;
1471 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001472
valerio38992cb2023-04-20 09:56:30 +02001473 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001474}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001475
1476/*
1477 * Export debug information
1478 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001479int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001480{
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (ctx->pk_info == NULL) {
1482 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1483 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 if (ctx->pk_info->debug_func == NULL) {
1486 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1487 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001488
valerio38992cb2023-04-20 09:56:30 +02001489 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001491}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001492
1493/*
1494 * Access the PK type name
1495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001496const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001497{
Gilles Peskine449bd832023-01-11 14:50:10 +01001498 if (ctx == NULL || ctx->pk_info == NULL) {
1499 return "invalid PK";
1500 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001501
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001503}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001504
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001505/*
1506 * Access the PK type
1507 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001508mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001509{
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (ctx == NULL || ctx->pk_info == NULL) {
1511 return MBEDTLS_PK_NONE;
1512 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001515}
1516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001517#endif /* MBEDTLS_PK_C */