blob: a93ed56eaaf1567ce001ad6d5cd97481fb73d177 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020018 */
19
Gilles Peskinedb09ef62020-06-03 01:43:33 +020020#include "common.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000024#include "pk_wrap.h"
Neil Armstrongca5b55f2022-03-15 15:00:55 +010025#include "pkwrite.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020026
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020027#include "hash_info.h"
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +020028
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050029#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000030#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020034#endif
Valerio Setti0d2980f2023-04-05 18:17:13 +020035#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020040#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020041
Jerry Yub02ee182022-03-16 10:30:41 +080042#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010043#include "mbedtls/psa_util.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050044#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
45#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
46 psa_to_pk_rsa_errors, \
47 psa_pk_status_to_mbedtls)
48#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
49 psa_to_pk_ecdsa_errors, \
50 psa_pk_status_to_mbedtls)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010051#endif
52
Andres AG72849872017-01-19 11:24:33 +000053#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010054#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020055
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020056/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020061 ctx->pk_info = NULL;
62 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020063}
64
65/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020067 */
Gilles Peskine449bd832023-01-11 14:50:10 +010068void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020069{
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020071 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010072 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if (ctx->pk_info != NULL) {
75 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
76 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020077
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020079}
80
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020081#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020082/*
83 * Initialize a restart context
84 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020086{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020087 ctx->pk_info = NULL;
88 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020089}
90
91/*
92 * Free the components of a restart context
93 */
Gilles Peskine449bd832023-01-11 14:50:10 +010094void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020095{
Gilles Peskine449bd832023-01-11 14:50:10 +010096 if (ctx == NULL || ctx->pk_info == NULL ||
97 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020099 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200102
103 ctx->pk_info = NULL;
104 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200105}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200106#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200107
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200108/*
109 * Get pk_info structure from type
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200112{
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114#if defined(MBEDTLS_RSA_C)
115 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200117#endif /* MBEDTLS_RSA_C */
118#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 return &mbedtls_eckeydh_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200123#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti7ca13182023-01-27 13:22:42 +0100124#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200127#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200129 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200131 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200132}
133
134/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200135 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200136 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200138{
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 if (info == NULL || ctx->pk_info != NULL) {
140 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
141 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
144 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;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100161 mbedtls_svc_key_id_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100162 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if (ctx == NULL || ctx->pk_info != NULL) {
165 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
166 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
169 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
170 }
171 type = psa_get_key_type(&attributes);
172 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100175 info = &mbedtls_pk_ecdsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 } else if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100177 info = &mbedtls_pk_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 } else {
179 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
180 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
183 return MBEDTLS_ERR_PK_ALLOC_FAILED;
184 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200185
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200186 ctx->pk_info = info;
187
Andrzej Kurek03e01462022-01-03 12:53:24 +0100188 pk_ctx = (mbedtls_svc_key_id_t *) ctx->pk_ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100189 *pk_ctx = key;
190
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
318 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
319 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
320 psa_algorithm_t key_alg, key_alg2;
321 psa_status_t status;
322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 status = psa_get_key_attributes(*key, &attributes);
324 if (status != PSA_SUCCESS) {
325 return 0;
326 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200327
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 key_alg = psa_get_key_algorithm(&attributes);
329 key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
330 key_usage = psa_get_key_usage_flags(&attributes);
331 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 if ((key_usage & usage) != usage) {
334 return 0;
335 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200336
Neil Armstronga88b1582022-05-11 14:11:25 +0200337 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200338 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200339 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
340 * directly.
341 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
342 * a fixed hash on key_alg/key_alg2.
343 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (alg == key_alg || alg == key_alg2) {
345 return 1;
346 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200347
348 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200349 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
350 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200351 * then alg is compliant with this key alg
352 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
356 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
357 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
358 return 1;
359 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
362 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
363 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
364 return 1;
365 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200366 }
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200369}
370#endif /* MBEDTLS_USE_PSA_CRYPTO */
371
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200372/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200374 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100375static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200376{
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (*hash_len != 0) {
378 return 0;
379 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 *hash_len = mbedtls_hash_info_get_size(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (*hash_len == 0) {
384 return -1;
385 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200388}
389
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200390#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200391/*
392 * Helper to set up a restart context if needed
393 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
395 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200396{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200397 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (ctx == NULL || ctx->pk_info != NULL) {
399 return 0;
400 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200401
402 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
404 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
405 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
408 return MBEDTLS_ERR_PK_ALLOC_FAILED;
409 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200410
411 ctx->pk_info = info;
412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200414}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200415#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200416
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200417/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200418 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200419 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100420int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
421 mbedtls_md_type_t md_alg,
422 const unsigned char *hash, size_t hash_len,
423 const unsigned char *sig, size_t sig_len,
424 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200425{
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100427 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (ctx->pk_info == NULL ||
431 pk_hashlen_helper(md_alg, &hash_len) != 0) {
432 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
433 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200434
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200435#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200436 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200438 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
443 return ret;
444 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 ret = ctx->pk_info->verify_rs_func(ctx->pk_ctx,
447 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
450 mbedtls_pk_restart_free(rs_ctx);
451 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200454 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200455#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200456 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200457#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (ctx->pk_info->verify_func == NULL) {
460 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
461 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 return ctx->pk_info->verify_func(ctx->pk_ctx, md_alg, hash, hash_len,
464 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200465}
466
467/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200468 * Verify a signature
469 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100470int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
471 const unsigned char *hash, size_t hash_len,
472 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200473{
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
475 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200476}
477
478/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200479 * Verify a signature with options
480 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
482 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
483 const unsigned char *hash, size_t hash_len,
484 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200485{
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100487 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (ctx->pk_info == NULL) {
491 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
492 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (!mbedtls_pk_can_do(ctx, type)) {
495 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
496 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500499 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (options != NULL) {
501 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
502 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500505 }
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500508 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
509 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
512 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
513 }
Andres AG72849872017-01-19 11:24:33 +0000514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 if (options == NULL) {
516 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
517 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200518
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500519 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200520
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500521#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200523 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500524 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500525 int key_len;
526 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500527 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
528 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 psa_algorithm_t psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500531 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
532 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
534 p = buf + sizeof(buf);
535 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (key_len < 0) {
538 return key_len;
539 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
542 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
543 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 status = psa_import_key(&attributes,
546 buf + sizeof(buf) - key_len, key_len,
547 &key_id);
548 if (status != PSA_SUCCESS) {
549 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500550 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500551 }
552
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500553 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
554 * on a valid signature with trailing data in a buffer, but
555 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
556 * so for this reason the passed sig_len is overwritten. Smaller
557 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
559 mbedtls_pk_get_len(ctx) : sig_len;
560 status = psa_verify_hash(key_id, psa_sig_alg, hash,
561 hash_len, sig, signature_length);
562 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
565 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
566 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500569 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500571
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500572 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 } else
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500574#endif
575 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (sig_len < mbedtls_pk_get_len(ctx)) {
577 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
578 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
581 md_alg, (unsigned int) hash_len, hash,
582 pss_opts->mgf1_hash_id,
583 pss_opts->expected_salt_len,
584 sig);
585 if (ret != 0) {
586 return ret;
587 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200588
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (sig_len > mbedtls_pk_get_len(ctx)) {
590 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
591 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500592
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200594 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500595#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500597#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200598}
599
600/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200601 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200602 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
604 mbedtls_md_type_t md_alg,
605 const unsigned char *hash, size_t hash_len,
606 unsigned char *sig, size_t sig_size, size_t *sig_len,
607 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
608 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200609{
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100611 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
615 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
616 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200617
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200618#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200619 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200621 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000623 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
626 return ret;
627 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 ret = ctx->pk_info->sign_rs_func(ctx->pk_ctx, md_alg,
630 hash, hash_len,
631 sig, sig_size, sig_len,
632 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
635 mbedtls_pk_restart_free(rs_ctx);
636 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200639 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200640#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200641 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200642#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if (ctx->pk_info->sign_func == NULL) {
645 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
646 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 return ctx->pk_info->sign_func(ctx->pk_ctx, md_alg,
649 hash, hash_len,
650 sig, sig_size, sig_len,
651 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200652}
653
654/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200655 * Make a signature
656 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100657int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
658 const unsigned char *hash, size_t hash_len,
659 unsigned char *sig, size_t sig_size, size_t *sig_len,
660 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200661{
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
663 sig, sig_size, sig_len,
664 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200665}
666
Jerry Yu1d172a32022-03-12 19:12:05 +0800667#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200668/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800669 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800670 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100671int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
672 mbedtls_pk_context *ctx,
673 mbedtls_md_type_t md_alg,
674 const unsigned char *hash, size_t hash_len,
675 unsigned char *sig, size_t sig_size, size_t *sig_len,
676 int (*f_rng)(void *, unsigned char *, size_t),
677 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800678{
Jerry Yufb0621d2022-03-23 11:42:06 +0800679#if defined(MBEDTLS_RSA_C)
680 psa_algorithm_t psa_md_alg;
681#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800682 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (ctx->pk_info == NULL) {
685 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
686 }
Jerry Yud69439a2022-02-24 15:52:15 +0800687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (!mbedtls_pk_can_do(ctx, pk_type)) {
689 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
690 }
Jerry Yud69439a2022-02-24 15:52:15 +0800691
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
693 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
694 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800695 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200696
Jerry Yu89107d12022-03-22 14:20:15 +0800697#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
699 if (psa_md_alg == 0) {
700 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
701 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200704 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
705 psa_status_t status;
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 status = psa_sign_hash(*key, PSA_ALG_RSA_PSS(psa_md_alg),
708 hash, hash_len,
709 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500710 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200711 }
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
714 ctx->pk_ctx, hash, hash_len,
715 sig, sig_size, sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800716#else /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Jerry Yu89107d12022-03-22 14:20:15 +0800718#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800719
720}
Jerry Yu1d172a32022-03-12 19:12:05 +0800721#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800722
723/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200724 * Decrypt message
725 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100726int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
727 const unsigned char *input, size_t ilen,
728 unsigned char *output, size_t *olen, size_t osize,
729 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200730{
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (ctx->pk_info == NULL) {
732 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
733 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (ctx->pk_info->decrypt_func == NULL) {
736 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
737 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 return ctx->pk_info->decrypt_func(ctx->pk_ctx, input, ilen,
740 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200741}
742
743/*
744 * Encrypt message
745 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100746int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
747 const unsigned char *input, size_t ilen,
748 unsigned char *output, size_t *olen, size_t osize,
749 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200750{
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (ctx->pk_info == NULL) {
752 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
753 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 if (ctx->pk_info->encrypt_func == NULL) {
756 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
757 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200758
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 return ctx->pk_info->encrypt_func(ctx->pk_ctx, input, ilen,
760 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200761}
762
763/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100764 * Check public-private key pair
765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
767 const mbedtls_pk_context *prv,
768 int (*f_rng)(void *, unsigned char *, size_t),
769 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100770{
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (pub->pk_info == NULL ||
772 prv->pk_info == NULL) {
773 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100774 }
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (f_rng == NULL) {
777 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100778 }
779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (prv->pk_info->check_pair_func == NULL) {
781 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
782 }
783
784 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
785 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
786 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
787 }
788 } else {
789 if (pub->pk_info != prv->pk_info) {
790 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
791 }
792 }
793
794 return prv->pk_info->check_pair_func(pub->pk_ctx, prv->pk_ctx, f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100795}
796
797/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200798 * Get key size in bits
799 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100800size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200801{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500802 /* For backward compatibility, accept NULL or a context that
803 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 if (ctx == NULL || ctx->pk_info == NULL) {
805 return 0;
806 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200807
Gilles Peskine449bd832023-01-11 14:50:10 +0100808 return ctx->pk_info->get_bitlen(ctx->pk_ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200809}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200810
811/*
812 * Export debug information
813 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100814int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200815{
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (ctx->pk_info == NULL) {
817 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
818 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (ctx->pk_info->debug_func == NULL) {
821 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
822 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 ctx->pk_info->debug_func(ctx->pk_ctx, items);
825 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200826}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200827
828/*
829 * Access the PK type name
830 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100831const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200832{
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 if (ctx == NULL || ctx->pk_info == NULL) {
834 return "invalid PK";
835 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200836
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200838}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200839
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200840/*
841 * Access the PK type
842 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200844{
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 if (ctx == NULL || ctx->pk_info == NULL) {
846 return MBEDTLS_PK_NONE;
847 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200850}
851
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100852#if defined(MBEDTLS_USE_PSA_CRYPTO)
853/*
854 * Load the key to a PSA key slot,
855 * then turn the PK context into a wrapper for that key slot.
856 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200857 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100858 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100859int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
860 mbedtls_svc_key_id_t *key,
861 psa_algorithm_t alg,
862 psa_key_usage_t usage,
863 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100864{
Valerio Setti0d2980f2023-04-05 18:17:13 +0200865#if !defined(MBEDTLS_ECP_LIGHT) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700866 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200867 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200868 ((void) alg);
869 ((void) usage);
870 ((void) alg2);
Valerio Setti0d2980f2023-04-05 18:17:13 +0200871#else /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
872#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100874 const mbedtls_ecp_keypair *ec;
875 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
876 size_t d_len;
877 psa_ecc_family_t curve_id;
878 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
879 psa_key_type_t key_type;
880 size_t bits;
881 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100882 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100883
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100884 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 ec = mbedtls_pk_ec(*pk);
886 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
887 if ((ret = mbedtls_mpi_write_binary(&ec->d, d, d_len)) != 0) {
888 return ret;
889 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
892 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100893
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100894 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 psa_set_key_type(&attributes, key_type);
896 psa_set_key_bits(&attributes, bits);
897 psa_set_key_usage_flags(&attributes, usage);
898 psa_set_key_algorithm(&attributes, alg);
899 if (alg2 != PSA_ALG_NONE) {
900 psa_set_key_enrollment_algorithm(&attributes, alg2);
901 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100902
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100903 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 status = psa_import_key(&attributes, d, d_len, key);
905 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500906 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100908
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100909 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 mbedtls_pk_free(pk);
911 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 return mbedtls_pk_setup_opaque(pk, *key);
914 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200915#endif /* MBEDTLS_ECP_LIGHT */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100916#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100918 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
920 int key_len;
921 psa_status_t status;
922
923 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
925 if (key_len <= 0) {
926 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
927 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100928
929 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
931 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
932 psa_set_key_usage_flags(&attributes, usage);
933 psa_set_key_algorithm(&attributes, alg);
934 if (alg2 != PSA_ALG_NONE) {
935 psa_set_key_enrollment_algorithm(&attributes, alg2);
936 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100937
938 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 status = psa_import_key(&attributes,
940 buf + sizeof(buf) - key_len,
941 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500946 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100948
949 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 mbedtls_pk_free(pk);
951 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 return mbedtls_pk_setup_opaque(pk, *key);
954 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100955#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200956#endif /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100958}
959#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#endif /* MBEDTLS_PK_C */