blob: 7b58ea22a587c6641ec38352b25d3139dbd07738 [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>
27#include <mbedtls/pk.h>
Dave Rodgman3b5e6f02021-04-06 17:58:16 +010028#include "pk_wrap.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010029
30#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010031 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010032 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
33 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
Valerio Settib2bcedb2023-07-10 11:24:00 +020034 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) || \
Valerio Settife478902023-07-25 12:27:19 +020035 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010036 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037
38/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
39 * that are not a multiple of 8) well. For example, there is only
40 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
41 * way to return the exact bit size of a key.
42 * To keep things simple, reject non-byte-aligned key sizes. */
43static psa_status_t psa_check_rsa_key_byte_aligned(
Gilles Peskine449bd832023-01-11 14:50:10 +010044 const mbedtls_rsa_context *rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010045{
46 mbedtls_mpi n;
47 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +010048 mbedtls_mpi_init(&n);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010049 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
51 if (status == PSA_SUCCESS) {
52 if (mbedtls_mpi_bitlen(&n) % 8 != 0) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010053 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010054 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010055 }
Gilles Peskine449bd832023-01-11 14:50:10 +010056 mbedtls_mpi_free(&n);
57 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010058}
59
60psa_status_t mbedtls_psa_rsa_load_representation(
61 psa_key_type_t type, const uint8_t *data, size_t data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_rsa_context **p_rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010063{
64 psa_status_t status;
65 mbedtls_pk_context ctx;
66 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +010067 mbedtls_pk_init(&ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010068
69 /* Parse the data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010071 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010072 mbedtls_pk_parse_key(&ctx, data, data_length, NULL, 0,
73 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE));
74 } else {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010075 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010076 mbedtls_pk_parse_public_key(&ctx, data, data_length));
77 }
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
82 /* We have something that the pkparse module recognizes. If it is a
83 * valid RSA key, store it. */
Gilles Peskine449bd832023-01-11 14:50:10 +010084 if (mbedtls_pk_get_type(&ctx) != MBEDTLS_PK_RSA) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010085 status = PSA_ERROR_INVALID_ARGUMENT;
86 goto exit;
87 }
88
89 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
90 * supports non-byte-aligned key sizes, but not well. For example,
91 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Gilles Peskine449bd832023-01-11 14:50:10 +010092 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(mbedtls_pk_rsa(ctx)));
93 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010094 status = PSA_ERROR_NOT_SUPPORTED;
95 goto exit;
96 }
Gilles Peskine449bd832023-01-11 14:50:10 +010097 status = psa_check_rsa_key_byte_aligned(mbedtls_pk_rsa(ctx));
98 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010099 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100101
102 /* Copy out the pointer to the RSA context, and reset the PK context
103 * such that pk_free doesn't free the RSA context we just grabbed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 *p_rsa = mbedtls_pk_rsa(ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100105 ctx.pk_info = NULL;
106
107exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 mbedtls_pk_free(&ctx);
109 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100110}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100111#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100112 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100113 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
114 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200115 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) ||
Valerio Settife478902023-07-25 12:27:19 +0200116 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100117 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100118
Valerio Settife478902023-07-25 12:27:19 +0200119#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
120 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +0100121 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron0266cfe2021-03-13 18:50:11 +0100122psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100123 const psa_key_attributes_t *attributes,
124 const uint8_t *data, size_t data_length,
125 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100127{
128 psa_status_t status;
129 mbedtls_rsa_context *rsa = NULL;
130
131 /* Parse input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
133 data,
134 data_length,
135 &rsa);
136 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100141
142 /* Re-export the data to PSA export format, such that we can store export
143 * representation in the key slot. Export representation in case of RSA is
144 * the smallest representation that's allowed as input, so a straight-up
145 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 status = mbedtls_psa_rsa_export_key(attributes->core.type,
147 rsa,
148 key_buffer,
149 key_buffer_size,
150 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100151exit:
152 /* Always free the RSA object */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_rsa_free(rsa);
154 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100157}
Valerio Settife478902023-07-25 12:27:19 +0200158#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
159 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200160 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronabf2aef2020-11-27 18:13:44 +0100161
Valerio Settib2bcedb2023-07-10 11:24:00 +0200162#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
163 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100164psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
165 mbedtls_rsa_context *rsa,
166 uint8_t *data,
167 size_t data_size,
168 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100169{
Ronald Crone5ca3d82020-11-26 16:36:16 +0100170 int ret;
171 mbedtls_pk_context pk;
172 uint8_t *pos = data + data_size;
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 mbedtls_pk_init(&pk);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100175 pk.pk_info = &mbedtls_rsa_info;
176 pk.pk_ctx = rsa;
177
178 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
179 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
180 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
182 ret = mbedtls_pk_write_key_der(&pk, data, data_size);
183 } else {
184 ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
185 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100188 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 memset(data, 0, data_size);
190 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100191 }
192
193 /* The mbedtls_pk_xxx functions write to the end of the buffer.
194 * Move the data to the beginning and erase remaining data
195 * at the original location. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (2 * (size_t) ret <= data_size) {
197 memcpy(data, data + data_size - ret, ret);
198 memset(data + data_size - ret, 0, ret);
199 } else if ((size_t) ret < data_size) {
200 memmove(data, data + data_size - ret, ret);
201 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100202 }
203
204 *data_length = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100206}
207
Ronald Cron0266cfe2021-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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100217 attributes->core.type, key_buffer, key_buffer_size, &rsa);
218 if (status != PSA_SUCCESS) {
219 return status;
220 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100221
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
223 rsa,
224 data,
225 data_size,
226 data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_rsa_free(rsa);
229 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100230
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100232}
Valerio Settib2bcedb2023-07-10 11:24:00 +0200233#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100234 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100235
Valerio Setti76df8c12023-07-11 14:11:28 +0200236#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100237static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
238 size_t domain_parameters_size,
239 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100240{
241 size_t i;
242 uint32_t acc = 0;
243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100245 *exponent = 65537;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100247 }
248
249 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
250 * support values that fit in a 32-bit integer, which is larger than
251 * int on just about every platform anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 if (domain_parameters_size > sizeof(acc)) {
253 return PSA_ERROR_NOT_SUPPORTED;
254 }
255 for (i = 0; i < domain_parameters_size; i++) {
256 acc = (acc << 8) | domain_parameters[i];
257 }
258 if (acc > INT_MAX) {
259 return PSA_ERROR_NOT_SUPPORTED;
260 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100261 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100263}
264
Ronald Cron0266cfe2021-03-13 18:50:11 +0100265psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100266 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100268{
269 psa_status_t status;
270 mbedtls_rsa_context rsa;
271 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
272 int exponent;
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 status = psa_rsa_read_exponent(attributes->domain_parameters,
275 attributes->domain_parameters_size,
276 &exponent);
277 if (status != PSA_SUCCESS) {
278 return status;
279 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 mbedtls_rsa_init(&rsa);
282 ret = mbedtls_rsa_gen_key(&rsa,
283 mbedtls_psa_get_random,
284 MBEDTLS_PSA_RANDOM_STATE,
285 (unsigned int) attributes->core.bits,
286 exponent);
287 if (ret != 0) {
288 return mbedtls_to_psa_error(ret);
289 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100290
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100296 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100297}
Valerio Setti76df8c12023-07-11 14:11:28 +0200298#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100299
Ronald Cron7bdbca32020-12-09 13:34:54 +0100300/****************************************************************/
301/* Sign/verify hashes */
302/****************************************************************/
303
Ronald Cron0266cfe2021-03-13 18:50:11 +0100304#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
305 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100306
307/* Decode the hash algorithm from alg and store the mbedtls encoding in
308 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
310 size_t hash_length,
311 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100312{
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200314 *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100315
316 /* The Mbed TLS RSA module uses an unsigned int for hash length
317 * parameters. Validate that it fits so that we don't risk an
318 * overflow later. */
Dave Rodgman02a53d72023-09-28 17:17:07 +0100319#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 if (hash_length > UINT_MAX) {
321 return PSA_ERROR_INVALID_ARGUMENT;
322 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100323#endif
Ronald Cron7bdbca32020-12-09 13:34:54 +0100324
Janos Follath0af093b2021-06-07 14:34:10 +0100325 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
327 if (*md_alg == MBEDTLS_MD_NONE) {
328 return PSA_ERROR_NOT_SUPPORTED;
329 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200330 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 return PSA_ERROR_INVALID_ARGUMENT;
332 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100333 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100336}
337
Ronald Cron0266cfe2021-03-13 18:50:11 +0100338psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100339 const psa_key_attributes_t *attributes,
340 const uint8_t *key_buffer, size_t key_buffer_size,
341 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100343{
344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
345 mbedtls_rsa_context *rsa = NULL;
346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
347 mbedtls_md_type_t md_alg;
348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
350 key_buffer,
351 key_buffer_size,
352 &rsa);
353 if (status != PSA_SUCCESS) {
354 return status;
355 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
358 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100359 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100363 status = PSA_ERROR_BUFFER_TOO_SMALL;
364 goto exit;
365 }
366
Ronald Cron0266cfe2021-03-13 18:50:11 +0100367#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
369 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
370 MBEDTLS_MD_NONE);
371 if (ret == 0) {
372 ret = mbedtls_rsa_pkcs1_sign(rsa,
373 mbedtls_psa_get_random,
374 MBEDTLS_PSA_RANDOM_STATE,
375 md_alg,
376 (unsigned int) hash_length,
377 hash,
378 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200379 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100381#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
382#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 if (PSA_ALG_IS_RSA_PSS(alg)) {
384 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200385
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 if (ret == 0) {
387 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
388 mbedtls_psa_get_random,
389 MBEDTLS_PSA_RANDOM_STATE,
390 MBEDTLS_MD_NONE,
391 (unsigned int) hash_length,
392 hash,
393 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200394 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100396#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100397 {
398 status = PSA_ERROR_INVALID_ARGUMENT;
399 goto exit;
400 }
401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (ret == 0) {
403 *signature_length = mbedtls_rsa_get_len(rsa);
404 }
405 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100406
407exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 mbedtls_rsa_free(rsa);
409 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100410
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100412}
413
Ronald Cron0266cfe2021-03-13 18:50:11 +0100414#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100415static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
416 const mbedtls_rsa_context *rsa,
417 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200418{
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
420 return MBEDTLS_RSA_SALT_LEN_ANY;
421 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200422 /* Otherwise: standard salt length, i.e. largest possible salt length
423 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200425 int hlen = (int) hash_length; // known to fit
426 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (room < 0) {
428 return 0; // there is no valid signature in this case anyway
429 } else if (room > hlen) {
430 return hlen;
431 } else {
432 return room;
433 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200434}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100435#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200436
Ronald Cron0266cfe2021-03-13 18:50:11 +0100437psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100438 const psa_key_attributes_t *attributes,
439 const uint8_t *key_buffer, size_t key_buffer_size,
440 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100442{
443 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
444 mbedtls_rsa_context *rsa = NULL;
445 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
446 mbedtls_md_type_t md_alg;
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
449 key_buffer,
450 key_buffer_size,
451 &rsa);
452 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100453 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
457 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100458 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100462 status = PSA_ERROR_INVALID_SIGNATURE;
463 goto exit;
464 }
465
Ronald Cron0266cfe2021-03-13 18:50:11 +0100466#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
468 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
469 MBEDTLS_MD_NONE);
470 if (ret == 0) {
471 ret = mbedtls_rsa_pkcs1_verify(rsa,
472 md_alg,
473 (unsigned int) hash_length,
474 hash,
475 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200476 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100478#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
479#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 if (PSA_ALG_IS_RSA_PSS(alg)) {
481 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
482 if (ret == 0) {
483 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
484 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
485 md_alg,
486 (unsigned) hash_length,
487 hash,
488 md_alg,
489 slen,
490 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200491 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100493#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100494 {
495 status = PSA_ERROR_INVALID_ARGUMENT;
496 goto exit;
497 }
498
499 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
500 * the rest of the signature is invalid". This has little use in
501 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100503 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100505
506exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 mbedtls_rsa_free(rsa);
508 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100511}
512
Ronald Crond2fb8542020-12-09 15:18:01 +0100513#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
514 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
515
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100516/****************************************************************/
517/* Asymmetric cryptography */
518/****************************************************************/
519
520#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100521static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
522 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100523{
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200525 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100526
Manuel Pégourié-Gonnardeb592042023-05-31 10:48:38 +0200527 /* Just to get the error status right, as rsa_set_padding() doesn't
528 * distinguish between "bad RSA algorithm" and "unknown hash". */
529 if (mbedtls_md_info_from_type(md_alg) == NULL) {
530 return PSA_ERROR_NOT_SUPPORTED;
531 }
532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100534}
535#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
538 const uint8_t *key_buffer,
539 size_t key_buffer_size,
540 psa_algorithm_t alg,
541 const uint8_t *input,
542 size_t input_length,
543 const uint8_t *salt,
544 size_t salt_length,
545 uint8_t *output,
546 size_t output_size,
547 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100548{
549 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
550 (void) key_buffer;
551 (void) key_buffer_size;
552 (void) input;
553 (void) input_length;
554 (void) salt;
555 (void) salt_length;
556 (void) output;
557 (void) output_size;
558 (void) output_length;
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100561#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100563 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
565 key_buffer,
566 key_buffer_size,
567 &rsa);
568 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100569 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100573 status = PSA_ERROR_BUFFER_TOO_SMALL;
574 goto rsa_exit;
575 }
576#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
577 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100579#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
580 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 mbedtls_rsa_pkcs1_encrypt(rsa,
582 mbedtls_psa_get_random,
583 MBEDTLS_PSA_RANDOM_STATE,
584 input_length,
585 input,
586 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100587#else
588 status = PSA_ERROR_NOT_SUPPORTED;
589#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 } else
591 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100592#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
593 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 psa_rsa_oaep_set_padding_mode(alg, rsa));
595 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100596 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100598
599 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
601 mbedtls_psa_get_random,
602 MBEDTLS_PSA_RANDOM_STATE,
603 salt, salt_length,
604 input_length,
605 input,
606 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100607#else
608 status = PSA_ERROR_NOT_SUPPORTED;
609#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100611 status = PSA_ERROR_INVALID_ARGUMENT;
612 }
613#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100615rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (status == PSA_SUCCESS) {
617 *output_length = mbedtls_rsa_get_len(rsa);
618 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 mbedtls_rsa_free(rsa);
621 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100622#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
623 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100625 status = PSA_ERROR_NOT_SUPPORTED;
626 }
627
628 return status;
629}
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
632 const uint8_t *key_buffer,
633 size_t key_buffer_size,
634 psa_algorithm_t alg,
635 const uint8_t *input,
636 size_t input_length,
637 const uint8_t *salt,
638 size_t salt_length,
639 uint8_t *output,
640 size_t output_size,
641 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100642{
643 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
644 (void) key_buffer;
645 (void) key_buffer_size;
646 (void) input;
647 (void) input_length;
648 (void) salt;
649 (void) salt_length;
650 (void) output;
651 (void) output_size;
652 (void) output_length;
653
654 *output_length = 0;
655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100657#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100659 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
661 key_buffer,
662 key_buffer_size,
663 &rsa);
664 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100665 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100669 status = PSA_ERROR_INVALID_ARGUMENT;
670 goto rsa_exit;
671 }
672#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
673 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100676#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
677 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 mbedtls_rsa_pkcs1_decrypt(rsa,
679 mbedtls_psa_get_random,
680 MBEDTLS_PSA_RANDOM_STATE,
681 output_length,
682 input,
683 output,
684 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100685#else
686 status = PSA_ERROR_NOT_SUPPORTED;
687#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 } else
689 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100690#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
691 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 psa_rsa_oaep_set_padding_mode(alg, rsa));
693 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100694 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100696
697 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
699 mbedtls_psa_get_random,
700 MBEDTLS_PSA_RANDOM_STATE,
701 salt, salt_length,
702 output_length,
703 input,
704 output,
705 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100706#else
707 status = PSA_ERROR_NOT_SUPPORTED;
708#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100710 status = PSA_ERROR_INVALID_ARGUMENT;
711 }
712
713#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100715rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 mbedtls_rsa_free(rsa);
717 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100718#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
719 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100721 status = PSA_ERROR_NOT_SUPPORTED;
722 }
723
724 return status;
725}
726
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100727#endif /* MBEDTLS_PSA_CRYPTO_C */