blob: 0fb3c42176accc79a81acb947bfd92884101144a [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 Rodgman16799db2023-11-02 19:47:20 +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
Dave Rodgman2eab4622023-10-05 13:30:37 +0100199#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
201 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
202 }
Dave Rodgman2eab4622023-10-05 13:30:37 +0100203#endif
Neil Armstrong52f41f82022-02-22 15:30:24 +0100204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 if (sig_len < rsa_len) {
206 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
207 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100208
Neil Armstrongea54dbe2022-03-14 09:26:48 +0100209 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100210 * re-construct one to make it happy */
Neil Armstrong253e9e72022-03-16 15:32:23 +0100211 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200212 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
214 if (key_len <= 0) {
215 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
216 }
Neil Armstrong52f41f82022-02-22 15:30:24 +0100217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
219 psa_set_key_algorithm(&attributes, psa_alg_md);
220 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 status = psa_import_key(&attributes,
223 buf + sizeof(buf) - key_len, key_len,
224 &key_id);
225 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500226 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100227 goto cleanup;
228 }
229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 status = psa_verify_hash(key_id, psa_alg_md, hash, hash_len,
231 sig, sig_len);
232 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500233 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong52f41f82022-02-22 15:30:24 +0100234 goto cleanup;
235 }
236 ret = 0;
237
238cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 status = psa_destroy_key(key_id);
240 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500241 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 }
Neil Armstronga33280a2022-02-24 15:17:47 +0100243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return ret;
Neil Armstrong52f41f82022-02-22 15:30:24 +0100245}
Valerio Setti5c26b302023-06-21 19:47:01 +0200246#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200247static int rsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 const unsigned char *hash, size_t hash_len,
249 const unsigned char *sig, size_t sig_len)
Neil Armstrong52f41f82022-02-22 15:30:24 +0100250{
Janos Follath24eed8d2019-11-22 13:21:35 +0000251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
valerio38992cb2023-04-20 09:56:30 +0200252 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 size_t rsa_len = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200254
Dave Rodgman02a53d72023-09-28 17:17:07 +0100255#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
257 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
258 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100259#endif
Andres AG72849872017-01-19 11:24:33 +0000260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (sig_len < rsa_len) {
262 return MBEDTLS_ERR_RSA_VERIFY_FAILED;
263 }
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 if ((ret = mbedtls_rsa_pkcs1_verify(rsa, md_alg,
266 (unsigned int) hash_len,
267 hash, sig)) != 0) {
268 return ret;
269 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200270
Gilles Peskine5114d3e2018-03-30 07:12:15 +0200271 /* The buffer contains a valid signature followed by extra data.
272 * We have a special error code for that so that so that callers can
273 * use mbedtls_pk_verify() to check "Does the buffer start with a
274 * valid signature?" and not just "Does the buffer contain a valid
275 * signature?". */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (sig_len > rsa_len) {
277 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
278 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return 0;
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200281}
Valerio Setti5c26b302023-06-21 19:47:01 +0200282#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200283
Jerry Yub02ee182022-03-16 10:30:41 +0800284#if defined(MBEDTLS_PSA_CRYPTO_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100285int mbedtls_pk_psa_rsa_sign_ext(psa_algorithm_t alg,
286 mbedtls_rsa_context *rsa_ctx,
287 const unsigned char *hash, size_t hash_len,
288 unsigned char *sig, size_t sig_size,
289 size_t *sig_len)
Neil Armstrong98545682022-02-22 16:12:51 +0100290{
Neil Armstrong98545682022-02-22 16:12:51 +0100291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
292 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
293 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
294 psa_status_t status;
295 mbedtls_pk_context key;
296 int key_len;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530297 unsigned char *buf = NULL;
298 buf = mbedtls_calloc(1, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
299 if (buf == NULL) {
300 return MBEDTLS_ERR_PK_ALLOC_FAILED;
301 }
Neil Armstrong98545682022-02-22 16:12:51 +0100302 mbedtls_pk_info_t pk_info = mbedtls_rsa_info;
Neil Armstrong98545682022-02-22 16:12:51 +0100303
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 *sig_len = mbedtls_rsa_get_len(rsa_ctx);
305 if (sig_size < *sig_len) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530306 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
308 }
Neil Armstrong98545682022-02-22 16:12:51 +0100309
Neil Armstronge4f28682022-02-24 15:41:39 +0100310 /* mbedtls_pk_write_key_der() expects a full PK context;
Neil Armstrong98545682022-02-22 16:12:51 +0100311 * re-construct one to make it happy */
312 key.pk_info = &pk_info;
Jerry Yue010de42022-03-23 11:45:55 +0800313 key.pk_ctx = rsa_ctx;
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530314 key_len = mbedtls_pk_write_key_der(&key, buf, MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES);
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (key_len <= 0) {
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530316 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
318 }
319 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
320 psa_set_key_algorithm(&attributes, alg);
321 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
Neil Armstrong98545682022-02-22 16:12:51 +0100322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 status = psa_import_key(&attributes,
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530324 buf + MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES - key_len, key_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 &key_id);
326 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500327 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100328 goto cleanup;
329 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 status = psa_sign_hash(key_id, alg, hash, hash_len,
331 sig, sig_size, sig_len);
332 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500333 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong98545682022-02-22 16:12:51 +0100334 goto cleanup;
335 }
336
337 ret = 0;
338
339cleanup:
Sarvesh Bodakhe430a4f32023-07-27 14:51:25 +0530340 mbedtls_free(buf);
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 status = psa_destroy_key(key_id);
342 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500343 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 }
345 return ret;
Neil Armstrong98545682022-02-22 16:12:51 +0100346}
Jerry Yu406cf272022-03-22 11:33:42 +0800347#endif /* MBEDTLS_PSA_CRYPTO_C */
Jerry Yu1d172a32022-03-12 19:12:05 +0800348
349#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200350static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 const unsigned char *hash, size_t hash_len,
352 unsigned char *sig, size_t sig_size, size_t *sig_len,
353 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Jerry Yu1d172a32022-03-12 19:12:05 +0800354{
Jerry Yu1d172a32022-03-12 19:12:05 +0800355 ((void) f_rng);
356 ((void) p_rng);
Jerry Yu1d172a32022-03-12 19:12:05 +0800357
Jerry Yubd1b3272022-03-24 13:05:20 +0800358 psa_algorithm_t psa_md_alg;
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200359 psa_md_alg = mbedtls_md_psa_alg_from_type(md_alg);
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 if (psa_md_alg == 0) {
361 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
362 }
Jerry Yu1d172a32022-03-12 19:12:05 +0800363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 return mbedtls_pk_psa_rsa_sign_ext(PSA_ALG_RSA_PKCS1V15_SIGN(
365 psa_md_alg),
valerio38992cb2023-04-20 09:56:30 +0200366 pk->pk_ctx, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 sig, sig_size, sig_len);
Jerry Yu1d172a32022-03-12 19:12:05 +0800368}
Valerio Setti5c26b302023-06-21 19:47:01 +0200369#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200370static int rsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 const unsigned char *hash, size_t hash_len,
372 unsigned char *sig, size_t sig_size, size_t *sig_len,
373 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200374{
valerio38992cb2023-04-20 09:56:30 +0200375 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100376
Dave Rodgman02a53d72023-09-28 17:17:07 +0100377#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (md_alg == MBEDTLS_MD_NONE && UINT_MAX < hash_len) {
379 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
380 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100381#endif
Andres AG72849872017-01-19 11:24:33 +0000382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 *sig_len = mbedtls_rsa_get_len(rsa);
384 if (sig_size < *sig_len) {
385 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
386 }
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200387
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 return mbedtls_rsa_pkcs1_sign(rsa, f_rng, p_rng,
389 md_alg, (unsigned int) hash_len,
390 hash, sig);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200391}
Valerio Setti5c26b302023-06-21 19:47:01 +0200392#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +0200393
Neil Armstrong18f43c72022-02-09 15:32:45 +0100394#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200395static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 const unsigned char *input, size_t ilen,
397 unsigned char *output, size_t *olen, size_t osize,
398 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong18f43c72022-02-09 15:32:45 +0100399{
valerio38992cb2023-04-20 09:56:30 +0200400 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100401 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
402 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
403 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
404 psa_status_t status;
405 mbedtls_pk_context key;
406 int key_len;
Neil Armstrongb556a422022-02-25 08:58:12 +0100407 unsigned char buf[MBEDTLS_PK_RSA_PRV_DER_MAX_BYTES];
Neil Armstrong18f43c72022-02-09 15:32:45 +0100408
409 ((void) f_rng);
410 ((void) p_rng);
411
412#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
414 return MBEDTLS_ERR_RSA_INVALID_PADDING;
415 }
Neil Armstrong8e805042022-03-16 15:30:31 +0100416#endif /* !MBEDTLS_RSA_ALT */
Neil Armstrong18f43c72022-02-09 15:32:45 +0100417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if (ilen != mbedtls_rsa_get_len(rsa)) {
419 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
420 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100421
422 /* mbedtls_pk_write_key_der() expects a full PK context;
423 * re-construct one to make it happy */
Neil Armstrong6b03a3d2022-03-16 15:31:07 +0100424 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200425 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 key_len = mbedtls_pk_write_key_der(&key, buf, sizeof(buf));
427 if (key_len <= 0) {
428 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
429 }
Neil Armstrong18f43c72022-02-09 15:32:45 +0100430
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_KEY_PAIR);
432 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
433 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 status = psa_import_key(&attributes,
436 buf + sizeof(buf) - key_len, key_len,
437 &key_id);
438 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500439 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100440 goto cleanup;
441 }
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 status = psa_asymmetric_decrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
444 input, ilen,
445 NULL, 0,
446 output, osize, olen);
447 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500448 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong18f43c72022-02-09 15:32:45 +0100449 goto cleanup;
450 }
451
452 ret = 0;
453
454cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 mbedtls_platform_zeroize(buf, sizeof(buf));
456 status = psa_destroy_key(key_id);
457 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500458 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 }
Neil Armstrongf1b564b2022-02-24 15:17:47 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 return ret;
Neil Armstrong18f43c72022-02-09 15:32:45 +0100462}
Valerio Setti5c26b302023-06-21 19:47:01 +0200463#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200464static int rsa_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 const unsigned char *input, size_t ilen,
466 unsigned char *output, size_t *olen, size_t osize,
467 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200468{
valerio38992cb2023-04-20 09:56:30 +0200469 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Hanno Becker6a1e7e52017-08-22 13:55:00 +0100470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (ilen != mbedtls_rsa_get_len(rsa)) {
472 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
473 }
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 return mbedtls_rsa_pkcs1_decrypt(rsa, f_rng, p_rng,
476 olen, input, output, osize);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200477}
Valerio Setti5c26b302023-06-21 19:47:01 +0200478#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200479
Neil Armstrong96a16a42022-02-10 10:40:11 +0100480#if defined(MBEDTLS_USE_PSA_CRYPTO)
valerio38992cb2023-04-20 09:56:30 +0200481static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 const unsigned char *input, size_t ilen,
483 unsigned char *output, size_t *olen, size_t osize,
484 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstrong96a16a42022-02-10 10:40:11 +0100485{
valerio38992cb2023-04-20 09:56:30 +0200486 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100487 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
488 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
489 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
490 psa_status_t status;
491 mbedtls_pk_context key;
492 int key_len;
Neil Armstrongdeb4bfb2022-02-25 08:58:12 +0100493 unsigned char buf[MBEDTLS_PK_RSA_PUB_DER_MAX_BYTES];
Neil Armstrong96a16a42022-02-10 10:40:11 +0100494
495 ((void) f_rng);
496 ((void) p_rng);
497
498#if !defined(MBEDTLS_RSA_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (rsa->padding != MBEDTLS_RSA_PKCS_V15) {
500 return MBEDTLS_ERR_RSA_INVALID_PADDING;
501 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100502#endif
503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (mbedtls_rsa_get_len(rsa) > osize) {
505 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
506 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100507
Neil Armstrongac014ca2022-02-24 15:27:54 +0100508 /* mbedtls_pk_write_pubkey_der() expects a full PK context;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100509 * re-construct one to make it happy */
Neil Armstrongda1d80d2022-03-16 15:36:32 +0100510 key.pk_info = &mbedtls_rsa_info;
valerio38992cb2023-04-20 09:56:30 +0200511 key.pk_ctx = rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 key_len = mbedtls_pk_write_pubkey_der(&key, buf, sizeof(buf));
513 if (key_len <= 0) {
514 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
515 }
Neil Armstrong96a16a42022-02-10 10:40:11 +0100516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
518 psa_set_key_algorithm(&attributes, PSA_ALG_RSA_PKCS1V15_CRYPT);
519 psa_set_key_type(&attributes, PSA_KEY_TYPE_RSA_PUBLIC_KEY);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 status = psa_import_key(&attributes,
522 buf + sizeof(buf) - key_len, key_len,
523 &key_id);
524 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500525 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100526 goto cleanup;
527 }
528
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 status = psa_asymmetric_encrypt(key_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
530 input, ilen,
531 NULL, 0,
532 output, osize, olen);
533 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500534 ret = PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Neil Armstrong96a16a42022-02-10 10:40:11 +0100535 goto cleanup;
536 }
537
538 ret = 0;
539
540cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 status = psa_destroy_key(key_id);
542 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500543 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 }
Neil Armstrong7dd3b202022-02-24 15:29:18 +0100545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return ret;
Neil Armstrong96a16a42022-02-10 10:40:11 +0100547}
Valerio Setti5c26b302023-06-21 19:47:01 +0200548#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200549static int rsa_encrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 const unsigned char *input, size_t ilen,
551 unsigned char *output, size_t *olen, size_t osize,
552 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200553{
valerio38992cb2023-04-20 09:56:30 +0200554 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 *olen = mbedtls_rsa_get_len(rsa);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200556
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (*olen > osize) {
558 return MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
559 }
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +0100560
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return mbedtls_rsa_pkcs1_encrypt(rsa, f_rng, p_rng,
562 ilen, input, output);
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200563}
Valerio Setti5c26b302023-06-21 19:47:01 +0200564#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnarda2d3f222013-08-21 11:51:08 +0200565
valerio38992cb2023-04-20 09:56:30 +0200566static int rsa_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 int (*f_rng)(void *, unsigned char *, size_t),
568 void *p_rng)
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100569{
Manuel Pégourié-Gonnard39be1412021-06-15 11:29:26 +0200570 (void) f_rng;
571 (void) p_rng;
valerio38992cb2023-04-20 09:56:30 +0200572 return mbedtls_rsa_check_pub_priv((const mbedtls_rsa_context *) pub->pk_ctx,
573 (const mbedtls_rsa_context *) prv->pk_ctx);
Manuel Pégourié-Gonnard70bdadf2014-11-06 16:51:20 +0100574}
575
Gilles Peskine449bd832023-01-11 14:50:10 +0100576static void *rsa_alloc_wrap(void)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200577{
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (ctx != NULL) {
581 mbedtls_rsa_init((mbedtls_rsa_context *) ctx);
582 }
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 return ctx;
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200585}
586
Gilles Peskine449bd832023-01-11 14:50:10 +0100587static void rsa_free_wrap(void *ctx)
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200588{
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 mbedtls_rsa_free((mbedtls_rsa_context *) ctx);
590 mbedtls_free(ctx);
Manuel Pégourié-Gonnard765db072013-08-14 15:00:27 +0200591}
592
valerio38992cb2023-04-20 09:56:30 +0200593static void rsa_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200594{
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200595#if defined(MBEDTLS_RSA_ALT)
596 /* Not supported */
valerio38992cb2023-04-20 09:56:30 +0200597 (void) pk;
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200598 (void) items;
599#else
valerio38992cb2023-04-20 09:56:30 +0200600 mbedtls_rsa_context *rsa = (mbedtls_rsa_context *) pk->pk_ctx;
601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200603 items->name = "rsa.N";
valerio38992cb2023-04-20 09:56:30 +0200604 items->value = &(rsa->N);
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200605
606 items++;
607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 items->type = MBEDTLS_PK_DEBUG_MPI;
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200609 items->name = "rsa.E";
valerio38992cb2023-04-20 09:56:30 +0200610 items->value = &(rsa->E);
Gilles Peskine85b1bc62021-05-25 09:20:26 +0200611#endif
Manuel Pégourié-Gonnardc6ac8872013-08-14 18:04:18 +0200612}
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614const mbedtls_pk_info_t mbedtls_rsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +0200615 .type = MBEDTLS_PK_RSA,
616 .name = "RSA",
617 .get_bitlen = rsa_get_bitlen,
618 .can_do = rsa_can_do,
619 .verify_func = rsa_verify_wrap,
620 .sign_func = rsa_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +0200621#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
622 .verify_rs_func = NULL,
623 .sign_rs_func = NULL,
624 .rs_alloc_func = NULL,
625 .rs_free_func = NULL,
626#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +0200627 .decrypt_func = rsa_decrypt_wrap,
628 .encrypt_func = rsa_encrypt_wrap,
629 .check_pair_func = rsa_check_pair_wrap,
630 .ctx_alloc_func = rsa_alloc_wrap,
631 .ctx_free_func = rsa_free_wrap,
632 .debug_func = rsa_debug,
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200633};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd73b3c12013-08-12 17:06:05 +0200635
Valerio Setti81d75122023-06-14 14:49:33 +0200636#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Manuel Pégourié-Gonnard835eb592013-08-12 18:51:26 +0200637/*
638 * Generic EC key
639 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100640static int eckey_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200641{
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 return type == MBEDTLS_PK_ECKEY ||
643 type == MBEDTLS_PK_ECKEY_DH ||
644 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnardf18c3e02013-08-12 18:41:18 +0200645}
646
valerio38992cb2023-04-20 09:56:30 +0200647static size_t eckey_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200648{
Valerio Settia1b8af62023-05-17 15:34:57 +0200649#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
650 return pk->ec_bits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200651#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +0200652 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
653 return ecp->grp.pbits;
Valerio Setti5c26b302023-06-21 19:47:01 +0200654#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Manuel Pégourié-Gonnardf8c948a2013-08-12 19:45:32 +0200655}
656
Valerio Setti1cdddac2023-02-02 13:55:57 +0100657#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400658#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400659/*
Andrzej Kurek9241d182018-11-20 05:04:35 -0500660 * An ASN.1 encoded signature is a sequence of two ASN.1 integers. Parse one of
661 * those integers and convert it to the fixed-length encoding expected by PSA.
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500662 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100663static int extract_ecdsa_sig_int(unsigned char **from, const unsigned char *end,
664 unsigned char *to, size_t to_len)
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500665{
Janos Follath24eed8d2019-11-22 13:21:35 +0000666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500667 size_t unpadded_len, padding_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 if ((ret = mbedtls_asn1_get_tag(from, end, &unpadded_len,
670 MBEDTLS_ASN1_INTEGER)) != 0) {
671 return ret;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500672 }
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 while (unpadded_len > 0 && **from == 0x00) {
675 (*from)++;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500676 unpadded_len--;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500677 }
678
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 if (unpadded_len > to_len || unpadded_len == 0) {
680 return MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
681 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500682
Andrzej Kurek9241d182018-11-20 05:04:35 -0500683 padding_len = to_len - unpadded_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 memset(to, 0x00, padding_len);
685 memcpy(to + padding_len, *from, unpadded_len);
686 (*from) += unpadded_len;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500687
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return 0;
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500689}
690
691/*
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400692 * Convert a signature from an ASN.1 sequence of two integers
Andrzej Kurek9241d182018-11-20 05:04:35 -0500693 * to a raw {r,s} buffer. Note: the provided sig buffer must be at least
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500694 * twice as big as int_size.
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400695 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696static int extract_ecdsa_sig(unsigned char **p, const unsigned char *end,
697 unsigned char *sig, size_t int_size)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400698{
Janos Follath24eed8d2019-11-22 13:21:35 +0000699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek9241d182018-11-20 05:04:35 -0500700 size_t tmp_size;
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if ((ret = mbedtls_asn1_get_tag(p, end, &tmp_size,
703 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
704 return ret;
705 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400706
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500707 /* Extract r */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if ((ret = extract_ecdsa_sig_int(p, end, sig, int_size)) != 0) {
709 return ret;
710 }
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500711 /* Extract s */
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 if ((ret = extract_ecdsa_sig_int(p, end, sig + int_size, int_size)) != 0) {
713 return ret;
714 }
Andrzej Kurek3f864c22018-11-07 09:30:50 -0500715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 return 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400717}
718
Valerio Setti76d0f962023-06-23 13:32:54 +0200719/* Common helper for ECDSA verify using PSA functions. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200720static int ecdsa_verify_psa(unsigned char *key, size_t key_len,
721 psa_ecc_family_t curve, size_t curve_bits,
722 const unsigned char *hash, size_t hash_len,
723 const unsigned char *sig, size_t sig_len)
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400724{
Janos Follath24eed8d2019-11-22 13:21:35 +0000725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskined2d45c12019-05-27 14:53:13 +0200726 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Andrzej Kurek03e01462022-01-03 12:53:24 +0100727 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
Valerio Settia1b8af62023-05-17 15:34:57 +0200728 psa_algorithm_t psa_sig_md = PSA_ALG_ECDSA_ANY;
Valerio Settied7d6af2023-06-21 15:42:21 +0200729 size_t signature_len = PSA_ECDSA_SIGNATURE_SIZE(curve_bits);
730 unsigned char extracted_sig[PSA_VENDOR_ECDSA_SIGNATURE_MAX_SIZE];
731 unsigned char *p;
732 psa_status_t status;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (curve == 0) {
735 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
736 }
Andrzej Kurekb3d1b122018-11-07 08:18:52 -0500737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve));
739 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
740 psa_set_key_algorithm(&attributes, psa_sig_md);
Andrzej Kurek2349c4d2019-01-08 09:36:01 -0500741
Valerio Settied7d6af2023-06-21 15:42:21 +0200742 status = psa_import_key(&attributes, key, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500744 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400745 goto cleanup;
746 }
747
Valerio Settied7d6af2023-06-21 15:42:21 +0200748 if (signature_len > sizeof(extracted_sig)) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500749 ret = MBEDTLS_ERR_PK_BAD_INPUT_DATA;
750 goto cleanup;
751 }
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500752
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 p = (unsigned char *) sig;
Valerio Settia1b8af62023-05-17 15:34:57 +0200754 /* extract_ecdsa_sig's last parameter is the size
Valerio Settif57007d2023-05-19 13:54:39 +0200755 * of each integer to be parsed, so it's actually half
Valerio Settia1b8af62023-05-17 15:34:57 +0200756 * the size of the signature. */
Valerio Settied7d6af2023-06-21 15:42:21 +0200757 if ((ret = extract_ecdsa_sig(&p, sig + sig_len, extracted_sig,
Valerio Settia1b8af62023-05-17 15:34:57 +0200758 signature_len/2)) != 0) {
Andrzej Kurekb6016c52018-11-19 17:41:58 -0500759 goto cleanup;
760 }
761
Valerio Settied7d6af2023-06-21 15:42:21 +0200762 status = psa_verify_hash(key_id, psa_sig_md, hash, hash_len,
763 extracted_sig, signature_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500765 ret = PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 goto cleanup;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400767 }
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (p != sig + sig_len) {
Andrzej Kurekad5d5812018-11-20 07:59:18 -0500770 ret = MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
771 goto cleanup;
772 }
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400773 ret = 0;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400774
Andrzej Kurekb7b04782018-11-19 17:01:16 -0500775cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 status = psa_destroy_key(key_id);
777 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500778 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +0100779 }
Neil Armstrong9dccd862022-02-24 15:33:13 +0100780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 return ret;
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400782}
Valerio Settied7d6af2023-06-21 15:42:21 +0200783
Valerio Setti76d0f962023-06-23 13:32:54 +0200784static int ecdsa_opaque_verify_wrap(mbedtls_pk_context *pk,
785 mbedtls_md_type_t md_alg,
786 const unsigned char *hash, size_t hash_len,
787 const unsigned char *sig, size_t sig_len)
Valerio Settie7730772023-06-21 16:58:40 +0200788{
789 (void) md_alg;
790 unsigned char key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
791 size_t key_len;
792 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
793 psa_ecc_family_t curve;
794 size_t curve_bits;
795 psa_status_t status;
796
797 status = psa_get_key_attributes(pk->priv_id, &key_attr);
798 if (status != PSA_SUCCESS) {
799 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
800 }
801 curve = PSA_KEY_TYPE_ECC_GET_FAMILY(psa_get_key_type(&key_attr));
802 curve_bits = psa_get_key_bits(&key_attr);
803 psa_reset_key_attributes(&key_attr);
804
805 status = psa_export_public_key(pk->priv_id, key, sizeof(key), &key_len);
806 if (status != PSA_SUCCESS) {
807 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
808 }
809
810 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
811 hash, hash_len, sig, sig_len);
812}
813
Valerio Settied7d6af2023-06-21 15:42:21 +0200814#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
815static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
816 mbedtls_md_type_t md_alg,
817 const unsigned char *hash, size_t hash_len,
818 const unsigned char *sig, size_t sig_len)
819{
820 (void) md_alg;
821 psa_ecc_family_t curve = pk->ec_family;
822 size_t curve_bits = pk->ec_bits;
823
824 return ecdsa_verify_psa(pk->pub_raw, pk->pub_raw_len, curve, curve_bits,
825 hash, hash_len, sig, sig_len);
826}
827#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
828static int ecdsa_verify_wrap(mbedtls_pk_context *pk,
829 mbedtls_md_type_t md_alg,
830 const unsigned char *hash, size_t hash_len,
831 const unsigned char *sig, size_t sig_len)
832{
833 (void) md_alg;
834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
835 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
836 unsigned char key[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
837 size_t key_len;
838 size_t curve_bits;
839 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
840
841 ret = mbedtls_ecp_point_write_binary(&ctx->grp, &ctx->Q,
842 MBEDTLS_ECP_PF_UNCOMPRESSED,
843 &key_len, key, sizeof(key));
844 if (ret != 0) {
845 return ret;
846 }
847
848 return ecdsa_verify_psa(key, key_len, curve, curve_bits,
849 hash, hash_len, sig, sig_len);
850}
851#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400852#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +0200853static int ecdsa_verify_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 const unsigned char *hash, size_t hash_len,
855 const unsigned char *sig, size_t sig_len)
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200856{
Janos Follath24eed8d2019-11-22 13:21:35 +0000857 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf73da022013-08-17 14:36:32 +0200858 ((void) md_alg);
859
valerio38992cb2023-04-20 09:56:30 +0200860 ret = mbedtls_ecdsa_read_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +0100861 hash, hash_len, sig, sig_len);
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200862
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
864 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
865 }
Manuel Pégourié-Gonnard2abed842014-04-08 12:40:15 +0200866
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 return ret;
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200868}
Andrzej Kurek8b036a62018-10-31 05:16:46 -0400869#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +0100870#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +0200871
Valerio Setti1cdddac2023-02-02 13:55:57 +0100872#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Neil Armstronge9606902022-02-09 14:23:00 +0100873#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstrong15021652022-03-01 10:14:17 +0100874/*
875 * Simultaneously convert and move raw MPI from the beginning of a buffer
876 * to an ASN.1 MPI at the end of the buffer.
877 * See also mbedtls_asn1_write_mpi().
878 *
879 * p: pointer to the end of the output buffer
880 * start: start of the output buffer, and also of the mpi to write at the end
881 * n_len: length of the mpi to read from start
882 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100883static int asn1_write_mpibuf(unsigned char **p, unsigned char *start,
884 size_t n_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100885{
886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
887 size_t len = 0;
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if ((size_t) (*p - start) < n_len) {
890 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
891 }
Neil Armstrong15021652022-03-01 10:14:17 +0100892
893 len = n_len;
894 *p -= len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100895 memmove(*p, start, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100896
897 /* ASN.1 DER encoding requires minimal length, so skip leading 0s.
898 * Neither r nor s should be 0, but as a failsafe measure, still detect
899 * that rather than overflowing the buffer in case of a PSA error. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 while (len > 0 && **p == 0x00) {
Neil Armstrong15021652022-03-01 10:14:17 +0100901 ++(*p);
902 --len;
903 }
904
905 /* this is only reached if the signature was invalid */
Gilles Peskine449bd832023-01-11 14:50:10 +0100906 if (len == 0) {
907 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
908 }
Neil Armstrong15021652022-03-01 10:14:17 +0100909
910 /* if the msb is 1, ASN.1 requires that we prepend a 0.
911 * Neither r nor s can be 0, so we can assume len > 0 at all times. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 if (**p & 0x80) {
913 if (*p - start < 1) {
914 return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL;
915 }
Neil Armstrong15021652022-03-01 10:14:17 +0100916
917 *--(*p) = 0x00;
918 len += 1;
919 }
920
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
922 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
923 MBEDTLS_ASN1_INTEGER));
Neil Armstrong15021652022-03-01 10:14:17 +0100924
Gilles Peskine449bd832023-01-11 14:50:10 +0100925 return (int) len;
Neil Armstrong15021652022-03-01 10:14:17 +0100926}
927
928/* Transcode signature from PSA format to ASN.1 sequence.
929 * See ecdsa_signature_to_asn1 in ecdsa.c, but with byte buffers instead of
930 * MPIs, and in-place.
931 *
932 * [in/out] sig: the signature pre- and post-transcoding
933 * [in/out] sig_len: signature length pre- and post-transcoding
934 * [int] buf_len: the available size the in/out buffer
935 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100936static int pk_ecdsa_sig_asn1_from_psa(unsigned char *sig, size_t *sig_len,
937 size_t buf_len)
Neil Armstrong15021652022-03-01 10:14:17 +0100938{
939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
940 size_t len = 0;
941 const size_t rs_len = *sig_len / 2;
942 unsigned char *p = sig + buf_len;
943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig + rs_len, rs_len));
945 MBEDTLS_ASN1_CHK_ADD(len, asn1_write_mpibuf(&p, sig, rs_len));
Neil Armstrong15021652022-03-01 10:14:17 +0100946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, sig, len));
948 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, sig,
949 MBEDTLS_ASN1_CONSTRUCTED |
950 MBEDTLS_ASN1_SEQUENCE));
Neil Armstrong15021652022-03-01 10:14:17 +0100951
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 memmove(sig, p, len);
Neil Armstrong15021652022-03-01 10:14:17 +0100953 *sig_len = len;
954
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 return 0;
Neil Armstrong15021652022-03-01 10:14:17 +0100956}
Neil Armstronge9606902022-02-09 14:23:00 +0100957
Valerio Setti4ac2c182023-12-05 07:59:01 +0100958/* Common helper for ECDSA sign using PSA functions.
959 * Instead of extracting key's properties in order to check which kind of ECDSA
960 * signature it supports, we try both deterministic and non-deterministic.
961 */
Valerio Setti884c1ec2023-06-23 12:09:13 +0200962static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg,
Valerio Setti4657f102023-06-21 13:55:16 +0200963 const unsigned char *hash, size_t hash_len,
964 unsigned char *sig, size_t sig_size, size_t *sig_len)
965{
966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
967 psa_status_t status;
Valerio Setti884c1ec2023-06-23 12:09:13 +0200968
Valerio Setti4ac2c182023-12-05 07:59:01 +0100969 status = psa_sign_hash(key_id,
970 PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)),
971 hash, hash_len, sig, sig_size, sig_len);
972 if (status == PSA_SUCCESS) {
973 goto done;
974 } else if (status != PSA_ERROR_NOT_PERMITTED) {
Valerio Setti884c1ec2023-06-23 12:09:13 +0200975 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
976 }
Valerio Setti884c1ec2023-06-23 12:09:13 +0200977
Valerio Setti4ac2c182023-12-05 07:59:01 +0100978 status = psa_sign_hash(key_id,
979 PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg)),
980 hash, hash_len, sig, sig_size, sig_len);
Valerio Setti4657f102023-06-21 13:55:16 +0200981 if (status != PSA_SUCCESS) {
982 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
983 }
984
Valerio Setti4ac2c182023-12-05 07:59:01 +0100985done:
Valerio Setti4657f102023-06-21 13:55:16 +0200986 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);
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +02001026 psa_algorithm_t psa_hash = mbedtls_md_psa_alg_from_type(md_alg);
1027 psa_algorithm_t psa_sig_md = MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(psa_hash);
Neil Armstronge9606902022-02-09 14:23:00 +01001028 ((void) f_rng);
1029 ((void) p_rng);
1030
Gilles Peskine449bd832023-01-11 14:50:10 +01001031 if (curve == 0) {
1032 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1033 }
Neil Armstronge9606902022-02-09 14:23:00 +01001034
Gilles Peskine13caa942022-10-04 22:59:26 +02001035 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +01001036 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 }
Gilles Peskine13caa942022-10-04 22:59:26 +02001038 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +01001040 goto cleanup;
1041 }
1042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1044 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
1045 psa_set_key_algorithm(&attributes, psa_sig_md);
1046
Valerio Setti4657f102023-06-21 13:55:16 +02001047 status = psa_import_key(&attributes, buf, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001049 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001050 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +01001051 }
1052
Valerio Setti884c1ec2023-06-23 12:09:13 +02001053 ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len);
Neil Armstronge9606902022-02-09 14:23:00 +01001054
1055cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 mbedtls_platform_zeroize(buf, sizeof(buf));
1057 status = psa_destroy_key(key_id);
1058 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001059 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001060 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001061
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +01001063}
Valerio Setti4657f102023-06-21 13:55:16 +02001064#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001065#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +02001066static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 const unsigned char *hash, size_t hash_len,
1068 unsigned char *sig, size_t sig_size, size_t *sig_len,
1069 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001070{
valerio38992cb2023-04-20 09:56:30 +02001071 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 md_alg, hash, hash_len,
1073 sig, sig_size, sig_len,
1074 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001075}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001076#endif /* MBEDTLS_USE_PSA_CRYPTO */
1077#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001078
Valerio Setti80d07982023-02-08 13:49:17 +01001079#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001080/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001081static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001082 const unsigned char *hash, size_t hash_len,
1083 const unsigned char *sig, size_t sig_len,
1084 void *rs_ctx);
1085
valerio38992cb2023-04-20 09:56:30 +02001086static int ecdsa_sign_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 unsigned char *sig, size_t sig_size, size_t *sig_len,
1089 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1090 void *rs_ctx);
1091
1092/*
1093 * Restart context for ECDSA operations with ECKEY context
1094 *
1095 * We need to store an actual ECDSA context, as we need to pass the same to
1096 * the underlying ecdsa function, so we can't create it on the fly every time.
1097 */
1098typedef struct {
1099 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1100 mbedtls_ecdsa_context ecdsa_ctx;
1101} eckey_restart_ctx;
1102
1103static void *eckey_rs_alloc(void)
1104{
1105 eckey_restart_ctx *rs_ctx;
1106
1107 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1108
1109 if (ctx != NULL) {
1110 rs_ctx = ctx;
1111 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1112 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1113 }
1114
1115 return ctx;
1116}
1117
1118static void eckey_rs_free(void *ctx)
1119{
1120 eckey_restart_ctx *rs_ctx;
1121
1122 if (ctx == NULL) {
1123 return;
1124 }
1125
1126 rs_ctx = ctx;
1127 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1128 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1129
1130 mbedtls_free(ctx);
1131}
1132
valerio38992cb2023-04-20 09:56:30 +02001133static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001134 const unsigned char *hash, size_t hash_len,
1135 const unsigned char *sig, size_t sig_len,
1136 void *rs_ctx)
1137{
1138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1139 eckey_restart_ctx *rs = rs_ctx;
1140
1141 /* Should never happen */
1142 if (rs == NULL) {
1143 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1144 }
1145
1146 /* set up our own sub-context if needed (that is, on first run) */
1147 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001148 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001149 }
1150
valerio38992cb2023-04-20 09:56:30 +02001151 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001152 md_alg, hash, hash_len,
1153 sig, sig_len, &rs->ecdsa_rs));
1154
1155cleanup:
1156 return ret;
1157}
1158
valerio38992cb2023-04-20 09:56:30 +02001159static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001160 const unsigned char *hash, size_t hash_len,
1161 unsigned char *sig, size_t sig_size, size_t *sig_len,
1162 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1163 void *rs_ctx)
1164{
1165 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1166 eckey_restart_ctx *rs = rs_ctx;
1167
1168 /* Should never happen */
1169 if (rs == NULL) {
1170 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1171 }
1172
1173 /* set up our own sub-context if needed (that is, on first run) */
1174 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001175 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001176 }
1177
valerio38992cb2023-04-20 09:56:30 +02001178 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001179 hash, hash_len, sig, sig_size, sig_len,
1180 f_rng, p_rng, &rs->ecdsa_rs));
1181
1182cleanup:
1183 return ret;
1184}
Valerio Setti80d07982023-02-08 13:49:17 +01001185#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001186
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001187#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settibb7603a2023-06-21 18:34:54 +02001188#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerio38992cb2023-04-20 09:56:30 +02001189static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001190{
Valerio Setti9efa8c42023-05-19 13:27:30 +02001191 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001192 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001193 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001194 size_t prv_key_len;
Valerio Settiae8c6282023-05-18 18:57:57 +02001195 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001196
1197 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1198 &prv_key_len);
1199 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1200 if (ret != 0) {
1201 return ret;
1202 }
1203
1204 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1205 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1206 }
Valerio Settibb7603a2023-06-21 18:34:54 +02001207
1208 return 0;
1209}
1210#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1211static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
1212{
1213 psa_status_t status;
1214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1215 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1216 size_t prv_key_len;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001217 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001218 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1219 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001220 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1221 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001222 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001223 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001224 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +02001225 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001226
Valerio Settia7cb8452023-05-22 18:39:43 +02001227 if (curve == 0) {
1228 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1229 }
1230
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001231 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1232 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1233
Valerio Settia1b8af62023-05-17 15:34:57 +02001234 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1235 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001236 if (ret != 0) {
Valerio Setti35d1dac2023-06-30 18:04:16 +02001237 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001238 return ret;
1239 }
1240
1241 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti35d1dac2023-06-30 18:04:16 +02001242 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti1df94f82023-04-07 08:59:24 +02001243 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1244 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001245 return ret;
1246 }
1247
Valerio Setti35d1dac2023-06-30 18:04:16 +02001248 // From now on prv_key_buf is used to store the public key of prv.
Valerio Setti1df94f82023-04-07 08:59:24 +02001249 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1250 &prv_key_len);
1251 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1252 destruction_status = psa_destroy_key(key_id);
1253 if (ret != 0) {
1254 return ret;
1255 } else if (destruction_status != PSA_SUCCESS) {
1256 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001257 }
1258
Valerio Settia1b8af62023-05-17 15:34:57 +02001259 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1260 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001261 MBEDTLS_ECP_PF_UNCOMPRESSED,
1262 &pub_key_len, pub_key_buf,
1263 sizeof(pub_key_buf));
1264 if (ret != 0) {
1265 return ret;
1266 }
1267
1268 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1269 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1270 }
1271
1272 return 0;
1273}
Valerio Settibb7603a2023-06-21 18:34:54 +02001274#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001275
Valerio Settibb7603a2023-06-21 18:34:54 +02001276static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1277 int (*f_rng)(void *, unsigned char *, size_t),
1278 void *p_rng)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001279{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001280 (void) f_rng;
1281 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001282 return eckey_check_pair_psa(pub, prv);
Valerio Settibb7603a2023-06-21 18:34:54 +02001283}
1284#else /* MBEDTLS_USE_PSA_CRYPTO */
1285static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1286 int (*f_rng)(void *, unsigned char *, size_t),
1287 void *p_rng)
1288{
valerio38992cb2023-04-20 09:56:30 +02001289 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1290 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001291 f_rng, p_rng);
1292}
Valerio Settibb7603a2023-06-21 18:34:54 +02001293#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001294
Valerio Setti88a3aee2023-06-29 15:01:10 +02001295#if defined(MBEDTLS_USE_PSA_CRYPTO)
1296#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1297/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1298 * using the same function. */
1299#define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap
1300#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1301static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub,
1302 mbedtls_pk_context *prv,
1303 int (*f_rng)(void *, unsigned char *, size_t),
1304 void *p_rng)
1305{
1306 psa_status_t status;
1307 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1308 size_t exp_pub_key_len = 0;
1309 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1310 size_t pub_key_len = 0;
1311 int ret;
1312 (void) f_rng;
1313 (void) p_rng;
1314
1315 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1316 &exp_pub_key_len);
1317 if (status != PSA_SUCCESS) {
1318 ret = psa_pk_status_to_mbedtls(status);
1319 return ret;
1320 }
1321 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1322 &(mbedtls_pk_ec_ro(*pub)->Q),
1323 MBEDTLS_ECP_PF_UNCOMPRESSED,
1324 &pub_key_len, pub_key, sizeof(pub_key));
1325 if (ret != 0) {
1326 return ret;
1327 }
1328 if ((exp_pub_key_len != pub_key_len) ||
1329 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1330 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1331 }
1332 return 0;
1333}
1334#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1335#endif /* MBEDTLS_USE_PSA_CRYPTO */
1336
Valerio Settib5361262023-05-18 18:51:58 +02001337#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001338static void *eckey_alloc_wrap(void)
1339{
1340 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1341
1342 if (ctx != NULL) {
1343 mbedtls_ecp_keypair_init(ctx);
1344 }
1345
1346 return ctx;
1347}
1348
1349static void eckey_free_wrap(void *ctx)
1350{
1351 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1352 mbedtls_free(ctx);
1353}
Valerio Settib5361262023-05-18 18:51:58 +02001354#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001355
valerio38992cb2023-04-20 09:56:30 +02001356static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001357{
Valerio Settia1b8af62023-05-17 15:34:57 +02001358#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1359 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1360 items->name = "eckey.Q";
1361 items->value = pk;
Valerio Setti5c26b302023-06-21 19:47:01 +02001362#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001363 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001364 items->type = MBEDTLS_PK_DEBUG_ECP;
1365 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001366 items->value = &(ecp->Q);
Valerio Setti5c26b302023-06-21 19:47:01 +02001367#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001368}
1369
1370const mbedtls_pk_info_t mbedtls_eckey_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001371 .type = MBEDTLS_PK_ECKEY,
1372 .name = "EC",
1373 .get_bitlen = eckey_get_bitlen,
1374 .can_do = eckey_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001375#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001376 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001377#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1378 .verify_func = NULL,
1379#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001380#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001381 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001382#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1383 .sign_func = NULL,
1384#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001385#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001386 .verify_rs_func = eckey_verify_rs_wrap,
1387 .sign_rs_func = eckey_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001388 .rs_alloc_func = eckey_rs_alloc,
1389 .rs_free_func = eckey_rs_free,
1390#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1391 .decrypt_func = NULL,
1392 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001393 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001394#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1395 .ctx_alloc_func = NULL,
1396 .ctx_free_func = NULL,
1397#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001398 .ctx_alloc_func = eckey_alloc_wrap,
1399 .ctx_free_func = eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001400#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001401 .debug_func = eckey_debug,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001402};
1403
1404/*
1405 * EC key restricted to ECDH
1406 */
1407static int eckeydh_can_do(mbedtls_pk_type_t type)
1408{
1409 return type == MBEDTLS_PK_ECKEY ||
1410 type == MBEDTLS_PK_ECKEY_DH;
1411}
1412
1413const mbedtls_pk_info_t mbedtls_eckeydh_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001414 .type = MBEDTLS_PK_ECKEY_DH,
1415 .name = "EC_DH",
1416 .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */
1417 .can_do = eckeydh_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001418 .verify_func = NULL,
1419 .sign_func = NULL,
1420#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1421 .verify_rs_func = NULL,
1422 .sign_rs_func = NULL,
1423#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1424 .decrypt_func = NULL,
1425 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001426 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001427#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1428 .ctx_alloc_func = NULL,
1429 .ctx_free_func = NULL,
1430#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001431 .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */
1432 .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */
Valerio Settib5361262023-05-18 18:51:58 +02001433#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001434 .debug_func = eckey_debug, /* Same underlying key structure */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001435};
Valerio Setti1cdddac2023-02-02 13:55:57 +01001436
1437#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1438static int ecdsa_can_do(mbedtls_pk_type_t type)
1439{
1440 return type == MBEDTLS_PK_ECDSA;
1441}
1442
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001443#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001444static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001445 const unsigned char *hash, size_t hash_len,
1446 const unsigned char *sig, size_t sig_len,
1447 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001448{
Janos Follath24eed8d2019-11-22 13:21:35 +00001449 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001450 ((void) md_alg);
1451
1452 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001453 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001454 hash, hash_len, sig, sig_len,
1455 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001456
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1458 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1459 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001462}
1463
valerio38992cb2023-04-20 09:56:30 +02001464static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 const unsigned char *hash, size_t hash_len,
1466 unsigned char *sig, size_t sig_size, size_t *sig_len,
1467 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1468 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001469{
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001471 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1473 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001474
1475}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001478{
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001480
Gilles Peskine449bd832023-01-11 14:50:10 +01001481 if (ctx != NULL) {
1482 mbedtls_ecdsa_restart_init(ctx);
1483 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001484
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001486}
1487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001489{
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 mbedtls_ecdsa_restart_free(ctx);
1491 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001492}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001493#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001495const mbedtls_pk_info_t mbedtls_ecdsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001496 .type = MBEDTLS_PK_ECDSA,
1497 .name = "ECDSA",
1498 .get_bitlen = eckey_get_bitlen, /* Compatible key structures */
1499 .can_do = ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001500#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001501 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001502#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1503 .verify_func = NULL,
1504#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001505#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001506 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001507#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1508 .sign_func = NULL,
1509#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001510#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001511 .verify_rs_func = ecdsa_verify_rs_wrap,
1512 .sign_rs_func = ecdsa_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001513 .rs_alloc_func = ecdsa_rs_alloc,
1514 .rs_free_func = ecdsa_rs_free,
1515#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1516 .decrypt_func = NULL,
1517 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001518 .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001519#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1520 .ctx_alloc_func = NULL,
1521 .ctx_free_func = NULL,
1522#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001523 .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */
1524 .ctx_free_func = eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001525#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001526 .debug_func = eckey_debug, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001527};
Valerio Setti7ca13182023-01-27 13:22:42 +01001528#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Valerio Settid9d74c22023-06-29 15:00:02 +02001529#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001531#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001532/*
1533 * Support for alternative RSA-private implementations
1534 */
1535
Gilles Peskine449bd832023-01-11 14:50:10 +01001536static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001537{
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001539}
1540
valerio38992cb2023-04-20 09:56:30 +02001541static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001542{
valerio38992cb2023-04-20 09:56:30 +02001543 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001546}
1547
valerio38992cb2023-04-20 09:56:30 +02001548static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 const unsigned char *hash, size_t hash_len,
1550 unsigned char *sig, size_t sig_size, size_t *sig_len,
1551 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001552{
valerio38992cb2023-04-20 09:56:30 +02001553 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001554
Dave Rodgman02a53d72023-09-28 17:17:07 +01001555#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (UINT_MAX < hash_len) {
1557 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1558 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001559#endif
Andres AG72849872017-01-19 11:24:33 +00001560
Gilles Peskine449bd832023-01-11 14:50:10 +01001561 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1562 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1563 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1564 }
1565 if (*sig_len > sig_size) {
1566 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1567 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1570 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001571}
1572
valerio38992cb2023-04-20 09:56:30 +02001573static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 const unsigned char *input, size_t ilen,
1575 unsigned char *output, size_t *olen, size_t osize,
1576 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001577{
valerio38992cb2023-04-20 09:56:30 +02001578 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001579
1580 ((void) f_rng);
1581 ((void) p_rng);
1582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1584 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1585 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001586
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 return rsa_alt->decrypt_func(rsa_alt->key,
1588 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001589}
1590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001592static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 int (*f_rng)(void *, unsigned char *, size_t),
1594 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001595{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001597 unsigned char hash[32];
1598 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1602 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001603 }
1604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605 memset(hash, 0x2a, sizeof(hash));
1606
valerio38992cb2023-04-20 09:56:30 +02001607 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 hash, sizeof(hash),
1609 sig, sizeof(sig), &sig_len,
1610 f_rng, p_rng)) != 0) {
1611 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001612 }
1613
valerio38992cb2023-04-20 09:56:30 +02001614 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001615 hash, sizeof(hash), sig, sig_len) != 0) {
1616 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1617 }
1618
1619 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001622
Gilles Peskine449bd832023-01-11 14:50:10 +01001623static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001624{
Gilles Peskine449bd832023-01-11 14:50:10 +01001625 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627 if (ctx != NULL) {
1628 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1629 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001630
Gilles Peskine449bd832023-01-11 14:50:10 +01001631 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001632}
1633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001635{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001636 mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001637}
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001640 .type = MBEDTLS_PK_RSA_ALT,
1641 .name = "RSA-alt",
1642 .get_bitlen = rsa_alt_get_bitlen,
1643 .can_do = rsa_alt_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001644 .verify_func = NULL,
Valerio Settif69514a2023-06-21 18:16:49 +02001645 .sign_func = rsa_alt_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001646#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1647 .verify_rs_func = NULL,
1648 .sign_rs_func = NULL,
1649 .rs_alloc_func = NULL,
1650 .rs_free_func = NULL,
1651#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +02001652 .decrypt_func = rsa_alt_decrypt_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001653 .encrypt_func = NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_RSA_C)
Valerio Settif69514a2023-06-21 18:16:49 +02001655 .check_pair_func = rsa_alt_check_pair,
Valerio Setti97976e32023-06-23 14:08:26 +02001656#else
1657 .check_pair_func = NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001658#endif
Valerio Settif69514a2023-06-21 18:16:49 +02001659 .ctx_alloc_func = rsa_alt_alloc_wrap,
1660 .ctx_free_func = rsa_alt_free_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001661 .debug_func = NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001662};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001664
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001665#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +02001666static size_t opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001667{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001668 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001669 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001670
Valerio Setti4f387ef2023-05-02 14:15:59 +02001671 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 return 0;
1673 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 bits = psa_get_key_bits(&attributes);
1676 psa_reset_key_attributes(&attributes);
1677 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001678}
1679
Valerio Setti38913c12023-06-30 16:18:33 +02001680#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti76d0f962023-06-23 13:32:54 +02001681static int ecdsa_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001682{
Gilles Peskine449bd832023-01-11 14:50:10 +01001683 return type == MBEDTLS_PK_ECKEY ||
1684 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001685}
1686
Valerio Setti76d0f962023-06-23 13:32:54 +02001687const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = {
Valerio Setti574a00b2023-06-21 19:47:37 +02001688 .type = MBEDTLS_PK_OPAQUE,
1689 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001690 .get_bitlen = opaque_get_bitlen,
1691 .can_do = ecdsa_opaque_can_do,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001692#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001693 .verify_func = ecdsa_opaque_verify_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001694#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1695 .verify_func = NULL,
1696#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1697#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Setti76d0f962023-06-23 13:32:54 +02001698 .sign_func = ecdsa_opaque_sign_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001699#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1700 .sign_func = NULL,
1701#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti97976e32023-06-23 14:08:26 +02001702#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1703 .verify_rs_func = NULL,
1704 .sign_rs_func = NULL,
1705 .rs_alloc_func = NULL,
1706 .rs_free_func = NULL,
1707#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1708 .decrypt_func = NULL,
1709 .encrypt_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001710 .check_pair_func = ecdsa_opaque_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001711 .ctx_alloc_func = NULL,
1712 .ctx_free_func = NULL,
1713 .debug_func = NULL,
Valerio Setti574a00b2023-06-21 19:47:37 +02001714};
Valerio Setti38913c12023-06-30 16:18:33 +02001715#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti574a00b2023-06-21 19:47:37 +02001716
Valerio Setti76d0f962023-06-23 13:32:54 +02001717static int rsa_opaque_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001718{
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 return type == MBEDTLS_PK_RSA ||
1720 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001721}
1722
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001723#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001724static int rsa_opaque_decrypt(mbedtls_pk_context *pk,
1725 const unsigned char *input, size_t ilen,
1726 unsigned char *output, size_t *olen, size_t osize,
1727 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001728{
Valerio Setti574a00b2023-06-21 19:47:37 +02001729 psa_status_t status;
1730
1731 /* PSA has its own RNG */
1732 (void) f_rng;
1733 (void) p_rng;
1734
1735 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
1736 input, ilen,
1737 NULL, 0,
1738 output, osize, olen);
1739 if (status != PSA_SUCCESS) {
1740 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1741 }
1742
1743 return 0;
1744}
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001745#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti574a00b2023-06-21 19:47:37 +02001746
Valerio Setti76d0f962023-06-23 13:32:54 +02001747static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
1748 const unsigned char *hash, size_t hash_len,
1749 unsigned char *sig, size_t sig_size, size_t *sig_len,
1750 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001751{
1752#if defined(MBEDTLS_RSA_C)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001753 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001754 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001755 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001756 psa_status_t status;
1757
1758 /* PSA has its own RNG */
1759 (void) f_rng;
1760 (void) p_rng;
1761
Valerio Setti4f387ef2023-05-02 14:15:59 +02001762 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001763 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001764 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001766
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 type = psa_get_key_type(&attributes);
1768 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001771 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Valerio Setti4657f102023-06-21 13:55:16 +02001772 } else {
1773 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1774 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001775
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001776 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001777 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001778 sig, sig_size, sig_len);
1779 if (status != PSA_SUCCESS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001781 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Valerio Setti4657f102023-06-21 13:55:16 +02001782 } else {
1783 return PSA_PK_TO_MBEDTLS_ERR(status);
1784 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001785 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001786
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001787 return 0;
Valerio Setti574a00b2023-06-21 19:47:37 +02001788#else /* !MBEDTLS_RSA_C */
1789 ((void) pk);
1790 ((void) md_alg);
1791 ((void) hash);
1792 ((void) hash_len);
1793 ((void) sig);
1794 ((void) sig_size);
1795 ((void) sig_len);
1796 ((void) f_rng);
1797 ((void) p_rng);
1798 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti4657f102023-06-21 13:55:16 +02001799#endif /* !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001800}
1801
Valerio Setti76d0f962023-06-23 13:32:54 +02001802const mbedtls_pk_info_t mbedtls_rsa_opaque_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001803 .type = MBEDTLS_PK_OPAQUE,
1804 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001805 .get_bitlen = opaque_get_bitlen,
1806 .can_do = rsa_opaque_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001807 .verify_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001808 .sign_func = rsa_opaque_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001809#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1810 .verify_rs_func = NULL,
1811 .sign_rs_func = NULL,
1812 .rs_alloc_func = NULL,
1813 .rs_free_func = NULL,
1814#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001815#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001816 .decrypt_func = rsa_opaque_decrypt,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001817#else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001818 .decrypt_func = NULL,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001819#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001820 .encrypt_func = NULL,
1821 .check_pair_func = NULL,
1822 .ctx_alloc_func = NULL,
1823 .ctx_free_func = NULL,
1824 .debug_func = NULL,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001825};
1826
1827#endif /* MBEDTLS_USE_PSA_CRYPTO */
1828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001829#endif /* MBEDTLS_PK_C */