blob: b5b4603998f877138c4f9260f8b19da0a0ed46df [file] [log] [blame]
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02001/*
2 * Public Key abstraction layer: wrapper functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgmane3c05852023-11-03 12:21:36 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02006 */
7
Gilles Peskinedb09ef62020-06-03 01:43:33 +02008#include "common.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +02009
Gilles Peskine8a6022e2022-10-04 23:01:59 +020010#include "mbedtls/platform_util.h"
11
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_PK_C)
Chris Jonesdaacb592021-03-09 17:03:29 +000013#include "pk_wrap.h"
Valerio Settia1b8af62023-05-17 15:34:57 +020014#include "pk_internal.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000015#include "mbedtls/error.h"
Manuel Pégourié-Gonnard02b10d82023-03-28 12:33:20 +020016#include "md_psa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020017
Manuel Pégourié-Gonnarde511ffc2013-08-22 17:33:21 +020018/* Even if RSA not activated, for the sake of RSA-alt */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/ecp.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020023#endif
24
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if defined(MBEDTLS_ECDSA_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/ecdsa.h"
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +020027#endif
28
Gilles Peskine8a6022e2022-10-04 23:01:59 +020029#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PSA_CRYPTO_C)
30#include "pkwrite.h"
Andres Amaya Garciae32df082017-10-25 09:37:04 +010031#endif
32
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050033#if defined(MBEDTLS_PSA_CRYPTO_C)
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020034#include "psa_util_internal.h"
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050035#endif
36
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010037#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -040038#include "psa/crypto.h"
Gilles Peskine8a6022e2022-10-04 23:01:59 +020039
Valerio Setti80d07982023-02-08 13:49:17 +010040#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine8a6022e2022-10-04 23:01:59 +020041#include "mbedtls/asn1write.h"
42#include "mbedtls/asn1.h"
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010043#endif
Gilles Peskine8a6022e2022-10-04 23:01:59 +020044#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard36867712018-10-31 16:22:49 +010045
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/platform.h"
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +020047
Andres AG72849872017-01-19 11:24:33 +000048#include <limits.h>
Andres Amaya Garcia7c02c502017-08-04 13:32:15 +010049#include <stdint.h>
Gilles Peskine8a6022e2022-10-04 23:01:59 +020050#include <string.h>
Paul Bakker34617722014-06-13 17:20:13 +020051
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050052#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yub02ee182022-03-16 10:30:41 +080053#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010054int mbedtls_pk_error_from_psa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010055{
Gilles Peskine449bd832023-01-11 14:50:10 +010056 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010057 case PSA_SUCCESS:
Gilles Peskine449bd832023-01-11 14:50:10 +010058 return 0;
Neil Armstrong19915c22022-03-01 15:21:02 +010059 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010060 return MBEDTLS_ERR_PK_KEY_INVALID_FORMAT;
Neil Armstrong19915c22022-03-01 15:21:02 +010061 case PSA_ERROR_NOT_PERMITTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010062 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010063 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010064 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
Neil Armstrong19915c22022-03-01 15:21:02 +010065 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010066 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Neil Armstrong19915c22022-03-01 15:21:02 +010067 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +010068 return MBEDTLS_ERR_PK_INVALID_ALG;
Neil Armstrong19915c22022-03-01 15:21:02 +010069 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +010070 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010071 case PSA_ERROR_BAD_STATE:
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010073 case PSA_ERROR_COMMUNICATION_FAILURE:
74 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010075 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +010076 case PSA_ERROR_DATA_CORRUPT:
77 case PSA_ERROR_DATA_INVALID:
78 case PSA_ERROR_STORAGE_FAILURE:
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010080 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine449bd832023-01-11 14:50:10 +010081 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +010082 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +010084 }
85}
86
Neil Armstrong30beca32022-05-03 15:42:13 +020087#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
Valerio Settif6d4dfb2023-07-10 10:55:12 +020088 defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Gilles Peskine449bd832023-01-11 14:50:10 +010089int mbedtls_pk_error_from_psa_rsa(psa_status_t status)
Neil Armstrong19915c22022-03-01 15:21:02 +010090{
Gilles Peskine449bd832023-01-11 14:50:10 +010091 switch (status) {
Neil Armstrong19915c22022-03-01 15:21:02 +010092 case PSA_ERROR_NOT_PERMITTED:
93 case PSA_ERROR_INVALID_ARGUMENT:
94 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
Neil Armstrong19915c22022-03-01 15:21:02 +010096 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +010097 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
Neil Armstrong19915c22022-03-01 15:21:02 +010098 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return MBEDTLS_ERR_RSA_RNG_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100100 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100102 case PSA_ERROR_INVALID_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 return MBEDTLS_ERR_RSA_INVALID_PADDING;
Andrzej Kurekba241382022-12-27 09:17:33 -0500104 case PSA_SUCCESS:
105 return 0;
106 case PSA_ERROR_NOT_SUPPORTED:
107 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
108 case PSA_ERROR_INSUFFICIENT_MEMORY:
109 return MBEDTLS_ERR_PK_ALLOC_FAILED;
110 case PSA_ERROR_BAD_STATE:
111 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
112 case PSA_ERROR_COMMUNICATION_FAILURE:
113 case PSA_ERROR_HARDWARE_FAILURE:
114 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
115 case PSA_ERROR_DATA_CORRUPT:
116 case PSA_ERROR_DATA_INVALID:
117 case PSA_ERROR_STORAGE_FAILURE:
118 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
119 case PSA_ERROR_CORRUPTION_DETECTED:
120 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstrong19915c22022-03-01 15:21:02 +0100121 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500122 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Neil Armstrong19915c22022-03-01 15:21:02 +0100123 }
124}
Valerio Settif6d4dfb2023-07-10 10:55:12 +0200125#endif /* PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY || PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Jerry Yu07869e82022-03-16 16:40:50 +0800126#endif /* MBEDTLS_PSA_CRYPTO_C */
127
128#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu07869e82022-03-16 16:40:50 +0800129#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100130int mbedtls_pk_error_from_psa_ecdsa(psa_status_t status)
Jerry Yu07869e82022-03-16 16:40:50 +0800131{
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 switch (status) {
Jerry Yu07869e82022-03-16 16:40:50 +0800133 case PSA_ERROR_NOT_PERMITTED:
134 case PSA_ERROR_INVALID_ARGUMENT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
Jerry Yu07869e82022-03-16 16:40:50 +0800136 case PSA_ERROR_INVALID_HANDLE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
Jerry Yu07869e82022-03-16 16:40:50 +0800138 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
Jerry Yu07869e82022-03-16 16:40:50 +0800140 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Jerry Yu07869e82022-03-16 16:40:50 +0800142 case PSA_ERROR_INVALID_SIGNATURE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return MBEDTLS_ERR_ECP_VERIFY_FAILED;
Andrzej Kurekba241382022-12-27 09:17:33 -0500144 case PSA_SUCCESS:
145 return 0;
146 case PSA_ERROR_NOT_SUPPORTED:
147 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
148 case PSA_ERROR_INSUFFICIENT_MEMORY:
149 return MBEDTLS_ERR_PK_ALLOC_FAILED;
150 case PSA_ERROR_BAD_STATE:
151 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
152 case PSA_ERROR_COMMUNICATION_FAILURE:
153 case PSA_ERROR_HARDWARE_FAILURE:
154 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
155 case PSA_ERROR_DATA_CORRUPT:
156 case PSA_ERROR_DATA_INVALID:
157 case PSA_ERROR_STORAGE_FAILURE:
158 return MBEDTLS_ERR_PK_FILE_IO_ERROR;
159 case PSA_ERROR_CORRUPTION_DETECTED:
160 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu07869e82022-03-16 16:40:50 +0800161 default:
Andrzej Kurekba241382022-12-27 09:17:33 -0500162 return MBEDTLS_ERR_ERROR_GENERIC_ERROR;
Jerry Yu07869e82022-03-16 16:40:50 +0800163 }
164}
Jerry Yu75339822022-03-23 12:06:31 +0800165#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
Jerry Yu75339822022-03-23 12:06:31 +0800166#endif /* MBEDTLS_USE_PSA_CRYPTO */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500167#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Neil Armstrong19915c22022-03-01 15:21:02 +0100168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100170static int rsa_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200171{
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 return type == MBEDTLS_PK_RSA ||
173 type == MBEDTLS_PK_RSASSA_PSS;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200174}
175
valerio38992cb2023-04-20 09:56:30 +0200176static size_t rsa_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200177{
valerio38992cb2023-04-20 09:56:30 +0200178 const mbedtls_rsa_context *rsa = (const mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 return 8 * mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200180}
181
Neil Armstrong52f41f82022-02-22 15:30:24 +0100182#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200183static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 const unsigned char *hash, size_t hash_len,
185 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200186{
valerio38992cb2023-04-20 09:56:30 +0200187 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
189 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
190 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
191 psa_status_t status;
192 mbedtls_pk_context key;
193 int key_len;
Neil Armstrong6baea782022-03-01 13:52:02 +0100194 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong82cf8042022-03-03 12:30:59 +0100195 psa_algorithm_t psa_alg_md =
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200196 PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
200 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
201 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 if (sig_len < rsa_len) {
204 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
205 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100206
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100207 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100208 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100209 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200210 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
212 if (key_len <= 0) {
213 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
214 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
217 psa_set_key_algorithm(&attributes, psa_alg_md);
218 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 status = psa_import_key(&attributes,
221 buf + sizeof(buf) - key_len, key_len,
222 &key_id);
223 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500224 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100225 goto cleanup;
226 }
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
229 sig, sig_len);
230 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500231 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100232 goto cleanup;
233 }
234 ret = 0;
235
236cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 status = psa_destroy_key(key_id);
238 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500239 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100243}
Valerio Setti5c26b302023-06-21 19:47:01 +0200244#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200245static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 const unsigned char *hash, size_t hash_len,
247 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100248{
Janos Follath24eed8d2019-11-22 13:21:35 +0000249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valerio38992cb2023-04-20 09:56:30 +0200250 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200252
Dave Rodgman02a53d72023-09-28 17:17:07 +0100253#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
255 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
256 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100257#endif
Andres AG72849872017-01-19 11:24:33 +0000258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (sig_len < rsa_len) {
260 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
261 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
264 (unsigned int) hash_len,
265 hash, sig)) != 0) {
266 return ret;
267 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200268
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200269 /* The buffer contains a valid signature followed by extra data.
270 * We have a special error code for that so that so that callers can
271 * use mbedtls_pk_verify() to check "Does the buffer start with a
272 * valid signature?" and not just "Does the buffer contain a valid
273 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (sig_len > rsa_len) {
275 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
276 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200279}
Valerio Setti5c26b302023-06-21 19:47:01 +0200280#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200281
Jerry Yub02ee182022-03-16 10:30:41 +0800282#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100283int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
284 mbedtls_rsa_context *rsa_ctx,
285 const unsigned char *hash, size_t hash_len,
286 unsigned char *sig, size_t sig_size,
287 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100288{
Neil Armstrong98545682022-02-22 16:12:51 +0100289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
290 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
291 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
292 psa_status_t status;
293 mbedtls_pk_context key;
294 int key_len;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530295 unsigned char *buf = NULL;
296 buf = mbedtls_calloc(1, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
297 if (buf == NULL) {
298 return MBEDTLS_ERR_PK_ALLOC_FAILED;
299 }
Neil Armstrong98545682022-02-22 16:12:51 +0100300 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
303 if (sig_size < *sig_len) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530304 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
306 }
Neil Armstrong98545682022-02-22 16:12:51 +0100307
Neil Armstronge4f28682022-02-24 15:41:39 +0100308 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100309 * re-construct one to make it happy */
310 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800311 key.pk_ctx = rsa_ctx;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530312 key_len = mbedtls_pk_write_key_der(&key, buf, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 if (key_len <= 0) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530314 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
316 }
317 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
318 psa_set_key_algorithm(&attributes, alg);
319 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 status = psa_import_key(&attributes,
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530322 buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES - key_len, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 &key_id);
324 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500325 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100326 goto cleanup;
327 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 status = psa_sign_hash(key_id, alg, hash, hash_len,
329 sig, sig_size, sig_len);
330 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500331 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100332 goto cleanup;
333 }
334
335 ret = 0;
336
337cleanup:
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530338 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 status = psa_destroy_key(key_id);
340 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500341 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 }
343 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100344}
Jerry Yu406cf272022-03-22 11:33:42 +0800345#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800346
347#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200348static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 const unsigned char *hash, size_t hash_len,
350 unsigned char *sig, size_t sig_size, size_t *sig_len,
351 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800352{
Jerry Yu1d172a32022-03-12 19:12:05 +0800353 ((void) f_rng);
354 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800355
Jerry Yubd1b3272022-03-24 13:05:20 +0800356 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200357 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (psa_md_alg == 0) {
359 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
360 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
363 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200364 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800366}
Valerio Setti5c26b302023-06-21 19:47:01 +0200367#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200368static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 const unsigned char *hash, size_t hash_len,
370 unsigned char *sig, size_t sig_size, size_t *sig_len,
371 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200372{
valerio38992cb2023-04-20 09:56:30 +0200373 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100374
Dave Rodgman02a53d72023-09-28 17:17:07 +0100375#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
377 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
378 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100379#endif
Andres AG72849872017-01-19 11:24:33 +0000380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 *sig_len = mbedtls_rsa_get_len(rsa);
382 if (sig_size < *sig_len) {
383 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
384 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
387 md_alg, (unsigned int) hash_len,
388 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200389}
Valerio Setti5c26b302023-06-21 19:47:01 +0200390#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200391
Neil Armstrong18f43c72022-02-09 15:32:45 +0100392#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200393static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 const unsigned char *input, size_t ilen,
395 unsigned char *output, size_t *olen, size_t osize,
396 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100397{
valerio38992cb2023-04-20 09:56:30 +0200398 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
400 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
401 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
402 psa_status_t status;
403 mbedtls_pk_context key;
404 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100405 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100406
407 ((void) f_rng);
408 ((void) p_rng);
409
410#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
412 return MBEDTLS_ERR_RSA_INVALID_PADDING;
413 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100414#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (ilen != mbedtls_rsa_get_len(rsa)) {
417 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
418 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100419
420 /* mbedtls_pk_write_key_der() expects a full PK context;
421 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100422 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200423 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
425 if (key_len <= 0) {
426 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
427 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
430 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
431 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 status = psa_import_key(&attributes,
434 buf + sizeof(buf) - key_len, key_len,
435 &key_id);
436 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500437 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100438 goto cleanup;
439 }
440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
442 input, ilen,
443 NULL, 0,
444 output, osize, olen);
445 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500446 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100447 goto cleanup;
448 }
449
450 ret = 0;
451
452cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 mbedtls_platform_zeroize(buf, sizeof(buf));
454 status = psa_destroy_key(key_id);
455 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500456 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100460}
Valerio Setti5c26b302023-06-21 19:47:01 +0200461#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200462static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 const unsigned char *input, size_t ilen,
464 unsigned char *output, size_t *olen, size_t osize,
465 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200466{
valerio38992cb2023-04-20 09:56:30 +0200467 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (ilen != mbedtls_rsa_get_len(rsa)) {
470 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
471 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
474 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200475}
Valerio Setti5c26b302023-06-21 19:47:01 +0200476#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200477
Neil Armstrong96a16a42022-02-10 10:40:11 +0100478#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200479static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 const unsigned char *input, size_t ilen,
481 unsigned char *output, size_t *olen, size_t osize,
482 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100483{
valerio38992cb2023-04-20 09:56:30 +0200484 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
486 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
487 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
488 psa_status_t status;
489 mbedtls_pk_context key;
490 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100491 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100492
493 ((void) f_rng);
494 ((void) p_rng);
495
496#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
498 return MBEDTLS_ERR_RSA_INVALID_PADDING;
499 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100500#endif
501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (mbedtls_rsa_get_len(rsa) > osize) {
503 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
504 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100505
Neil Armstrongac014ca2022-02-24 15:27:54 +0100506 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100507 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100508 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200509 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
511 if (key_len <= 0) {
512 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
513 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
516 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
517 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 status = psa_import_key(&attributes,
520 buf + sizeof(buf) - key_len, key_len,
521 &key_id);
522 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500523 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100524 goto cleanup;
525 }
526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
528 input, ilen,
529 NULL, 0,
530 output, osize, olen);
531 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500532 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100533 goto cleanup;
534 }
535
536 ret = 0;
537
538cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 status = psa_destroy_key(key_id);
540 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500541 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100545}
Valerio Setti5c26b302023-06-21 19:47:01 +0200546#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200547static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 const unsigned char *input, size_t ilen,
549 unsigned char *output, size_t *olen, size_t osize,
550 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200551{
valerio38992cb2023-04-20 09:56:30 +0200552 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200554
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 if (*olen > osize) {
556 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
557 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
560 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200561}
Valerio Setti5c26b302023-06-21 19:47:01 +0200562#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200563
valerio38992cb2023-04-20 09:56:30 +0200564static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 int (*f_rng)(void *, unsigned char *, size_t),
566 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100567{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200568 (void) f_rng;
569 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200570 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
571 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100572}
573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200575{
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200577
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (ctx != NULL) {
579 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
580 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200583}
584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200586{
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
588 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200589}
590
valerio38992cb2023-04-20 09:56:30 +0200591static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200592{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200593#if defined(MBEDTLS_RSA_ALT)
594 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200595 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200596 (void) items;
597#else
valerio38992cb2023-04-20 09:56:30 +0200598 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200601 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200602 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200603
604 items++;
605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200607 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200608 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200609#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200610}
611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200612const mbedtls_pk_info_t mbedtls_rsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +0200613 .type = MBEDTLS_PK_RSA,
614 .name = "RSA",
615 .get_bitlen = rsa_get_bitlen,
616 .can_do = rsa_can_do,
617 .verify_func = rsa_verify_wrap,
618 .sign_func = rsa_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +0200619#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
620 .verify_rs_func = NULL,
621 .sign_rs_func = NULL,
622 .rs_alloc_func = NULL,
623 .rs_free_func = NULL,
624#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +0200625 .decrypt_func = rsa_decrypt_wrap,
626 .encrypt_func = rsa_encrypt_wrap,
627 .check_pair_func = rsa_check_pair_wrap,
628 .ctx_alloc_func = rsa_alloc_wrap,
629 .ctx_free_func = rsa_free_wrap,
630 .debug_func = rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200631};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200633
Valerio Setti81d75122023-06-14 14:49:33 +0200634#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200635/*
636 * Generic EC key
637 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100638static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200639{
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 return type == MBEDTLS_PK_ECKEY ||
641 type == MBEDTLS_PK_ECKEY_DH ||
642 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200643}
644
valerio38992cb2023-04-20 09:56:30 +0200645static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200646{
Valerio Settia1b8af62023-05-17 15:34:57 +0200647#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
648 return pk->ec_bits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200649#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +0200650 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
651 return ecp->grp.pbits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200652#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200653}
654
Valerio Setti1cdddac2023-02-02 13:55:57 +0100655#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400656#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400657/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500658 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
659 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500660 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100661static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
662 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500663{
Janos Follath24eed8d2019-11-22 13:21:35 +0000664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500665 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
668 MBEDTLS_ASN1_INTEGER)) != 0) {
669 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 while (unpadded_len > 0 && **from == 0x00) {
673 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500674 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500675 }
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (unpadded_len > to_len || unpadded_len == 0) {
678 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
679 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500680
Andrzej Kurek9241d182018-11-20 05:04:35 -0500681 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 memset(to, 0x00, padding_len);
683 memcpy(to + padding_len, *from, unpadded_len);
684 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500687}
688
689/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400690 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500691 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500692 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400693 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100694static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
695 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400696{
Janos Follath24eed8d2019-11-22 13:21:35 +0000697 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500698 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
701 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
702 return ret;
703 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400704
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500705 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
707 return ret;
708 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500709 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
711 return ret;
712 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400715}
716
Valerio Setti76d0f962023-06-23 13:32:54 +0200717/* Common helper for ECDSA verify using PSA functions. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200718static int ecdsa_verify_psa(unsigned char *key, size_t key_len,
719 psa_ecc_family_t curve, size_t curve_bits,
720 const unsigned char *hash, size_t hash_len,
721 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400722{
Janos Follath24eed8d2019-11-22 13:21:35 +0000723 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200724 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100725 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settia1b8af62023-05-17 15:34:57 +0200726 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Valerio Settied7d6af2023-06-21 15:42:21 +0200727 size_t signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
728 unsigned char extracted_sig[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
729 unsigned char *p;
730 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if (curve == 0) {
733 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
734 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
737 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
738 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500739
Valerio Settied7d6af2023-06-21 15:42:21 +0200740 status = psa_import_key(&attributes, key, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +0100741 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500742 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400743 goto cleanup;
744 }
745
Valerio Settied7d6af2023-06-21 15:42:21 +0200746 if (signature_len > sizeof(extracted_sig)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500747 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
748 goto cleanup;
749 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 p = (unsigned char *) sig;
Valerio Settia1b8af62023-05-17 15:34:57 +0200752 /* extract_ecdsa_sig's last parameter is the size
Valerio Settif57007d2023-05-19 13:54:39 +0200753 * of each integer to be parsed, so it's actually half
Valerio Settia1b8af62023-05-17 15:34:57 +0200754 * the size of the signature. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200755 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, extracted_sig,
Valerio Settia1b8af62023-05-17 15:34:57 +0200756 signature_len/2)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500757 goto cleanup;
758 }
759
Valerio Settied7d6af2023-06-21 15:42:21 +0200760 status = psa_verify_hash(key_id, psa_sig_md, hash, hash_len,
761 extracted_sig, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500763 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400765 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500768 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
769 goto cleanup;
770 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400771 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400772
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500773cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 status = psa_destroy_key(key_id);
775 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500776 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400780}
Valerio Settied7d6af2023-06-21 15:42:21 +0200781
Valerio Setti76d0f962023-06-23 13:32:54 +0200782static int ecdsa_opaque_verify_wrap(mbedtls_pk_context *pk,
783 mbedtls_md_type_t md_alg,
784 const unsigned char *hash, size_t hash_len,
785 const unsigned char *sig, size_t sig_len)
Valerio Settie7730772023-06-21 16:58:40 +0200786{
787 (void) md_alg;
788 unsigned char key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
789 size_t key_len;
790 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
791 psa_ecc_family_t curve;
792 size_t curve_bits;
793 psa_status_t status;
794
795 status = psa_get_key_attributes(pk->priv_id, &key_attr);
796 if (status != PSA_SUCCESS) {
797 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
798 }
799 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(&key_attr));
800 curve_bits = psa_get_key_bits(&key_attr);
801 psa_reset_key_attributes(&key_attr);
802
803 status = psa_export_public_key(pk->priv_id, key, sizeof(key), &key_len);
804 if (status != PSA_SUCCESS) {
805 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
806 }
807
808 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
809 hash, hash_len, sig, sig_len);
810}
811
Valerio Settied7d6af2023-06-21 15:42:21 +0200812#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
813static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
814 mbedtls_md_type_t md_alg,
815 const unsigned char *hash, size_t hash_len,
816 const unsigned char *sig, size_t sig_len)
817{
818 (void) md_alg;
819 psa_ecc_family_t curve = pk->ec_family;
820 size_t curve_bits = pk->ec_bits;
821
822 return ecdsa_verify_psa(pk->pub_raw, pk->pub_raw_len, curve, curve_bits,
823 hash, hash_len, sig, sig_len);
824}
825#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
826static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
827 mbedtls_md_type_t md_alg,
828 const unsigned char *hash, size_t hash_len,
829 const unsigned char *sig, size_t sig_len)
830{
831 (void) md_alg;
832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
833 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
834 unsigned char key[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
835 size_t key_len;
836 size_t curve_bits;
837 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
838
839 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
840 MBEDTLS_ECP_PF_UNCOMPRESSED,
841 &key_len, key, sizeof(key));
842 if (ret != 0) {
843 return ret;
844 }
845
846 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
847 hash, hash_len, sig, sig_len);
848}
849#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400850#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200851static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 const unsigned char *hash, size_t hash_len,
853 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200854{
Janos Follath24eed8d2019-11-22 13:21:35 +0000855 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200856 ((void) md_alg);
857
valerio38992cb2023-04-20 09:56:30 +0200858 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200860
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
862 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
863 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200866}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400867#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100868#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200869
Valerio Setti1cdddac2023-02-02 13:55:57 +0100870#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100871#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100872/*
873 * Simultaneously convert and move raw MPI from the beginning of a buffer
874 * to an ASN.1 MPI at the end of the buffer.
875 * See also mbedtls_asn1_write_mpi().
876 *
877 * p: pointer to the end of the output buffer
878 * start: start of the output buffer, and also of the mpi to write at the end
879 * n_len: length of the mpi to read from start
880 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
882 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100883{
884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
885 size_t len = 0;
886
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 if ((size_t) (*p - start) < n_len) {
888 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
889 }
Neil Armstrong15021652022-03-01 10:14:17 +0100890
891 len = n_len;
892 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100894
895 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
896 * Neither r nor s should be 0, but as a failsafe measure, still detect
897 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100898 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100899 ++(*p);
900 --len;
901 }
902
903 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 if (len == 0) {
905 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
906 }
Neil Armstrong15021652022-03-01 10:14:17 +0100907
908 /* if the msb is 1, ASN.1 requires that we prepend a 0.
909 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 if (**p & 0x80) {
911 if (*p - start < 1) {
912 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
913 }
Neil Armstrong15021652022-03-01 10:14:17 +0100914
915 *--(*p) = 0x00;
916 len += 1;
917 }
918
Gilles Peskine449bd832023-01-11 14:50:10 +0100919 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
920 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
921 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100922
Gilles Peskine449bd832023-01-11 14:50:10 +0100923 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100924}
925
926/* Transcode signature from PSA format to ASN.1 sequence.
927 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
928 * MPIs, and in-place.
929 *
930 * [in/out] sig: the signature pre- and post-transcoding
931 * [in/out] sig_len: signature length pre- and post-transcoding
932 * [int] buf_len: the available size the in/out buffer
933 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100934static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
935 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100936{
937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
938 size_t len = 0;
939 const size_t rs_len = *sig_len / 2;
940 unsigned char *p = sig + buf_len;
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
943 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100944
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
946 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
947 MBEDTLS_ASN1_CONSTRUCTED |
948 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100949
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100951 *sig_len = len;
952
Gilles Peskine449bd832023-01-11 14:50:10 +0100953 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100954}
Neil Armstronge9606902022-02-09 14:23:00 +0100955
Valerio Setti76d0f962023-06-23 13:32:54 +0200956/* Common helper for ECDSA sign using PSA functions. */
Valerio Setti884c1ec2023-06-23 12:09:13 +0200957static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg,
Valerio Setti4657f102023-06-21 13:55:16 +0200958 const unsigned char *hash, size_t hash_len,
959 unsigned char *sig, size_t sig_size, size_t *sig_len)
960{
961 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
962 psa_status_t status;
Valerio Setti884c1ec2023-06-23 12:09:13 +0200963 psa_algorithm_t psa_sig_md;
964 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
965 psa_algorithm_t alg;
966
967 status = psa_get_key_attributes(key_id, &key_attr);
968 if (status != PSA_SUCCESS) {
969 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
970 }
971 alg = psa_get_key_algorithm(&key_attr);
Valerio Setti76d0f962023-06-23 13:32:54 +0200972 psa_reset_key_attributes(&key_attr);
Valerio Setti884c1ec2023-06-23 12:09:13 +0200973
974 if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg)) {
975 psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
976 } else {
977 psa_sig_md = PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
978 }
Valerio Setti4657f102023-06-21 13:55:16 +0200979
980 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
981 sig, sig_size, sig_len);
982 if (status != PSA_SUCCESS) {
983 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
984 }
985
986 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
987
988 return ret;
989}
990
Valerio Setti76d0f962023-06-23 13:32:54 +0200991static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk,
992 mbedtls_md_type_t md_alg,
993 const unsigned char *hash, size_t hash_len,
994 unsigned char *sig, size_t sig_size,
995 size_t *sig_len,
996 int (*f_rng)(void *, unsigned char *, size_t),
997 void *p_rng)
Valerio Setti4657f102023-06-21 13:55:16 +0200998{
999 ((void) f_rng);
1000 ((void) p_rng);
Valerio Setti4657f102023-06-21 13:55:16 +02001001
Valerio Setti884c1ec2023-06-23 12:09:13 +02001002 return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size,
Valerio Setti4657f102023-06-21 13:55:16 +02001003 sig_len);
1004}
1005
1006#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti76d0f962023-06-23 13:32:54 +02001007/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1008 * using the same function. */
1009#define ecdsa_sign_wrap ecdsa_opaque_sign_wrap
Valerio Setti4657f102023-06-21 13:55:16 +02001010#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001011static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 const unsigned char *hash, size_t hash_len,
1013 unsigned char *sig, size_t sig_size, size_t *sig_len,
1014 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +01001015{
Neil Armstronge9606902022-02-09 14:23:00 +01001016 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronge9606902022-02-09 14:23:00 +01001017 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1018 psa_status_t status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001019 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
1020 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1021 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Neil Armstronge9606902022-02-09 14:23:00 +01001022 size_t curve_bits;
1023 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +02001025 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti4657f102023-06-21 13:55:16 +02001026#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
1027 psa_algorithm_t psa_sig_md =
1028 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
1029#else
1030 psa_algorithm_t psa_sig_md =
1031 PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
1032#endif
Neil Armstronge9606902022-02-09 14:23:00 +01001033 ((void) f_rng);
1034 ((void) p_rng);
1035
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (curve == 0) {
1037 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1038 }
Neil Armstronge9606902022-02-09 14:23:00 +01001039
Gilles Peskine13caa942022-10-04 22:59:26 +02001040 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +01001041 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 }
Gilles Peskine13caa942022-10-04 22:59:26 +02001043 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +01001045 goto cleanup;
1046 }
1047
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1049 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
1050 psa_set_key_algorithm(&attributes, psa_sig_md);
1051
Valerio Setti4657f102023-06-21 13:55:16 +02001052 status = psa_import_key(&attributes, buf, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001054 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +01001056 }
1057
Valerio Setti884c1ec2023-06-23 12:09:13 +02001058 ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len);
Neil Armstronge9606902022-02-09 14:23:00 +01001059
1060cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 mbedtls_platform_zeroize(buf, sizeof(buf));
1062 status = psa_destroy_key(key_id);
1063 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001064 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +01001068}
Valerio Setti4657f102023-06-21 13:55:16 +02001069#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001070#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +02001071static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 const unsigned char *hash, size_t hash_len,
1073 unsigned char *sig, size_t sig_size, size_t *sig_len,
1074 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001075{
valerio38992cb2023-04-20 09:56:30 +02001076 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 md_alg, hash, hash_len,
1078 sig, sig_size, sig_len,
1079 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001080}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001081#endif /* MBEDTLS_USE_PSA_CRYPTO */
1082#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001083
Valerio Setti80d07982023-02-08 13:49:17 +01001084#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001085/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001086static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001087 const unsigned char *hash, size_t hash_len,
1088 const unsigned char *sig, size_t sig_len,
1089 void *rs_ctx);
1090
valerio38992cb2023-04-20 09:56:30 +02001091static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001092 const unsigned char *hash, size_t hash_len,
1093 unsigned char *sig, size_t sig_size, size_t *sig_len,
1094 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1095 void *rs_ctx);
1096
1097/*
1098 * Restart context for ECDSA operations with ECKEY context
1099 *
1100 * We need to store an actual ECDSA context, as we need to pass the same to
1101 * the underlying ecdsa function, so we can't create it on the fly every time.
1102 */
1103typedef struct {
1104 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1105 mbedtls_ecdsa_context ecdsa_ctx;
1106} eckey_restart_ctx;
1107
1108static void *eckey_rs_alloc(void)
1109{
1110 eckey_restart_ctx *rs_ctx;
1111
1112 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1113
1114 if (ctx != NULL) {
1115 rs_ctx = ctx;
1116 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1117 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1118 }
1119
1120 return ctx;
1121}
1122
1123static void eckey_rs_free(void *ctx)
1124{
1125 eckey_restart_ctx *rs_ctx;
1126
1127 if (ctx == NULL) {
1128 return;
1129 }
1130
1131 rs_ctx = ctx;
1132 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1133 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1134
1135 mbedtls_free(ctx);
1136}
1137
valerio38992cb2023-04-20 09:56:30 +02001138static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001139 const unsigned char *hash, size_t hash_len,
1140 const unsigned char *sig, size_t sig_len,
1141 void *rs_ctx)
1142{
1143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1144 eckey_restart_ctx *rs = rs_ctx;
1145
1146 /* Should never happen */
1147 if (rs == NULL) {
1148 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1149 }
1150
1151 /* set up our own sub-context if needed (that is, on first run) */
1152 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001153 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001154 }
1155
valerio38992cb2023-04-20 09:56:30 +02001156 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001157 md_alg, hash, hash_len,
1158 sig, sig_len, &rs->ecdsa_rs));
1159
1160cleanup:
1161 return ret;
1162}
1163
valerio38992cb2023-04-20 09:56:30 +02001164static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001165 const unsigned char *hash, size_t hash_len,
1166 unsigned char *sig, size_t sig_size, size_t *sig_len,
1167 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1168 void *rs_ctx)
1169{
1170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1171 eckey_restart_ctx *rs = rs_ctx;
1172
1173 /* Should never happen */
1174 if (rs == NULL) {
1175 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1176 }
1177
1178 /* set up our own sub-context if needed (that is, on first run) */
1179 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001180 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001181 }
1182
valerio38992cb2023-04-20 09:56:30 +02001183 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001184 hash, hash_len, sig, sig_size, sig_len,
1185 f_rng, p_rng, &rs->ecdsa_rs));
1186
1187cleanup:
1188 return ret;
1189}
Valerio Setti80d07982023-02-08 13:49:17 +01001190#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001191
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001192#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settibb7603a2023-06-21 18:34:54 +02001193#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerio38992cb2023-04-20 09:56:30 +02001194static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001195{
Valerio Setti9efa8c42023-05-19 13:27:30 +02001196 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001198 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001199 size_t prv_key_len;
Valerio Settiae8c6282023-05-18 18:57:57 +02001200 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001201
1202 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1203 &prv_key_len);
1204 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1205 if (ret != 0) {
1206 return ret;
1207 }
1208
1209 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1210 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1211 }
Valerio Settibb7603a2023-06-21 18:34:54 +02001212
1213 return 0;
1214}
1215#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1216static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
1217{
1218 psa_status_t status;
1219 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1220 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1221 size_t prv_key_len;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001222 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001223 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1224 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001225 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1226 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001227 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001228 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001229 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +02001230 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001231
Valerio Settia7cb8452023-05-22 18:39:43 +02001232 if (curve == 0) {
1233 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1234 }
1235
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001236 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1237 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1238
Valerio Settia1b8af62023-05-17 15:34:57 +02001239 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1240 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001241 if (ret != 0) {
Valerio Setti35d1dac2023-06-30 18:04:16 +02001242 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001243 return ret;
1244 }
1245
1246 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti35d1dac2023-06-30 18:04:16 +02001247 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti1df94f82023-04-07 08:59:24 +02001248 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1249 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001250 return ret;
1251 }
1252
Valerio Setti35d1dac2023-06-30 18:04:16 +02001253 // From now on prv_key_buf is used to store the public key of prv.
Valerio Setti1df94f82023-04-07 08:59:24 +02001254 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1255 &prv_key_len);
1256 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1257 destruction_status = psa_destroy_key(key_id);
1258 if (ret != 0) {
1259 return ret;
1260 } else if (destruction_status != PSA_SUCCESS) {
1261 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001262 }
1263
Valerio Settia1b8af62023-05-17 15:34:57 +02001264 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1265 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001266 MBEDTLS_ECP_PF_UNCOMPRESSED,
1267 &pub_key_len, pub_key_buf,
1268 sizeof(pub_key_buf));
1269 if (ret != 0) {
1270 return ret;
1271 }
1272
1273 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1274 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1275 }
1276
1277 return 0;
1278}
Valerio Settibb7603a2023-06-21 18:34:54 +02001279#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001280
Valerio Settibb7603a2023-06-21 18:34:54 +02001281static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1282 int (*f_rng)(void *, unsigned char *, size_t),
1283 void *p_rng)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001284{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001285 (void) f_rng;
1286 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001287 return eckey_check_pair_psa(pub, prv);
Valerio Settibb7603a2023-06-21 18:34:54 +02001288}
1289#else /* MBEDTLS_USE_PSA_CRYPTO */
1290static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1291 int (*f_rng)(void *, unsigned char *, size_t),
1292 void *p_rng)
1293{
valerio38992cb2023-04-20 09:56:30 +02001294 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1295 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001296 f_rng, p_rng);
1297}
Valerio Settibb7603a2023-06-21 18:34:54 +02001298#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001299
Valerio Setti88a3aee2023-06-29 15:01:10 +02001300#if defined(MBEDTLS_USE_PSA_CRYPTO)
1301#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1302/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1303 * using the same function. */
1304#define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap
1305#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1306static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub,
1307 mbedtls_pk_context *prv,
1308 int (*f_rng)(void *, unsigned char *, size_t),
1309 void *p_rng)
1310{
1311 psa_status_t status;
1312 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1313 size_t exp_pub_key_len = 0;
1314 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1315 size_t pub_key_len = 0;
1316 int ret;
1317 (void) f_rng;
1318 (void) p_rng;
1319
1320 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1321 &exp_pub_key_len);
1322 if (status != PSA_SUCCESS) {
1323 ret = psa_pk_status_to_mbedtls(status);
1324 return ret;
1325 }
1326 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1327 &(mbedtls_pk_ec_ro(*pub)->Q),
1328 MBEDTLS_ECP_PF_UNCOMPRESSED,
1329 &pub_key_len, pub_key, sizeof(pub_key));
1330 if (ret != 0) {
1331 return ret;
1332 }
1333 if ((exp_pub_key_len != pub_key_len) ||
1334 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1335 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1336 }
1337 return 0;
1338}
1339#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1340#endif /* MBEDTLS_USE_PSA_CRYPTO */
1341
Valerio Settib5361262023-05-18 18:51:58 +02001342#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001343static void *eckey_alloc_wrap(void)
1344{
1345 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1346
1347 if (ctx != NULL) {
1348 mbedtls_ecp_keypair_init(ctx);
1349 }
1350
1351 return ctx;
1352}
1353
1354static void eckey_free_wrap(void *ctx)
1355{
1356 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1357 mbedtls_free(ctx);
1358}
Valerio Settib5361262023-05-18 18:51:58 +02001359#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001360
valerio38992cb2023-04-20 09:56:30 +02001361static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001362{
Valerio Settia1b8af62023-05-17 15:34:57 +02001363#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1364 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1365 items->name = "eckey.Q";
1366 items->value = pk;
Valerio Setti5c26b302023-06-21 19:47:01 +02001367#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001368 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001369 items->type = MBEDTLS_PK_DEBUG_ECP;
1370 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001371 items->value = &(ecp->Q);
Valerio Setti5c26b302023-06-21 19:47:01 +02001372#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001373}
1374
1375const mbedtls_pk_info_t mbedtls_eckey_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001376 .type = MBEDTLS_PK_ECKEY,
1377 .name = "EC",
1378 .get_bitlen = eckey_get_bitlen,
1379 .can_do = eckey_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001380#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001381 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001382#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1383 .verify_func = NULL,
1384#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001385#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001386 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001387#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1388 .sign_func = NULL,
1389#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001390#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001391 .verify_rs_func = eckey_verify_rs_wrap,
1392 .sign_rs_func = eckey_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001393 .rs_alloc_func = eckey_rs_alloc,
1394 .rs_free_func = eckey_rs_free,
1395#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1396 .decrypt_func = NULL,
1397 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001398 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001399#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1400 .ctx_alloc_func = NULL,
1401 .ctx_free_func = NULL,
1402#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001403 .ctx_alloc_func = eckey_alloc_wrap,
1404 .ctx_free_func = eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001405#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001406 .debug_func = eckey_debug,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001407};
1408
1409/*
1410 * EC key restricted to ECDH
1411 */
1412static int eckeydh_can_do(mbedtls_pk_type_t type)
1413{
1414 return type == MBEDTLS_PK_ECKEY ||
1415 type == MBEDTLS_PK_ECKEY_DH;
1416}
1417
1418const mbedtls_pk_info_t mbedtls_eckeydh_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001419 .type = MBEDTLS_PK_ECKEY_DH,
1420 .name = "EC_DH",
1421 .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */
1422 .can_do = eckeydh_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001423 .verify_func = NULL,
1424 .sign_func = NULL,
1425#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1426 .verify_rs_func = NULL,
1427 .sign_rs_func = NULL,
1428#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1429 .decrypt_func = NULL,
1430 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001431 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001432#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1433 .ctx_alloc_func = NULL,
1434 .ctx_free_func = NULL,
1435#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001436 .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */
1437 .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */
Valerio Settib5361262023-05-18 18:51:58 +02001438#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001439 .debug_func = eckey_debug, /* Same underlying key structure */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001440};
Valerio Setti1cdddac2023-02-02 13:55:57 +01001441
1442#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1443static int ecdsa_can_do(mbedtls_pk_type_t type)
1444{
1445 return type == MBEDTLS_PK_ECDSA;
1446}
1447
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001448#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001449static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001450 const unsigned char *hash, size_t hash_len,
1451 const unsigned char *sig, size_t sig_len,
1452 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001453{
Janos Follath24eed8d2019-11-22 13:21:35 +00001454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001455 ((void) md_alg);
1456
1457 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001458 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 hash, hash_len, sig, sig_len,
1460 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1463 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1464 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001467}
1468
valerio38992cb2023-04-20 09:56:30 +02001469static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 const unsigned char *hash, size_t hash_len,
1471 unsigned char *sig, size_t sig_size, size_t *sig_len,
1472 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1473 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001474{
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001476 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1478 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001479
1480}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001481
Gilles Peskine449bd832023-01-11 14:50:10 +01001482static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001483{
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001485
Gilles Peskine449bd832023-01-11 14:50:10 +01001486 if (ctx != NULL) {
1487 mbedtls_ecdsa_restart_init(ctx);
1488 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001491}
1492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001494{
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 mbedtls_ecdsa_restart_free(ctx);
1496 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001497}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001498#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500const mbedtls_pk_info_t mbedtls_ecdsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001501 .type = MBEDTLS_PK_ECDSA,
1502 .name = "ECDSA",
1503 .get_bitlen = eckey_get_bitlen, /* Compatible key structures */
1504 .can_do = ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001505#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001506 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001507#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1508 .verify_func = NULL,
1509#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001510#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001511 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001512#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1513 .sign_func = NULL,
1514#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001515#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001516 .verify_rs_func = ecdsa_verify_rs_wrap,
1517 .sign_rs_func = ecdsa_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001518 .rs_alloc_func = ecdsa_rs_alloc,
1519 .rs_free_func = ecdsa_rs_free,
1520#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1521 .decrypt_func = NULL,
1522 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001523 .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001524#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1525 .ctx_alloc_func = NULL,
1526 .ctx_free_func = NULL,
1527#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001528 .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */
1529 .ctx_free_func = eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001530#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001531 .debug_func = eckey_debug, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001532};
Valerio Setti7ca13182023-01-27 13:22:42 +01001533#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Valerio Settid9d74c22023-06-29 15:00:02 +02001534#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001537/*
1538 * Support for alternative RSA-private implementations
1539 */
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001542{
Gilles Peskine449bd832023-01-11 14:50:10 +01001543 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001544}
1545
valerio38992cb2023-04-20 09:56:30 +02001546static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001547{
valerio38992cb2023-04-20 09:56:30 +02001548 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001551}
1552
valerio38992cb2023-04-20 09:56:30 +02001553static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001554 const unsigned char *hash, size_t hash_len,
1555 unsigned char *sig, size_t sig_size, size_t *sig_len,
1556 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001557{
valerio38992cb2023-04-20 09:56:30 +02001558 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001559
Dave Rodgman02a53d72023-09-28 17:17:07 +01001560#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 if (UINT_MAX < hash_len) {
1562 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1563 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001564#endif
Andres AG72849872017-01-19 11:24:33 +00001565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1567 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1568 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1569 }
1570 if (*sig_len > sig_size) {
1571 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1572 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1575 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001576}
1577
valerio38992cb2023-04-20 09:56:30 +02001578static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001579 const unsigned char *input, size_t ilen,
1580 unsigned char *output, size_t *olen, size_t osize,
1581 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001582{
valerio38992cb2023-04-20 09:56:30 +02001583 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001584
1585 ((void) f_rng);
1586 ((void) p_rng);
1587
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1589 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1590 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001591
Gilles Peskine449bd832023-01-11 14:50:10 +01001592 return rsa_alt->decrypt_func(rsa_alt->key,
1593 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001594}
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001597static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001598 int (*f_rng)(void *, unsigned char *, size_t),
1599 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001602 unsigned char hash[32];
1603 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001604 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1607 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001608 }
1609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 memset(hash, 0x2a, sizeof(hash));
1611
valerio38992cb2023-04-20 09:56:30 +02001612 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 hash, sizeof(hash),
1614 sig, sizeof(sig), &sig_len,
1615 f_rng, p_rng)) != 0) {
1616 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001617 }
1618
valerio38992cb2023-04-20 09:56:30 +02001619 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001620 hash, sizeof(hash), sig, sig_len) != 0) {
1621 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1622 }
1623
1624 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001625}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001627
Gilles Peskine449bd832023-01-11 14:50:10 +01001628static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001629{
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632 if (ctx != NULL) {
1633 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1634 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001637}
1638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001640{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001641 mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001642}
1643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001645 .type = MBEDTLS_PK_RSA_ALT,
1646 .name = "RSA-alt",
1647 .get_bitlen = rsa_alt_get_bitlen,
1648 .can_do = rsa_alt_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001649 .verify_func = NULL,
Valerio Settif69514a2023-06-21 18:16:49 +02001650 .sign_func = rsa_alt_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001651#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1652 .verify_rs_func = NULL,
1653 .sign_rs_func = NULL,
1654 .rs_alloc_func = NULL,
1655 .rs_free_func = NULL,
1656#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +02001657 .decrypt_func = rsa_alt_decrypt_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001658 .encrypt_func = NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_RSA_C)
Valerio Settif69514a2023-06-21 18:16:49 +02001660 .check_pair_func = rsa_alt_check_pair,
Valerio Setti97976e32023-06-23 14:08:26 +02001661#else
1662 .check_pair_func = NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001663#endif
Valerio Settif69514a2023-06-21 18:16:49 +02001664 .ctx_alloc_func = rsa_alt_alloc_wrap,
1665 .ctx_free_func = rsa_alt_free_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001666 .debug_func = NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001667};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001669
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001670#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +02001671static size_t opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001672{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001673 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001674 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001675
Valerio Setti4f387ef2023-05-02 14:15:59 +02001676 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 return 0;
1678 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 bits = psa_get_key_bits(&attributes);
1681 psa_reset_key_attributes(&attributes);
1682 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001683}
1684
Valerio Setti38913c12023-06-30 16:18:33 +02001685#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti76d0f962023-06-23 13:32:54 +02001686static int ecdsa_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001687{
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 return type == MBEDTLS_PK_ECKEY ||
1689 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001690}
1691
Valerio Setti76d0f962023-06-23 13:32:54 +02001692const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = {
Valerio Setti574a00b2023-06-21 19:47:37 +02001693 .type = MBEDTLS_PK_OPAQUE,
1694 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001695 .get_bitlen = opaque_get_bitlen,
1696 .can_do = ecdsa_opaque_can_do,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001697#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001698 .verify_func = ecdsa_opaque_verify_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001699#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1700 .verify_func = NULL,
1701#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1702#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Setti76d0f962023-06-23 13:32:54 +02001703 .sign_func = ecdsa_opaque_sign_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001704#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1705 .sign_func = NULL,
1706#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti97976e32023-06-23 14:08:26 +02001707#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1708 .verify_rs_func = NULL,
1709 .sign_rs_func = NULL,
1710 .rs_alloc_func = NULL,
1711 .rs_free_func = NULL,
1712#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1713 .decrypt_func = NULL,
1714 .encrypt_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001715 .check_pair_func = ecdsa_opaque_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001716 .ctx_alloc_func = NULL,
1717 .ctx_free_func = NULL,
1718 .debug_func = NULL,
Valerio Setti574a00b2023-06-21 19:47:37 +02001719};
Valerio Setti38913c12023-06-30 16:18:33 +02001720#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti574a00b2023-06-21 19:47:37 +02001721
Valerio Setti76d0f962023-06-23 13:32:54 +02001722static int rsa_opaque_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001723{
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 return type == MBEDTLS_PK_RSA ||
1725 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001726}
1727
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001728#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001729static int rsa_opaque_decrypt(mbedtls_pk_context *pk,
1730 const unsigned char *input, size_t ilen,
1731 unsigned char *output, size_t *olen, size_t osize,
1732 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001733{
Valerio Setti574a00b2023-06-21 19:47:37 +02001734 psa_status_t status;
1735
1736 /* PSA has its own RNG */
1737 (void) f_rng;
1738 (void) p_rng;
1739
1740 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
1741 input, ilen,
1742 NULL, 0,
1743 output, osize, olen);
1744 if (status != PSA_SUCCESS) {
1745 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1746 }
1747
1748 return 0;
1749}
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001750#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti574a00b2023-06-21 19:47:37 +02001751
Valerio Setti76d0f962023-06-23 13:32:54 +02001752static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
1753 const unsigned char *hash, size_t hash_len,
1754 unsigned char *sig, size_t sig_size, size_t *sig_len,
1755 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001756{
1757#if defined(MBEDTLS_RSA_C)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001758 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001759 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001760 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001761 psa_status_t status;
1762
1763 /* PSA has its own RNG */
1764 (void) f_rng;
1765 (void) p_rng;
1766
Valerio Setti4f387ef2023-05-02 14:15:59 +02001767 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001769 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 type = psa_get_key_type(&attributes);
1773 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001776 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Valerio Setti4657f102023-06-21 13:55:16 +02001777 } else {
1778 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1779 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001780
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001781 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001782 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 sig, sig_size, sig_len);
1784 if (status != PSA_SUCCESS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001786 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Valerio Setti4657f102023-06-21 13:55:16 +02001787 } else {
1788 return PSA_PK_TO_MBEDTLS_ERR(status);
1789 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001790 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001791
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001792 return 0;
Valerio Setti574a00b2023-06-21 19:47:37 +02001793#else /* !MBEDTLS_RSA_C */
1794 ((void) pk);
1795 ((void) md_alg);
1796 ((void) hash);
1797 ((void) hash_len);
1798 ((void) sig);
1799 ((void) sig_size);
1800 ((void) sig_len);
1801 ((void) f_rng);
1802 ((void) p_rng);
1803 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti4657f102023-06-21 13:55:16 +02001804#endif /* !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001805}
1806
Valerio Setti76d0f962023-06-23 13:32:54 +02001807const mbedtls_pk_info_t mbedtls_rsa_opaque_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001808 .type = MBEDTLS_PK_OPAQUE,
1809 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001810 .get_bitlen = opaque_get_bitlen,
1811 .can_do = rsa_opaque_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001812 .verify_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001813 .sign_func = rsa_opaque_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001814#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1815 .verify_rs_func = NULL,
1816 .sign_rs_func = NULL,
1817 .rs_alloc_func = NULL,
1818 .rs_free_func = NULL,
1819#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001820#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001821 .decrypt_func = rsa_opaque_decrypt,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001822#else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001823 .decrypt_func = NULL,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001824#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001825 .encrypt_func = NULL,
1826 .check_pair_func = NULL,
1827 .ctx_alloc_func = NULL,
1828 .ctx_free_func = NULL,
1829 .debug_func = NULL,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001830};
1831
1832#endif /* MBEDTLS_USE_PSA_CRYPTO */
1833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#endif /* MBEDTLS_PK_C */