blob: 9cf5ad6272d6fae322fcd2d9951f030643b05735 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010#if defined(MBEDTLS_PK_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000011#include "mbedtls/pk.h"
Chris Jonesdaacb592021-03-09 17:03:29 +000012#include "pk_wrap.h"
Neil Armstrongca5b55f2022-03-15 15:00:55 +010013#include "pkwrite.h"
Valerio Setti3f00b842023-05-15 12:57:06 +020014#include "pk_internal.h"
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020015
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050016#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000017#include "mbedtls/error.h"
Andres AG72849872017-01-19 11:24:33 +000018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_RSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020#include "mbedtls/rsa.h"
Tomi Fontanilles573dc232023-12-10 14:57:51 +020021#if defined(MBEDTLS_PKCS1_V21) && !defined(MBEDTLS_USE_PSA_CRYPTO)
22#include "rsa_internal.h"
23#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020024#endif
Valerio Setti81d75122023-06-14 14:49:33 +020025#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020030#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020031
Gilles Peskine0b172552024-01-18 14:11:26 +010032#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020033#include "psa_util_internal.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010034#include "mbedtls/psa_util.h"
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +010035#endif
36
Andres AG72849872017-01-19 11:24:33 +000037#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010038#include <stdint.h>
Paul Bakker34617722014-06-13 17:20:13 +020039
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020040/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 * Initialise a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020042 */
Gilles Peskine449bd832023-01-11 14:50:10 +010043void mbedtls_pk_init(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020044{
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +020045 ctx->pk_info = NULL;
46 ctx->pk_ctx = NULL;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020047#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti4f387ef2023-05-02 14:15:59 +020048 ctx->priv_id = MBEDTLS_SVC_KEY_ID_INIT;
Tomi Fontanillesbad170e2023-12-14 22:03:12 +020049#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti722f8f72023-05-17 15:31:21 +020050#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
51 memset(ctx->pub_raw, 0, sizeof(ctx->pub_raw));
52 ctx->pub_raw_len = 0;
53 ctx->ec_family = 0;
54 ctx->ec_bits = 0;
55#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020056}
57
58/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 * Free (the components of) a mbedtls_pk_context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020060 */
Gilles Peskine449bd832023-01-11 14:50:10 +010061void mbedtls_pk_free(mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020062{
Gilles Peskine449bd832023-01-11 14:50:10 +010063 if (ctx == NULL) {
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020064 return;
Gilles Peskine449bd832023-01-11 14:50:10 +010065 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066
Valerio Settie00954d2023-04-28 15:24:32 +020067 if ((ctx->pk_info != NULL) && (ctx->pk_info->ctx_free_func != NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +010068 ctx->pk_info->ctx_free_func(ctx->pk_ctx);
69 }
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020070
Valerio Settib5361262023-05-18 18:51:58 +020071#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
72 /* The ownership of the priv_id key for opaque keys is external of the PK
73 * module. It's the user responsibility to clear it after use. */
74 if ((ctx->pk_info != NULL) && (ctx->pk_info->type != MBEDTLS_PK_OPAQUE)) {
75 psa_destroy_key(ctx->priv_id);
76 }
77#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_pk_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020080}
81
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +020082#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020083/*
84 * Initialize a restart context
85 */
Gilles Peskine449bd832023-01-11 14:50:10 +010086void mbedtls_pk_restart_init(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020087{
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +020088 ctx->pk_info = NULL;
89 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020090}
91
92/*
93 * Free the components of a restart context
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void mbedtls_pk_restart_free(mbedtls_pk_restart_ctx *ctx)
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020096{
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (ctx == NULL || ctx->pk_info == NULL ||
98 ctx->pk_info->rs_free_func == NULL) {
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +020099 return;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200100 }
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 ctx->pk_info->rs_free_func(ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200103
104 ctx->pk_info = NULL;
105 ctx->rs_ctx = NULL;
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200106}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200107#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard15d7df22017-08-17 14:33:31 +0200108
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200109/*
110 * Get pk_info structure from type
111 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112const mbedtls_pk_info_t *mbedtls_pk_info_from_type(mbedtls_pk_type_t pk_type)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200113{
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 switch (pk_type) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115#if defined(MBEDTLS_RSA_C)
116 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return &mbedtls_rsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200118#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +0200119#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return &mbedtls_eckey_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 case MBEDTLS_PK_ECKEY_DH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 return &mbedtls_eckeydh_info;
Valerio Setti81d75122023-06-14 14:49:33 +0200124#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti7ca13182023-01-27 13:22:42 +0100125#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 case MBEDTLS_PK_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 return &mbedtls_ecdsa_info;
Valerio Setti0d2980f2023-04-05 18:17:13 +0200128#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 /* MBEDTLS_PK_RSA_ALT omitted on purpose */
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200130 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return NULL;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200132 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200133}
134
135/*
Manuel Pégourié-Gonnardab466942013-08-15 11:30:27 +0200136 * Initialise context
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200137 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138int mbedtls_pk_setup(mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200139{
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (info == NULL || ctx->pk_info != NULL) {
141 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
142 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200143
Valerio Settib5361262023-05-18 18:51:58 +0200144 if ((info->ctx_alloc_func != NULL) &&
Valerio Settie00954d2023-04-28 15:24:32 +0200145 ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 return MBEDTLS_ERR_PK_ALLOC_FAILED;
147 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200148
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +0200149 ctx->pk_info = info;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 return 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200152}
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200153
Valerio Setti7986c772024-02-19 12:03:59 +0100154#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200155/*
156 * Initialise a PSA-wrapping context
157 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158int mbedtls_pk_setup_opaque(mbedtls_pk_context *ctx,
159 const mbedtls_svc_key_id_t key)
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200160{
Neil Armstrongeabbf9d2022-03-15 12:01:26 +0100161 const mbedtls_pk_info_t *info = NULL;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200162 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100163 psa_key_type_t type;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (ctx == NULL || ctx->pk_info != NULL) {
166 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
167 }
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (PSA_SUCCESS != psa_get_key_attributes(key, &attributes)) {
170 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
171 }
172 type = psa_get_key_type(&attributes);
173 psa_reset_key_attributes(&attributes);
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100174
Valerio Settif7cd4192023-06-30 18:11:29 +0200175#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (PSA_KEY_TYPE_IS_ECC_KEY_PAIR(type)) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200177 info = &mbedtls_ecdsa_opaque_info;
Valerio Settif7cd4192023-06-30 18:11:29 +0200178 } else
179#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
180 if (type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Valerio Setti76d0f962023-06-23 13:32:54 +0200181 info = &mbedtls_rsa_opaque_info;
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 } else {
183 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
184 }
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +0100185
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200186 ctx->pk_info = info;
Valerio Setti4f387ef2023-05-02 14:15:59 +0200187 ctx->priv_id = key;
Manuel Pégourié-Gonnard7b5fe042018-10-31 09:57:45 +0100188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return 0;
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200190}
Valerio Setti7986c772024-02-19 12:03:59 +0100191#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +0200192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnardb4fae572014-01-20 11:22:25 +0100194/*
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200195 * Initialize an RSA-alt context
196 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197int mbedtls_pk_setup_rsa_alt(mbedtls_pk_context *ctx, void *key,
198 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
199 mbedtls_pk_rsa_alt_sign_func sign_func,
200 mbedtls_pk_rsa_alt_key_len_func key_len_func)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_rsa_alt_context *rsa_alt;
203 const mbedtls_pk_info_t *info = &mbedtls_rsa_alt_info;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (ctx->pk_info != NULL) {
206 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
207 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if ((ctx->pk_ctx = info->ctx_alloc_func()) == NULL) {
210 return MBEDTLS_ERR_PK_ALLOC_FAILED;
211 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200212
213 ctx->pk_info = info;
214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 rsa_alt = (mbedtls_rsa_alt_context *) ctx->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200216
217 rsa_alt->key = key;
218 rsa_alt->decrypt_func = decrypt_func;
219 rsa_alt->sign_func = sign_func;
220 rsa_alt->key_len_func = key_len_func;
221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 return 0;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200223}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +0200225
226/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200227 * Tell if a PK can do the operations of the given type
228 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_pk_can_do(const mbedtls_pk_context *ctx, mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200230{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500231 /* A context with null pk_info is not set up yet and can't do anything.
232 * For backward compatibility, also accept NULL instead of a context
233 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (ctx == NULL || ctx->pk_info == NULL) {
235 return 0;
236 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 return ctx->pk_info->can_do(type);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200239}
240
Valerio Setti50122b62024-02-22 14:45:02 +0100241#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Neil Armstronga88b1582022-05-11 14:11:25 +0200242/*
243 * Tell if a PK can do the operations of the given PSA algorithm
244 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100245int mbedtls_pk_can_do_ext(const mbedtls_pk_context *ctx, psa_algorithm_t alg,
246 psa_key_usage_t usage)
Neil Armstronga88b1582022-05-11 14:11:25 +0200247{
Neil Armstrong408f6a62022-05-17 14:23:20 +0200248 psa_key_usage_t key_usage;
249
Neil Armstronga88b1582022-05-11 14:11:25 +0200250 /* A context with null pk_info is not set up yet and can't do anything.
251 * For backward compatibility, also accept NULL instead of a context
252 * pointer. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (ctx == NULL || ctx->pk_info == NULL) {
254 return 0;
255 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200256
257 /* Filter out non allowed algorithms */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (PSA_ALG_IS_ECDSA(alg) == 0 &&
259 PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) == 0 &&
260 PSA_ALG_IS_RSA_PSS(alg) == 0 &&
Neil Armstronga88b1582022-05-11 14:11:25 +0200261 alg != PSA_ALG_RSA_PKCS1V15_CRYPT &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 PSA_ALG_IS_ECDH(alg) == 0) {
263 return 0;
264 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200265
Neil Armstrong408f6a62022-05-17 14:23:20 +0200266 /* Filter out non allowed usage flags */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (usage == 0 ||
268 (usage & ~(PSA_KEY_USAGE_SIGN_HASH |
269 PSA_KEY_USAGE_DECRYPT |
270 PSA_KEY_USAGE_DERIVE)) != 0) {
271 return 0;
272 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200273
Neil Armstronga88b1582022-05-11 14:11:25 +0200274 /* Wildcard hash is not allowed */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (PSA_ALG_IS_SIGN_HASH(alg) &&
276 PSA_ALG_SIGN_GET_HASH(alg) == PSA_ALG_ANY_HASH) {
277 return 0;
278 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (mbedtls_pk_get_type(ctx) != MBEDTLS_PK_OPAQUE) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200281 mbedtls_pk_type_t type;
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 if (PSA_ALG_IS_ECDSA(alg) || PSA_ALG_IS_ECDH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200284 type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 } else if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg) ||
286 alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200287 type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 } else if (PSA_ALG_IS_RSA_PSS(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200289 type = MBEDTLS_PK_RSASSA_PSS;
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 } else {
291 return 0;
292 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (ctx->pk_info->can_do(type) == 0) {
295 return 0;
296 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 switch (type) {
Neil Armstrong084338d2022-05-19 16:22:40 +0200299 case MBEDTLS_PK_ECKEY:
300 key_usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
301 break;
302 case MBEDTLS_PK_RSA:
303 case MBEDTLS_PK_RSASSA_PSS:
304 key_usage = PSA_KEY_USAGE_SIGN_HASH |
305 PSA_KEY_USAGE_SIGN_MESSAGE |
306 PSA_KEY_USAGE_DECRYPT;
307 break;
308 default:
Neil Armstrongb80785f2022-05-20 09:25:55 +0200309 /* Should never happen */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return 0;
Neil Armstrong084338d2022-05-19 16:22:40 +0200311 }
312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return (key_usage & usage) == usage;
Neil Armstronga88b1582022-05-11 14:11:25 +0200314 }
315
Neil Armstronga88b1582022-05-11 14:11:25 +0200316 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstronga88b1582022-05-11 14:11:25 +0200317 psa_status_t status;
318
Valerio Setti4f387ef2023-05-02 14:15:59 +0200319 status = psa_get_key_attributes(ctx->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (status != PSA_SUCCESS) {
321 return 0;
322 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200323
Valerio Setti2bd53662023-12-05 10:14:06 +0100324 psa_algorithm_t key_alg = psa_get_key_algorithm(&attributes);
Valerio Setti2bd53662023-12-05 10:14:06 +0100325 psa_algorithm_t key_alg2 = psa_get_key_enrollment_algorithm(&attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 key_usage = psa_get_key_usage_flags(&attributes);
327 psa_reset_key_attributes(&attributes);
Neil Armstronga88b1582022-05-11 14:11:25 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if ((key_usage & usage) != usage) {
330 return 0;
331 }
Neil Armstrong408f6a62022-05-17 14:23:20 +0200332
Neil Armstronga88b1582022-05-11 14:11:25 +0200333 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100334 * Common case: the key alg [or alg2] only allows alg.
Neil Armstronga88b1582022-05-11 14:11:25 +0200335 * This will match PSA_ALG_RSA_PKCS1V15_CRYPT & PSA_ALG_IS_ECDH
336 * directly.
337 * This would also match ECDSA/RSA_PKCS1V15_SIGN/RSA_PSS with
Valerio Setti2bd53662023-12-05 10:14:06 +0100338 * a fixed hash on key_alg [or key_alg2].
Neil Armstronga88b1582022-05-11 14:11:25 +0200339 */
Valerio Setti2bd53662023-12-05 10:14:06 +0100340 if (alg == key_alg) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return 1;
342 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100343 if (alg == key_alg2) {
344 return 1;
345 }
Neil Armstronga88b1582022-05-11 14:11:25 +0200346
347 /*
Valerio Setti2bd53662023-12-05 10:14:06 +0100348 * If key_alg [or key_alg2] is a hash-and-sign with a wildcard for the hash,
Neil Armstrongbbb8b752022-05-17 14:58:27 +0200349 * and alg is the same hash-and-sign family with any hash,
Neil Armstronga88b1582022-05-11 14:11:25 +0200350 * then alg is compliant with this key alg
351 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (PSA_ALG_IS_SIGN_HASH(alg)) {
Neil Armstronga88b1582022-05-11 14:11:25 +0200353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 if (PSA_ALG_IS_SIGN_HASH(key_alg) &&
355 PSA_ALG_SIGN_GET_HASH(key_alg) == PSA_ALG_ANY_HASH &&
356 (alg & ~PSA_ALG_HASH_MASK) == (key_alg & ~PSA_ALG_HASH_MASK)) {
357 return 1;
358 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100359#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (PSA_ALG_IS_SIGN_HASH(key_alg2) &&
361 PSA_ALG_SIGN_GET_HASH(key_alg2) == PSA_ALG_ANY_HASH &&
362 (alg & ~PSA_ALG_HASH_MASK) == (key_alg2 & ~PSA_ALG_HASH_MASK)) {
363 return 1;
364 }
Valerio Setti2bd53662023-12-05 10:14:06 +0100365#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200366 }
367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 return 0;
Neil Armstronga88b1582022-05-11 14:11:25 +0200369}
Valerio Setti50122b62024-02-22 14:45:02 +0100370#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Neil Armstronga88b1582022-05-11 14:11:25 +0200371
Gilles Peskine9cd2e9a2024-01-24 13:40:09 +0100372#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine6ea18362024-01-18 14:16:27 +0100373#if defined(MBEDTLS_RSA_C)
374static psa_algorithm_t psa_algorithm_for_rsa(const mbedtls_rsa_context *rsa,
375 int want_crypt)
376{
377 if (mbedtls_rsa_get_padding_mode(rsa) == MBEDTLS_RSA_PKCS_V21) {
378 if (want_crypt) {
Dave Rodgmanaa741652024-02-13 13:40:26 +0000379 mbedtls_md_type_t md_type = (mbedtls_md_type_t) mbedtls_rsa_get_md_alg(rsa);
Gilles Peskine6ea18362024-01-18 14:16:27 +0100380 return PSA_ALG_RSA_OAEP(mbedtls_md_psa_alg_from_type(md_type));
381 } else {
382 return PSA_ALG_RSA_PSS_ANY_SALT(PSA_ALG_ANY_HASH);
383 }
384 } else {
385 if (want_crypt) {
386 return PSA_ALG_RSA_PKCS1V15_CRYPT;
387 } else {
388 return PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
389 }
390 }
391}
392#endif /* MBEDTLS_RSA_C */
393
Gilles Peskine0b172552024-01-18 14:11:26 +0100394int mbedtls_pk_get_psa_attributes(const mbedtls_pk_context *pk,
395 psa_key_usage_t usage,
396 psa_key_attributes_t *attributes)
397{
398 mbedtls_pk_type_t pk_type = mbedtls_pk_get_type(pk);
399
Gilles Peskineace7c772024-01-18 17:47:54 +0100400 psa_key_usage_t more_usage = usage;
401 if (usage == PSA_KEY_USAGE_SIGN_MESSAGE) {
402 more_usage |= PSA_KEY_USAGE_VERIFY_MESSAGE;
403 } else if (usage == PSA_KEY_USAGE_SIGN_HASH) {
404 more_usage |= PSA_KEY_USAGE_VERIFY_HASH;
405 } else if (usage == PSA_KEY_USAGE_DECRYPT) {
406 more_usage |= PSA_KEY_USAGE_ENCRYPT;
407 }
408 more_usage |= PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY;
409
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100410 int want_private = !(usage == PSA_KEY_USAGE_VERIFY_MESSAGE ||
411 usage == PSA_KEY_USAGE_VERIFY_HASH ||
412 usage == PSA_KEY_USAGE_ENCRYPT);
413
Gilles Peskine0b172552024-01-18 14:11:26 +0100414 switch (pk_type) {
Gilles Peskine6ea18362024-01-18 14:16:27 +0100415#if defined(MBEDTLS_RSA_C)
416 case MBEDTLS_PK_RSA:
Gilles Peskineace7c772024-01-18 17:47:54 +0100417 {
Gilles Peskinee8209752024-02-01 21:00:33 +0100418 int want_crypt = 0; /* 0: sign/verify; 1: encrypt/decrypt */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100419 switch (usage) {
420 case PSA_KEY_USAGE_SIGN_MESSAGE:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100421 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100422 case PSA_KEY_USAGE_VERIFY_MESSAGE:
423 case PSA_KEY_USAGE_VERIFY_HASH:
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100424 /* Nothing to do. */
Gilles Peskine6ea18362024-01-18 14:16:27 +0100425 break;
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100426 case PSA_KEY_USAGE_DECRYPT:
Gilles Peskine6ea18362024-01-18 14:16:27 +0100427 case PSA_KEY_USAGE_ENCRYPT:
428 want_crypt = 1;
429 break;
430 default:
431 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
432 }
433 /* Detect the presence of a private key in a way that works both
434 * in CRT and non-CRT configurations. */
435 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(*pk);
436 int has_private = (mbedtls_rsa_check_privkey(rsa) == 0);
437 if (want_private && !has_private) {
438 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
439 }
440 psa_set_key_type(attributes, (want_private ?
441 PSA_KEY_TYPE_RSA_KEY_PAIR :
442 PSA_KEY_TYPE_RSA_PUBLIC_KEY));
Gilles Peskine55effd92024-01-23 18:07:36 +0100443 psa_set_key_bits(attributes, mbedtls_pk_get_bitlen(pk));
Gilles Peskine6ea18362024-01-18 14:16:27 +0100444 psa_set_key_algorithm(attributes,
445 psa_algorithm_for_rsa(rsa, want_crypt));
446 break;
Gilles Peskineace7c772024-01-18 17:47:54 +0100447 }
Gilles Peskine6ea18362024-01-18 14:16:27 +0100448#endif /* MBEDTLS_RSA_C */
449
Gilles Peskineace7c772024-01-18 17:47:54 +0100450#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
451 case MBEDTLS_PK_ECKEY:
452 case MBEDTLS_PK_ECKEY_DH:
453 case MBEDTLS_PK_ECDSA:
454 {
455 int sign_ok = (pk_type != MBEDTLS_PK_ECKEY_DH);
456 int derive_ok = (pk_type != MBEDTLS_PK_ECDSA);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100457#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Gilles Peskinecb3b4ca2024-02-02 13:12:39 +0100458 psa_ecc_family_t family = pk->ec_family;
459 size_t bits = pk->ec_bits;
460 int has_private = 0;
461 if (pk->priv_id != MBEDTLS_SVC_KEY_ID_INIT) {
462 has_private = 1;
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100463 }
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100464#else
Gilles Peskineae2668b2024-02-01 20:48:04 +0100465 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec_ro(*pk);
Gilles Peskineace7c772024-01-18 17:47:54 +0100466 int has_private = (ec->d.n != 0);
467 size_t bits = 0;
468 psa_ecc_family_t family =
469 mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Gilles Peskinef3dbc982024-01-23 11:05:34 +0100470#endif
Gilles Peskineace7c772024-01-18 17:47:54 +0100471 psa_algorithm_t alg = 0;
472 switch (usage) {
473 case PSA_KEY_USAGE_SIGN_MESSAGE:
474 case PSA_KEY_USAGE_SIGN_HASH:
Gilles Peskineace7c772024-01-18 17:47:54 +0100475 case PSA_KEY_USAGE_VERIFY_MESSAGE:
476 case PSA_KEY_USAGE_VERIFY_HASH:
477 if (!sign_ok) {
478 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
479 }
480#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
481 alg = PSA_ALG_DETERMINISTIC_ECDSA(PSA_ALG_ANY_HASH);
482#else
483 alg = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
484#endif
485 break;
486 case PSA_KEY_USAGE_DERIVE:
Gilles Peskineace7c772024-01-18 17:47:54 +0100487 alg = PSA_ALG_ECDH;
488 if (!derive_ok) {
489 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
490 }
491 break;
492 default:
493 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
494 }
495 if (want_private && !has_private) {
496 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
497 }
498 psa_set_key_type(attributes, (want_private ?
499 PSA_KEY_TYPE_ECC_KEY_PAIR(family) :
500 PSA_KEY_TYPE_ECC_PUBLIC_KEY(family)));
501 psa_set_key_bits(attributes, bits);
502 psa_set_key_algorithm(attributes, alg);
503 break;
504 }
505#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
506
Gilles Peskine0b172552024-01-18 14:11:26 +0100507#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
508 case MBEDTLS_PK_RSA_ALT:
509 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
510#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
511
Gilles Peskine758d8c72024-01-22 20:53:21 +0100512#if defined(MBEDTLS_USE_PSA_CRYPTO)
513 case MBEDTLS_PK_OPAQUE:
514 {
515 psa_key_attributes_t old_attributes = PSA_KEY_ATTRIBUTES_INIT;
516 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
517 status = psa_get_key_attributes(pk->priv_id, &old_attributes);
518 if (status != PSA_SUCCESS) {
519 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
520 }
521 psa_key_type_t old_type = psa_get_key_type(&old_attributes);
522 switch (usage) {
523 case PSA_KEY_USAGE_SIGN_MESSAGE:
524 case PSA_KEY_USAGE_SIGN_HASH:
525 case PSA_KEY_USAGE_VERIFY_MESSAGE:
526 case PSA_KEY_USAGE_VERIFY_HASH:
527 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type) ||
528 old_type == PSA_KEY_TYPE_RSA_KEY_PAIR)) {
529 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
530 }
531 break;
532 case PSA_KEY_USAGE_DECRYPT:
533 case PSA_KEY_USAGE_ENCRYPT:
534 if (old_type != PSA_KEY_TYPE_RSA_KEY_PAIR) {
535 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
536 }
537 break;
538 case PSA_KEY_USAGE_DERIVE:
539 if (!(PSA_KEY_TYPE_IS_ECC_KEY_PAIR(old_type))) {
540 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
541 }
542 break;
Gilles Peskine758d8c72024-01-22 20:53:21 +0100543 default:
544 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
545 }
546 psa_key_type_t new_type = old_type;
547 /* Opaque keys are always key pairs, so we don't need a check
548 * on the input if the required usage is private. We just need
549 * to adjust the type correctly if the required usage is public. */
Gilles Peskinec09df2f2024-01-23 17:03:31 +0100550 if (!want_private) {
Gilles Peskine758d8c72024-01-22 20:53:21 +0100551 new_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(new_type);
552 }
553 more_usage = psa_get_key_usage_flags(&old_attributes);
Gilles Peskine793920c2024-02-01 21:26:54 +0100554 if ((usage & more_usage) == 0) {
555 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
556 }
Gilles Peskine758d8c72024-01-22 20:53:21 +0100557 psa_set_key_type(attributes, new_type);
558 psa_set_key_bits(attributes, psa_get_key_bits(&old_attributes));
559 psa_set_key_algorithm(attributes, psa_get_key_algorithm(&old_attributes));
560 break;
561 }
562#endif /* MBEDTLS_USE_PSA_CRYPTO */
563
Gilles Peskine0b172552024-01-18 14:11:26 +0100564 default:
565 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
566 }
567
Gilles Peskineace7c772024-01-18 17:47:54 +0100568 psa_set_key_usage_flags(attributes, more_usage);
Gilles Peskine1f97e732024-01-18 14:14:24 +0100569 psa_set_key_enrollment_algorithm(attributes, PSA_ALG_NONE);
Gilles Peskine0b172552024-01-18 14:11:26 +0100570
571 return 0;
572}
Gilles Peskine9cd2e9a2024-01-24 13:40:09 +0100573#endif /* MBEDTLS_PSA_CRYPTO_C */
Gilles Peskine0b172552024-01-18 14:11:26 +0100574
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200575/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 * Helper for mbedtls_pk_sign and mbedtls_pk_verify
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200577 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578static inline int pk_hashlen_helper(mbedtls_md_type_t md_alg, size_t *hash_len)
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200579{
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (*hash_len != 0) {
581 return 0;
582 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200583
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200584 *hash_len = mbedtls_md_get_size_from_type(md_alg);
Manuel Pégourié-Gonnarda370e062022-07-05 11:55:20 +0200585
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (*hash_len == 0) {
587 return -1;
588 }
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 return 0;
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200591}
592
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200593#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200594/*
595 * Helper to set up a restart context if needed
596 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100597static int pk_restart_setup(mbedtls_pk_restart_ctx *ctx,
598 const mbedtls_pk_info_t *info)
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200599{
Manuel Pégourié-Gonnardc8c12b62018-07-02 13:09:39 +0200600 /* Don't do anything if already set up or invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 if (ctx == NULL || ctx->pk_info != NULL) {
602 return 0;
603 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200604
605 /* Should never happen when we're called */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 if (info->rs_alloc_func == NULL || info->rs_free_func == NULL) {
607 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
608 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if ((ctx->rs_ctx = info->rs_alloc_func()) == NULL) {
611 return MBEDTLS_ERR_PK_ALLOC_FAILED;
612 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200613
614 ctx->pk_info = info;
615
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 return 0;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200617}
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200618#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200619
Manuel Pégourié-Gonnardbfe32ef2013-08-22 14:55:30 +0200620/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200621 * Verify a signature (restartable)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200622 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623int mbedtls_pk_verify_restartable(mbedtls_pk_context *ctx,
624 mbedtls_md_type_t md_alg,
625 const unsigned char *hash, size_t hash_len,
626 const unsigned char *sig, size_t sig_len,
627 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200628{
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100630 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (ctx->pk_info == NULL ||
634 pk_hashlen_helper(md_alg, &hash_len) != 0) {
635 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
636 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200637
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200638#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200639 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200641 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 ctx->pk_info->verify_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
646 return ret;
647 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200648
valerio38992cb2023-04-20 09:56:30 +0200649 ret = ctx->pk_info->verify_rs_func(ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 md_alg, hash, hash_len, sig, sig_len, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
653 mbedtls_pk_restart_free(rs_ctx);
654 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200657 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200658#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200659 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200660#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (ctx->pk_info->verify_func == NULL) {
663 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
664 }
Manuel Pégourié-Gonnardfff80f82013-08-17 15:20:06 +0200665
valerio38992cb2023-04-20 09:56:30 +0200666 return ctx->pk_info->verify_func(ctx, md_alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 sig, sig_len);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +0200668}
669
670/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200671 * Verify a signature
672 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673int mbedtls_pk_verify(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
674 const unsigned char *hash, size_t hash_len,
675 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200676{
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return mbedtls_pk_verify_restartable(ctx, md_alg, hash, hash_len,
678 sig, sig_len, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200679}
680
681/*
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200682 * Verify a signature with options
683 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100684int mbedtls_pk_verify_ext(mbedtls_pk_type_t type, const void *options,
685 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
686 const unsigned char *hash, size_t hash_len,
687 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200688{
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100690 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500692
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 if (ctx->pk_info == NULL) {
694 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
695 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200696
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (!mbedtls_pk_can_do(ctx, type)) {
698 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
699 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 if (type != MBEDTLS_PK_RSASSA_PSS) {
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500702 /* General case: no options */
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 if (options != NULL) {
704 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
705 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 return mbedtls_pk_verify(ctx, md_alg, hash, hash_len, sig, sig_len);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500708 }
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
712 const mbedtls_pk_rsassa_pss_options *pss_opts;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200713
Dave Rodgman02a53d72023-09-28 17:17:07 +0100714#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
716 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
717 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100718#endif
Andres AG72849872017-01-19 11:24:33 +0000719
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if (options == NULL) {
721 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
722 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200723
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500724 pss_opts = (const mbedtls_pk_rsassa_pss_options *) options;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200725
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500726#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if (pss_opts->mgf1_hash_id == md_alg) {
Manuel Pégourié-Gonnard4dacf582022-06-10 12:50:00 +0200728 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500729 unsigned char *p;
Andrzej Kurek59550532022-02-16 07:46:42 -0500730 int key_len;
731 size_t signature_length;
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500732 psa_status_t status = PSA_ERROR_DATA_CORRUPT;
733 psa_status_t destruction_status = PSA_ERROR_DATA_CORRUPT;
Andrzej Kurek59550532022-02-16 07:46:42 -0500734
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200735 psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500736 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
737 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 psa_algorithm_t psa_sig_alg = PSA_ALG_RSA_PSS_ANY_SALT(psa_md_alg);
739 p = buf + sizeof(buf);
740 key_len = mbedtls_pk_write_pubkey(&p, buf, ctx);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 if (key_len < 0) {
743 return key_len;
744 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500745
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
747 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
748 psa_set_key_algorithm(&attributes, psa_sig_alg);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500749
Gilles Peskine449bd832023-01-11 14:50:10 +0100750 status = psa_import_key(&attributes,
751 buf + sizeof(buf) - key_len, key_len,
752 &key_id);
753 if (status != PSA_SUCCESS) {
754 psa_destroy_key(key_id);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500755 return PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500756 }
757
Andrzej Kurek4a953cd2022-02-16 06:13:35 -0500758 /* This function requires returning MBEDTLS_ERR_PK_SIG_LEN_MISMATCH
759 * on a valid signature with trailing data in a buffer, but
760 * mbedtls_psa_rsa_verify_hash requires the sig_len to be exact,
761 * so for this reason the passed sig_len is overwritten. Smaller
762 * signature lengths should not be accepted for verification. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 signature_length = sig_len > mbedtls_pk_get_len(ctx) ?
764 mbedtls_pk_get_len(ctx) : sig_len;
765 status = psa_verify_hash(key_id, psa_sig_alg, hash,
766 hash_len, sig, signature_length);
767 destruction_status = psa_destroy_key(key_id);
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (status == PSA_SUCCESS && sig_len > mbedtls_pk_get_len(ctx)) {
770 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
771 }
Andrzej Kurek8666df62022-02-15 08:23:02 -0500772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 if (status == PSA_SUCCESS) {
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500774 status = destruction_status;
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 }
Andrzej Kurekd70fa0e2022-02-17 10:51:15 -0500776
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500777 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 } else
Tomi Fontanilles81746622023-07-16 13:06:06 +0300779#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500780 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (sig_len < mbedtls_pk_get_len(ctx)) {
782 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
783 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 ret = mbedtls_rsa_rsassa_pss_verify_ext(mbedtls_pk_rsa(*ctx),
786 md_alg, (unsigned int) hash_len, hash,
787 pss_opts->mgf1_hash_id,
788 pss_opts->expected_salt_len,
789 sig);
790 if (ret != 0) {
791 return ret;
792 }
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (sig_len > mbedtls_pk_get_len(ctx)) {
795 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
796 }
Andrzej Kurek90ba2cb2022-02-15 08:18:44 -0500797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 return 0;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200799 }
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500800#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Andrzej Kurek7db1b782022-02-09 14:13:44 -0500802#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +0200803}
804
805/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200806 * Make a signature (restartable)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200807 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100808int mbedtls_pk_sign_restartable(mbedtls_pk_context *ctx,
809 mbedtls_md_type_t md_alg,
810 const unsigned char *hash, size_t hash_len,
811 unsigned char *sig, size_t sig_size, size_t *sig_len,
812 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
813 mbedtls_pk_restart_ctx *rs_ctx)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200814{
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 if ((md_alg != MBEDTLS_MD_NONE || hash_len != 0) && hash == NULL) {
Tuvshinzaya Erdenekhuu78c1d8c2022-07-29 14:51:50 +0100816 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100817 }
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500818
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 if (ctx->pk_info == NULL || pk_hashlen_helper(md_alg, &hash_len) != 0) {
820 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
821 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200822
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200823#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Manuel Pégourié-Gonnardd55f7762017-08-18 17:40:15 +0200824 /* optimization: use non-restartable version if restart disabled */
Gilles Peskine449bd832023-01-11 14:50:10 +0100825 if (rs_ctx != NULL &&
Manuel Pégourié-Gonnardb843b152018-10-16 10:41:31 +0200826 mbedtls_ecp_restart_is_enabled() &&
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 ctx->pk_info->sign_rs_func != NULL) {
Janos Follath24eed8d2019-11-22 13:21:35 +0000828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 if ((ret = pk_restart_setup(rs_ctx, ctx->pk_info)) != 0) {
831 return ret;
832 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200833
valerio38992cb2023-04-20 09:56:30 +0200834 ret = ctx->pk_info->sign_rs_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100835 hash, hash_len,
836 sig, sig_size, sig_len,
837 f_rng, p_rng, rs_ctx->rs_ctx);
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200838
Gilles Peskine449bd832023-01-11 14:50:10 +0100839 if (ret != MBEDTLS_ERR_ECP_IN_PROGRESS) {
840 mbedtls_pk_restart_free(rs_ctx);
841 }
Manuel Pégourié-Gonnard0bbc66c2017-08-18 16:22:06 +0200842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200844 }
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200845#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200846 (void) rs_ctx;
Manuel Pégourié-Gonnardaaa98142017-08-18 17:30:37 +0200847#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +0200848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 if (ctx->pk_info->sign_func == NULL) {
850 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
851 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200852
valerio38992cb2023-04-20 09:56:30 +0200853 return ctx->pk_info->sign_func(ctx, md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 hash, hash_len,
855 sig, sig_size, sig_len,
856 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200857}
858
859/*
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200860 * Make a signature
861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862int mbedtls_pk_sign(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
863 const unsigned char *hash, size_t hash_len,
864 unsigned char *sig, size_t sig_size, size_t *sig_len,
865 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200866{
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 return mbedtls_pk_sign_restartable(ctx, md_alg, hash, hash_len,
868 sig, sig_size, sig_len,
869 f_rng, p_rng, NULL);
Manuel Pégourié-Gonnard82cb27b2017-05-03 10:59:45 +0200870}
871
872/*
Jerry Yufb0621d2022-03-23 11:42:06 +0800873 * Make a signature given a signature type.
Jerry Yud69439a2022-02-24 15:52:15 +0800874 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100875int mbedtls_pk_sign_ext(mbedtls_pk_type_t pk_type,
876 mbedtls_pk_context *ctx,
877 mbedtls_md_type_t md_alg,
878 const unsigned char *hash, size_t hash_len,
879 unsigned char *sig, size_t sig_size, size_t *sig_len,
880 int (*f_rng)(void *, unsigned char *, size_t),
881 void *p_rng)
Jerry Yud69439a2022-02-24 15:52:15 +0800882{
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 if (ctx->pk_info == NULL) {
884 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
885 }
Jerry Yud69439a2022-02-24 15:52:15 +0800886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if (!mbedtls_pk_can_do(ctx, pk_type)) {
888 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
889 }
Jerry Yud69439a2022-02-24 15:52:15 +0800890
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 if (pk_type != MBEDTLS_PK_RSASSA_PSS) {
892 return mbedtls_pk_sign(ctx, md_alg, hash, hash_len,
893 sig, sig_size, sig_len, f_rng, p_rng);
Jerry Yud69439a2022-02-24 15:52:15 +0800894 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200895
Tomi Fontanilles81746622023-07-16 13:06:06 +0300896#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
897
898#if defined(MBEDTLS_USE_PSA_CRYPTO)
899 const psa_algorithm_t psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 if (psa_md_alg == 0) {
901 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
902 }
Neil Armstrong13e76be2022-04-21 12:08:52 +0200903
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (mbedtls_pk_get_type(ctx) == MBEDTLS_PK_OPAQUE) {
Neil Armstrong13e76be2022-04-21 12:08:52 +0200905 psa_status_t status;
906
Valerio Setti4f387ef2023-05-02 14:15:59 +0200907 status = psa_sign_hash(ctx->priv_id, PSA_ALG_RSA_PSS(psa_md_alg),
Gilles Peskine449bd832023-01-11 14:50:10 +0100908 hash, hash_len,
909 sig, sig_size, sig_len);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500910 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong13e76be2022-04-21 12:08:52 +0200911 }
912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PSS(psa_md_alg),
914 ctx->pk_ctx, hash, hash_len,
915 sig, sig_size, sig_len);
Tomi Fontanilles81746622023-07-16 13:06:06 +0300916#else /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yud69439a2022-02-24 15:52:15 +0800917
Tomi Fontanilles81746622023-07-16 13:06:06 +0300918 if (sig_size < mbedtls_pk_get_len(ctx)) {
919 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
920 }
921
922 if (pk_hashlen_helper(md_alg, &hash_len) != 0) {
923 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
924 }
925
926 mbedtls_rsa_context *const rsa_ctx = mbedtls_pk_rsa(*ctx);
927
Tomi Fontanilles573dc232023-12-10 14:57:51 +0200928 const int ret = mbedtls_rsa_rsassa_pss_sign_no_mode_check(rsa_ctx, f_rng, p_rng, md_alg,
929 (unsigned int) hash_len, hash, sig);
Tomi Fontanilles81746622023-07-16 13:06:06 +0300930 if (ret == 0) {
931 *sig_len = rsa_ctx->len;
932 }
933 return ret;
934
935#endif /* MBEDTLS_USE_PSA_CRYPTO */
936
937#else
938 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
939#endif /* MBEDTLS_RSA_C && MBEDTLS_PKCS1_V21 */
Jerry Yud69439a2022-02-24 15:52:15 +0800940}
941
942/*
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200943 * Decrypt message
944 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100945int mbedtls_pk_decrypt(mbedtls_pk_context *ctx,
946 const unsigned char *input, size_t ilen,
947 unsigned char *output, size_t *olen, size_t osize,
948 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200949{
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (ctx->pk_info == NULL) {
951 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
952 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200953
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 if (ctx->pk_info->decrypt_func == NULL) {
955 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
956 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200957
valerio38992cb2023-04-20 09:56:30 +0200958 return ctx->pk_info->decrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200960}
961
962/*
963 * Encrypt message
964 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100965int mbedtls_pk_encrypt(mbedtls_pk_context *ctx,
966 const unsigned char *input, size_t ilen,
967 unsigned char *output, size_t *olen, size_t osize,
968 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200969{
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 if (ctx->pk_info == NULL) {
971 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
972 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200973
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 if (ctx->pk_info->encrypt_func == NULL) {
975 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
976 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200977
valerio38992cb2023-04-20 09:56:30 +0200978 return ctx->pk_info->encrypt_func(ctx, input, ilen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 output, olen, osize, f_rng, p_rng);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200980}
981
982/*
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100983 * Check public-private key pair
984 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985int mbedtls_pk_check_pair(const mbedtls_pk_context *pub,
986 const mbedtls_pk_context *prv,
987 int (*f_rng)(void *, unsigned char *, size_t),
988 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100989{
Gilles Peskine449bd832023-01-11 14:50:10 +0100990 if (pub->pk_info == NULL ||
991 prv->pk_info == NULL) {
992 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100993 }
994
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 if (f_rng == NULL) {
996 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100997 }
998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 if (prv->pk_info->check_pair_func == NULL) {
1000 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1001 }
1002
1003 if (prv->pk_info->type == MBEDTLS_PK_RSA_ALT) {
1004 if (pub->pk_info->type != MBEDTLS_PK_RSA) {
1005 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1006 }
1007 } else {
valerio8cbef4d2023-06-01 10:59:03 +02001008 if ((prv->pk_info->type != MBEDTLS_PK_OPAQUE) &&
1009 (pub->pk_info != prv->pk_info)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001010 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1011 }
1012 }
1013
valerio38992cb2023-04-20 09:56:30 +02001014 return prv->pk_info->check_pair_func((mbedtls_pk_context *) pub,
1015 (mbedtls_pk_context *) prv,
1016 f_rng, p_rng);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +01001017}
1018
1019/*
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001020 * Get key size in bits
1021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001022size_t mbedtls_pk_get_bitlen(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001023{
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001024 /* For backward compatibility, accept NULL or a context that
1025 * isn't set up yet, and return a fake value that should be safe. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (ctx == NULL || ctx->pk_info == NULL) {
1027 return 0;
1028 }
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001029
valerio38992cb2023-04-20 09:56:30 +02001030 return ctx->pk_info->get_bitlen((mbedtls_pk_context *) ctx);
Manuel Pégourié-Gonnardb3d91872013-08-14 15:56:19 +02001031}
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001032
1033/*
1034 * Export debug information
1035 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001036int mbedtls_pk_debug(const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001037{
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if (ctx->pk_info == NULL) {
1039 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1040 }
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001041
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (ctx->pk_info->debug_func == NULL) {
1043 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
1044 }
Manuel Pégourié-Gonnard01488752014-04-03 22:09:18 +02001045
valerio38992cb2023-04-20 09:56:30 +02001046 ctx->pk_info->debug_func((mbedtls_pk_context *) ctx, items);
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 return 0;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +02001048}
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001049
1050/*
1051 * Access the PK type name
1052 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001053const char *mbedtls_pk_get_name(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001054{
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ctx == NULL || ctx->pk_info == NULL) {
1056 return "invalid PK";
1057 }
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001058
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 return ctx->pk_info->name;
Manuel Pégourié-Gonnard3fb5c5e2013-08-14 18:26:41 +02001060}
Manuel Pégourié-Gonnardc40b4c32013-08-22 13:29:31 +02001061
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001062/*
1063 * Access the PK type
1064 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001065mbedtls_pk_type_t mbedtls_pk_get_type(const mbedtls_pk_context *ctx)
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001066{
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (ctx == NULL || ctx->pk_info == NULL) {
1068 return MBEDTLS_PK_NONE;
1069 }
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001070
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 return ctx->pk_info->type;
Manuel Pégourié-Gonnard8053da42013-09-11 22:28:30 +02001072}
1073
Valerio Setti91905222024-02-20 16:44:28 +01001074#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001075/*
1076 * Load the key to a PSA key slot,
1077 * then turn the PK context into a wrapper for that key slot.
1078 *
Neil Armstrong56e71d42022-04-08 15:12:42 +02001079 * Currently only works for EC & RSA private keys.
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001081int mbedtls_pk_wrap_as_opaque(mbedtls_pk_context *pk,
1082 mbedtls_svc_key_id_t *key,
1083 psa_algorithm_t alg,
1084 psa_key_usage_t usage,
1085 psa_algorithm_t alg2)
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001086{
Valerio Setti81d75122023-06-14 14:49:33 +02001087#if !defined(MBEDTLS_PK_HAVE_ECC_KEYS) && !defined(MBEDTLS_RSA_C)
John Durkopf35069a2020-08-17 22:05:14 -07001088 ((void) pk);
Ronald Croncf56a0a2020-08-04 09:51:30 +02001089 ((void) key);
Neil Armstronga1fc18f2022-04-22 13:57:14 +02001090 ((void) alg);
1091 ((void) usage);
1092 ((void) alg2);
Valerio Setti81d75122023-06-14 14:49:33 +02001093#else /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
1094#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_ECKEY) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001096 size_t d_len;
1097 psa_ecc_family_t curve_id;
1098 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1099 psa_key_type_t key_type;
1100 size_t bits;
Neil Armstrongc1152e42022-03-22 10:29:06 +01001101 psa_status_t status;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001102
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001103 /* export the private key material in the format PSA wants */
Valerio Settiae8c6282023-05-18 18:57:57 +02001104#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1194ffa2023-05-24 13:15:58 +02001105 unsigned char d[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Valerio Settiae8c6282023-05-18 18:57:57 +02001106 status = psa_export_key(pk->priv_id, d, sizeof(d), &d_len);
1107 if (status != PSA_SUCCESS) {
1108 return psa_pk_status_to_mbedtls(status);
1109 }
1110
1111 curve_id = pk->ec_family;
1112 bits = pk->ec_bits;
1113#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1194ffa2023-05-24 13:15:58 +02001114 unsigned char d[MBEDTLS_ECP_MAX_BYTES];
Valerio Settiae8c6282023-05-18 18:57:57 +02001115 mbedtls_ecp_keypair *ec = mbedtls_pk_ec_rw(*pk);
1116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1117
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 d_len = PSA_BITS_TO_BYTES(ec->grp.nbits);
Jethro Beekman33a3ccd2023-05-03 18:25:27 +02001119 if ((ret = mbedtls_ecp_write_key(ec, d, d_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 return ret;
1121 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 curve_id = mbedtls_ecc_group_to_psa(ec->grp.id, &bits);
Valerio Settiae8c6282023-05-18 18:57:57 +02001124#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 key_type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_id);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001126
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001127 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 psa_set_key_type(&attributes, key_type);
1129 psa_set_key_bits(&attributes, bits);
1130 psa_set_key_usage_flags(&attributes, usage);
1131 psa_set_key_algorithm(&attributes, alg);
1132 if (alg2 != PSA_ALG_NONE) {
1133 psa_set_key_enrollment_algorithm(&attributes, alg2);
1134 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001135
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001136 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 status = psa_import_key(&attributes, d, d_len, key);
Valerio Setti2d814992023-04-27 10:05:03 +02001138 mbedtls_platform_zeroize(d, sizeof(d));
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001140 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 }
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001142
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001143 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 mbedtls_pk_free(pk);
1145 mbedtls_pk_init(pk);
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001146
Gilles Peskine449bd832023-01-11 14:50:10 +01001147 return mbedtls_pk_setup_opaque(pk, *key);
1148 } else
Valerio Setti81d75122023-06-14 14:49:33 +02001149#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001150#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (mbedtls_pk_get_type(pk) == MBEDTLS_PK_RSA) {
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001152 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
1153 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1154 int key_len;
1155 psa_status_t status;
1156
1157 /* export the private key material in the format PSA wants */
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 key_len = mbedtls_pk_write_key_der(pk, buf, sizeof(buf));
1159 if (key_len <= 0) {
1160 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1161 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001162
1163 /* prepare the key attributes */
Gilles Peskine449bd832023-01-11 14:50:10 +01001164 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
1165 psa_set_key_bits(&attributes, mbedtls_pk_get_bitlen(pk));
1166 psa_set_key_usage_flags(&attributes, usage);
1167 psa_set_key_algorithm(&attributes, alg);
1168 if (alg2 != PSA_ALG_NONE) {
1169 psa_set_key_enrollment_algorithm(&attributes, alg2);
1170 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001171
1172 /* import private key into PSA */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 status = psa_import_key(&attributes,
1174 buf + sizeof(buf) - key_len,
1175 key_len, key);
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001176
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 mbedtls_platform_zeroize(buf, sizeof(buf));
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001178
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001180 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 }
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001182
1183 /* make PK context wrap the key slot */
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 mbedtls_pk_free(pk);
1185 mbedtls_pk_init(pk);
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 return mbedtls_pk_setup_opaque(pk, *key);
1188 } else
Neil Armstrongca5b55f2022-03-15 15:00:55 +01001189#endif /* MBEDTLS_RSA_C */
Valerio Setti81d75122023-06-14 14:49:33 +02001190#endif /* !MBEDTLS_PK_HAVE_ECC_KEYS && !MBEDTLS_RSA_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 return MBEDTLS_ERR_PK_TYPE_MISMATCH;
Manuel Pégourié-Gonnard347a00e2018-11-19 12:25:37 +01001192}
Valerio Setti91905222024-02-20 16:44:28 +01001193#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194#endif /* MBEDTLS_PK_C */