blob: 853a0443c84d982836a84900e44b7122c784d4fb [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>
26#include "psa_crypto_core.h"
Ronald Cron9e18fc12020-11-05 17:36:40 +010027#include "psa_crypto_random_impl.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010028#include "psa_crypto_rsa.h"
Steven Cooreman5f88e772021-03-15 11:07:12 +010029#include "psa_crypto_hash.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010030
31#include <stdlib.h>
32#include <string.h>
33#include "mbedtls/platform.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010034
35#include <mbedtls/rsa.h>
36#include <mbedtls/error.h>
37#include <mbedtls/pk.h>
38#include <mbedtls/pk_internal.h>
39
40#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010041 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Croncfc3c7b2021-03-13 18:50:11 +010042 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
43 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
44 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
45 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010046
47/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
48 * that are not a multiple of 8) well. For example, there is only
49 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
50 * way to return the exact bit size of a key.
51 * To keep things simple, reject non-byte-aligned key sizes. */
52static psa_status_t psa_check_rsa_key_byte_aligned(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 const mbedtls_rsa_context *rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010054{
55 mbedtls_mpi n;
56 psa_status_t status;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 mbedtls_mpi_init(&n);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010058 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 mbedtls_rsa_export(rsa, &n, NULL, NULL, NULL, NULL));
60 if (status == PSA_SUCCESS) {
61 if (mbedtls_mpi_bitlen(&n) % 8 != 0) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010062 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010064 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 mbedtls_mpi_free(&n);
66 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +010067}
68
69psa_status_t mbedtls_psa_rsa_load_representation(
70 psa_key_type_t type, const uint8_t *data, size_t data_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010071 mbedtls_rsa_context **p_rsa)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010072{
73 psa_status_t status;
74 mbedtls_pk_context ctx;
75 size_t bits;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 mbedtls_pk_init(&ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +010077
78 /* Parse the data. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010080 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 mbedtls_pk_parse_key(&ctx, data, data_length, NULL, 0));
82 } else {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010083 status = mbedtls_to_psa_error(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 mbedtls_pk_parse_public_key(&ctx, data, data_length));
85 }
86 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010087 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +010089
90 /* We have something that the pkparse module recognizes. If it is a
91 * valid RSA key, store it. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 if (mbedtls_pk_get_type(&ctx) != MBEDTLS_PK_RSA) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +010093 status = PSA_ERROR_INVALID_ARGUMENT;
94 goto exit;
95 }
96
97 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
98 * supports non-byte-aligned key sizes, but not well. For example,
99 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 bits = PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(mbedtls_pk_rsa(ctx)));
101 if (bits > PSA_VENDOR_RSA_MAX_KEY_BITS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100102 status = PSA_ERROR_NOT_SUPPORTED;
103 goto exit;
104 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 status = psa_check_rsa_key_byte_aligned(mbedtls_pk_rsa(ctx));
106 if (status != PSA_SUCCESS) {
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100107 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 }
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100109
110 /* Copy out the pointer to the RSA context, and reset the PK context
111 * such that pk_free doesn't free the RSA context we just grabbed. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 *p_rsa = mbedtls_pk_rsa(ctx);
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100113 ctx.pk_info = NULL;
114
115exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_pk_free(&ctx);
117 return status;
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100118}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100119#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100120 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100121 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
122 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
123 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
124 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100125
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100126#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
127 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100128
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100129psa_status_t mbedtls_psa_rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100130 const psa_key_attributes_t *attributes,
131 const uint8_t *data, size_t data_length,
132 uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 size_t *key_buffer_length, size_t *bits)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100134{
135 psa_status_t status;
136 mbedtls_rsa_context *rsa = NULL;
137
138 /* Parse input */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
140 data,
141 data_length,
142 &rsa);
143 if (status != PSA_SUCCESS) {
Ronald Cronabf2aef2020-11-27 18:13:44 +0100144 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 }
Ronald Cronabf2aef2020-11-27 18:13:44 +0100146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(mbedtls_rsa_get_len(rsa));
Ronald Cronabf2aef2020-11-27 18:13:44 +0100148
149 /* Re-export the data to PSA export format, such that we can store export
150 * representation in the key slot. Export representation in case of RSA is
151 * the smallest representation that's allowed as input, so a straight-up
152 * allocation of the same size as the input buffer will be large enough. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 status = mbedtls_psa_rsa_export_key(attributes->core.type,
154 rsa,
155 key_buffer,
156 key_buffer_size,
157 key_buffer_length);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100158exit:
159 /* Always free the RSA object */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 mbedtls_rsa_free(rsa);
161 mbedtls_free(rsa);
Ronald Cronabf2aef2020-11-27 18:13:44 +0100162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 return status;
Ronald Cronabf2aef2020-11-27 18:13:44 +0100164}
165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166psa_status_t mbedtls_psa_rsa_export_key(psa_key_type_t type,
167 mbedtls_rsa_context *rsa,
168 uint8_t *data,
169 size_t data_size,
170 size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100171{
172#if defined(MBEDTLS_PK_WRITE_C)
173 int ret;
174 mbedtls_pk_context pk;
175 uint8_t *pos = data + data_size;
176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 mbedtls_pk_init(&pk);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100178 pk.pk_info = &mbedtls_rsa_info;
179 pk.pk_ctx = rsa;
180
181 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
182 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
183 * private key and of the RFC3279 RSAPublicKey for a public key. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100184 if (PSA_KEY_TYPE_IS_KEY_PAIR(type)) {
185 ret = mbedtls_pk_write_key_der(&pk, data, data_size);
186 } else {
187 ret = mbedtls_pk_write_pubkey(&pos, data, &pk);
188 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 if (ret < 0) {
Ronald Crone5ca3d82020-11-26 16:36:16 +0100191 /* Clean up in case pk_write failed halfway through. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 memset(data, 0, data_size);
193 return mbedtls_to_psa_error(ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100194 }
195
196 /* The mbedtls_pk_xxx functions write to the end of the buffer.
197 * Move the data to the beginning and erase remaining data
198 * at the original location. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 if (2 * (size_t) ret <= data_size) {
200 memcpy(data, data + data_size - ret, ret);
201 memset(data + data_size - ret, 0, ret);
202 } else if ((size_t) ret < data_size) {
203 memmove(data, data + data_size - ret, ret);
204 memset(data + ret, 0, data_size - ret);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100205 }
206
207 *data_length = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 return PSA_SUCCESS;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100209#else
210 (void) type;
211 (void) rsa;
212 (void) data;
213 (void) data_size;
214 (void) data_length;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 return PSA_ERROR_NOT_SUPPORTED;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100216#endif /* MBEDTLS_PK_WRITE_C */
217}
218
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100219psa_status_t mbedtls_psa_rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100220 const psa_key_attributes_t *attributes,
221 const uint8_t *key_buffer, size_t key_buffer_size,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 uint8_t *data, size_t data_size, size_t *data_length)
Ronald Crone5ca3d82020-11-26 16:36:16 +0100223{
224 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
225 mbedtls_rsa_context *rsa = NULL;
226
227 status = mbedtls_psa_rsa_load_representation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 attributes->core.type, key_buffer, key_buffer_size, &rsa);
229 if (status != PSA_SUCCESS) {
230 return status;
231 }
Ronald Crone5ca3d82020-11-26 16:36:16 +0100232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 status = mbedtls_psa_rsa_export_key(PSA_KEY_TYPE_RSA_PUBLIC_KEY,
234 rsa,
235 data,
236 data_size,
237 data_length);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100238
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100239 mbedtls_rsa_free(rsa);
240 mbedtls_free(rsa);
Ronald Crone5ca3d82020-11-26 16:36:16 +0100241
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 return status;
Ronald Crone5ca3d82020-11-26 16:36:16 +0100243}
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100244#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
245 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cronf1057d32020-11-26 19:19:10 +0100246
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100247#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
Jaeden Ameroc17f2932021-05-14 08:34:32 +0100248 defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100256 if (domain_parameters_size == 0) {
Ronald Cron9e18fc12020-11-05 17:36:40 +0100257 *exponent = 65537;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100274 return PSA_SUCCESS;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100275}
276
Ronald Croncfc3c7b2021-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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100293 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE);
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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +0100308 return status;
Ronald Cron9e18fc12020-11-05 17:36:40 +0100309}
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100310#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Jaeden Ameroc17f2932021-05-14 08:34:32 +0100311 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100312
Ronald Cron7bdbca32020-12-09 13:34:54 +0100313/****************************************************************/
314/* Sign/verify hashes */
315/****************************************************************/
316
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100317#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
318 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100319
320/* Decode the hash algorithm from alg and store the mbedtls encoding in
321 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322static psa_status_t psa_rsa_decode_md_type(psa_algorithm_t alg,
323 size_t hash_length,
324 mbedtls_md_type_t *md_alg)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100325{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100326 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH(alg);
327 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa(hash_alg);
328 *md_alg = mbedtls_md_get_type(md_info);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100329
330 /* The Mbed TLS RSA module uses an unsigned int for hash length
331 * parameters. Validate that it fits so that we don't risk an
332 * overflow later. */
333#if SIZE_MAX > UINT_MAX
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334 if (hash_length > UINT_MAX) {
335 return PSA_ERROR_INVALID_ARGUMENT;
336 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100337#endif
338
Janos Follathb23b5742021-06-07 14:34:10 +0100339 /* For signatures using a hash, the hash length must be correct. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 if (alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW) {
341 if (md_info == NULL) {
342 return PSA_ERROR_NOT_SUPPORTED;
343 }
344 if (mbedtls_md_get_size(md_info) != hash_length) {
345 return PSA_ERROR_INVALID_ARGUMENT;
346 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100347 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 return PSA_SUCCESS;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100350}
351
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100352psa_status_t mbedtls_psa_rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100353 const psa_key_attributes_t *attributes,
354 const uint8_t *key_buffer, size_t key_buffer_size,
355 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100356 uint8_t *signature, size_t signature_size, size_t *signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100357{
358 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
359 mbedtls_rsa_context *rsa = NULL;
360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
361 mbedtls_md_type_t md_alg;
362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
364 key_buffer,
365 key_buffer_size,
366 &rsa);
367 if (status != PSA_SUCCESS) {
368 return status;
369 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100370
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
372 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100373 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100375
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100376 if (signature_size < mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100377 status = PSA_ERROR_BUFFER_TOO_SMALL;
378 goto exit;
379 }
380
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100381#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
383 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
384 MBEDTLS_MD_NONE);
385 ret = mbedtls_rsa_pkcs1_sign(rsa,
386 mbedtls_psa_get_random,
387 MBEDTLS_PSA_RANDOM_STATE,
388 MBEDTLS_RSA_PRIVATE,
389 md_alg,
390 (unsigned int) hash_length,
391 hash,
392 signature);
393 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100394#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
395#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100396 if (PSA_ALG_IS_RSA_PSS(alg)) {
397 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
398 ret = mbedtls_rsa_rsassa_pss_sign(rsa,
399 mbedtls_psa_get_random,
400 MBEDTLS_PSA_RANDOM_STATE,
401 MBEDTLS_RSA_PRIVATE,
402 MBEDTLS_MD_NONE,
403 (unsigned int) hash_length,
404 hash,
405 signature);
406 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100407#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100408 {
409 status = PSA_ERROR_INVALID_ARGUMENT;
410 goto exit;
411 }
412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 if (ret == 0) {
414 *signature_length = mbedtls_rsa_get_len(rsa);
415 }
416 status = mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100417
418exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100419 mbedtls_rsa_free(rsa);
420 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100421
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100422 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100423}
424
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100425#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100426static int rsa_pss_expected_salt_len(psa_algorithm_t alg,
427 const mbedtls_rsa_context *rsa,
428 size_t hash_length)
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200429{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if (PSA_ALG_IS_RSA_PSS_ANY_SALT(alg)) {
431 return MBEDTLS_RSA_SALT_LEN_ANY;
432 }
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200433 /* Otherwise: standard salt length, i.e. largest possible salt length
434 * up to the hash length. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 int klen = (int) mbedtls_rsa_get_len(rsa); // known to fit
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200436 int hlen = (int) hash_length; // known to fit
437 int room = klen - 2 - hlen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 if (room < 0) {
439 return 0; // there is no valid signature in this case anyway
440 } else if (room > hlen) {
441 return hlen;
442 } else {
443 return room;
444 }
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200445}
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100446#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine44fa40cd2021-10-04 22:15:05 +0200447
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100448psa_status_t mbedtls_psa_rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100449 const psa_key_attributes_t *attributes,
450 const uint8_t *key_buffer, size_t key_buffer_size,
451 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100452 const uint8_t *signature, size_t signature_length)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100453{
454 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
455 mbedtls_rsa_context *rsa = NULL;
456 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
457 mbedtls_md_type_t md_alg;
458
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 status = mbedtls_psa_rsa_load_representation(attributes->core.type,
460 key_buffer,
461 key_buffer_size,
462 &rsa);
463 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100464 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100465 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 status = psa_rsa_decode_md_type(alg, hash_length, &md_alg);
468 if (status != PSA_SUCCESS) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100469 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100470 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100471
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100472 if (signature_length != mbedtls_rsa_get_len(rsa)) {
Ronald Cron7bdbca32020-12-09 13:34:54 +0100473 status = PSA_ERROR_INVALID_SIGNATURE;
474 goto exit;
475 }
476
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100477#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 if (PSA_ALG_IS_RSA_PKCS1V15_SIGN(alg)) {
479 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V15,
480 MBEDTLS_MD_NONE);
481 ret = mbedtls_rsa_pkcs1_verify(rsa,
482 mbedtls_psa_get_random,
483 MBEDTLS_PSA_RANDOM_STATE,
484 MBEDTLS_RSA_PUBLIC,
485 md_alg,
486 (unsigned int) hash_length,
487 hash,
488 signature);
489 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100490#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
491#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100492 if (PSA_ALG_IS_RSA_PSS(alg)) {
493 int slen = rsa_pss_expected_salt_len(alg, rsa, hash_length);
494 mbedtls_rsa_set_padding(rsa, MBEDTLS_RSA_PKCS_V21, md_alg);
495 ret = mbedtls_rsa_rsassa_pss_verify_ext(rsa,
496 mbedtls_psa_get_random,
497 MBEDTLS_PSA_RANDOM_STATE,
498 MBEDTLS_RSA_PUBLIC,
499 md_alg,
500 (unsigned int) hash_length,
501 hash,
502 md_alg,
503 slen,
504 signature);
505 } else
Ronald Croncfc3c7b2021-03-13 18:50:11 +0100506#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100507 {
508 status = PSA_ERROR_INVALID_ARGUMENT;
509 goto exit;
510 }
511
512 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
513 * the rest of the signature is invalid". This has little use in
514 * practice and PSA doesn't report this distinction. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100515 status = (ret == MBEDTLS_ERR_RSA_INVALID_PADDING) ?
Ronald Cron7bdbca32020-12-09 13:34:54 +0100516 PSA_ERROR_INVALID_SIGNATURE :
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100517 mbedtls_to_psa_error(ret);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100518
519exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 mbedtls_rsa_free(rsa);
521 mbedtls_free(rsa);
Ronald Cron7bdbca32020-12-09 13:34:54 +0100522
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100523 return status;
Ronald Cron7bdbca32020-12-09 13:34:54 +0100524}
525
Ronald Crond2fb8542020-12-09 15:18:01 +0100526#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
527 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
528
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100529#endif /* MBEDTLS_PSA_CRYPTO_C */