blob: 38dc3b8edc2e900c983e019f893d4e86f260f7ef [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
Dave Rodgman16799db2023-11-02 19:47:20 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Ronald Cron00b7bfc2020-11-25 15:25:26 +01007 */
8
9#include "common.h"
10
11#if defined(MBEDTLS_PSA_CRYPTO_C)
12
13#include <psa/crypto.h>
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +010014#include "psa/crypto_values.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010015#include "psa_crypto_core.h"
Ronald Cron9e18fc12020-11-05 17:36:40 +010016#include "psa_crypto_random_impl.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010017#include "psa_crypto_rsa.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010018#include "psa_crypto_hash.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010019#include "mbedtls/psa_util.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010020
21#include <stdlib.h>
22#include <string.h>
23#include "mbedtls/platform.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010024
25#include <mbedtls/rsa.h>
26#include <mbedtls/error.h>
Valerio Setti7b7ffd32024-01-23 16:14:18 +010027#include "rsa_internal.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010028
29#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010031 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
32 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
Valerio Settib2bcedb2023-07-10 11:24:00 +020033 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) || \
Valerio Settife478902023-07-25 12:27:19 +020034 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +010035 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010036
37/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
38 * that are not a multiple of 8) well. For example, there is only
39 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
40 * way to return the exact bit size of a key.
41 * To keep things simple, reject non-byte-aligned key sizes. */
42static psa_status_t psa_check_rsa_key_byte_aligned(
Gilles Peskine449bd832023-01-11 14:50:10 +010043 const mbedtls_rsa_context *rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010044{
45 mbedtls_mpi n;
46 psa_status_t status;
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_mpi_init(&n);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010048 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +010049 mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
50 if (status == PSA_SUCCESS) {
51 if (mbedtls_mpi_bitlen(&n) % 8 != 0) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010052 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine449bd832023-01-11 14:50:10 +010053 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010054 }
Gilles Peskine449bd832023-01-11 14:50:10 +010055 mbedtls_mpi_free(&n);
56 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010057}
58
59psa_status_t mbedtls_psa_rsa_load_representation(
60 psa_key_type_t type, const uint8_t *data, size_t data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_rsa_context **p_rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010062{
63 psa_status_t status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010064 size_t bits;
Valerio Setti7b7ffd32024-01-23 16:14:18 +010065
66 *p_rsa = mbedtls_calloc(1, sizeof(mbedtls_rsa_context));
67 if (*p_rsa == NULL) {
68 return PSA_ERROR_INSUFFICIENT_MEMORY;
69 }
70 mbedtls_rsa_init(*p_rsa);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010071
72 /* Parse the data. */
Gilles Peskine449bd832023-01-11 14:50:10 +010073 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Valerio Setti135ebde2024-02-01 17:00:29 +010074 status = mbedtls_to_psa_error(mbedtls_rsa_parse_key(*p_rsa, data, data_length));
Gilles Peskine449bd832023-01-11 14:50:10 +010075 } else {
Valerio Setti201e6432024-02-01 17:19:37 +010076 status = mbedtls_to_psa_error(mbedtls_rsa_parse_pubkey(*p_rsa, data, data_length));
Gilles Peskine449bd832023-01-11 14:50:10 +010077 }
78 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010079 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010080 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010081
Ronald Cron00b7bfc2020-11-25 15:25:26 +010082 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
83 * supports non-byte-aligned key sizes, but not well. For example,
84 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Valerio Setti7b7ffd32024-01-23 16:14:18 +010085 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(*p_rsa));
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010087 status = PSA_ERROR_NOT_SUPPORTED;
88 goto exit;
89 }
Valerio Setti7b7ffd32024-01-23 16:14:18 +010090 status = psa_check_rsa_key_byte_aligned(*p_rsa);
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010092 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +010093 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010094
Ronald Cron00b7bfc2020-11-25 15:25:26 +010095exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010096 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010097}
Ronald Cron00b7bfc2020-11-25 15:25:26 +010098#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +010099 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100100 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
101 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200102 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) ||
Valerio Settife478902023-07-25 12:27:19 +0200103 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100104 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100105
Valerio Settife478902023-07-25 12:27:19 +0200106#if (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) && \
107 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) || \
Ronald Cron0266cfe2021-03-13 18:50:11 +0100108 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron0266cfe2021-03-13 18:50:11 +0100109psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100110 const psa_key_attributes_t *attributes,
111 const uint8_t *data, size_t data_length,
112 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100114{
115 psa_status_t status;
116 mbedtls_rsa_context *rsa = NULL;
117
118 /* Parse input */
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100119 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 data,
121 data_length,
122 &rsa);
123 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100128
129 /* Re-export the data to PSA export format, such that we can store export
130 * representation in the key slot. Export representation in case of RSA is
131 * the smallest representation that's allowed as input, so a straight-up
132 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100133 status = mbedtls_psa_rsa_export_key(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 rsa,
135 key_buffer,
136 key_buffer_size,
137 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100138exit:
139 /* Always free the RSA object */
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_rsa_free(rsa);
141 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100144}
Valerio Settife478902023-07-25 12:27:19 +0200145#endif /* (defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_IMPORT) &&
146 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT)) ||
Valerio Settib2bcedb2023-07-10 11:24:00 +0200147 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronabf2aef2020-11-27 18:13:44 +0100148
Valerio Settib2bcedb2023-07-10 11:24:00 +0200149#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) || \
150 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine449bd832023-01-11 14:50:10 +0100151psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
152 mbedtls_rsa_context *rsa,
153 uint8_t *data,
154 size_t data_size,
155 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100156{
Ronald Crone5ca3d82020-11-26 16:36:16 +0100157 int ret;
Valerio Setti7b7ffd32024-01-23 16:14:18 +0100158 uint8_t *end = data + data_size;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100159
160 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
161 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
162 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Valerio Setti135ebde2024-02-01 17:00:29 +0100164 ret = mbedtls_rsa_write_key(rsa, data, &end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 } else {
Valerio Setti135ebde2024-02-01 17:00:29 +0100166 ret = mbedtls_rsa_write_pubkey(rsa, data, &end);
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100170 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 memset(data, 0, data_size);
172 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100173 }
174
175 /* The mbedtls_pk_xxx functions write to the end of the buffer.
176 * Move the data to the beginning and erase remaining data
177 * at the original location. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if (2 * (size_t) ret <= data_size) {
179 memcpy(data, data + data_size - ret, ret);
180 memset(data + data_size - ret, 0, ret);
181 } else if ((size_t) ret < data_size) {
182 memmove(data, data + data_size - ret, ret);
183 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100184 }
185
186 *data_length = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100188}
189
Ronald Cron0266cfe2021-03-13 18:50:11 +0100190psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100191 const psa_key_attributes_t *attributes,
192 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100194{
195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
196 mbedtls_rsa_context *rsa = NULL;
197
198 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100199 attributes->type, key_buffer, key_buffer_size, &rsa);
Gowtham Suresh Kumarc2ec6fa2024-08-19 11:50:10 +0100200 if (status == PSA_SUCCESS) {
201 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
202 rsa,
203 data,
204 data_size,
205 data_length);
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 mbedtls_rsa_free(rsa);
209 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100212}
Valerio Settib2bcedb2023-07-10 11:24:00 +0200213#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_EXPORT) ||
Ronald Cron0266cfe2021-03-13 18:50:11 +0100214 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100215
Valerio Setti76df8c12023-07-11 14:11:28 +0200216#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE)
Gilles Peskinebb6f3ff2024-02-16 00:51:07 +0100217static psa_status_t psa_rsa_read_exponent(const uint8_t *e_bytes,
218 size_t e_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 int *exponent)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100220{
221 size_t i;
222 uint32_t acc = 0;
223
Ronald Cron9e18fc12020-11-05 17:36:40 +0100224 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
225 * support values that fit in a 32-bit integer, which is larger than
226 * int on just about every platform anyway. */
Gilles Peskinebb6f3ff2024-02-16 00:51:07 +0100227 if (e_length > sizeof(acc)) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return PSA_ERROR_NOT_SUPPORTED;
229 }
Gilles Peskinebb6f3ff2024-02-16 00:51:07 +0100230 for (i = 0; i < e_length; i++) {
231 acc = (acc << 8) | e_bytes[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 }
233 if (acc > INT_MAX) {
234 return PSA_ERROR_NOT_SUPPORTED;
235 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100236 *exponent = acc;
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100238}
239
Ronald Cron0266cfe2021-03-13 18:50:11 +0100240psa_status_t mbedtls_psa_rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100241 const psa_key_attributes_t *attributes,
Gilles Peskinef36d7852024-06-06 21:11:44 +0200242 const uint8_t *custom_data, size_t custom_data_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length)
Ronald Cron9e18fc12020-11-05 17:36:40 +0100244{
245 psa_status_t status;
246 mbedtls_rsa_context rsa;
247 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4c32b692024-02-16 00:11:09 +0100248 int exponent = 65537;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100249
Gilles Peskinef36d7852024-06-06 21:11:44 +0200250 if (custom_data_length != 0) {
251 status = psa_rsa_read_exponent(custom_data, custom_data_length,
Gilles Peskine4c32b692024-02-16 00:11:09 +0100252 &exponent);
253 if (status != PSA_SUCCESS) {
254 return status;
255 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 mbedtls_rsa_init(&rsa);
259 ret = mbedtls_rsa_gen_key(&rsa,
260 mbedtls_psa_get_random,
261 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100262 (unsigned int) attributes->bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 exponent);
264 if (ret != 0) {
Gowtham Suresh Kumarc2ec6fa2024-08-19 11:50:10 +0100265 mbedtls_rsa_free(&rsa);
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 return mbedtls_to_psa_error(ret);
267 }
Ronald Cron9e18fc12020-11-05 17:36:40 +0100268
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100269 status = mbedtls_psa_rsa_export_key(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 &rsa, key_buffer, key_buffer_size,
271 key_buffer_length);
272 mbedtls_rsa_free(&rsa);
Ronald Cron9e18fc12020-11-05 17:36:40 +0100273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100275}
Valerio Setti76df8c12023-07-11 14:11:28 +0200276#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR_GENERATE) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100277
Ronald Cron7bdbca32020-12-09 13:34:54 +0100278/****************************************************************/
279/* Sign/verify hashes */
280/****************************************************************/
281
Ronald Cron0266cfe2021-03-13 18:50:11 +0100282#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
283 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100284
285/* Decode the hash algorithm from alg and store the mbedtls encoding in
286 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100287static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
288 size_t hash_length,
289 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100290{
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200292 *md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100293
294 /* The Mbed TLS RSA module uses an unsigned int for hash length
295 * parameters. Validate that it fits so that we don't risk an
296 * overflow later. */
Dave Rodgman02a53d72023-09-28 17:17:07 +0100297#if SIZE_MAX > UINT_MAX
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (hash_length > UINT_MAX) {
299 return PSA_ERROR_INVALID_ARGUMENT;
300 }
Dave Rodgman02a53d72023-09-28 17:17:07 +0100301#endif
Ronald Cron7bdbca32020-12-09 13:34:54 +0100302
Janos Follath0af093b2021-06-07 14:34:10 +0100303 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
305 if (*md_alg == MBEDTLS_MD_NONE) {
306 return PSA_ERROR_NOT_SUPPORTED;
307 }
Manuel Pégourié-Gonnard9b41eb82023-03-28 11:14:24 +0200308 if (mbedtls_md_get_size_from_type(*md_alg) != hash_length) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 return PSA_ERROR_INVALID_ARGUMENT;
310 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100311 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100312
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100314}
315
Ronald Cron0266cfe2021-03-13 18:50:11 +0100316psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100317 const psa_key_attributes_t *attributes,
318 const uint8_t *key_buffer, size_t key_buffer_size,
319 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100321{
322 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
323 mbedtls_rsa_context *rsa = NULL;
324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
325 mbedtls_md_type_t md_alg;
326
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100327 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 key_buffer,
329 key_buffer_size,
330 &rsa);
331 if (status != PSA_SUCCESS) {
Gowtham Suresh Kumarc2ec6fa2024-08-19 11:50:10 +0100332 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
336 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100337 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100341 status = PSA_ERROR_BUFFER_TOO_SMALL;
342 goto exit;
343 }
344
Ronald Cron0266cfe2021-03-13 18:50:11 +0100345#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
347 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
348 MBEDTLS_MD_NONE);
349 if (ret == 0) {
350 ret = mbedtls_rsa_pkcs1_sign(rsa,
351 mbedtls_psa_get_random,
352 MBEDTLS_PSA_RANDOM_STATE,
353 md_alg,
354 (unsigned int) hash_length,
355 hash,
356 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200357 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100359#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
360#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (PSA_ALG_IS_RSA_PSS(alg)) {
362 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Ronald Cronea7631b2021-06-03 18:51:59 +0200363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (ret == 0) {
365 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
366 mbedtls_psa_get_random,
367 MBEDTLS_PSA_RANDOM_STATE,
368 MBEDTLS_MD_NONE,
369 (unsigned int) hash_length,
370 hash,
371 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200372 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100374#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100375 {
376 status = PSA_ERROR_INVALID_ARGUMENT;
377 goto exit;
378 }
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 if (ret == 0) {
381 *signature_length = mbedtls_rsa_get_len(rsa);
382 }
383 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100384
385exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 mbedtls_rsa_free(rsa);
387 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100388
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100390}
391
Ronald Cron0266cfe2021-03-13 18:50:11 +0100392#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
394 const mbedtls_rsa_context *rsa,
395 size_t hash_length)
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200396{
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
398 return MBEDTLS_RSA_SALT_LEN_ANY;
399 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200400 /* Otherwise: standard salt length, i.e. largest possible salt length
401 * up to the hash length. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200403 int hlen = (int) hash_length; // known to fit
404 int room = klen - 2 - hlen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 if (room < 0) {
406 return 0; // there is no valid signature in this case anyway
407 } else if (room > hlen) {
408 return hlen;
409 } else {
410 return room;
411 }
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200412}
Ronald Cron0266cfe2021-03-13 18:50:11 +0100413#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200414
Ronald Cron0266cfe2021-03-13 18:50:11 +0100415psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100416 const psa_key_attributes_t *attributes,
417 const uint8_t *key_buffer, size_t key_buffer_size,
418 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100420{
421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
422 mbedtls_rsa_context *rsa = NULL;
423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
424 mbedtls_md_type_t md_alg;
425
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100426 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 key_buffer,
428 key_buffer_size,
429 &rsa);
430 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100431 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
435 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100436 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100440 status = PSA_ERROR_INVALID_SIGNATURE;
441 goto exit;
442 }
443
Ronald Cron0266cfe2021-03-13 18:50:11 +0100444#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
446 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
447 MBEDTLS_MD_NONE);
448 if (ret == 0) {
449 ret = mbedtls_rsa_pkcs1_verify(rsa,
450 md_alg,
451 (unsigned int) hash_length,
452 hash,
453 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200454 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100456#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
457#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (PSA_ALG_IS_RSA_PSS(alg)) {
459 ret = mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
460 if (ret == 0) {
461 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
462 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
463 md_alg,
464 (unsigned) hash_length,
465 hash,
466 md_alg,
467 slen,
468 signature);
Ronald Cronea7631b2021-06-03 18:51:59 +0200469 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 } else
Ronald Cron0266cfe2021-03-13 18:50:11 +0100471#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100472 {
473 status = PSA_ERROR_INVALID_ARGUMENT;
474 goto exit;
475 }
476
477 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
478 * the rest of the signature is invalid". This has little use in
479 * practice and PSA doesn't report this distinction. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100481 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100483
484exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 mbedtls_rsa_free(rsa);
486 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100489}
490
Ronald Crond2fb8542020-12-09 15:18:01 +0100491#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
492 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
493
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100494/****************************************************************/
495/* Asymmetric cryptography */
496/****************************************************************/
497
498#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine449bd832023-01-11 14:50:10 +0100499static int psa_rsa_oaep_set_padding_mode(psa_algorithm_t alg,
500 mbedtls_rsa_context *rsa)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100501{
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH(alg);
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200503 mbedtls_md_type_t md_alg = mbedtls_md_type_from_psa_alg(hash_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100504
Manuel Pégourié-Gonnardeb592042023-05-31 10:48:38 +0200505 /* Just to get the error status right, as rsa_set_padding() doesn't
506 * distinguish between "bad RSA algorithm" and "unknown hash". */
507 if (mbedtls_md_info_from_type(md_alg) == NULL) {
508 return PSA_ERROR_NOT_SUPPORTED;
509 }
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100512}
513#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515psa_status_t mbedtls_psa_asymmetric_encrypt(const psa_key_attributes_t *attributes,
516 const uint8_t *key_buffer,
517 size_t key_buffer_size,
518 psa_algorithm_t alg,
519 const uint8_t *input,
520 size_t input_length,
521 const uint8_t *salt,
522 size_t salt_length,
523 uint8_t *output,
524 size_t output_size,
525 size_t *output_length)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100526{
527 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
528 (void) key_buffer;
529 (void) key_buffer_size;
530 (void) input;
531 (void) input_length;
532 (void) salt;
533 (void) salt_length;
534 (void) output;
535 (void) output_size;
536 (void) output_length;
537
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100538 if (PSA_KEY_TYPE_IS_RSA(attributes->type)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100539#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100541 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100542 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 key_buffer,
544 key_buffer_size,
545 &rsa);
546 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100547 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 if (output_size < mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100551 status = PSA_ERROR_BUFFER_TOO_SMALL;
552 goto rsa_exit;
553 }
554#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
555 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100557#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
558 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 mbedtls_rsa_pkcs1_encrypt(rsa,
560 mbedtls_psa_get_random,
561 MBEDTLS_PSA_RANDOM_STATE,
562 input_length,
563 input,
564 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100565#else
566 status = PSA_ERROR_NOT_SUPPORTED;
567#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 } else
569 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100570#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
571 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 psa_rsa_oaep_set_padding_mode(alg, rsa));
573 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100574 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100576
577 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 mbedtls_rsa_rsaes_oaep_encrypt(rsa,
579 mbedtls_psa_get_random,
580 MBEDTLS_PSA_RANDOM_STATE,
581 salt, salt_length,
582 input_length,
583 input,
584 output));
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100585#else
586 status = PSA_ERROR_NOT_SUPPORTED;
587#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100589 status = PSA_ERROR_INVALID_ARGUMENT;
590 }
591#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100593rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 if (status == PSA_SUCCESS) {
595 *output_length = mbedtls_rsa_get_len(rsa);
596 }
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 mbedtls_rsa_free(rsa);
599 mbedtls_free(rsa);
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100600#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
601 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 } else {
Przemyslaw Stekiel234f3182021-12-09 11:13:58 +0100603 status = PSA_ERROR_NOT_SUPPORTED;
604 }
605
606 return status;
607}
608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609psa_status_t mbedtls_psa_asymmetric_decrypt(const psa_key_attributes_t *attributes,
610 const uint8_t *key_buffer,
611 size_t key_buffer_size,
612 psa_algorithm_t alg,
613 const uint8_t *input,
614 size_t input_length,
615 const uint8_t *salt,
616 size_t salt_length,
617 uint8_t *output,
618 size_t output_size,
619 size_t *output_length)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100620{
621 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
622 (void) key_buffer;
623 (void) key_buffer_size;
624 (void) input;
625 (void) input_length;
626 (void) salt;
627 (void) salt_length;
628 (void) output;
629 (void) output_size;
630 (void) output_length;
631
632 *output_length = 0;
633
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100634 if (attributes->type == PSA_KEY_TYPE_RSA_KEY_PAIR) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100635#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100637 mbedtls_rsa_context *rsa = NULL;
Gilles Peskine2f107ae2024-02-28 01:26:46 +0100638 status = mbedtls_psa_rsa_load_representation(attributes->type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 key_buffer,
640 key_buffer_size,
641 &rsa);
642 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100643 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100645
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 if (input_length != mbedtls_rsa_get_len(rsa)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100647 status = PSA_ERROR_INVALID_ARGUMENT;
648 goto rsa_exit;
649 }
650#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
651 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
652
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 if (alg == PSA_ALG_RSA_PKCS1V15_CRYPT) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100654#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
655 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 mbedtls_rsa_pkcs1_decrypt(rsa,
657 mbedtls_psa_get_random,
658 MBEDTLS_PSA_RANDOM_STATE,
659 output_length,
660 input,
661 output,
662 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100663#else
664 status = PSA_ERROR_NOT_SUPPORTED;
665#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100666 } else
667 if (PSA_ALG_IS_RSA_OAEP(alg)) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100668#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
669 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100670 psa_rsa_oaep_set_padding_mode(alg, rsa));
671 if (status != PSA_SUCCESS) {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100672 goto rsa_exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 }
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100674
675 status = mbedtls_to_psa_error(
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 mbedtls_rsa_rsaes_oaep_decrypt(rsa,
677 mbedtls_psa_get_random,
678 MBEDTLS_PSA_RANDOM_STATE,
679 salt, salt_length,
680 output_length,
681 input,
682 output,
683 output_size));
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100684#else
685 status = PSA_ERROR_NOT_SUPPORTED;
686#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100688 status = PSA_ERROR_INVALID_ARGUMENT;
689 }
690
691#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100693rsa_exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 mbedtls_rsa_free(rsa);
695 mbedtls_free(rsa);
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100696#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
697 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 } else {
Przemyslaw Stekiel2ecfd572021-12-13 09:08:05 +0100699 status = PSA_ERROR_NOT_SUPPORTED;
700 }
701
702 return status;
703}
704
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100705#endif /* MBEDTLS_PSA_CRYPTO_C */