blob: 0047a26bfbd66755e27156c61d07cc8243b74c5d [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 Rodgman16799db2023-11-02 19:47:20 +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>
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +010014#include "psa/crypto_values.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010015#include "psa_crypto_core.h"
Ronald Cron9e18fc12020-11-05 17:36:40 +010016#include "psa_crypto_random_impl.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010017#include "psa_crypto_rsa.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010018#include "psa_crypto_hash.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010019#include "mbedtls/psa_util.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010020
21#include <stdlib.h>
22#include <string.h>
23#include "mbedtls/platform.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010024
25#include <mbedtls/rsa.h>
26#include <mbedtls/error.h>
Valerio Setti7b7ffd32024-01-23 16:14:18 +010027#include "rsa_internal.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010028
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 Cron0266cfe2021-03-13 18:50:11 +010031 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
32 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
Valerio Settib2bcedb2023-07-10 11:24:00 +020033 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) || \
Valerio Settife478902023-07-25 12:27:19 +020034 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010035 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036
37/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
38 * that are not a multiple of 8) well. For example, there is only
39 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
40 * way to return the exact bit size of a key.
41 * To keep things simple, reject non-byte-aligned key sizes. */
42static psa_status_t psa_check_rsa_key_byte_aligned(
Gilles Peskine449bd832023-01-11 14:50:10 +010043 const mbedtls_rsa_context *rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010044{
45 mbedtls_mpi n;
46 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_mpi_init(&n);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010048 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010049 mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
50 if (status == PSA_SUCCESS) {
51 if (mbedtls_mpi_bitlen(&n) % 8 != 0) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010052 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010053 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010054 }
Gilles Peskine449bd832023-01-11 14:50:10 +010055 mbedtls_mpi_free(&n);
56 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010057}
58
59psa_status_t mbedtls_psa_rsa_load_representation(
60 psa_key_type_t type, const uint8_t *data, size_t data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_rsa_context **p_rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010062{
63 psa_status_t status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010064 size_t bits;
Valerio Setti7b7ffd32024-01-23 16:14:18 +010065
66 *p_rsa = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
67 if (*p_rsa == NULL) {
68 return PSA_ERROR_INSUFFICIENT_MEMORY;
69 }
70 mbedtls_rsa_init(*p_rsa);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010071
72 /* Parse the data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Valerio Setti135ebde2024-02-01 17:00:29 +010074 status = mbedtls_to_psa_error(mbedtls_rsa_parse_key(*p_rsa, data, data_length));
Gilles Peskine449bd832023-01-11 14:50:10 +010075 } else {
Valerio Setti52ed54b2024-02-01 16:29:01 +010076 unsigned char *p = data;
77 unsigned char *end = (data + data_length);
Valerio Setti135ebde2024-02-01 17:00:29 +010078 status = mbedtls_to_psa_error(mbedtls_rsa_parse_pubkey(*p_rsa, &p, end));
Gilles Peskine449bd832023-01-11 14:50:10 +010079 }
80 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010082 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010083
Ronald Cron00b7bfc2020-11-25 15:25:26 +010084 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
85 * supports non-byte-aligned key sizes, but not well. For example,
86 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Valerio Setti7b7ffd32024-01-23 16:14:18 +010087 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(*p_rsa));
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010089 status = PSA_ERROR_NOT_SUPPORTED;
90 goto exit;
91 }
Valerio Setti7b7ffd32024-01-23 16:14:18 +010092 status = psa_check_rsa_key_byte_aligned(*p_rsa);
Gilles Peskine449bd832023-01-11 14:50:10 +010093 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010094 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010095 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010096
Ronald Cron00b7bfc2020-11-25 15:25:26 +010097exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010098 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010099}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100100#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100101 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100102 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
103 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200104 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) ||
Valerio Settife478902023-07-25 12:27:19 +0200105 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100106 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100107
Valerio Settife478902023-07-25 12:27:19 +0200108#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
109 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +0100110 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron0266cfe2021-03-13 18:50:11 +0100111psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100112 const psa_key_attributes_t *attributes,
113 const uint8_t *data, size_t data_length,
114 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100116{
117 psa_status_t status;
118 mbedtls_rsa_context *rsa = NULL;
119
120 /* Parse input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
122 data,
123 data_length,
124 &rsa);
125 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100126 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100130
131 /* Re-export the data to PSA export format, such that we can store export
132 * representation in the key slot. Export representation in case of RSA is
133 * the smallest representation that's allowed as input, so a straight-up
134 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 status = mbedtls_psa_rsa_export_key(attributes->core.type,
136 rsa,
137 key_buffer,
138 key_buffer_size,
139 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100140exit:
141 /* Always free the RSA object */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 mbedtls_rsa_free(rsa);
143 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100144
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100146}
Valerio Settife478902023-07-25 12:27:19 +0200147#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
148 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200149 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronabf2aef2020-11-27 18:13:44 +0100150
Valerio Settib2bcedb2023-07-10 11:24:00 +0200151#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
152 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100153psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
154 mbedtls_rsa_context *rsa,
155 uint8_t *data,
156 size_t data_size,
157 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100158{
Ronald Crone5ca3d82020-11-26 16:36:16 +0100159 int ret;
Valerio Setti7b7ffd32024-01-23 16:14:18 +0100160 uint8_t *end = data + data_size;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100161
162 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
163 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
164 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Valerio Setti135ebde2024-02-01 17:00:29 +0100166 ret = mbedtls_rsa_write_key(rsa, data, &end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 } else {
Valerio Setti135ebde2024-02-01 17:00:29 +0100168 ret = mbedtls_rsa_write_pubkey(rsa, data, &end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100172 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 memset(data, 0, data_size);
174 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100175 }
176
177 /* The mbedtls_pk_xxx functions write to the end of the buffer.
178 * Move the data to the beginning and erase remaining data
179 * at the original location. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (2 * (size_t) ret <= data_size) {
181 memcpy(data, data + data_size - ret, ret);
182 memset(data + data_size - ret, 0, ret);
183 } else if ((size_t) ret < data_size) {
184 memmove(data, data + data_size - ret, ret);
185 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100186 }
187
188 *data_length = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100190}
191
Ronald Cron0266cfe2021-03-13 18:50:11 +0100192psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100193 const psa_key_attributes_t *attributes,
194 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100196{
197 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
198 mbedtls_rsa_context *rsa = NULL;
199
200 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 attributes->core.type, key_buffer, key_buffer_size, &rsa);
202 if (status != PSA_SUCCESS) {
203 return status;
204 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
207 rsa,
208 data,
209 data_size,
210 data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 mbedtls_rsa_free(rsa);
213 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100214
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100216}
Valerio Settib2bcedb2023-07-10 11:24:00 +0200217#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100218 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100219
Valerio Setti76df8c12023-07-11 14:11:28 +0200220#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100221static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
222 size_t domain_parameters_size,
223 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100224{
225 size_t i;
226 uint32_t acc = 0;
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100229 *exponent = 65537;
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100231 }
232
233 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
234 * support values that fit in a 32-bit integer, which is larger than
235 * int on just about every platform anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 if (domain_parameters_size > sizeof(acc)) {
237 return PSA_ERROR_NOT_SUPPORTED;
238 }
239 for (i = 0; i < domain_parameters_size; i++) {
240 acc = (acc << 8) | domain_parameters[i];
241 }
242 if (acc > INT_MAX) {
243 return PSA_ERROR_NOT_SUPPORTED;
244 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100245 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100247}
248
Ronald Cron0266cfe2021-03-13 18:50:11 +0100249psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100250 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100252{
253 psa_status_t status;
254 mbedtls_rsa_context rsa;
255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
256 int exponent;
257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 status = psa_rsa_read_exponent(attributes->domain_parameters,
259 attributes->domain_parameters_size,
260 &exponent);
261 if (status != PSA_SUCCESS) {
262 return status;
263 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100264
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_rsa_init(&rsa);
266 ret = mbedtls_rsa_gen_key(&rsa,
267 mbedtls_psa_get_random,
268 MBEDTLS_PSA_RANDOM_STATE,
269 (unsigned int) attributes->core.bits,
270 exponent);
271 if (ret != 0) {
272 return mbedtls_to_psa_error(ret);
273 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 status = mbedtls_psa_rsa_export_key(attributes->core.type,
276 &rsa, key_buffer, key_buffer_size,
277 key_buffer_length);
278 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100281}
Valerio Setti76df8c12023-07-11 14:11:28 +0200282#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100283
Ronald Cron7bdbca32020-12-09 13:34:54 +0100284/****************************************************************/
285/* Sign/verify hashes */
286/****************************************************************/
287
Ronald Cron0266cfe2021-03-13 18:50:11 +0100288#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
289 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100290
291/* Decode the hash algorithm from alg and store the mbedtls encoding in
292 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100293static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
294 size_t hash_length,
295 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100296{
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200298 *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100299
300 /* The Mbed TLS RSA module uses an unsigned int for hash length
301 * parameters. Validate that it fits so that we don't risk an
302 * overflow later. */
Dave Rodgman02a53d72023-09-28 17:17:07 +0100303#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (hash_length > UINT_MAX) {
305 return PSA_ERROR_INVALID_ARGUMENT;
306 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100307#endif
Ronald Cron7bdbca32020-12-09 13:34:54 +0100308
Janos Follath0af093b2021-06-07 14:34:10 +0100309 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
311 if (*md_alg == MBEDTLS_MD_NONE) {
312 return PSA_ERROR_NOT_SUPPORTED;
313 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200314 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return PSA_ERROR_INVALID_ARGUMENT;
316 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100317 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100318
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100320}
321
Ronald Cron0266cfe2021-03-13 18:50:11 +0100322psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100323 const psa_key_attributes_t *attributes,
324 const uint8_t *key_buffer, size_t key_buffer_size,
325 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100327{
328 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
329 mbedtls_rsa_context *rsa = NULL;
330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
331 mbedtls_md_type_t md_alg;
332
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
334 key_buffer,
335 key_buffer_size,
336 &rsa);
337 if (status != PSA_SUCCESS) {
338 return status;
339 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
342 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100343 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100347 status = PSA_ERROR_BUFFER_TOO_SMALL;
348 goto exit;
349 }
350
Ronald Cron0266cfe2021-03-13 18:50:11 +0100351#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
353 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
354 MBEDTLS_MD_NONE);
355 if (ret == 0) {
356 ret = mbedtls_rsa_pkcs1_sign(rsa,
357 mbedtls_psa_get_random,
358 MBEDTLS_PSA_RANDOM_STATE,
359 md_alg,
360 (unsigned int) hash_length,
361 hash,
362 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200363 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100365#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
366#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (PSA_ALG_IS_RSA_PSS(alg)) {
368 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (ret == 0) {
371 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
372 mbedtls_psa_get_random,
373 MBEDTLS_PSA_RANDOM_STATE,
374 MBEDTLS_MD_NONE,
375 (unsigned int) hash_length,
376 hash,
377 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200378 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100380#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100381 {
382 status = PSA_ERROR_INVALID_ARGUMENT;
383 goto exit;
384 }
385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (ret == 0) {
387 *signature_length = mbedtls_rsa_get_len(rsa);
388 }
389 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100390
391exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 mbedtls_rsa_free(rsa);
393 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100394
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100396}
397
Ronald Cron0266cfe2021-03-13 18:50:11 +0100398#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100399static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
400 const mbedtls_rsa_context *rsa,
401 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200402{
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
404 return MBEDTLS_RSA_SALT_LEN_ANY;
405 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200406 /* Otherwise: standard salt length, i.e. largest possible salt length
407 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200409 int hlen = (int) hash_length; // known to fit
410 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (room < 0) {
412 return 0; // there is no valid signature in this case anyway
413 } else if (room > hlen) {
414 return hlen;
415 } else {
416 return room;
417 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200418}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100419#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200420
Ronald Cron0266cfe2021-03-13 18:50:11 +0100421psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100422 const psa_key_attributes_t *attributes,
423 const uint8_t *key_buffer, size_t key_buffer_size,
424 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100426{
427 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
428 mbedtls_rsa_context *rsa = NULL;
429 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
430 mbedtls_md_type_t md_alg;
431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
433 key_buffer,
434 key_buffer_size,
435 &rsa);
436 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100437 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
441 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100442 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100446 status = PSA_ERROR_INVALID_SIGNATURE;
447 goto exit;
448 }
449
Ronald Cron0266cfe2021-03-13 18:50:11 +0100450#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
452 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
453 MBEDTLS_MD_NONE);
454 if (ret == 0) {
455 ret = mbedtls_rsa_pkcs1_verify(rsa,
456 md_alg,
457 (unsigned int) hash_length,
458 hash,
459 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200460 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100462#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
463#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 if (PSA_ALG_IS_RSA_PSS(alg)) {
465 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
466 if (ret == 0) {
467 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
468 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
469 md_alg,
470 (unsigned) hash_length,
471 hash,
472 md_alg,
473 slen,
474 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200475 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100477#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100478 {
479 status = PSA_ERROR_INVALID_ARGUMENT;
480 goto exit;
481 }
482
483 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
484 * the rest of the signature is invalid". This has little use in
485 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100487 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100489
490exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 mbedtls_rsa_free(rsa);
492 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100495}
496
Ronald Crond2fb8542020-12-09 15:18:01 +0100497#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
498 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
499
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100500/****************************************************************/
501/* Asymmetric cryptography */
502/****************************************************************/
503
504#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100505static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
506 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200509 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100510
Manuel Pégourié-Gonnardeb592042023-05-31 10:48:38 +0200511 /* Just to get the error status right, as rsa_set_padding() doesn't
512 * distinguish between "bad RSA algorithm" and "unknown hash". */
513 if (mbedtls_md_info_from_type(md_alg) == NULL) {
514 return PSA_ERROR_NOT_SUPPORTED;
515 }
516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100518}
519#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
520
Gilles Peskine449bd832023-01-11 14:50:10 +0100521psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
522 const uint8_t *key_buffer,
523 size_t key_buffer_size,
524 psa_algorithm_t alg,
525 const uint8_t *input,
526 size_t input_length,
527 const uint8_t *salt,
528 size_t salt_length,
529 uint8_t *output,
530 size_t output_size,
531 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100532{
533 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
534 (void) key_buffer;
535 (void) key_buffer_size;
536 (void) input;
537 (void) input_length;
538 (void) salt;
539 (void) salt_length;
540 (void) output;
541 (void) output_size;
542 (void) output_length;
543
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100545#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100547 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
549 key_buffer,
550 key_buffer_size,
551 &rsa);
552 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100553 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100555
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100557 status = PSA_ERROR_BUFFER_TOO_SMALL;
558 goto rsa_exit;
559 }
560#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
561 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100563#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
564 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 mbedtls_rsa_pkcs1_encrypt(rsa,
566 mbedtls_psa_get_random,
567 MBEDTLS_PSA_RANDOM_STATE,
568 input_length,
569 input,
570 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100571#else
572 status = PSA_ERROR_NOT_SUPPORTED;
573#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 } else
575 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100576#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
577 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 psa_rsa_oaep_set_padding_mode(alg, rsa));
579 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100580 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100582
583 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
585 mbedtls_psa_get_random,
586 MBEDTLS_PSA_RANDOM_STATE,
587 salt, salt_length,
588 input_length,
589 input,
590 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100591#else
592 status = PSA_ERROR_NOT_SUPPORTED;
593#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100595 status = PSA_ERROR_INVALID_ARGUMENT;
596 }
597#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100599rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if (status == PSA_SUCCESS) {
601 *output_length = mbedtls_rsa_get_len(rsa);
602 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 mbedtls_rsa_free(rsa);
605 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100606#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
607 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100609 status = PSA_ERROR_NOT_SUPPORTED;
610 }
611
612 return status;
613}
614
Gilles Peskine449bd832023-01-11 14:50:10 +0100615psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
616 const uint8_t *key_buffer,
617 size_t key_buffer_size,
618 psa_algorithm_t alg,
619 const uint8_t *input,
620 size_t input_length,
621 const uint8_t *salt,
622 size_t salt_length,
623 uint8_t *output,
624 size_t output_size,
625 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100626{
627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
628 (void) key_buffer;
629 (void) key_buffer_size;
630 (void) input;
631 (void) input_length;
632 (void) salt;
633 (void) salt_length;
634 (void) output;
635 (void) output_size;
636 (void) output_length;
637
638 *output_length = 0;
639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100641#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100643 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
645 key_buffer,
646 key_buffer_size,
647 &rsa);
648 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100649 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100651
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100653 status = PSA_ERROR_INVALID_ARGUMENT;
654 goto rsa_exit;
655 }
656#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
657 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
658
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100660#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
661 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 mbedtls_rsa_pkcs1_decrypt(rsa,
663 mbedtls_psa_get_random,
664 MBEDTLS_PSA_RANDOM_STATE,
665 output_length,
666 input,
667 output,
668 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100669#else
670 status = PSA_ERROR_NOT_SUPPORTED;
671#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 } else
673 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100674#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
675 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 psa_rsa_oaep_set_padding_mode(alg, rsa));
677 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100678 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100680
681 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
683 mbedtls_psa_get_random,
684 MBEDTLS_PSA_RANDOM_STATE,
685 salt, salt_length,
686 output_length,
687 input,
688 output,
689 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100690#else
691 status = PSA_ERROR_NOT_SUPPORTED;
692#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100694 status = PSA_ERROR_INVALID_ARGUMENT;
695 }
696
697#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100699rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 mbedtls_rsa_free(rsa);
701 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100702#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
703 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100705 status = PSA_ERROR_NOT_SUPPORTED;
706 }
707
708 return status;
709}
710
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100711#endif /* MBEDTLS_PSA_CRYPTO_C */