blob: 1ab1e94911f724ad9ca972a5f2eca36506f60403 [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>
42#include <mbedtls/pk_internal.h>
43
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(
111 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
112 else
113 status = mbedtls_to_psa_error(
114 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
115 if( status != PSA_SUCCESS )
116 goto exit;
117
118 /* We have something that the pkparse module recognizes. If it is a
119 * valid RSA key, store it. */
120 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
121 {
122 status = PSA_ERROR_INVALID_ARGUMENT;
123 goto exit;
124 }
125
126 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
127 * supports non-byte-aligned key sizes, but not well. For example,
128 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
129 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
130 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
131 {
132 status = PSA_ERROR_NOT_SUPPORTED;
133 goto exit;
134 }
135 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
136 if( status != PSA_SUCCESS )
137 goto exit;
138
139 /* Copy out the pointer to the RSA context, and reset the PK context
140 * such that pk_free doesn't free the RSA context we just grabbed. */
141 *p_rsa = mbedtls_pk_rsa( ctx );
142 ctx.pk_info = NULL;
143
144exit:
145 mbedtls_pk_free( &ctx );
146 return( status );
147}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100148#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100149 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Crond2fb8542020-12-09 15:18:01 +0100150 * defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
151 * defined(BUILTIN_ALG_RSA_PSS) ||
Ronald Cronf1057d32020-11-26 19:19:10 +0100152 * defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
153 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100154
Ronald Cronf1057d32020-11-26 19:19:10 +0100155#if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
156 defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100157
Ronald Cron784fb322020-11-30 13:55:05 +0100158static psa_status_t rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100159 const psa_key_attributes_t *attributes,
160 const uint8_t *data, size_t data_length,
161 uint8_t *key_buffer, size_t key_buffer_size,
162 size_t *key_buffer_length, size_t *bits )
163{
164 psa_status_t status;
165 mbedtls_rsa_context *rsa = NULL;
166
167 /* Parse input */
168 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
169 data,
170 data_length,
171 &rsa );
172 if( status != PSA_SUCCESS )
173 goto exit;
174
175 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
176
177 /* Re-export the data to PSA export format, such that we can store export
178 * representation in the key slot. Export representation in case of RSA is
179 * the smallest representation that's allowed as input, so a straight-up
180 * allocation of the same size as the input buffer will be large enough. */
181 status = mbedtls_psa_rsa_export_key( attributes->core.type,
182 rsa,
183 key_buffer,
184 key_buffer_size,
185 key_buffer_length );
186exit:
187 /* Always free the RSA object */
188 mbedtls_rsa_free( rsa );
189 mbedtls_free( rsa );
190
191 return( status );
192}
193
Ronald Crone5ca3d82020-11-26 16:36:16 +0100194psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
195 mbedtls_rsa_context *rsa,
196 uint8_t *data,
197 size_t data_size,
198 size_t *data_length )
199{
200#if defined(MBEDTLS_PK_WRITE_C)
201 int ret;
202 mbedtls_pk_context pk;
203 uint8_t *pos = data + data_size;
204
205 mbedtls_pk_init( &pk );
206 pk.pk_info = &mbedtls_rsa_info;
207 pk.pk_ctx = rsa;
208
209 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
210 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
211 * private key and of the RFC3279 RSAPublicKey for a public key. */
212 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
213 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
214 else
215 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
216
217 if( ret < 0 )
218 {
219 /* Clean up in case pk_write failed halfway through. */
220 memset( data, 0, data_size );
221 return( mbedtls_to_psa_error( ret ) );
222 }
223
224 /* The mbedtls_pk_xxx functions write to the end of the buffer.
225 * Move the data to the beginning and erase remaining data
226 * at the original location. */
227 if( 2 * (size_t) ret <= data_size )
228 {
229 memcpy( data, data + data_size - ret, ret );
230 memset( data + data_size - ret, 0, ret );
231 }
232 else if( (size_t) ret < data_size )
233 {
234 memmove( data, data + data_size - ret, ret );
235 memset( data + ret, 0, data_size - ret );
236 }
237
238 *data_length = ret;
239 return( PSA_SUCCESS );
240#else
241 (void) type;
242 (void) rsa;
243 (void) data;
244 (void) data_size;
245 (void) data_length;
246 return( PSA_ERROR_NOT_SUPPORTED );
247#endif /* MBEDTLS_PK_WRITE_C */
248}
249
Ronald Cronf1057d32020-11-26 19:19:10 +0100250static psa_status_t rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100251 const psa_key_attributes_t *attributes,
252 const uint8_t *key_buffer, size_t key_buffer_size,
253 uint8_t *data, size_t data_size, size_t *data_length )
254{
255 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
256 mbedtls_rsa_context *rsa = NULL;
257
258 status = mbedtls_psa_rsa_load_representation(
259 attributes->core.type, key_buffer, key_buffer_size, &rsa );
260 if( status != PSA_SUCCESS )
261 return( status );
262
263 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
264 rsa,
265 data,
266 data_size,
267 data_length );
268
269 mbedtls_rsa_free( rsa );
270 mbedtls_free( rsa );
271
272 return( status );
273}
Ronald Cronf1057d32020-11-26 19:19:10 +0100274#endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
275 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
276
Ronald Cron9e18fc12020-11-05 17:36:40 +0100277#if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron2365fde2021-02-08 09:52:24 +0100278static psa_status_t psa_rsa_read_exponent( const uint8_t *domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100279 size_t domain_parameters_size,
280 int *exponent )
281{
282 size_t i;
283 uint32_t acc = 0;
284
285 if( domain_parameters_size == 0 )
286 {
287 *exponent = 65537;
288 return( PSA_SUCCESS );
289 }
290
291 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
292 * support values that fit in a 32-bit integer, which is larger than
293 * int on just about every platform anyway. */
294 if( domain_parameters_size > sizeof( acc ) )
295 return( PSA_ERROR_NOT_SUPPORTED );
296 for( i = 0; i < domain_parameters_size; i++ )
297 acc = ( acc << 8 ) | domain_parameters[i];
298 if( acc > INT_MAX )
299 return( PSA_ERROR_NOT_SUPPORTED );
300 *exponent = acc;
301 return( PSA_SUCCESS );
302}
303
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100304static psa_status_t rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100305 const psa_key_attributes_t *attributes,
306 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
307{
308 psa_status_t status;
309 mbedtls_rsa_context rsa;
310 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
311 int exponent;
312
Ronald Cron2365fde2021-02-08 09:52:24 +0100313 status = psa_rsa_read_exponent( attributes->domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100314 attributes->domain_parameters_size,
315 &exponent );
316 if( status != PSA_SUCCESS )
317 return( status );
318
319 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
320 ret = mbedtls_rsa_gen_key( &rsa,
321 mbedtls_psa_get_random,
322 MBEDTLS_PSA_RANDOM_STATE,
323 (unsigned int)attributes->core.bits,
324 exponent );
325 if( ret != 0 )
326 return( mbedtls_to_psa_error( ret ) );
327
328 status = mbedtls_psa_rsa_export_key( attributes->core.type,
329 &rsa, key_buffer, key_buffer_size,
330 key_buffer_length );
331 mbedtls_rsa_free( &rsa );
332
333 return( status );
334}
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100335#endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100336
Ronald Cron7bdbca32020-12-09 13:34:54 +0100337/****************************************************************/
338/* Sign/verify hashes */
339/****************************************************************/
340
Ronald Crond2fb8542020-12-09 15:18:01 +0100341#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100342
343/* Decode the hash algorithm from alg and store the mbedtls encoding in
344 * md_alg. Verify that the hash length is acceptable. */
345static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
346 size_t hash_length,
347 mbedtls_md_type_t *md_alg )
348{
349 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
350 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
351 *md_alg = mbedtls_md_get_type( md_info );
352
353 /* The Mbed TLS RSA module uses an unsigned int for hash length
354 * parameters. Validate that it fits so that we don't risk an
355 * overflow later. */
356#if SIZE_MAX > UINT_MAX
357 if( hash_length > UINT_MAX )
358 return( PSA_ERROR_INVALID_ARGUMENT );
359#endif
360
Ronald Crond2fb8542020-12-09 15:18:01 +0100361#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100362 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
363 * must be correct. */
364 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
365 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
366 {
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 Crond2fb8542020-12-09 15:18:01 +0100372#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100373
Ronald Crond2fb8542020-12-09 15:18:01 +0100374#if defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100375 /* PSS requires a hash internally. */
376 if( PSA_ALG_IS_RSA_PSS( alg ) )
377 {
378 if( md_info == NULL )
379 return( PSA_ERROR_NOT_SUPPORTED );
380 }
Ronald Crond2fb8542020-12-09 15:18:01 +0100381#endif /* BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100382
383 return( PSA_SUCCESS );
384}
385
Ronald Crond2fb8542020-12-09 15:18:01 +0100386static psa_status_t rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100387 const psa_key_attributes_t *attributes,
388 const uint8_t *key_buffer, size_t key_buffer_size,
389 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
390 uint8_t *signature, size_t signature_size, size_t *signature_length )
391{
392 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
393 mbedtls_rsa_context *rsa = NULL;
394 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
395 mbedtls_md_type_t md_alg;
396
397 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
398 key_buffer,
399 key_buffer_size,
400 &rsa );
401 if( status != PSA_SUCCESS )
402 return( status );
403
404 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
405 if( status != PSA_SUCCESS )
406 goto exit;
407
408 if( signature_size < mbedtls_rsa_get_len( rsa ) )
409 {
410 status = PSA_ERROR_BUFFER_TOO_SMALL;
411 goto exit;
412 }
413
Ronald Crond2fb8542020-12-09 15:18:01 +0100414#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100415 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
416 {
417 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
418 MBEDTLS_MD_NONE );
419 ret = mbedtls_rsa_pkcs1_sign( rsa,
420 mbedtls_psa_get_random,
421 MBEDTLS_PSA_RANDOM_STATE,
422 MBEDTLS_RSA_PRIVATE,
423 md_alg,
424 (unsigned int) hash_length,
425 hash,
426 signature );
427 }
428 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100429#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
430#if defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100431 if( PSA_ALG_IS_RSA_PSS( alg ) )
432 {
433 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
434 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
435 mbedtls_psa_get_random,
436 MBEDTLS_PSA_RANDOM_STATE,
437 MBEDTLS_RSA_PRIVATE,
438 MBEDTLS_MD_NONE,
439 (unsigned int) hash_length,
440 hash,
441 signature );
442 }
443 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100444#endif /* BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100445 {
446 status = PSA_ERROR_INVALID_ARGUMENT;
447 goto exit;
448 }
449
450 if( ret == 0 )
451 *signature_length = mbedtls_rsa_get_len( rsa );
452 status = mbedtls_to_psa_error( ret );
453
454exit:
455 mbedtls_rsa_free( rsa );
456 mbedtls_free( rsa );
457
458 return( status );
459}
460
Ronald Crond2fb8542020-12-09 15:18:01 +0100461static psa_status_t rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100462 const psa_key_attributes_t *attributes,
463 const uint8_t *key_buffer, size_t key_buffer_size,
464 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
465 const uint8_t *signature, size_t signature_length )
466{
467 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
468 mbedtls_rsa_context *rsa = NULL;
469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
470 mbedtls_md_type_t md_alg;
471
472 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
473 key_buffer,
474 key_buffer_size,
475 &rsa );
476 if( status != PSA_SUCCESS )
477 goto exit;
478
479 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
480 if( status != PSA_SUCCESS )
481 goto exit;
482
483 if( signature_length != mbedtls_rsa_get_len( rsa ) )
484 {
485 status = PSA_ERROR_INVALID_SIGNATURE;
486 goto exit;
487 }
488
Ronald Crond2fb8542020-12-09 15:18:01 +0100489#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100490 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
491 {
492 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
493 MBEDTLS_MD_NONE );
494 ret = mbedtls_rsa_pkcs1_verify( rsa,
495 mbedtls_psa_get_random,
496 MBEDTLS_PSA_RANDOM_STATE,
497 MBEDTLS_RSA_PUBLIC,
498 md_alg,
499 (unsigned int) hash_length,
500 hash,
501 signature );
502 }
503 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100504#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
505#if defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100506 if( PSA_ALG_IS_RSA_PSS( alg ) )
507 {
508 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
509 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
510 mbedtls_psa_get_random,
511 MBEDTLS_PSA_RANDOM_STATE,
512 MBEDTLS_RSA_PUBLIC,
513 MBEDTLS_MD_NONE,
514 (unsigned int) hash_length,
515 hash,
516 signature );
517 }
518 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100519#endif /* BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100520 {
521 status = PSA_ERROR_INVALID_ARGUMENT;
522 goto exit;
523 }
524
525 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
526 * the rest of the signature is invalid". This has little use in
527 * practice and PSA doesn't report this distinction. */
528 status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ?
529 PSA_ERROR_INVALID_SIGNATURE :
530 mbedtls_to_psa_error( ret );
531
532exit:
533 mbedtls_rsa_free( rsa );
534 mbedtls_free( rsa );
535
536 return( status );
537}
538
Ronald Crond2fb8542020-12-09 15:18:01 +0100539#endif /* defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
540 * defined(BUILTIN_ALG_RSA_PSS) */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100541
Ronald Cronf1057d32020-11-26 19:19:10 +0100542#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
543 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
544
Ronald Cron784fb322020-11-30 13:55:05 +0100545psa_status_t mbedtls_psa_rsa_import_key(
546 const psa_key_attributes_t *attributes,
547 const uint8_t *data, size_t data_length,
548 uint8_t *key_buffer, size_t key_buffer_size,
549 size_t *key_buffer_length, size_t *bits )
550{
551 return( rsa_import_key( attributes, data, data_length,
552 key_buffer, key_buffer_size,
553 key_buffer_length, bits ) );
554}
555
Ronald Cronf1057d32020-11-26 19:19:10 +0100556psa_status_t mbedtls_psa_rsa_export_public_key(
557 const psa_key_attributes_t *attributes,
558 const uint8_t *key_buffer, size_t key_buffer_size,
559 uint8_t *data, size_t data_size, size_t *data_length )
560{
561 return( rsa_export_public_key( attributes, key_buffer, key_buffer_size,
562 data, data_size, data_length ) );
563}
564
Ronald Crone5ca3d82020-11-26 16:36:16 +0100565#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
566 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
567
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100568#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
569psa_status_t mbedtls_psa_rsa_generate_key(
570 const psa_key_attributes_t *attributes,
571 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
572{
573 return( rsa_generate_key( attributes, key_buffer, key_buffer_size,
574 key_buffer_length ) );
575}
576#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
577
Ronald Crond2fb8542020-12-09 15:18:01 +0100578#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
579 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
580psa_status_t mbedtls_psa_rsa_sign_hash(
581 const psa_key_attributes_t *attributes,
582 const uint8_t *key_buffer, size_t key_buffer_size,
583 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
584 uint8_t *signature, size_t signature_size, size_t *signature_length )
585{
586 return( rsa_sign_hash(
587 attributes,
588 key_buffer, key_buffer_size,
589 alg, hash, hash_length,
590 signature, signature_size, signature_length ) );
591}
592
593psa_status_t mbedtls_psa_rsa_verify_hash(
594 const psa_key_attributes_t *attributes,
595 const uint8_t *key_buffer, size_t key_buffer_size,
596 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
597 const uint8_t *signature, size_t signature_length )
598{
599 return( rsa_verify_hash(
600 attributes,
601 key_buffer, key_buffer_size,
602 alg, hash, hash_length,
603 signature, signature_length ) );
604}
605#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
606 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
607
Ronald Cronf1057d32020-11-26 19:19:10 +0100608/*
609 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
610 */
611
612#if defined(PSA_CRYPTO_DRIVER_TEST)
613
614#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) || \
615 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron784fb322020-11-30 13:55:05 +0100616
617psa_status_t mbedtls_transparent_test_driver_rsa_import_key(
618 const psa_key_attributes_t *attributes,
619 const uint8_t *data, size_t data_length,
620 uint8_t *key_buffer, size_t key_buffer_size,
621 size_t *key_buffer_length, size_t *bits )
622{
623 return( rsa_import_key( attributes, data, data_length,
624 key_buffer, key_buffer_size,
625 key_buffer_length, bits ) );
626}
627
Ronald Cronf1057d32020-11-26 19:19:10 +0100628psa_status_t mbedtls_transparent_test_driver_rsa_export_public_key(
629 const psa_key_attributes_t *attributes,
630 const uint8_t *key_buffer, size_t key_buffer_size,
631 uint8_t *data, size_t data_size, size_t *data_length )
632{
633 return( rsa_export_public_key( attributes, key_buffer, key_buffer_size,
634 data, data_size, data_length ) );
635}
Ronald Cron784fb322020-11-30 13:55:05 +0100636
Ronald Cronf1057d32020-11-26 19:19:10 +0100637#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) ||
638 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) */
639
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100640#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR)
641psa_status_t mbedtls_transparent_test_driver_rsa_generate_key(
642 const psa_key_attributes_t *attributes,
643 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
644{
645 return( rsa_generate_key( attributes, key_buffer, key_buffer_size,
646 key_buffer_length ) );
647}
648#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) */
649
Ronald Crond2fb8542020-12-09 15:18:01 +0100650#if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \
651 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS)
652psa_status_t mbedtls_transparent_test_driver_rsa_sign_hash(
653 const psa_key_attributes_t *attributes,
654 const uint8_t *key_buffer, size_t key_buffer_size,
655 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
656 uint8_t *signature, size_t signature_size, size_t *signature_length )
657{
658#if defined(MBEDTLS_RSA_C) && \
659 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
660 return( rsa_sign_hash(
661 attributes,
662 key_buffer, key_buffer_size,
663 alg, hash, hash_length,
664 signature, signature_size, signature_length ) );
665#else
666 (void)attributes;
667 (void)key_buffer;
668 (void)key_buffer_size;
669 (void)alg;
670 (void)hash;
671 (void)hash_length;
672 (void)signature;
673 (void)signature_size;
674 (void)signature_length;
675 return( PSA_ERROR_NOT_SUPPORTED );
676#endif
677}
678
679psa_status_t mbedtls_transparent_test_driver_rsa_verify_hash(
680 const psa_key_attributes_t *attributes,
681 const uint8_t *key_buffer, size_t key_buffer_size,
682 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
683 const uint8_t *signature, size_t signature_length )
684{
685#if defined(MBEDTLS_RSA_C) && \
686 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
687 return( rsa_verify_hash(
688 attributes,
689 key_buffer, key_buffer_size,
690 alg, hash, hash_length,
691 signature, signature_length ) );
692#else
693 (void)attributes;
694 (void)key_buffer;
695 (void)key_buffer_size;
696 (void)alg;
697 (void)hash;
698 (void)hash_length;
699 (void)signature;
700 (void)signature_length;
701 return( PSA_ERROR_NOT_SUPPORTED );
702#endif
703}
704#endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) ||
705 * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */
706
Ronald Cronf1057d32020-11-26 19:19:10 +0100707#endif /* PSA_CRYPTO_DRIVER_TEST */
708
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100709#endif /* MBEDTLS_PSA_CRYPTO_C */