blob: 065e55af18c54d6611e619dc871be5eb9944e18b [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. */
Dave Rodgman02a53d72023-09-28 17:17:07 +0100331#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (hash_length > UINT_MAX) {
333 return PSA_ERROR_INVALID_ARGUMENT;
334 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100335#endif
Ronald Cron7bdbca32020-12-09 13:34:54 +0100336
Janos Follath0af093b2021-06-07 14:34:10 +0100337 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
339 if (*md_alg == MBEDTLS_MD_NONE) {
340 return PSA_ERROR_NOT_SUPPORTED;
341 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200342 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 return PSA_ERROR_INVALID_ARGUMENT;
344 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100345 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100348}
349
Ronald Cron0266cfe2021-03-13 18:50:11 +0100350psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100351 const psa_key_attributes_t *attributes,
352 const uint8_t *key_buffer, size_t key_buffer_size,
353 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100354 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100355{
356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
357 mbedtls_rsa_context *rsa = NULL;
358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
359 mbedtls_md_type_t md_alg;
360
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
362 key_buffer,
363 key_buffer_size,
364 &rsa);
365 if (status != PSA_SUCCESS) {
366 return status;
367 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
370 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100371 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100373
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100375 status = PSA_ERROR_BUFFER_TOO_SMALL;
376 goto exit;
377 }
378
Ronald Cron0266cfe2021-03-13 18:50:11 +0100379#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
381 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
382 MBEDTLS_MD_NONE);
383 if (ret == 0) {
384 ret = mbedtls_rsa_pkcs1_sign(rsa,
385 mbedtls_psa_get_random,
386 MBEDTLS_PSA_RANDOM_STATE,
387 md_alg,
388 (unsigned int) hash_length,
389 hash,
390 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200391 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100393#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
394#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (PSA_ALG_IS_RSA_PSS(alg)) {
396 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 if (ret == 0) {
399 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
400 mbedtls_psa_get_random,
401 MBEDTLS_PSA_RANDOM_STATE,
402 MBEDTLS_MD_NONE,
403 (unsigned int) hash_length,
404 hash,
405 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200406 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100408#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100409 {
410 status = PSA_ERROR_INVALID_ARGUMENT;
411 goto exit;
412 }
413
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 if (ret == 0) {
415 *signature_length = mbedtls_rsa_get_len(rsa);
416 }
417 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100418
419exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 mbedtls_rsa_free(rsa);
421 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100424}
425
Ronald Cron0266cfe2021-03-13 18:50:11 +0100426#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100427static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
428 const mbedtls_rsa_context *rsa,
429 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200430{
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
432 return MBEDTLS_RSA_SALT_LEN_ANY;
433 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200434 /* Otherwise: standard salt length, i.e. largest possible salt length
435 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200437 int hlen = (int) hash_length; // known to fit
438 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (room < 0) {
440 return 0; // there is no valid signature in this case anyway
441 } else if (room > hlen) {
442 return hlen;
443 } else {
444 return room;
445 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200446}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100447#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200448
Ronald Cron0266cfe2021-03-13 18:50:11 +0100449psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100450 const psa_key_attributes_t *attributes,
451 const uint8_t *key_buffer, size_t key_buffer_size,
452 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100453 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100454{
455 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
456 mbedtls_rsa_context *rsa = NULL;
457 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
458 mbedtls_md_type_t md_alg;
459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
461 key_buffer,
462 key_buffer_size,
463 &rsa);
464 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100465 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
469 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100470 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100474 status = PSA_ERROR_INVALID_SIGNATURE;
475 goto exit;
476 }
477
Ronald Cron0266cfe2021-03-13 18:50:11 +0100478#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
480 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
481 MBEDTLS_MD_NONE);
482 if (ret == 0) {
483 ret = mbedtls_rsa_pkcs1_verify(rsa,
484 md_alg,
485 (unsigned int) hash_length,
486 hash,
487 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200488 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100490#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
491#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 if (PSA_ALG_IS_RSA_PSS(alg)) {
493 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
494 if (ret == 0) {
495 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
496 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
497 md_alg,
498 (unsigned) hash_length,
499 hash,
500 md_alg,
501 slen,
502 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200503 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100505#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100506 {
507 status = PSA_ERROR_INVALID_ARGUMENT;
508 goto exit;
509 }
510
511 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
512 * the rest of the signature is invalid". This has little use in
513 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100515 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100517
518exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 mbedtls_rsa_free(rsa);
520 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100521
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100523}
524
Ronald Crond2fb8542020-12-09 15:18:01 +0100525#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
526 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
527
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100528/****************************************************************/
529/* Asymmetric cryptography */
530/****************************************************************/
531
532#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100533static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
534 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100535{
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200537 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100538
Manuel Pégourié-Gonnardeb592042023-05-31 10:48:38 +0200539 /* Just to get the error status right, as rsa_set_padding() doesn't
540 * distinguish between "bad RSA algorithm" and "unknown hash". */
541 if (mbedtls_md_info_from_type(md_alg) == NULL) {
542 return PSA_ERROR_NOT_SUPPORTED;
543 }
544
Gilles Peskine449bd832023-01-11 14:50:10 +0100545 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100546}
547#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
548
Gilles Peskine449bd832023-01-11 14:50:10 +0100549psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
550 const uint8_t *key_buffer,
551 size_t key_buffer_size,
552 psa_algorithm_t alg,
553 const uint8_t *input,
554 size_t input_length,
555 const uint8_t *salt,
556 size_t salt_length,
557 uint8_t *output,
558 size_t output_size,
559 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100560{
561 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
562 (void) key_buffer;
563 (void) key_buffer_size;
564 (void) input;
565 (void) input_length;
566 (void) salt;
567 (void) salt_length;
568 (void) output;
569 (void) output_size;
570 (void) output_length;
571
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (PSA_KEY_TYPE_IS_RSA(attributes->core.type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100573#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100575 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
577 key_buffer,
578 key_buffer_size,
579 &rsa);
580 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100581 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100583
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100585 status = PSA_ERROR_BUFFER_TOO_SMALL;
586 goto rsa_exit;
587 }
588#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
589 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100591#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
592 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100593 mbedtls_rsa_pkcs1_encrypt(rsa,
594 mbedtls_psa_get_random,
595 MBEDTLS_PSA_RANDOM_STATE,
596 input_length,
597 input,
598 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100599#else
600 status = PSA_ERROR_NOT_SUPPORTED;
601#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 } else
603 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100604#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
605 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 psa_rsa_oaep_set_padding_mode(alg, rsa));
607 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100608 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100610
611 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100612 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
613 mbedtls_psa_get_random,
614 MBEDTLS_PSA_RANDOM_STATE,
615 salt, salt_length,
616 input_length,
617 input,
618 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100619#else
620 status = PSA_ERROR_NOT_SUPPORTED;
621#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100623 status = PSA_ERROR_INVALID_ARGUMENT;
624 }
625#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100627rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 if (status == PSA_SUCCESS) {
629 *output_length = mbedtls_rsa_get_len(rsa);
630 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 mbedtls_rsa_free(rsa);
633 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100634#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
635 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100637 status = PSA_ERROR_NOT_SUPPORTED;
638 }
639
640 return status;
641}
642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
644 const uint8_t *key_buffer,
645 size_t key_buffer_size,
646 psa_algorithm_t alg,
647 const uint8_t *input,
648 size_t input_length,
649 const uint8_t *salt,
650 size_t salt_length,
651 uint8_t *output,
652 size_t output_size,
653 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100654{
655 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
656 (void) key_buffer;
657 (void) key_buffer_size;
658 (void) input;
659 (void) input_length;
660 (void) salt;
661 (void) salt_length;
662 (void) output;
663 (void) output_size;
664 (void) output_length;
665
666 *output_length = 0;
667
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100669#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100671 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
673 key_buffer,
674 key_buffer_size,
675 &rsa);
676 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100677 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100681 status = PSA_ERROR_INVALID_ARGUMENT;
682 goto rsa_exit;
683 }
684#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
685 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100688#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
689 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 mbedtls_rsa_pkcs1_decrypt(rsa,
691 mbedtls_psa_get_random,
692 MBEDTLS_PSA_RANDOM_STATE,
693 output_length,
694 input,
695 output,
696 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100697#else
698 status = PSA_ERROR_NOT_SUPPORTED;
699#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 } else
701 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100702#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
703 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 psa_rsa_oaep_set_padding_mode(alg, rsa));
705 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100706 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100708
709 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100710 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
711 mbedtls_psa_get_random,
712 MBEDTLS_PSA_RANDOM_STATE,
713 salt, salt_length,
714 output_length,
715 input,
716 output,
717 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100718#else
719 status = PSA_ERROR_NOT_SUPPORTED;
720#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100722 status = PSA_ERROR_INVALID_ARGUMENT;
723 }
724
725#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100727rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 mbedtls_rsa_free(rsa);
729 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100730#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
731 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100733 status = PSA_ERROR_NOT_SUPPORTED;
734 }
735
736 return status;
737}
738
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100739#endif /* MBEDTLS_PSA_CRYPTO_C */