blob: 02cade2debd68fcc84b8ec476189af8aaf1e40ce [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{
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);
321 *md_alg = mbedtls_hash_info_md_from_psa(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);
530 mbedtls_md_type_t md_alg = mbedtls_hash_info_md_from_psa(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100531
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100533}
534#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
535
Gilles Peskine449bd832023-01-11 14:50:10 +0100536psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
537 const uint8_t *key_buffer,
538 size_t key_buffer_size,
539 psa_algorithm_t alg,
540 const uint8_t *input,
541 size_t input_length,
542 const uint8_t *salt,
543 size_t salt_length,
544 uint8_t *output,
545 size_t output_size,
546 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100547{
548 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
549 (void) key_buffer;
550 (void) key_buffer_size;
551 (void) input;
552 (void) input_length;
553 (void) salt;
554 (void) salt_length;
555 (void) output;
556 (void) output_size;
557 (void) output_length;
558
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100560#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100562 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
564 key_buffer,
565 key_buffer_size,
566 &rsa);
567 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100568 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100572 status = PSA_ERROR_BUFFER_TOO_SMALL;
573 goto rsa_exit;
574 }
575#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
576 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100578#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
579 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_rsa_pkcs1_encrypt(rsa,
581 mbedtls_psa_get_random,
582 MBEDTLS_PSA_RANDOM_STATE,
583 input_length,
584 input,
585 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100586#else
587 status = PSA_ERROR_NOT_SUPPORTED;
588#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 } else
590 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100591#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
592 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 psa_rsa_oaep_set_padding_mode(alg, rsa));
594 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100595 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100597
598 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
600 mbedtls_psa_get_random,
601 MBEDTLS_PSA_RANDOM_STATE,
602 salt, salt_length,
603 input_length,
604 input,
605 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100606#else
607 status = PSA_ERROR_NOT_SUPPORTED;
608#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100610 status = PSA_ERROR_INVALID_ARGUMENT;
611 }
612#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100613 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100614rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 if (status == PSA_SUCCESS) {
616 *output_length = mbedtls_rsa_get_len(rsa);
617 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100618
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 mbedtls_rsa_free(rsa);
620 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100621#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
622 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100624 status = PSA_ERROR_NOT_SUPPORTED;
625 }
626
627 return status;
628}
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
631 const uint8_t *key_buffer,
632 size_t key_buffer_size,
633 psa_algorithm_t alg,
634 const uint8_t *input,
635 size_t input_length,
636 const uint8_t *salt,
637 size_t salt_length,
638 uint8_t *output,
639 size_t output_size,
640 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100641{
642 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
643 (void) key_buffer;
644 (void) key_buffer_size;
645 (void) input;
646 (void) input_length;
647 (void) salt;
648 (void) salt_length;
649 (void) output;
650 (void) output_size;
651 (void) output_length;
652
653 *output_length = 0;
654
Gilles Peskine449bd832023-01-11 14:50:10 +0100655 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100656#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100658 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100659 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
660 key_buffer,
661 key_buffer_size,
662 &rsa);
663 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100664 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100668 status = PSA_ERROR_INVALID_ARGUMENT;
669 goto rsa_exit;
670 }
671#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
672 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100675#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
676 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 mbedtls_rsa_pkcs1_decrypt(rsa,
678 mbedtls_psa_get_random,
679 MBEDTLS_PSA_RANDOM_STATE,
680 output_length,
681 input,
682 output,
683 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100684#else
685 status = PSA_ERROR_NOT_SUPPORTED;
686#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 } else
688 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100689#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
690 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 psa_rsa_oaep_set_padding_mode(alg, rsa));
692 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100693 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100695
696 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
698 mbedtls_psa_get_random,
699 MBEDTLS_PSA_RANDOM_STATE,
700 salt, salt_length,
701 output_length,
702 input,
703 output,
704 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100705#else
706 status = PSA_ERROR_NOT_SUPPORTED;
707#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100709 status = PSA_ERROR_INVALID_ARGUMENT;
710 }
711
712#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100714rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 mbedtls_rsa_free(rsa);
716 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100717#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
718 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100720 status = PSA_ERROR_NOT_SUPPORTED;
721 }
722
723 return status;
724}
725
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100726#endif /* MBEDTLS_PSA_CRYPTO_C */