blob: 508a68b03288f928ad645a17ba80542399a7ffd4 [file] [log] [blame]
Ronald Cron00b7bfc2020-11-25 15:25:26 +01001/*
2 * PSA RSA layer on top of Mbed TLS crypto
3 */
4/*
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21#include "common.h"
22
23#if defined(MBEDTLS_PSA_CRYPTO_C)
24
25#include <psa/crypto.h>
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +010026#include "psa/crypto_values.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010027#include "psa_crypto_core.h"
Ronald Cron9e18fc12020-11-05 17:36:40 +010028#include "psa_crypto_random_impl.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010029#include "psa_crypto_rsa.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010030#include "psa_crypto_hash.h"
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +020031#include "md_psa.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010032
33#include <stdlib.h>
34#include <string.h>
35#include "mbedtls/platform.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036
37#include <mbedtls/rsa.h>
38#include <mbedtls/error.h>
39#include <mbedtls/pk.h>
Dave Rodgman3b5e6f02021-04-06 17:58:16 +010040#include "pk_wrap.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041
42#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010043 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010044 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
45 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
Valerio Settib2bcedb2023-07-10 11:24:00 +020046 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) || \
Valerio Settife478902023-07-25 12:27:19 +020047 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010048 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) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200127 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) ||
Valerio Settife478902023-07-25 12:27:19 +0200128 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100129 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100130
Valerio Settife478902023-07-25 12:27:19 +0200131#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
132 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +0100133 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron0266cfe2021-03-13 18:50:11 +0100134psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100135 const psa_key_attributes_t *attributes,
136 const uint8_t *data, size_t data_length,
137 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100139{
140 psa_status_t status;
141 mbedtls_rsa_context *rsa = NULL;
142
143 /* Parse input */
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
145 data,
146 data_length,
147 &rsa);
148 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100149 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100153
154 /* Re-export the data to PSA export format, such that we can store export
155 * representation in the key slot. Export representation in case of RSA is
156 * the smallest representation that's allowed as input, so a straight-up
157 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 status = mbedtls_psa_rsa_export_key(attributes->core.type,
159 rsa,
160 key_buffer,
161 key_buffer_size,
162 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100163exit:
164 /* Always free the RSA object */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 mbedtls_rsa_free(rsa);
166 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100167
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100169}
Valerio Settife478902023-07-25 12:27:19 +0200170#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
171 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200172 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronabf2aef2020-11-27 18:13:44 +0100173
Valerio Settib2bcedb2023-07-10 11:24:00 +0200174#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
175 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100176psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
177 mbedtls_rsa_context *rsa,
178 uint8_t *data,
179 size_t data_size,
180 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100181{
Ronald Crone5ca3d82020-11-26 16:36:16 +0100182 int ret;
183 mbedtls_pk_context pk;
184 uint8_t *pos = data + data_size;
185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 mbedtls_pk_init(&pk);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100187 pk.pk_info = &mbedtls_rsa_info;
188 pk.pk_ctx = rsa;
189
190 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
191 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
192 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
194 ret = mbedtls_pk_write_key_der(&pk, data, data_size);
195 } else {
196 ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
197 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100200 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 memset(data, 0, data_size);
202 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100203 }
204
205 /* The mbedtls_pk_xxx functions write to the end of the buffer.
206 * Move the data to the beginning and erase remaining data
207 * at the original location. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if (2 * (size_t) ret <= data_size) {
209 memcpy(data, data + data_size - ret, ret);
210 memset(data + data_size - ret, 0, ret);
211 } else if ((size_t) ret < data_size) {
212 memmove(data, data + data_size - ret, ret);
213 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100214 }
215
216 *data_length = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100217 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100218}
219
Ronald Cron0266cfe2021-03-13 18:50:11 +0100220psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100221 const psa_key_attributes_t *attributes,
222 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100224{
225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
226 mbedtls_rsa_context *rsa = NULL;
227
228 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100229 attributes->core.type, key_buffer, key_buffer_size, &rsa);
230 if (status != PSA_SUCCESS) {
231 return status;
232 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
235 rsa,
236 data,
237 data_size,
238 data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 mbedtls_rsa_free(rsa);
241 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100244}
Valerio Settib2bcedb2023-07-10 11:24:00 +0200245#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100246 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100247
Valerio Setti76df8c12023-07-11 14:11:28 +0200248#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100249static psa_status_t psa_rsa_read_exponent(const uint8_t *domain_parameters,
250 size_t domain_parameters_size,
251 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100252{
253 size_t i;
254 uint32_t acc = 0;
255
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100257 *exponent = 65537;
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100259 }
260
261 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
262 * support values that fit in a 32-bit integer, which is larger than
263 * int on just about every platform anyway. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if (domain_parameters_size > sizeof(acc)) {
265 return PSA_ERROR_NOT_SUPPORTED;
266 }
267 for (i = 0; i < domain_parameters_size; i++) {
268 acc = (acc << 8) | domain_parameters[i];
269 }
270 if (acc > INT_MAX) {
271 return PSA_ERROR_NOT_SUPPORTED;
272 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100273 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100275}
276
Ronald Cron0266cfe2021-03-13 18:50:11 +0100277psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100278 const psa_key_attributes_t *attributes,
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100280{
281 psa_status_t status;
282 mbedtls_rsa_context rsa;
283 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
284 int exponent;
285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 status = psa_rsa_read_exponent(attributes->domain_parameters,
287 attributes->domain_parameters_size,
288 &exponent);
289 if (status != PSA_SUCCESS) {
290 return status;
291 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 mbedtls_rsa_init(&rsa);
294 ret = mbedtls_rsa_gen_key(&rsa,
295 mbedtls_psa_get_random,
296 MBEDTLS_PSA_RANDOM_STATE,
297 (unsigned int) attributes->core.bits,
298 exponent);
299 if (ret != 0) {
300 return mbedtls_to_psa_error(ret);
301 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 status = mbedtls_psa_rsa_export_key(attributes->core.type,
304 &rsa, key_buffer, key_buffer_size,
305 key_buffer_length);
306 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100309}
Valerio Setti76df8c12023-07-11 14:11:28 +0200310#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100311
Ronald Cron7bdbca32020-12-09 13:34:54 +0100312/****************************************************************/
313/* Sign/verify hashes */
314/****************************************************************/
315
Ronald Cron0266cfe2021-03-13 18:50:11 +0100316#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
317 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100318
319/* Decode the hash algorithm from alg and store the mbedtls encoding in
320 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
322 size_t hash_length,
323 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100324{
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200326 *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100327
328 /* The Mbed TLS RSA module uses an unsigned int for hash length
329 * parameters. Validate that it fits so that we don't risk an
330 * overflow later. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 if (hash_length > UINT_MAX) {
332 return PSA_ERROR_INVALID_ARGUMENT;
333 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100334
Janos Follath0af093b2021-06-07 14:34:10 +0100335 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
337 if (*md_alg == MBEDTLS_MD_NONE) {
338 return PSA_ERROR_NOT_SUPPORTED;
339 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200340 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 return PSA_ERROR_INVALID_ARGUMENT;
342 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100343 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100346}
347
Ronald Cron0266cfe2021-03-13 18:50:11 +0100348psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100349 const psa_key_attributes_t *attributes,
350 const uint8_t *key_buffer, size_t key_buffer_size,
351 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100353{
354 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
355 mbedtls_rsa_context *rsa = NULL;
356 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
357 mbedtls_md_type_t md_alg;
358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
360 key_buffer,
361 key_buffer_size,
362 &rsa);
363 if (status != PSA_SUCCESS) {
364 return status;
365 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
368 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100369 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100373 status = PSA_ERROR_BUFFER_TOO_SMALL;
374 goto exit;
375 }
376
Ronald Cron0266cfe2021-03-13 18:50:11 +0100377#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
379 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
380 MBEDTLS_MD_NONE);
381 if (ret == 0) {
382 ret = mbedtls_rsa_pkcs1_sign(rsa,
383 mbedtls_psa_get_random,
384 MBEDTLS_PSA_RANDOM_STATE,
385 md_alg,
386 (unsigned int) hash_length,
387 hash,
388 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200389 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100391#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
392#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 if (PSA_ALG_IS_RSA_PSS(alg)) {
394 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200395
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 if (ret == 0) {
397 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
398 mbedtls_psa_get_random,
399 MBEDTLS_PSA_RANDOM_STATE,
400 MBEDTLS_MD_NONE,
401 (unsigned int) hash_length,
402 hash,
403 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200404 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100406#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100407 {
408 status = PSA_ERROR_INVALID_ARGUMENT;
409 goto exit;
410 }
411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (ret == 0) {
413 *signature_length = mbedtls_rsa_get_len(rsa);
414 }
415 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100416
417exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_rsa_free(rsa);
419 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100422}
423
Ronald Cron0266cfe2021-03-13 18:50:11 +0100424#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100425static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
426 const mbedtls_rsa_context *rsa,
427 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200428{
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
430 return MBEDTLS_RSA_SALT_LEN_ANY;
431 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200432 /* Otherwise: standard salt length, i.e. largest possible salt length
433 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200435 int hlen = (int) hash_length; // known to fit
436 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 if (room < 0) {
438 return 0; // there is no valid signature in this case anyway
439 } else if (room > hlen) {
440 return hlen;
441 } else {
442 return room;
443 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200444}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100445#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200446
Ronald Cron0266cfe2021-03-13 18:50:11 +0100447psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100448 const psa_key_attributes_t *attributes,
449 const uint8_t *key_buffer, size_t key_buffer_size,
450 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100452{
453 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
454 mbedtls_rsa_context *rsa = NULL;
455 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
456 mbedtls_md_type_t md_alg;
457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
459 key_buffer,
460 key_buffer_size,
461 &rsa);
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 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
467 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100468 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100472 status = PSA_ERROR_INVALID_SIGNATURE;
473 goto exit;
474 }
475
Ronald Cron0266cfe2021-03-13 18:50:11 +0100476#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
478 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
479 MBEDTLS_MD_NONE);
480 if (ret == 0) {
481 ret = mbedtls_rsa_pkcs1_verify(rsa,
482 md_alg,
483 (unsigned int) hash_length,
484 hash,
485 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200486 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100488#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
489#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 if (PSA_ALG_IS_RSA_PSS(alg)) {
491 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
492 if (ret == 0) {
493 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
494 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
495 md_alg,
496 (unsigned) hash_length,
497 hash,
498 md_alg,
499 slen,
500 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200501 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100503#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100504 {
505 status = PSA_ERROR_INVALID_ARGUMENT;
506 goto exit;
507 }
508
509 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
510 * the rest of the signature is invalid". This has little use in
511 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100513 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100515
516exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 mbedtls_rsa_free(rsa);
518 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100519
Gilles Peskine449bd832023-01-11 14:50:10 +0100520 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100521}
522
Ronald Crond2fb8542020-12-09 15:18:01 +0100523#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
524 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
525
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100526/****************************************************************/
527/* Asymmetric cryptography */
528/****************************************************************/
529
530#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100531static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
532 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100533{
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200535 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100536
Manuel Pégourié-Gonnardeb592042023-05-31 10:48:38 +0200537 /* Just to get the error status right, as rsa_set_padding() doesn't
538 * distinguish between "bad RSA algorithm" and "unknown hash". */
539 if (mbedtls_md_info_from_type(md_alg) == NULL) {
540 return PSA_ERROR_NOT_SUPPORTED;
541 }
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100544}
545#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
546
Gilles Peskine449bd832023-01-11 14:50:10 +0100547psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
548 const uint8_t *key_buffer,
549 size_t key_buffer_size,
550 psa_algorithm_t alg,
551 const uint8_t *input,
552 size_t input_length,
553 const uint8_t *salt,
554 size_t salt_length,
555 uint8_t *output,
556 size_t output_size,
557 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100558{
559 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
560 (void) key_buffer;
561 (void) key_buffer_size;
562 (void) input;
563 (void) input_length;
564 (void) salt;
565 (void) salt_length;
566 (void) output;
567 (void) output_size;
568 (void) output_length;
569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100571#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100573 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
575 key_buffer,
576 key_buffer_size,
577 &rsa);
578 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100579 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100583 status = PSA_ERROR_BUFFER_TOO_SMALL;
584 goto rsa_exit;
585 }
586#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
587 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100589#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
590 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 mbedtls_rsa_pkcs1_encrypt(rsa,
592 mbedtls_psa_get_random,
593 MBEDTLS_PSA_RANDOM_STATE,
594 input_length,
595 input,
596 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100597#else
598 status = PSA_ERROR_NOT_SUPPORTED;
599#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 } else
601 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100602#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
603 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 psa_rsa_oaep_set_padding_mode(alg, rsa));
605 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100606 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100608
609 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
611 mbedtls_psa_get_random,
612 MBEDTLS_PSA_RANDOM_STATE,
613 salt, salt_length,
614 input_length,
615 input,
616 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100617#else
618 status = PSA_ERROR_NOT_SUPPORTED;
619#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100620 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100621 status = PSA_ERROR_INVALID_ARGUMENT;
622 }
623#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100625rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if (status == PSA_SUCCESS) {
627 *output_length = mbedtls_rsa_get_len(rsa);
628 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 mbedtls_rsa_free(rsa);
631 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100632#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
633 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100635 status = PSA_ERROR_NOT_SUPPORTED;
636 }
637
638 return status;
639}
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
642 const uint8_t *key_buffer,
643 size_t key_buffer_size,
644 psa_algorithm_t alg,
645 const uint8_t *input,
646 size_t input_length,
647 const uint8_t *salt,
648 size_t salt_length,
649 uint8_t *output,
650 size_t output_size,
651 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100652{
653 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
654 (void) key_buffer;
655 (void) key_buffer_size;
656 (void) input;
657 (void) input_length;
658 (void) salt;
659 (void) salt_length;
660 (void) output;
661 (void) output_size;
662 (void) output_length;
663
664 *output_length = 0;
665
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100667#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100669 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
671 key_buffer,
672 key_buffer_size,
673 &rsa);
674 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100675 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100679 status = PSA_ERROR_INVALID_ARGUMENT;
680 goto rsa_exit;
681 }
682#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
683 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
684
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100686#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
687 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 mbedtls_rsa_pkcs1_decrypt(rsa,
689 mbedtls_psa_get_random,
690 MBEDTLS_PSA_RANDOM_STATE,
691 output_length,
692 input,
693 output,
694 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100695#else
696 status = PSA_ERROR_NOT_SUPPORTED;
697#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 } else
699 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100700#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
701 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 psa_rsa_oaep_set_padding_mode(alg, rsa));
703 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100704 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100706
707 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
709 mbedtls_psa_get_random,
710 MBEDTLS_PSA_RANDOM_STATE,
711 salt, salt_length,
712 output_length,
713 input,
714 output,
715 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100716#else
717 status = PSA_ERROR_NOT_SUPPORTED;
718#endif /* 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_INVALID_ARGUMENT;
721 }
722
723#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100725rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 mbedtls_rsa_free(rsa);
727 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100728#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
729 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100731 status = PSA_ERROR_NOT_SUPPORTED;
732 }
733
734 return status;
735}
736
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100737#endif /* MBEDTLS_PSA_CRYPTO_C */