blob: 5ed485bafb4bbe12889ac303aafd0b1fd38887c0 [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
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020028#include "hash_info.h"
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +020029
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050030#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000031#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020035#endif
Valerio Setti0d2980f2023-04-05 18:17:13 +020036#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020041#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020042
Jerry Yub02ee182022-03-16 10:30:41 +080043#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010044#include "mbedtls/psa_util.h"
45#endif
46
Andres AG72849872017-01-19 11:24:33 +000047#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010048#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020049
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020050/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020052 */
Gilles Peskine449bd832023-01-11 14:50:10 +010053void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020054{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020055 ctx->pk_info = NULL;
56 ctx->pk_ctx = NULL;
Valerio Settie00954d2023-04-28 15:24:32 +020057#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti4f387ef2023-05-02 14:15:59 +020058 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settie00954d2023-04-28 15:24:32 +020059#endif /* MBEDTLS_PSA_CRYPTO_C */
Valerio Setti722f8f72023-05-17 15:31:21 +020060#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
61 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
62 ctx->pub_raw_len = 0;
63 ctx->ec_family = 0;
64 ctx->ec_bits = 0;
65#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066}
67
68/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020074 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010075 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020076
Valerio Settie00954d2023-04-28 15:24:32 +020077 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010078 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
79 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020082}
83
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020084#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020085/*
86 * Initialize a restart context
87 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020089{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020090 ctx->pk_info = NULL;
91 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020092}
93
94/*
95 * Free the components of a restart context
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (ctx == NULL || ctx->pk_info == NULL ||
100 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200101 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200102 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200105
106 ctx->pk_info = NULL;
107 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200108}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200109#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200110
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111/*
112 * Get pk_info structure from type
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200115{
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_RSA_C)
118 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200120#endif /* MBEDTLS_RSA_C */
121#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return &mbedtls_eckeydh_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200126#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti7ca13182023-01-27 13:22:42 +0100127#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200130#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200132 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200134 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135}
136
137/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200138 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200139 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141{
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (info == NULL || ctx->pk_info != NULL) {
143 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
144 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200145
Valerio Settie00954d2023-04-28 15:24:32 +0200146 if ((info->ctx_alloc_func == NULL) ||
147 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return MBEDTLS_ERR_PK_ALLOC_FAILED;
149 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200151 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200154}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200155
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200156#if defined(MBEDTLS_USE_PSA_CRYPTO)
157/*
158 * Initialise a PSA-wrapping context
159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
161 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200162{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100163 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100165 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (ctx == NULL || ctx->pk_info != NULL) {
168 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
169 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
172 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
173 }
174 type = psa_get_key_type(&attributes);
175 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100178 info = &mbedtls_pk_ecdsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 } else if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100180 info = &mbedtls_pk_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 } else {
182 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
183 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100184
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200185 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200186 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200189}
190#endif /* MBEDTLS_USE_PSA_CRYPTO */
191
Valerio Setti483738e2023-05-17 15:37:29 +0200192#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
193int mbedtls_pk_update_public_key_from_keypair(mbedtls_pk_context *pk,
194 mbedtls_ecp_keypair *ecp_keypair)
195{
196 int ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
197
198 if (pk == NULL) {
199 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
200 }
201 /* The raw public key storing mechanism is only supported for EC keys so
202 * we fail silently for other ones. */
203 if ((pk->pk_info->type != MBEDTLS_PK_ECKEY) &&
204 (pk->pk_info->type != MBEDTLS_PK_ECKEY_DH) &&
205 (pk->pk_info->type != MBEDTLS_PK_ECDSA)) {
206 return 0;
207 }
208
209 ret = mbedtls_ecp_point_write_binary(&ecp_keypair->grp, &ecp_keypair->Q,
210 MBEDTLS_ECP_PF_UNCOMPRESSED,
211 &pk->pub_raw_len,
212 pk->pub_raw,
213 MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
214 if (ret != 0) {
215 return ret;
216 }
217
218 pk->ec_family = mbedtls_ecc_group_to_psa(ecp_keypair->grp.id,
219 &pk->ec_bits);
Valerio Settia7cb8452023-05-22 18:39:43 +0200220 if (pk->ec_family == 0) {
221 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
222 }
Valerio Setti483738e2023-05-17 15:37:29 +0200223
224 return 0;
225}
Valerio Settic1541cb2023-05-17 15:49:55 +0200226#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti483738e2023-05-17 15:37:29 +0200227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100229/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200230 * Initialize an RSA-alt context
231 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100232int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
233 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
234 mbedtls_pk_rsa_alt_sign_func sign_func,
235 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200236{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_rsa_alt_context *rsa_alt;
238 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if (ctx->pk_info != NULL) {
241 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
242 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
245 return MBEDTLS_ERR_PK_ALLOC_FAILED;
246 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200247
248 ctx->pk_info = info;
249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200251
252 rsa_alt->key = key;
253 rsa_alt->decrypt_func = decrypt_func;
254 rsa_alt->sign_func = sign_func;
255 rsa_alt->key_len_func = key_len_func;
256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200258}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200260
261/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200262 * Tell if a PK can do the operations of the given type
263 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200265{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500266 /* A context with null pk_info is not set up yet and can't do anything.
267 * For backward compatibility, also accept NULL instead of a context
268 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (ctx == NULL || ctx->pk_info == NULL) {
270 return 0;
271 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200274}
275
Neil Armstronga88b1582022-05-11 14:11:25 +0200276#if defined(MBEDTLS_USE_PSA_CRYPTO)
277/*
278 * Tell if a PK can do the operations of the given PSA algorithm
279 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100280int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
281 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200282{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200283 psa_key_usage_t key_usage;
284
Neil Armstronga88b1582022-05-11 14:11:25 +0200285 /* A context with null pk_info is not set up yet and can't do anything.
286 * For backward compatibility, also accept NULL instead of a context
287 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 if (ctx == NULL || ctx->pk_info == NULL) {
289 return 0;
290 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200291
292 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
294 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
295 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200296 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 PSA_ALG_IS_ECDH(alg) == 0) {
298 return 0;
299 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200300
Neil Armstrong408f6a62022-05-17 14:23:20 +0200301 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (usage == 0 ||
303 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
304 PSA_KEY_USAGE_DECRYPT |
305 PSA_KEY_USAGE_DERIVE)) != 0) {
306 return 0;
307 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200308
Neil Armstronga88b1582022-05-11 14:11:25 +0200309 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (PSA_ALG_IS_SIGN_HASH(alg) &&
311 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
312 return 0;
313 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200316 mbedtls_pk_type_t type;
317
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200319 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
321 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200322 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200324 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 } else {
326 return 0;
327 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (ctx->pk_info->can_do(type) == 0) {
330 return 0;
331 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200334 case MBEDTLS_PK_ECKEY:
335 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
336 break;
337 case MBEDTLS_PK_RSA:
338 case MBEDTLS_PK_RSASSA_PSS:
339 key_usage = PSA_KEY_USAGE_SIGN_HASH |
340 PSA_KEY_USAGE_SIGN_MESSAGE |
341 PSA_KEY_USAGE_DECRYPT;
342 break;
343 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200344 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200346 }
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200349 }
350
Neil Armstronga88b1582022-05-11 14:11:25 +0200351 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
352 psa_algorithm_t key_alg, key_alg2;
353 psa_status_t status;
354
Valerio Setti4f387ef2023-05-02 14:15:59 +0200355 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 if (status != PSA_SUCCESS) {
357 return 0;
358 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 key_alg = psa_get_key_algorithm(&attributes);
361 key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
362 key_usage = psa_get_key_usage_flags(&attributes);
363 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if ((key_usage & usage) != usage) {
366 return 0;
367 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200368
Neil Armstronga88b1582022-05-11 14:11:25 +0200369 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200370 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200371 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
372 * directly.
373 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
374 * a fixed hash on key_alg/key_alg2.
375 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (alg == key_alg || alg == key_alg2) {
377 return 1;
378 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200379
380 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200381 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
382 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200383 * then alg is compliant with this key alg
384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
388 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
389 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
390 return 1;
391 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
394 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
395 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
396 return 1;
397 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200398 }
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200401}
402#endif /* MBEDTLS_USE_PSA_CRYPTO */
403
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200404/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200406 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100407static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200408{
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (*hash_len != 0) {
410 return 0;
411 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200412
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 *hash_len = mbedtls_hash_info_get_size(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if (*hash_len == 0) {
416 return -1;
417 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200420}
421
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200422#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200423/*
424 * Helper to set up a restart context if needed
425 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
427 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200428{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200429 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (ctx == NULL || ctx->pk_info != NULL) {
431 return 0;
432 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200433
434 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
436 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
437 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
440 return MBEDTLS_ERR_PK_ALLOC_FAILED;
441 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200442
443 ctx->pk_info = info;
444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200446}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200447#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200448
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200449/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200450 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200451 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
453 mbedtls_md_type_t md_alg,
454 const unsigned char *hash, size_t hash_len,
455 const unsigned char *sig, size_t sig_len,
456 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200457{
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100459 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (ctx->pk_info == NULL ||
463 pk_hashlen_helper(md_alg, &hash_len) != 0) {
464 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
465 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200466
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200467#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200468 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200470 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
475 return ret;
476 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200477
valerio38992cb2023-04-20 09:56:30 +0200478 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
482 mbedtls_pk_restart_free(rs_ctx);
483 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200486 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200487#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200488 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200489#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (ctx->pk_info->verify_func == NULL) {
492 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
493 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200494
valerio38992cb2023-04-20 09:56:30 +0200495 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200497}
498
499/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200500 * Verify a signature
501 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100502int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
503 const unsigned char *hash, size_t hash_len,
504 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200505{
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
507 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200508}
509
510/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200511 * Verify a signature with options
512 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100513int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
514 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
515 const unsigned char *hash, size_t hash_len,
516 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200517{
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100519 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (ctx->pk_info == NULL) {
523 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
524 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if (!mbedtls_pk_can_do(ctx, type)) {
527 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
528 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500531 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 if (options != NULL) {
533 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
534 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500537 }
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
541 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
544 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
545 }
Andres AG72849872017-01-19 11:24:33 +0000546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 if (options == NULL) {
548 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
549 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200550
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500551 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200552
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500553#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200555 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500556 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500557 int key_len;
558 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500559 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
560 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 psa_algorithm_t psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500563 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
564 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
566 p = buf + sizeof(buf);
567 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 if (key_len < 0) {
570 return key_len;
571 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
574 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
575 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 status = psa_import_key(&attributes,
578 buf + sizeof(buf) - key_len, key_len,
579 &key_id);
580 if (status != PSA_SUCCESS) {
581 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500582 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500583 }
584
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500585 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
586 * on a valid signature with trailing data in a buffer, but
587 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
588 * so for this reason the passed sig_len is overwritten. Smaller
589 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
591 mbedtls_pk_get_len(ctx) : sig_len;
592 status = psa_verify_hash(key_id, psa_sig_alg, hash,
593 hash_len, sig, signature_length);
594 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
597 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
598 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500601 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500603
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500604 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 } else
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500606#endif
607 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (sig_len < mbedtls_pk_get_len(ctx)) {
609 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
610 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200611
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
613 md_alg, (unsigned int) hash_len, hash,
614 pss_opts->mgf1_hash_id,
615 pss_opts->expected_salt_len,
616 sig);
617 if (ret != 0) {
618 return ret;
619 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (sig_len > mbedtls_pk_get_len(ctx)) {
622 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
623 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200626 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500627#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500629#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200630}
631
632/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200633 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200634 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100635int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
636 mbedtls_md_type_t md_alg,
637 const unsigned char *hash, size_t hash_len,
638 unsigned char *sig, size_t sig_size, size_t *sig_len,
639 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
640 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200641{
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100643 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
647 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
648 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200649
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200650#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200651 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200653 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
658 return ret;
659 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200660
valerio38992cb2023-04-20 09:56:30 +0200661 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 hash, hash_len,
663 sig, sig_size, sig_len,
664 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
667 mbedtls_pk_restart_free(rs_ctx);
668 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200671 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200672#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200673 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200674#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (ctx->pk_info->sign_func == NULL) {
677 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
678 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200679
valerio38992cb2023-04-20 09:56:30 +0200680 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 hash, hash_len,
682 sig, sig_size, sig_len,
683 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200684}
685
686/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200687 * Make a signature
688 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100689int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
690 const unsigned char *hash, size_t hash_len,
691 unsigned char *sig, size_t sig_size, size_t *sig_len,
692 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200693{
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
695 sig, sig_size, sig_len,
696 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200697}
698
Jerry Yu1d172a32022-03-12 19:12:05 +0800699#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200700/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800701 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800702 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
704 mbedtls_pk_context *ctx,
705 mbedtls_md_type_t md_alg,
706 const unsigned char *hash, size_t hash_len,
707 unsigned char *sig, size_t sig_size, size_t *sig_len,
708 int (*f_rng)(void *, unsigned char *, size_t),
709 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800710{
Jerry Yufb0621d2022-03-23 11:42:06 +0800711#if defined(MBEDTLS_RSA_C)
712 psa_algorithm_t psa_md_alg;
713#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800714 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (ctx->pk_info == NULL) {
717 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
718 }
Jerry Yud69439a2022-02-24 15:52:15 +0800719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (!mbedtls_pk_can_do(ctx, pk_type)) {
721 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
722 }
Jerry Yud69439a2022-02-24 15:52:15 +0800723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
725 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
726 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800727 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200728
Jerry Yu89107d12022-03-22 14:20:15 +0800729#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
731 if (psa_md_alg == 0) {
732 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
733 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200736 psa_status_t status;
737
Valerio Setti4f387ef2023-05-02 14:15:59 +0200738 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 hash, hash_len,
740 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500741 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200742 }
743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
745 ctx->pk_ctx, hash, hash_len,
746 sig, sig_size, sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800747#else /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Jerry Yu89107d12022-03-22 14:20:15 +0800749#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800750
751}
Jerry Yu1d172a32022-03-12 19:12:05 +0800752#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800753
754/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200755 * Decrypt message
756 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
758 const unsigned char *input, size_t ilen,
759 unsigned char *output, size_t *olen, size_t osize,
760 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200761{
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (ctx->pk_info == NULL) {
763 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
764 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 if (ctx->pk_info->decrypt_func == NULL) {
767 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
768 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200769
valerio38992cb2023-04-20 09:56:30 +0200770 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200772}
773
774/*
775 * Encrypt message
776 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100777int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
778 const unsigned char *input, size_t ilen,
779 unsigned char *output, size_t *olen, size_t osize,
780 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200781{
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (ctx->pk_info == NULL) {
783 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
784 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if (ctx->pk_info->encrypt_func == NULL) {
787 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
788 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200789
valerio38992cb2023-04-20 09:56:30 +0200790 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200792}
793
794/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100795 * Check public-private key pair
796 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
798 const mbedtls_pk_context *prv,
799 int (*f_rng)(void *, unsigned char *, size_t),
800 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100801{
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (pub->pk_info == NULL ||
803 prv->pk_info == NULL) {
804 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100805 }
806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 if (f_rng == NULL) {
808 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100809 }
810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (prv->pk_info->check_pair_func == NULL) {
812 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
813 }
814
815 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
816 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
817 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
818 }
819 } else {
820 if (pub->pk_info != prv->pk_info) {
821 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
822 }
823 }
824
valerio38992cb2023-04-20 09:56:30 +0200825 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
826 (mbedtls_pk_context *) prv,
827 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100828}
829
830/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200831 * Get key size in bits
832 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100833size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200834{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500835 /* For backward compatibility, accept NULL or a context that
836 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100837 if (ctx == NULL || ctx->pk_info == NULL) {
838 return 0;
839 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200840
valerio38992cb2023-04-20 09:56:30 +0200841 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200842}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200843
844/*
845 * Export debug information
846 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200848{
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if (ctx->pk_info == NULL) {
850 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
851 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200852
Gilles Peskine449bd832023-01-11 14:50:10 +0100853 if (ctx->pk_info->debug_func == NULL) {
854 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
855 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200856
valerio38992cb2023-04-20 09:56:30 +0200857 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200859}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200860
861/*
862 * Access the PK type name
863 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100864const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200865{
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if (ctx == NULL || ctx->pk_info == NULL) {
867 return "invalid PK";
868 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200869
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200871}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200872
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200873/*
874 * Access the PK type
875 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100876mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200877{
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 if (ctx == NULL || ctx->pk_info == NULL) {
879 return MBEDTLS_PK_NONE;
880 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200881
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200883}
884
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100885#if defined(MBEDTLS_USE_PSA_CRYPTO)
886/*
887 * Load the key to a PSA key slot,
888 * then turn the PK context into a wrapper for that key slot.
889 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200890 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100891 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100892int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
893 mbedtls_svc_key_id_t *key,
894 psa_algorithm_t alg,
895 psa_key_usage_t usage,
896 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100897{
Valerio Setti0d2980f2023-04-05 18:17:13 +0200898#if !defined(MBEDTLS_ECP_LIGHT) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700899 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200900 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200901 ((void) alg);
902 ((void) usage);
903 ((void) alg2);
Valerio Setti0d2980f2023-04-05 18:17:13 +0200904#else /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
905#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200907 mbedtls_ecp_keypair *ec;
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100908 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
909 size_t d_len;
910 psa_ecc_family_t curve_id;
911 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
912 psa_key_type_t key_type;
913 size_t bits;
914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100915 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100916
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100917 /* export the private key material in the format PSA wants */
Valerio Setti77a75682023-05-15 11:18:46 +0200918 ec = mbedtls_pk_ec_rw(*pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200920 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 return ret;
922 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100923
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
925 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100926
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100927 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 psa_set_key_type(&attributes, key_type);
929 psa_set_key_bits(&attributes, bits);
930 psa_set_key_usage_flags(&attributes, usage);
931 psa_set_key_algorithm(&attributes, alg);
932 if (alg2 != PSA_ALG_NONE) {
933 psa_set_key_enrollment_algorithm(&attributes, alg2);
934 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100935
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100936 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100937 status = psa_import_key(&attributes, d, d_len, key);
Valerio Setti2d814992023-04-27 10:05:03 +0200938 mbedtls_platform_zeroize(d, sizeof(d));
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500940 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100942
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100943 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 mbedtls_pk_free(pk);
945 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 return mbedtls_pk_setup_opaque(pk, *key);
948 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200949#endif /* MBEDTLS_ECP_LIGHT */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100950#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100952 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
953 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
954 int key_len;
955 psa_status_t status;
956
957 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
959 if (key_len <= 0) {
960 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
961 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100962
963 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
965 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
966 psa_set_key_usage_flags(&attributes, usage);
967 psa_set_key_algorithm(&attributes, alg);
968 if (alg2 != PSA_ALG_NONE) {
969 psa_set_key_enrollment_algorithm(&attributes, alg2);
970 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100971
972 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 status = psa_import_key(&attributes,
974 buf + sizeof(buf) - key_len,
975 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100978
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500980 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100982
983 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_pk_free(pk);
985 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 return mbedtls_pk_setup_opaque(pk, *key);
988 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100989#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200990#endif /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100992}
993#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200994#endif /* MBEDTLS_PK_C */