blob: 0becc6c4bd3bd708450f0016b467b5c66f9b71d6 [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) || \
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{
Ronald Crone5ca3d82020-11-26 16:36:16 +0100175 int ret;
176 mbedtls_pk_context pk;
177 uint8_t *pos = data + data_size;
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 mbedtls_pk_init(&pk);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100180 pk.pk_info = &mbedtls_rsa_info;
181 pk.pk_ctx = rsa;
182
183 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
184 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
185 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
187 ret = mbedtls_pk_write_key_der(&pk, data, data_size);
188 } else {
189 ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
190 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100193 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 memset(data, 0, data_size);
195 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100196 }
197
198 /* The mbedtls_pk_xxx functions write to the end of the buffer.
199 * Move the data to the beginning and erase remaining data
200 * at the original location. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (2 * (size_t) ret <= data_size) {
202 memcpy(data, data + data_size - ret, ret);
203 memset(data + data_size - ret, 0, ret);
204 } else if ((size_t) ret < data_size) {
205 memmove(data, data + data_size - ret, ret);
206 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100207 }
208
209 *data_length = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100211}
212
Ronald Cron0266cfe2021-03-13 18:50:11 +0100213psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100214 const psa_key_attributes_t *attributes,
215 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100217{
218 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
219 mbedtls_rsa_context *rsa = NULL;
220
221 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 attributes->core.type, key_buffer, key_buffer_size, &rsa);
223 if (status != PSA_SUCCESS) {
224 return status;
225 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
228 rsa,
229 data,
230 data_size,
231 data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 mbedtls_rsa_free(rsa);
234 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100237}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100238#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
239 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100240
Ronald Cron0266cfe2021-03-13 18:50:11 +0100241#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
Jaeden Amero424fa932021-05-14 08:34:32 +0100242 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100243static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
244 size_t domain_parameters_size,
245 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100246{
247 size_t i;
248 uint32_t acc = 0;
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100251 *exponent = 65537;
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100253 }
254
255 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
256 * support values that fit in a 32-bit integer, which is larger than
257 * int on just about every platform anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (domain_parameters_size > sizeof(acc)) {
259 return PSA_ERROR_NOT_SUPPORTED;
260 }
261 for (i = 0; i < domain_parameters_size; i++) {
262 acc = (acc << 8) | domain_parameters[i];
263 }
264 if (acc > INT_MAX) {
265 return PSA_ERROR_NOT_SUPPORTED;
266 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100267 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100269}
270
Ronald Cron0266cfe2021-03-13 18:50:11 +0100271psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100272 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100274{
275 psa_status_t status;
276 mbedtls_rsa_context rsa;
277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
278 int exponent;
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 status = psa_rsa_read_exponent(attributes->domain_parameters,
281 attributes->domain_parameters_size,
282 &exponent);
283 if (status != PSA_SUCCESS) {
284 return status;
285 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 mbedtls_rsa_init(&rsa);
288 ret = mbedtls_rsa_gen_key(&rsa,
289 mbedtls_psa_get_random,
290 MBEDTLS_PSA_RANDOM_STATE,
291 (unsigned int) attributes->core.bits,
292 exponent);
293 if (ret != 0) {
294 return mbedtls_to_psa_error(ret);
295 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 status = mbedtls_psa_rsa_export_key(attributes->core.type,
298 &rsa, key_buffer, key_buffer_size,
299 key_buffer_length);
300 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100303}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100304#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Jaeden Amero424fa932021-05-14 08:34:32 +0100305 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100306
Ronald Cron7bdbca32020-12-09 13:34:54 +0100307/****************************************************************/
308/* Sign/verify hashes */
309/****************************************************************/
310
Ronald Cron0266cfe2021-03-13 18:50:11 +0100311#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
312 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100313
314/* Decode the hash algorithm from alg and store the mbedtls encoding in
315 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100316static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
317 size_t hash_length,
318 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100319{
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200321 *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100322
323 /* The Mbed TLS RSA module uses an unsigned int for hash length
324 * parameters. Validate that it fits so that we don't risk an
325 * overflow later. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if (hash_length > UINT_MAX) {
327 return PSA_ERROR_INVALID_ARGUMENT;
328 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100329
Janos Follath0af093b2021-06-07 14:34:10 +0100330 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
332 if (*md_alg == MBEDTLS_MD_NONE) {
333 return PSA_ERROR_NOT_SUPPORTED;
334 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200335 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 return PSA_ERROR_INVALID_ARGUMENT;
337 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100338 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100341}
342
Ronald Cron0266cfe2021-03-13 18:50:11 +0100343psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100344 const psa_key_attributes_t *attributes,
345 const uint8_t *key_buffer, size_t key_buffer_size,
346 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100348{
349 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
350 mbedtls_rsa_context *rsa = NULL;
351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
352 mbedtls_md_type_t md_alg;
353
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
355 key_buffer,
356 key_buffer_size,
357 &rsa);
358 if (status != PSA_SUCCESS) {
359 return status;
360 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
363 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100364 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100368 status = PSA_ERROR_BUFFER_TOO_SMALL;
369 goto exit;
370 }
371
Ronald Cron0266cfe2021-03-13 18:50:11 +0100372#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
374 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
375 MBEDTLS_MD_NONE);
376 if (ret == 0) {
377 ret = mbedtls_rsa_pkcs1_sign(rsa,
378 mbedtls_psa_get_random,
379 MBEDTLS_PSA_RANDOM_STATE,
380 md_alg,
381 (unsigned int) hash_length,
382 hash,
383 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200384 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100386#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
387#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 if (PSA_ALG_IS_RSA_PSS(alg)) {
389 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 if (ret == 0) {
392 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
393 mbedtls_psa_get_random,
394 MBEDTLS_PSA_RANDOM_STATE,
395 MBEDTLS_MD_NONE,
396 (unsigned int) hash_length,
397 hash,
398 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200399 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100401#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100402 {
403 status = PSA_ERROR_INVALID_ARGUMENT;
404 goto exit;
405 }
406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (ret == 0) {
408 *signature_length = mbedtls_rsa_get_len(rsa);
409 }
410 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100411
412exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 mbedtls_rsa_free(rsa);
414 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100417}
418
Ronald Cron0266cfe2021-03-13 18:50:11 +0100419#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100420static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
421 const mbedtls_rsa_context *rsa,
422 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200423{
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
425 return MBEDTLS_RSA_SALT_LEN_ANY;
426 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200427 /* Otherwise: standard salt length, i.e. largest possible salt length
428 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200430 int hlen = (int) hash_length; // known to fit
431 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (room < 0) {
433 return 0; // there is no valid signature in this case anyway
434 } else if (room > hlen) {
435 return hlen;
436 } else {
437 return room;
438 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200439}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100440#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200441
Ronald Cron0266cfe2021-03-13 18:50:11 +0100442psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100443 const psa_key_attributes_t *attributes,
444 const uint8_t *key_buffer, size_t key_buffer_size,
445 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100447{
448 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
449 mbedtls_rsa_context *rsa = NULL;
450 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
451 mbedtls_md_type_t md_alg;
452
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
454 key_buffer,
455 key_buffer_size,
456 &rsa);
457 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100458 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100460
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
462 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100463 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100465
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100467 status = PSA_ERROR_INVALID_SIGNATURE;
468 goto exit;
469 }
470
Ronald Cron0266cfe2021-03-13 18:50:11 +0100471#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
473 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
474 MBEDTLS_MD_NONE);
475 if (ret == 0) {
476 ret = mbedtls_rsa_pkcs1_verify(rsa,
477 md_alg,
478 (unsigned int) hash_length,
479 hash,
480 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200481 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100483#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
484#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (PSA_ALG_IS_RSA_PSS(alg)) {
486 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
487 if (ret == 0) {
488 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
489 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
490 md_alg,
491 (unsigned) hash_length,
492 hash,
493 md_alg,
494 slen,
495 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200496 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100498#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100499 {
500 status = PSA_ERROR_INVALID_ARGUMENT;
501 goto exit;
502 }
503
504 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
505 * the rest of the signature is invalid". This has little use in
506 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100507 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100508 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100510
511exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 mbedtls_rsa_free(rsa);
513 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100516}
517
Ronald Crond2fb8542020-12-09 15:18:01 +0100518#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
519 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
520
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100521/****************************************************************/
522/* Asymmetric cryptography */
523/****************************************************************/
524
525#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100526static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
527 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100528{
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200530 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100531
Manuel Pégourié-Gonnardeb592042023-05-31 10:48:38 +0200532 /* Just to get the error status right, as rsa_set_padding() doesn't
533 * distinguish between "bad RSA algorithm" and "unknown hash". */
534 if (mbedtls_md_info_from_type(md_alg) == NULL) {
535 return PSA_ERROR_NOT_SUPPORTED;
536 }
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100539}
540#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
543 const uint8_t *key_buffer,
544 size_t key_buffer_size,
545 psa_algorithm_t alg,
546 const uint8_t *input,
547 size_t input_length,
548 const uint8_t *salt,
549 size_t salt_length,
550 uint8_t *output,
551 size_t output_size,
552 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100553{
554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
555 (void) key_buffer;
556 (void) key_buffer_size;
557 (void) input;
558 (void) input_length;
559 (void) salt;
560 (void) salt_length;
561 (void) output;
562 (void) output_size;
563 (void) output_length;
564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100566#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100568 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
570 key_buffer,
571 key_buffer_size,
572 &rsa);
573 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100574 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100578 status = PSA_ERROR_BUFFER_TOO_SMALL;
579 goto rsa_exit;
580 }
581#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
582 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100584#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
585 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 mbedtls_rsa_pkcs1_encrypt(rsa,
587 mbedtls_psa_get_random,
588 MBEDTLS_PSA_RANDOM_STATE,
589 input_length,
590 input,
591 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100592#else
593 status = PSA_ERROR_NOT_SUPPORTED;
594#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 } else
596 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100597#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
598 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 psa_rsa_oaep_set_padding_mode(alg, rsa));
600 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100601 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100603
604 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
606 mbedtls_psa_get_random,
607 MBEDTLS_PSA_RANDOM_STATE,
608 salt, salt_length,
609 input_length,
610 input,
611 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100612#else
613 status = PSA_ERROR_NOT_SUPPORTED;
614#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100616 status = PSA_ERROR_INVALID_ARGUMENT;
617 }
618#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100620rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (status == PSA_SUCCESS) {
622 *output_length = mbedtls_rsa_get_len(rsa);
623 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 mbedtls_rsa_free(rsa);
626 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100627#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
628 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100630 status = PSA_ERROR_NOT_SUPPORTED;
631 }
632
633 return status;
634}
635
Gilles Peskine449bd832023-01-11 14:50:10 +0100636psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
637 const uint8_t *key_buffer,
638 size_t key_buffer_size,
639 psa_algorithm_t alg,
640 const uint8_t *input,
641 size_t input_length,
642 const uint8_t *salt,
643 size_t salt_length,
644 uint8_t *output,
645 size_t output_size,
646 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100647{
648 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
649 (void) key_buffer;
650 (void) key_buffer_size;
651 (void) input;
652 (void) input_length;
653 (void) salt;
654 (void) salt_length;
655 (void) output;
656 (void) output_size;
657 (void) output_length;
658
659 *output_length = 0;
660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100662#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100664 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
666 key_buffer,
667 key_buffer_size,
668 &rsa);
669 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100670 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100672
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100674 status = PSA_ERROR_INVALID_ARGUMENT;
675 goto rsa_exit;
676 }
677#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
678 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100681#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
682 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 mbedtls_rsa_pkcs1_decrypt(rsa,
684 mbedtls_psa_get_random,
685 MBEDTLS_PSA_RANDOM_STATE,
686 output_length,
687 input,
688 output,
689 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100690#else
691 status = PSA_ERROR_NOT_SUPPORTED;
692#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 } else
694 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100695#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
696 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 psa_rsa_oaep_set_padding_mode(alg, rsa));
698 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100699 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100701
702 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
704 mbedtls_psa_get_random,
705 MBEDTLS_PSA_RANDOM_STATE,
706 salt, salt_length,
707 output_length,
708 input,
709 output,
710 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100711#else
712 status = PSA_ERROR_NOT_SUPPORTED;
713#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100715 status = PSA_ERROR_INVALID_ARGUMENT;
716 }
717
718#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100720rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 mbedtls_rsa_free(rsa);
722 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100723#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
724 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100726 status = PSA_ERROR_NOT_SUPPORTED;
727 }
728
729 return status;
730}
731
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100732#endif /* MBEDTLS_PSA_CRYPTO_C */