blob: b01694ca0532437317a6c30249015ad836df9bcc [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ECP_C)
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"
44#endif
45
Andres AG72849872017-01-19 11:24:33 +000046#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010047#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020048
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020049/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020051 */
Gilles Peskine449bd832023-01-11 14:50:10 +010052void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020054 ctx->pk_info = NULL;
55 ctx->pk_ctx = NULL;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020056}
57
58/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060 */
Gilles Peskine449bd832023-01-11 14:50:10 +010061void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062{
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020064 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010065 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066
Gilles Peskine449bd832023-01-11 14:50:10 +010067 if (ctx->pk_info != NULL) {
68 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
69 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020072}
73
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020074#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020075/*
76 * Initialize a restart context
77 */
Gilles Peskine449bd832023-01-11 14:50:10 +010078void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020079{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020080 ctx->pk_info = NULL;
81 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020082}
83
84/*
85 * Free the components of a restart context
86 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020088{
Gilles Peskine449bd832023-01-11 14:50:10 +010089 if (ctx == NULL || ctx->pk_info == NULL ||
90 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020091 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020092 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020095
96 ctx->pk_info = NULL;
97 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020099#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200100
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200101/*
102 * Get pk_info structure from type
103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200105{
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#if defined(MBEDTLS_RSA_C)
108 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 return &mbedtls_rsa_info;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200110#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#if defined(MBEDTLS_ECP_C)
112 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return &mbedtls_eckeydh_info;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200116#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_ECDSA_C)
118 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return &mbedtls_ecdsa_info;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200122 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200124 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200125}
126
127/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200128 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200129 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200131{
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (info == NULL || ctx->pk_info != NULL) {
133 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
134 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
137 return MBEDTLS_ERR_PK_ALLOC_FAILED;
138 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200139
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200140 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200143}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200144
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200145#if defined(MBEDTLS_USE_PSA_CRYPTO)
146/*
147 * Initialise a PSA-wrapping context
148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
150 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200151{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100152 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100154 mbedtls_svc_key_id_t *pk_ctx;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100155 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (ctx == NULL || ctx->pk_info != NULL) {
158 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
159 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
162 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
163 }
164 type = psa_get_key_type(&attributes);
165 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100168 info = &mbedtls_pk_ecdsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 } else if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100170 info = &mbedtls_pk_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 } else {
172 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
173 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
176 return MBEDTLS_ERR_PK_ALLOC_FAILED;
177 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200178
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200179 ctx->pk_info = info;
180
Andrzej Kurek03e01462022-01-03 12:53:24 +0100181 pk_ctx = (mbedtls_svc_key_id_t *) ctx->pk_ctx;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100182 *pk_ctx = key;
183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200185}
186#endif /* MBEDTLS_USE_PSA_CRYPTO */
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100189/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200190 * Initialize an RSA-alt context
191 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100192int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
193 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
194 mbedtls_pk_rsa_alt_sign_func sign_func,
195 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_rsa_alt_context *rsa_alt;
198 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (ctx->pk_info != NULL) {
201 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
202 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
205 return MBEDTLS_ERR_PK_ALLOC_FAILED;
206 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200207
208 ctx->pk_info = info;
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200211
212 rsa_alt->key = key;
213 rsa_alt->decrypt_func = decrypt_func;
214 rsa_alt->sign_func = sign_func;
215 rsa_alt->key_len_func = key_len_func;
216
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200218}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200220
221/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200222 * Tell if a PK can do the operations of the given type
223 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100224int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200225{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500226 /* A context with null pk_info is not set up yet and can't do anything.
227 * For backward compatibility, also accept NULL instead of a context
228 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 if (ctx == NULL || ctx->pk_info == NULL) {
230 return 0;
231 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200234}
235
Neil Armstronga88b1582022-05-11 14:11:25 +0200236#if defined(MBEDTLS_USE_PSA_CRYPTO)
237/*
238 * Tell if a PK can do the operations of the given PSA algorithm
239 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
241 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200242{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200243 psa_key_usage_t key_usage;
244
Neil Armstronga88b1582022-05-11 14:11:25 +0200245 /* A context with null pk_info is not set up yet and can't do anything.
246 * For backward compatibility, also accept NULL instead of a context
247 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (ctx == NULL || ctx->pk_info == NULL) {
249 return 0;
250 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200251
252 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
254 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
255 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200256 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 PSA_ALG_IS_ECDH(alg) == 0) {
258 return 0;
259 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200260
Neil Armstrong408f6a62022-05-17 14:23:20 +0200261 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (usage == 0 ||
263 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
264 PSA_KEY_USAGE_DECRYPT |
265 PSA_KEY_USAGE_DERIVE)) != 0) {
266 return 0;
267 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200268
Neil Armstronga88b1582022-05-11 14:11:25 +0200269 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (PSA_ALG_IS_SIGN_HASH(alg) &&
271 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
272 return 0;
273 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200276 mbedtls_pk_type_t type;
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200279 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
281 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200282 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200284 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 } else {
286 return 0;
287 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (ctx->pk_info->can_do(type) == 0) {
290 return 0;
291 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200294 case MBEDTLS_PK_ECKEY:
295 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
296 break;
297 case MBEDTLS_PK_RSA:
298 case MBEDTLS_PK_RSASSA_PSS:
299 key_usage = PSA_KEY_USAGE_SIGN_HASH |
300 PSA_KEY_USAGE_SIGN_MESSAGE |
301 PSA_KEY_USAGE_DECRYPT;
302 break;
303 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200304 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200306 }
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200309 }
310
311 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
312 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
313 psa_algorithm_t key_alg, key_alg2;
314 psa_status_t status;
315
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 status = psa_get_key_attributes(*key, &attributes);
317 if (status != PSA_SUCCESS) {
318 return 0;
319 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 key_alg = psa_get_key_algorithm(&attributes);
322 key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
323 key_usage = psa_get_key_usage_flags(&attributes);
324 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if ((key_usage & usage) != usage) {
327 return 0;
328 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200329
Neil Armstronga88b1582022-05-11 14:11:25 +0200330 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200331 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200332 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
333 * directly.
334 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
335 * a fixed hash on key_alg/key_alg2.
336 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (alg == key_alg || alg == key_alg2) {
338 return 1;
339 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200340
341 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200342 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
343 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200344 * then alg is compliant with this key alg
345 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
349 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
350 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
351 return 1;
352 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
355 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
356 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
357 return 1;
358 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200359 }
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200362}
363#endif /* MBEDTLS_USE_PSA_CRYPTO */
364
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200365/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200367 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100368static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200369{
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (*hash_len != 0) {
371 return 0;
372 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 *hash_len = mbedtls_hash_info_get_size(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (*hash_len == 0) {
377 return -1;
378 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200381}
382
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200383#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200384/*
385 * Helper to set up a restart context if needed
386 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
388 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200389{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200390 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (ctx == NULL || ctx->pk_info != NULL) {
392 return 0;
393 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200394
395 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
397 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
398 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
401 return MBEDTLS_ERR_PK_ALLOC_FAILED;
402 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200403
404 ctx->pk_info = info;
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200407}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200408#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200409
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200410/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200411 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200412 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100413int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
414 mbedtls_md_type_t md_alg,
415 const unsigned char *hash, size_t hash_len,
416 const unsigned char *sig, size_t sig_len,
417 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200418{
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100420 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (ctx->pk_info == NULL ||
424 pk_hashlen_helper(md_alg, &hash_len) != 0) {
425 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
426 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200427
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200428#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200429 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200431 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000433 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
436 return ret;
437 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 ret = ctx->pk_info->verify_rs_func(ctx->pk_ctx,
440 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
443 mbedtls_pk_restart_free(rs_ctx);
444 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200447 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200448#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200449 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200450#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (ctx->pk_info->verify_func == NULL) {
453 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
454 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 return ctx->pk_info->verify_func(ctx->pk_ctx, md_alg, hash, hash_len,
457 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200458}
459
460/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200461 * Verify a signature
462 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100463int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
464 const unsigned char *hash, size_t hash_len,
465 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200466{
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
468 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200469}
470
471/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200472 * Verify a signature with options
473 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100474int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
475 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
476 const unsigned char *hash, size_t hash_len,
477 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200478{
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100480 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if (ctx->pk_info == NULL) {
484 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
485 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (!mbedtls_pk_can_do(ctx, type)) {
488 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
489 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500492 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (options != NULL) {
494 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
495 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500498 }
499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200500#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500501 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
502 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200503
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100504#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
506 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
507 }
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +0100508#endif /* SIZE_MAX > UINT_MAX */
Andres AG72849872017-01-19 11:24:33 +0000509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 if (options == NULL) {
511 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
512 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200513
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500514 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200515
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500516#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200518 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500519 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500520 int key_len;
521 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500522 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
523 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500524
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 psa_algorithm_t psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500526 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
527 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
529 p = buf + sizeof(buf);
530 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (key_len < 0) {
533 return key_len;
534 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
537 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
538 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 status = psa_import_key(&attributes,
541 buf + sizeof(buf) - key_len, key_len,
542 &key_id);
543 if (status != PSA_SUCCESS) {
544 psa_destroy_key(key_id);
545 return mbedtls_pk_error_from_psa(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500546 }
547
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500548 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
549 * on a valid signature with trailing data in a buffer, but
550 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
551 * so for this reason the passed sig_len is overwritten. Smaller
552 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
554 mbedtls_pk_get_len(ctx) : sig_len;
555 status = psa_verify_hash(key_id, psa_sig_alg, hash,
556 hash_len, sig, signature_length);
557 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
560 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
561 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500564 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500566
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return mbedtls_pk_error_from_psa_rsa(status);
568 } else
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500569#endif
570 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (sig_len < mbedtls_pk_get_len(ctx)) {
572 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
573 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200574
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
576 md_alg, (unsigned int) hash_len, hash,
577 pss_opts->mgf1_hash_id,
578 pss_opts->expected_salt_len,
579 sig);
580 if (ret != 0) {
581 return ret;
582 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (sig_len > mbedtls_pk_get_len(ctx)) {
585 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
586 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500587
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200589 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500590#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500592#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200593}
594
595/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200596 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200597 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100598int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
599 mbedtls_md_type_t md_alg,
600 const unsigned char *hash, size_t hash_len,
601 unsigned char *sig, size_t sig_size, size_t *sig_len,
602 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
603 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200604{
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100606 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
610 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
611 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200612
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200613#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200614 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200616 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
621 return ret;
622 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 ret = ctx->pk_info->sign_rs_func(ctx->pk_ctx, md_alg,
625 hash, hash_len,
626 sig, sig_size, sig_len,
627 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
630 mbedtls_pk_restart_free(rs_ctx);
631 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200634 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200635#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200636 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200637#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 if (ctx->pk_info->sign_func == NULL) {
640 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
641 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return ctx->pk_info->sign_func(ctx->pk_ctx, md_alg,
644 hash, hash_len,
645 sig, sig_size, sig_len,
646 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200647}
648
649/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200650 * Make a signature
651 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
653 const unsigned char *hash, size_t hash_len,
654 unsigned char *sig, size_t sig_size, size_t *sig_len,
655 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200656{
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
658 sig, sig_size, sig_len,
659 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200660}
661
Jerry Yu1d172a32022-03-12 19:12:05 +0800662#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200663/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800664 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800665 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
667 mbedtls_pk_context *ctx,
668 mbedtls_md_type_t md_alg,
669 const unsigned char *hash, size_t hash_len,
670 unsigned char *sig, size_t sig_size, size_t *sig_len,
671 int (*f_rng)(void *, unsigned char *, size_t),
672 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800673{
Jerry Yufb0621d2022-03-23 11:42:06 +0800674#if defined(MBEDTLS_RSA_C)
675 psa_algorithm_t psa_md_alg;
676#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800677 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (ctx->pk_info == NULL) {
680 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
681 }
Jerry Yud69439a2022-02-24 15:52:15 +0800682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (!mbedtls_pk_can_do(ctx, pk_type)) {
684 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
685 }
Jerry Yud69439a2022-02-24 15:52:15 +0800686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
688 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
689 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800690 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200691
Jerry Yu89107d12022-03-22 14:20:15 +0800692#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
694 if (psa_md_alg == 0) {
695 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
696 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200699 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
700 psa_status_t status;
701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 status = psa_sign_hash(*key, PSA_ALG_RSA_PSS(psa_md_alg),
703 hash, hash_len,
704 sig, sig_size, sig_len);
705 return mbedtls_pk_error_from_psa_rsa(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200706 }
707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
709 ctx->pk_ctx, hash, hash_len,
710 sig, sig_size, sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800711#else /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Jerry Yu89107d12022-03-22 14:20:15 +0800713#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800714
715}
Jerry Yu1d172a32022-03-12 19:12:05 +0800716#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800717
718/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200719 * Decrypt message
720 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
722 const unsigned char *input, size_t ilen,
723 unsigned char *output, size_t *olen, size_t osize,
724 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200725{
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if (ctx->pk_info == NULL) {
727 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
728 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if (ctx->pk_info->decrypt_func == NULL) {
731 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
732 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 return ctx->pk_info->decrypt_func(ctx->pk_ctx, input, ilen,
735 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200736}
737
738/*
739 * Encrypt message
740 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100741int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
742 const unsigned char *input, size_t ilen,
743 unsigned char *output, size_t *olen, size_t osize,
744 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200745{
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (ctx->pk_info == NULL) {
747 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
748 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 if (ctx->pk_info->encrypt_func == NULL) {
751 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
752 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200753
Gilles Peskine449bd832023-01-11 14:50:10 +0100754 return ctx->pk_info->encrypt_func(ctx->pk_ctx, input, ilen,
755 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200756}
757
758/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100759 * Check public-private key pair
760 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100761int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
762 const mbedtls_pk_context *prv,
763 int (*f_rng)(void *, unsigned char *, size_t),
764 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100765{
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (pub->pk_info == NULL ||
767 prv->pk_info == NULL) {
768 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100769 }
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (f_rng == NULL) {
772 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100773 }
774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (prv->pk_info->check_pair_func == NULL) {
776 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
777 }
778
779 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
780 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
781 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
782 }
783 } else {
784 if (pub->pk_info != prv->pk_info) {
785 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
786 }
787 }
788
789 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 +0100790}
791
792/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200793 * Get key size in bits
794 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100795size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200796{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500797 /* For backward compatibility, accept NULL or a context that
798 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 if (ctx == NULL || ctx->pk_info == NULL) {
800 return 0;
801 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 return ctx->pk_info->get_bitlen(ctx->pk_ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200804}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200805
806/*
807 * Export debug information
808 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200810{
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (ctx->pk_info == NULL) {
812 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
813 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (ctx->pk_info->debug_func == NULL) {
816 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
817 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 ctx->pk_info->debug_func(ctx->pk_ctx, items);
820 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200821}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200822
823/*
824 * Access the PK type name
825 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100826const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200827{
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (ctx == NULL || ctx->pk_info == NULL) {
829 return "invalid PK";
830 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200833}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200834
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200835/*
836 * Access the PK type
837 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100838mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200839{
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 if (ctx == NULL || ctx->pk_info == NULL) {
841 return MBEDTLS_PK_NONE;
842 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200845}
846
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100847#if defined(MBEDTLS_USE_PSA_CRYPTO)
848/*
849 * Load the key to a PSA key slot,
850 * then turn the PK context into a wrapper for that key slot.
851 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200852 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100853 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
855 mbedtls_svc_key_id_t *key,
856 psa_algorithm_t alg,
857 psa_key_usage_t usage,
858 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100859{
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100860#if !defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700861 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200862 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200863 ((void) alg);
864 ((void) usage);
865 ((void) alg2);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100866#else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100867#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100869 const mbedtls_ecp_keypair *ec;
870 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
871 size_t d_len;
872 psa_ecc_family_t curve_id;
873 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
874 psa_key_type_t key_type;
875 size_t bits;
876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100877 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100878
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100879 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 ec = mbedtls_pk_ec(*pk);
881 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
882 if ((ret = mbedtls_mpi_write_binary(&ec->d, d, d_len)) != 0) {
883 return ret;
884 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
887 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100888
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100889 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 psa_set_key_type(&attributes, key_type);
891 psa_set_key_bits(&attributes, bits);
892 psa_set_key_usage_flags(&attributes, usage);
893 psa_set_key_algorithm(&attributes, alg);
894 if (alg2 != PSA_ALG_NONE) {
895 psa_set_key_enrollment_algorithm(&attributes, alg2);
896 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100897
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100898 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899 status = psa_import_key(&attributes, d, d_len, key);
900 if (status != PSA_SUCCESS) {
901 return mbedtls_pk_error_from_psa(status);
902 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100903
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100904 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 mbedtls_pk_free(pk);
906 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100907
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 return mbedtls_pk_setup_opaque(pk, *key);
909 } else
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100910#endif /* MBEDTLS_ECP_C */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100911#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100913 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
915 int key_len;
916 psa_status_t status;
917
918 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
920 if (key_len <= 0) {
921 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
922 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100923
924 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
926 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
927 psa_set_key_usage_flags(&attributes, usage);
928 psa_set_key_algorithm(&attributes, alg);
929 if (alg2 != PSA_ALG_NONE) {
930 psa_set_key_enrollment_algorithm(&attributes, alg2);
931 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100932
933 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 status = psa_import_key(&attributes,
935 buf + sizeof(buf) - key_len,
936 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100939
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 if (status != PSA_SUCCESS) {
941 return mbedtls_pk_error_from_psa(status);
942 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100943
944 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 mbedtls_pk_free(pk);
946 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 return mbedtls_pk_setup_opaque(pk, *key);
949 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100950#endif /* MBEDTLS_RSA_C */
951#endif /* !MBEDTLS_ECP_C && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100953}
954#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200955#endif /* MBEDTLS_PK_C */