blob: 6e90f1cea6f508be032c24b0ca3f747b463f5e50 [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"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010031
32#include <stdlib.h>
33#include <string.h>
34#include "mbedtls/platform.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035
36#include <mbedtls/rsa.h>
37#include <mbedtls/error.h>
38#include <mbedtls/pk.h>
Dave Rodgman3b5e6f02021-04-06 17:58:16 +010039#include "pk_wrap.h"
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020040#include "hash_info.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) || \
46 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
47 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) ||
126 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
127 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100128
Ronald Cron0266cfe2021-03-13 18:50:11 +0100129#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
130 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100131
Ronald Cron0266cfe2021-03-13 18:50:11 +0100132psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100133 const psa_key_attributes_t *attributes,
134 const uint8_t *data, size_t data_length,
135 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100137{
138 psa_status_t status;
139 mbedtls_rsa_context *rsa = NULL;
140
141 /* Parse input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
143 data,
144 data_length,
145 &rsa);
146 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100147 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100151
152 /* Re-export the data to PSA export format, such that we can store export
153 * representation in the key slot. Export representation in case of RSA is
154 * the smallest representation that's allowed as input, so a straight-up
155 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 status = mbedtls_psa_rsa_export_key(attributes->core.type,
157 rsa,
158 key_buffer,
159 key_buffer_size,
160 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100161exit:
162 /* Always free the RSA object */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 mbedtls_rsa_free(rsa);
164 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100167}
168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
170 mbedtls_rsa_context *rsa,
171 uint8_t *data,
172 size_t data_size,
173 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100174{
175#if defined(MBEDTLS_PK_WRITE_C)
176 int ret;
177 mbedtls_pk_context pk;
178 uint8_t *pos = data + data_size;
179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 mbedtls_pk_init(&pk);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100181 pk.pk_info = &mbedtls_rsa_info;
182 pk.pk_ctx = rsa;
183
184 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
185 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
186 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
188 ret = mbedtls_pk_write_key_der(&pk, data, data_size);
189 } else {
190 ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
191 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100194 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 memset(data, 0, data_size);
196 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100197 }
198
199 /* The mbedtls_pk_xxx functions write to the end of the buffer.
200 * Move the data to the beginning and erase remaining data
201 * at the original location. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (2 * (size_t) ret <= data_size) {
203 memcpy(data, data + data_size - ret, ret);
204 memset(data + data_size - ret, 0, ret);
205 } else if ((size_t) ret < data_size) {
206 memmove(data, data + data_size - ret, ret);
207 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100208 }
209
210 *data_length = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100212#else
213 (void) type;
214 (void) rsa;
215 (void) data;
216 (void) data_size;
217 (void) data_length;
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 return PSA_ERROR_NOT_SUPPORTED;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100219#endif /* MBEDTLS_PK_WRITE_C */
220}
221
Ronald Cron0266cfe2021-03-13 18:50:11 +0100222psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100223 const psa_key_attributes_t *attributes,
224 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100226{
227 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
228 mbedtls_rsa_context *rsa = NULL;
229
230 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 attributes->core.type, key_buffer, key_buffer_size, &rsa);
232 if (status != PSA_SUCCESS) {
233 return status;
234 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
237 rsa,
238 data,
239 data_size,
240 data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 mbedtls_rsa_free(rsa);
243 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100246}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100247#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
248 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100249
Ronald Cron0266cfe2021-03-13 18:50:11 +0100250#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
Jaeden Amero424fa932021-05-14 08:34:32 +0100251 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100252static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
253 size_t domain_parameters_size,
254 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100255{
256 size_t i;
257 uint32_t acc = 0;
258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100260 *exponent = 65537;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100262 }
263
264 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
265 * support values that fit in a 32-bit integer, which is larger than
266 * int on just about every platform anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (domain_parameters_size > sizeof(acc)) {
268 return PSA_ERROR_NOT_SUPPORTED;
269 }
270 for (i = 0; i < domain_parameters_size; i++) {
271 acc = (acc << 8) | domain_parameters[i];
272 }
273 if (acc > INT_MAX) {
274 return PSA_ERROR_NOT_SUPPORTED;
275 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100276 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100277 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100278}
279
Ronald Cron0266cfe2021-03-13 18:50:11 +0100280psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100281 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100283{
284 psa_status_t status;
285 mbedtls_rsa_context rsa;
286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
287 int exponent;
288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 status = psa_rsa_read_exponent(attributes->domain_parameters,
290 attributes->domain_parameters_size,
291 &exponent);
292 if (status != PSA_SUCCESS) {
293 return status;
294 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 mbedtls_rsa_init(&rsa);
297 ret = mbedtls_rsa_gen_key(&rsa,
298 mbedtls_psa_get_random,
299 MBEDTLS_PSA_RANDOM_STATE,
300 (unsigned int) attributes->core.bits,
301 exponent);
302 if (ret != 0) {
303 return mbedtls_to_psa_error(ret);
304 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100305
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 status = mbedtls_psa_rsa_export_key(attributes->core.type,
307 &rsa, key_buffer, key_buffer_size,
308 key_buffer_length);
309 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100312}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100313#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Jaeden Amero424fa932021-05-14 08:34:32 +0100314 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100315
Ronald Cron7bdbca32020-12-09 13:34:54 +0100316/****************************************************************/
317/* Sign/verify hashes */
318/****************************************************************/
319
Ronald Cron0266cfe2021-03-13 18:50:11 +0100320#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
321 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100322
323/* Decode the hash algorithm from alg and store the mbedtls encoding in
324 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100325static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
326 size_t hash_length,
327 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100328{
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
330 *md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100331
332 /* The Mbed TLS RSA module uses an unsigned int for hash length
333 * parameters. Validate that it fits so that we don't risk an
334 * overflow later. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 if (hash_length > UINT_MAX) {
336 return PSA_ERROR_INVALID_ARGUMENT;
337 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100338
Janos Follath0af093b2021-06-07 14:34:10 +0100339 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
341 if (*md_alg == MBEDTLS_MD_NONE) {
342 return PSA_ERROR_NOT_SUPPORTED;
343 }
344 if (mbedtls_hash_info_get_size(*md_alg) != hash_length) {
345 return PSA_ERROR_INVALID_ARGUMENT;
346 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100347 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100348
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100350}
351
Ronald Cron0266cfe2021-03-13 18:50:11 +0100352psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100353 const psa_key_attributes_t *attributes,
354 const uint8_t *key_buffer, size_t key_buffer_size,
355 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100356 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100357{
358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
359 mbedtls_rsa_context *rsa = NULL;
360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
361 mbedtls_md_type_t md_alg;
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
364 key_buffer,
365 key_buffer_size,
366 &rsa);
367 if (status != PSA_SUCCESS) {
368 return status;
369 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100370
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
372 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100373 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100375
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100377 status = PSA_ERROR_BUFFER_TOO_SMALL;
378 goto exit;
379 }
380
Ronald Cron0266cfe2021-03-13 18:50:11 +0100381#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
383 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
384 MBEDTLS_MD_NONE);
385 if (ret == 0) {
386 ret = mbedtls_rsa_pkcs1_sign(rsa,
387 mbedtls_psa_get_random,
388 MBEDTLS_PSA_RANDOM_STATE,
389 md_alg,
390 (unsigned int) hash_length,
391 hash,
392 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200393 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100395#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
396#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (PSA_ALG_IS_RSA_PSS(alg)) {
398 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (ret == 0) {
401 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
402 mbedtls_psa_get_random,
403 MBEDTLS_PSA_RANDOM_STATE,
404 MBEDTLS_MD_NONE,
405 (unsigned int) hash_length,
406 hash,
407 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200408 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100410#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100411 {
412 status = PSA_ERROR_INVALID_ARGUMENT;
413 goto exit;
414 }
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 if (ret == 0) {
417 *signature_length = mbedtls_rsa_get_len(rsa);
418 }
419 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100420
421exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 mbedtls_rsa_free(rsa);
423 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100424
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100426}
427
Ronald Cron0266cfe2021-03-13 18:50:11 +0100428#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100429static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
430 const mbedtls_rsa_context *rsa,
431 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200432{
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
434 return MBEDTLS_RSA_SALT_LEN_ANY;
435 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200436 /* Otherwise: standard salt length, i.e. largest possible salt length
437 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200439 int hlen = (int) hash_length; // known to fit
440 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if (room < 0) {
442 return 0; // there is no valid signature in this case anyway
443 } else if (room > hlen) {
444 return hlen;
445 } else {
446 return room;
447 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200448}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100449#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200450
Ronald Cron0266cfe2021-03-13 18:50:11 +0100451psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100452 const psa_key_attributes_t *attributes,
453 const uint8_t *key_buffer, size_t key_buffer_size,
454 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100456{
457 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
458 mbedtls_rsa_context *rsa = NULL;
459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
460 mbedtls_md_type_t md_alg;
461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
463 key_buffer,
464 key_buffer_size,
465 &rsa);
466 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100467 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
471 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100472 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100476 status = PSA_ERROR_INVALID_SIGNATURE;
477 goto exit;
478 }
479
Ronald Cron0266cfe2021-03-13 18:50:11 +0100480#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
482 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
483 MBEDTLS_MD_NONE);
484 if (ret == 0) {
485 ret = mbedtls_rsa_pkcs1_verify(rsa,
486 md_alg,
487 (unsigned int) hash_length,
488 hash,
489 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200490 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100492#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
493#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (PSA_ALG_IS_RSA_PSS(alg)) {
495 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
496 if (ret == 0) {
497 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
498 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
499 md_alg,
500 (unsigned) hash_length,
501 hash,
502 md_alg,
503 slen,
504 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200505 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100507#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100508 {
509 status = PSA_ERROR_INVALID_ARGUMENT;
510 goto exit;
511 }
512
513 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
514 * the rest of the signature is invalid". This has little use in
515 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100517 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100518 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100519
520exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 mbedtls_rsa_free(rsa);
522 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100525}
526
Ronald Crond2fb8542020-12-09 15:18:01 +0100527#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
528 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
529
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100530/****************************************************************/
531/* Asymmetric cryptography */
532/****************************************************************/
533
534#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100535static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
536 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100537{
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
539 mbedtls_md_type_t md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100542}
543#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
546 const uint8_t *key_buffer,
547 size_t key_buffer_size,
548 psa_algorithm_t alg,
549 const uint8_t *input,
550 size_t input_length,
551 const uint8_t *salt,
552 size_t salt_length,
553 uint8_t *output,
554 size_t output_size,
555 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100556{
557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
558 (void) key_buffer;
559 (void) key_buffer_size;
560 (void) input;
561 (void) input_length;
562 (void) salt;
563 (void) salt_length;
564 (void) output;
565 (void) output_size;
566 (void) output_length;
567
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100569#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100571 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
573 key_buffer,
574 key_buffer_size,
575 &rsa);
576 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100577 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100581 status = PSA_ERROR_BUFFER_TOO_SMALL;
582 goto rsa_exit;
583 }
584#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
585 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100587#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
588 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 mbedtls_rsa_pkcs1_encrypt(rsa,
590 mbedtls_psa_get_random,
591 MBEDTLS_PSA_RANDOM_STATE,
592 input_length,
593 input,
594 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100595#else
596 status = PSA_ERROR_NOT_SUPPORTED;
597#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 } else
599 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100600#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
601 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 psa_rsa_oaep_set_padding_mode(alg, rsa));
603 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100604 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100606
607 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
609 mbedtls_psa_get_random,
610 MBEDTLS_PSA_RANDOM_STATE,
611 salt, salt_length,
612 input_length,
613 input,
614 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100615#else
616 status = PSA_ERROR_NOT_SUPPORTED;
617#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100619 status = PSA_ERROR_INVALID_ARGUMENT;
620 }
621#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100623rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (status == PSA_SUCCESS) {
625 *output_length = mbedtls_rsa_get_len(rsa);
626 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 mbedtls_rsa_free(rsa);
629 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100630#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
631 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100633 status = PSA_ERROR_NOT_SUPPORTED;
634 }
635
636 return status;
637}
638
Gilles Peskine449bd832023-01-11 14:50:10 +0100639psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
640 const uint8_t *key_buffer,
641 size_t key_buffer_size,
642 psa_algorithm_t alg,
643 const uint8_t *input,
644 size_t input_length,
645 const uint8_t *salt,
646 size_t salt_length,
647 uint8_t *output,
648 size_t output_size,
649 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100650{
651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
652 (void) key_buffer;
653 (void) key_buffer_size;
654 (void) input;
655 (void) input_length;
656 (void) salt;
657 (void) salt_length;
658 (void) output;
659 (void) output_size;
660 (void) output_length;
661
662 *output_length = 0;
663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100665#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100667 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
669 key_buffer,
670 key_buffer_size,
671 &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
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100677 status = PSA_ERROR_INVALID_ARGUMENT;
678 goto rsa_exit;
679 }
680#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
681 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100684#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
685 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 mbedtls_rsa_pkcs1_decrypt(rsa,
687 mbedtls_psa_get_random,
688 MBEDTLS_PSA_RANDOM_STATE,
689 output_length,
690 input,
691 output,
692 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100693#else
694 status = PSA_ERROR_NOT_SUPPORTED;
695#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 } else
697 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100698#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
699 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 psa_rsa_oaep_set_padding_mode(alg, rsa));
701 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100702 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100704
705 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
707 mbedtls_psa_get_random,
708 MBEDTLS_PSA_RANDOM_STATE,
709 salt, salt_length,
710 output_length,
711 input,
712 output,
713 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100714#else
715 status = PSA_ERROR_NOT_SUPPORTED;
716#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100718 status = PSA_ERROR_INVALID_ARGUMENT;
719 }
720
721#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100723rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 mbedtls_rsa_free(rsa);
725 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100726#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
727 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100729 status = PSA_ERROR_NOT_SUPPORTED;
730 }
731
732 return status;
733}
734
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100735#endif /* MBEDTLS_PSA_CRYPTO_C */