blob: 869da7df68205f9867cca6e6503974ff43facc8b [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;
Gilles Peskine6b362e62021-02-15 12:03:16 +010049 size_t bits;
Gilles Peskinee78b0022021-02-13 00:41:11 +010050
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
Steven Cooremanaaec3412021-02-18 13:30:34 +0100246 /* IV length for CCM needs to be between 7 and 13 bytes */
247 if( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) ==
248 PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ) )
249 {
250 nonce_length = 12;
251 }
252
Gilles Peskinee78b0022021-02-13 00:41:11 +0100253 if( usage & PSA_KEY_USAGE_ENCRYPT )
254 {
255 PSA_ASSERT( psa_aead_encrypt( key, alg,
256 nonce, nonce_length,
257 NULL, 0,
258 plaintext, sizeof( plaintext ),
259 ciphertext, sizeof( ciphertext ),
260 &ciphertext_length ) );
261 }
262
263 if( usage & PSA_KEY_USAGE_DECRYPT )
264 {
265 psa_status_t verify_status =
266 ( usage & PSA_KEY_USAGE_ENCRYPT ?
267 PSA_SUCCESS :
268 PSA_ERROR_INVALID_SIGNATURE );
269 TEST_EQUAL( psa_aead_decrypt( key, alg,
270 nonce, nonce_length,
271 NULL, 0,
272 ciphertext, ciphertext_length,
273 plaintext, sizeof( plaintext ),
274 &plaintext_length ),
275 verify_status );
276 }
277
278 return( 1 );
279
280exit:
281 return( 0 );
282}
283
284static int exercise_signature_key( mbedtls_svc_key_id_t key,
285 psa_key_usage_t usage,
286 psa_algorithm_t alg )
287{
288 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
289 size_t payload_length = 16;
290 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
291 size_t signature_length = sizeof( signature );
292 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
293
294 /* If the policy allows signing with any hash, just pick one. */
295 if( PSA_ALG_IS_HASH_AND_SIGN( alg ) && hash_alg == PSA_ALG_ANY_HASH )
296 {
297#if defined(KNOWN_SUPPORTED_HASH_ALG)
298 hash_alg = KNOWN_SUPPORTED_HASH_ALG;
299 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
300#else
Gilles Peskine2385f712021-02-14 01:34:21 +0100301 TEST_ASSERT( ! "No hash algorithm for hash-and-sign testing" );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100302#endif
303 }
304
305 if( usage & PSA_KEY_USAGE_SIGN_HASH )
306 {
307 /* Some algorithms require the payload to have the size of
308 * the hash encoded in the algorithm. Use this input size
309 * even for algorithms that allow other input sizes. */
310 if( hash_alg != 0 )
311 payload_length = PSA_HASH_LENGTH( hash_alg );
312 PSA_ASSERT( psa_sign_hash( key, alg,
313 payload, payload_length,
314 signature, sizeof( signature ),
315 &signature_length ) );
316 }
317
318 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
319 {
320 psa_status_t verify_status =
321 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
322 PSA_SUCCESS :
323 PSA_ERROR_INVALID_SIGNATURE );
324 TEST_EQUAL( psa_verify_hash( key, alg,
325 payload, payload_length,
326 signature, signature_length ),
327 verify_status );
328 }
329
330 return( 1 );
331
332exit:
333 return( 0 );
334}
335
336static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
337 psa_key_usage_t usage,
338 psa_algorithm_t alg )
339{
340 unsigned char plaintext[256] = "Hello, world...";
341 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
342 size_t ciphertext_length = sizeof( ciphertext );
343 size_t plaintext_length = 16;
344
345 if( usage & PSA_KEY_USAGE_ENCRYPT )
346 {
347 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
348 plaintext, plaintext_length,
349 NULL, 0,
350 ciphertext, sizeof( ciphertext ),
351 &ciphertext_length ) );
352 }
353
354 if( usage & PSA_KEY_USAGE_DECRYPT )
355 {
356 psa_status_t status =
357 psa_asymmetric_decrypt( key, alg,
358 ciphertext, ciphertext_length,
359 NULL, 0,
360 plaintext, sizeof( plaintext ),
361 &plaintext_length );
362 TEST_ASSERT( status == PSA_SUCCESS ||
363 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
364 ( status == PSA_ERROR_INVALID_ARGUMENT ||
365 status == PSA_ERROR_INVALID_PADDING ) ) );
366 }
367
368 return( 1 );
369
370exit:
371 return( 0 );
372}
373
374int mbedtls_test_psa_setup_key_derivation_wrap(
375 psa_key_derivation_operation_t* operation,
376 mbedtls_svc_key_id_t key,
377 psa_algorithm_t alg,
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100378 const unsigned char* input1, size_t input1_length,
379 const unsigned char* input2, size_t input2_length,
Gilles Peskinee78b0022021-02-13 00:41:11 +0100380 size_t capacity )
381{
382 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
383 if( PSA_ALG_IS_HKDF( alg ) )
384 {
385 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
386 PSA_KEY_DERIVATION_INPUT_SALT,
387 input1, input1_length ) );
388 PSA_ASSERT( psa_key_derivation_input_key( operation,
389 PSA_KEY_DERIVATION_INPUT_SECRET,
390 key ) );
391 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
392 PSA_KEY_DERIVATION_INPUT_INFO,
393 input2,
394 input2_length ) );
395 }
396 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
397 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
398 {
399 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
400 PSA_KEY_DERIVATION_INPUT_SEED,
401 input1, input1_length ) );
402 PSA_ASSERT( psa_key_derivation_input_key( operation,
403 PSA_KEY_DERIVATION_INPUT_SECRET,
404 key ) );
405 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
406 PSA_KEY_DERIVATION_INPUT_LABEL,
407 input2, input2_length ) );
408 }
409 else
410 {
411 TEST_ASSERT( ! "Key derivation algorithm not supported" );
412 }
413
414 if( capacity != SIZE_MAX )
415 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
416
417 return( 1 );
418
419exit:
420 return( 0 );
421}
422
423
424static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
425 psa_key_usage_t usage,
426 psa_algorithm_t alg )
427{
428 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
429 unsigned char input1[] = "Input 1";
430 size_t input1_length = sizeof( input1 );
431 unsigned char input2[] = "Input 2";
432 size_t input2_length = sizeof( input2 );
433 unsigned char output[1];
434 size_t capacity = sizeof( output );
435
436 if( usage & PSA_KEY_USAGE_DERIVE )
437 {
438 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
439 input1, input1_length,
440 input2, input2_length,
441 capacity ) )
442 goto exit;
443
444 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
445 output,
446 capacity ) );
447 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
448 }
449
450 return( 1 );
451
452exit:
453 return( 0 );
454}
455
456/* We need two keys to exercise key agreement. Exercise the
457 * private key against its own public key. */
458psa_status_t mbedtls_test_psa_key_agreement_with_self(
459 psa_key_derivation_operation_t *operation,
460 mbedtls_svc_key_id_t key )
461{
462 psa_key_type_t private_key_type;
463 psa_key_type_t public_key_type;
464 size_t key_bits;
465 uint8_t *public_key = NULL;
466 size_t public_key_length;
467 /* Return GENERIC_ERROR if something other than the final call to
468 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
469 * but it's good enough: callers will report it as a failed test anyway. */
470 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
471 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
472
473 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
474 private_key_type = psa_get_key_type( &attributes );
475 key_bits = psa_get_key_bits( &attributes );
476 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
477 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
478 ASSERT_ALLOC( public_key, public_key_length );
479 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
480 &public_key_length ) );
481
482 status = psa_key_derivation_key_agreement(
483 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
484 public_key, public_key_length );
485exit:
486 /*
487 * Key attributes may have been returned by psa_get_key_attributes()
488 * thus reset them as required.
489 */
490 psa_reset_key_attributes( &attributes );
491
492 mbedtls_free( public_key );
493 return( status );
494}
495
496/* We need two keys to exercise key agreement. Exercise the
497 * private key against its own public key. */
498psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
499 psa_algorithm_t alg,
500 mbedtls_svc_key_id_t key )
501{
502 psa_key_type_t private_key_type;
503 psa_key_type_t public_key_type;
504 size_t key_bits;
505 uint8_t *public_key = NULL;
506 size_t public_key_length;
507 uint8_t output[1024];
508 size_t output_length;
509 /* Return GENERIC_ERROR if something other than the final call to
510 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
511 * but it's good enough: callers will report it as a failed test anyway. */
512 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
513 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
514
515 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
516 private_key_type = psa_get_key_type( &attributes );
517 key_bits = psa_get_key_bits( &attributes );
518 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
519 public_key_length = PSA_EXPORT_KEY_OUTPUT_SIZE( public_key_type, key_bits );
520 ASSERT_ALLOC( public_key, public_key_length );
521 PSA_ASSERT( psa_export_public_key( key,
522 public_key, public_key_length,
523 &public_key_length ) );
524
525 status = psa_raw_key_agreement( alg, key,
526 public_key, public_key_length,
527 output, sizeof( output ), &output_length );
528exit:
529 /*
530 * Key attributes may have been returned by psa_get_key_attributes()
531 * thus reset them as required.
532 */
533 psa_reset_key_attributes( &attributes );
534
535 mbedtls_free( public_key );
536 return( status );
537}
538
539static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
540 psa_key_usage_t usage,
541 psa_algorithm_t alg )
542{
543 int ok = 0;
544
545 if( usage & PSA_KEY_USAGE_DERIVE )
546 {
547 /* We need two keys to exercise key agreement. Exercise the
548 * private key against its own public key. */
549 PSA_ASSERT( mbedtls_test_psa_raw_key_agreement_with_self( alg, key ) );
550 }
551 ok = 1;
552
553exit:
554 return( ok );
555}
556
557static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
558 psa_key_usage_t usage,
559 psa_algorithm_t alg )
560{
561 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
562 unsigned char output[1];
563 int ok = 0;
564
565 if( usage & PSA_KEY_USAGE_DERIVE )
566 {
567 /* We need two keys to exercise key agreement. Exercise the
568 * private key against its own public key. */
569 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
570 PSA_ASSERT( mbedtls_test_psa_key_agreement_with_self( &operation, key ) );
571 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
572 output,
573 sizeof( output ) ) );
574 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
575 }
576 ok = 1;
577
578exit:
579 return( ok );
580}
581
582int mbedtls_test_psa_exported_key_sanity_check(
583 psa_key_type_t type, size_t bits,
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100584 const uint8_t *exported, size_t exported_length )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100585{
Gilles Peskinecc9db302021-02-14 01:29:52 +0100586 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100587
Gilles Peskinecc9db302021-02-14 01:29:52 +0100588 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
589 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100590 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100591
592#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
593 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
594 {
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100595 uint8_t *p = (uint8_t*) exported;
596 const uint8_t *end = exported + exported_length;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100597 size_t len;
598 /* RSAPrivateKey ::= SEQUENCE {
599 * version INTEGER, -- must be 0
600 * modulus INTEGER, -- n
601 * publicExponent INTEGER, -- e
602 * privateExponent INTEGER, -- d
603 * prime1 INTEGER, -- p
604 * prime2 INTEGER, -- q
605 * exponent1 INTEGER, -- d mod (p-1)
606 * exponent2 INTEGER, -- d mod (q-1)
607 * coefficient INTEGER, -- (inverse of q) mod p
608 * }
609 */
610 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
611 MBEDTLS_ASN1_SEQUENCE |
612 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
613 TEST_EQUAL( p + len, end );
614 if( ! mbedtls_test_asn1_skip_integer( &p, end, 0, 0, 0 ) )
615 goto exit;
616 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
617 goto exit;
618 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
619 goto exit;
620 /* Require d to be at least half the size of n. */
621 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
622 goto exit;
623 /* Require p and q to be at most half the size of n, rounded up. */
624 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
625 goto exit;
626 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
627 goto exit;
628 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
629 goto exit;
630 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
631 goto exit;
632 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
633 goto exit;
634 TEST_EQUAL( p, end );
635 }
636 else
637#endif /* MBEDTLS_RSA_C */
638
639#if defined(MBEDTLS_ECP_C)
640 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
641 {
642 /* Just the secret value */
643 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
644 }
645 else
646#endif /* MBEDTLS_ECP_C */
647
Gilles Peskinead557e52021-02-14 01:19:21 +0100648#if defined(MBEDTLS_RSA_C)
649 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100650 {
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100651 uint8_t *p = (uint8_t*) exported;
652 const uint8_t *end = exported + exported_length;
Gilles Peskinead557e52021-02-14 01:19:21 +0100653 size_t len;
654 /* RSAPublicKey ::= SEQUENCE {
655 * modulus INTEGER, -- n
656 * publicExponent INTEGER } -- e
657 */
658 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
659 MBEDTLS_ASN1_SEQUENCE |
660 MBEDTLS_ASN1_CONSTRUCTED ),
661 0 );
662 TEST_EQUAL( p + len, end );
663 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
664 goto exit;
665 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
666 goto exit;
667 TEST_EQUAL( p, end );
668 }
669 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100670#endif /* MBEDTLS_RSA_C */
Gilles Peskinead557e52021-02-14 01:19:21 +0100671
Gilles Peskinee78b0022021-02-13 00:41:11 +0100672#if defined(MBEDTLS_ECP_C)
Gilles Peskinead557e52021-02-14 01:19:21 +0100673 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
674 {
675 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100676 {
Gilles Peskinead557e52021-02-14 01:19:21 +0100677 /* The representation of an ECC Montgomery public key is
678 * the raw compressed point */
679 TEST_EQUAL( PSA_BITS_TO_BYTES( bits ), exported_length );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100680 }
681 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100682 {
Gilles Peskinead557e52021-02-14 01:19:21 +0100683 /* The representation of an ECC Weierstrass public key is:
684 * - The byte 0x04;
685 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
686 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
687 * - where m is the bit size associated with the curve.
688 */
689 TEST_EQUAL( 1 + 2 * PSA_BITS_TO_BYTES( bits ), exported_length );
690 TEST_EQUAL( exported[0], 4 );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100691 }
692 }
693 else
Gilles Peskinead557e52021-02-14 01:19:21 +0100694#endif /* MBEDTLS_ECP_C */
695
Gilles Peskinead557e52021-02-14 01:19:21 +0100696 {
Gilles Peskinecc9db302021-02-14 01:29:52 +0100697 TEST_ASSERT( ! "Sanity check not implemented for this key type" );
Gilles Peskinead557e52021-02-14 01:19:21 +0100698 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100699
Gilles Peskinecc9db302021-02-14 01:29:52 +0100700#if defined(MBEDTLS_DES_C)
701 if( type == PSA_KEY_TYPE_DES )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100702 {
Gilles Peskinecc9db302021-02-14 01:29:52 +0100703 /* Check the parity bits. */
704 unsigned i;
705 for( i = 0; i < bits / 8; i++ )
706 {
707 unsigned bit_count = 0;
708 unsigned m;
709 for( m = 1; m <= 0x100; m <<= 1 )
710 {
711 if( exported[i] & m )
712 ++bit_count;
713 }
714 TEST_ASSERT( bit_count % 2 != 0 );
715 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100716 }
Gilles Peskinecc9db302021-02-14 01:29:52 +0100717#endif
Gilles Peskinee78b0022021-02-13 00:41:11 +0100718
719 return( 1 );
720
721exit:
722 return( 0 );
723}
724
725static int exercise_export_key( mbedtls_svc_key_id_t key,
726 psa_key_usage_t usage )
727{
728 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
729 uint8_t *exported = NULL;
730 size_t exported_size = 0;
731 size_t exported_length = 0;
732 int ok = 0;
733
734 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
735
736 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
737 psa_get_key_type( &attributes ),
738 psa_get_key_bits( &attributes ) );
739 ASSERT_ALLOC( exported, exported_size );
740
741 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
742 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
743 {
744 TEST_EQUAL( psa_export_key( key, exported,
745 exported_size, &exported_length ),
746 PSA_ERROR_NOT_PERMITTED );
747 ok = 1;
748 goto exit;
749 }
750
751 PSA_ASSERT( psa_export_key( key,
752 exported, exported_size,
753 &exported_length ) );
754 ok = mbedtls_test_psa_exported_key_sanity_check(
755 psa_get_key_type( &attributes ), psa_get_key_bits( &attributes ),
756 exported, exported_length );
757
758exit:
759 /*
760 * Key attributes may have been returned by psa_get_key_attributes()
761 * thus reset them as required.
762 */
763 psa_reset_key_attributes( &attributes );
764
765 mbedtls_free( exported );
766 return( ok );
767}
768
769static int exercise_export_public_key( mbedtls_svc_key_id_t key )
770{
771 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
772 psa_key_type_t public_type;
773 uint8_t *exported = NULL;
774 size_t exported_size = 0;
775 size_t exported_length = 0;
776 int ok = 0;
777
778 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
779 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
780 {
781 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
782 psa_get_key_type( &attributes ),
783 psa_get_key_bits( &attributes ) );
784 ASSERT_ALLOC( exported, exported_size );
785
786 TEST_EQUAL( psa_export_public_key( key, exported,
787 exported_size, &exported_length ),
788 PSA_ERROR_INVALID_ARGUMENT );
789 ok = 1;
790 goto exit;
791 }
792
793 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
794 psa_get_key_type( &attributes ) );
795 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE( public_type,
796 psa_get_key_bits( &attributes ) );
797 ASSERT_ALLOC( exported, exported_size );
798
799 PSA_ASSERT( psa_export_public_key( key,
800 exported, exported_size,
801 &exported_length ) );
802 ok = mbedtls_test_psa_exported_key_sanity_check(
803 public_type, psa_get_key_bits( &attributes ),
804 exported, exported_length );
805
806exit:
807 /*
808 * Key attributes may have been returned by psa_get_key_attributes()
809 * thus reset them as required.
810 */
811 psa_reset_key_attributes( &attributes );
812
813 mbedtls_free( exported );
814 return( ok );
815}
816
817int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
818 psa_key_usage_t usage,
819 psa_algorithm_t alg )
820{
Gilles Peskine2385f712021-02-14 01:34:21 +0100821 int ok = 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100822
823 if( ! check_key_attributes_sanity( key ) )
824 return( 0 );
825
826 if( alg == 0 )
827 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
828 else if( PSA_ALG_IS_MAC( alg ) )
829 ok = exercise_mac_key( key, usage, alg );
830 else if( PSA_ALG_IS_CIPHER( alg ) )
831 ok = exercise_cipher_key( key, usage, alg );
832 else if( PSA_ALG_IS_AEAD( alg ) )
833 ok = exercise_aead_key( key, usage, alg );
834 else if( PSA_ALG_IS_SIGN( alg ) )
835 ok = exercise_signature_key( key, usage, alg );
836 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
837 ok = exercise_asymmetric_encryption_key( key, usage, alg );
838 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
839 ok = exercise_key_derivation_key( key, usage, alg );
840 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
841 ok = exercise_raw_key_agreement_key( key, usage, alg );
842 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
843 ok = exercise_key_agreement_key( key, usage, alg );
844 else
Gilles Peskine2385f712021-02-14 01:34:21 +0100845 TEST_ASSERT( ! "No code to exercise this category of algorithm" );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100846
847 ok = ok && exercise_export_key( key, usage );
848 ok = ok && exercise_export_public_key( key );
849
Gilles Peskine2385f712021-02-14 01:34:21 +0100850exit:
Gilles Peskinee78b0022021-02-13 00:41:11 +0100851 return( ok );
852}
853
854psa_key_usage_t mbedtls_test_psa_usage_to_exercise( psa_key_type_t type,
855 psa_algorithm_t alg )
856{
857 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
858 {
859 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
860 PSA_KEY_USAGE_VERIFY_HASH :
861 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
862 }
863 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
864 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
865 {
866 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
867 PSA_KEY_USAGE_ENCRYPT :
868 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
869 }
870 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
871 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
872 {
873 return( PSA_KEY_USAGE_DERIVE );
874 }
875 else
876 {
877 return( 0 );
878 }
879
880}
Gilles Peskine66e7b902021-02-12 23:40:58 +0100881
882#endif /* MBEDTLS_PSA_CRYPTO_C */