blob: d731d5b2d415235eca784be7aa44565537ad9a97 [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
Valerio Settib5361262023-05-18 18:51:58 +020081#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
82 /* The ownership of the priv_id key for opaque keys is external of the PK
83 * module. It's the user responsibility to clear it after use. */
84 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
85 psa_destroy_key(ctx->priv_id);
86 }
87#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020090}
91
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020092#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020093/*
94 * Initialize a restart context
95 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020097{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020098 ctx->pk_info = NULL;
99 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200100}
101
102/*
103 * Free the components of a restart context
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ctx == NULL || ctx->pk_info == NULL ||
108 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200109 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200110 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200113
114 ctx->pk_info = NULL;
115 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200116}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200117#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200118
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200119/*
120 * Get pk_info structure from type
121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200123{
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_RSA_C)
126 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200128#endif /* MBEDTLS_RSA_C */
129#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return &mbedtls_eckeydh_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200134#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti7ca13182023-01-27 13:22:42 +0100135#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200136 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200138#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200140 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200142 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200143}
144
145/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200146 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200147 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200149{
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (info == NULL || ctx->pk_info != NULL) {
151 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
152 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200153
Valerio Settib5361262023-05-18 18:51:58 +0200154 if ((info->ctx_alloc_func != NULL) &&
Valerio Settie00954d2023-04-28 15:24:32 +0200155 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return MBEDTLS_ERR_PK_ALLOC_FAILED;
157 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200158
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200159 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200162}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200163
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200164#if defined(MBEDTLS_USE_PSA_CRYPTO)
165/*
166 * Initialise a PSA-wrapping context
167 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
169 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200170{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100171 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200172 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100173 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (ctx == NULL || ctx->pk_info != NULL) {
176 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
177 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
180 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
181 }
182 type = psa_get_key_type(&attributes);
183 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100186 info = &mbedtls_pk_ecdsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 } else if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100188 info = &mbedtls_pk_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 } else {
190 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
191 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100192
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200193 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200194 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200197}
198#endif /* MBEDTLS_USE_PSA_CRYPTO */
199
Valerio Setti483738e2023-05-17 15:37:29 +0200200#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
201int mbedtls_pk_update_public_key_from_keypair(mbedtls_pk_context *pk,
202 mbedtls_ecp_keypair *ecp_keypair)
203{
204 int ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
205
206 if (pk == NULL) {
207 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
208 }
209 /* The raw public key storing mechanism is only supported for EC keys so
210 * we fail silently for other ones. */
211 if ((pk->pk_info->type != MBEDTLS_PK_ECKEY) &&
212 (pk->pk_info->type != MBEDTLS_PK_ECKEY_DH) &&
213 (pk->pk_info->type != MBEDTLS_PK_ECDSA)) {
214 return 0;
215 }
216
217 ret = mbedtls_ecp_point_write_binary(&ecp_keypair->grp, &ecp_keypair->Q,
218 MBEDTLS_ECP_PF_UNCOMPRESSED,
219 &pk->pub_raw_len,
220 pk->pub_raw,
221 MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
222 if (ret != 0) {
223 return ret;
224 }
225
226 pk->ec_family = mbedtls_ecc_group_to_psa(ecp_keypair->grp.id,
227 &pk->ec_bits);
Valerio Settia7cb8452023-05-22 18:39:43 +0200228 if (pk->ec_family == 0) {
229 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
230 }
Valerio Setti483738e2023-05-17 15:37:29 +0200231
232 return 0;
233}
Valerio Settic1541cb2023-05-17 15:49:55 +0200234#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti483738e2023-05-17 15:37:29 +0200235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100237/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200238 * Initialize an RSA-alt context
239 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100240int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
241 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
242 mbedtls_pk_rsa_alt_sign_func sign_func,
243 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_rsa_alt_context *rsa_alt;
246 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (ctx->pk_info != NULL) {
249 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
250 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
253 return MBEDTLS_ERR_PK_ALLOC_FAILED;
254 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200255
256 ctx->pk_info = info;
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200259
260 rsa_alt->key = key;
261 rsa_alt->decrypt_func = decrypt_func;
262 rsa_alt->sign_func = sign_func;
263 rsa_alt->key_len_func = key_len_func;
264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200268
269/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200270 * Tell if a PK can do the operations of the given type
271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200273{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 /* A context with null pk_info is not set up yet and can't do anything.
275 * For backward compatibility, also accept NULL instead of a context
276 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 if (ctx == NULL || ctx->pk_info == NULL) {
278 return 0;
279 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200282}
283
Neil Armstronga88b1582022-05-11 14:11:25 +0200284#if defined(MBEDTLS_USE_PSA_CRYPTO)
285/*
286 * Tell if a PK can do the operations of the given PSA algorithm
287 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
289 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200290{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200291 psa_key_usage_t key_usage;
292
Neil Armstronga88b1582022-05-11 14:11:25 +0200293 /* A context with null pk_info is not set up yet and can't do anything.
294 * For backward compatibility, also accept NULL instead of a context
295 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 if (ctx == NULL || ctx->pk_info == NULL) {
297 return 0;
298 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200299
300 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
302 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
303 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200304 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 PSA_ALG_IS_ECDH(alg) == 0) {
306 return 0;
307 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200308
Neil Armstrong408f6a62022-05-17 14:23:20 +0200309 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (usage == 0 ||
311 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
312 PSA_KEY_USAGE_DECRYPT |
313 PSA_KEY_USAGE_DERIVE)) != 0) {
314 return 0;
315 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200316
Neil Armstronga88b1582022-05-11 14:11:25 +0200317 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 if (PSA_ALG_IS_SIGN_HASH(alg) &&
319 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
320 return 0;
321 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200324 mbedtls_pk_type_t type;
325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200327 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
329 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200330 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200332 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 } else {
334 return 0;
335 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 if (ctx->pk_info->can_do(type) == 0) {
338 return 0;
339 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200342 case MBEDTLS_PK_ECKEY:
343 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
344 break;
345 case MBEDTLS_PK_RSA:
346 case MBEDTLS_PK_RSASSA_PSS:
347 key_usage = PSA_KEY_USAGE_SIGN_HASH |
348 PSA_KEY_USAGE_SIGN_MESSAGE |
349 PSA_KEY_USAGE_DECRYPT;
350 break;
351 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200352 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200354 }
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200357 }
358
Neil Armstronga88b1582022-05-11 14:11:25 +0200359 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
360 psa_algorithm_t key_alg, key_alg2;
361 psa_status_t status;
362
Valerio Setti4f387ef2023-05-02 14:15:59 +0200363 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (status != PSA_SUCCESS) {
365 return 0;
366 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 key_alg = psa_get_key_algorithm(&attributes);
369 key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
370 key_usage = psa_get_key_usage_flags(&attributes);
371 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if ((key_usage & usage) != usage) {
374 return 0;
375 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200376
Neil Armstronga88b1582022-05-11 14:11:25 +0200377 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200378 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200379 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
380 * directly.
381 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
382 * a fixed hash on key_alg/key_alg2.
383 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (alg == key_alg || alg == key_alg2) {
385 return 1;
386 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200387
388 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200389 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
390 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200391 * then alg is compliant with this key alg
392 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
396 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
397 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
398 return 1;
399 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
402 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
403 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
404 return 1;
405 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200406 }
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200409}
410#endif /* MBEDTLS_USE_PSA_CRYPTO */
411
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200412/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200414 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200416{
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 if (*hash_len != 0) {
418 return 0;
419 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200420
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200421 *hash_len = mbedtls_md_get_size_from_type(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if (*hash_len == 0) {
424 return -1;
425 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200428}
429
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200430#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200431/*
432 * Helper to set up a restart context if needed
433 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
435 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200436{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200437 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 if (ctx == NULL || ctx->pk_info != NULL) {
439 return 0;
440 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200441
442 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
444 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
445 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
448 return MBEDTLS_ERR_PK_ALLOC_FAILED;
449 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200450
451 ctx->pk_info = info;
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200454}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200455#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200456
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200457/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200458 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200459 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100460int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
461 mbedtls_md_type_t md_alg,
462 const unsigned char *hash, size_t hash_len,
463 const unsigned char *sig, size_t sig_len,
464 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200465{
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100467 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (ctx->pk_info == NULL ||
471 pk_hashlen_helper(md_alg, &hash_len) != 0) {
472 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
473 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200474
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200475#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200476 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200478 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
483 return ret;
484 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200485
valerio38992cb2023-04-20 09:56:30 +0200486 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
490 mbedtls_pk_restart_free(rs_ctx);
491 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200494 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200495#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200496 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200497#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (ctx->pk_info->verify_func == NULL) {
500 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
501 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200502
valerio38992cb2023-04-20 09:56:30 +0200503 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200505}
506
507/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200508 * Verify a signature
509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
511 const unsigned char *hash, size_t hash_len,
512 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200513{
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
515 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200516}
517
518/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200519 * Verify a signature with options
520 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100521int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
522 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
523 const unsigned char *hash, size_t hash_len,
524 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200525{
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100527 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 if (ctx->pk_info == NULL) {
531 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
532 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (!mbedtls_pk_can_do(ctx, type)) {
535 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
536 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500539 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 if (options != NULL) {
541 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
542 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500545 }
546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500548 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
549 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
552 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
553 }
Andres AG72849872017-01-19 11:24:33 +0000554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (options == NULL) {
556 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
557 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200558
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500559 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200560
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500561#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200563 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500564 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500565 int key_len;
566 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500567 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
568 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 psa_algorithm_t psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500571 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
572 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
574 p = buf + sizeof(buf);
575 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (key_len < 0) {
578 return key_len;
579 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
582 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
583 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 status = psa_import_key(&attributes,
586 buf + sizeof(buf) - key_len, key_len,
587 &key_id);
588 if (status != PSA_SUCCESS) {
589 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500590 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500591 }
592
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500593 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
594 * on a valid signature with trailing data in a buffer, but
595 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
596 * so for this reason the passed sig_len is overwritten. Smaller
597 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
599 mbedtls_pk_get_len(ctx) : sig_len;
600 status = psa_verify_hash(key_id, psa_sig_alg, hash,
601 hash_len, sig, signature_length);
602 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
605 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
606 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500609 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500611
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500612 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 } else
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500614#endif
615 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (sig_len < mbedtls_pk_get_len(ctx)) {
617 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
618 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
621 md_alg, (unsigned int) hash_len, hash,
622 pss_opts->mgf1_hash_id,
623 pss_opts->expected_salt_len,
624 sig);
625 if (ret != 0) {
626 return ret;
627 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if (sig_len > mbedtls_pk_get_len(ctx)) {
630 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
631 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200634 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500635#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500637#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200638}
639
640/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200641 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200642 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100643int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
644 mbedtls_md_type_t md_alg,
645 const unsigned char *hash, size_t hash_len,
646 unsigned char *sig, size_t sig_size, size_t *sig_len,
647 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
648 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200649{
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100651 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
655 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
656 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200657
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200658#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200659 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200661 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000663 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200664
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
666 return ret;
667 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200668
valerio38992cb2023-04-20 09:56:30 +0200669 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 hash, hash_len,
671 sig, sig_size, sig_len,
672 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
675 mbedtls_pk_restart_free(rs_ctx);
676 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200679 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200680#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200681 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200682#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 if (ctx->pk_info->sign_func == NULL) {
685 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
686 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200687
valerio38992cb2023-04-20 09:56:30 +0200688 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 hash, hash_len,
690 sig, sig_size, sig_len,
691 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200692}
693
694/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200695 * Make a signature
696 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100697int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
698 const unsigned char *hash, size_t hash_len,
699 unsigned char *sig, size_t sig_size, size_t *sig_len,
700 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200701{
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
703 sig, sig_size, sig_len,
704 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200705}
706
Jerry Yu1d172a32022-03-12 19:12:05 +0800707#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200708/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800709 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800710 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100711int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
712 mbedtls_pk_context *ctx,
713 mbedtls_md_type_t md_alg,
714 const unsigned char *hash, size_t hash_len,
715 unsigned char *sig, size_t sig_size, size_t *sig_len,
716 int (*f_rng)(void *, unsigned char *, size_t),
717 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800718{
Jerry Yufb0621d2022-03-23 11:42:06 +0800719#if defined(MBEDTLS_RSA_C)
720 psa_algorithm_t psa_md_alg;
721#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800722 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800723
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if (ctx->pk_info == NULL) {
725 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
726 }
Jerry Yud69439a2022-02-24 15:52:15 +0800727
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (!mbedtls_pk_can_do(ctx, pk_type)) {
729 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
730 }
Jerry Yud69439a2022-02-24 15:52:15 +0800731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
733 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
734 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800735 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200736
Jerry Yu89107d12022-03-22 14:20:15 +0800737#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
739 if (psa_md_alg == 0) {
740 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
741 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200742
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200744 psa_status_t status;
745
Valerio Setti4f387ef2023-05-02 14:15:59 +0200746 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 hash, hash_len,
748 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500749 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200750 }
751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
753 ctx->pk_ctx, hash, hash_len,
754 sig, sig_size, sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800755#else /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Jerry Yu89107d12022-03-22 14:20:15 +0800757#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800758
759}
Jerry Yu1d172a32022-03-12 19:12:05 +0800760#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800761
762/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200763 * Decrypt message
764 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100765int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
766 const unsigned char *input, size_t ilen,
767 unsigned char *output, size_t *olen, size_t osize,
768 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200769{
Gilles Peskine449bd832023-01-11 14:50:10 +0100770 if (ctx->pk_info == NULL) {
771 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
772 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 if (ctx->pk_info->decrypt_func == NULL) {
775 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
776 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200777
valerio38992cb2023-04-20 09:56:30 +0200778 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200780}
781
782/*
783 * Encrypt message
784 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100785int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
786 const unsigned char *input, size_t ilen,
787 unsigned char *output, size_t *olen, size_t osize,
788 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200789{
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (ctx->pk_info == NULL) {
791 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
792 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (ctx->pk_info->encrypt_func == NULL) {
795 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
796 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200797
valerio38992cb2023-04-20 09:56:30 +0200798 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200800}
801
802/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100803 * Check public-private key pair
804 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100805int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
806 const mbedtls_pk_context *prv,
807 int (*f_rng)(void *, unsigned char *, size_t),
808 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100809{
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (pub->pk_info == NULL ||
811 prv->pk_info == NULL) {
812 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100813 }
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (f_rng == NULL) {
816 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100817 }
818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (prv->pk_info->check_pair_func == NULL) {
820 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
821 }
822
823 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
824 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
825 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
826 }
827 } else {
valerio8cbef4d2023-06-01 10:59:03 +0200828 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
829 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
831 }
832 }
833
valerio38992cb2023-04-20 09:56:30 +0200834 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
835 (mbedtls_pk_context *) prv,
836 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100837}
838
839/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200840 * Get key size in bits
841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200843{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500844 /* For backward compatibility, accept NULL or a context that
845 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100846 if (ctx == NULL || ctx->pk_info == NULL) {
847 return 0;
848 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200849
valerio38992cb2023-04-20 09:56:30 +0200850 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200851}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200852
853/*
854 * Export debug information
855 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100856int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200857{
Gilles Peskine449bd832023-01-11 14:50:10 +0100858 if (ctx->pk_info == NULL) {
859 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
860 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200861
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 if (ctx->pk_info->debug_func == NULL) {
863 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
864 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200865
valerio38992cb2023-04-20 09:56:30 +0200866 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200868}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200869
870/*
871 * Access the PK type name
872 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100873const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200874{
Gilles Peskine449bd832023-01-11 14:50:10 +0100875 if (ctx == NULL || ctx->pk_info == NULL) {
876 return "invalid PK";
877 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200878
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200880}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200881
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200882/*
883 * Access the PK type
884 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100885mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200886{
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (ctx == NULL || ctx->pk_info == NULL) {
888 return MBEDTLS_PK_NONE;
889 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200892}
893
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100894#if defined(MBEDTLS_USE_PSA_CRYPTO)
895/*
896 * Load the key to a PSA key slot,
897 * then turn the PK context into a wrapper for that key slot.
898 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200899 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100900 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100901int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
902 mbedtls_svc_key_id_t *key,
903 psa_algorithm_t alg,
904 psa_key_usage_t usage,
905 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100906{
Valerio Setti0d2980f2023-04-05 18:17:13 +0200907#if !defined(MBEDTLS_ECP_LIGHT) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700908 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200909 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200910 ((void) alg);
911 ((void) usage);
912 ((void) alg2);
Valerio Setti0d2980f2023-04-05 18:17:13 +0200913#else /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
914#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100916 size_t d_len;
917 psa_ecc_family_t curve_id;
918 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
919 psa_key_type_t key_type;
920 size_t bits;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100921 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100922
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100923 /* export the private key material in the format PSA wants */
Valerio Settiae8c6282023-05-18 18:57:57 +0200924#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1194ffa2023-05-24 13:15:58 +0200925 unsigned char d[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Valerio Settiae8c6282023-05-18 18:57:57 +0200926 status = psa_export_key(pk->priv_id, d, sizeof(d), &d_len);
927 if (status != PSA_SUCCESS) {
928 return psa_pk_status_to_mbedtls(status);
929 }
930
931 curve_id = pk->ec_family;
932 bits = pk->ec_bits;
933#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1194ffa2023-05-24 13:15:58 +0200934 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
Valerio Settiae8c6282023-05-18 18:57:57 +0200935 mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*pk);
936 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200939 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100940 return ret;
941 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Valerio Settiae8c6282023-05-18 18:57:57 +0200944#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100946
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100947 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 psa_set_key_type(&attributes, key_type);
949 psa_set_key_bits(&attributes, bits);
950 psa_set_key_usage_flags(&attributes, usage);
951 psa_set_key_algorithm(&attributes, alg);
952 if (alg2 != PSA_ALG_NONE) {
953 psa_set_key_enrollment_algorithm(&attributes, alg2);
954 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100955
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100956 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 status = psa_import_key(&attributes, d, d_len, key);
Valerio Setti2d814992023-04-27 10:05:03 +0200958 mbedtls_platform_zeroize(d, sizeof(d));
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500960 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100961 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100962
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100963 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_pk_free(pk);
965 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 return mbedtls_pk_setup_opaque(pk, *key);
968 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200969#endif /* MBEDTLS_ECP_LIGHT */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100970#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100972 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
973 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
974 int key_len;
975 psa_status_t status;
976
977 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100978 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
979 if (key_len <= 0) {
980 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
981 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100982
983 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
985 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
986 psa_set_key_usage_flags(&attributes, usage);
987 psa_set_key_algorithm(&attributes, alg);
988 if (alg2 != PSA_ALG_NONE) {
989 psa_set_key_enrollment_algorithm(&attributes, alg2);
990 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100991
992 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 status = psa_import_key(&attributes,
994 buf + sizeof(buf) - key_len,
995 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001000 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001002
1003 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 mbedtls_pk_free(pk);
1005 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 return mbedtls_pk_setup_opaque(pk, *key);
1008 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001009#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +02001010#endif /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001012}
1013#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#endif /* MBEDTLS_PK_C */