blob: 9492a6b14f09a46587dfee7eb6a86c6942e1db52 [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) && \
Ronald Cron73c9d9e2021-04-09 11:09:54 +020046 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cronf1057d32020-11-26 19:19:10 +010047 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) ) )
48#define BUILTIN_KEY_TYPE_RSA_KEY_PAIR 1
49#endif
50
Ronald Cron73c9d9e2021-04-09 11:09:54 +020051#if ( defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) || \
52 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
53 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Cronf1057d32020-11-26 19:19:10 +010054 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) ) )
55#define BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY 1
56#endif
57
Ronald Crond2fb8542020-12-09 15:18:01 +010058#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
59 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
Ronald Cron73c9d9e2021-04-09 11:09:54 +020060 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Crond2fb8542020-12-09 15:18:01 +010061 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) && \
62 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V15) ) )
63#define BUILTIN_ALG_RSA_PKCS1V15_SIGN 1
64#endif
65
66#if ( defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
67 ( defined(PSA_CRYPTO_DRIVER_TEST) && \
Ronald Cron73c9d9e2021-04-09 11:09:54 +020068 defined(MBEDTLS_PSA_CRYPTO_CONFIG) && \
Ronald Crond2fb8542020-12-09 15:18:01 +010069 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) && \
70 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) ) )
71#define BUILTIN_ALG_RSA_PSS 1
72#endif
73
Ronald Cron00b7bfc2020-11-25 15:25:26 +010074#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
Ronald Cron00b7bfc2020-11-25 15:25:26 +010075 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
Ronald Crond2fb8542020-12-09 15:18:01 +010076 defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
77 defined(BUILTIN_ALG_RSA_PSS) || \
Ronald Cronf1057d32020-11-26 19:19:10 +010078 defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
79 defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron00b7bfc2020-11-25 15:25:26 +010080
81/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
82 * that are not a multiple of 8) well. For example, there is only
83 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
84 * way to return the exact bit size of a key.
85 * To keep things simple, reject non-byte-aligned key sizes. */
86static psa_status_t psa_check_rsa_key_byte_aligned(
87 const mbedtls_rsa_context *rsa )
88{
89 mbedtls_mpi n;
90 psa_status_t status;
91 mbedtls_mpi_init( &n );
92 status = mbedtls_to_psa_error(
93 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
94 if( status == PSA_SUCCESS )
95 {
96 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
97 status = PSA_ERROR_NOT_SUPPORTED;
98 }
99 mbedtls_mpi_free( &n );
100 return( status );
101}
102
103psa_status_t mbedtls_psa_rsa_load_representation(
104 psa_key_type_t type, const uint8_t *data, size_t data_length,
105 mbedtls_rsa_context **p_rsa )
106{
107 psa_status_t status;
108 mbedtls_pk_context ctx;
109 size_t bits;
110 mbedtls_pk_init( &ctx );
111
112 /* Parse the data. */
113 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
114 status = mbedtls_to_psa_error(
Manuel Pégourié-Gonnard84dea012021-06-15 11:29:26 +0200115 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0,
116 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100117 else
118 status = mbedtls_to_psa_error(
119 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
120 if( status != PSA_SUCCESS )
121 goto exit;
122
123 /* We have something that the pkparse module recognizes. If it is a
124 * valid RSA key, store it. */
125 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
126 {
127 status = PSA_ERROR_INVALID_ARGUMENT;
128 goto exit;
129 }
130
131 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
132 * supports non-byte-aligned key sizes, but not well. For example,
133 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
134 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
135 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
136 {
137 status = PSA_ERROR_NOT_SUPPORTED;
138 goto exit;
139 }
140 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
141 if( status != PSA_SUCCESS )
142 goto exit;
143
144 /* Copy out the pointer to the RSA context, and reset the PK context
145 * such that pk_free doesn't free the RSA context we just grabbed. */
146 *p_rsa = mbedtls_pk_rsa( ctx );
147 ctx.pk_info = NULL;
148
149exit:
150 mbedtls_pk_free( &ctx );
151 return( status );
152}
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100153#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100154 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
Ronald Crond2fb8542020-12-09 15:18:01 +0100155 * defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
156 * defined(BUILTIN_ALG_RSA_PSS) ||
Ronald Cronf1057d32020-11-26 19:19:10 +0100157 * defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
158 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100159
Ronald Cronf1057d32020-11-26 19:19:10 +0100160#if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
161 defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cronabf2aef2020-11-27 18:13:44 +0100162
Ronald Cron784fb322020-11-30 13:55:05 +0100163static psa_status_t rsa_import_key(
Ronald Cronabf2aef2020-11-27 18:13:44 +0100164 const psa_key_attributes_t *attributes,
165 const uint8_t *data, size_t data_length,
166 uint8_t *key_buffer, size_t key_buffer_size,
167 size_t *key_buffer_length, size_t *bits )
168{
169 psa_status_t status;
170 mbedtls_rsa_context *rsa = NULL;
171
172 /* Parse input */
173 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
174 data,
175 data_length,
176 &rsa );
177 if( status != PSA_SUCCESS )
178 goto exit;
179
180 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
181
182 /* Re-export the data to PSA export format, such that we can store export
183 * representation in the key slot. Export representation in case of RSA is
184 * the smallest representation that's allowed as input, so a straight-up
185 * allocation of the same size as the input buffer will be large enough. */
186 status = mbedtls_psa_rsa_export_key( attributes->core.type,
187 rsa,
188 key_buffer,
189 key_buffer_size,
190 key_buffer_length );
191exit:
192 /* Always free the RSA object */
193 mbedtls_rsa_free( rsa );
194 mbedtls_free( rsa );
195
196 return( status );
197}
198
Ronald Crone5ca3d82020-11-26 16:36:16 +0100199psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
200 mbedtls_rsa_context *rsa,
201 uint8_t *data,
202 size_t data_size,
203 size_t *data_length )
204{
205#if defined(MBEDTLS_PK_WRITE_C)
206 int ret;
207 mbedtls_pk_context pk;
208 uint8_t *pos = data + data_size;
209
210 mbedtls_pk_init( &pk );
211 pk.pk_info = &mbedtls_rsa_info;
212 pk.pk_ctx = rsa;
213
214 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
215 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
216 * private key and of the RFC3279 RSAPublicKey for a public key. */
217 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
218 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
219 else
220 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
221
222 if( ret < 0 )
223 {
224 /* Clean up in case pk_write failed halfway through. */
225 memset( data, 0, data_size );
226 return( mbedtls_to_psa_error( ret ) );
227 }
228
229 /* The mbedtls_pk_xxx functions write to the end of the buffer.
230 * Move the data to the beginning and erase remaining data
231 * at the original location. */
232 if( 2 * (size_t) ret <= data_size )
233 {
234 memcpy( data, data + data_size - ret, ret );
235 memset( data + data_size - ret, 0, ret );
236 }
237 else if( (size_t) ret < data_size )
238 {
239 memmove( data, data + data_size - ret, ret );
240 memset( data + ret, 0, data_size - ret );
241 }
242
243 *data_length = ret;
244 return( PSA_SUCCESS );
245#else
246 (void) type;
247 (void) rsa;
248 (void) data;
249 (void) data_size;
250 (void) data_length;
251 return( PSA_ERROR_NOT_SUPPORTED );
252#endif /* MBEDTLS_PK_WRITE_C */
253}
254
Ronald Cronf1057d32020-11-26 19:19:10 +0100255static psa_status_t rsa_export_public_key(
Ronald Crone5ca3d82020-11-26 16:36:16 +0100256 const psa_key_attributes_t *attributes,
257 const uint8_t *key_buffer, size_t key_buffer_size,
258 uint8_t *data, size_t data_size, size_t *data_length )
259{
260 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
261 mbedtls_rsa_context *rsa = NULL;
262
263 status = mbedtls_psa_rsa_load_representation(
264 attributes->core.type, key_buffer, key_buffer_size, &rsa );
265 if( status != PSA_SUCCESS )
266 return( status );
267
268 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
269 rsa,
270 data,
271 data_size,
272 data_length );
273
274 mbedtls_rsa_free( rsa );
275 mbedtls_free( rsa );
276
277 return( status );
278}
Ronald Cronf1057d32020-11-26 19:19:10 +0100279#endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
280 * defined(BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
281
Jaeden Amero424fa932021-05-14 08:34:32 +0100282#if defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
283 defined(MBEDTLS_GENPRIME)
Ronald Cron2365fde2021-02-08 09:52:24 +0100284static psa_status_t psa_rsa_read_exponent( const uint8_t *domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100285 size_t domain_parameters_size,
286 int *exponent )
287{
288 size_t i;
289 uint32_t acc = 0;
290
291 if( domain_parameters_size == 0 )
292 {
293 *exponent = 65537;
294 return( PSA_SUCCESS );
295 }
296
297 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
298 * support values that fit in a 32-bit integer, which is larger than
299 * int on just about every platform anyway. */
300 if( domain_parameters_size > sizeof( acc ) )
301 return( PSA_ERROR_NOT_SUPPORTED );
302 for( i = 0; i < domain_parameters_size; i++ )
303 acc = ( acc << 8 ) | domain_parameters[i];
304 if( acc > INT_MAX )
305 return( PSA_ERROR_NOT_SUPPORTED );
306 *exponent = acc;
307 return( PSA_SUCCESS );
308}
309
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100310static psa_status_t rsa_generate_key(
Ronald Cron9e18fc12020-11-05 17:36:40 +0100311 const psa_key_attributes_t *attributes,
312 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
313{
314 psa_status_t status;
315 mbedtls_rsa_context rsa;
316 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
317 int exponent;
318
Ronald Cron2365fde2021-02-08 09:52:24 +0100319 status = psa_rsa_read_exponent( attributes->domain_parameters,
Ronald Cron9e18fc12020-11-05 17:36:40 +0100320 attributes->domain_parameters_size,
321 &exponent );
322 if( status != PSA_SUCCESS )
323 return( status );
324
Ronald Cronc1905a12021-06-05 11:11:14 +0200325 mbedtls_rsa_init( &rsa );
Ronald Cron9e18fc12020-11-05 17:36:40 +0100326 ret = mbedtls_rsa_gen_key( &rsa,
327 mbedtls_psa_get_random,
328 MBEDTLS_PSA_RANDOM_STATE,
329 (unsigned int)attributes->core.bits,
330 exponent );
331 if( ret != 0 )
332 return( mbedtls_to_psa_error( ret ) );
333
334 status = mbedtls_psa_rsa_export_key( attributes->core.type,
335 &rsa, key_buffer, key_buffer_size,
336 key_buffer_length );
337 mbedtls_rsa_free( &rsa );
338
339 return( status );
340}
Jaeden Amero424fa932021-05-14 08:34:32 +0100341#endif /* defined(BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
342 * defined(MBEDTLS_GENPRIME) */
Ronald Cron9e18fc12020-11-05 17:36:40 +0100343
Ronald Cron7bdbca32020-12-09 13:34:54 +0100344/****************************************************************/
345/* Sign/verify hashes */
346/****************************************************************/
347
Ronald Crond2fb8542020-12-09 15:18:01 +0100348#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) || defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100349
350/* Decode the hash algorithm from alg and store the mbedtls encoding in
351 * md_alg. Verify that the hash length is acceptable. */
352static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
353 size_t hash_length,
354 mbedtls_md_type_t *md_alg )
355{
356 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
357 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
358 *md_alg = mbedtls_md_get_type( md_info );
359
360 /* The Mbed TLS RSA module uses an unsigned int for hash length
361 * parameters. Validate that it fits so that we don't risk an
362 * overflow later. */
363#if SIZE_MAX > UINT_MAX
364 if( hash_length > UINT_MAX )
365 return( PSA_ERROR_INVALID_ARGUMENT );
366#endif
367
Janos Follath0af093b2021-06-07 14:34:10 +0100368 /* For signatures using a hash, the hash length must be correct. */
369 if( alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Ronald Cron7bdbca32020-12-09 13:34:54 +0100370 {
371 if( md_info == NULL )
372 return( PSA_ERROR_NOT_SUPPORTED );
373 if( mbedtls_md_get_size( md_info ) != hash_length )
374 return( PSA_ERROR_INVALID_ARGUMENT );
375 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100376
377 return( PSA_SUCCESS );
378}
379
Ronald Crond2fb8542020-12-09 15:18:01 +0100380static psa_status_t rsa_sign_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100381 const psa_key_attributes_t *attributes,
382 const uint8_t *key_buffer, size_t key_buffer_size,
383 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
384 uint8_t *signature, size_t signature_size, size_t *signature_length )
385{
386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
387 mbedtls_rsa_context *rsa = NULL;
388 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
389 mbedtls_md_type_t md_alg;
390
391 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
392 key_buffer,
393 key_buffer_size,
394 &rsa );
395 if( status != PSA_SUCCESS )
396 return( status );
397
398 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
399 if( status != PSA_SUCCESS )
400 goto exit;
401
402 if( signature_size < mbedtls_rsa_get_len( rsa ) )
403 {
404 status = PSA_ERROR_BUFFER_TOO_SMALL;
405 goto exit;
406 }
407
Ronald Crond2fb8542020-12-09 15:18:01 +0100408#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100409 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
410 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200411 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
412 MBEDTLS_MD_NONE );
413 if( ret == 0 )
414 {
415 ret = mbedtls_rsa_pkcs1_sign( rsa,
416 mbedtls_psa_get_random,
417 MBEDTLS_PSA_RANDOM_STATE,
418 md_alg,
419 (unsigned int) hash_length,
420 hash,
421 signature );
422 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100423 }
424 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100425#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
426#if defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100427 if( PSA_ALG_IS_RSA_PSS( alg ) )
428 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200429 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
430
431 if( ret == 0 )
432 {
433 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
434 mbedtls_psa_get_random,
435 MBEDTLS_PSA_RANDOM_STATE,
436 MBEDTLS_MD_NONE,
437 (unsigned int) hash_length,
438 hash,
439 signature );
440 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100441 }
442 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100443#endif /* BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100444 {
445 status = PSA_ERROR_INVALID_ARGUMENT;
446 goto exit;
447 }
448
449 if( ret == 0 )
450 *signature_length = mbedtls_rsa_get_len( rsa );
451 status = mbedtls_to_psa_error( ret );
452
453exit:
454 mbedtls_rsa_free( rsa );
455 mbedtls_free( rsa );
456
457 return( status );
458}
459
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200460#if defined(BUILTIN_ALG_RSA_PSS)
461static int rsa_pss_expected_salt_len( psa_algorithm_t alg,
462 const mbedtls_rsa_context *rsa,
463 size_t hash_length )
464{
465 if( PSA_ALG_IS_RSA_PSS_ANY_SALT( alg ) )
466 return( MBEDTLS_RSA_SALT_LEN_ANY );
467 /* Otherwise: standard salt length, i.e. largest possible salt length
468 * up to the hash length. */
Gilles Peskinef6892de2021-10-08 16:28:32 +0200469 int klen = (int) mbedtls_rsa_get_len( rsa ); // known to fit
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200470 int hlen = (int) hash_length; // known to fit
471 int room = klen - 2 - hlen;
472 if( room < 0 )
473 return( 0 ); // there is no valid signature in this case anyway
474 else if( room > hlen )
475 return( hlen );
476 else
477 return( room );
478}
479#endif
480
Ronald Crond2fb8542020-12-09 15:18:01 +0100481static psa_status_t rsa_verify_hash(
Ronald Cron7bdbca32020-12-09 13:34:54 +0100482 const psa_key_attributes_t *attributes,
483 const uint8_t *key_buffer, size_t key_buffer_size,
484 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
485 const uint8_t *signature, size_t signature_length )
486{
487 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
488 mbedtls_rsa_context *rsa = NULL;
489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
490 mbedtls_md_type_t md_alg;
491
492 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
493 key_buffer,
494 key_buffer_size,
495 &rsa );
496 if( status != PSA_SUCCESS )
497 goto exit;
498
499 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
500 if( status != PSA_SUCCESS )
501 goto exit;
502
503 if( signature_length != mbedtls_rsa_get_len( rsa ) )
504 {
505 status = PSA_ERROR_INVALID_SIGNATURE;
506 goto exit;
507 }
508
Ronald Crond2fb8542020-12-09 15:18:01 +0100509#if defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100510 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
511 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200512 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
513 MBEDTLS_MD_NONE );
514 if( ret == 0 )
515 {
516 ret = mbedtls_rsa_pkcs1_verify( rsa,
517 md_alg,
518 (unsigned int) hash_length,
519 hash,
520 signature );
521 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100522 }
523 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100524#endif /* BUILTIN_ALG_RSA_PKCS1V15_SIGN */
525#if defined(BUILTIN_ALG_RSA_PSS)
Ronald Cron7bdbca32020-12-09 13:34:54 +0100526 if( PSA_ALG_IS_RSA_PSS( alg ) )
527 {
Ronald Cronea7631b2021-06-03 18:51:59 +0200528 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
529 if( ret == 0 )
530 {
Gilles Peskineb9b817e2021-10-04 22:15:05 +0200531 int slen = rsa_pss_expected_salt_len( alg, rsa, hash_length );
532 ret = mbedtls_rsa_rsassa_pss_verify_ext( rsa,
533 md_alg,
534 (unsigned) hash_length,
535 hash,
536 md_alg,
537 slen,
538 signature );
Ronald Cronea7631b2021-06-03 18:51:59 +0200539 }
Ronald Cron7bdbca32020-12-09 13:34:54 +0100540 }
541 else
Ronald Crond2fb8542020-12-09 15:18:01 +0100542#endif /* BUILTIN_ALG_RSA_PSS */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100543 {
544 status = PSA_ERROR_INVALID_ARGUMENT;
545 goto exit;
546 }
547
548 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
549 * the rest of the signature is invalid". This has little use in
550 * practice and PSA doesn't report this distinction. */
551 status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ?
552 PSA_ERROR_INVALID_SIGNATURE :
553 mbedtls_to_psa_error( ret );
554
555exit:
556 mbedtls_rsa_free( rsa );
557 mbedtls_free( rsa );
558
559 return( status );
560}
561
Ronald Crond2fb8542020-12-09 15:18:01 +0100562#endif /* defined(BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
563 * defined(BUILTIN_ALG_RSA_PSS) */
Ronald Cron7bdbca32020-12-09 13:34:54 +0100564
Ronald Cronf1057d32020-11-26 19:19:10 +0100565#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
566 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
567
Ronald Cron784fb322020-11-30 13:55:05 +0100568psa_status_t mbedtls_psa_rsa_import_key(
569 const psa_key_attributes_t *attributes,
570 const uint8_t *data, size_t data_length,
571 uint8_t *key_buffer, size_t key_buffer_size,
572 size_t *key_buffer_length, size_t *bits )
573{
574 return( rsa_import_key( attributes, data, data_length,
575 key_buffer, key_buffer_size,
576 key_buffer_length, bits ) );
577}
578
Ronald Cronf1057d32020-11-26 19:19:10 +0100579psa_status_t mbedtls_psa_rsa_export_public_key(
580 const psa_key_attributes_t *attributes,
581 const uint8_t *key_buffer, size_t key_buffer_size,
582 uint8_t *data, size_t data_size, size_t *data_length )
583{
584 return( rsa_export_public_key( attributes, key_buffer, key_buffer_size,
585 data, data_size, data_length ) );
586}
587
Ronald Crone5ca3d82020-11-26 16:36:16 +0100588#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
589 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
590
Jaeden Amero424fa932021-05-14 08:34:32 +0100591#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
592 defined(MBEDTLS_GENPRIME)
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100593psa_status_t mbedtls_psa_rsa_generate_key(
594 const psa_key_attributes_t *attributes,
595 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
596{
597 return( rsa_generate_key( attributes, key_buffer, key_buffer_size,
598 key_buffer_length ) );
599}
Jaeden Amero424fa932021-05-14 08:34:32 +0100600#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
601 * defined(MBEDTLS_GENPRIME) */
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100602
Ronald Crond2fb8542020-12-09 15:18:01 +0100603#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
604 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
605psa_status_t mbedtls_psa_rsa_sign_hash(
606 const psa_key_attributes_t *attributes,
607 const uint8_t *key_buffer, size_t key_buffer_size,
608 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
609 uint8_t *signature, size_t signature_size, size_t *signature_length )
610{
611 return( rsa_sign_hash(
612 attributes,
613 key_buffer, key_buffer_size,
614 alg, hash, hash_length,
615 signature, signature_size, signature_length ) );
616}
617
618psa_status_t mbedtls_psa_rsa_verify_hash(
619 const psa_key_attributes_t *attributes,
620 const uint8_t *key_buffer, size_t key_buffer_size,
621 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
622 const uint8_t *signature, size_t signature_length )
623{
624 return( rsa_verify_hash(
625 attributes,
626 key_buffer, key_buffer_size,
627 alg, hash, hash_length,
628 signature, signature_length ) );
629}
630#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
631 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
632
Ronald Cronf1057d32020-11-26 19:19:10 +0100633/*
634 * BEYOND THIS POINT, TEST DRIVER ENTRY POINTS ONLY.
635 */
636
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200637#if defined(PSA_CRYPTO_DRIVER_TEST) && defined(MBEDTLS_PSA_CRYPTO_CONFIG)
Ronald Cronf1057d32020-11-26 19:19:10 +0100638
639#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) || \
640 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron784fb322020-11-30 13:55:05 +0100641
Ronald Cron40170d92021-03-13 18:19:08 +0100642psa_status_t libtestdriver1_mbedtls_psa_rsa_import_key(
Ronald Cron784fb322020-11-30 13:55:05 +0100643 const psa_key_attributes_t *attributes,
644 const uint8_t *data, size_t data_length,
645 uint8_t *key_buffer, size_t key_buffer_size,
646 size_t *key_buffer_length, size_t *bits )
647{
648 return( rsa_import_key( attributes, data, data_length,
649 key_buffer, key_buffer_size,
650 key_buffer_length, bits ) );
651}
652
Ronald Cron40170d92021-03-13 18:19:08 +0100653psa_status_t libtestdriver1_mbedtls_psa_rsa_export_public_key(
Ronald Cronf1057d32020-11-26 19:19:10 +0100654 const psa_key_attributes_t *attributes,
655 const uint8_t *key_buffer, size_t key_buffer_size,
656 uint8_t *data, size_t data_size, size_t *data_length )
657{
658 return( rsa_export_public_key( attributes, key_buffer, key_buffer_size,
659 data, data_size, data_length ) );
660}
Ronald Cron784fb322020-11-30 13:55:05 +0100661
Ronald Cronf1057d32020-11-26 19:19:10 +0100662#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) ||
663 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_PUBLIC_KEY) */
664
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100665#if defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron40170d92021-03-13 18:19:08 +0100666psa_status_t libtestdriver1_mbedtls_psa_rsa_generate_key(
Ronald Cron3a9c46b2020-11-06 09:38:35 +0100667 const psa_key_attributes_t *attributes,
668 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
669{
670 return( rsa_generate_key( attributes, key_buffer, key_buffer_size,
671 key_buffer_length ) );
672}
673#endif /* defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_RSA_KEY_PAIR) */
674
Ronald Crond2fb8542020-12-09 15:18:01 +0100675#if defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) || \
676 defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS)
Ronald Cron40170d92021-03-13 18:19:08 +0100677psa_status_t libtestdriver1_mbedtls_psa_rsa_sign_hash(
Ronald Crond2fb8542020-12-09 15:18:01 +0100678 const psa_key_attributes_t *attributes,
679 const uint8_t *key_buffer, size_t key_buffer_size,
680 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
681 uint8_t *signature, size_t signature_size, size_t *signature_length )
682{
683#if defined(MBEDTLS_RSA_C) && \
684 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
685 return( rsa_sign_hash(
686 attributes,
687 key_buffer, key_buffer_size,
688 alg, hash, hash_length,
689 signature, signature_size, signature_length ) );
690#else
691 (void)attributes;
692 (void)key_buffer;
693 (void)key_buffer_size;
694 (void)alg;
695 (void)hash;
696 (void)hash_length;
697 (void)signature;
698 (void)signature_size;
699 (void)signature_length;
700 return( PSA_ERROR_NOT_SUPPORTED );
701#endif
702}
703
Ronald Cron40170d92021-03-13 18:19:08 +0100704psa_status_t libtestdriver1_mbedtls_psa_rsa_verify_hash(
Ronald Crond2fb8542020-12-09 15:18:01 +0100705 const psa_key_attributes_t *attributes,
706 const uint8_t *key_buffer, size_t key_buffer_size,
707 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
708 const uint8_t *signature, size_t signature_length )
709{
710#if defined(MBEDTLS_RSA_C) && \
711 (defined(MBEDTLS_PKCS1_V15) || defined(MBEDTLS_PKCS1_V21))
712 return( rsa_verify_hash(
713 attributes,
714 key_buffer, key_buffer_size,
715 alg, hash, hash_length,
716 signature, signature_length ) );
717#else
718 (void)attributes;
719 (void)key_buffer;
720 (void)key_buffer_size;
721 (void)alg;
722 (void)hash;
723 (void)hash_length;
724 (void)signature;
725 (void)signature_length;
726 return( PSA_ERROR_NOT_SUPPORTED );
727#endif
728}
729#endif /* defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PKCS1V15_SIGN) ||
730 * defined(MBEDTLS_PSA_ACCEL_ALG_RSA_PSS) */
731
Ronald Cron73c9d9e2021-04-09 11:09:54 +0200732#endif /* PSA_CRYPTO_DRIVER_TEST && MBEDTLS_PSA_CRYPTO_CONFIG */
Ronald Cronf1057d32020-11-26 19:19:10 +0100733
Ronald Cron00b7bfc2020-11-25 15:25:26 +0100734#endif /* MBEDTLS_PSA_CRYPTO_C */