blob: bb8371a885ac1709e8cf33f67c12f15cc9f54ce9 [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"
Manuel Pégourié-Gonnard47728842022-07-18 13:00:40 +020041#include "hash_info.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010042
43#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010044 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010045 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
46 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
47 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
48 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010049
50/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
51 * that are not a multiple of 8) well. For example, there is only
52 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
53 * way to return the exact bit size of a key.
54 * To keep things simple, reject non-byte-aligned key sizes. */
55static psa_status_t psa_check_rsa_key_byte_aligned(
Gilles Peskine449bd832023-01-11 14:50:10 +010056 const mbedtls_rsa_context *rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010057{
58 mbedtls_mpi n;
59 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +010060 mbedtls_mpi_init(&n);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010061 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010062 mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
63 if (status == PSA_SUCCESS) {
64 if (mbedtls_mpi_bitlen(&n) % 8 != 0) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010065 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010066 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010067 }
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_mpi_free(&n);
69 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010070}
71
72psa_status_t mbedtls_psa_rsa_load_representation(
73 psa_key_type_t type, const uint8_t *data, size_t data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +010074 mbedtls_rsa_context **p_rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010075{
76 psa_status_t status;
77 mbedtls_pk_context ctx;
78 size_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +010079 mbedtls_pk_init(&ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010080
81 /* Parse the data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010082 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010083 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010084 mbedtls_pk_parse_key(&ctx, data, data_length, NULL, 0,
85 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE));
86 } else {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010087 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_pk_parse_public_key(&ctx, data, data_length));
89 }
90 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010091 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010092 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010093
94 /* We have something that the pkparse module recognizes. If it is a
95 * valid RSA key, store it. */
Gilles Peskine449bd832023-01-11 14:50:10 +010096 if (mbedtls_pk_get_type(&ctx) != MBEDTLS_PK_RSA) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010097 status = PSA_ERROR_INVALID_ARGUMENT;
98 goto exit;
99 }
100
101 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
102 * supports non-byte-aligned key sizes, but not well. For example,
103 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(mbedtls_pk_rsa(ctx)));
105 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100106 status = PSA_ERROR_NOT_SUPPORTED;
107 goto exit;
108 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 status = psa_check_rsa_key_byte_aligned(mbedtls_pk_rsa(ctx));
110 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100113
114 /* Copy out the pointer to the RSA context, and reset the PK context
115 * such that pk_free doesn't free the RSA context we just grabbed. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 *p_rsa = mbedtls_pk_rsa(ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100117 ctx.pk_info = NULL;
118
119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_pk_free(&ctx);
121 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100122}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100123#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100124 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100125 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
126 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
127 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
128 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100129
Ronald Cron0266cfe2021-03-13 18:50:11 +0100130#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100132
Ronald Cron0266cfe2021-03-13 18:50:11 +0100133psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100134 const psa_key_attributes_t *attributes,
135 const uint8_t *data, size_t data_length,
136 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100138{
139 psa_status_t status;
140 mbedtls_rsa_context *rsa = NULL;
141
142 /* Parse input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
144 data,
145 data_length,
146 &rsa);
147 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100148 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100152
153 /* Re-export the data to PSA export format, such that we can store export
154 * representation in the key slot. Export representation in case of RSA is
155 * the smallest representation that's allowed as input, so a straight-up
156 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 status = mbedtls_psa_rsa_export_key(attributes->core.type,
158 rsa,
159 key_buffer,
160 key_buffer_size,
161 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100162exit:
163 /* Always free the RSA object */
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_rsa_free(rsa);
165 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100168}
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
171 mbedtls_rsa_context *rsa,
172 uint8_t *data,
173 size_t data_size,
174 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100175{
Ronald Crone5ca3d82020-11-26 16:36:16 +0100176 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}
213
Ronald Cron0266cfe2021-03-13 18:50:11 +0100214psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100215 const psa_key_attributes_t *attributes,
216 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100218{
219 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
220 mbedtls_rsa_context *rsa = NULL;
221
222 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 attributes->core.type, key_buffer, key_buffer_size, &rsa);
224 if (status != PSA_SUCCESS) {
225 return status;
226 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
229 rsa,
230 data,
231 data_size,
232 data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_rsa_free(rsa);
235 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100238}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100239#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
240 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100241
Ronald Cron0266cfe2021-03-13 18:50:11 +0100242#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
Jaeden Amero424fa932021-05-14 08:34:32 +0100243 defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100244static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
245 size_t domain_parameters_size,
246 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100247{
248 size_t i;
249 uint32_t acc = 0;
250
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100252 *exponent = 65537;
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100254 }
255
256 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
257 * support values that fit in a 32-bit integer, which is larger than
258 * int on just about every platform anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 if (domain_parameters_size > sizeof(acc)) {
260 return PSA_ERROR_NOT_SUPPORTED;
261 }
262 for (i = 0; i < domain_parameters_size; i++) {
263 acc = (acc << 8) | domain_parameters[i];
264 }
265 if (acc > INT_MAX) {
266 return PSA_ERROR_NOT_SUPPORTED;
267 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100268 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100270}
271
Ronald Cron0266cfe2021-03-13 18:50:11 +0100272psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100273 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100275{
276 psa_status_t status;
277 mbedtls_rsa_context rsa;
278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
279 int exponent;
280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 status = psa_rsa_read_exponent(attributes->domain_parameters,
282 attributes->domain_parameters_size,
283 &exponent);
284 if (status != PSA_SUCCESS) {
285 return status;
286 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 mbedtls_rsa_init(&rsa);
289 ret = mbedtls_rsa_gen_key(&rsa,
290 mbedtls_psa_get_random,
291 MBEDTLS_PSA_RANDOM_STATE,
292 (unsigned int) attributes->core.bits,
293 exponent);
294 if (ret != 0) {
295 return mbedtls_to_psa_error(ret);
296 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 status = mbedtls_psa_rsa_export_key(attributes->core.type,
299 &rsa, key_buffer, key_buffer_size,
300 key_buffer_length);
301 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100304}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100305#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Jaeden Amero424fa932021-05-14 08:34:32 +0100306 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100307
Ronald Cron7bdbca32020-12-09 13:34:54 +0100308/****************************************************************/
309/* Sign/verify hashes */
310/****************************************************************/
311
Ronald Cron0266cfe2021-03-13 18:50:11 +0100312#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
313 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100314
315/* Decode the hash algorithm from alg and store the mbedtls encoding in
316 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100317static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
318 size_t hash_length,
319 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100320{
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200322 *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100323
324 /* The Mbed TLS RSA module uses an unsigned int for hash length
325 * parameters. Validate that it fits so that we don't risk an
326 * overflow later. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 if (hash_length > UINT_MAX) {
328 return PSA_ERROR_INVALID_ARGUMENT;
329 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100330
Janos Follath0af093b2021-06-07 14:34:10 +0100331 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
333 if (*md_alg == MBEDTLS_MD_NONE) {
334 return PSA_ERROR_NOT_SUPPORTED;
335 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200336 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 return PSA_ERROR_INVALID_ARGUMENT;
338 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100339 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100342}
343
Ronald Cron0266cfe2021-03-13 18:50:11 +0100344psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100345 const psa_key_attributes_t *attributes,
346 const uint8_t *key_buffer, size_t key_buffer_size,
347 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100349{
350 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
351 mbedtls_rsa_context *rsa = NULL;
352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
353 mbedtls_md_type_t md_alg;
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
356 key_buffer,
357 key_buffer_size,
358 &rsa);
359 if (status != PSA_SUCCESS) {
360 return status;
361 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
364 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100365 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100369 status = PSA_ERROR_BUFFER_TOO_SMALL;
370 goto exit;
371 }
372
Ronald Cron0266cfe2021-03-13 18:50:11 +0100373#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
375 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
376 MBEDTLS_MD_NONE);
377 if (ret == 0) {
378 ret = mbedtls_rsa_pkcs1_sign(rsa,
379 mbedtls_psa_get_random,
380 MBEDTLS_PSA_RANDOM_STATE,
381 md_alg,
382 (unsigned int) hash_length,
383 hash,
384 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200385 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100387#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
388#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 if (PSA_ALG_IS_RSA_PSS(alg)) {
390 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (ret == 0) {
393 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
394 mbedtls_psa_get_random,
395 MBEDTLS_PSA_RANDOM_STATE,
396 MBEDTLS_MD_NONE,
397 (unsigned int) hash_length,
398 hash,
399 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200400 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100402#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100403 {
404 status = PSA_ERROR_INVALID_ARGUMENT;
405 goto exit;
406 }
407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (ret == 0) {
409 *signature_length = mbedtls_rsa_get_len(rsa);
410 }
411 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100412
413exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 mbedtls_rsa_free(rsa);
415 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100416
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100418}
419
Ronald Cron0266cfe2021-03-13 18:50:11 +0100420#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100421static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
422 const mbedtls_rsa_context *rsa,
423 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200424{
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
426 return MBEDTLS_RSA_SALT_LEN_ANY;
427 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200428 /* Otherwise: standard salt length, i.e. largest possible salt length
429 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200431 int hlen = (int) hash_length; // known to fit
432 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (room < 0) {
434 return 0; // there is no valid signature in this case anyway
435 } else if (room > hlen) {
436 return hlen;
437 } else {
438 return room;
439 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200440}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100441#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200442
Ronald Cron0266cfe2021-03-13 18:50:11 +0100443psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100444 const psa_key_attributes_t *attributes,
445 const uint8_t *key_buffer, size_t key_buffer_size,
446 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100448{
449 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
450 mbedtls_rsa_context *rsa = NULL;
451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
452 mbedtls_md_type_t md_alg;
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
455 key_buffer,
456 key_buffer_size,
457 &rsa);
458 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100459 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100461
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
463 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100464 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100468 status = PSA_ERROR_INVALID_SIGNATURE;
469 goto exit;
470 }
471
Ronald Cron0266cfe2021-03-13 18:50:11 +0100472#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
474 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
475 MBEDTLS_MD_NONE);
476 if (ret == 0) {
477 ret = mbedtls_rsa_pkcs1_verify(rsa,
478 md_alg,
479 (unsigned int) hash_length,
480 hash,
481 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200482 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100484#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
485#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 if (PSA_ALG_IS_RSA_PSS(alg)) {
487 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
488 if (ret == 0) {
489 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
490 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
491 md_alg,
492 (unsigned) hash_length,
493 hash,
494 md_alg,
495 slen,
496 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200497 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100499#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100500 {
501 status = PSA_ERROR_INVALID_ARGUMENT;
502 goto exit;
503 }
504
505 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
506 * the rest of the signature is invalid". This has little use in
507 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100509 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100511
512exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 mbedtls_rsa_free(rsa);
514 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100515
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100517}
518
Ronald Crond2fb8542020-12-09 15:18:01 +0100519#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
520 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
521
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100522/****************************************************************/
523/* Asymmetric cryptography */
524/****************************************************************/
525
526#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100527static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
528 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100529{
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200531 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100532
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100534}
535#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
538 const uint8_t *key_buffer,
539 size_t key_buffer_size,
540 psa_algorithm_t alg,
541 const uint8_t *input,
542 size_t input_length,
543 const uint8_t *salt,
544 size_t salt_length,
545 uint8_t *output,
546 size_t output_size,
547 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100548{
549 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
550 (void) key_buffer;
551 (void) key_buffer_size;
552 (void) input;
553 (void) input_length;
554 (void) salt;
555 (void) salt_length;
556 (void) output;
557 (void) output_size;
558 (void) output_length;
559
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100561#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100563 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
565 key_buffer,
566 key_buffer_size,
567 &rsa);
568 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100569 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100573 status = PSA_ERROR_BUFFER_TOO_SMALL;
574 goto rsa_exit;
575 }
576#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
577 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100579#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
580 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 mbedtls_rsa_pkcs1_encrypt(rsa,
582 mbedtls_psa_get_random,
583 MBEDTLS_PSA_RANDOM_STATE,
584 input_length,
585 input,
586 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100587#else
588 status = PSA_ERROR_NOT_SUPPORTED;
589#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 } else
591 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100592#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
593 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 psa_rsa_oaep_set_padding_mode(alg, rsa));
595 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100596 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100598
599 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
601 mbedtls_psa_get_random,
602 MBEDTLS_PSA_RANDOM_STATE,
603 salt, salt_length,
604 input_length,
605 input,
606 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100607#else
608 status = PSA_ERROR_NOT_SUPPORTED;
609#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100611 status = PSA_ERROR_INVALID_ARGUMENT;
612 }
613#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100615rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100616 if (status == PSA_SUCCESS) {
617 *output_length = mbedtls_rsa_get_len(rsa);
618 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100619
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 mbedtls_rsa_free(rsa);
621 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100622#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
623 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100625 status = PSA_ERROR_NOT_SUPPORTED;
626 }
627
628 return status;
629}
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
632 const uint8_t *key_buffer,
633 size_t key_buffer_size,
634 psa_algorithm_t alg,
635 const uint8_t *input,
636 size_t input_length,
637 const uint8_t *salt,
638 size_t salt_length,
639 uint8_t *output,
640 size_t output_size,
641 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100642{
643 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
644 (void) key_buffer;
645 (void) key_buffer_size;
646 (void) input;
647 (void) input_length;
648 (void) salt;
649 (void) salt_length;
650 (void) output;
651 (void) output_size;
652 (void) output_length;
653
654 *output_length = 0;
655
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100657#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100659 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
661 key_buffer,
662 key_buffer_size,
663 &rsa);
664 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100665 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100669 status = PSA_ERROR_INVALID_ARGUMENT;
670 goto rsa_exit;
671 }
672#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
673 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100676#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
677 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 mbedtls_rsa_pkcs1_decrypt(rsa,
679 mbedtls_psa_get_random,
680 MBEDTLS_PSA_RANDOM_STATE,
681 output_length,
682 input,
683 output,
684 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100685#else
686 status = PSA_ERROR_NOT_SUPPORTED;
687#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 } else
689 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100690#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
691 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 psa_rsa_oaep_set_padding_mode(alg, rsa));
693 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100694 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100695 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100696
697 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
699 mbedtls_psa_get_random,
700 MBEDTLS_PSA_RANDOM_STATE,
701 salt, salt_length,
702 output_length,
703 input,
704 output,
705 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100706#else
707 status = PSA_ERROR_NOT_SUPPORTED;
708#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100710 status = PSA_ERROR_INVALID_ARGUMENT;
711 }
712
713#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100715rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 mbedtls_rsa_free(rsa);
717 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100718#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
719 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100721 status = PSA_ERROR_NOT_SUPPORTED;
722 }
723
724 return status;
725}
726
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100727#endif /* MBEDTLS_PSA_CRYPTO_C */