blob: 9c4aa16a6baf37d5f57da82da79f0ec5a9bd8660 [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"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050045#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
46#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
47 psa_to_pk_rsa_errors, \
48 psa_pk_status_to_mbedtls)
49#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
50 psa_to_pk_ecdsa_errors, \
51 psa_pk_status_to_mbedtls)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010052#endif
53
Andres AG72849872017-01-19 11:24:33 +000054#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010055#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020056
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020057/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020059 */
Gilles Peskine449bd832023-01-11 14:50:10 +010060void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020061{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020062 ctx->pk_info = NULL;
63 ctx->pk_ctx = NULL;
Valerio Settie00954d2023-04-28 15:24:32 +020064#if defined(MBEDTLS_PSA_CRYPTO_C)
Valerio Setti4f387ef2023-05-02 14:15:59 +020065 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settie00954d2023-04-28 15:24:32 +020066#endif /* MBEDTLS_PSA_CRYPTO_C */
Valerio Setti722f8f72023-05-17 15:31:21 +020067#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
68 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
69 ctx->pub_raw_len = 0;
70 ctx->ec_family = 0;
71 ctx->ec_bits = 0;
72#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020073}
74
75/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020077 */
Gilles Peskine449bd832023-01-11 14:50:10 +010078void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020079{
Gilles Peskine449bd832023-01-11 14:50:10 +010080 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020081 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010082 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020083
Valerio Settie00954d2023-04-28 15:24:32 +020084 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010085 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
86 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020089}
90
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020091#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020092/*
93 * Initialize a restart context
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020096{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020097 ctx->pk_info = NULL;
98 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020099}
100
101/*
102 * Free the components of a restart context
103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200105{
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (ctx == NULL || ctx->pk_info == NULL ||
107 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200108 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200109 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200112
113 ctx->pk_info = NULL;
114 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200115}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200116#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200117
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200118/*
119 * Get pk_info structure from type
120 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200122{
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if defined(MBEDTLS_RSA_C)
125 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200127#endif /* MBEDTLS_RSA_C */
128#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 return &mbedtls_eckeydh_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200133#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti7ca13182023-01-27 13:22:42 +0100134#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200137#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200139 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200141 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200142}
143
144/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200145 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200146 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200148{
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if (info == NULL || ctx->pk_info != NULL) {
150 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
151 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200152
Valerio Settie00954d2023-04-28 15:24:32 +0200153 if ((info->ctx_alloc_func == NULL) ||
154 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 return MBEDTLS_ERR_PK_ALLOC_FAILED;
156 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200157
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200158 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200161}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200162
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200163#if defined(MBEDTLS_USE_PSA_CRYPTO)
164/*
165 * Initialise a PSA-wrapping context
166 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100167int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
168 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200169{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100170 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200171 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100172 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (ctx == NULL || ctx->pk_info != NULL) {
175 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
176 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
179 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
180 }
181 type = psa_get_key_type(&attributes);
182 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100185 info = &mbedtls_pk_ecdsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 } else if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100187 info = &mbedtls_pk_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 } else {
189 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
190 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100191
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200192 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200193 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200196}
197#endif /* MBEDTLS_USE_PSA_CRYPTO */
198
Valerio Setti483738e2023-05-17 15:37:29 +0200199#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
200int mbedtls_pk_update_public_key_from_keypair(mbedtls_pk_context *pk,
201 mbedtls_ecp_keypair *ecp_keypair)
202{
203 int ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
204
205 if (pk == NULL) {
206 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
207 }
208 /* The raw public key storing mechanism is only supported for EC keys so
209 * we fail silently for other ones. */
210 if ((pk->pk_info->type != MBEDTLS_PK_ECKEY) &&
211 (pk->pk_info->type != MBEDTLS_PK_ECKEY_DH) &&
212 (pk->pk_info->type != MBEDTLS_PK_ECDSA)) {
213 return 0;
214 }
215
216 ret = mbedtls_ecp_point_write_binary(&ecp_keypair->grp, &ecp_keypair->Q,
217 MBEDTLS_ECP_PF_UNCOMPRESSED,
218 &pk->pub_raw_len,
219 pk->pub_raw,
220 MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN);
221 if (ret != 0) {
222 return ret;
223 }
224
225 pk->ec_family = mbedtls_ecc_group_to_psa(ecp_keypair->grp.id,
226 &pk->ec_bits);
Valerio Settia7cb8452023-05-22 18:39:43 +0200227 if (pk->ec_family == 0) {
228 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
229 }
Valerio Setti483738e2023-05-17 15:37:29 +0200230
231 return 0;
232}
Valerio Settic1541cb2023-05-17 15:49:55 +0200233#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti483738e2023-05-17 15:37:29 +0200234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100236/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200237 * Initialize an RSA-alt context
238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
240 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
241 mbedtls_pk_rsa_alt_sign_func sign_func,
242 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_rsa_alt_context *rsa_alt;
245 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200246
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 if (ctx->pk_info != NULL) {
248 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
249 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
252 return MBEDTLS_ERR_PK_ALLOC_FAILED;
253 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200254
255 ctx->pk_info = info;
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200258
259 rsa_alt->key = key;
260 rsa_alt->decrypt_func = decrypt_func;
261 rsa_alt->sign_func = sign_func;
262 rsa_alt->key_len_func = key_len_func;
263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200267
268/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200269 * Tell if a PK can do the operations of the given type
270 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100271int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200272{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500273 /* A context with null pk_info is not set up yet and can't do anything.
274 * For backward compatibility, also accept NULL instead of a context
275 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (ctx == NULL || ctx->pk_info == NULL) {
277 return 0;
278 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200281}
282
Neil Armstronga88b1582022-05-11 14:11:25 +0200283#if defined(MBEDTLS_USE_PSA_CRYPTO)
284/*
285 * Tell if a PK can do the operations of the given PSA algorithm
286 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
288 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200289{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200290 psa_key_usage_t key_usage;
291
Neil Armstronga88b1582022-05-11 14:11:25 +0200292 /* A context with null pk_info is not set up yet and can't do anything.
293 * For backward compatibility, also accept NULL instead of a context
294 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (ctx == NULL || ctx->pk_info == NULL) {
296 return 0;
297 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200298
299 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
301 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
302 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200303 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 PSA_ALG_IS_ECDH(alg) == 0) {
305 return 0;
306 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200307
Neil Armstrong408f6a62022-05-17 14:23:20 +0200308 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (usage == 0 ||
310 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
311 PSA_KEY_USAGE_DECRYPT |
312 PSA_KEY_USAGE_DERIVE)) != 0) {
313 return 0;
314 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200315
Neil Armstronga88b1582022-05-11 14:11:25 +0200316 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 if (PSA_ALG_IS_SIGN_HASH(alg) &&
318 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
319 return 0;
320 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200323 mbedtls_pk_type_t type;
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200326 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
328 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200329 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200331 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 } else {
333 return 0;
334 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ctx->pk_info->can_do(type) == 0) {
337 return 0;
338 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200341 case MBEDTLS_PK_ECKEY:
342 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
343 break;
344 case MBEDTLS_PK_RSA:
345 case MBEDTLS_PK_RSASSA_PSS:
346 key_usage = PSA_KEY_USAGE_SIGN_HASH |
347 PSA_KEY_USAGE_SIGN_MESSAGE |
348 PSA_KEY_USAGE_DECRYPT;
349 break;
350 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200351 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200353 }
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200356 }
357
Neil Armstronga88b1582022-05-11 14:11:25 +0200358 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
359 psa_algorithm_t key_alg, key_alg2;
360 psa_status_t status;
361
Valerio Setti4f387ef2023-05-02 14:15:59 +0200362 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (status != PSA_SUCCESS) {
364 return 0;
365 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 key_alg = psa_get_key_algorithm(&attributes);
368 key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
369 key_usage = psa_get_key_usage_flags(&attributes);
370 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if ((key_usage & usage) != usage) {
373 return 0;
374 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200375
Neil Armstronga88b1582022-05-11 14:11:25 +0200376 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200377 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200378 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
379 * directly.
380 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
381 * a fixed hash on key_alg/key_alg2.
382 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (alg == key_alg || alg == key_alg2) {
384 return 1;
385 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200386
387 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200388 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
389 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200390 * then alg is compliant with this key alg
391 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
395 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
396 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
397 return 1;
398 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
401 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
402 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
403 return 1;
404 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200405 }
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200408}
409#endif /* MBEDTLS_USE_PSA_CRYPTO */
410
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200411/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200413 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100414static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200415{
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (*hash_len != 0) {
417 return 0;
418 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200419
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 *hash_len = mbedtls_hash_info_get_size(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200421
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 if (*hash_len == 0) {
423 return -1;
424 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200425
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200427}
428
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200429#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200430/*
431 * Helper to set up a restart context if needed
432 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
434 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200435{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200436 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (ctx == NULL || ctx->pk_info != NULL) {
438 return 0;
439 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200440
441 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
443 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
444 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
447 return MBEDTLS_ERR_PK_ALLOC_FAILED;
448 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200449
450 ctx->pk_info = info;
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200453}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200454#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200455
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200456/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200457 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200458 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100459int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
460 mbedtls_md_type_t md_alg,
461 const unsigned char *hash, size_t hash_len,
462 const unsigned char *sig, size_t sig_len,
463 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200464{
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100466 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (ctx->pk_info == NULL ||
470 pk_hashlen_helper(md_alg, &hash_len) != 0) {
471 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
472 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200473
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200474#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200475 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200477 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
482 return ret;
483 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200484
valerio38992cb2023-04-20 09:56:30 +0200485 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
489 mbedtls_pk_restart_free(rs_ctx);
490 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200493 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200494#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200495 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200496#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (ctx->pk_info->verify_func == NULL) {
499 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
500 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200501
valerio38992cb2023-04-20 09:56:30 +0200502 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200504}
505
506/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200507 * Verify a signature
508 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100509int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
510 const unsigned char *hash, size_t hash_len,
511 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200512{
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
514 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200515}
516
517/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200518 * Verify a signature with options
519 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100520int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
521 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
522 const unsigned char *hash, size_t hash_len,
523 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200524{
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100526 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 if (ctx->pk_info == NULL) {
530 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
531 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (!mbedtls_pk_can_do(ctx, type)) {
534 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
535 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500538 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 if (options != NULL) {
540 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
541 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500544 }
545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200546#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
548 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
551 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
552 }
Andres AG72849872017-01-19 11:24:33 +0000553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (options == NULL) {
555 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
556 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200557
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500558 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200559
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500560#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200562 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500563 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500564 int key_len;
565 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500566 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
567 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500568
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 psa_algorithm_t psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500570 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
571 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
573 p = buf + sizeof(buf);
574 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 if (key_len < 0) {
577 return key_len;
578 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
581 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
582 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 status = psa_import_key(&attributes,
585 buf + sizeof(buf) - key_len, key_len,
586 &key_id);
587 if (status != PSA_SUCCESS) {
588 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500589 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500590 }
591
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500592 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
593 * on a valid signature with trailing data in a buffer, but
594 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
595 * so for this reason the passed sig_len is overwritten. Smaller
596 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
598 mbedtls_pk_get_len(ctx) : sig_len;
599 status = psa_verify_hash(key_id, psa_sig_alg, hash,
600 hash_len, sig, signature_length);
601 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500602
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
604 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
605 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500608 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500610
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500611 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 } else
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500613#endif
614 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (sig_len < mbedtls_pk_get_len(ctx)) {
616 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
617 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
620 md_alg, (unsigned int) hash_len, hash,
621 pss_opts->mgf1_hash_id,
622 pss_opts->expected_salt_len,
623 sig);
624 if (ret != 0) {
625 return ret;
626 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (sig_len > mbedtls_pk_get_len(ctx)) {
629 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
630 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200633 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500634#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500636#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200637}
638
639/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200640 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200641 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100642int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
643 mbedtls_md_type_t md_alg,
644 const unsigned char *hash, size_t hash_len,
645 unsigned char *sig, size_t sig_size, size_t *sig_len,
646 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
647 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200648{
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100650 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
654 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
655 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200656
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200657#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200658 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200660 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
665 return ret;
666 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200667
valerio38992cb2023-04-20 09:56:30 +0200668 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 hash, hash_len,
670 sig, sig_size, sig_len,
671 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
674 mbedtls_pk_restart_free(rs_ctx);
675 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200678 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200679#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200680 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200681#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (ctx->pk_info->sign_func == NULL) {
684 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
685 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200686
valerio38992cb2023-04-20 09:56:30 +0200687 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 hash, hash_len,
689 sig, sig_size, sig_len,
690 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200691}
692
693/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200694 * Make a signature
695 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
697 const unsigned char *hash, size_t hash_len,
698 unsigned char *sig, size_t sig_size, size_t *sig_len,
699 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200700{
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
702 sig, sig_size, sig_len,
703 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200704}
705
Jerry Yu1d172a32022-03-12 19:12:05 +0800706#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200707/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800708 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800709 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
711 mbedtls_pk_context *ctx,
712 mbedtls_md_type_t md_alg,
713 const unsigned char *hash, size_t hash_len,
714 unsigned char *sig, size_t sig_size, size_t *sig_len,
715 int (*f_rng)(void *, unsigned char *, size_t),
716 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800717{
Jerry Yufb0621d2022-03-23 11:42:06 +0800718#if defined(MBEDTLS_RSA_C)
719 psa_algorithm_t psa_md_alg;
720#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800721 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if (ctx->pk_info == NULL) {
724 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
725 }
Jerry Yud69439a2022-02-24 15:52:15 +0800726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (!mbedtls_pk_can_do(ctx, pk_type)) {
728 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
729 }
Jerry Yud69439a2022-02-24 15:52:15 +0800730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
732 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
733 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800734 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200735
Jerry Yu89107d12022-03-22 14:20:15 +0800736#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
738 if (psa_md_alg == 0) {
739 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
740 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200743 psa_status_t status;
744
Valerio Setti4f387ef2023-05-02 14:15:59 +0200745 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 hash, hash_len,
747 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500748 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200749 }
750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
752 ctx->pk_ctx, hash, hash_len,
753 sig, sig_size, sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800754#else /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Jerry Yu89107d12022-03-22 14:20:15 +0800756#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800757
758}
Jerry Yu1d172a32022-03-12 19:12:05 +0800759#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800760
761/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200762 * Decrypt message
763 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100764int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
765 const unsigned char *input, size_t ilen,
766 unsigned char *output, size_t *olen, size_t osize,
767 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200768{
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (ctx->pk_info == NULL) {
770 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
771 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (ctx->pk_info->decrypt_func == NULL) {
774 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
775 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200776
valerio38992cb2023-04-20 09:56:30 +0200777 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200779}
780
781/*
782 * Encrypt message
783 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100784int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
785 const unsigned char *input, size_t ilen,
786 unsigned char *output, size_t *olen, size_t osize,
787 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200788{
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (ctx->pk_info == NULL) {
790 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
791 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (ctx->pk_info->encrypt_func == NULL) {
794 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
795 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200796
valerio38992cb2023-04-20 09:56:30 +0200797 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200799}
800
801/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100802 * Check public-private key pair
803 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
805 const mbedtls_pk_context *prv,
806 int (*f_rng)(void *, unsigned char *, size_t),
807 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100808{
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 if (pub->pk_info == NULL ||
810 prv->pk_info == NULL) {
811 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100812 }
813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 if (f_rng == NULL) {
815 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100816 }
817
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 if (prv->pk_info->check_pair_func == NULL) {
819 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
820 }
821
822 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
823 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
824 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
825 }
826 } else {
827 if (pub->pk_info != prv->pk_info) {
828 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
829 }
830 }
831
valerio38992cb2023-04-20 09:56:30 +0200832 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
833 (mbedtls_pk_context *) prv,
834 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100835}
836
837/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200838 * Get key size in bits
839 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200841{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500842 /* For backward compatibility, accept NULL or a context that
843 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (ctx == NULL || ctx->pk_info == NULL) {
845 return 0;
846 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200847
valerio38992cb2023-04-20 09:56:30 +0200848 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200849}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200850
851/*
852 * Export debug information
853 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100854int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200855{
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 if (ctx->pk_info == NULL) {
857 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
858 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200859
Gilles Peskine449bd832023-01-11 14:50:10 +0100860 if (ctx->pk_info->debug_func == NULL) {
861 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
862 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200863
valerio38992cb2023-04-20 09:56:30 +0200864 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200866}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200867
868/*
869 * Access the PK type name
870 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100871const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200872{
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 if (ctx == NULL || ctx->pk_info == NULL) {
874 return "invalid PK";
875 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200878}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200879
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200880/*
881 * Access the PK type
882 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200884{
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 if (ctx == NULL || ctx->pk_info == NULL) {
886 return MBEDTLS_PK_NONE;
887 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200890}
891
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100892#if defined(MBEDTLS_USE_PSA_CRYPTO)
893/*
894 * Load the key to a PSA key slot,
895 * then turn the PK context into a wrapper for that key slot.
896 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200897 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100898 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100899int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
900 mbedtls_svc_key_id_t *key,
901 psa_algorithm_t alg,
902 psa_key_usage_t usage,
903 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100904{
Valerio Setti0d2980f2023-04-05 18:17:13 +0200905#if !defined(MBEDTLS_ECP_LIGHT) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700906 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200907 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200908 ((void) alg);
909 ((void) usage);
910 ((void) alg2);
Valerio Setti0d2980f2023-04-05 18:17:13 +0200911#else /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
912#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200914 mbedtls_ecp_keypair *ec;
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100915 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
916 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;
921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
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 Setti77a75682023-05-15 11:18:46 +0200925 ec = mbedtls_pk_ec_rw(*pk);
Gilles Peskine449bd832023-01-11 14:50:10 +0100926 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200927 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100928 return ret;
929 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100930
Gilles Peskine449bd832023-01-11 14:50:10 +0100931 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
932 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100933
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100934 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 psa_set_key_type(&attributes, key_type);
936 psa_set_key_bits(&attributes, bits);
937 psa_set_key_usage_flags(&attributes, usage);
938 psa_set_key_algorithm(&attributes, alg);
939 if (alg2 != PSA_ALG_NONE) {
940 psa_set_key_enrollment_algorithm(&attributes, alg2);
941 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100942
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100943 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 status = psa_import_key(&attributes, d, d_len, key);
Valerio Setti2d814992023-04-27 10:05:03 +0200945 mbedtls_platform_zeroize(d, sizeof(d));
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500947 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100949
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100950 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 mbedtls_pk_free(pk);
952 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 return mbedtls_pk_setup_opaque(pk, *key);
955 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200956#endif /* MBEDTLS_ECP_LIGHT */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100957#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100959 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
960 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
961 int key_len;
962 psa_status_t status;
963
964 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
966 if (key_len <= 0) {
967 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
968 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100969
970 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
972 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
973 psa_set_key_usage_flags(&attributes, usage);
974 psa_set_key_algorithm(&attributes, alg);
975 if (alg2 != PSA_ALG_NONE) {
976 psa_set_key_enrollment_algorithm(&attributes, alg2);
977 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100978
979 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 status = psa_import_key(&attributes,
981 buf + sizeof(buf) - key_len,
982 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100983
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100985
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500987 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100989
990 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 mbedtls_pk_free(pk);
992 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100993
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 return mbedtls_pk_setup_opaque(pk, *key);
995 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100996#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200997#endif /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100999}
1000#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#endif /* MBEDTLS_PK_C */