blob: 433b65fb0c52f99c774eca3f097f7318b7544303 [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"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020026
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020027#include "hash_info.h"
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +020028
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050029#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000030#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020034#endif
Valerio Setti0d2980f2023-04-05 18:17:13 +020035#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020040#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020041
Jerry Yub02ee182022-03-16 10:30:41 +080042#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010043#include "mbedtls/psa_util.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050044#define PSA_PK_TO_MBEDTLS_ERR(status) psa_pk_status_to_mbedtls(status)
45#define PSA_PK_RSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
46 psa_to_pk_rsa_errors, \
47 psa_pk_status_to_mbedtls)
48#define PSA_PK_ECDSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
49 psa_to_pk_ecdsa_errors, \
50 psa_pk_status_to_mbedtls)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010051#endif
52
Andres AG72849872017-01-19 11:24:33 +000053#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010054#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020055
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020056/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020061 ctx->pk_info = NULL;
62 ctx->pk_ctx = NULL;
Valerio Settie00954d2023-04-28 15:24:32 +020063#if defined(MBEDTLS_PSA_CRYPTO_C)
64 ctx->opaque_id = MBEDTLS_SVC_KEY_ID_INIT;
65#endif /* MBEDTLS_PSA_CRYPTO_C */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066}
67
68/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020070 */
Gilles Peskine449bd832023-01-11 14:50:10 +010071void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020074 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010075 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020076
Valerio Settie00954d2023-04-28 15:24:32 +020077 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010078 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
79 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020082}
83
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020084#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020085/*
86 * Initialize a restart context
87 */
Gilles Peskine449bd832023-01-11 14:50:10 +010088void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020089{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020090 ctx->pk_info = NULL;
91 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020092}
93
94/*
95 * Free the components of a restart context
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020098{
Gilles Peskine449bd832023-01-11 14:50:10 +010099 if (ctx == NULL || ctx->pk_info == NULL ||
100 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200101 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200102 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200105
106 ctx->pk_info = NULL;
107 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200108}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200109#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200110
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200111/*
112 * Get pk_info structure from type
113 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100114const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200115{
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117#if defined(MBEDTLS_RSA_C)
118 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200120#endif /* MBEDTLS_RSA_C */
121#if defined(MBEDTLS_ECP_LIGHT)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 return &mbedtls_eckeydh_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200126#endif /* MBEDTLS_ECP_LIGHT */
Valerio Setti7ca13182023-01-27 13:22:42 +0100127#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200130#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200132 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200134 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135}
136
137/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200138 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200139 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200141{
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (info == NULL || ctx->pk_info != NULL) {
143 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
144 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200145
Valerio Settie00954d2023-04-28 15:24:32 +0200146 if ((info->ctx_alloc_func == NULL) ||
147 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 return MBEDTLS_ERR_PK_ALLOC_FAILED;
149 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200151 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200154}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200155
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200156#if defined(MBEDTLS_USE_PSA_CRYPTO)
157/*
158 * Initialise a PSA-wrapping context
159 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100160int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
161 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200162{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100163 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200164 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100165 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (ctx == NULL || ctx->pk_info != NULL) {
168 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
169 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
172 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
173 }
174 type = psa_get_key_type(&attributes);
175 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100178 info = &mbedtls_pk_ecdsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 } else if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100180 info = &mbedtls_pk_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 } else {
182 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
183 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100184
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200185 ctx->pk_info = info;
Valerio Settie00954d2023-04-28 15:24:32 +0200186 ctx->opaque_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200189}
190#endif /* MBEDTLS_USE_PSA_CRYPTO */
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100193/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200194 * Initialize an RSA-alt context
195 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
197 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
198 mbedtls_pk_rsa_alt_sign_func sign_func,
199 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 mbedtls_rsa_alt_context *rsa_alt;
202 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (ctx->pk_info != NULL) {
205 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
206 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
209 return MBEDTLS_ERR_PK_ALLOC_FAILED;
210 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200211
212 ctx->pk_info = info;
213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200215
216 rsa_alt->key = key;
217 rsa_alt->decrypt_func = decrypt_func;
218 rsa_alt->sign_func = sign_func;
219 rsa_alt->key_len_func = key_len_func;
220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200224
225/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200226 * Tell if a PK can do the operations of the given type
227 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200229{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500230 /* A context with null pk_info is not set up yet and can't do anything.
231 * For backward compatibility, also accept NULL instead of a context
232 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if (ctx == NULL || ctx->pk_info == NULL) {
234 return 0;
235 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200238}
239
Neil Armstronga88b1582022-05-11 14:11:25 +0200240#if defined(MBEDTLS_USE_PSA_CRYPTO)
241/*
242 * Tell if a PK can do the operations of the given PSA algorithm
243 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100244int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
245 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200246{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200247 psa_key_usage_t key_usage;
248
Neil Armstronga88b1582022-05-11 14:11:25 +0200249 /* A context with null pk_info is not set up yet and can't do anything.
250 * For backward compatibility, also accept NULL instead of a context
251 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (ctx == NULL || ctx->pk_info == NULL) {
253 return 0;
254 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200255
256 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
258 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
259 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200260 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 PSA_ALG_IS_ECDH(alg) == 0) {
262 return 0;
263 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200264
Neil Armstrong408f6a62022-05-17 14:23:20 +0200265 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if (usage == 0 ||
267 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
268 PSA_KEY_USAGE_DECRYPT |
269 PSA_KEY_USAGE_DERIVE)) != 0) {
270 return 0;
271 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200272
Neil Armstronga88b1582022-05-11 14:11:25 +0200273 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (PSA_ALG_IS_SIGN_HASH(alg) &&
275 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
276 return 0;
277 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200280 mbedtls_pk_type_t type;
281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200283 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
285 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200286 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200288 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 } else {
290 return 0;
291 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (ctx->pk_info->can_do(type) == 0) {
294 return 0;
295 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200298 case MBEDTLS_PK_ECKEY:
299 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
300 break;
301 case MBEDTLS_PK_RSA:
302 case MBEDTLS_PK_RSASSA_PSS:
303 key_usage = PSA_KEY_USAGE_SIGN_HASH |
304 PSA_KEY_USAGE_SIGN_MESSAGE |
305 PSA_KEY_USAGE_DECRYPT;
306 break;
307 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200308 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200310 }
311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200313 }
314
315 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
317 psa_algorithm_t key_alg, key_alg2;
318 psa_status_t status;
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 status = psa_get_key_attributes(*key, &attributes);
321 if (status != PSA_SUCCESS) {
322 return 0;
323 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 key_alg = psa_get_key_algorithm(&attributes);
326 key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
327 key_usage = psa_get_key_usage_flags(&attributes);
328 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200329
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 if ((key_usage & usage) != usage) {
331 return 0;
332 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200333
Neil Armstronga88b1582022-05-11 14:11:25 +0200334 /*
Neil Armstrongdab56ba2022-05-17 11:56:55 +0200335 * Common case: the key alg or alg2 only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200336 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
337 * directly.
338 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
339 * a fixed hash on key_alg/key_alg2.
340 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (alg == key_alg || alg == key_alg2) {
342 return 1;
343 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200344
345 /*
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200346 * If key_alg or key_alg2 is a hash-and-sign with a wildcard for the hash,
347 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200348 * then alg is compliant with this key alg
349 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
353 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
354 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
355 return 1;
356 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
359 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
360 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
361 return 1;
362 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200363 }
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200366}
367#endif /* MBEDTLS_USE_PSA_CRYPTO */
368
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200369/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200371 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100372static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200373{
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (*hash_len != 0) {
375 return 0;
376 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 *hash_len = mbedtls_hash_info_get_size(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (*hash_len == 0) {
381 return -1;
382 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200385}
386
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200387#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200388/*
389 * Helper to set up a restart context if needed
390 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100391static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
392 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200393{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200394 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (ctx == NULL || ctx->pk_info != NULL) {
396 return 0;
397 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200398
399 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
401 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
402 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200403
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
405 return MBEDTLS_ERR_PK_ALLOC_FAILED;
406 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200407
408 ctx->pk_info = info;
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200411}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200412#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200413
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200414/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200415 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200416 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100417int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
418 mbedtls_md_type_t md_alg,
419 const unsigned char *hash, size_t hash_len,
420 const unsigned char *sig, size_t sig_len,
421 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200422{
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100424 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (ctx->pk_info == NULL ||
428 pk_hashlen_helper(md_alg, &hash_len) != 0) {
429 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
430 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200431
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200432#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200433 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200435 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
440 return ret;
441 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200442
valerio38992cb2023-04-20 09:56:30 +0200443 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
447 mbedtls_pk_restart_free(rs_ctx);
448 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200451 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200452#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200453 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200454#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if (ctx->pk_info->verify_func == NULL) {
457 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
458 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200459
valerio38992cb2023-04-20 09:56:30 +0200460 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200462}
463
464/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200465 * Verify a signature
466 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
468 const unsigned char *hash, size_t hash_len,
469 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200470{
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
472 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200473}
474
475/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200476 * Verify a signature with options
477 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
479 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
480 const unsigned char *hash, size_t hash_len,
481 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200482{
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100484 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (ctx->pk_info == NULL) {
488 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
489 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (!mbedtls_pk_can_do(ctx, type)) {
492 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
493 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500496 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (options != NULL) {
498 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
499 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500502 }
503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200504#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500505 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
506 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200507
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
509 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
510 }
Andres AG72849872017-01-19 11:24:33 +0000511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (options == NULL) {
513 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
514 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200515
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500516 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200517
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500518#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200520 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500521 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500522 int key_len;
523 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500524 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
525 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 psa_algorithm_t psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500528 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
529 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
531 p = buf + sizeof(buf);
532 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 if (key_len < 0) {
535 return key_len;
536 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
539 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
540 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 status = psa_import_key(&attributes,
543 buf + sizeof(buf) - key_len, key_len,
544 &key_id);
545 if (status != PSA_SUCCESS) {
546 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500547 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500548 }
549
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500550 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
551 * on a valid signature with trailing data in a buffer, but
552 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
553 * so for this reason the passed sig_len is overwritten. Smaller
554 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
556 mbedtls_pk_get_len(ctx) : sig_len;
557 status = psa_verify_hash(key_id, psa_sig_alg, hash,
558 hash_len, sig, signature_length);
559 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
562 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
563 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500566 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500568
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500569 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 } else
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500571#endif
572 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (sig_len < mbedtls_pk_get_len(ctx)) {
574 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
575 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
578 md_alg, (unsigned int) hash_len, hash,
579 pss_opts->mgf1_hash_id,
580 pss_opts->expected_salt_len,
581 sig);
582 if (ret != 0) {
583 return ret;
584 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (sig_len > mbedtls_pk_get_len(ctx)) {
587 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
588 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200591 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500592#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500594#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200595}
596
597/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200598 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200599 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100600int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
601 mbedtls_md_type_t md_alg,
602 const unsigned char *hash, size_t hash_len,
603 unsigned char *sig, size_t sig_size, size_t *sig_len,
604 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
605 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200606{
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100608 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
612 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
613 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200614
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200615#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200616 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200618 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
623 return ret;
624 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200625
valerio38992cb2023-04-20 09:56:30 +0200626 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100627 hash, hash_len,
628 sig, sig_size, sig_len,
629 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
632 mbedtls_pk_restart_free(rs_ctx);
633 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200634
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200636 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200637#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200638 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200639#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if (ctx->pk_info->sign_func == NULL) {
642 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
643 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200644
valerio38992cb2023-04-20 09:56:30 +0200645 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 hash, hash_len,
647 sig, sig_size, sig_len,
648 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200649}
650
651/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200652 * Make a signature
653 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
655 const unsigned char *hash, size_t hash_len,
656 unsigned char *sig, size_t sig_size, size_t *sig_len,
657 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200658{
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
660 sig, sig_size, sig_len,
661 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200662}
663
Jerry Yu1d172a32022-03-12 19:12:05 +0800664#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200665/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800666 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
669 mbedtls_pk_context *ctx,
670 mbedtls_md_type_t md_alg,
671 const unsigned char *hash, size_t hash_len,
672 unsigned char *sig, size_t sig_size, size_t *sig_len,
673 int (*f_rng)(void *, unsigned char *, size_t),
674 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800675{
Jerry Yufb0621d2022-03-23 11:42:06 +0800676#if defined(MBEDTLS_RSA_C)
677 psa_algorithm_t psa_md_alg;
678#endif /* MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800679 *sig_len = 0;
Jerry Yu1d172a32022-03-12 19:12:05 +0800680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 if (ctx->pk_info == NULL) {
682 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
683 }
Jerry Yud69439a2022-02-24 15:52:15 +0800684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (!mbedtls_pk_can_do(ctx, pk_type)) {
686 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
687 }
Jerry Yud69439a2022-02-24 15:52:15 +0800688
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
690 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
691 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800692 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200693
Jerry Yu89107d12022-03-22 14:20:15 +0800694#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 psa_md_alg = mbedtls_hash_info_psa_from_md(md_alg);
696 if (psa_md_alg == 0) {
697 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
698 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200701 const mbedtls_svc_key_id_t *key = (const mbedtls_svc_key_id_t *) ctx->pk_ctx;
702 psa_status_t status;
703
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 status = psa_sign_hash(*key, PSA_ALG_RSA_PSS(psa_md_alg),
705 hash, hash_len,
706 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500707 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200708 }
709
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
711 ctx->pk_ctx, hash, hash_len,
712 sig, sig_size, sig_len);
Jerry Yu89107d12022-03-22 14:20:15 +0800713#else /* MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Jerry Yu89107d12022-03-22 14:20:15 +0800715#endif /* !MBEDTLS_RSA_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800716
717}
Jerry Yu1d172a32022-03-12 19:12:05 +0800718#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yud69439a2022-02-24 15:52:15 +0800719
720/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200721 * Decrypt message
722 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100723int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
724 const unsigned char *input, size_t ilen,
725 unsigned char *output, size_t *olen, size_t osize,
726 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200727{
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 if (ctx->pk_info == NULL) {
729 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
730 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (ctx->pk_info->decrypt_func == NULL) {
733 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
734 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200735
valerio38992cb2023-04-20 09:56:30 +0200736 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200738}
739
740/*
741 * Encrypt message
742 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100743int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
744 const unsigned char *input, size_t ilen,
745 unsigned char *output, size_t *olen, size_t osize,
746 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200747{
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 if (ctx->pk_info == NULL) {
749 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
750 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200751
Gilles Peskine449bd832023-01-11 14:50:10 +0100752 if (ctx->pk_info->encrypt_func == NULL) {
753 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
754 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200755
valerio38992cb2023-04-20 09:56:30 +0200756 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200758}
759
760/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100761 * Check public-private key pair
762 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
764 const mbedtls_pk_context *prv,
765 int (*f_rng)(void *, unsigned char *, size_t),
766 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100767{
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (pub->pk_info == NULL ||
769 prv->pk_info == NULL) {
770 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100771 }
772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (f_rng == NULL) {
774 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100775 }
776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 if (prv->pk_info->check_pair_func == NULL) {
778 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
779 }
780
781 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
782 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
783 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
784 }
785 } else {
786 if (pub->pk_info != prv->pk_info) {
787 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
788 }
789 }
790
valerio38992cb2023-04-20 09:56:30 +0200791 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
792 (mbedtls_pk_context *) prv,
793 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100794}
795
796/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200797 * Get key size in bits
798 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100799size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200800{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500801 /* For backward compatibility, accept NULL or a context that
802 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 if (ctx == NULL || ctx->pk_info == NULL) {
804 return 0;
805 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200806
valerio38992cb2023-04-20 09:56:30 +0200807 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200808}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200809
810/*
811 * Export debug information
812 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100813int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200814{
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if (ctx->pk_info == NULL) {
816 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
817 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (ctx->pk_info->debug_func == NULL) {
820 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
821 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +0200822
valerio38992cb2023-04-20 09:56:30 +0200823 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200825}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200826
827/*
828 * Access the PK type name
829 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100830const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200831{
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 if (ctx == NULL || ctx->pk_info == NULL) {
833 return "invalid PK";
834 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200837}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +0200838
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200839/*
840 * Access the PK type
841 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100842mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200843{
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (ctx == NULL || ctx->pk_info == NULL) {
845 return MBEDTLS_PK_NONE;
846 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +0200849}
850
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100851#if defined(MBEDTLS_USE_PSA_CRYPTO)
852/*
853 * Load the key to a PSA key slot,
854 * then turn the PK context into a wrapper for that key slot.
855 *
Neil Armstrong56e71d42022-04-08 15:12:42 +0200856 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100857 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100858int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
859 mbedtls_svc_key_id_t *key,
860 psa_algorithm_t alg,
861 psa_key_usage_t usage,
862 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100863{
Valerio Setti0d2980f2023-04-05 18:17:13 +0200864#if !defined(MBEDTLS_ECP_LIGHT) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -0700865 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +0200866 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +0200867 ((void) alg);
868 ((void) usage);
869 ((void) alg2);
Valerio Setti0d2980f2023-04-05 18:17:13 +0200870#else /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
871#if defined(MBEDTLS_ECP_LIGHT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200873 mbedtls_ecp_keypair *ec;
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100874 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
875 size_t d_len;
876 psa_ecc_family_t curve_id;
877 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
878 psa_key_type_t key_type;
879 size_t bits;
880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrongc1152e42022-03-22 10:29:06 +0100881 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100882
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100883 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 ec = mbedtls_pk_ec(*pk);
885 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
Jethro Beekman33a3ccd2023-05-03 18:25:27 +0200886 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 return ret;
888 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100889
Gilles Peskine449bd832023-01-11 14:50:10 +0100890 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
891 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100892
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100893 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 psa_set_key_type(&attributes, key_type);
895 psa_set_key_bits(&attributes, bits);
896 psa_set_key_usage_flags(&attributes, usage);
897 psa_set_key_algorithm(&attributes, alg);
898 if (alg2 != PSA_ALG_NONE) {
899 psa_set_key_enrollment_algorithm(&attributes, alg2);
900 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100901
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100902 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 status = psa_import_key(&attributes, d, d_len, key);
Valerio Setti2d814992023-04-27 10:05:03 +0200904 mbedtls_platform_zeroize(d, sizeof(d));
Gilles Peskine449bd832023-01-11 14:50:10 +0100905 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500906 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100908
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100909 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 mbedtls_pk_free(pk);
911 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 return mbedtls_pk_setup_opaque(pk, *key);
914 } else
Valerio Setti0d2980f2023-04-05 18:17:13 +0200915#endif /* MBEDTLS_ECP_LIGHT */
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100916#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100918 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
919 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
920 int key_len;
921 psa_status_t status;
922
923 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
925 if (key_len <= 0) {
926 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
927 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100928
929 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
931 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
932 psa_set_key_usage_flags(&attributes, usage);
933 psa_set_key_algorithm(&attributes, alg);
934 if (alg2 != PSA_ALG_NONE) {
935 psa_set_key_enrollment_algorithm(&attributes, alg2);
936 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100937
938 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 status = psa_import_key(&attributes,
940 buf + sizeof(buf) - key_len,
941 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500946 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100948
949 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 mbedtls_pk_free(pk);
951 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 return mbedtls_pk_setup_opaque(pk, *key);
954 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +0100955#endif /* MBEDTLS_RSA_C */
Valerio Setti0d2980f2023-04-05 18:17:13 +0200956#endif /* !MBEDTLS_ECP_LIGHT && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100957 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +0100958}
959#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200960#endif /* MBEDTLS_PK_C */