blob: 77bf29183b42e1655796fe425f19e42ffdbbcb89 [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"
Valerio Setti3f00b842023-05-15 12:57:06 +020026#include "pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020027
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050028#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000029#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020033#endif
Valerio Setti81d75122023-06-14 14:49:33 +020034#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020039#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020040
Jerry Yub02ee182022-03-16 10:30:41 +080041#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010042#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020043#include "md_psa.h"
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010044#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;
Valerio Settie00954d2023-04-28 15:24:32 +020056#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti4f387ef2023-05-02 14:15:59 +020057 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settie00954d2023-04-28 15:24:32 +020058#endif /* MBEDTLS_PSA_CRYPTO_C */
Valerio Setti722f8f72023-05-17 15:31:21 +020059#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
60 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
61 ctx->pub_raw_len = 0;
62 ctx->ec_family = 0;
63 ctx->ec_bits = 0;
64#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020065}
66
67/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020069 */
Gilles Peskine449bd832023-01-11 14:50:10 +010070void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020071{
Gilles Peskine449bd832023-01-11 14:50:10 +010072 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020073 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010074 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020075
Valerio Settie00954d2023-04-28 15:24:32 +020076 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010077 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
78 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020079
Valerio Settib5361262023-05-18 18:51:58 +020080#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
81 /* The ownership of the priv_id key for opaque keys is external of the PK
82 * module. It's the user responsibility to clear it after use. */
83 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
84 psa_destroy_key(ctx->priv_id);
85 }
86#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020089}
90
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020091#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020092/*
93 * Initialize a restart context
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020096{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020097 ctx->pk_info = NULL;
98 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020099}
100
101/*
102 * Free the components of a restart context
103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200105{
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (ctx == NULL || ctx->pk_info == NULL ||
107 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200108 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200109 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200112
113 ctx->pk_info = NULL;
114 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200115}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200116#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200117
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200118/*
119 * Get pk_info structure from type
120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200122{
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if defined(MBEDTLS_RSA_C)
125 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200127#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200128#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return &mbedtls_eckeydh_info;
Valerio Setti81d75122023-06-14 14:49:33 +0200133#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti7ca13182023-01-27 13:22:42 +0100134#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200137#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200139 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200141 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200142}
143
144/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200145 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200146 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200148{
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (info == NULL || ctx->pk_info != NULL) {
150 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
151 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200152
Valerio Settib5361262023-05-18 18:51:58 +0200153 if ((info->ctx_alloc_func != NULL) &&
Valerio Settie00954d2023-04-28 15:24:32 +0200154 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return MBEDTLS_ERR_PK_ALLOC_FAILED;
156 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200157
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200158 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200161}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200162
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200163#if defined(MBEDTLS_USE_PSA_CRYPTO)
164/*
165 * Initialise a PSA-wrapping context
166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
168 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200169{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100170 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100172 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (ctx == NULL || ctx->pk_info != NULL) {
175 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
176 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
179 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
180 }
181 type = psa_get_key_type(&attributes);
182 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100183
Valerio Settif7cd4192023-06-30 18:11:29 +0200184#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200186 info = &mbedtls_ecdsa_opaque_info;
Valerio Settif7cd4192023-06-30 18:11:29 +0200187 } else
188#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
189 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200190 info = &mbedtls_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 } else {
192 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
193 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100194
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200195 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200196 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200199}
200#endif /* MBEDTLS_USE_PSA_CRYPTO */
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100203/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200204 * Initialize an RSA-alt context
205 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
207 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
208 mbedtls_pk_rsa_alt_sign_func sign_func,
209 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_rsa_alt_context *rsa_alt;
212 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 if (ctx->pk_info != NULL) {
215 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
216 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
219 return MBEDTLS_ERR_PK_ALLOC_FAILED;
220 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200221
222 ctx->pk_info = info;
223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200225
226 rsa_alt->key = key;
227 rsa_alt->decrypt_func = decrypt_func;
228 rsa_alt->sign_func = sign_func;
229 rsa_alt->key_len_func = key_len_func;
230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200234
235/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200236 * Tell if a PK can do the operations of the given type
237 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100238int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200239{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500240 /* A context with null pk_info is not set up yet and can't do anything.
241 * For backward compatibility, also accept NULL instead of a context
242 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 if (ctx == NULL || ctx->pk_info == NULL) {
244 return 0;
245 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200248}
249
Neil Armstronga88b1582022-05-11 14:11:25 +0200250#if defined(MBEDTLS_USE_PSA_CRYPTO)
251/*
252 * Tell if a PK can do the operations of the given PSA algorithm
253 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100254int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
255 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200256{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200257 psa_key_usage_t key_usage;
258
Neil Armstronga88b1582022-05-11 14:11:25 +0200259 /* A context with null pk_info is not set up yet and can't do anything.
260 * For backward compatibility, also accept NULL instead of a context
261 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (ctx == NULL || ctx->pk_info == NULL) {
263 return 0;
264 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200265
266 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
268 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
269 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200270 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 PSA_ALG_IS_ECDH(alg) == 0) {
272 return 0;
273 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200274
Neil Armstrong408f6a62022-05-17 14:23:20 +0200275 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (usage == 0 ||
277 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
278 PSA_KEY_USAGE_DECRYPT |
279 PSA_KEY_USAGE_DERIVE)) != 0) {
280 return 0;
281 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200282
Neil Armstronga88b1582022-05-11 14:11:25 +0200283 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 if (PSA_ALG_IS_SIGN_HASH(alg) &&
285 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
286 return 0;
287 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200290 mbedtls_pk_type_t type;
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200293 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
295 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200296 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200298 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 } else {
300 return 0;
301 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (ctx->pk_info->can_do(type) == 0) {
304 return 0;
305 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200308 case MBEDTLS_PK_ECKEY:
309 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
310 break;
311 case MBEDTLS_PK_RSA:
312 case MBEDTLS_PK_RSASSA_PSS:
313 key_usage = PSA_KEY_USAGE_SIGN_HASH |
314 PSA_KEY_USAGE_SIGN_MESSAGE |
315 PSA_KEY_USAGE_DECRYPT;
316 break;
317 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200318 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200320 }
321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200323 }
324
Neil Armstronga88b1582022-05-11 14:11:25 +0200325 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
326 psa_algorithm_t key_alg, key_alg2;
327 psa_status_t status;
328
Valerio Setti4f387ef2023-05-02 14:15:59 +0200329 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if (status != PSA_SUCCESS) {
331 return 0;
332 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 key_alg = psa_get_key_algorithm(&attributes);
335 key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
336 key_usage = psa_get_key_usage_flags(&attributes);
337 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if ((key_usage & usage) != usage) {
340 return 0;
341 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200342
Neil Armstronga88b1582022-05-11 14:11:25 +0200343 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200344 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200345 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
346 * directly.
347 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
348 * a fixed hash on key_alg/key_alg2.
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (alg == key_alg || alg == key_alg2) {
351 return 1;
352 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200353
354 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200355 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
356 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200357 * then alg is compliant with this key alg
358 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
362 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
363 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
364 return 1;
365 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
368 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
369 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
370 return 1;
371 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200372 }
373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200375}
376#endif /* MBEDTLS_USE_PSA_CRYPTO */
377
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200378/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200379 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200380 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100381static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200382{
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (*hash_len != 0) {
384 return 0;
385 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200386
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200387 *hash_len = mbedtls_md_get_size_from_type(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (*hash_len == 0) {
390 return -1;
391 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200394}
395
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200396#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200397/*
398 * Helper to set up a restart context if needed
399 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
401 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200402{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200403 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if (ctx == NULL || ctx->pk_info != NULL) {
405 return 0;
406 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200407
408 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
410 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
411 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
414 return MBEDTLS_ERR_PK_ALLOC_FAILED;
415 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200416
417 ctx->pk_info = info;
418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200420}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200421#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200422
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200423/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200424 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200425 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
427 mbedtls_md_type_t md_alg,
428 const unsigned char *hash, size_t hash_len,
429 const unsigned char *sig, size_t sig_len,
430 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200431{
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100433 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if (ctx->pk_info == NULL ||
437 pk_hashlen_helper(md_alg, &hash_len) != 0) {
438 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
439 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200440
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200441#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200442 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200444 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000446 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
449 return ret;
450 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200451
valerio38992cb2023-04-20 09:56:30 +0200452 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
456 mbedtls_pk_restart_free(rs_ctx);
457 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200460 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200461#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200462 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200463#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (ctx->pk_info->verify_func == NULL) {
466 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
467 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200468
valerio38992cb2023-04-20 09:56:30 +0200469 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200471}
472
473/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200474 * Verify a signature
475 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
477 const unsigned char *hash, size_t hash_len,
478 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200479{
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
481 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200482}
483
484/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200485 * Verify a signature with options
486 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
488 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
489 const unsigned char *hash, size_t hash_len,
490 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200491{
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100493 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 if (ctx->pk_info == NULL) {
497 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
498 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (!mbedtls_pk_can_do(ctx, type)) {
501 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
502 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500505 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 if (options != NULL) {
507 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
508 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500511 }
512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
515 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
518 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
519 }
Andres AG72849872017-01-19 11:24:33 +0000520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 if (options == NULL) {
522 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
523 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200524
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500525 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200526
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500527#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200529 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500530 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500531 int key_len;
532 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500533 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
534 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500535
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200536 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500537 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
538 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
540 p = buf + sizeof(buf);
541 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (key_len < 0) {
544 return key_len;
545 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
548 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
549 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 status = psa_import_key(&attributes,
552 buf + sizeof(buf) - key_len, key_len,
553 &key_id);
554 if (status != PSA_SUCCESS) {
555 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500556 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500557 }
558
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500559 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
560 * on a valid signature with trailing data in a buffer, but
561 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
562 * so for this reason the passed sig_len is overwritten. Smaller
563 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
565 mbedtls_pk_get_len(ctx) : sig_len;
566 status = psa_verify_hash(key_id, psa_sig_alg, hash,
567 hash_len, sig, signature_length);
568 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
571 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
572 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500575 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500577
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500578 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 } else
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500580#endif
581 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (sig_len < mbedtls_pk_get_len(ctx)) {
583 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
584 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
587 md_alg, (unsigned int) hash_len, hash,
588 pss_opts->mgf1_hash_id,
589 pss_opts->expected_salt_len,
590 sig);
591 if (ret != 0) {
592 return ret;
593 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (sig_len > mbedtls_pk_get_len(ctx)) {
596 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
597 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200600 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500601#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500603#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200604}
605
606/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200607 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200608 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
610 mbedtls_md_type_t md_alg,
611 const unsigned char *hash, size_t hash_len,
612 unsigned char *sig, size_t sig_size, size_t *sig_len,
613 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
614 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200615{
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100617 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
621 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
622 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200623
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200624#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200625 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200627 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000629 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
632 return ret;
633 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200634
valerio38992cb2023-04-20 09:56:30 +0200635 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 hash, hash_len,
637 sig, sig_size, sig_len,
638 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
641 mbedtls_pk_restart_free(rs_ctx);
642 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200643
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200645 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200646#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200647 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200648#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (ctx->pk_info->sign_func == NULL) {
651 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
652 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200653
valerio38992cb2023-04-20 09:56:30 +0200654 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 hash, hash_len,
656 sig, sig_size, sig_len,
657 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200658}
659
660/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200661 * Make a signature
662 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
664 const unsigned char *hash, size_t hash_len,
665 unsigned char *sig, size_t sig_size, size_t *sig_len,
666 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200667{
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
669 sig, sig_size, sig_len,
670 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200671}
672
Jerry Yu1d172a32022-03-12 19:12:05 +0800673#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200674/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800675 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800676 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100677int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
678 mbedtls_pk_context *ctx,
679 mbedtls_md_type_t md_alg,
680 const unsigned char *hash, size_t hash_len,
681 unsigned char *sig, size_t sig_size, size_t *sig_len,
682 int (*f_rng)(void *, unsigned char *, size_t),
683 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800684{
Jerry Yufb0621d2022-03-23 11:42:06 +0800685#if defined(MBEDTLS_RSA_C)
686 psa_algorithm_t psa_md_alg;
687#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800688 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 if (ctx->pk_info == NULL) {
691 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
692 }
Jerry Yud69439a2022-02-24 15:52:15 +0800693
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 if (!mbedtls_pk_can_do(ctx, pk_type)) {
695 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
696 }
Jerry Yud69439a2022-02-24 15:52:15 +0800697
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
699 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
700 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800701 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200702
Jerry Yu89107d12022-03-22 14:20:15 +0800703#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200704 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (psa_md_alg == 0) {
706 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
707 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200710 psa_status_t status;
711
Valerio Setti4f387ef2023-05-02 14:15:59 +0200712 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 hash, hash_len,
714 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500715 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200716 }
717
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
719 ctx->pk_ctx, hash, hash_len,
720 sig, sig_size, sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800721#else /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Jerry Yu89107d12022-03-22 14:20:15 +0800723#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800724
725}
Jerry Yu1d172a32022-03-12 19:12:05 +0800726#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800727
728/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200729 * Decrypt message
730 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100731int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
732 const unsigned char *input, size_t ilen,
733 unsigned char *output, size_t *olen, size_t osize,
734 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200735{
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 if (ctx->pk_info == NULL) {
737 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
738 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (ctx->pk_info->decrypt_func == NULL) {
741 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
742 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200743
valerio38992cb2023-04-20 09:56:30 +0200744 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200746}
747
748/*
749 * Encrypt message
750 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100751int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
752 const unsigned char *input, size_t ilen,
753 unsigned char *output, size_t *olen, size_t osize,
754 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200755{
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 if (ctx->pk_info == NULL) {
757 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
758 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200759
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 if (ctx->pk_info->encrypt_func == NULL) {
761 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
762 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200763
valerio38992cb2023-04-20 09:56:30 +0200764 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200766}
767
768/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100769 * Check public-private key pair
770 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100771int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
772 const mbedtls_pk_context *prv,
773 int (*f_rng)(void *, unsigned char *, size_t),
774 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100775{
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (pub->pk_info == NULL ||
777 prv->pk_info == NULL) {
778 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100779 }
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (f_rng == NULL) {
782 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100783 }
784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (prv->pk_info->check_pair_func == NULL) {
786 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
787 }
788
789 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
790 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
791 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
792 }
793 } else {
valerio8cbef4d2023-06-01 10:59:03 +0200794 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
795 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
797 }
798 }
799
valerio38992cb2023-04-20 09:56:30 +0200800 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
801 (mbedtls_pk_context *) prv,
802 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100803}
804
805/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200806 * Get key size in bits
807 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200809{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500810 /* For backward compatibility, accept NULL or a context that
811 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100812 if (ctx == NULL || ctx->pk_info == NULL) {
813 return 0;
814 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200815
valerio38992cb2023-04-20 09:56:30 +0200816 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200817}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200818
819/*
820 * Export debug information
821 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100822int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200823{
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if (ctx->pk_info == NULL) {
825 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
826 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200827
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 if (ctx->pk_info->debug_func == NULL) {
829 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
830 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200831
valerio38992cb2023-04-20 09:56:30 +0200832 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200834}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200835
836/*
837 * Access the PK type name
838 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100839const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200840{
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 if (ctx == NULL || ctx->pk_info == NULL) {
842 return "invalid PK";
843 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200844
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200846}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200847
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200848/*
849 * Access the PK type
850 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100851mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200852{
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (ctx == NULL || ctx->pk_info == NULL) {
854 return MBEDTLS_PK_NONE;
855 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200858}
859
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100860#if defined(MBEDTLS_USE_PSA_CRYPTO)
861/*
862 * Load the key to a PSA key slot,
863 * then turn the PK context into a wrapper for that key slot.
864 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200865 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100866 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100867int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
868 mbedtls_svc_key_id_t *key,
869 psa_algorithm_t alg,
870 psa_key_usage_t usage,
871 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100872{
Valerio Setti81d75122023-06-14 14:49:33 +0200873#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700874 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200875 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200876 ((void) alg);
877 ((void) usage);
878 ((void) alg2);
Valerio Setti81d75122023-06-14 14:49:33 +0200879#else /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
880#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100882 size_t d_len;
883 psa_ecc_family_t curve_id;
884 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
885 psa_key_type_t key_type;
886 size_t bits;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100887 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100888
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100889 /* export the private key material in the format PSA wants */
Valerio Settiae8c6282023-05-18 18:57:57 +0200890#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1194ffa2023-05-24 13:15:58 +0200891 unsigned char d[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Valerio Settiae8c6282023-05-18 18:57:57 +0200892 status = psa_export_key(pk->priv_id, d, sizeof(d), &d_len);
893 if (status != PSA_SUCCESS) {
894 return psa_pk_status_to_mbedtls(status);
895 }
896
897 curve_id = pk->ec_family;
898 bits = pk->ec_bits;
899#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1194ffa2023-05-24 13:15:58 +0200900 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
Valerio Settiae8c6282023-05-18 18:57:57 +0200901 mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*pk);
902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200905 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 return ret;
907 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100908
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Valerio Settiae8c6282023-05-18 18:57:57 +0200910#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100912
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100913 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 psa_set_key_type(&attributes, key_type);
915 psa_set_key_bits(&attributes, bits);
916 psa_set_key_usage_flags(&attributes, usage);
917 psa_set_key_algorithm(&attributes, alg);
918 if (alg2 != PSA_ALG_NONE) {
919 psa_set_key_enrollment_algorithm(&attributes, alg2);
920 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100921
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100922 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 status = psa_import_key(&attributes, d, d_len, key);
Valerio Setti2d814992023-04-27 10:05:03 +0200924 mbedtls_platform_zeroize(d, sizeof(d));
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500926 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100927 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100928
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100929 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 mbedtls_pk_free(pk);
931 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 return mbedtls_pk_setup_opaque(pk, *key);
934 } else
Valerio Setti81d75122023-06-14 14:49:33 +0200935#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100936#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100938 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
939 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
940 int key_len;
941 psa_status_t status;
942
943 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
945 if (key_len <= 0) {
946 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
947 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100948
949 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
951 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
952 psa_set_key_usage_flags(&attributes, usage);
953 psa_set_key_algorithm(&attributes, alg);
954 if (alg2 != PSA_ALG_NONE) {
955 psa_set_key_enrollment_algorithm(&attributes, alg2);
956 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100957
958 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 status = psa_import_key(&attributes,
960 buf + sizeof(buf) - key_len,
961 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100962
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500966 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100968
969 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 mbedtls_pk_free(pk);
971 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 return mbedtls_pk_setup_opaque(pk, *key);
974 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100975#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200976#endif /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100978}
979#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980#endif /* MBEDTLS_PK_C */