blob: d85b86c45de7053e39de9571ba1d857edd50eb86 [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"
34#if !defined(MBEDTLS_PLATFORM_C)
35#define mbedtls_calloc calloc
36#define mbedtls_free free
37#endif
38
39#include <mbedtls/rsa.h>
40#include <mbedtls/error.h>
41#include <mbedtls/pk.h>
Dave Rodgman3b5e6f02021-04-06 17:58:16 +010042#include "pk_wrap.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010043
Ronald Cronf1057d32020-11-26 19:19:10 +010044#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
45 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
46 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) ) )
47#define BUILTIN_KEY_TYPE_RSA_KEY_PAIR 1
48#endif
49
50#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) || \
51 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
52 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) ) )
53#define BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY 1
54#endif
55
Ronald Crond2fb8542020-12-09 15:18:01 +010056#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
57 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
58 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) && \
59 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V15) ) )
60#define BUILTIN_ALG_RSA_PKCS1V15_SIGN 1
61#endif
62
63#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
64 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
65 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) && \
66 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) ) )
67#define BUILTIN_ALG_RSA_PSS 1
68#endif
69
Ronald Cron00b7bfc2020-11-25 15:25:26 +010070#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010071 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Crond2fb8542020-12-09 15:18:01 +010072 defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
73 defined(BUILTIN_ALG_RSA_PSS) || \
Ronald Cronf1057d32020-11-26 19:19:10 +010074 defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
75 defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010076
77/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
78 * that are not a multiple of 8) well. For example, there is only
79 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
80 * way to return the exact bit size of a key.
81 * To keep things simple, reject non-byte-aligned key sizes. */
82static psa_status_t psa_check_rsa_key_byte_aligned(
83 const mbedtls_rsa_context *rsa )
84{
85 mbedtls_mpi n;
86 psa_status_t status;
87 mbedtls_mpi_init( &n );
88 status = mbedtls_to_psa_error(
89 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
90 if( status == PSA_SUCCESS )
91 {
92 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
93 status = PSA_ERROR_NOT_SUPPORTED;
94 }
95 mbedtls_mpi_free( &n );
96 return( status );
97}
98
99psa_status_t mbedtls_psa_rsa_load_representation(
100 psa_key_type_t type, const uint8_t *data, size_t data_length,
101 mbedtls_rsa_context **p_rsa )
102{
103 psa_status_t status;
104 mbedtls_pk_context ctx;
105 size_t bits;
106 mbedtls_pk_init( &ctx );
107
108 /* Parse the data. */
109 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
110 status = mbedtls_to_psa_error(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200111 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0,
112 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100113 else
114 status = mbedtls_to_psa_error(
115 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
116 if( status != PSA_SUCCESS )
117 goto exit;
118
119 /* We have something that the pkparse module recognizes. If it is a
120 * valid RSA key, store it. */
121 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
122 {
123 status = PSA_ERROR_INVALID_ARGUMENT;
124 goto exit;
125 }
126
127 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
128 * supports non-byte-aligned key sizes, but not well. For example,
129 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
130 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
131 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
132 {
133 status = PSA_ERROR_NOT_SUPPORTED;
134 goto exit;
135 }
136 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
137 if( status != PSA_SUCCESS )
138 goto exit;
139
140 /* Copy out the pointer to the RSA context, and reset the PK context
141 * such that pk_free doesn't free the RSA context we just grabbed. */
142 *p_rsa = mbedtls_pk_rsa( ctx );
143 ctx.pk_info = NULL;
144
145exit:
146 mbedtls_pk_free( &ctx );
147 return( status );
148}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100149#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100150 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Crond2fb8542020-12-09 15:18:01 +0100151 * defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
152 * defined(BUILTIN_ALG_RSA_PSS) ||
Ronald Cronf1057d32020-11-26 19:19:10 +0100153 * defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
154 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100155
Ronald Cronf1057d32020-11-26 19:19:10 +0100156#if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
157 defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100158
Ronald Cron784fb322020-11-30 13:55:05 +0100159static psa_status_t rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100160 const psa_key_attributes_t *attributes,
161 const uint8_t *data, size_t data_length,
162 uint8_t *key_buffer, size_t key_buffer_size,
163 size_t *key_buffer_length, size_t *bits )
164{
165 psa_status_t status;
166 mbedtls_rsa_context *rsa = NULL;
167
168 /* Parse input */
169 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
170 data,
171 data_length,
172 &rsa );
173 if( status != PSA_SUCCESS )
174 goto exit;
175
176 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
177
178 /* Re-export the data to PSA export format, such that we can store export
179 * representation in the key slot. Export representation in case of RSA is
180 * the smallest representation that's allowed as input, so a straight-up
181 * allocation of the same size as the input buffer will be large enough. */
182 status = mbedtls_psa_rsa_export_key( attributes->core.type,
183 rsa,
184 key_buffer,
185 key_buffer_size,
186 key_buffer_length );
187exit:
188 /* Always free the RSA object */
189 mbedtls_rsa_free( rsa );
190 mbedtls_free( rsa );
191
192 return( status );
193}
194
Ronald Crone5ca3d82020-11-26 16:36:16 +0100195psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
196 mbedtls_rsa_context *rsa,
197 uint8_t *data,
198 size_t data_size,
199 size_t *data_length )
200{
201#if defined(MBEDTLS_PK_WRITE_C)
202 int ret;
203 mbedtls_pk_context pk;
204 uint8_t *pos = data + data_size;
205
206 mbedtls_pk_init( &pk );
207 pk.pk_info = &mbedtls_rsa_info;
208 pk.pk_ctx = rsa;
209
210 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
211 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
212 * private key and of the RFC3279 RSAPublicKey for a public key. */
213 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
214 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
215 else
216 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
217
218 if( ret < 0 )
219 {
220 /* Clean up in case pk_write failed halfway through. */
221 memset( data, 0, data_size );
222 return( mbedtls_to_psa_error( ret ) );
223 }
224
225 /* The mbedtls_pk_xxx functions write to the end of the buffer.
226 * Move the data to the beginning and erase remaining data
227 * at the original location. */
228 if( 2 * (size_t) ret <= data_size )
229 {
230 memcpy( data, data + data_size - ret, ret );
231 memset( data + data_size - ret, 0, ret );
232 }
233 else if( (size_t) ret < data_size )
234 {
235 memmove( data, data + data_size - ret, ret );
236 memset( data + ret, 0, data_size - ret );
237 }
238
239 *data_length = ret;
240 return( PSA_SUCCESS );
241#else
242 (void) type;
243 (void) rsa;
244 (void) data;
245 (void) data_size;
246 (void) data_length;
247 return( PSA_ERROR_NOT_SUPPORTED );
248#endif /* MBEDTLS_PK_WRITE_C */
249}
250
Ronald Cronf1057d32020-11-26 19:19:10 +0100251static psa_status_t rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100252 const psa_key_attributes_t *attributes,
253 const uint8_t *key_buffer, size_t key_buffer_size,
254 uint8_t *data, size_t data_size, size_t *data_length )
255{
256 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
257 mbedtls_rsa_context *rsa = NULL;
258
259 status = mbedtls_psa_rsa_load_representation(
260 attributes->core.type, key_buffer, key_buffer_size, &rsa );
261 if( status != PSA_SUCCESS )
262 return( status );
263
264 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
265 rsa,
266 data,
267 data_size,
268 data_length );
269
270 mbedtls_rsa_free( rsa );
271 mbedtls_free( rsa );
272
273 return( status );
274}
Ronald Cronf1057d32020-11-26 19:19:10 +0100275#endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
276 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
277
Jaeden Amero424fa932021-05-14 08:34:32 +0100278#if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
279 defined(MBEDTLS_GENPRIME)
Ronald Cron2365fde2021-02-08 09:52:24 +0100280static psa_status_t psa_rsa_read_exponent( const uint8_t *domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100281 size_t domain_parameters_size,
282 int *exponent )
283{
284 size_t i;
285 uint32_t acc = 0;
286
287 if( domain_parameters_size == 0 )
288 {
289 *exponent = 65537;
290 return( PSA_SUCCESS );
291 }
292
293 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
294 * support values that fit in a 32-bit integer, which is larger than
295 * int on just about every platform anyway. */
296 if( domain_parameters_size > sizeof( acc ) )
297 return( PSA_ERROR_NOT_SUPPORTED );
298 for( i = 0; i < domain_parameters_size; i++ )
299 acc = ( acc << 8 ) | domain_parameters[i];
300 if( acc > INT_MAX )
301 return( PSA_ERROR_NOT_SUPPORTED );
302 *exponent = acc;
303 return( PSA_SUCCESS );
304}
305
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100306static psa_status_t rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100307 const psa_key_attributes_t *attributes,
308 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
309{
310 psa_status_t status;
311 mbedtls_rsa_context rsa;
312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
313 int exponent;
314
Ronald Cron2365fde2021-02-08 09:52:24 +0100315 status = psa_rsa_read_exponent( attributes->domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100316 attributes->domain_parameters_size,
317 &exponent );
318 if( status != PSA_SUCCESS )
319 return( status );
320
Ronald Cronc1905a12021-06-05 11:11:14 +0200321 mbedtls_rsa_init( &rsa );
Ronald Cron9e18fc12020-11-05 17:36:40 +0100322 ret = mbedtls_rsa_gen_key( &rsa,
323 mbedtls_psa_get_random,
324 MBEDTLS_PSA_RANDOM_STATE,
325 (unsigned int)attributes->core.bits,
326 exponent );
327 if( ret != 0 )
328 return( mbedtls_to_psa_error( ret ) );
329
330 status = mbedtls_psa_rsa_export_key( attributes->core.type,
331 &rsa, key_buffer, key_buffer_size,
332 key_buffer_length );
333 mbedtls_rsa_free( &rsa );
334
335 return( status );
336}
Jaeden Amero424fa932021-05-14 08:34:32 +0100337#endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
338 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100339
Ronald Cron7bdbca32020-12-09 13:34:54 +0100340/****************************************************************/
341/* Sign/verify hashes */
342/****************************************************************/
343
Ronald Crond2fb8542020-12-09 15:18:01 +0100344#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100345
346/* Decode the hash algorithm from alg and store the mbedtls encoding in
347 * md_alg. Verify that the hash length is acceptable. */
348static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
349 size_t hash_length,
350 mbedtls_md_type_t *md_alg )
351{
352 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
353 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
354 *md_alg = mbedtls_md_get_type( md_info );
355
356 /* The Mbed TLS RSA module uses an unsigned int for hash length
357 * parameters. Validate that it fits so that we don't risk an
358 * overflow later. */
359#if SIZE_MAX > UINT_MAX
360 if( hash_length > UINT_MAX )
361 return( PSA_ERROR_INVALID_ARGUMENT );
362#endif
363
Janos Follath0af093b2021-06-07 14:34:10 +0100364 /* For signatures using a hash, the hash length must be correct. */
365 if( alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Ronald Cron7bdbca32020-12-09 13:34:54 +0100366 {
367 if( md_info == NULL )
368 return( PSA_ERROR_NOT_SUPPORTED );
369 if( mbedtls_md_get_size( md_info ) != hash_length )
370 return( PSA_ERROR_INVALID_ARGUMENT );
371 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100372
373 return( PSA_SUCCESS );
374}
375
Ronald Crond2fb8542020-12-09 15:18:01 +0100376static psa_status_t rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100377 const psa_key_attributes_t *attributes,
378 const uint8_t *key_buffer, size_t key_buffer_size,
379 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
380 uint8_t *signature, size_t signature_size, size_t *signature_length )
381{
382 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
383 mbedtls_rsa_context *rsa = NULL;
384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
385 mbedtls_md_type_t md_alg;
386
387 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
388 key_buffer,
389 key_buffer_size,
390 &rsa );
391 if( status != PSA_SUCCESS )
392 return( status );
393
394 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
395 if( status != PSA_SUCCESS )
396 goto exit;
397
398 if( signature_size < mbedtls_rsa_get_len( rsa ) )
399 {
400 status = PSA_ERROR_BUFFER_TOO_SMALL;
401 goto exit;
402 }
403
Ronald Crond2fb8542020-12-09 15:18:01 +0100404#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100405 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
406 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200407 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
408 MBEDTLS_MD_NONE );
409 if( ret == 0 )
410 {
411 ret = mbedtls_rsa_pkcs1_sign( rsa,
412 mbedtls_psa_get_random,
413 MBEDTLS_PSA_RANDOM_STATE,
414 md_alg,
415 (unsigned int) hash_length,
416 hash,
417 signature );
418 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100419 }
420 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100421#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
422#if defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100423 if( PSA_ALG_IS_RSA_PSS( alg ) )
424 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200425 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
426
427 if( ret == 0 )
428 {
429 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
430 mbedtls_psa_get_random,
431 MBEDTLS_PSA_RANDOM_STATE,
432 MBEDTLS_MD_NONE,
433 (unsigned int) hash_length,
434 hash,
435 signature );
436 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100437 }
438 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100439#endif /* BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100440 {
441 status = PSA_ERROR_INVALID_ARGUMENT;
442 goto exit;
443 }
444
445 if( ret == 0 )
446 *signature_length = mbedtls_rsa_get_len( rsa );
447 status = mbedtls_to_psa_error( ret );
448
449exit:
450 mbedtls_rsa_free( rsa );
451 mbedtls_free( rsa );
452
453 return( status );
454}
455
Ronald Crond2fb8542020-12-09 15:18:01 +0100456static psa_status_t rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100457 const psa_key_attributes_t *attributes,
458 const uint8_t *key_buffer, size_t key_buffer_size,
459 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
460 const uint8_t *signature, size_t signature_length )
461{
462 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
463 mbedtls_rsa_context *rsa = NULL;
464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
465 mbedtls_md_type_t md_alg;
466
467 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
468 key_buffer,
469 key_buffer_size,
470 &rsa );
471 if( status != PSA_SUCCESS )
472 goto exit;
473
474 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
475 if( status != PSA_SUCCESS )
476 goto exit;
477
478 if( signature_length != mbedtls_rsa_get_len( rsa ) )
479 {
480 status = PSA_ERROR_INVALID_SIGNATURE;
481 goto exit;
482 }
483
Ronald Crond2fb8542020-12-09 15:18:01 +0100484#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100485 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
486 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200487 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
488 MBEDTLS_MD_NONE );
489 if( ret == 0 )
490 {
491 ret = mbedtls_rsa_pkcs1_verify( rsa,
492 md_alg,
493 (unsigned int) hash_length,
494 hash,
495 signature );
496 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100497 }
498 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100499#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
500#if defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100501 if( PSA_ALG_IS_RSA_PSS( alg ) )
502 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200503 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
504 if( ret == 0 )
505 {
506 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Janos Follath0af093b2021-06-07 14:34:10 +0100507 md_alg,
Ronald Cronea7631b2021-06-03 18:51:59 +0200508 (unsigned int) hash_length,
509 hash,
510 signature );
511 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100512 }
513 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100514#endif /* BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100515 {
516 status = PSA_ERROR_INVALID_ARGUMENT;
517 goto exit;
518 }
519
520 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
521 * the rest of the signature is invalid". This has little use in
522 * practice and PSA doesn't report this distinction. */
523 status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ?
524 PSA_ERROR_INVALID_SIGNATURE :
525 mbedtls_to_psa_error( ret );
526
527exit:
528 mbedtls_rsa_free( rsa );
529 mbedtls_free( rsa );
530
531 return( status );
532}
533
Ronald Crond2fb8542020-12-09 15:18:01 +0100534#endif /* defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
535 * defined(BUILTIN_ALG_RSA_PSS) */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100536
Ronald Cronf1057d32020-11-26 19:19:10 +0100537#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
538 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
539
Ronald Cron784fb322020-11-30 13:55:05 +0100540psa_status_t mbedtls_psa_rsa_import_key(
541 const psa_key_attributes_t *attributes,
542 const uint8_t *data, size_t data_length,
543 uint8_t *key_buffer, size_t key_buffer_size,
544 size_t *key_buffer_length, size_t *bits )
545{
546 return( rsa_import_key( attributes, data, data_length,
547 key_buffer, key_buffer_size,
548 key_buffer_length, bits ) );
549}
550
Ronald Cronf1057d32020-11-26 19:19:10 +0100551psa_status_t mbedtls_psa_rsa_export_public_key(
552 const psa_key_attributes_t *attributes,
553 const uint8_t *key_buffer, size_t key_buffer_size,
554 uint8_t *data, size_t data_size, size_t *data_length )
555{
556 return( rsa_export_public_key( attributes, key_buffer, key_buffer_size,
557 data, data_size, data_length ) );
558}
559
Ronald Crone5ca3d82020-11-26 16:36:16 +0100560#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
561 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
562
Jaeden Amero424fa932021-05-14 08:34:32 +0100563#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
564 defined(MBEDTLS_GENPRIME)
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100565psa_status_t mbedtls_psa_rsa_generate_key(
566 const psa_key_attributes_t *attributes,
567 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
568{
569 return( rsa_generate_key( attributes, key_buffer, key_buffer_size,
570 key_buffer_length ) );
571}
Jaeden Amero424fa932021-05-14 08:34:32 +0100572#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
573 * defined(MBEDTLS_GENPRIME) */
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100574
Ronald Crond2fb8542020-12-09 15:18:01 +0100575#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
576 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
577psa_status_t mbedtls_psa_rsa_sign_hash(
578 const psa_key_attributes_t *attributes,
579 const uint8_t *key_buffer, size_t key_buffer_size,
580 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
581 uint8_t *signature, size_t signature_size, size_t *signature_length )
582{
583 return( rsa_sign_hash(
584 attributes,
585 key_buffer, key_buffer_size,
586 alg, hash, hash_length,
587 signature, signature_size, signature_length ) );
588}
589
590psa_status_t mbedtls_psa_rsa_verify_hash(
591 const psa_key_attributes_t *attributes,
592 const uint8_t *key_buffer, size_t key_buffer_size,
593 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
594 const uint8_t *signature, size_t signature_length )
595{
596 return( rsa_verify_hash(
597 attributes,
598 key_buffer, key_buffer_size,
599 alg, hash, hash_length,
600 signature, signature_length ) );
601}
602#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
603 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
604
Ronald Cronf1057d32020-11-26 19:19:10 +0100605/*
606 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
607 */
608
609#if defined(PSA_CRYPTO_DRIVER_TEST)
610
611#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) || \
612 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron784fb322020-11-30 13:55:05 +0100613
614psa_status_t mbedtls_transparent_test_driver_rsa_import_key(
615 const psa_key_attributes_t *attributes,
616 const uint8_t *data, size_t data_length,
617 uint8_t *key_buffer, size_t key_buffer_size,
618 size_t *key_buffer_length, size_t *bits )
619{
620 return( rsa_import_key( attributes, data, data_length,
621 key_buffer, key_buffer_size,
622 key_buffer_length, bits ) );
623}
624
Ronald Cronf1057d32020-11-26 19:19:10 +0100625psa_status_t mbedtls_transparent_test_driver_rsa_export_public_key(
626 const psa_key_attributes_t *attributes,
627 const uint8_t *key_buffer, size_t key_buffer_size,
628 uint8_t *data, size_t data_size, size_t *data_length )
629{
630 return( rsa_export_public_key( attributes, key_buffer, key_buffer_size,
631 data, data_size, data_length ) );
632}
Ronald Cron784fb322020-11-30 13:55:05 +0100633
Ronald Cronf1057d32020-11-26 19:19:10 +0100634#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) ||
635 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) */
636
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100637#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR)
638psa_status_t mbedtls_transparent_test_driver_rsa_generate_key(
639 const psa_key_attributes_t *attributes,
640 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
641{
642 return( rsa_generate_key( attributes, key_buffer, key_buffer_size,
643 key_buffer_length ) );
644}
645#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) */
646
Ronald Crond2fb8542020-12-09 15:18:01 +0100647#if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \
648 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS)
649psa_status_t mbedtls_transparent_test_driver_rsa_sign_hash(
650 const psa_key_attributes_t *attributes,
651 const uint8_t *key_buffer, size_t key_buffer_size,
652 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
653 uint8_t *signature, size_t signature_size, size_t *signature_length )
654{
655#if defined(MBEDTLS_RSA_C) && \
656 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
657 return( rsa_sign_hash(
658 attributes,
659 key_buffer, key_buffer_size,
660 alg, hash, hash_length,
661 signature, signature_size, signature_length ) );
662#else
663 (void)attributes;
664 (void)key_buffer;
665 (void)key_buffer_size;
666 (void)alg;
667 (void)hash;
668 (void)hash_length;
669 (void)signature;
670 (void)signature_size;
671 (void)signature_length;
672 return( PSA_ERROR_NOT_SUPPORTED );
673#endif
674}
675
676psa_status_t mbedtls_transparent_test_driver_rsa_verify_hash(
677 const psa_key_attributes_t *attributes,
678 const uint8_t *key_buffer, size_t key_buffer_size,
679 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
680 const uint8_t *signature, size_t signature_length )
681{
682#if defined(MBEDTLS_RSA_C) && \
683 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
684 return( rsa_verify_hash(
685 attributes,
686 key_buffer, key_buffer_size,
687 alg, hash, hash_length,
688 signature, signature_length ) );
689#else
690 (void)attributes;
691 (void)key_buffer;
692 (void)key_buffer_size;
693 (void)alg;
694 (void)hash;
695 (void)hash_length;
696 (void)signature;
697 (void)signature_length;
698 return( PSA_ERROR_NOT_SUPPORTED );
699#endif
700}
701#endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) ||
702 * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */
703
Ronald Cronf1057d32020-11-26 19:19:10 +0100704#endif /* PSA_CRYPTO_DRIVER_TEST */
705
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100706#endif /* MBEDTLS_PSA_CRYPTO_C */