blob: 74a1ffae35fb9ad823dab0d797850cbef97d4e1b [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"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020045#include "md_psa.h"
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010046#endif
47
Andres AG72849872017-01-19 11:24:33 +000048#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010049#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020050
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020051/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053 */
Gilles Peskine449bd832023-01-11 14:50:10 +010054void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020055{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020056 ctx->pk_info = NULL;
57 ctx->pk_ctx = NULL;
Valerio Settie00954d2023-04-28 15:24:32 +020058#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti4f387ef2023-05-02 14:15:59 +020059 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settie00954d2023-04-28 15:24:32 +020060#endif /* MBEDTLS_PSA_CRYPTO_C */
Valerio Setti722f8f72023-05-17 15:31:21 +020061#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
62 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
63 ctx->pub_raw_len = 0;
64 ctx->ec_family = 0;
65 ctx->ec_bits = 0;
66#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020067}
68
69/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020071 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020073{
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020075 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010076 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020077
Valerio Settie00954d2023-04-28 15:24:32 +020078 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010079 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
80 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020081
Valerio Settib5361262023-05-18 18:51:58 +020082#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
83 /* The ownership of the priv_id key for opaque keys is external of the PK
84 * module. It's the user responsibility to clear it after use. */
85 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
86 psa_destroy_key(ctx->priv_id);
87 }
88#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020091}
92
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020093#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020094/*
95 * Initialize a restart context
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020099 ctx->pk_info = NULL;
100 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200101}
102
103/*
104 * Free the components of a restart context
105 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200107{
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if (ctx == NULL || ctx->pk_info == NULL ||
109 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200110 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200111 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200114
115 ctx->pk_info = NULL;
116 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200117}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200118#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200119
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200120/*
121 * Get pk_info structure from type
122 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200124{
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_RSA_C)
127 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200129#endif /* MBEDTLS_RSA_C */
130#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 return &mbedtls_eckeydh_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200135#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti7ca13182023-01-27 13:22:42 +0100136#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200139#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200141 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200143 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200144}
145
146/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200147 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200148 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100149int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150{
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (info == NULL || ctx->pk_info != NULL) {
152 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
153 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200154
Valerio Settib5361262023-05-18 18:51:58 +0200155 if ((info->ctx_alloc_func != NULL) &&
Valerio Settie00954d2023-04-28 15:24:32 +0200156 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 return MBEDTLS_ERR_PK_ALLOC_FAILED;
158 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200159
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200160 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200163}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200164
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200165#if defined(MBEDTLS_USE_PSA_CRYPTO)
166/*
167 * Initialise a PSA-wrapping context
168 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
170 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200171{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100172 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200173 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100174 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (ctx == NULL || ctx->pk_info != NULL) {
177 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
178 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
181 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
182 }
183 type = psa_get_key_type(&attributes);
184 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100187 info = &mbedtls_pk_ecdsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 } else if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100189 info = &mbedtls_pk_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 } else {
191 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
192 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100193
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200194 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200195 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100196
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200198}
199#endif /* MBEDTLS_USE_PSA_CRYPTO */
200
Valerio Setti483738e2023-05-17 15:37:29 +0200201#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
202int mbedtls_pk_update_public_key_from_keypair(mbedtls_pk_context *pk,
203 mbedtls_ecp_keypair *ecp_keypair)
204{
205 int ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
206
207 if (pk == NULL) {
208 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
209 }
210 /* The raw public key storing mechanism is only supported for EC keys so
211 * we fail silently for other ones. */
212 if ((pk->pk_info->type != MBEDTLS_PK_ECKEY) &&
213 (pk->pk_info->type != MBEDTLS_PK_ECKEY_DH) &&
214 (pk->pk_info->type != MBEDTLS_PK_ECDSA)) {
215 return 0;
216 }
217
218 ret = mbedtls_ecp_point_write_binary(&ecp_keypair->grp, &ecp_keypair->Q,
219 MBEDTLS_ECP_PF_UNCOMPRESSED,
220 &pk->pub_raw_len,
221 pk->pub_raw,
222 MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
223 if (ret != 0) {
224 return ret;
225 }
226
227 pk->ec_family = mbedtls_ecc_group_to_psa(ecp_keypair->grp.id,
228 &pk->ec_bits);
Valerio Settia7cb8452023-05-22 18:39:43 +0200229 if (pk->ec_family == 0) {
230 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
231 }
Valerio Setti483738e2023-05-17 15:37:29 +0200232
233 return 0;
234}
Valerio Settic1541cb2023-05-17 15:49:55 +0200235#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti483738e2023-05-17 15:37:29 +0200236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100238/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200239 * Initialize an RSA-alt context
240 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100241int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
242 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
243 mbedtls_pk_rsa_alt_sign_func sign_func,
244 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200245{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 mbedtls_rsa_alt_context *rsa_alt;
247 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (ctx->pk_info != NULL) {
250 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
251 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
254 return MBEDTLS_ERR_PK_ALLOC_FAILED;
255 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200256
257 ctx->pk_info = info;
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200260
261 rsa_alt->key = key;
262 rsa_alt->decrypt_func = decrypt_func;
263 rsa_alt->sign_func = sign_func;
264 rsa_alt->key_len_func = key_len_func;
265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200267}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200269
270/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200271 * Tell if a PK can do the operations of the given type
272 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100273int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200274{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500275 /* A context with null pk_info is not set up yet and can't do anything.
276 * For backward compatibility, also accept NULL instead of a context
277 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 if (ctx == NULL || ctx->pk_info == NULL) {
279 return 0;
280 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200283}
284
Neil Armstronga88b1582022-05-11 14:11:25 +0200285#if defined(MBEDTLS_USE_PSA_CRYPTO)
286/*
287 * Tell if a PK can do the operations of the given PSA algorithm
288 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
290 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200291{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200292 psa_key_usage_t key_usage;
293
Neil Armstronga88b1582022-05-11 14:11:25 +0200294 /* A context with null pk_info is not set up yet and can't do anything.
295 * For backward compatibility, also accept NULL instead of a context
296 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (ctx == NULL || ctx->pk_info == NULL) {
298 return 0;
299 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200300
301 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
303 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
304 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200305 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 PSA_ALG_IS_ECDH(alg) == 0) {
307 return 0;
308 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200309
Neil Armstrong408f6a62022-05-17 14:23:20 +0200310 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 if (usage == 0 ||
312 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
313 PSA_KEY_USAGE_DECRYPT |
314 PSA_KEY_USAGE_DERIVE)) != 0) {
315 return 0;
316 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200317
Neil Armstronga88b1582022-05-11 14:11:25 +0200318 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if (PSA_ALG_IS_SIGN_HASH(alg) &&
320 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
321 return 0;
322 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200325 mbedtls_pk_type_t type;
326
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200328 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
330 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200331 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200333 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 } else {
335 return 0;
336 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (ctx->pk_info->can_do(type) == 0) {
339 return 0;
340 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200343 case MBEDTLS_PK_ECKEY:
344 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
345 break;
346 case MBEDTLS_PK_RSA:
347 case MBEDTLS_PK_RSASSA_PSS:
348 key_usage = PSA_KEY_USAGE_SIGN_HASH |
349 PSA_KEY_USAGE_SIGN_MESSAGE |
350 PSA_KEY_USAGE_DECRYPT;
351 break;
352 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200353 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200355 }
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200358 }
359
Neil Armstronga88b1582022-05-11 14:11:25 +0200360 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
361 psa_algorithm_t key_alg, key_alg2;
362 psa_status_t status;
363
Valerio Setti4f387ef2023-05-02 14:15:59 +0200364 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (status != PSA_SUCCESS) {
366 return 0;
367 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 key_alg = psa_get_key_algorithm(&attributes);
370 key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
371 key_usage = psa_get_key_usage_flags(&attributes);
372 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if ((key_usage & usage) != usage) {
375 return 0;
376 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200377
Neil Armstronga88b1582022-05-11 14:11:25 +0200378 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200379 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200380 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
381 * directly.
382 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
383 * a fixed hash on key_alg/key_alg2.
384 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (alg == key_alg || alg == key_alg2) {
386 return 1;
387 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200388
389 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200390 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
391 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200392 * then alg is compliant with this key alg
393 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
397 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
398 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
399 return 1;
400 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
403 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
404 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
405 return 1;
406 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200407 }
408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200410}
411#endif /* MBEDTLS_USE_PSA_CRYPTO */
412
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200413/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200415 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100416static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200417{
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (*hash_len != 0) {
419 return 0;
420 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200421
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200422 *hash_len = mbedtls_md_get_size_from_type(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200423
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (*hash_len == 0) {
425 return -1;
426 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200429}
430
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200431#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200432/*
433 * Helper to set up a restart context if needed
434 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100435static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
436 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200437{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200438 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (ctx == NULL || ctx->pk_info != NULL) {
440 return 0;
441 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200442
443 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
445 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
446 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
449 return MBEDTLS_ERR_PK_ALLOC_FAILED;
450 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200451
452 ctx->pk_info = info;
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200455}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200456#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200457
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200458/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200459 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200460 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
462 mbedtls_md_type_t md_alg,
463 const unsigned char *hash, size_t hash_len,
464 const unsigned char *sig, size_t sig_len,
465 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200466{
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100468 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (ctx->pk_info == NULL ||
472 pk_hashlen_helper(md_alg, &hash_len) != 0) {
473 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
474 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200475
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200476#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200477 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200479 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
484 return ret;
485 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200486
valerio38992cb2023-04-20 09:56:30 +0200487 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
491 mbedtls_pk_restart_free(rs_ctx);
492 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200495 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200496#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200497 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200498#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (ctx->pk_info->verify_func == NULL) {
501 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
502 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200503
valerio38992cb2023-04-20 09:56:30 +0200504 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200506}
507
508/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200509 * Verify a signature
510 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100511int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
512 const unsigned char *hash, size_t hash_len,
513 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200514{
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
516 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200517}
518
519/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200520 * Verify a signature with options
521 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
523 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
524 const unsigned char *hash, size_t hash_len,
525 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200526{
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100528 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 if (ctx->pk_info == NULL) {
532 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
533 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if (!mbedtls_pk_can_do(ctx, type)) {
536 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
537 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500540 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 if (options != NULL) {
542 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
543 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500546 }
547
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200548#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
550 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200551
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
553 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
554 }
Andres AG72849872017-01-19 11:24:33 +0000555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (options == NULL) {
557 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
558 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200559
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500560 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200561
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500562#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200564 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500565 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500566 int key_len;
567 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500568 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
569 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500570
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200571 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500572 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
573 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
575 p = buf + sizeof(buf);
576 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (key_len < 0) {
579 return key_len;
580 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
583 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
584 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 status = psa_import_key(&attributes,
587 buf + sizeof(buf) - key_len, key_len,
588 &key_id);
589 if (status != PSA_SUCCESS) {
590 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500591 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500592 }
593
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500594 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
595 * on a valid signature with trailing data in a buffer, but
596 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
597 * so for this reason the passed sig_len is overwritten. Smaller
598 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
600 mbedtls_pk_get_len(ctx) : sig_len;
601 status = psa_verify_hash(key_id, psa_sig_alg, hash,
602 hash_len, sig, signature_length);
603 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
606 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
607 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500610 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500612
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500613 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 } else
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500615#endif
616 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (sig_len < mbedtls_pk_get_len(ctx)) {
618 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
619 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
622 md_alg, (unsigned int) hash_len, hash,
623 pss_opts->mgf1_hash_id,
624 pss_opts->expected_salt_len,
625 sig);
626 if (ret != 0) {
627 return ret;
628 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 if (sig_len > mbedtls_pk_get_len(ctx)) {
631 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
632 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200635 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500636#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500638#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200639}
640
641/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200642 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200643 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100644int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
645 mbedtls_md_type_t md_alg,
646 const unsigned char *hash, size_t hash_len,
647 unsigned char *sig, size_t sig_size, size_t *sig_len,
648 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
649 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200650{
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100652 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
656 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
657 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200658
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200659#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200660 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200662 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
667 return ret;
668 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200669
valerio38992cb2023-04-20 09:56:30 +0200670 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 hash, hash_len,
672 sig, sig_size, sig_len,
673 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
676 mbedtls_pk_restart_free(rs_ctx);
677 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200680 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200681#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200682 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200683#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (ctx->pk_info->sign_func == NULL) {
686 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
687 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200688
valerio38992cb2023-04-20 09:56:30 +0200689 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 hash, hash_len,
691 sig, sig_size, sig_len,
692 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200693}
694
695/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200696 * Make a signature
697 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
699 const unsigned char *hash, size_t hash_len,
700 unsigned char *sig, size_t sig_size, size_t *sig_len,
701 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200702{
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
704 sig, sig_size, sig_len,
705 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200706}
707
Jerry Yu1d172a32022-03-12 19:12:05 +0800708#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200709/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800710 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800711 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
713 mbedtls_pk_context *ctx,
714 mbedtls_md_type_t md_alg,
715 const unsigned char *hash, size_t hash_len,
716 unsigned char *sig, size_t sig_size, size_t *sig_len,
717 int (*f_rng)(void *, unsigned char *, size_t),
718 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800719{
Jerry Yufb0621d2022-03-23 11:42:06 +0800720#if defined(MBEDTLS_RSA_C)
721 psa_algorithm_t psa_md_alg;
722#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800723 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if (ctx->pk_info == NULL) {
726 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
727 }
Jerry Yud69439a2022-02-24 15:52:15 +0800728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 if (!mbedtls_pk_can_do(ctx, pk_type)) {
730 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
731 }
Jerry Yud69439a2022-02-24 15:52:15 +0800732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
734 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
735 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800736 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200737
Jerry Yu89107d12022-03-22 14:20:15 +0800738#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200739 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 if (psa_md_alg == 0) {
741 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
742 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200743
Gilles Peskine449bd832023-01-11 14:50:10 +0100744 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200745 psa_status_t status;
746
Valerio Setti4f387ef2023-05-02 14:15:59 +0200747 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 hash, hash_len,
749 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500750 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200751 }
752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
754 ctx->pk_ctx, hash, hash_len,
755 sig, sig_size, sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800756#else /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Jerry Yu89107d12022-03-22 14:20:15 +0800758#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800759
760}
Jerry Yu1d172a32022-03-12 19:12:05 +0800761#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800762
763/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200764 * Decrypt message
765 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100766int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
767 const unsigned char *input, size_t ilen,
768 unsigned char *output, size_t *olen, size_t osize,
769 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200770{
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 if (ctx->pk_info == NULL) {
772 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
773 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200774
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (ctx->pk_info->decrypt_func == NULL) {
776 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
777 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200778
valerio38992cb2023-04-20 09:56:30 +0200779 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200781}
782
783/*
784 * Encrypt message
785 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100786int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
787 const unsigned char *input, size_t ilen,
788 unsigned char *output, size_t *olen, size_t osize,
789 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200790{
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 if (ctx->pk_info == NULL) {
792 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
793 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200794
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 if (ctx->pk_info->encrypt_func == NULL) {
796 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
797 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200798
valerio38992cb2023-04-20 09:56:30 +0200799 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100800 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200801}
802
803/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100804 * Check public-private key pair
805 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
807 const mbedtls_pk_context *prv,
808 int (*f_rng)(void *, unsigned char *, size_t),
809 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100810{
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (pub->pk_info == NULL ||
812 prv->pk_info == NULL) {
813 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100814 }
815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if (f_rng == NULL) {
817 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100818 }
819
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 if (prv->pk_info->check_pair_func == NULL) {
821 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
822 }
823
824 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
825 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
826 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
827 }
828 } else {
valerio8cbef4d2023-06-01 10:59:03 +0200829 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
830 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
832 }
833 }
834
valerio38992cb2023-04-20 09:56:30 +0200835 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
836 (mbedtls_pk_context *) prv,
837 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100838}
839
840/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200841 * Get key size in bits
842 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100843size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200844{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500845 /* For backward compatibility, accept NULL or a context that
846 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 if (ctx == NULL || ctx->pk_info == NULL) {
848 return 0;
849 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200850
valerio38992cb2023-04-20 09:56:30 +0200851 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200852}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200853
854/*
855 * Export debug information
856 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100857int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200858{
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 if (ctx->pk_info == NULL) {
860 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
861 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (ctx->pk_info->debug_func == NULL) {
864 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
865 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200866
valerio38992cb2023-04-20 09:56:30 +0200867 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200869}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200870
871/*
872 * Access the PK type name
873 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100874const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200875{
Gilles Peskine449bd832023-01-11 14:50:10 +0100876 if (ctx == NULL || ctx->pk_info == NULL) {
877 return "invalid PK";
878 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200879
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200881}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200882
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200883/*
884 * Access the PK type
885 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100886mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200887{
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 if (ctx == NULL || ctx->pk_info == NULL) {
889 return MBEDTLS_PK_NONE;
890 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200893}
894
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100895#if defined(MBEDTLS_USE_PSA_CRYPTO)
896/*
897 * Load the key to a PSA key slot,
898 * then turn the PK context into a wrapper for that key slot.
899 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200900 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100901 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100902int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
903 mbedtls_svc_key_id_t *key,
904 psa_algorithm_t alg,
905 psa_key_usage_t usage,
906 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100907{
Valerio Setti0d2980f2023-04-05 18:17:13 +0200908#if !defined(MBEDTLS_ECP_LIGHT) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700909 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200910 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200911 ((void) alg);
912 ((void) usage);
913 ((void) alg2);
Valerio Setti0d2980f2023-04-05 18:17:13 +0200914#else /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
915#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100916 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100917 size_t d_len;
918 psa_ecc_family_t curve_id;
919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
920 psa_key_type_t key_type;
921 size_t bits;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100922 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100923
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100924 /* export the private key material in the format PSA wants */
Valerio Settiae8c6282023-05-18 18:57:57 +0200925#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1194ffa2023-05-24 13:15:58 +0200926 unsigned char d[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Valerio Settiae8c6282023-05-18 18:57:57 +0200927 status = psa_export_key(pk->priv_id, d, sizeof(d), &d_len);
928 if (status != PSA_SUCCESS) {
929 return psa_pk_status_to_mbedtls(status);
930 }
931
932 curve_id = pk->ec_family;
933 bits = pk->ec_bits;
934#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1194ffa2023-05-24 13:15:58 +0200935 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
Valerio Settiae8c6282023-05-18 18:57:57 +0200936 mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*pk);
937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
938
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200940 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 return ret;
942 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Valerio Settiae8c6282023-05-18 18:57:57 +0200945#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100947
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100948 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 psa_set_key_type(&attributes, key_type);
950 psa_set_key_bits(&attributes, bits);
951 psa_set_key_usage_flags(&attributes, usage);
952 psa_set_key_algorithm(&attributes, alg);
953 if (alg2 != PSA_ALG_NONE) {
954 psa_set_key_enrollment_algorithm(&attributes, alg2);
955 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100956
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100957 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 status = psa_import_key(&attributes, d, d_len, key);
Valerio Setti2d814992023-04-27 10:05:03 +0200959 mbedtls_platform_zeroize(d, sizeof(d));
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500961 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100963
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100964 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 mbedtls_pk_free(pk);
966 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100967
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 return mbedtls_pk_setup_opaque(pk, *key);
969 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200970#endif /* MBEDTLS_ECP_LIGHT */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100971#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100973 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
974 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
975 int key_len;
976 psa_status_t status;
977
978 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
980 if (key_len <= 0) {
981 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
982 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100983
984 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
986 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
987 psa_set_key_usage_flags(&attributes, usage);
988 psa_set_key_algorithm(&attributes, alg);
989 if (alg2 != PSA_ALG_NONE) {
990 psa_set_key_enrollment_algorithm(&attributes, alg2);
991 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100992
993 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 status = psa_import_key(&attributes,
995 buf + sizeof(buf) - key_len,
996 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001001 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001003
1004 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 mbedtls_pk_free(pk);
1006 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001007
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 return mbedtls_pk_setup_opaque(pk, *key);
1009 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001010#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001011#endif /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001013}
1014#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001015#endif /* MBEDTLS_PK_C */