blob: 22b5f4cdf24e6a53f2a8a87b099208f5dfe09fd1 [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
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include <psa/crypto.h>
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +010026#include "psa/crypto_values.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010027#include "psa_crypto_core.h"
Ronald Cron9e18fc12020-11-05 17:36:40 +010028#include "psa_crypto_random_impl.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010029#include "psa_crypto_rsa.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010030#include "psa_crypto_hash.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020031#include "md_psa.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010032
33#include <stdlib.h>
34#include <string.h>
35#include "mbedtls/platform.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036
37#include <mbedtls/rsa.h>
38#include <mbedtls/error.h>
39#include <mbedtls/pk.h>
Dave Rodgman3b5e6f02021-04-06 17:58:16 +010040#include "pk_wrap.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041
42#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010043 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010044 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
45 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
Valerio Settib2bcedb2023-07-10 11:24:00 +020046 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010047 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010048
49/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
50 * that are not a multiple of 8) well. For example, there is only
51 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
52 * way to return the exact bit size of a key.
53 * To keep things simple, reject non-byte-aligned key sizes. */
54static psa_status_t psa_check_rsa_key_byte_aligned(
Gilles Peskine449bd832023-01-11 14:50:10 +010055 const mbedtls_rsa_context *rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010056{
57 mbedtls_mpi n;
58 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +010059 mbedtls_mpi_init(&n);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010060 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
62 if (status == PSA_SUCCESS) {
63 if (mbedtls_mpi_bitlen(&n) % 8 != 0) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010064 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010065 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010066 }
Gilles Peskine449bd832023-01-11 14:50:10 +010067 mbedtls_mpi_free(&n);
68 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010069}
70
71psa_status_t mbedtls_psa_rsa_load_representation(
72 psa_key_type_t type, const uint8_t *data, size_t data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +010073 mbedtls_rsa_context **p_rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010074{
75 psa_status_t status;
76 mbedtls_pk_context ctx;
77 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_pk_init(&ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010079
80 /* Parse the data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010081 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010082 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_pk_parse_key(&ctx, data, data_length, NULL, 0,
84 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE));
85 } else {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010086 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010087 mbedtls_pk_parse_public_key(&ctx, data, data_length));
88 }
89 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010090 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010091 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010092
93 /* We have something that the pkparse module recognizes. If it is a
94 * valid RSA key, store it. */
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if (mbedtls_pk_get_type(&ctx) != MBEDTLS_PK_RSA) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010096 status = PSA_ERROR_INVALID_ARGUMENT;
97 goto exit;
98 }
99
100 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
101 * supports non-byte-aligned key sizes, but not well. For example,
102 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(mbedtls_pk_rsa(ctx)));
104 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100105 status = PSA_ERROR_NOT_SUPPORTED;
106 goto exit;
107 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 status = psa_check_rsa_key_byte_aligned(mbedtls_pk_rsa(ctx));
109 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100112
113 /* Copy out the pointer to the RSA context, and reset the PK context
114 * such that pk_free doesn't free the RSA context we just grabbed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 *p_rsa = mbedtls_pk_rsa(ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100116 ctx.pk_info = NULL;
117
118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_pk_free(&ctx);
120 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100121}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100122#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100123 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100124 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
125 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200126 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100127 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100128
Valerio Settib2bcedb2023-07-10 11:24:00 +0200129#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +0100130 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron0266cfe2021-03-13 18:50:11 +0100131psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100132 const psa_key_attributes_t *attributes,
133 const uint8_t *data, size_t data_length,
134 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100136{
137 psa_status_t status;
138 mbedtls_rsa_context *rsa = NULL;
139
140 /* Parse input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
142 data,
143 data_length,
144 &rsa);
145 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100150
151 /* Re-export the data to PSA export format, such that we can store export
152 * representation in the key slot. Export representation in case of RSA is
153 * the smallest representation that's allowed as input, so a straight-up
154 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 status = mbedtls_psa_rsa_export_key(attributes->core.type,
156 rsa,
157 key_buffer,
158 key_buffer_size,
159 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100160exit:
161 /* Always free the RSA object */
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 mbedtls_rsa_free(rsa);
163 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100166}
Valerio Settib2bcedb2023-07-10 11:24:00 +0200167#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) ||
168 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronabf2aef2020-11-27 18:13:44 +0100169
Valerio Settib2bcedb2023-07-10 11:24:00 +0200170#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
171 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100172psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
173 mbedtls_rsa_context *rsa,
174 uint8_t *data,
175 size_t data_size,
176 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100177{
Ronald Crone5ca3d82020-11-26 16:36:16 +0100178 int ret;
179 mbedtls_pk_context pk;
180 uint8_t *pos = data + data_size;
181
Gilles Peskine449bd832023-01-11 14:50:10 +0100182 mbedtls_pk_init(&pk);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100183 pk.pk_info = &mbedtls_rsa_info;
184 pk.pk_ctx = rsa;
185
186 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
187 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
188 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
190 ret = mbedtls_pk_write_key_der(&pk, data, data_size);
191 } else {
192 ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
193 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100196 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 memset(data, 0, data_size);
198 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100199 }
200
201 /* The mbedtls_pk_xxx functions write to the end of the buffer.
202 * Move the data to the beginning and erase remaining data
203 * at the original location. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (2 * (size_t) ret <= data_size) {
205 memcpy(data, data + data_size - ret, ret);
206 memset(data + data_size - ret, 0, ret);
207 } else if ((size_t) ret < data_size) {
208 memmove(data, data + data_size - ret, ret);
209 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100210 }
211
212 *data_length = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100214}
215
Ronald Cron0266cfe2021-03-13 18:50:11 +0100216psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100217 const psa_key_attributes_t *attributes,
218 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100220{
221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
222 mbedtls_rsa_context *rsa = NULL;
223
224 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 attributes->core.type, key_buffer, key_buffer_size, &rsa);
226 if (status != PSA_SUCCESS) {
227 return status;
228 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
231 rsa,
232 data,
233 data_size,
234 data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 mbedtls_rsa_free(rsa);
237 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100240}
Valerio Settib2bcedb2023-07-10 11:24:00 +0200241#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100242 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100243
Valerio Settib2bcedb2023-07-10 11:24:00 +0200244#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) && \
Jaeden Amero424fa932021-05-14 08:34:32 +0100245 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100246static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
247 size_t domain_parameters_size,
248 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100249{
250 size_t i;
251 uint32_t acc = 0;
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100254 *exponent = 65537;
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100256 }
257
258 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
259 * support values that fit in a 32-bit integer, which is larger than
260 * int on just about every platform anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if (domain_parameters_size > sizeof(acc)) {
262 return PSA_ERROR_NOT_SUPPORTED;
263 }
264 for (i = 0; i < domain_parameters_size; i++) {
265 acc = (acc << 8) | domain_parameters[i];
266 }
267 if (acc > INT_MAX) {
268 return PSA_ERROR_NOT_SUPPORTED;
269 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100270 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100272}
273
Ronald Cron0266cfe2021-03-13 18:50:11 +0100274psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100275 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100277{
278 psa_status_t status;
279 mbedtls_rsa_context rsa;
280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
281 int exponent;
282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 status = psa_rsa_read_exponent(attributes->domain_parameters,
284 attributes->domain_parameters_size,
285 &exponent);
286 if (status != PSA_SUCCESS) {
287 return status;
288 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 mbedtls_rsa_init(&rsa);
291 ret = mbedtls_rsa_gen_key(&rsa,
292 mbedtls_psa_get_random,
293 MBEDTLS_PSA_RANDOM_STATE,
294 (unsigned int) attributes->core.bits,
295 exponent);
296 if (ret != 0) {
297 return mbedtls_to_psa_error(ret);
298 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 status = mbedtls_psa_rsa_export_key(attributes->core.type,
301 &rsa, key_buffer, key_buffer_size,
302 key_buffer_length);
303 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100304
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100306}
Valerio Settib2bcedb2023-07-10 11:24:00 +0200307#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Jaeden Amero424fa932021-05-14 08:34:32 +0100308 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100309
Ronald Cron7bdbca32020-12-09 13:34:54 +0100310/****************************************************************/
311/* Sign/verify hashes */
312/****************************************************************/
313
Ronald Cron0266cfe2021-03-13 18:50:11 +0100314#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
315 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100316
317/* Decode the hash algorithm from alg and store the mbedtls encoding in
318 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100319static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
320 size_t hash_length,
321 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100322{
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200324 *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100325
326 /* The Mbed TLS RSA module uses an unsigned int for hash length
327 * parameters. Validate that it fits so that we don't risk an
328 * overflow later. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 if (hash_length > UINT_MAX) {
330 return PSA_ERROR_INVALID_ARGUMENT;
331 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100332
Janos Follath0af093b2021-06-07 14:34:10 +0100333 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
335 if (*md_alg == MBEDTLS_MD_NONE) {
336 return PSA_ERROR_NOT_SUPPORTED;
337 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200338 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 return PSA_ERROR_INVALID_ARGUMENT;
340 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100341 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100344}
345
Ronald Cron0266cfe2021-03-13 18:50:11 +0100346psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100347 const psa_key_attributes_t *attributes,
348 const uint8_t *key_buffer, size_t key_buffer_size,
349 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100351{
352 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
353 mbedtls_rsa_context *rsa = NULL;
354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
355 mbedtls_md_type_t md_alg;
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
358 key_buffer,
359 key_buffer_size,
360 &rsa);
361 if (status != PSA_SUCCESS) {
362 return status;
363 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
366 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100367 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100371 status = PSA_ERROR_BUFFER_TOO_SMALL;
372 goto exit;
373 }
374
Ronald Cron0266cfe2021-03-13 18:50:11 +0100375#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
377 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
378 MBEDTLS_MD_NONE);
379 if (ret == 0) {
380 ret = mbedtls_rsa_pkcs1_sign(rsa,
381 mbedtls_psa_get_random,
382 MBEDTLS_PSA_RANDOM_STATE,
383 md_alg,
384 (unsigned int) hash_length,
385 hash,
386 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200387 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100389#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
390#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (PSA_ALG_IS_RSA_PSS(alg)) {
392 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 if (ret == 0) {
395 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
396 mbedtls_psa_get_random,
397 MBEDTLS_PSA_RANDOM_STATE,
398 MBEDTLS_MD_NONE,
399 (unsigned int) hash_length,
400 hash,
401 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200402 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100404#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100405 {
406 status = PSA_ERROR_INVALID_ARGUMENT;
407 goto exit;
408 }
409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (ret == 0) {
411 *signature_length = mbedtls_rsa_get_len(rsa);
412 }
413 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100414
415exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 mbedtls_rsa_free(rsa);
417 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100418
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100420}
421
Ronald Cron0266cfe2021-03-13 18:50:11 +0100422#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100423static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
424 const mbedtls_rsa_context *rsa,
425 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200426{
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
428 return MBEDTLS_RSA_SALT_LEN_ANY;
429 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200430 /* Otherwise: standard salt length, i.e. largest possible salt length
431 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200433 int hlen = (int) hash_length; // known to fit
434 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (room < 0) {
436 return 0; // there is no valid signature in this case anyway
437 } else if (room > hlen) {
438 return hlen;
439 } else {
440 return room;
441 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200442}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100443#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200444
Ronald Cron0266cfe2021-03-13 18:50:11 +0100445psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100446 const psa_key_attributes_t *attributes,
447 const uint8_t *key_buffer, size_t key_buffer_size,
448 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100450{
451 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
452 mbedtls_rsa_context *rsa = NULL;
453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
454 mbedtls_md_type_t md_alg;
455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
457 key_buffer,
458 key_buffer_size,
459 &rsa);
460 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100461 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
465 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100466 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100470 status = PSA_ERROR_INVALID_SIGNATURE;
471 goto exit;
472 }
473
Ronald Cron0266cfe2021-03-13 18:50:11 +0100474#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
476 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
477 MBEDTLS_MD_NONE);
478 if (ret == 0) {
479 ret = mbedtls_rsa_pkcs1_verify(rsa,
480 md_alg,
481 (unsigned int) hash_length,
482 hash,
483 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200484 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100486#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
487#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 if (PSA_ALG_IS_RSA_PSS(alg)) {
489 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
490 if (ret == 0) {
491 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
492 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
493 md_alg,
494 (unsigned) hash_length,
495 hash,
496 md_alg,
497 slen,
498 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200499 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100501#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100502 {
503 status = PSA_ERROR_INVALID_ARGUMENT;
504 goto exit;
505 }
506
507 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
508 * the rest of the signature is invalid". This has little use in
509 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100511 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100513
514exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 mbedtls_rsa_free(rsa);
516 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100517
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100519}
520
Ronald Crond2fb8542020-12-09 15:18:01 +0100521#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
522 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
523
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100524/****************************************************************/
525/* Asymmetric cryptography */
526/****************************************************************/
527
528#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100529static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
530 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100531{
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200533 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100536}
537#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
540 const uint8_t *key_buffer,
541 size_t key_buffer_size,
542 psa_algorithm_t alg,
543 const uint8_t *input,
544 size_t input_length,
545 const uint8_t *salt,
546 size_t salt_length,
547 uint8_t *output,
548 size_t output_size,
549 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100550{
551 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
552 (void) key_buffer;
553 (void) key_buffer_size;
554 (void) input;
555 (void) input_length;
556 (void) salt;
557 (void) salt_length;
558 (void) output;
559 (void) output_size;
560 (void) output_length;
561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100563#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100565 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
567 key_buffer,
568 key_buffer_size,
569 &rsa);
570 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100571 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100575 status = PSA_ERROR_BUFFER_TOO_SMALL;
576 goto rsa_exit;
577 }
578#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
579 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100581#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
582 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 mbedtls_rsa_pkcs1_encrypt(rsa,
584 mbedtls_psa_get_random,
585 MBEDTLS_PSA_RANDOM_STATE,
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_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 } else
593 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100594#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
595 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 psa_rsa_oaep_set_padding_mode(alg, rsa));
597 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100598 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100600
601 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
603 mbedtls_psa_get_random,
604 MBEDTLS_PSA_RANDOM_STATE,
605 salt, salt_length,
606 input_length,
607 input,
608 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100609#else
610 status = PSA_ERROR_NOT_SUPPORTED;
611#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100613 status = PSA_ERROR_INVALID_ARGUMENT;
614 }
615#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100617rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 if (status == PSA_SUCCESS) {
619 *output_length = mbedtls_rsa_get_len(rsa);
620 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100621
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 mbedtls_rsa_free(rsa);
623 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100624#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
625 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100627 status = PSA_ERROR_NOT_SUPPORTED;
628 }
629
630 return status;
631}
632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
634 const uint8_t *key_buffer,
635 size_t key_buffer_size,
636 psa_algorithm_t alg,
637 const uint8_t *input,
638 size_t input_length,
639 const uint8_t *salt,
640 size_t salt_length,
641 uint8_t *output,
642 size_t output_size,
643 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100644{
645 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
646 (void) key_buffer;
647 (void) key_buffer_size;
648 (void) input;
649 (void) input_length;
650 (void) salt;
651 (void) salt_length;
652 (void) output;
653 (void) output_size;
654 (void) output_length;
655
656 *output_length = 0;
657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100659#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100661 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
663 key_buffer,
664 key_buffer_size,
665 &rsa);
666 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100667 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100669
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100671 status = PSA_ERROR_INVALID_ARGUMENT;
672 goto rsa_exit;
673 }
674#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
675 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100678#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
679 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 mbedtls_rsa_pkcs1_decrypt(rsa,
681 mbedtls_psa_get_random,
682 MBEDTLS_PSA_RANDOM_STATE,
683 output_length,
684 input,
685 output,
686 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100687#else
688 status = PSA_ERROR_NOT_SUPPORTED;
689#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 } else
691 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100692#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
693 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 psa_rsa_oaep_set_padding_mode(alg, rsa));
695 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100696 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100698
699 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
701 mbedtls_psa_get_random,
702 MBEDTLS_PSA_RANDOM_STATE,
703 salt, salt_length,
704 output_length,
705 input,
706 output,
707 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100708#else
709 status = PSA_ERROR_NOT_SUPPORTED;
710#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100712 status = PSA_ERROR_INVALID_ARGUMENT;
713 }
714
715#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100717rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100718 mbedtls_rsa_free(rsa);
719 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100720#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
721 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100723 status = PSA_ERROR_NOT_SUPPORTED;
724 }
725
726 return status;
727}
728
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100729#endif /* MBEDTLS_PSA_CRYPTO_C */