1 /*
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"
27 #include "psa_crypto_random_impl.h"
28 #include "psa_crypto_rsa.h"
29 #include "psa_crypto_hash.h"
30
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 "pk_wrap.h"
43
44 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
45 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
46 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
47 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
48 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
49 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
50
51 /* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
52 * that are not a multiple of 8) well. For example, there is only
53 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
54 * way to return the exact bit size of a key.
55 * To keep things simple, reject non-byte-aligned key sizes. */
psa_check_rsa_key_byte_aligned(const mbedtls_rsa_context * rsa)56 static psa_status_t psa_check_rsa_key_byte_aligned(
57 const mbedtls_rsa_context *rsa )
58 {
59 mbedtls_mpi n;
60 psa_status_t status;
61 mbedtls_mpi_init( &n );
62 status = mbedtls_to_psa_error(
63 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
64 if( status == PSA_SUCCESS )
65 {
66 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
67 status = PSA_ERROR_NOT_SUPPORTED;
68 }
69 mbedtls_mpi_free( &n );
70 return( status );
71 }
72
mbedtls_psa_rsa_load_representation(psa_key_type_t type,const uint8_t * data,size_t data_length,mbedtls_rsa_context ** p_rsa)73 psa_status_t mbedtls_psa_rsa_load_representation(
74 psa_key_type_t type, const uint8_t *data, size_t data_length,
75 mbedtls_rsa_context **p_rsa )
76 {
77 psa_status_t status;
78 mbedtls_pk_context ctx;
79 size_t bits;
80 mbedtls_pk_init( &ctx );
81
82 /* Parse the data. */
83 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
84 status = mbedtls_to_psa_error(
85 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0,
86 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
87 else
88 status = mbedtls_to_psa_error(
89 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
90 if( status != PSA_SUCCESS )
91 goto exit;
92
93 /* We have something that the pkparse module recognizes. If it is a
94 * valid RSA key, store it. */
95 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
96 {
97 status = PSA_ERROR_INVALID_ARGUMENT;
98 goto exit;
99 }
100
101 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
102 * supports non-byte-aligned key sizes, but not well. For example,
103 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
104 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
105 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
106 {
107 status = PSA_ERROR_NOT_SUPPORTED;
108 goto exit;
109 }
110 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
111 if( status != PSA_SUCCESS )
112 goto exit;
113
114 /* Copy out the pointer to the RSA context, and reset the PK context
115 * such that pk_free doesn't free the RSA context we just grabbed. */
116 *p_rsa = mbedtls_pk_rsa( ctx );
117 ctx.pk_info = NULL;
118
119 exit:
120 mbedtls_pk_free( &ctx );
121 return( status );
122 }
123 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
124 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
125 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
126 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
127 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
128 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
129
130 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
131 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
132
mbedtls_psa_rsa_import_key(const psa_key_attributes_t * attributes,const uint8_t * data,size_t data_length,uint8_t * key_buffer,size_t key_buffer_size,size_t * key_buffer_length,size_t * bits)133 psa_status_t mbedtls_psa_rsa_import_key(
134 const psa_key_attributes_t *attributes,
135 const uint8_t *data, size_t data_length,
136 uint8_t *key_buffer, size_t key_buffer_size,
137 size_t *key_buffer_length, size_t *bits )
138 {
139 psa_status_t status;
140 mbedtls_rsa_context *rsa = NULL;
141
142 /* Parse input */
143 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
144 data,
145 data_length,
146 &rsa );
147 if( status != PSA_SUCCESS )
148 goto exit;
149
150 *bits = (psa_key_bits_t) PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
151
152 /* Re-export the data to PSA export format, such that we can store export
153 * representation in the key slot. Export representation in case of RSA is
154 * the smallest representation that's allowed as input, so a straight-up
155 * allocation of the same size as the input buffer will be large enough. */
156 status = mbedtls_psa_rsa_export_key( attributes->core.type,
157 rsa,
158 key_buffer,
159 key_buffer_size,
160 key_buffer_length );
161 exit:
162 /* Always free the RSA object */
163 mbedtls_rsa_free( rsa );
164 mbedtls_free( rsa );
165
166 return( status );
167 }
168
mbedtls_psa_rsa_export_key(psa_key_type_t type,mbedtls_rsa_context * rsa,uint8_t * data,size_t data_size,size_t * data_length)169 psa_status_t mbedtls_psa_rsa_export_key( psa_key_type_t type,
170 mbedtls_rsa_context *rsa,
171 uint8_t *data,
172 size_t data_size,
173 size_t *data_length )
174 {
175 #if defined(MBEDTLS_PK_WRITE_C)
176 int ret;
177 mbedtls_pk_context pk;
178 uint8_t *pos = data + data_size;
179
180 mbedtls_pk_init( &pk );
181 pk.pk_info = &mbedtls_rsa_info;
182 pk.pk_ctx = rsa;
183
184 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
185 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
186 * private key and of the RFC3279 RSAPublicKey for a public key. */
187 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
188 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
189 else
190 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
191
192 if( ret < 0 )
193 {
194 /* Clean up in case pk_write failed halfway through. */
195 memset( data, 0, data_size );
196 return( mbedtls_to_psa_error( ret ) );
197 }
198
199 /* The mbedtls_pk_xxx functions write to the end of the buffer.
200 * Move the data to the beginning and erase remaining data
201 * at the original location. */
202 if( 2 * (size_t) ret <= data_size )
203 {
204 memcpy( data, data + data_size - ret, ret );
205 memset( data + data_size - ret, 0, ret );
206 }
207 else if( (size_t) ret < data_size )
208 {
209 memmove( data, data + data_size - ret, ret );
210 memset( data + ret, 0, data_size - ret );
211 }
212
213 *data_length = ret;
214 return( PSA_SUCCESS );
215 #else
216 (void) type;
217 (void) rsa;
218 (void) data;
219 (void) data_size;
220 (void) data_length;
221 return( PSA_ERROR_NOT_SUPPORTED );
222 #endif /* MBEDTLS_PK_WRITE_C */
223 }
224
mbedtls_psa_rsa_export_public_key(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,uint8_t * data,size_t data_size,size_t * data_length)225 psa_status_t mbedtls_psa_rsa_export_public_key(
226 const psa_key_attributes_t *attributes,
227 const uint8_t *key_buffer, size_t key_buffer_size,
228 uint8_t *data, size_t data_size, size_t *data_length )
229 {
230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
231 mbedtls_rsa_context *rsa = NULL;
232
233 status = mbedtls_psa_rsa_load_representation(
234 attributes->core.type, key_buffer, key_buffer_size, &rsa );
235 if( status != PSA_SUCCESS )
236 return( status );
237
238 status = mbedtls_psa_rsa_export_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
239 rsa,
240 data,
241 data_size,
242 data_length );
243
244 mbedtls_rsa_free( rsa );
245 mbedtls_free( rsa );
246
247 return( status );
248 }
249 #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
250 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
251
252 #if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) && \
253 defined(MBEDTLS_GENPRIME)
psa_rsa_read_exponent(const uint8_t * domain_parameters,size_t domain_parameters_size,int * exponent)254 static psa_status_t psa_rsa_read_exponent( const uint8_t *domain_parameters,
255 size_t domain_parameters_size,
256 int *exponent )
257 {
258 size_t i;
259 uint32_t acc = 0;
260
261 if( domain_parameters_size == 0 )
262 {
263 *exponent = 65537;
264 return( PSA_SUCCESS );
265 }
266
267 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
268 * support values that fit in a 32-bit integer, which is larger than
269 * int on just about every platform anyway. */
270 if( domain_parameters_size > sizeof( acc ) )
271 return( PSA_ERROR_NOT_SUPPORTED );
272 for( i = 0; i < domain_parameters_size; i++ )
273 acc = ( acc << 8 ) | domain_parameters[i];
274 if( acc > INT_MAX )
275 return( PSA_ERROR_NOT_SUPPORTED );
276 *exponent = acc;
277 return( PSA_SUCCESS );
278 }
279
mbedtls_psa_rsa_generate_key(const psa_key_attributes_t * attributes,uint8_t * key_buffer,size_t key_buffer_size,size_t * key_buffer_length)280 psa_status_t mbedtls_psa_rsa_generate_key(
281 const psa_key_attributes_t *attributes,
282 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
283 {
284 psa_status_t status;
285 mbedtls_rsa_context rsa;
286 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
287 int exponent;
288
289 status = psa_rsa_read_exponent( attributes->domain_parameters,
290 attributes->domain_parameters_size,
291 &exponent );
292 if( status != PSA_SUCCESS )
293 return( status );
294
295 mbedtls_rsa_init( &rsa );
296 ret = mbedtls_rsa_gen_key( &rsa,
297 mbedtls_psa_get_random,
298 MBEDTLS_PSA_RANDOM_STATE,
299 (unsigned int)attributes->core.bits,
300 exponent );
301 if( ret != 0 )
302 return( mbedtls_to_psa_error( ret ) );
303
304 status = mbedtls_psa_rsa_export_key( attributes->core.type,
305 &rsa, key_buffer, key_buffer_size,
306 key_buffer_length );
307 mbedtls_rsa_free( &rsa );
308
309 return( status );
310 }
311 #endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
312 * defined(MBEDTLS_GENPRIME) */
313
314 /****************************************************************/
315 /* Sign/verify hashes */
316 /****************************************************************/
317
318 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
319 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
320
321 /* Decode the hash algorithm from alg and store the mbedtls encoding in
322 * md_alg. Verify that the hash length is acceptable. */
psa_rsa_decode_md_type(psa_algorithm_t alg,size_t hash_length,mbedtls_md_type_t * md_alg)323 static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
324 size_t hash_length,
325 mbedtls_md_type_t *md_alg )
326 {
327 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
328 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
329 *md_alg = mbedtls_md_get_type( md_info );
330
331 /* The Mbed TLS RSA module uses an unsigned int for hash length
332 * parameters. Validate that it fits so that we don't risk an
333 * overflow later. */
334 #if SIZE_MAX > UINT_MAX
335 if( hash_length > UINT_MAX )
336 return( PSA_ERROR_INVALID_ARGUMENT );
337 #endif
338
339 /* For signatures using a hash, the hash length must be correct. */
340 if( alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
341 {
342 if( md_info == NULL )
343 return( PSA_ERROR_NOT_SUPPORTED );
344 if( mbedtls_md_get_size( md_info ) != hash_length )
345 return( PSA_ERROR_INVALID_ARGUMENT );
346 }
347
348 return( PSA_SUCCESS );
349 }
350
mbedtls_psa_rsa_sign_hash(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * hash,size_t hash_length,uint8_t * signature,size_t signature_size,size_t * signature_length)351 psa_status_t mbedtls_psa_rsa_sign_hash(
352 const psa_key_attributes_t *attributes,
353 const uint8_t *key_buffer, size_t key_buffer_size,
354 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
355 uint8_t *signature, size_t signature_size, size_t *signature_length )
356 {
357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
358 mbedtls_rsa_context *rsa = NULL;
359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
360 mbedtls_md_type_t md_alg;
361
362 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
363 key_buffer,
364 key_buffer_size,
365 &rsa );
366 if( status != PSA_SUCCESS )
367 return( status );
368
369 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
370 if( status != PSA_SUCCESS )
371 goto exit;
372
373 if( signature_size < mbedtls_rsa_get_len( rsa ) )
374 {
375 status = PSA_ERROR_BUFFER_TOO_SMALL;
376 goto exit;
377 }
378
379 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
380 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
381 {
382 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
383 MBEDTLS_MD_NONE );
384 if( ret == 0 )
385 {
386 ret = mbedtls_rsa_pkcs1_sign( rsa,
387 mbedtls_psa_get_random,
388 MBEDTLS_PSA_RANDOM_STATE,
389 md_alg,
390 (unsigned int) hash_length,
391 hash,
392 signature );
393 }
394 }
395 else
396 #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
397 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
398 if( PSA_ALG_IS_RSA_PSS( alg ) )
399 {
400 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
401
402 if( ret == 0 )
403 {
404 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
405 mbedtls_psa_get_random,
406 MBEDTLS_PSA_RANDOM_STATE,
407 MBEDTLS_MD_NONE,
408 (unsigned int) hash_length,
409 hash,
410 signature );
411 }
412 }
413 else
414 #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
415 {
416 status = PSA_ERROR_INVALID_ARGUMENT;
417 goto exit;
418 }
419
420 if( ret == 0 )
421 *signature_length = mbedtls_rsa_get_len( rsa );
422 status = mbedtls_to_psa_error( ret );
423
424 exit:
425 mbedtls_rsa_free( rsa );
426 mbedtls_free( rsa );
427
428 return( status );
429 }
430
431 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
rsa_pss_expected_salt_len(psa_algorithm_t alg,const mbedtls_rsa_context * rsa,size_t hash_length)432 static int rsa_pss_expected_salt_len( psa_algorithm_t alg,
433 const mbedtls_rsa_context *rsa,
434 size_t hash_length )
435 {
436 if( PSA_ALG_IS_RSA_PSS_ANY_SALT( alg ) )
437 return( MBEDTLS_RSA_SALT_LEN_ANY );
438 /* Otherwise: standard salt length, i.e. largest possible salt length
439 * up to the hash length. */
440 int klen = (int) mbedtls_rsa_get_len( rsa ); // known to fit
441 int hlen = (int) hash_length; // known to fit
442 int room = klen - 2 - hlen;
443 if( room < 0 )
444 return( 0 ); // there is no valid signature in this case anyway
445 else if( room > hlen )
446 return( hlen );
447 else
448 return( room );
449 }
450 #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
451
mbedtls_psa_rsa_verify_hash(const psa_key_attributes_t * attributes,const uint8_t * key_buffer,size_t key_buffer_size,psa_algorithm_t alg,const uint8_t * hash,size_t hash_length,const uint8_t * signature,size_t signature_length)452 psa_status_t mbedtls_psa_rsa_verify_hash(
453 const psa_key_attributes_t *attributes,
454 const uint8_t *key_buffer, size_t key_buffer_size,
455 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
456 const uint8_t *signature, size_t signature_length )
457 {
458 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
459 mbedtls_rsa_context *rsa = NULL;
460 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
461 mbedtls_md_type_t md_alg;
462
463 status = mbedtls_psa_rsa_load_representation( attributes->core.type,
464 key_buffer,
465 key_buffer_size,
466 &rsa );
467 if( status != PSA_SUCCESS )
468 goto exit;
469
470 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
471 if( status != PSA_SUCCESS )
472 goto exit;
473
474 if( signature_length != mbedtls_rsa_get_len( rsa ) )
475 {
476 status = PSA_ERROR_INVALID_SIGNATURE;
477 goto exit;
478 }
479
480 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
481 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
482 {
483 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
484 MBEDTLS_MD_NONE );
485 if( ret == 0 )
486 {
487 ret = mbedtls_rsa_pkcs1_verify( rsa,
488 md_alg,
489 (unsigned int) hash_length,
490 hash,
491 signature );
492 }
493 }
494 else
495 #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
496 #if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
497 if( PSA_ALG_IS_RSA_PSS( alg ) )
498 {
499 ret = mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
500 if( ret == 0 )
501 {
502 int slen = rsa_pss_expected_salt_len( alg, rsa, hash_length );
503 ret = mbedtls_rsa_rsassa_pss_verify_ext( rsa,
504 md_alg,
505 (unsigned) hash_length,
506 hash,
507 md_alg,
508 slen,
509 signature );
510 }
511 }
512 else
513 #endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
514 {
515 status = PSA_ERROR_INVALID_ARGUMENT;
516 goto exit;
517 }
518
519 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
520 * the rest of the signature is invalid". This has little use in
521 * practice and PSA doesn't report this distinction. */
522 status = ( ret == MBEDTLS_ERR_RSA_INVALID_PADDING ) ?
523 PSA_ERROR_INVALID_SIGNATURE :
524 mbedtls_to_psa_error( ret );
525
526 exit:
527 mbedtls_rsa_free( rsa );
528 mbedtls_free( rsa );
529
530 return( status );
531 }
532
533 #endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
534 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
535
536 #endif /* MBEDTLS_PSA_CRYPTO_C */
537