blob: 3e878ad7ed6bc2479f0c2c86c004b246124267d9 [file] [log] [blame]
Ronald Cron00b7bfc2020-11-25 15:25:26 +01001/*
2 * PSA RSA layer on top of Mbed TLS crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cron00b7bfc2020-11-25 15:25:26 +01007 */
8
9#include "common.h"
10
11#if defined(MBEDTLS_PSA_CRYPTO_C)
12
13#include <psa/crypto.h>
14#include "psa_crypto_core.h"
Ronald Cron9e18fc12020-11-05 17:36:40 +010015#include "psa_crypto_random_impl.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010016#include "psa_crypto_rsa.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010017#include "psa_crypto_hash.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010018
Gilles Peskinea8cd2e62024-05-17 19:00:46 +020019#include <limits.h>
Ronald Cron00b7bfc2020-11-25 15:25:26 +010020#include <stdlib.h>
21#include <string.h>
22#include "mbedtls/platform.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010023
24#include <mbedtls/rsa.h>
25#include <mbedtls/error.h>
26#include <mbedtls/pk.h>
27#include <mbedtls/pk_internal.h>
28
29#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Croncfc3c7b2021-03-13 18:50:11 +010031 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
32 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
33 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
34 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035
36/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
37 * that are not a multiple of 8) well. For example, there is only
38 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
39 * way to return the exact bit size of a key.
40 * To keep things simple, reject non-byte-aligned key sizes. */
41static psa_status_t psa_check_rsa_key_byte_aligned(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010042 const mbedtls_rsa_context *rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010043{
44 mbedtls_mpi n;
45 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046 mbedtls_mpi_init(&n);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010047 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048 mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
49 if (status == PSA_SUCCESS) {
50 if (mbedtls_mpi_bitlen(&n) % 8 != 0) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010051 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010053 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054 mbedtls_mpi_free(&n);
55 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010056}
57
58psa_status_t mbedtls_psa_rsa_load_representation(
59 psa_key_type_t type, const uint8_t *data, size_t data_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060 mbedtls_rsa_context **p_rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010061{
62 psa_status_t status;
63 mbedtls_pk_context ctx;
64 size_t bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 mbedtls_pk_init(&ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010066
67 /* Parse the data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010069 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 mbedtls_pk_parse_key(&ctx, data, data_length, NULL, 0));
71 } else {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010072 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 mbedtls_pk_parse_public_key(&ctx, data, data_length));
74 }
75 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010076 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010078
79 /* We have something that the pkparse module recognizes. If it is a
80 * valid RSA key, store it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 if (mbedtls_pk_get_type(&ctx) != MBEDTLS_PK_RSA) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010082 status = PSA_ERROR_INVALID_ARGUMENT;
83 goto exit;
84 }
85
86 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
87 * supports non-byte-aligned key sizes, but not well. For example,
88 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(mbedtls_pk_rsa(ctx)));
90 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010091 status = PSA_ERROR_NOT_SUPPORTED;
92 goto exit;
93 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 status = psa_check_rsa_key_byte_aligned(mbedtls_pk_rsa(ctx));
95 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010096 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010098
99 /* Copy out the pointer to the RSA context, and reset the PK context
100 * such that pk_free doesn't free the RSA context we just grabbed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 *p_rsa = mbedtls_pk_rsa(ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100102 ctx.pk_info = NULL;
103
104exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 mbedtls_pk_free(&ctx);
106 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100107}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100108#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100109 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100110 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
111 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
112 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
113 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100114
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100115#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
116 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100117
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100118psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100119 const psa_key_attributes_t *attributes,
120 const uint8_t *data, size_t data_length,
121 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100123{
124 psa_status_t status;
125 mbedtls_rsa_context *rsa = NULL;
126
127 /* Parse input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
129 data,
130 data_length,
131 &rsa);
132 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100133 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100137
138 /* Re-export the data to PSA export format, such that we can store export
139 * representation in the key slot. Export representation in case of RSA is
140 * the smallest representation that's allowed as input, so a straight-up
141 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 status = mbedtls_psa_rsa_export_key(attributes->core.type,
143 rsa,
144 key_buffer,
145 key_buffer_size,
146 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100147exit:
148 /* Always free the RSA object */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 mbedtls_rsa_free(rsa);
150 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100151
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100153}
154
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
156 mbedtls_rsa_context *rsa,
157 uint8_t *data,
158 size_t data_size,
159 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100160{
161#if defined(MBEDTLS_PK_WRITE_C)
162 int ret;
163 mbedtls_pk_context pk;
164 uint8_t *pos = data + data_size;
165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 mbedtls_pk_init(&pk);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100167 pk.pk_info = &mbedtls_rsa_info;
168 pk.pk_ctx = rsa;
169
170 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
171 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
172 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
174 ret = mbedtls_pk_write_key_der(&pk, data, data_size);
175 } else {
176 ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
177 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100180 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100181 memset(data, 0, data_size);
182 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100183 }
184
185 /* The mbedtls_pk_xxx functions write to the end of the buffer.
186 * Move the data to the beginning and erase remaining data
187 * at the original location. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 if (2 * (size_t) ret <= data_size) {
189 memcpy(data, data + data_size - ret, ret);
190 memset(data + data_size - ret, 0, ret);
191 } else if ((size_t) ret < data_size) {
192 memmove(data, data + data_size - ret, ret);
193 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100194 }
195
196 *data_length = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100198#else
199 (void) type;
200 (void) rsa;
201 (void) data;
202 (void) data_size;
203 (void) data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 return PSA_ERROR_NOT_SUPPORTED;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100205#endif /* MBEDTLS_PK_WRITE_C */
206}
207
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100208psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100209 const psa_key_attributes_t *attributes,
210 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100212{
213 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
214 mbedtls_rsa_context *rsa = NULL;
215
216 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 attributes->core.type, key_buffer, key_buffer_size, &rsa);
Gowtham Suresh Kumard9e16c42024-08-19 13:22:35 +0100218 if (status == PSA_SUCCESS) {
219 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
220 rsa,
221 data,
222 data_size,
223 data_length);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 mbedtls_rsa_free(rsa);
227 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100230}
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100231#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
232 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100233
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100234#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
Jaeden Ameroc17f2932021-05-14 08:34:32 +0100235 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
237 size_t domain_parameters_size,
238 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100239{
240 size_t i;
241 uint32_t acc = 0;
242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100244 *exponent = 65537;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100245 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100246 }
247
248 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
249 * support values that fit in a 32-bit integer, which is larger than
250 * int on just about every platform anyway. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 if (domain_parameters_size > sizeof(acc)) {
252 return PSA_ERROR_NOT_SUPPORTED;
253 }
254 for (i = 0; i < domain_parameters_size; i++) {
255 acc = (acc << 8) | domain_parameters[i];
256 }
257 if (acc > INT_MAX) {
258 return PSA_ERROR_NOT_SUPPORTED;
259 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100260 *exponent = acc;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100262}
263
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100264psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100265 const psa_key_attributes_t *attributes,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100267{
268 psa_status_t status;
269 mbedtls_rsa_context rsa;
270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
271 int exponent;
272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 status = psa_rsa_read_exponent(attributes->domain_parameters,
274 attributes->domain_parameters_size,
275 &exponent);
276 if (status != PSA_SUCCESS) {
277 return status;
278 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100279
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE);
281 ret = mbedtls_rsa_gen_key(&rsa,
282 mbedtls_psa_get_random,
283 MBEDTLS_PSA_RANDOM_STATE,
284 (unsigned int) attributes->core.bits,
285 exponent);
286 if (ret != 0) {
Gowtham Suresh Kumard9e16c42024-08-19 13:22:35 +0100287 mbedtls_rsa_free(&rsa);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100288 return mbedtls_to_psa_error(ret);
289 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100290
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 status = mbedtls_psa_rsa_export_key(attributes->core.type,
292 &rsa, key_buffer, key_buffer_size,
293 key_buffer_length);
294 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100295
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100296 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100297}
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100298#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Jaeden Ameroc17f2932021-05-14 08:34:32 +0100299 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100300
Ronald Cron7bdbca32020-12-09 13:34:54 +0100301/****************************************************************/
302/* Sign/verify hashes */
303/****************************************************************/
304
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100305#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
306 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100307
308/* Decode the hash algorithm from alg and store the mbedtls encoding in
309 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
311 size_t hash_length,
312 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100313{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100314 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
315 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
316 *md_alg = mbedtls_md_get_type(md_info);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100317
318 /* The Mbed TLS RSA module uses an unsigned int for hash length
319 * parameters. Validate that it fits so that we don't risk an
320 * overflow later. */
321#if SIZE_MAX > UINT_MAX
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 if (hash_length > UINT_MAX) {
323 return PSA_ERROR_INVALID_ARGUMENT;
324 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100325#endif
326
Janos Follathb23b5742021-06-07 14:34:10 +0100327 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
329 if (md_info == NULL) {
330 return PSA_ERROR_NOT_SUPPORTED;
331 }
332 if (mbedtls_md_get_size(md_info) != hash_length) {
333 return PSA_ERROR_INVALID_ARGUMENT;
334 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100335 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100336
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100337 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100338}
339
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100340psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100341 const psa_key_attributes_t *attributes,
342 const uint8_t *key_buffer, size_t key_buffer_size,
343 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100345{
346 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
347 mbedtls_rsa_context *rsa = NULL;
348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
349 mbedtls_md_type_t md_alg;
350
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
352 key_buffer,
353 key_buffer_size,
354 &rsa);
355 if (status != PSA_SUCCESS) {
Gowtham Suresh Kumard9e16c42024-08-19 13:22:35 +0100356 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100357 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
360 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100361 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100362 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100363
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100365 status = PSA_ERROR_BUFFER_TOO_SMALL;
366 goto exit;
367 }
368
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100369#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
371 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
372 MBEDTLS_MD_NONE);
373 ret = mbedtls_rsa_pkcs1_sign(rsa,
374 mbedtls_psa_get_random,
375 MBEDTLS_PSA_RANDOM_STATE,
376 MBEDTLS_RSA_PRIVATE,
377 md_alg,
378 (unsigned int) hash_length,
379 hash,
380 signature);
381 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100382#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
383#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100384 if (PSA_ALG_IS_RSA_PSS(alg)) {
385 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
386 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
387 mbedtls_psa_get_random,
388 MBEDTLS_PSA_RANDOM_STATE,
389 MBEDTLS_RSA_PRIVATE,
390 MBEDTLS_MD_NONE,
391 (unsigned int) hash_length,
392 hash,
393 signature);
394 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100395#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100396 {
397 status = PSA_ERROR_INVALID_ARGUMENT;
398 goto exit;
399 }
400
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100401 if (ret == 0) {
402 *signature_length = mbedtls_rsa_get_len(rsa);
403 }
404 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100405
406exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100407 mbedtls_rsa_free(rsa);
408 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100409
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100411}
412
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100413#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100414static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
415 const mbedtls_rsa_context *rsa,
416 size_t hash_length)
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200417{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100418 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
419 return MBEDTLS_RSA_SALT_LEN_ANY;
420 }
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200421 /* Otherwise: standard salt length, i.e. largest possible salt length
422 * up to the hash length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200424 int hlen = (int) hash_length; // known to fit
425 int room = klen - 2 - hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426 if (room < 0) {
427 return 0; // there is no valid signature in this case anyway
428 } else if (room > hlen) {
429 return hlen;
430 } else {
431 return room;
432 }
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200433}
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100434#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200435
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100436psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100437 const psa_key_attributes_t *attributes,
438 const uint8_t *key_buffer, size_t key_buffer_size,
439 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100440 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100441{
442 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
443 mbedtls_rsa_context *rsa = NULL;
444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
445 mbedtls_md_type_t md_alg;
446
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
448 key_buffer,
449 key_buffer_size,
450 &rsa);
451 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100452 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100454
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
456 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100457 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100458 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100459
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100460 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100461 status = PSA_ERROR_INVALID_SIGNATURE;
462 goto exit;
463 }
464
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100465#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100466 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
467 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
468 MBEDTLS_MD_NONE);
469 ret = mbedtls_rsa_pkcs1_verify(rsa,
470 mbedtls_psa_get_random,
471 MBEDTLS_PSA_RANDOM_STATE,
472 MBEDTLS_RSA_PUBLIC,
473 md_alg,
474 (unsigned int) hash_length,
475 hash,
476 signature);
477 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100478#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
479#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 if (PSA_ALG_IS_RSA_PSS(alg)) {
481 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
482 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
483 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
484 mbedtls_psa_get_random,
485 MBEDTLS_PSA_RANDOM_STATE,
486 MBEDTLS_RSA_PUBLIC,
487 md_alg,
488 (unsigned int) hash_length,
489 hash,
490 md_alg,
491 slen,
492 signature);
493 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100494#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100495 {
496 status = PSA_ERROR_INVALID_ARGUMENT;
497 goto exit;
498 }
499
500 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
501 * the rest of the signature is invalid". This has little use in
502 * practice and PSA doesn't report this distinction. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100504 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100505 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100506
507exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100508 mbedtls_rsa_free(rsa);
509 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100512}
513
Ronald Crond2fb8542020-12-09 15:18:01 +0100514#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
515 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
516
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100517#endif /* MBEDTLS_PSA_CRYPTO_C */