blob: 2f613b32dacf060abb1bc6a0655a0119afda5ad7 [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 Peskine2f107ae2024-02-28 01:26:46 +0100119 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 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 Peskine2f107ae2024-02-28 01:26:46 +0100133 status = mbedtls_psa_rsa_export_key(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 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 Peskine2f107ae2024-02-28 01:26:46 +0100199 attributes->type, key_buffer, key_buffer_size, &rsa);
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 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 Peskinebb6f3ff2024-02-16 00:51:07 +0100219static psa_status_t psa_rsa_read_exponent(const uint8_t *e_bytes,
220 size_t e_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100222{
223 size_t i;
224 uint32_t acc = 0;
225
Ronald Cron9e18fc12020-11-05 17:36:40 +0100226 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
227 * support values that fit in a 32-bit integer, which is larger than
228 * int on just about every platform anyway. */
Gilles Peskinebb6f3ff2024-02-16 00:51:07 +0100229 if (e_length > sizeof(acc)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 return PSA_ERROR_NOT_SUPPORTED;
231 }
Gilles Peskinebb6f3ff2024-02-16 00:51:07 +0100232 for (i = 0; i < e_length; i++) {
233 acc = (acc << 8) | e_bytes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 }
235 if (acc > INT_MAX) {
236 return PSA_ERROR_NOT_SUPPORTED;
237 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100238 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100240}
241
Ronald Cron0266cfe2021-03-13 18:50:11 +0100242psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100243 const psa_key_attributes_t *attributes,
Gilles Peskine4c32b692024-02-16 00:11:09 +0100244 const psa_key_production_parameters_t *params, size_t params_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100246{
247 psa_status_t status;
248 mbedtls_rsa_context rsa;
249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4c32b692024-02-16 00:11:09 +0100250 int exponent = 65537;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100251
Gilles Peskine4c32b692024-02-16 00:11:09 +0100252 if (params_data_length != 0) {
253 status = psa_rsa_read_exponent(params->data, params_data_length,
254 &exponent);
255 if (status != PSA_SUCCESS) {
256 return status;
257 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 mbedtls_rsa_init(&rsa);
261 ret = mbedtls_rsa_gen_key(&rsa,
262 mbedtls_psa_get_random,
263 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100264 (unsigned int) attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 exponent);
266 if (ret != 0) {
267 return mbedtls_to_psa_error(ret);
268 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100269
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100270 status = mbedtls_psa_rsa_export_key(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 &rsa, key_buffer, key_buffer_size,
272 key_buffer_length);
273 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100276}
Valerio Setti76df8c12023-07-11 14:11:28 +0200277#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100278
Ronald Cron7bdbca32020-12-09 13:34:54 +0100279/****************************************************************/
280/* Sign/verify hashes */
281/****************************************************************/
282
Ronald Cron0266cfe2021-03-13 18:50:11 +0100283#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
284 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100285
286/* Decode the hash algorithm from alg and store the mbedtls encoding in
287 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100288static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
289 size_t hash_length,
290 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100291{
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200293 *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100294
295 /* The Mbed TLS RSA module uses an unsigned int for hash length
296 * parameters. Validate that it fits so that we don't risk an
297 * overflow later. */
Dave Rodgman02a53d72023-09-28 17:17:07 +0100298#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (hash_length > UINT_MAX) {
300 return PSA_ERROR_INVALID_ARGUMENT;
301 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100302#endif
Ronald Cron7bdbca32020-12-09 13:34:54 +0100303
Janos Follath0af093b2021-06-07 14:34:10 +0100304 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
306 if (*md_alg == MBEDTLS_MD_NONE) {
307 return PSA_ERROR_NOT_SUPPORTED;
308 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200309 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 return PSA_ERROR_INVALID_ARGUMENT;
311 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100312 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100313
Gilles Peskine449bd832023-01-11 14:50:10 +0100314 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100315}
316
Ronald Cron0266cfe2021-03-13 18:50:11 +0100317psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100318 const psa_key_attributes_t *attributes,
319 const uint8_t *key_buffer, size_t key_buffer_size,
320 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100322{
323 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
324 mbedtls_rsa_context *rsa = NULL;
325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
326 mbedtls_md_type_t md_alg;
327
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100328 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 key_buffer,
330 key_buffer_size,
331 &rsa);
332 if (status != PSA_SUCCESS) {
333 return status;
334 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
337 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100338 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100342 status = PSA_ERROR_BUFFER_TOO_SMALL;
343 goto exit;
344 }
345
Ronald Cron0266cfe2021-03-13 18:50:11 +0100346#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
348 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
349 MBEDTLS_MD_NONE);
350 if (ret == 0) {
351 ret = mbedtls_rsa_pkcs1_sign(rsa,
352 mbedtls_psa_get_random,
353 MBEDTLS_PSA_RANDOM_STATE,
354 md_alg,
355 (unsigned int) hash_length,
356 hash,
357 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200358 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100360#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
361#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (PSA_ALG_IS_RSA_PSS(alg)) {
363 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (ret == 0) {
366 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
367 mbedtls_psa_get_random,
368 MBEDTLS_PSA_RANDOM_STATE,
369 MBEDTLS_MD_NONE,
370 (unsigned int) hash_length,
371 hash,
372 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200373 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100375#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100376 {
377 status = PSA_ERROR_INVALID_ARGUMENT;
378 goto exit;
379 }
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 if (ret == 0) {
382 *signature_length = mbedtls_rsa_get_len(rsa);
383 }
384 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100385
386exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 mbedtls_rsa_free(rsa);
388 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100391}
392
Ronald Cron0266cfe2021-03-13 18:50:11 +0100393#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100394static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
395 const mbedtls_rsa_context *rsa,
396 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200397{
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
399 return MBEDTLS_RSA_SALT_LEN_ANY;
400 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200401 /* Otherwise: standard salt length, i.e. largest possible salt length
402 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200404 int hlen = (int) hash_length; // known to fit
405 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (room < 0) {
407 return 0; // there is no valid signature in this case anyway
408 } else if (room > hlen) {
409 return hlen;
410 } else {
411 return room;
412 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200413}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100414#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200415
Ronald Cron0266cfe2021-03-13 18:50:11 +0100416psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100417 const psa_key_attributes_t *attributes,
418 const uint8_t *key_buffer, size_t key_buffer_size,
419 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100421{
422 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
423 mbedtls_rsa_context *rsa = NULL;
424 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
425 mbedtls_md_type_t md_alg;
426
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100427 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 key_buffer,
429 key_buffer_size,
430 &rsa);
431 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100432 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
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 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100441 status = PSA_ERROR_INVALID_SIGNATURE;
442 goto exit;
443 }
444
Ronald Cron0266cfe2021-03-13 18:50:11 +0100445#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
447 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
448 MBEDTLS_MD_NONE);
449 if (ret == 0) {
450 ret = mbedtls_rsa_pkcs1_verify(rsa,
451 md_alg,
452 (unsigned int) hash_length,
453 hash,
454 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200455 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100457#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
458#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 if (PSA_ALG_IS_RSA_PSS(alg)) {
460 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
461 if (ret == 0) {
462 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
463 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
464 md_alg,
465 (unsigned) hash_length,
466 hash,
467 md_alg,
468 slen,
469 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200470 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100472#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100473 {
474 status = PSA_ERROR_INVALID_ARGUMENT;
475 goto exit;
476 }
477
478 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
479 * the rest of the signature is invalid". This has little use in
480 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100482 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100484
485exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 mbedtls_rsa_free(rsa);
487 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100490}
491
Ronald Crond2fb8542020-12-09 15:18:01 +0100492#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
493 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
494
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100495/****************************************************************/
496/* Asymmetric cryptography */
497/****************************************************************/
498
499#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100500static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
501 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100502{
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200504 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100505
Manuel Pégourié-Gonnardeb592042023-05-31 10:48:38 +0200506 /* Just to get the error status right, as rsa_set_padding() doesn't
507 * distinguish between "bad RSA algorithm" and "unknown hash". */
508 if (mbedtls_md_info_from_type(md_alg) == NULL) {
509 return PSA_ERROR_NOT_SUPPORTED;
510 }
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100513}
514#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
517 const uint8_t *key_buffer,
518 size_t key_buffer_size,
519 psa_algorithm_t alg,
520 const uint8_t *input,
521 size_t input_length,
522 const uint8_t *salt,
523 size_t salt_length,
524 uint8_t *output,
525 size_t output_size,
526 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100527{
528 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
529 (void) key_buffer;
530 (void) key_buffer_size;
531 (void) input;
532 (void) input_length;
533 (void) salt;
534 (void) salt_length;
535 (void) output;
536 (void) output_size;
537 (void) output_length;
538
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100539 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100540#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100542 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100543 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 key_buffer,
545 key_buffer_size,
546 &rsa);
547 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100548 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100552 status = PSA_ERROR_BUFFER_TOO_SMALL;
553 goto rsa_exit;
554 }
555#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
556 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100558#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
559 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 mbedtls_rsa_pkcs1_encrypt(rsa,
561 mbedtls_psa_get_random,
562 MBEDTLS_PSA_RANDOM_STATE,
563 input_length,
564 input,
565 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100566#else
567 status = PSA_ERROR_NOT_SUPPORTED;
568#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 } else
570 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100571#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
572 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 psa_rsa_oaep_set_padding_mode(alg, rsa));
574 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100575 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100577
578 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
580 mbedtls_psa_get_random,
581 MBEDTLS_PSA_RANDOM_STATE,
582 salt, salt_length,
583 input_length,
584 input,
585 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100586#else
587 status = PSA_ERROR_NOT_SUPPORTED;
588#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100590 status = PSA_ERROR_INVALID_ARGUMENT;
591 }
592#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100594rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if (status == PSA_SUCCESS) {
596 *output_length = mbedtls_rsa_get_len(rsa);
597 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 mbedtls_rsa_free(rsa);
600 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100601#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
602 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100604 status = PSA_ERROR_NOT_SUPPORTED;
605 }
606
607 return status;
608}
609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
611 const uint8_t *key_buffer,
612 size_t key_buffer_size,
613 psa_algorithm_t alg,
614 const uint8_t *input,
615 size_t input_length,
616 const uint8_t *salt,
617 size_t salt_length,
618 uint8_t *output,
619 size_t output_size,
620 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100621{
622 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
623 (void) key_buffer;
624 (void) key_buffer_size;
625 (void) input;
626 (void) input_length;
627 (void) salt;
628 (void) salt_length;
629 (void) output;
630 (void) output_size;
631 (void) output_length;
632
633 *output_length = 0;
634
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100635 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100636#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100638 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100639 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 key_buffer,
641 key_buffer_size,
642 &rsa);
643 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100644 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100646
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100648 status = PSA_ERROR_INVALID_ARGUMENT;
649 goto rsa_exit;
650 }
651#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
652 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
653
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100655#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
656 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 mbedtls_rsa_pkcs1_decrypt(rsa,
658 mbedtls_psa_get_random,
659 MBEDTLS_PSA_RANDOM_STATE,
660 output_length,
661 input,
662 output,
663 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100664#else
665 status = PSA_ERROR_NOT_SUPPORTED;
666#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 } else
668 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100669#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
670 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 psa_rsa_oaep_set_padding_mode(alg, rsa));
672 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100673 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100675
676 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
678 mbedtls_psa_get_random,
679 MBEDTLS_PSA_RANDOM_STATE,
680 salt, salt_length,
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_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100689 status = PSA_ERROR_INVALID_ARGUMENT;
690 }
691
692#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100694rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 mbedtls_rsa_free(rsa);
696 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100697#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
698 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100699 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100700 status = PSA_ERROR_NOT_SUPPORTED;
701 }
702
703 return status;
704}
705
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100706#endif /* MBEDTLS_PSA_CRYPTO_C */