blob: 4a574d1c70854bdcb92312340916170b635bcc1b [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 Setti201e6432024-02-01 17:19:37 +010076 status = mbedtls_to_psa_error(mbedtls_rsa_parse_pubkey(*p_rsa, data, data_length));
Gilles Peskine449bd832023-01-11 14:50:10 +010077 }
78 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010080 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010081
Ronald Cron00b7bfc2020-11-25 15:25:26 +010082 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
83 * supports non-byte-aligned key sizes, but not well. For example,
84 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Valerio Setti7b7ffd32024-01-23 16:14:18 +010085 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(*p_rsa));
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010087 status = PSA_ERROR_NOT_SUPPORTED;
88 goto exit;
89 }
Valerio Setti7b7ffd32024-01-23 16:14:18 +010090 status = psa_check_rsa_key_byte_aligned(*p_rsa);
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010094
Ronald Cron00b7bfc2020-11-25 15:25:26 +010095exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010096 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010097}
Ronald Cron00b7bfc2020-11-25 15:25:26 +010098#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +010099 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100100 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
101 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200102 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) ||
Valerio Settife478902023-07-25 12:27:19 +0200103 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100104 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100105
Valerio Settife478902023-07-25 12:27:19 +0200106#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
107 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +0100108 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron0266cfe2021-03-13 18:50:11 +0100109psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100110 const psa_key_attributes_t *attributes,
111 const uint8_t *data, size_t data_length,
112 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100114{
115 psa_status_t status;
116 mbedtls_rsa_context *rsa = NULL;
117
118 /* Parse input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
120 data,
121 data_length,
122 &rsa);
123 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100128
129 /* Re-export the data to PSA export format, such that we can store export
130 * representation in the key slot. Export representation in case of RSA is
131 * the smallest representation that's allowed as input, so a straight-up
132 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 status = mbedtls_psa_rsa_export_key(attributes->core.type,
134 rsa,
135 key_buffer,
136 key_buffer_size,
137 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100138exit:
139 /* Always free the RSA object */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_rsa_free(rsa);
141 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100144}
Valerio Settife478902023-07-25 12:27:19 +0200145#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
146 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200147 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronabf2aef2020-11-27 18:13:44 +0100148
Valerio Settib2bcedb2023-07-10 11:24:00 +0200149#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
150 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100151psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
152 mbedtls_rsa_context *rsa,
153 uint8_t *data,
154 size_t data_size,
155 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100156{
Ronald Crone5ca3d82020-11-26 16:36:16 +0100157 int ret;
Valerio Setti7b7ffd32024-01-23 16:14:18 +0100158 uint8_t *end = data + data_size;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100159
160 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
161 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
162 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Valerio Setti135ebde2024-02-01 17:00:29 +0100164 ret = mbedtls_rsa_write_key(rsa, data, &end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 } else {
Valerio Setti135ebde2024-02-01 17:00:29 +0100166 ret = mbedtls_rsa_write_pubkey(rsa, data, &end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100170 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 memset(data, 0, data_size);
172 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100173 }
174
175 /* The mbedtls_pk_xxx functions write to the end of the buffer.
176 * Move the data to the beginning and erase remaining data
177 * at the original location. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (2 * (size_t) ret <= data_size) {
179 memcpy(data, data + data_size - ret, ret);
180 memset(data + data_size - ret, 0, ret);
181 } else if ((size_t) ret < data_size) {
182 memmove(data, data + data_size - ret, ret);
183 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100184 }
185
186 *data_length = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100188}
189
Ronald Cron0266cfe2021-03-13 18:50:11 +0100190psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100191 const psa_key_attributes_t *attributes,
192 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100194{
195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
196 mbedtls_rsa_context *rsa = NULL;
197
198 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 attributes->core.type, key_buffer, key_buffer_size, &rsa);
200 if (status != PSA_SUCCESS) {
201 return status;
202 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
205 rsa,
206 data,
207 data_size,
208 data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 mbedtls_rsa_free(rsa);
211 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100212
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100214}
Valerio Settib2bcedb2023-07-10 11:24:00 +0200215#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100216 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100217
Valerio Setti76df8c12023-07-11 14:11:28 +0200218#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100219static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
220 size_t domain_parameters_size,
221 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100222{
223 size_t i;
224 uint32_t acc = 0;
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100227 *exponent = 65537;
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100229 }
230
231 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
232 * support values that fit in a 32-bit integer, which is larger than
233 * int on just about every platform anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 if (domain_parameters_size > sizeof(acc)) {
235 return PSA_ERROR_NOT_SUPPORTED;
236 }
237 for (i = 0; i < domain_parameters_size; i++) {
238 acc = (acc << 8) | domain_parameters[i];
239 }
240 if (acc > INT_MAX) {
241 return PSA_ERROR_NOT_SUPPORTED;
242 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100243 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100245}
246
Ronald Cron0266cfe2021-03-13 18:50:11 +0100247psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100248 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100250{
251 psa_status_t status;
252 mbedtls_rsa_context rsa;
253 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
254 int exponent;
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 status = psa_rsa_read_exponent(attributes->domain_parameters,
257 attributes->domain_parameters_size,
258 &exponent);
259 if (status != PSA_SUCCESS) {
260 return status;
261 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 mbedtls_rsa_init(&rsa);
264 ret = mbedtls_rsa_gen_key(&rsa,
265 mbedtls_psa_get_random,
266 MBEDTLS_PSA_RANDOM_STATE,
267 (unsigned int) attributes->core.bits,
268 exponent);
269 if (ret != 0) {
270 return mbedtls_to_psa_error(ret);
271 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100272
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 status = mbedtls_psa_rsa_export_key(attributes->core.type,
274 &rsa, key_buffer, key_buffer_size,
275 key_buffer_length);
276 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100279}
Valerio Setti76df8c12023-07-11 14:11:28 +0200280#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100281
Ronald Cron7bdbca32020-12-09 13:34:54 +0100282/****************************************************************/
283/* Sign/verify hashes */
284/****************************************************************/
285
Ronald Cron0266cfe2021-03-13 18:50:11 +0100286#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
287 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100288
289/* Decode the hash algorithm from alg and store the mbedtls encoding in
290 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100291static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
292 size_t hash_length,
293 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100294{
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200296 *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100297
298 /* The Mbed TLS RSA module uses an unsigned int for hash length
299 * parameters. Validate that it fits so that we don't risk an
300 * overflow later. */
Dave Rodgman02a53d72023-09-28 17:17:07 +0100301#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (hash_length > UINT_MAX) {
303 return PSA_ERROR_INVALID_ARGUMENT;
304 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100305#endif
Ronald Cron7bdbca32020-12-09 13:34:54 +0100306
Janos Follath0af093b2021-06-07 14:34:10 +0100307 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
309 if (*md_alg == MBEDTLS_MD_NONE) {
310 return PSA_ERROR_NOT_SUPPORTED;
311 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200312 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_ERROR_INVALID_ARGUMENT;
314 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100315 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100316
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100318}
319
Ronald Cron0266cfe2021-03-13 18:50:11 +0100320psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100321 const psa_key_attributes_t *attributes,
322 const uint8_t *key_buffer, size_t key_buffer_size,
323 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100325{
326 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
327 mbedtls_rsa_context *rsa = NULL;
328 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
329 mbedtls_md_type_t md_alg;
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
332 key_buffer,
333 key_buffer_size,
334 &rsa);
335 if (status != PSA_SUCCESS) {
336 return status;
337 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100338
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
340 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100341 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100343
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100345 status = PSA_ERROR_BUFFER_TOO_SMALL;
346 goto exit;
347 }
348
Ronald Cron0266cfe2021-03-13 18:50:11 +0100349#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
351 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
352 MBEDTLS_MD_NONE);
353 if (ret == 0) {
354 ret = mbedtls_rsa_pkcs1_sign(rsa,
355 mbedtls_psa_get_random,
356 MBEDTLS_PSA_RANDOM_STATE,
357 md_alg,
358 (unsigned int) hash_length,
359 hash,
360 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200361 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100363#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
364#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (PSA_ALG_IS_RSA_PSS(alg)) {
366 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (ret == 0) {
369 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
370 mbedtls_psa_get_random,
371 MBEDTLS_PSA_RANDOM_STATE,
372 MBEDTLS_MD_NONE,
373 (unsigned int) hash_length,
374 hash,
375 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200376 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100378#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100379 {
380 status = PSA_ERROR_INVALID_ARGUMENT;
381 goto exit;
382 }
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 if (ret == 0) {
385 *signature_length = mbedtls_rsa_get_len(rsa);
386 }
387 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100388
389exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 mbedtls_rsa_free(rsa);
391 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100394}
395
Ronald Cron0266cfe2021-03-13 18:50:11 +0100396#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
398 const mbedtls_rsa_context *rsa,
399 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200400{
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
402 return MBEDTLS_RSA_SALT_LEN_ANY;
403 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200404 /* Otherwise: standard salt length, i.e. largest possible salt length
405 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200407 int hlen = (int) hash_length; // known to fit
408 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (room < 0) {
410 return 0; // there is no valid signature in this case anyway
411 } else if (room > hlen) {
412 return hlen;
413 } else {
414 return room;
415 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200416}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100417#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200418
Ronald Cron0266cfe2021-03-13 18:50:11 +0100419psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100420 const psa_key_attributes_t *attributes,
421 const uint8_t *key_buffer, size_t key_buffer_size,
422 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100424{
425 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
426 mbedtls_rsa_context *rsa = NULL;
427 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
428 mbedtls_md_type_t md_alg;
429
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
431 key_buffer,
432 key_buffer_size,
433 &rsa);
434 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
439 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100440 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100444 status = PSA_ERROR_INVALID_SIGNATURE;
445 goto exit;
446 }
447
Ronald Cron0266cfe2021-03-13 18:50:11 +0100448#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
450 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
451 MBEDTLS_MD_NONE);
452 if (ret == 0) {
453 ret = mbedtls_rsa_pkcs1_verify(rsa,
454 md_alg,
455 (unsigned int) hash_length,
456 hash,
457 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200458 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100460#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
461#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (PSA_ALG_IS_RSA_PSS(alg)) {
463 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
464 if (ret == 0) {
465 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
466 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
467 md_alg,
468 (unsigned) hash_length,
469 hash,
470 md_alg,
471 slen,
472 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200473 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100475#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100476 {
477 status = PSA_ERROR_INVALID_ARGUMENT;
478 goto exit;
479 }
480
481 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
482 * the rest of the signature is invalid". This has little use in
483 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100485 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100487
488exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 mbedtls_rsa_free(rsa);
490 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100493}
494
Ronald Crond2fb8542020-12-09 15:18:01 +0100495#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
496 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
497
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100498/****************************************************************/
499/* Asymmetric cryptography */
500/****************************************************************/
501
502#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100503static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
504 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100505{
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200507 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100508
Manuel Pégourié-Gonnardeb592042023-05-31 10:48:38 +0200509 /* Just to get the error status right, as rsa_set_padding() doesn't
510 * distinguish between "bad RSA algorithm" and "unknown hash". */
511 if (mbedtls_md_info_from_type(md_alg) == NULL) {
512 return PSA_ERROR_NOT_SUPPORTED;
513 }
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100516}
517#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
520 const uint8_t *key_buffer,
521 size_t key_buffer_size,
522 psa_algorithm_t alg,
523 const uint8_t *input,
524 size_t input_length,
525 const uint8_t *salt,
526 size_t salt_length,
527 uint8_t *output,
528 size_t output_size,
529 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100530{
531 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
532 (void) key_buffer;
533 (void) key_buffer_size;
534 (void) input;
535 (void) input_length;
536 (void) salt;
537 (void) salt_length;
538 (void) output;
539 (void) output_size;
540 (void) output_length;
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100543#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100545 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
547 key_buffer,
548 key_buffer_size,
549 &rsa);
550 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100551 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100553
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100555 status = PSA_ERROR_BUFFER_TOO_SMALL;
556 goto rsa_exit;
557 }
558#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
559 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100561#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
562 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 mbedtls_rsa_pkcs1_encrypt(rsa,
564 mbedtls_psa_get_random,
565 MBEDTLS_PSA_RANDOM_STATE,
566 input_length,
567 input,
568 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100569#else
570 status = PSA_ERROR_NOT_SUPPORTED;
571#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 } else
573 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100574#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
575 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 psa_rsa_oaep_set_padding_mode(alg, rsa));
577 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100578 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100580
581 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
583 mbedtls_psa_get_random,
584 MBEDTLS_PSA_RANDOM_STATE,
585 salt, salt_length,
586 input_length,
587 input,
588 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100589#else
590 status = PSA_ERROR_NOT_SUPPORTED;
591#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100593 status = PSA_ERROR_INVALID_ARGUMENT;
594 }
595#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100597rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 if (status == PSA_SUCCESS) {
599 *output_length = mbedtls_rsa_get_len(rsa);
600 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 mbedtls_rsa_free(rsa);
603 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100604#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
605 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100607 status = PSA_ERROR_NOT_SUPPORTED;
608 }
609
610 return status;
611}
612
Gilles Peskine449bd832023-01-11 14:50:10 +0100613psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
614 const uint8_t *key_buffer,
615 size_t key_buffer_size,
616 psa_algorithm_t alg,
617 const uint8_t *input,
618 size_t input_length,
619 const uint8_t *salt,
620 size_t salt_length,
621 uint8_t *output,
622 size_t output_size,
623 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100624{
625 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
626 (void) key_buffer;
627 (void) key_buffer_size;
628 (void) input;
629 (void) input_length;
630 (void) salt;
631 (void) salt_length;
632 (void) output;
633 (void) output_size;
634 (void) output_length;
635
636 *output_length = 0;
637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100639#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100641 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
643 key_buffer,
644 key_buffer_size,
645 &rsa);
646 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100647 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100649
Gilles Peskine449bd832023-01-11 14:50:10 +0100650 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100651 status = PSA_ERROR_INVALID_ARGUMENT;
652 goto rsa_exit;
653 }
654#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
655 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100658#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
659 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 mbedtls_rsa_pkcs1_decrypt(rsa,
661 mbedtls_psa_get_random,
662 MBEDTLS_PSA_RANDOM_STATE,
663 output_length,
664 input,
665 output,
666 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100667#else
668 status = PSA_ERROR_NOT_SUPPORTED;
669#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 } else
671 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100672#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
673 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 psa_rsa_oaep_set_padding_mode(alg, rsa));
675 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100676 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100678
679 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
681 mbedtls_psa_get_random,
682 MBEDTLS_PSA_RANDOM_STATE,
683 salt, salt_length,
684 output_length,
685 input,
686 output,
687 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100688#else
689 status = PSA_ERROR_NOT_SUPPORTED;
690#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100692 status = PSA_ERROR_INVALID_ARGUMENT;
693 }
694
695#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100697rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 mbedtls_rsa_free(rsa);
699 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100700#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
701 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100703 status = PSA_ERROR_NOT_SUPPORTED;
704 }
705
706 return status;
707}
708
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100709#endif /* MBEDTLS_PSA_CRYPTO_C */