blob: 11c6fcdeb88997fe83afb13cd0badeb1bff39d8d [file] [log] [blame]
Gilles Peskine66e7b902021-02-12 23:40:58 +01001/** Code to exercise a PSA key object, i.e. validate that it seems well-formed
2 * and can do what it is supposed to do.
3 */
4
5/*
6 * Copyright The Mbed TLS Contributors
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22#include <test/helpers.h>
23#include <test/macros.h>
Gilles Peskinee78b0022021-02-13 00:41:11 +010024#include <test/psa_exercise_key.h>
Gilles Peskine66e7b902021-02-12 23:40:58 +010025
26#if defined(MBEDTLS_PSA_CRYPTO_C)
27
Gilles Peskinee78b0022021-02-13 00:41:11 +010028#include <mbedtls/asn1.h>
Gilles Peskine66e7b902021-02-12 23:40:58 +010029#include <psa/crypto.h>
30
Gilles Peskinee78b0022021-02-13 00:41:11 +010031#include <test/asn1_helpers.h>
32#include <test/psa_crypto_helpers.h>
33
34#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
35static int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
36{
37 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
38 PSA_KEY_LOCATION_LOCAL_STORAGE );
39}
40#endif
41
42static int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
43{
44 int ok = 0;
45 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
46 psa_key_lifetime_t lifetime;
47 mbedtls_svc_key_id_t id;
48 psa_key_type_t type;
49 psa_key_type_t bits;
50
51 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
52 lifetime = psa_get_key_lifetime( &attributes );
53 id = psa_get_key_id( &attributes );
54 type = psa_get_key_type( &attributes );
55 bits = psa_get_key_bits( &attributes );
56
57 /* Persistence */
58 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
59 {
60 TEST_ASSERT(
61 ( PSA_KEY_ID_VOLATILE_MIN <=
62 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
63 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
64 PSA_KEY_ID_VOLATILE_MAX ) );
65 }
66 else
67 {
68 TEST_ASSERT(
69 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
70 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
71 }
72#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
73 /* randomly-generated 64-bit constant, should never appear in test data */
74 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
75 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
76 if( lifetime_is_dynamic_secure_element( lifetime ) )
77 {
78 /* Mbed Crypto currently always exposes the slot number to
79 * applications. This is not mandated by the PSA specification
80 * and may change in future versions. */
81 TEST_EQUAL( status, 0 );
82 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
83 }
84 else
85 {
86 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
87 }
88#endif
89
90 /* Type and size */
91 TEST_ASSERT( type != 0 );
92 TEST_ASSERT( bits != 0 );
93 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
94 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
95 TEST_ASSERT( bits % 8 == 0 );
96
97 /* MAX macros concerning specific key types */
98 if( PSA_KEY_TYPE_IS_ECC( type ) )
99 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
100 else if( PSA_KEY_TYPE_IS_RSA( type ) )
101 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
102 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
103
104 ok = 1;
105
106exit:
107 /*
108 * Key attributes may have been returned by psa_get_key_attributes()
109 * thus reset them as required.
110 */
111 psa_reset_key_attributes( &attributes );
112
113 return( ok );
114}
115
116static int exercise_mac_key( mbedtls_svc_key_id_t key,
117 psa_key_usage_t usage,
118 psa_algorithm_t alg )
119{
120 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
121 const unsigned char input[] = "foo";
122 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
123 size_t mac_length = sizeof( mac );
124
125 if( usage & PSA_KEY_USAGE_SIGN_HASH )
126 {
127 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
128 PSA_ASSERT( psa_mac_update( &operation,
129 input, sizeof( input ) ) );
130 PSA_ASSERT( psa_mac_sign_finish( &operation,
131 mac, sizeof( mac ),
132 &mac_length ) );
133 }
134
135 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
136 {
137 psa_status_t verify_status =
138 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
139 PSA_SUCCESS :
140 PSA_ERROR_INVALID_SIGNATURE );
141 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
142 PSA_ASSERT( psa_mac_update( &operation,
143 input, sizeof( input ) ) );
144 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
145 verify_status );
146 }
147
148 return( 1 );
149
150exit:
151 psa_mac_abort( &operation );
152 return( 0 );
153}
154
155static int exercise_cipher_key( mbedtls_svc_key_id_t key,
156 psa_key_usage_t usage,
157 psa_algorithm_t alg )
158{
159 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
160 unsigned char iv[16] = {0};
161 size_t iv_length = sizeof( iv );
162 const unsigned char plaintext[16] = "Hello, world...";
163 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
164 size_t ciphertext_length = sizeof( ciphertext );
165 unsigned char decrypted[sizeof( ciphertext )];
166 size_t part_length;
167
168 if( usage & PSA_KEY_USAGE_ENCRYPT )
169 {
170 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
171 PSA_ASSERT( psa_cipher_generate_iv( &operation,
172 iv, sizeof( iv ),
173 &iv_length ) );
174 PSA_ASSERT( psa_cipher_update( &operation,
175 plaintext, sizeof( plaintext ),
176 ciphertext, sizeof( ciphertext ),
177 &ciphertext_length ) );
178 PSA_ASSERT( psa_cipher_finish( &operation,
179 ciphertext + ciphertext_length,
180 sizeof( ciphertext ) - ciphertext_length,
181 &part_length ) );
182 ciphertext_length += part_length;
183 }
184
185 if( usage & PSA_KEY_USAGE_DECRYPT )
186 {
187 psa_status_t status;
188 int maybe_invalid_padding = 0;
189 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
190 {
191 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
192 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
193 /* This should be PSA_CIPHER_GET_IV_SIZE but the API doesn't
194 * have this macro yet. */
195 iv_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH(
196 psa_get_key_type( &attributes ) );
197 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
198 psa_reset_key_attributes( &attributes );
199 }
200 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
201 PSA_ASSERT( psa_cipher_set_iv( &operation,
202 iv, iv_length ) );
203 PSA_ASSERT( psa_cipher_update( &operation,
204 ciphertext, ciphertext_length,
205 decrypted, sizeof( decrypted ),
206 &part_length ) );
207 status = psa_cipher_finish( &operation,
208 decrypted + part_length,
209 sizeof( decrypted ) - part_length,
210 &part_length );
211 /* For a stream cipher, all inputs are valid. For a block cipher,
212 * if the input is some aribtrary data rather than an actual
213 ciphertext, a padding error is likely. */
214 if( maybe_invalid_padding )
215 TEST_ASSERT( status == PSA_SUCCESS ||
216 status == PSA_ERROR_INVALID_PADDING );
217 else
218 PSA_ASSERT( status );
219 }
220
221 return( 1 );
222
223exit:
224 psa_cipher_abort( &operation );
225 return( 0 );
226}
227
228static int exercise_aead_key( mbedtls_svc_key_id_t key,
229 psa_key_usage_t usage,
230 psa_algorithm_t alg )
231{
232 unsigned char nonce[16] = {0};
233 size_t nonce_length = sizeof( nonce );
234 unsigned char plaintext[16] = "Hello, world...";
235 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
236 size_t ciphertext_length = sizeof( ciphertext );
237 size_t plaintext_length = sizeof( ciphertext );
238
239 /* Default IV length for AES-GCM is 12 bytes */
240 if( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ==
241 PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ) )
242 {
243 nonce_length = 12;
244 }
245
246 if( usage & PSA_KEY_USAGE_ENCRYPT )
247 {
248 PSA_ASSERT( psa_aead_encrypt( key, alg,
249 nonce, nonce_length,
250 NULL, 0,
251 plaintext, sizeof( plaintext ),
252 ciphertext, sizeof( ciphertext ),
253 &ciphertext_length ) );
254 }
255
256 if( usage & PSA_KEY_USAGE_DECRYPT )
257 {
258 psa_status_t verify_status =
259 ( usage & PSA_KEY_USAGE_ENCRYPT ?
260 PSA_SUCCESS :
261 PSA_ERROR_INVALID_SIGNATURE );
262 TEST_EQUAL( psa_aead_decrypt( key, alg,
263 nonce, nonce_length,
264 NULL, 0,
265 ciphertext, ciphertext_length,
266 plaintext, sizeof( plaintext ),
267 &plaintext_length ),
268 verify_status );
269 }
270
271 return( 1 );
272
273exit:
274 return( 0 );
275}
276
277static int exercise_signature_key( mbedtls_svc_key_id_t key,
278 psa_key_usage_t usage,
279 psa_algorithm_t alg )
280{
281 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
282 size_t payload_length = 16;
283 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
284 size_t signature_length = sizeof( signature );
285 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
286
287 /* If the policy allows signing with any hash, just pick one. */
288 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
289 {
290#if defined(KNOWN_SUPPORTED_HASH_ALG)
291 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
292 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
293#else
294 mbedtls_test_fail( "No hash algorithm for hash-and-sign testing",
295 __LINE__, __FILE__ );
296 return( 1 );
297#endif
298 }
299
300 if( usage & PSA_KEY_USAGE_SIGN_HASH )
301 {
302 /* Some algorithms require the payload to have the size of
303 * the hash encoded in the algorithm. Use this input size
304 * even for algorithms that allow other input sizes. */
305 if( hash_alg != 0 )
306 payload_length = PSA_HASH_LENGTH( hash_alg );
307 PSA_ASSERT( psa_sign_hash( key, alg,
308 payload, payload_length,
309 signature, sizeof( signature ),
310 &signature_length ) );
311 }
312
313 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
314 {
315 psa_status_t verify_status =
316 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
317 PSA_SUCCESS :
318 PSA_ERROR_INVALID_SIGNATURE );
319 TEST_EQUAL( psa_verify_hash( key, alg,
320 payload, payload_length,
321 signature, signature_length ),
322 verify_status );
323 }
324
325 return( 1 );
326
327exit:
328 return( 0 );
329}
330
331static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
332 psa_key_usage_t usage,
333 psa_algorithm_t alg )
334{
335 unsigned char plaintext[256] = "Hello, world...";
336 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
337 size_t ciphertext_length = sizeof( ciphertext );
338 size_t plaintext_length = 16;
339
340 if( usage & PSA_KEY_USAGE_ENCRYPT )
341 {
342 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
343 plaintext, plaintext_length,
344 NULL, 0,
345 ciphertext, sizeof( ciphertext ),
346 &ciphertext_length ) );
347 }
348
349 if( usage & PSA_KEY_USAGE_DECRYPT )
350 {
351 psa_status_t status =
352 psa_asymmetric_decrypt( key, alg,
353 ciphertext, ciphertext_length,
354 NULL, 0,
355 plaintext, sizeof( plaintext ),
356 &plaintext_length );
357 TEST_ASSERT( status == PSA_SUCCESS ||
358 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
359 ( status == PSA_ERROR_INVALID_ARGUMENT ||
360 status == PSA_ERROR_INVALID_PADDING ) ) );
361 }
362
363 return( 1 );
364
365exit:
366 return( 0 );
367}
368
369int mbedtls_test_psa_setup_key_derivation_wrap(
370 psa_key_derivation_operation_t* operation,
371 mbedtls_svc_key_id_t key,
372 psa_algorithm_t alg,
373 unsigned char* input1, size_t input1_length,
374 unsigned char* input2, size_t input2_length,
375 size_t capacity )
376{
377 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
378 if( PSA_ALG_IS_HKDF( alg ) )
379 {
380 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
381 PSA_KEY_DERIVATION_INPUT_SALT,
382 input1, input1_length ) );
383 PSA_ASSERT( psa_key_derivation_input_key( operation,
384 PSA_KEY_DERIVATION_INPUT_SECRET,
385 key ) );
386 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
387 PSA_KEY_DERIVATION_INPUT_INFO,
388 input2,
389 input2_length ) );
390 }
391 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
392 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
393 {
394 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
395 PSA_KEY_DERIVATION_INPUT_SEED,
396 input1, input1_length ) );
397 PSA_ASSERT( psa_key_derivation_input_key( operation,
398 PSA_KEY_DERIVATION_INPUT_SECRET,
399 key ) );
400 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
401 PSA_KEY_DERIVATION_INPUT_LABEL,
402 input2, input2_length ) );
403 }
404 else
405 {
406 TEST_ASSERT( ! "Key derivation algorithm not supported" );
407 }
408
409 if( capacity != SIZE_MAX )
410 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
411
412 return( 1 );
413
414exit:
415 return( 0 );
416}
417
418
419static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
420 psa_key_usage_t usage,
421 psa_algorithm_t alg )
422{
423 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
424 unsigned char input1[] = "Input 1";
425 size_t input1_length = sizeof( input1 );
426 unsigned char input2[] = "Input 2";
427 size_t input2_length = sizeof( input2 );
428 unsigned char output[1];
429 size_t capacity = sizeof( output );
430
431 if( usage & PSA_KEY_USAGE_DERIVE )
432 {
433 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
434 input1, input1_length,
435 input2, input2_length,
436 capacity ) )
437 goto exit;
438
439 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
440 output,
441 capacity ) );
442 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
443 }
444
445 return( 1 );
446
447exit:
448 return( 0 );
449}
450
451/* We need two keys to exercise key agreement. Exercise the
452 * private key against its own public key. */
453psa_status_t mbedtls_test_psa_key_agreement_with_self(
454 psa_key_derivation_operation_t *operation,
455 mbedtls_svc_key_id_t key )
456{
457 psa_key_type_t private_key_type;
458 psa_key_type_t public_key_type;
459 size_t key_bits;
460 uint8_t *public_key = NULL;
461 size_t public_key_length;
462 /* Return GENERIC_ERROR if something other than the final call to
463 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
464 * but it's good enough: callers will report it as a failed test anyway. */
465 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
466 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
467
468 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
469 private_key_type = psa_get_key_type( &attributes );
470 key_bits = psa_get_key_bits( &attributes );
471 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
472 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
473 ASSERT_ALLOC( public_key, public_key_length );
474 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
475 &public_key_length ) );
476
477 status = psa_key_derivation_key_agreement(
478 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
479 public_key, public_key_length );
480exit:
481 /*
482 * Key attributes may have been returned by psa_get_key_attributes()
483 * thus reset them as required.
484 */
485 psa_reset_key_attributes( &attributes );
486
487 mbedtls_free( public_key );
488 return( status );
489}
490
491/* We need two keys to exercise key agreement. Exercise the
492 * private key against its own public key. */
493psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
494 psa_algorithm_t alg,
495 mbedtls_svc_key_id_t key )
496{
497 psa_key_type_t private_key_type;
498 psa_key_type_t public_key_type;
499 size_t key_bits;
500 uint8_t *public_key = NULL;
501 size_t public_key_length;
502 uint8_t output[1024];
503 size_t output_length;
504 /* Return GENERIC_ERROR if something other than the final call to
505 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
506 * but it's good enough: callers will report it as a failed test anyway. */
507 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
508 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
509
510 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
511 private_key_type = psa_get_key_type( &attributes );
512 key_bits = psa_get_key_bits( &attributes );
513 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
514 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
515 ASSERT_ALLOC( public_key, public_key_length );
516 PSA_ASSERT( psa_export_public_key( key,
517 public_key, public_key_length,
518 &public_key_length ) );
519
520 status = psa_raw_key_agreement( alg, key,
521 public_key, public_key_length,
522 output, sizeof( output ), &output_length );
523exit:
524 /*
525 * Key attributes may have been returned by psa_get_key_attributes()
526 * thus reset them as required.
527 */
528 psa_reset_key_attributes( &attributes );
529
530 mbedtls_free( public_key );
531 return( status );
532}
533
534static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
535 psa_key_usage_t usage,
536 psa_algorithm_t alg )
537{
538 int ok = 0;
539
540 if( usage & PSA_KEY_USAGE_DERIVE )
541 {
542 /* We need two keys to exercise key agreement. Exercise the
543 * private key against its own public key. */
544 PSA_ASSERT( mbedtls_test_psa_raw_key_agreement_with_self( alg, key ) );
545 }
546 ok = 1;
547
548exit:
549 return( ok );
550}
551
552static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
553 psa_key_usage_t usage,
554 psa_algorithm_t alg )
555{
556 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
557 unsigned char output[1];
558 int ok = 0;
559
560 if( usage & PSA_KEY_USAGE_DERIVE )
561 {
562 /* We need two keys to exercise key agreement. Exercise the
563 * private key against its own public key. */
564 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
565 PSA_ASSERT( mbedtls_test_psa_key_agreement_with_self( &operation, key ) );
566 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
567 output,
568 sizeof( output ) ) );
569 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
570 }
571 ok = 1;
572
573exit:
574 return( ok );
575}
576
577int mbedtls_test_psa_exported_key_sanity_check(
578 psa_key_type_t type, size_t bits,
579 uint8_t *exported, size_t exported_length )
580{
581 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
582 TEST_EQUAL( exported_length, ( bits + 7 ) / 8 );
583 else
584 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
585
586#if defined(MBEDTLS_DES_C)
587 if( type == PSA_KEY_TYPE_DES )
588 {
589 /* Check the parity bits. */
590 unsigned i;
591 for( i = 0; i < bits / 8; i++ )
592 {
593 unsigned bit_count = 0;
594 unsigned m;
595 for( m = 1; m <= 0x100; m <<= 1 )
596 {
597 if( exported[i] & m )
598 ++bit_count;
599 }
600 TEST_ASSERT( bit_count % 2 != 0 );
601 }
602 }
603 else
604#endif
605
606#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
607 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
608 {
609 uint8_t *p = exported;
610 uint8_t *end = exported + exported_length;
611 size_t len;
612 /* RSAPrivateKey ::= SEQUENCE {
613 * version INTEGER, -- must be 0
614 * modulus INTEGER, -- n
615 * publicExponent INTEGER, -- e
616 * privateExponent INTEGER, -- d
617 * prime1 INTEGER, -- p
618 * prime2 INTEGER, -- q
619 * exponent1 INTEGER, -- d mod (p-1)
620 * exponent2 INTEGER, -- d mod (q-1)
621 * coefficient INTEGER, -- (inverse of q) mod p
622 * }
623 */
624 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
625 MBEDTLS_ASN1_SEQUENCE |
626 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
627 TEST_EQUAL( p + len, end );
628 if( ! mbedtls_test_asn1_skip_integer( &p, end, 0, 0, 0 ) )
629 goto exit;
630 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
631 goto exit;
632 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
633 goto exit;
634 /* Require d to be at least half the size of n. */
635 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
636 goto exit;
637 /* Require p and q to be at most half the size of n, rounded up. */
638 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
639 goto exit;
640 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
641 goto exit;
642 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
643 goto exit;
644 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
645 goto exit;
646 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
647 goto exit;
648 TEST_EQUAL( p, end );
649 }
650 else
651#endif /* MBEDTLS_RSA_C */
652
653#if defined(MBEDTLS_ECP_C)
654 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
655 {
656 /* Just the secret value */
657 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
658 }
659 else
660#endif /* MBEDTLS_ECP_C */
661
Gilles Peskinead557e52021-02-14 01:19:21 +0100662#if defined(MBEDTLS_RSA_C)
663 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100664 {
665 uint8_t *p = exported;
666 uint8_t *end = exported + exported_length;
Gilles Peskinead557e52021-02-14 01:19:21 +0100667 size_t len;
668 /* RSAPublicKey ::= SEQUENCE {
669 * modulus INTEGER, -- n
670 * publicExponent INTEGER } -- e
671 */
672 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
673 MBEDTLS_ASN1_SEQUENCE |
674 MBEDTLS_ASN1_CONSTRUCTED ),
675 0 );
676 TEST_EQUAL( p + len, end );
677 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
678 goto exit;
679 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
680 goto exit;
681 TEST_EQUAL( p, end );
682 }
683 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100684#endif /* MBEDTLS_RSA_C */
Gilles Peskinead557e52021-02-14 01:19:21 +0100685
Gilles Peskinee78b0022021-02-13 00:41:11 +0100686#if defined(MBEDTLS_ECP_C)
Gilles Peskinead557e52021-02-14 01:19:21 +0100687 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
688 {
689 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100690 {
Gilles Peskinead557e52021-02-14 01:19:21 +0100691 /* The representation of an ECC Montgomery public key is
692 * the raw compressed point */
693 TEST_EQUAL( PSA_BITS_TO_BYTES( bits ), exported_length );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100694 }
695 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100696 {
Gilles Peskinead557e52021-02-14 01:19:21 +0100697 /* The representation of an ECC Weierstrass public key is:
698 * - The byte 0x04;
699 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
700 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
701 * - where m is the bit size associated with the curve.
702 */
703 TEST_EQUAL( 1 + 2 * PSA_BITS_TO_BYTES( bits ), exported_length );
704 TEST_EQUAL( exported[0], 4 );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100705 }
706 }
707 else
Gilles Peskinead557e52021-02-14 01:19:21 +0100708#endif /* MBEDTLS_ECP_C */
709
710 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
711 {
712 char message[47];
713 mbedtls_snprintf( message, sizeof( message ),
714 "No sanity check for public key type=0x%08lx",
715 (unsigned long) type );
716 mbedtls_test_fail( message, __LINE__, __FILE__ );
717 return( 0 );
718 }
719 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100720
721 {
722 /* No sanity checks for other types */
723 }
724
725 return( 1 );
726
727exit:
728 return( 0 );
729}
730
731static int exercise_export_key( mbedtls_svc_key_id_t key,
732 psa_key_usage_t usage )
733{
734 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
735 uint8_t *exported = NULL;
736 size_t exported_size = 0;
737 size_t exported_length = 0;
738 int ok = 0;
739
740 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
741
742 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
743 psa_get_key_type( &attributes ),
744 psa_get_key_bits( &attributes ) );
745 ASSERT_ALLOC( exported, exported_size );
746
747 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
748 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
749 {
750 TEST_EQUAL( psa_export_key( key, exported,
751 exported_size, &exported_length ),
752 PSA_ERROR_NOT_PERMITTED );
753 ok = 1;
754 goto exit;
755 }
756
757 PSA_ASSERT( psa_export_key( key,
758 exported, exported_size,
759 &exported_length ) );
760 ok = mbedtls_test_psa_exported_key_sanity_check(
761 psa_get_key_type( &attributes ), psa_get_key_bits( &attributes ),
762 exported, exported_length );
763
764exit:
765 /*
766 * Key attributes may have been returned by psa_get_key_attributes()
767 * thus reset them as required.
768 */
769 psa_reset_key_attributes( &attributes );
770
771 mbedtls_free( exported );
772 return( ok );
773}
774
775static int exercise_export_public_key( mbedtls_svc_key_id_t key )
776{
777 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
778 psa_key_type_t public_type;
779 uint8_t *exported = NULL;
780 size_t exported_size = 0;
781 size_t exported_length = 0;
782 int ok = 0;
783
784 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
785 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
786 {
787 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
788 psa_get_key_type( &attributes ),
789 psa_get_key_bits( &attributes ) );
790 ASSERT_ALLOC( exported, exported_size );
791
792 TEST_EQUAL( psa_export_public_key( key, exported,
793 exported_size, &exported_length ),
794 PSA_ERROR_INVALID_ARGUMENT );
795 ok = 1;
796 goto exit;
797 }
798
799 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
800 psa_get_key_type( &attributes ) );
801 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
802 psa_get_key_bits( &attributes ) );
803 ASSERT_ALLOC( exported, exported_size );
804
805 PSA_ASSERT( psa_export_public_key( key,
806 exported, exported_size,
807 &exported_length ) );
808 ok = mbedtls_test_psa_exported_key_sanity_check(
809 public_type, psa_get_key_bits( &attributes ),
810 exported, exported_length );
811
812exit:
813 /*
814 * Key attributes may have been returned by psa_get_key_attributes()
815 * thus reset them as required.
816 */
817 psa_reset_key_attributes( &attributes );
818
819 mbedtls_free( exported );
820 return( ok );
821}
822
823int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
824 psa_key_usage_t usage,
825 psa_algorithm_t alg )
826{
827 int ok;
828
829 if( ! check_key_attributes_sanity( key ) )
830 return( 0 );
831
832 if( alg == 0 )
833 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
834 else if( PSA_ALG_IS_MAC( alg ) )
835 ok = exercise_mac_key( key, usage, alg );
836 else if( PSA_ALG_IS_CIPHER( alg ) )
837 ok = exercise_cipher_key( key, usage, alg );
838 else if( PSA_ALG_IS_AEAD( alg ) )
839 ok = exercise_aead_key( key, usage, alg );
840 else if( PSA_ALG_IS_SIGN( alg ) )
841 ok = exercise_signature_key( key, usage, alg );
842 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
843 ok = exercise_asymmetric_encryption_key( key, usage, alg );
844 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
845 ok = exercise_key_derivation_key( key, usage, alg );
846 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
847 ok = exercise_raw_key_agreement_key( key, usage, alg );
848 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
849 ok = exercise_key_agreement_key( key, usage, alg );
850 else
851 {
852 char message[40];
853 mbedtls_snprintf( message, sizeof( message ),
854 "No code to exercise alg=0x%08lx",
855 (unsigned long) alg );
856 mbedtls_test_fail( message, __LINE__, __FILE__ );
857 ok = 0;
858 }
859
860 ok = ok && exercise_export_key( key, usage );
861 ok = ok && exercise_export_public_key( key );
862
863 return( ok );
864}
865
866psa_key_usage_t mbedtls_test_psa_usage_to_exercise( psa_key_type_t type,
867 psa_algorithm_t alg )
868{
869 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
870 {
871 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
872 PSA_KEY_USAGE_VERIFY_HASH :
873 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
874 }
875 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
876 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
877 {
878 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
879 PSA_KEY_USAGE_ENCRYPT :
880 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
881 }
882 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
883 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
884 {
885 return( PSA_KEY_USAGE_DERIVE );
886 }
887 else
888 {
889 return( 0 );
890 }
891
892}
Gilles Peskine66e7b902021-02-12 23:40:58 +0100893
894#endif /* MBEDTLS_PSA_CRYPTO_C */