blob: 182d07f72b5f8712cec0661c563aee4f451c0b80 [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 Setti76d0f962023-06-23 13:32:54 +0200958/* Common helper for ECDSA sign using PSA functions. */
Valerio Setti884c1ec2023-06-23 12:09:13 +0200959static int ecdsa_sign_psa(mbedtls_svc_key_id_t key_id, mbedtls_md_type_t md_alg,
Valerio Setti4657f102023-06-21 13:55:16 +0200960 const unsigned char *hash, size_t hash_len,
961 unsigned char *sig, size_t sig_size, size_t *sig_len)
962{
963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
964 psa_status_t status;
Valerio Setti884c1ec2023-06-23 12:09:13 +0200965 psa_algorithm_t psa_sig_md;
966 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard564bc1b2023-08-02 12:05:16 +0200967 psa_algorithm_t alg, alg2;
Valerio Setti884c1ec2023-06-23 12:09:13 +0200968
969 status = psa_get_key_attributes(key_id, &key_attr);
970 if (status != PSA_SUCCESS) {
971 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
972 }
973 alg = psa_get_key_algorithm(&key_attr);
Manuel Pégourié-Gonnard564bc1b2023-08-02 12:05:16 +0200974 alg2 = psa_get_key_enrollment_algorithm(&key_attr);
Valerio Setti76d0f962023-06-23 13:32:54 +0200975 psa_reset_key_attributes(&key_attr);
Valerio Setti884c1ec2023-06-23 12:09:13 +0200976
Manuel Pégourié-Gonnard564bc1b2023-08-02 12:05:16 +0200977 if (PSA_ALG_IS_DETERMINISTIC_ECDSA(alg) || PSA_ALG_IS_DETERMINISTIC_ECDSA(alg2)) {
Valerio Setti884c1ec2023-06-23 12:09:13 +0200978 psa_sig_md = PSA_ALG_DETERMINISTIC_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
979 } else {
980 psa_sig_md = PSA_ALG_ECDSA(mbedtls_md_psa_alg_from_type(md_alg));
981 }
Valerio Setti4657f102023-06-21 13:55:16 +0200982
983 status = psa_sign_hash(key_id, psa_sig_md, hash, hash_len,
984 sig, sig_size, sig_len);
985 if (status != PSA_SUCCESS) {
986 return PSA_PK_ECDSA_TO_MBEDTLS_ERR(status);
987 }
988
989 ret = pk_ecdsa_sig_asn1_from_psa(sig, sig_len, sig_size);
990
991 return ret;
992}
993
Valerio Setti76d0f962023-06-23 13:32:54 +0200994static int ecdsa_opaque_sign_wrap(mbedtls_pk_context *pk,
995 mbedtls_md_type_t md_alg,
996 const unsigned char *hash, size_t hash_len,
997 unsigned char *sig, size_t sig_size,
998 size_t *sig_len,
999 int (*f_rng)(void *, unsigned char *, size_t),
1000 void *p_rng)
Valerio Setti4657f102023-06-21 13:55:16 +02001001{
1002 ((void) f_rng);
1003 ((void) p_rng);
Valerio Setti4657f102023-06-21 13:55:16 +02001004
Valerio Setti884c1ec2023-06-23 12:09:13 +02001005 return ecdsa_sign_psa(pk->priv_id, md_alg, hash, hash_len, sig, sig_size,
Valerio Setti4657f102023-06-21 13:55:16 +02001006 sig_len);
1007}
1008
1009#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti76d0f962023-06-23 13:32:54 +02001010/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1011 * using the same function. */
1012#define ecdsa_sign_wrap ecdsa_opaque_sign_wrap
Valerio Setti4657f102023-06-21 13:55:16 +02001013#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001014static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 const unsigned char *hash, size_t hash_len,
1016 unsigned char *sig, size_t sig_size, size_t *sig_len,
1017 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Neil Armstronge9606902022-02-09 14:23:00 +01001018{
Neil Armstronge9606902022-02-09 14:23:00 +01001019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Neil Armstronge9606902022-02-09 14:23:00 +01001020 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1021 psa_status_t status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001022 mbedtls_ecp_keypair *ctx = pk->pk_ctx;
1023 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1024 unsigned char buf[MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH];
Neil Armstronge9606902022-02-09 14:23:00 +01001025 size_t curve_bits;
1026 psa_ecc_family_t curve =
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_ecc_group_to_psa(ctx->grp.id, &curve_bits);
Gilles Peskine13caa942022-10-04 22:59:26 +02001028 size_t key_len = PSA_BITS_TO_BYTES(curve_bits);
Manuel Pégourié-Gonnard116175c2023-07-25 12:06:55 +02001029 psa_algorithm_t psa_hash = mbedtls_md_psa_alg_from_type(md_alg);
1030 psa_algorithm_t psa_sig_md = MBEDTLS_PK_PSA_ALG_ECDSA_MAYBE_DET(psa_hash);
Neil Armstronge9606902022-02-09 14:23:00 +01001031 ((void) f_rng);
1032 ((void) p_rng);
1033
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 if (curve == 0) {
1035 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1036 }
Neil Armstronge9606902022-02-09 14:23:00 +01001037
Gilles Peskine13caa942022-10-04 22:59:26 +02001038 if (key_len > sizeof(buf)) {
Valerio Settib761b152023-01-31 14:56:04 +01001039 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 }
Gilles Peskine13caa942022-10-04 22:59:26 +02001041 ret = mbedtls_mpi_write_binary(&ctx->d, buf, key_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001042 if (ret != 0) {
Neil Armstronge9606902022-02-09 14:23:00 +01001043 goto cleanup;
1044 }
1045
Gilles Peskine449bd832023-01-11 14:50:10 +01001046 psa_set_key_type(&attributes, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1047 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_HASH);
1048 psa_set_key_algorithm(&attributes, psa_sig_md);
1049
Valerio Setti4657f102023-06-21 13:55:16 +02001050 status = psa_import_key(&attributes, buf, key_len, &key_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001052 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 goto cleanup;
Neil Armstronge9606902022-02-09 14:23:00 +01001054 }
1055
Valerio Setti884c1ec2023-06-23 12:09:13 +02001056 ret = ecdsa_sign_psa(key_id, md_alg, hash, hash_len, sig, sig_size, sig_len);
Neil Armstronge9606902022-02-09 14:23:00 +01001057
1058cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 mbedtls_platform_zeroize(buf, sizeof(buf));
1060 status = psa_destroy_key(key_id);
1061 if (ret == 0 && status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001062 ret = PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001063 }
Neil Armstrongff70f0b2022-03-03 14:31:17 +01001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 return ret;
Neil Armstronge9606902022-02-09 14:23:00 +01001066}
Valerio Setti4657f102023-06-21 13:55:16 +02001067#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001068#else /* MBEDTLS_USE_PSA_CRYPTO */
valerio38992cb2023-04-20 09:56:30 +02001069static int ecdsa_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 const unsigned char *hash, size_t hash_len,
1071 unsigned char *sig, size_t sig_size, size_t *sig_len,
1072 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001073{
valerio38992cb2023-04-20 09:56:30 +02001074 return mbedtls_ecdsa_write_signature((mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001075 md_alg, hash, hash_len,
1076 sig, sig_size, sig_len,
1077 f_rng, p_rng);
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001078}
Valerio Setti1cdddac2023-02-02 13:55:57 +01001079#endif /* MBEDTLS_USE_PSA_CRYPTO */
1080#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Manuel Pégourié-Gonnard8df27692013-08-21 10:34:38 +02001081
Valerio Setti80d07982023-02-08 13:49:17 +01001082#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001083/* Forward declarations */
valerio38992cb2023-04-20 09:56:30 +02001084static int ecdsa_verify_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001085 const unsigned char *hash, size_t hash_len,
1086 const unsigned char *sig, size_t sig_len,
1087 void *rs_ctx);
1088
valerio38992cb2023-04-20 09:56:30 +02001089static int ecdsa_sign_rs_wrap(mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001090 const unsigned char *hash, size_t hash_len,
1091 unsigned char *sig, size_t sig_size, size_t *sig_len,
1092 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1093 void *rs_ctx);
1094
1095/*
1096 * Restart context for ECDSA operations with ECKEY context
1097 *
1098 * We need to store an actual ECDSA context, as we need to pass the same to
1099 * the underlying ecdsa function, so we can't create it on the fly every time.
1100 */
1101typedef struct {
1102 mbedtls_ecdsa_restart_ctx ecdsa_rs;
1103 mbedtls_ecdsa_context ecdsa_ctx;
1104} eckey_restart_ctx;
1105
1106static void *eckey_rs_alloc(void)
1107{
1108 eckey_restart_ctx *rs_ctx;
1109
1110 void *ctx = mbedtls_calloc(1, sizeof(eckey_restart_ctx));
1111
1112 if (ctx != NULL) {
1113 rs_ctx = ctx;
1114 mbedtls_ecdsa_restart_init(&rs_ctx->ecdsa_rs);
1115 mbedtls_ecdsa_init(&rs_ctx->ecdsa_ctx);
1116 }
1117
1118 return ctx;
1119}
1120
1121static void eckey_rs_free(void *ctx)
1122{
1123 eckey_restart_ctx *rs_ctx;
1124
1125 if (ctx == NULL) {
1126 return;
1127 }
1128
1129 rs_ctx = ctx;
1130 mbedtls_ecdsa_restart_free(&rs_ctx->ecdsa_rs);
1131 mbedtls_ecdsa_free(&rs_ctx->ecdsa_ctx);
1132
1133 mbedtls_free(ctx);
1134}
1135
valerio38992cb2023-04-20 09:56:30 +02001136static int eckey_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001137 const unsigned char *hash, size_t hash_len,
1138 const unsigned char *sig, size_t sig_len,
1139 void *rs_ctx)
1140{
1141 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1142 eckey_restart_ctx *rs = rs_ctx;
1143
1144 /* Should never happen */
1145 if (rs == NULL) {
1146 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1147 }
1148
1149 /* set up our own sub-context if needed (that is, on first run) */
1150 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001151 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001152 }
1153
valerio38992cb2023-04-20 09:56:30 +02001154 MBEDTLS_MPI_CHK(ecdsa_verify_rs_wrap(pk,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001155 md_alg, hash, hash_len,
1156 sig, sig_len, &rs->ecdsa_rs));
1157
1158cleanup:
1159 return ret;
1160}
1161
valerio38992cb2023-04-20 09:56:30 +02001162static int eckey_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001163 const unsigned char *hash, size_t hash_len,
1164 unsigned char *sig, size_t sig_size, size_t *sig_len,
1165 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1166 void *rs_ctx)
1167{
1168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1169 eckey_restart_ctx *rs = rs_ctx;
1170
1171 /* Should never happen */
1172 if (rs == NULL) {
1173 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1174 }
1175
1176 /* set up our own sub-context if needed (that is, on first run) */
1177 if (rs->ecdsa_ctx.grp.pbits == 0) {
valerio38992cb2023-04-20 09:56:30 +02001178 MBEDTLS_MPI_CHK(mbedtls_ecdsa_from_keypair(&rs->ecdsa_ctx, pk->pk_ctx));
Valerio Setti1cdddac2023-02-02 13:55:57 +01001179 }
1180
valerio38992cb2023-04-20 09:56:30 +02001181 MBEDTLS_MPI_CHK(ecdsa_sign_rs_wrap(pk, md_alg,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001182 hash, hash_len, sig, sig_size, sig_len,
1183 f_rng, p_rng, &rs->ecdsa_rs));
1184
1185cleanup:
1186 return ret;
1187}
Valerio Setti80d07982023-02-08 13:49:17 +01001188#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001189
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001190#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Settibb7603a2023-06-21 18:34:54 +02001191#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
valerio38992cb2023-04-20 09:56:30 +02001192static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001193{
Valerio Setti9efa8c42023-05-19 13:27:30 +02001194 psa_status_t status;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001195 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Valerio Setti8eb55262023-04-04 10:20:53 +02001196 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001197 size_t prv_key_len;
Valerio Settiae8c6282023-05-18 18:57:57 +02001198 mbedtls_svc_key_id_t key_id = prv->priv_id;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001199
1200 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1201 &prv_key_len);
1202 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1203 if (ret != 0) {
1204 return ret;
1205 }
1206
1207 if (memcmp(prv_key_buf, pub->pub_raw, pub->pub_raw_len) != 0) {
1208 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1209 }
Valerio Settibb7603a2023-06-21 18:34:54 +02001210
1211 return 0;
1212}
1213#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1214static int eckey_check_pair_psa(mbedtls_pk_context *pub, mbedtls_pk_context *prv)
1215{
1216 psa_status_t status;
1217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1218 uint8_t prv_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1219 size_t prv_key_len;
Valerio Setti9efa8c42023-05-19 13:27:30 +02001220 psa_status_t destruction_status;
Valerio Settiae8c6282023-05-18 18:57:57 +02001221 mbedtls_svc_key_id_t key_id = MBEDTLS_SVC_KEY_ID_INIT;
1222 psa_key_attributes_t key_attr = PSA_KEY_ATTRIBUTES_INIT;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001223 uint8_t pub_key_buf[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH];
1224 size_t pub_key_len;
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001225 size_t curve_bits;
Valerio Settif2866642023-04-06 16:49:54 +02001226 const psa_ecc_family_t curve =
Valerio Settia1b8af62023-05-17 15:34:57 +02001227 mbedtls_ecc_group_to_psa(mbedtls_pk_ec_ro(*prv)->grp.id, &curve_bits);
Valerio Settic1541cb2023-05-17 15:49:55 +02001228 const size_t curve_bytes = PSA_BITS_TO_BYTES(curve_bits);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001229
Valerio Settia7cb8452023-05-22 18:39:43 +02001230 if (curve == 0) {
1231 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1232 }
1233
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001234 psa_set_key_type(&key_attr, PSA_KEY_TYPE_ECC_KEY_PAIR(curve));
1235 psa_set_key_usage_flags(&key_attr, PSA_KEY_USAGE_EXPORT);
1236
Valerio Settia1b8af62023-05-17 15:34:57 +02001237 ret = mbedtls_mpi_write_binary(&mbedtls_pk_ec_ro(*prv)->d,
1238 prv_key_buf, curve_bytes);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001239 if (ret != 0) {
Valerio Setti35d1dac2023-06-30 18:04:16 +02001240 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001241 return ret;
1242 }
1243
1244 status = psa_import_key(&key_attr, prv_key_buf, curve_bytes, &key_id);
Valerio Setti35d1dac2023-06-30 18:04:16 +02001245 mbedtls_platform_zeroize(prv_key_buf, sizeof(prv_key_buf));
Valerio Setti1df94f82023-04-07 08:59:24 +02001246 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1247 if (ret != 0) {
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001248 return ret;
1249 }
1250
Valerio Setti35d1dac2023-06-30 18:04:16 +02001251 // From now on prv_key_buf is used to store the public key of prv.
Valerio Setti1df94f82023-04-07 08:59:24 +02001252 status = psa_export_public_key(key_id, prv_key_buf, sizeof(prv_key_buf),
1253 &prv_key_len);
1254 ret = PSA_PK_TO_MBEDTLS_ERR(status);
1255 destruction_status = psa_destroy_key(key_id);
1256 if (ret != 0) {
1257 return ret;
1258 } else if (destruction_status != PSA_SUCCESS) {
1259 return PSA_PK_TO_MBEDTLS_ERR(destruction_status);
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001260 }
1261
Valerio Settia1b8af62023-05-17 15:34:57 +02001262 ret = mbedtls_ecp_point_write_binary(&mbedtls_pk_ec_rw(*pub)->grp,
1263 &mbedtls_pk_ec_rw(*pub)->Q,
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001264 MBEDTLS_ECP_PF_UNCOMPRESSED,
1265 &pub_key_len, pub_key_buf,
1266 sizeof(pub_key_buf));
1267 if (ret != 0) {
1268 return ret;
1269 }
1270
1271 if (memcmp(prv_key_buf, pub_key_buf, curve_bytes) != 0) {
1272 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1273 }
1274
1275 return 0;
1276}
Valerio Settibb7603a2023-06-21 18:34:54 +02001277#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001278
Valerio Settibb7603a2023-06-21 18:34:54 +02001279static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1280 int (*f_rng)(void *, unsigned char *, size_t),
1281 void *p_rng)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001282{
Valerio Setti0fe1ee22023-04-03 14:42:22 +02001283 (void) f_rng;
1284 (void) p_rng;
Valerio Setti9d65f0e2023-04-07 08:53:17 +02001285 return eckey_check_pair_psa(pub, prv);
Valerio Settibb7603a2023-06-21 18:34:54 +02001286}
1287#else /* MBEDTLS_USE_PSA_CRYPTO */
1288static int eckey_check_pair_wrap(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
1289 int (*f_rng)(void *, unsigned char *, size_t),
1290 void *p_rng)
1291{
valerio38992cb2023-04-20 09:56:30 +02001292 return mbedtls_ecp_check_pub_priv((const mbedtls_ecp_keypair *) pub->pk_ctx,
1293 (const mbedtls_ecp_keypair *) prv->pk_ctx,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001294 f_rng, p_rng);
1295}
Valerio Settibb7603a2023-06-21 18:34:54 +02001296#endif /* MBEDTLS_USE_PSA_CRYPTO */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001297
Valerio Setti88a3aee2023-06-29 15:01:10 +02001298#if defined(MBEDTLS_USE_PSA_CRYPTO)
1299#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1300/* When PK_USE_PSA_EC_DATA is defined opaque and non-opaque keys end up
1301 * using the same function. */
1302#define ecdsa_opaque_check_pair_wrap eckey_check_pair_wrap
1303#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
1304static int ecdsa_opaque_check_pair_wrap(mbedtls_pk_context *pub,
1305 mbedtls_pk_context *prv,
1306 int (*f_rng)(void *, unsigned char *, size_t),
1307 void *p_rng)
1308{
1309 psa_status_t status;
1310 uint8_t exp_pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1311 size_t exp_pub_key_len = 0;
1312 uint8_t pub_key[MBEDTLS_PK_MAX_EC_PUBKEY_RAW_LEN];
1313 size_t pub_key_len = 0;
1314 int ret;
1315 (void) f_rng;
1316 (void) p_rng;
1317
1318 status = psa_export_public_key(prv->priv_id, exp_pub_key, sizeof(exp_pub_key),
1319 &exp_pub_key_len);
1320 if (status != PSA_SUCCESS) {
1321 ret = psa_pk_status_to_mbedtls(status);
1322 return ret;
1323 }
1324 ret = mbedtls_ecp_point_write_binary(&(mbedtls_pk_ec_ro(*pub)->grp),
1325 &(mbedtls_pk_ec_ro(*pub)->Q),
1326 MBEDTLS_ECP_PF_UNCOMPRESSED,
1327 &pub_key_len, pub_key, sizeof(pub_key));
1328 if (ret != 0) {
1329 return ret;
1330 }
1331 if ((exp_pub_key_len != pub_key_len) ||
1332 memcmp(exp_pub_key, pub_key, exp_pub_key_len)) {
1333 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1334 }
1335 return 0;
1336}
1337#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
1338#endif /* MBEDTLS_USE_PSA_CRYPTO */
1339
Valerio Settib5361262023-05-18 18:51:58 +02001340#if !defined(MBEDTLS_PK_USE_PSA_EC_DATA)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001341static void *eckey_alloc_wrap(void)
1342{
1343 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecp_keypair));
1344
1345 if (ctx != NULL) {
1346 mbedtls_ecp_keypair_init(ctx);
1347 }
1348
1349 return ctx;
1350}
1351
1352static void eckey_free_wrap(void *ctx)
1353{
1354 mbedtls_ecp_keypair_free((mbedtls_ecp_keypair *) ctx);
1355 mbedtls_free(ctx);
1356}
Valerio Settib5361262023-05-18 18:51:58 +02001357#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001358
valerio38992cb2023-04-20 09:56:30 +02001359static void eckey_debug(mbedtls_pk_context *pk, mbedtls_pk_debug_item *items)
Valerio Setti1cdddac2023-02-02 13:55:57 +01001360{
Valerio Settia1b8af62023-05-17 15:34:57 +02001361#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1362 items->type = MBEDTLS_PK_DEBUG_PSA_EC;
1363 items->name = "eckey.Q";
1364 items->value = pk;
Valerio Setti5c26b302023-06-21 19:47:01 +02001365#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
valerio38992cb2023-04-20 09:56:30 +02001366 mbedtls_ecp_keypair *ecp = (mbedtls_ecp_keypair *) pk->pk_ctx;
Valerio Setti1cdddac2023-02-02 13:55:57 +01001367 items->type = MBEDTLS_PK_DEBUG_ECP;
1368 items->name = "eckey.Q";
valerio38992cb2023-04-20 09:56:30 +02001369 items->value = &(ecp->Q);
Valerio Setti5c26b302023-06-21 19:47:01 +02001370#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001371}
1372
1373const mbedtls_pk_info_t mbedtls_eckey_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001374 .type = MBEDTLS_PK_ECKEY,
1375 .name = "EC",
1376 .get_bitlen = eckey_get_bitlen,
1377 .can_do = eckey_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001378#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001379 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001380#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1381 .verify_func = NULL,
1382#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001383#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001384 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001385#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1386 .sign_func = NULL,
1387#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001388#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001389 .verify_rs_func = eckey_verify_rs_wrap,
1390 .sign_rs_func = eckey_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001391 .rs_alloc_func = eckey_rs_alloc,
1392 .rs_free_func = eckey_rs_free,
1393#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1394 .decrypt_func = NULL,
1395 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001396 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001397#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1398 .ctx_alloc_func = NULL,
1399 .ctx_free_func = NULL,
1400#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001401 .ctx_alloc_func = eckey_alloc_wrap,
1402 .ctx_free_func = eckey_free_wrap,
Valerio Settib5361262023-05-18 18:51:58 +02001403#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001404 .debug_func = eckey_debug,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001405};
1406
1407/*
1408 * EC key restricted to ECDH
1409 */
1410static int eckeydh_can_do(mbedtls_pk_type_t type)
1411{
1412 return type == MBEDTLS_PK_ECKEY ||
1413 type == MBEDTLS_PK_ECKEY_DH;
1414}
1415
1416const mbedtls_pk_info_t mbedtls_eckeydh_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001417 .type = MBEDTLS_PK_ECKEY_DH,
1418 .name = "EC_DH",
1419 .get_bitlen = eckey_get_bitlen, /* Same underlying key structure */
1420 .can_do = eckeydh_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001421 .verify_func = NULL,
1422 .sign_func = NULL,
1423#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1424 .verify_rs_func = NULL,
1425 .sign_rs_func = NULL,
1426#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1427 .decrypt_func = NULL,
1428 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001429 .check_pair_func = eckey_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001430#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1431 .ctx_alloc_func = NULL,
1432 .ctx_free_func = NULL,
1433#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001434 .ctx_alloc_func = eckey_alloc_wrap, /* Same underlying key structure */
1435 .ctx_free_func = eckey_free_wrap, /* Same underlying key structure */
Valerio Settib5361262023-05-18 18:51:58 +02001436#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001437 .debug_func = eckey_debug, /* Same underlying key structure */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001438};
Valerio Setti1cdddac2023-02-02 13:55:57 +01001439
1440#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
1441static int ecdsa_can_do(mbedtls_pk_type_t type)
1442{
1443 return type == MBEDTLS_PK_ECDSA;
1444}
1445
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001446#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
valerio38992cb2023-04-20 09:56:30 +02001447static int ecdsa_verify_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001448 const unsigned char *hash, size_t hash_len,
1449 const unsigned char *sig, size_t sig_len,
1450 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001451{
Janos Follath24eed8d2019-11-22 13:21:35 +00001452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001453 ((void) md_alg);
1454
1455 ret = mbedtls_ecdsa_read_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001456 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 hash, hash_len, sig, sig_len,
1458 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 if (ret == MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH) {
1461 return MBEDTLS_ERR_PK_SIG_LEN_MISMATCH;
1462 }
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001463
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 return ret;
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001465}
1466
valerio38992cb2023-04-20 09:56:30 +02001467static int ecdsa_sign_rs_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 const unsigned char *hash, size_t hash_len,
1469 unsigned char *sig, size_t sig_size, size_t *sig_len,
1470 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
1471 void *rs_ctx)
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001472{
Gilles Peskine449bd832023-01-11 14:50:10 +01001473 return mbedtls_ecdsa_write_signature_restartable(
valerio38992cb2023-04-20 09:56:30 +02001474 (mbedtls_ecdsa_context *) pk->pk_ctx,
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 md_alg, hash, hash_len, sig, sig_size, sig_len, f_rng, p_rng,
1476 (mbedtls_ecdsa_restart_ctx *) rs_ctx);
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001477
1478}
Manuel Pégourié-Gonnard1f596062017-05-09 10:42:40 +02001479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480static void *ecdsa_rs_alloc(void)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001481{
Gilles Peskine449bd832023-01-11 14:50:10 +01001482 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_ecdsa_restart_ctx));
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if (ctx != NULL) {
1485 mbedtls_ecdsa_restart_init(ctx);
1486 }
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 return ctx;
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001489}
1490
Gilles Peskine449bd832023-01-11 14:50:10 +01001491static void ecdsa_rs_free(void *ctx)
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001492{
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_ecdsa_restart_free(ctx);
1494 mbedtls_free(ctx);
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001495}
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001496#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Manuel Pégourié-Gonnardfe687702017-08-18 17:04:07 +02001497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498const mbedtls_pk_info_t mbedtls_ecdsa_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001499 .type = MBEDTLS_PK_ECDSA,
1500 .name = "ECDSA",
1501 .get_bitlen = eckey_get_bitlen, /* Compatible key structures */
1502 .can_do = ecdsa_can_do,
Valerio Setti1cdddac2023-02-02 13:55:57 +01001503#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Settif69514a2023-06-21 18:16:49 +02001504 .verify_func = ecdsa_verify_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001505#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1506 .verify_func = NULL,
1507#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
Valerio Setti1cdddac2023-02-02 13:55:57 +01001508#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Settif69514a2023-06-21 18:16:49 +02001509 .sign_func = ecdsa_sign_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001510#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1511 .sign_func = NULL,
1512#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti5b16e9e2023-02-07 08:08:53 +01001513#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
Valerio Settif69514a2023-06-21 18:16:49 +02001514 .verify_rs_func = ecdsa_verify_rs_wrap,
1515 .sign_rs_func = ecdsa_sign_rs_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001516 .rs_alloc_func = ecdsa_rs_alloc,
1517 .rs_free_func = ecdsa_rs_free,
1518#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1519 .decrypt_func = NULL,
1520 .encrypt_func = NULL,
Valerio Settibb7603a2023-06-21 18:34:54 +02001521 .check_pair_func = eckey_check_pair_wrap, /* Compatible key structures */
Valerio Setti97976e32023-06-23 14:08:26 +02001522#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
1523 .ctx_alloc_func = NULL,
1524 .ctx_free_func = NULL,
1525#else /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001526 .ctx_alloc_func = eckey_alloc_wrap, /* Compatible key structures */
1527 .ctx_free_func = eckey_free_wrap, /* Compatible key structures */
Valerio Settib5361262023-05-18 18:51:58 +02001528#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
Valerio Settif69514a2023-06-21 18:16:49 +02001529 .debug_func = eckey_debug, /* Compatible key structures */
Manuel Pégourié-Gonnard09162dd2013-08-14 18:16:50 +02001530};
Valerio Setti7ca13182023-01-27 13:22:42 +01001531#endif /* MBEDTLS_PK_CAN_ECDSA_SOME */
Valerio Settid9d74c22023-06-29 15:00:02 +02001532#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001535/*
1536 * Support for alternative RSA-private implementations
1537 */
1538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539static int rsa_alt_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001540{
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 return type == MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnard20422e92014-06-05 13:41:44 +02001542}
1543
valerio38992cb2023-04-20 09:56:30 +02001544static size_t rsa_alt_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001545{
valerio38992cb2023-04-20 09:56:30 +02001546 const mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001547
Gilles Peskine449bd832023-01-11 14:50:10 +01001548 return 8 * rsa_alt->key_len_func(rsa_alt->key);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001549}
1550
valerio38992cb2023-04-20 09:56:30 +02001551static int rsa_alt_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 const unsigned char *hash, size_t hash_len,
1553 unsigned char *sig, size_t sig_size, size_t *sig_len,
1554 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001555{
valerio38992cb2023-04-20 09:56:30 +02001556 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001557
Dave Rodgman02a53d72023-09-28 17:17:07 +01001558#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (UINT_MAX < hash_len) {
1560 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1561 }
Dave Rodgman02a53d72023-09-28 17:17:07 +01001562#endif
Andres AG72849872017-01-19 11:24:33 +00001563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564 *sig_len = rsa_alt->key_len_func(rsa_alt->key);
1565 if (*sig_len > MBEDTLS_PK_SIGNATURE_MAX_SIZE) {
1566 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
1567 }
1568 if (*sig_len > sig_size) {
1569 return MBEDTLS_ERR_PK_BUFFER_TOO_SMALL;
1570 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 return rsa_alt->sign_func(rsa_alt->key, f_rng, p_rng,
1573 md_alg, (unsigned int) hash_len, hash, sig);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001574}
1575
valerio38992cb2023-04-20 09:56:30 +02001576static int rsa_alt_decrypt_wrap(mbedtls_pk_context *pk,
Gilles Peskine449bd832023-01-11 14:50:10 +01001577 const unsigned char *input, size_t ilen,
1578 unsigned char *output, size_t *olen, size_t osize,
1579 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001580{
valerio38992cb2023-04-20 09:56:30 +02001581 mbedtls_rsa_alt_context *rsa_alt = pk->pk_ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001582
1583 ((void) f_rng);
1584 ((void) p_rng);
1585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586 if (ilen != rsa_alt->key_len_func(rsa_alt->key)) {
1587 return MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1588 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001589
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 return rsa_alt->decrypt_func(rsa_alt->key,
1591 olen, input, output, osize);
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001592}
1593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#if defined(MBEDTLS_RSA_C)
valerio38992cb2023-04-20 09:56:30 +02001595static int rsa_alt_check_pair(mbedtls_pk_context *pub, mbedtls_pk_context *prv,
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 int (*f_rng)(void *, unsigned char *, size_t),
1597 void *p_rng)
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 unsigned char sig[MBEDTLS_MPI_MAX_SIZE];
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001600 unsigned char hash[32];
1601 size_t sig_len = 0;
Janos Follath24eed8d2019-11-22 13:21:35 +00001602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 if (rsa_alt_get_bitlen(prv) != rsa_get_bitlen(pub)) {
1605 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001606 }
1607
Gilles Peskine449bd832023-01-11 14:50:10 +01001608 memset(hash, 0x2a, sizeof(hash));
1609
valerio38992cb2023-04-20 09:56:30 +02001610 if ((ret = rsa_alt_sign_wrap(prv, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001611 hash, sizeof(hash),
1612 sig, sizeof(sig), &sig_len,
1613 f_rng, p_rng)) != 0) {
1614 return ret;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001615 }
1616
valerio38992cb2023-04-20 09:56:30 +02001617 if (rsa_verify_wrap(pub, MBEDTLS_MD_NONE,
Gilles Peskine449bd832023-01-11 14:50:10 +01001618 hash, sizeof(hash), sig, sig_len) != 0) {
1619 return MBEDTLS_ERR_RSA_KEY_CHECK_FAILED;
1620 }
1621
1622 return 0;
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnarda1efcb02014-11-08 17:08:08 +01001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626static void *rsa_alt_alloc_wrap(void)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001627{
Gilles Peskine449bd832023-01-11 14:50:10 +01001628 void *ctx = mbedtls_calloc(1, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630 if (ctx != NULL) {
1631 memset(ctx, 0, sizeof(mbedtls_rsa_alt_context));
1632 }
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001633
Gilles Peskine449bd832023-01-11 14:50:10 +01001634 return ctx;
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001635}
1636
Gilles Peskine449bd832023-01-11 14:50:10 +01001637static void rsa_alt_free_wrap(void *ctx)
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001638{
Tom Cosgroveca8c61b2023-07-17 15:17:40 +01001639 mbedtls_zeroize_and_free(ctx, sizeof(mbedtls_rsa_alt_context));
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001640}
1641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642const mbedtls_pk_info_t mbedtls_rsa_alt_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001643 .type = MBEDTLS_PK_RSA_ALT,
1644 .name = "RSA-alt",
1645 .get_bitlen = rsa_alt_get_bitlen,
1646 .can_do = rsa_alt_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001647 .verify_func = NULL,
Valerio Settif69514a2023-06-21 18:16:49 +02001648 .sign_func = rsa_alt_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001649#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1650 .verify_rs_func = NULL,
1651 .sign_rs_func = NULL,
1652 .rs_alloc_func = NULL,
1653 .rs_free_func = NULL,
1654#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif69514a2023-06-21 18:16:49 +02001655 .decrypt_func = rsa_alt_decrypt_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001656 .encrypt_func = NULL,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001657#if defined(MBEDTLS_RSA_C)
Valerio Settif69514a2023-06-21 18:16:49 +02001658 .check_pair_func = rsa_alt_check_pair,
Valerio Setti97976e32023-06-23 14:08:26 +02001659#else
1660 .check_pair_func = NULL,
Manuel Pégourié-Gonnard7c13d692014-11-12 00:01:34 +01001661#endif
Valerio Settif69514a2023-06-21 18:16:49 +02001662 .ctx_alloc_func = rsa_alt_alloc_wrap,
1663 .ctx_free_func = rsa_alt_free_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001664 .debug_func = NULL,
Manuel Pégourié-Gonnard12c1ff02013-08-21 12:28:31 +02001665};
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
Manuel Pégourié-Gonnard348bcb32015-03-31 14:01:33 +02001667
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001668#if defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti76d0f962023-06-23 13:32:54 +02001669static size_t opaque_get_bitlen(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001670{
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001671 size_t bits;
Gilles Peskined2d45c12019-05-27 14:53:13 +02001672 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001673
Valerio Setti4f387ef2023-05-02 14:15:59 +02001674 if (PSA_SUCCESS != psa_get_key_attributes(pk->priv_id, &attributes)) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 return 0;
1676 }
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001677
Gilles Peskine449bd832023-01-11 14:50:10 +01001678 bits = psa_get_key_bits(&attributes);
1679 psa_reset_key_attributes(&attributes);
1680 return bits;
Manuel Pégourié-Gonnard0184b3c2018-10-31 10:36:51 +01001681}
1682
Valerio Setti38913c12023-06-30 16:18:33 +02001683#if defined(MBEDTLS_PK_HAVE_ECC_KEYS)
Valerio Setti76d0f962023-06-23 13:32:54 +02001684static int ecdsa_opaque_can_do(mbedtls_pk_type_t type)
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001685{
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 return type == MBEDTLS_PK_ECKEY ||
1687 type == MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnard920c0632018-10-31 10:57:29 +01001688}
1689
Valerio Setti76d0f962023-06-23 13:32:54 +02001690const mbedtls_pk_info_t mbedtls_ecdsa_opaque_info = {
Valerio Setti574a00b2023-06-21 19:47:37 +02001691 .type = MBEDTLS_PK_OPAQUE,
1692 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001693 .get_bitlen = opaque_get_bitlen,
1694 .can_do = ecdsa_opaque_can_do,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001695#if defined(MBEDTLS_PK_CAN_ECDSA_VERIFY)
Valerio Setti76d0f962023-06-23 13:32:54 +02001696 .verify_func = ecdsa_opaque_verify_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001697#else /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1698 .verify_func = NULL,
1699#endif /* MBEDTLS_PK_CAN_ECDSA_VERIFY */
1700#if defined(MBEDTLS_PK_CAN_ECDSA_SIGN)
Valerio Setti76d0f962023-06-23 13:32:54 +02001701 .sign_func = ecdsa_opaque_sign_wrap,
Valerio Setti4d1daf82023-06-26 13:31:18 +02001702#else /* MBEDTLS_PK_CAN_ECDSA_SIGN */
1703 .sign_func = NULL,
1704#endif /* MBEDTLS_PK_CAN_ECDSA_SIGN */
Valerio Setti97976e32023-06-23 14:08:26 +02001705#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1706 .verify_rs_func = NULL,
1707 .sign_rs_func = NULL,
1708 .rs_alloc_func = NULL,
1709 .rs_free_func = NULL,
1710#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
1711 .decrypt_func = NULL,
1712 .encrypt_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001713 .check_pair_func = ecdsa_opaque_check_pair_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001714 .ctx_alloc_func = NULL,
1715 .ctx_free_func = NULL,
1716 .debug_func = NULL,
Valerio Setti574a00b2023-06-21 19:47:37 +02001717};
Valerio Setti38913c12023-06-30 16:18:33 +02001718#endif /* MBEDTLS_PK_HAVE_ECC_KEYS */
Valerio Setti574a00b2023-06-21 19:47:37 +02001719
Valerio Setti76d0f962023-06-23 13:32:54 +02001720static int rsa_opaque_can_do(mbedtls_pk_type_t type)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001721{
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 return type == MBEDTLS_PK_RSA ||
1723 type == MBEDTLS_PK_RSASSA_PSS;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001724}
1725
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001726#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001727static int rsa_opaque_decrypt(mbedtls_pk_context *pk,
1728 const unsigned char *input, size_t ilen,
1729 unsigned char *output, size_t *olen, size_t osize,
1730 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001731{
Valerio Setti574a00b2023-06-21 19:47:37 +02001732 psa_status_t status;
1733
1734 /* PSA has its own RNG */
1735 (void) f_rng;
1736 (void) p_rng;
1737
1738 status = psa_asymmetric_decrypt(pk->priv_id, PSA_ALG_RSA_PKCS1V15_CRYPT,
1739 input, ilen,
1740 NULL, 0,
1741 output, osize, olen);
1742 if (status != PSA_SUCCESS) {
1743 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
1744 }
1745
1746 return 0;
1747}
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001748#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti574a00b2023-06-21 19:47:37 +02001749
Valerio Setti76d0f962023-06-23 13:32:54 +02001750static int rsa_opaque_sign_wrap(mbedtls_pk_context *pk, mbedtls_md_type_t md_alg,
1751 const unsigned char *hash, size_t hash_len,
1752 unsigned char *sig, size_t sig_size, size_t *sig_len,
1753 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
Valerio Setti574a00b2023-06-21 19:47:37 +02001754{
1755#if defined(MBEDTLS_RSA_C)
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001756 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001757 psa_algorithm_t alg;
Neil Armstrongeabbf9d2022-03-15 12:01:26 +01001758 psa_key_type_t type;
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001759 psa_status_t status;
1760
1761 /* PSA has its own RNG */
1762 (void) f_rng;
1763 (void) p_rng;
1764
Valerio Setti4f387ef2023-05-02 14:15:59 +02001765 status = psa_get_key_attributes(pk->priv_id, &attributes);
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001767 return PSA_PK_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001769
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 type = psa_get_key_type(&attributes);
1771 psa_reset_key_attributes(&attributes);
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001772
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 if (PSA_KEY_TYPE_IS_RSA(type)) {
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +02001774 alg = PSA_ALG_RSA_PKCS1V15_SIGN(mbedtls_md_psa_alg_from_type(md_alg));
Valerio Setti4657f102023-06-21 13:55:16 +02001775 } else {
1776 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
1777 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001778
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001779 /* make the signature */
Valerio Setti4f387ef2023-05-02 14:15:59 +02001780 status = psa_sign_hash(pk->priv_id, alg, hash, hash_len,
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 sig, sig_size, sig_len);
1782 if (status != PSA_SUCCESS) {
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 if (PSA_KEY_TYPE_IS_RSA(type)) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05001784 return PSA_PK_RSA_TO_MBEDTLS_ERR(status);
Valerio Setti4657f102023-06-21 13:55:16 +02001785 } else {
1786 return PSA_PK_TO_MBEDTLS_ERR(status);
1787 }
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001788 }
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001789
Neil Armstrongb980c9b2022-03-15 16:19:16 +01001790 return 0;
Valerio Setti574a00b2023-06-21 19:47:37 +02001791#else /* !MBEDTLS_RSA_C */
1792 ((void) pk);
1793 ((void) md_alg);
1794 ((void) hash);
1795 ((void) hash_len);
1796 ((void) sig);
1797 ((void) sig_size);
1798 ((void) sig_len);
1799 ((void) f_rng);
1800 ((void) p_rng);
1801 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Valerio Setti4657f102023-06-21 13:55:16 +02001802#endif /* !MBEDTLS_RSA_C */
Manuel Pégourié-Gonnardd8454bc2018-11-13 10:32:00 +01001803}
1804
Valerio Setti76d0f962023-06-23 13:32:54 +02001805const mbedtls_pk_info_t mbedtls_rsa_opaque_info = {
Valerio Settif69514a2023-06-21 18:16:49 +02001806 .type = MBEDTLS_PK_OPAQUE,
1807 .name = "Opaque",
Valerio Setti76d0f962023-06-23 13:32:54 +02001808 .get_bitlen = opaque_get_bitlen,
1809 .can_do = rsa_opaque_can_do,
Valerio Setti97976e32023-06-23 14:08:26 +02001810 .verify_func = NULL,
Valerio Setti76d0f962023-06-23 13:32:54 +02001811 .sign_func = rsa_opaque_sign_wrap,
Valerio Setti97976e32023-06-23 14:08:26 +02001812#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
1813 .verify_rs_func = NULL,
1814 .sign_rs_func = NULL,
1815 .rs_alloc_func = NULL,
1816 .rs_free_func = NULL,
1817#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001818#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC)
Valerio Setti76d0f962023-06-23 13:32:54 +02001819 .decrypt_func = rsa_opaque_decrypt,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001820#else /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001821 .decrypt_func = NULL,
Valerio Settif6d4dfb2023-07-10 10:55:12 +02001822#endif /* PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_BASIC */
Valerio Setti97976e32023-06-23 14:08:26 +02001823 .encrypt_func = NULL,
1824 .check_pair_func = NULL,
1825 .ctx_alloc_func = NULL,
1826 .ctx_free_func = NULL,
1827 .debug_func = NULL,
Manuel Pégourié-Gonnard20678b22018-10-22 12:11:15 +02001828};
1829
1830#endif /* MBEDTLS_USE_PSA_CRYPTO */
1831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001832#endif /* MBEDTLS_PK_C */