blob: 8a2207c0175997774e6e340728ffef21a8e94a40 [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>
Przemyslaw Stekiel53de2622021-11-03 09:35:35 +010032#include <psa_crypto_slot_management.h>
Gilles Peskinee78b0022021-02-13 00:41:11 +010033#include <test/psa_crypto_helpers.h>
34
35#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
36static int lifetime_is_dynamic_secure_element( psa_key_lifetime_t lifetime )
37{
38 return( PSA_KEY_LIFETIME_GET_LOCATION( lifetime ) !=
39 PSA_KEY_LOCATION_LOCAL_STORAGE );
40}
41#endif
42
43static int check_key_attributes_sanity( mbedtls_svc_key_id_t key )
44{
45 int ok = 0;
46 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
47 psa_key_lifetime_t lifetime;
48 mbedtls_svc_key_id_t id;
49 psa_key_type_t type;
Gilles Peskine6b362e62021-02-15 12:03:16 +010050 size_t bits;
Gilles Peskinee78b0022021-02-13 00:41:11 +010051
52 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
53 lifetime = psa_get_key_lifetime( &attributes );
54 id = psa_get_key_id( &attributes );
55 type = psa_get_key_type( &attributes );
56 bits = psa_get_key_bits( &attributes );
57
58 /* Persistence */
59 if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
60 {
61 TEST_ASSERT(
62 ( PSA_KEY_ID_VOLATILE_MIN <=
63 MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
64 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <=
65 PSA_KEY_ID_VOLATILE_MAX ) );
66 }
67 else
68 {
69 TEST_ASSERT(
70 ( PSA_KEY_ID_USER_MIN <= MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) ) &&
71 ( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( id ) <= PSA_KEY_ID_USER_MAX ) );
72 }
73#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
74 /* randomly-generated 64-bit constant, should never appear in test data */
75 psa_key_slot_number_t slot_number = 0xec94d4a5058a1a21;
76 psa_status_t status = psa_get_key_slot_number( &attributes, &slot_number );
77 if( lifetime_is_dynamic_secure_element( lifetime ) )
78 {
79 /* Mbed Crypto currently always exposes the slot number to
80 * applications. This is not mandated by the PSA specification
81 * and may change in future versions. */
82 TEST_EQUAL( status, 0 );
83 TEST_ASSERT( slot_number != 0xec94d4a5058a1a21 );
84 }
85 else
86 {
87 TEST_EQUAL( status, PSA_ERROR_INVALID_ARGUMENT );
88 }
89#endif
90
91 /* Type and size */
92 TEST_ASSERT( type != 0 );
93 TEST_ASSERT( bits != 0 );
94 TEST_ASSERT( bits <= PSA_MAX_KEY_BITS );
95 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
96 TEST_ASSERT( bits % 8 == 0 );
97
98 /* MAX macros concerning specific key types */
99 if( PSA_KEY_TYPE_IS_ECC( type ) )
100 TEST_ASSERT( bits <= PSA_VENDOR_ECC_MAX_CURVE_BITS );
101 else if( PSA_KEY_TYPE_IS_RSA( type ) )
102 TEST_ASSERT( bits <= PSA_VENDOR_RSA_MAX_KEY_BITS );
103 TEST_ASSERT( PSA_BLOCK_CIPHER_BLOCK_LENGTH( type ) <= PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE );
104
105 ok = 1;
106
107exit:
108 /*
109 * Key attributes may have been returned by psa_get_key_attributes()
110 * thus reset them as required.
111 */
112 psa_reset_key_attributes( &attributes );
113
114 return( ok );
115}
116
117static int exercise_mac_key( mbedtls_svc_key_id_t key,
118 psa_key_usage_t usage,
119 psa_algorithm_t alg )
120{
121 psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
122 const unsigned char input[] = "foo";
123 unsigned char mac[PSA_MAC_MAX_SIZE] = {0};
124 size_t mac_length = sizeof( mac );
125
Steven Cooremanfb9cb922021-02-23 14:37:38 +0100126 /* Convert wildcard algorithm to exercisable algorithm */
127 if( alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG )
128 {
129 alg = PSA_ALG_TRUNCATED_MAC( alg, PSA_MAC_TRUNCATED_LENGTH( alg ) );
130 }
131
Gilles Peskinee78b0022021-02-13 00:41:11 +0100132 if( usage & PSA_KEY_USAGE_SIGN_HASH )
133 {
134 PSA_ASSERT( psa_mac_sign_setup( &operation, key, alg ) );
135 PSA_ASSERT( psa_mac_update( &operation,
136 input, sizeof( input ) ) );
137 PSA_ASSERT( psa_mac_sign_finish( &operation,
138 mac, sizeof( mac ),
139 &mac_length ) );
140 }
141
142 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
143 {
144 psa_status_t verify_status =
145 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
146 PSA_SUCCESS :
147 PSA_ERROR_INVALID_SIGNATURE );
148 PSA_ASSERT( psa_mac_verify_setup( &operation, key, alg ) );
149 PSA_ASSERT( psa_mac_update( &operation,
150 input, sizeof( input ) ) );
151 TEST_EQUAL( psa_mac_verify_finish( &operation, mac, mac_length ),
152 verify_status );
153 }
154
155 return( 1 );
156
157exit:
158 psa_mac_abort( &operation );
159 return( 0 );
160}
161
162static int exercise_cipher_key( mbedtls_svc_key_id_t key,
163 psa_key_usage_t usage,
164 psa_algorithm_t alg )
165{
166 psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
167 unsigned char iv[16] = {0};
168 size_t iv_length = sizeof( iv );
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100169 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
170 psa_key_type_t key_type;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100171 const unsigned char plaintext[16] = "Hello, world...";
172 unsigned char ciphertext[32] = "(wabblewebblewibblewobblewubble)";
173 size_t ciphertext_length = sizeof( ciphertext );
174 unsigned char decrypted[sizeof( ciphertext )];
175 size_t part_length;
176
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100177 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
178 key_type = psa_get_key_type( &attributes );
179 iv_length = PSA_CIPHER_IV_LENGTH( key_type, alg );
180
Gilles Peskinee78b0022021-02-13 00:41:11 +0100181 if( usage & PSA_KEY_USAGE_ENCRYPT )
182 {
183 PSA_ASSERT( psa_cipher_encrypt_setup( &operation, key, alg ) );
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100184 if( PSA_CIPHER_IV_LENGTH( key_type, alg ) != 0 )
185 {
186 PSA_ASSERT( psa_cipher_generate_iv( &operation,
187 iv, sizeof( iv ),
188 &iv_length ) );
189 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100190 PSA_ASSERT( psa_cipher_update( &operation,
191 plaintext, sizeof( plaintext ),
192 ciphertext, sizeof( ciphertext ),
193 &ciphertext_length ) );
194 PSA_ASSERT( psa_cipher_finish( &operation,
195 ciphertext + ciphertext_length,
196 sizeof( ciphertext ) - ciphertext_length,
197 &part_length ) );
198 ciphertext_length += part_length;
199 }
200
201 if( usage & PSA_KEY_USAGE_DECRYPT )
202 {
203 psa_status_t status;
204 int maybe_invalid_padding = 0;
205 if( ! ( usage & PSA_KEY_USAGE_ENCRYPT ) )
206 {
Gilles Peskinee78b0022021-02-13 00:41:11 +0100207 maybe_invalid_padding = ! PSA_ALG_IS_STREAM_CIPHER( alg );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100208 }
209 PSA_ASSERT( psa_cipher_decrypt_setup( &operation, key, alg ) );
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100210 if( iv_length != 0 )
211 {
212 PSA_ASSERT( psa_cipher_set_iv( &operation,
213 iv, iv_length ) );
214 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100215 PSA_ASSERT( psa_cipher_update( &operation,
216 ciphertext, ciphertext_length,
217 decrypted, sizeof( decrypted ),
218 &part_length ) );
219 status = psa_cipher_finish( &operation,
220 decrypted + part_length,
221 sizeof( decrypted ) - part_length,
222 &part_length );
223 /* For a stream cipher, all inputs are valid. For a block cipher,
224 * if the input is some aribtrary data rather than an actual
225 ciphertext, a padding error is likely. */
226 if( maybe_invalid_padding )
227 TEST_ASSERT( status == PSA_SUCCESS ||
228 status == PSA_ERROR_INVALID_PADDING );
229 else
230 PSA_ASSERT( status );
231 }
232
233 return( 1 );
234
235exit:
236 psa_cipher_abort( &operation );
Gilles Peskinebbf452c2022-03-18 18:40:47 +0100237 psa_reset_key_attributes( &attributes );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100238 return( 0 );
239}
240
241static int exercise_aead_key( mbedtls_svc_key_id_t key,
242 psa_key_usage_t usage,
243 psa_algorithm_t alg )
244{
245 unsigned char nonce[16] = {0};
Gilles Peskine7acb1982022-03-19 11:03:32 +0100246 size_t nonce_length;
247 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
248 psa_key_type_t key_type;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100249 unsigned char plaintext[16] = "Hello, world...";
250 unsigned char ciphertext[48] = "(wabblewebblewibblewobblewubble)";
251 size_t ciphertext_length = sizeof( ciphertext );
252 size_t plaintext_length = sizeof( ciphertext );
253
Steven Cooremanfb9cb922021-02-23 14:37:38 +0100254 /* Convert wildcard algorithm to exercisable algorithm */
255 if( alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG )
256 {
257 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, PSA_ALG_AEAD_GET_TAG_LENGTH( alg ) );
258 }
259
Gilles Peskine7acb1982022-03-19 11:03:32 +0100260 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
261 key_type = psa_get_key_type( &attributes );
262 nonce_length = PSA_AEAD_NONCE_LENGTH( key_type, alg );
Steven Cooremanaaec3412021-02-18 13:30:34 +0100263
Gilles Peskinee78b0022021-02-13 00:41:11 +0100264 if( usage & PSA_KEY_USAGE_ENCRYPT )
265 {
266 PSA_ASSERT( psa_aead_encrypt( key, alg,
267 nonce, nonce_length,
268 NULL, 0,
269 plaintext, sizeof( plaintext ),
270 ciphertext, sizeof( ciphertext ),
271 &ciphertext_length ) );
272 }
273
274 if( usage & PSA_KEY_USAGE_DECRYPT )
275 {
276 psa_status_t verify_status =
277 ( usage & PSA_KEY_USAGE_ENCRYPT ?
278 PSA_SUCCESS :
279 PSA_ERROR_INVALID_SIGNATURE );
280 TEST_EQUAL( psa_aead_decrypt( key, alg,
281 nonce, nonce_length,
282 NULL, 0,
283 ciphertext, ciphertext_length,
284 plaintext, sizeof( plaintext ),
285 &plaintext_length ),
286 verify_status );
287 }
288
289 return( 1 );
290
291exit:
Gilles Peskine7acb1982022-03-19 11:03:32 +0100292 psa_reset_key_attributes( &attributes );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100293 return( 0 );
294}
295
296static int exercise_signature_key( mbedtls_svc_key_id_t key,
297 psa_key_usage_t usage,
298 psa_algorithm_t alg )
299{
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200300 if( usage & ( PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH ) )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100301 {
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200302 unsigned char payload[PSA_HASH_MAX_SIZE] = {1};
303 size_t payload_length = 16;
304 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
305 size_t signature_length = sizeof( signature );
306 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
307
308 /* If the policy allows signing with any hash, just pick one. */
Gilles Peskinef7b41372021-09-22 16:15:05 +0200309 if( PSA_ALG_IS_SIGN_HASH( alg ) && hash_alg == PSA_ALG_ANY_HASH )
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200310 {
Ronald Cron4c0ec762021-08-31 19:08:55 +0200311 #if defined(KNOWN_MBEDTLS_SUPPORTED_HASH_ALG)
312 hash_alg = KNOWN_MBEDTLS_SUPPORTED_HASH_ALG;
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200313 alg ^= PSA_ALG_ANY_HASH ^ hash_alg;
314 #else
315 TEST_ASSERT( ! "No hash algorithm for hash-and-sign testing" );
316 #endif
317 }
318
Janos Follath4c0b60e2021-06-14 12:34:30 +0100319 /* Some algorithms require the payload to have the size of
320 * the hash encoded in the algorithm. Use this input size
321 * even for algorithms that allow other input sizes. */
322 if( hash_alg != 0 )
323 payload_length = PSA_HASH_LENGTH( hash_alg );
324
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200325 if( usage & PSA_KEY_USAGE_SIGN_HASH )
326 {
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200327 PSA_ASSERT( psa_sign_hash( key, alg,
328 payload, payload_length,
329 signature, sizeof( signature ),
330 &signature_length ) );
331 }
332
333 if( usage & PSA_KEY_USAGE_VERIFY_HASH )
334 {
335 psa_status_t verify_status =
336 ( usage & PSA_KEY_USAGE_SIGN_HASH ?
337 PSA_SUCCESS :
338 PSA_ERROR_INVALID_SIGNATURE );
339 TEST_EQUAL( psa_verify_hash( key, alg,
340 payload, payload_length,
341 signature, signature_length ),
342 verify_status );
343 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100344 }
345
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200346 if( usage & ( PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE ) )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100347 {
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200348 unsigned char message[256] = "Hello, world...";
349 unsigned char signature[PSA_SIGNATURE_MAX_SIZE] = {0};
350 size_t message_length = 16;
351 size_t signature_length = sizeof( signature );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100352
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200353 if( usage & PSA_KEY_USAGE_SIGN_MESSAGE )
354 {
355 PSA_ASSERT( psa_sign_message( key, alg,
356 message, message_length,
357 signature, sizeof( signature ),
358 &signature_length ) );
359 }
360
361 if( usage & PSA_KEY_USAGE_VERIFY_MESSAGE )
362 {
363 psa_status_t verify_status =
364 ( usage & PSA_KEY_USAGE_SIGN_MESSAGE ?
365 PSA_SUCCESS :
366 PSA_ERROR_INVALID_SIGNATURE );
367 TEST_EQUAL( psa_verify_message( key, alg,
368 message, message_length,
369 signature, signature_length ),
370 verify_status );
371 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100372 }
373
374 return( 1 );
375
376exit:
377 return( 0 );
378}
379
380static int exercise_asymmetric_encryption_key( mbedtls_svc_key_id_t key,
381 psa_key_usage_t usage,
382 psa_algorithm_t alg )
383{
384 unsigned char plaintext[256] = "Hello, world...";
385 unsigned char ciphertext[256] = "(wabblewebblewibblewobblewubble)";
386 size_t ciphertext_length = sizeof( ciphertext );
387 size_t plaintext_length = 16;
388
389 if( usage & PSA_KEY_USAGE_ENCRYPT )
390 {
391 PSA_ASSERT( psa_asymmetric_encrypt( key, alg,
392 plaintext, plaintext_length,
393 NULL, 0,
394 ciphertext, sizeof( ciphertext ),
395 &ciphertext_length ) );
396 }
397
398 if( usage & PSA_KEY_USAGE_DECRYPT )
399 {
400 psa_status_t status =
401 psa_asymmetric_decrypt( key, alg,
402 ciphertext, ciphertext_length,
403 NULL, 0,
404 plaintext, sizeof( plaintext ),
405 &plaintext_length );
406 TEST_ASSERT( status == PSA_SUCCESS ||
407 ( ( usage & PSA_KEY_USAGE_ENCRYPT ) == 0 &&
408 ( status == PSA_ERROR_INVALID_ARGUMENT ||
409 status == PSA_ERROR_INVALID_PADDING ) ) );
410 }
411
412 return( 1 );
413
414exit:
415 return( 0 );
416}
417
418int mbedtls_test_psa_setup_key_derivation_wrap(
419 psa_key_derivation_operation_t* operation,
420 mbedtls_svc_key_id_t key,
421 psa_algorithm_t alg,
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100422 const unsigned char* input1, size_t input1_length,
423 const unsigned char* input2, size_t input2_length,
Gilles Peskinee78b0022021-02-13 00:41:11 +0100424 size_t capacity )
425{
426 PSA_ASSERT( psa_key_derivation_setup( operation, alg ) );
427 if( PSA_ALG_IS_HKDF( alg ) )
428 {
429 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
430 PSA_KEY_DERIVATION_INPUT_SALT,
431 input1, input1_length ) );
432 PSA_ASSERT( psa_key_derivation_input_key( operation,
433 PSA_KEY_DERIVATION_INPUT_SECRET,
434 key ) );
435 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
436 PSA_KEY_DERIVATION_INPUT_INFO,
437 input2,
438 input2_length ) );
439 }
440 else if( PSA_ALG_IS_TLS12_PRF( alg ) ||
441 PSA_ALG_IS_TLS12_PSK_TO_MS( alg ) )
442 {
443 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
444 PSA_KEY_DERIVATION_INPUT_SEED,
445 input1, input1_length ) );
446 PSA_ASSERT( psa_key_derivation_input_key( operation,
447 PSA_KEY_DERIVATION_INPUT_SECRET,
448 key ) );
449 PSA_ASSERT( psa_key_derivation_input_bytes( operation,
450 PSA_KEY_DERIVATION_INPUT_LABEL,
451 input2, input2_length ) );
452 }
453 else
454 {
455 TEST_ASSERT( ! "Key derivation algorithm not supported" );
456 }
457
458 if( capacity != SIZE_MAX )
459 PSA_ASSERT( psa_key_derivation_set_capacity( operation, capacity ) );
460
461 return( 1 );
462
463exit:
464 return( 0 );
465}
466
467
468static int exercise_key_derivation_key( mbedtls_svc_key_id_t key,
469 psa_key_usage_t usage,
470 psa_algorithm_t alg )
471{
472 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
473 unsigned char input1[] = "Input 1";
474 size_t input1_length = sizeof( input1 );
475 unsigned char input2[] = "Input 2";
476 size_t input2_length = sizeof( input2 );
477 unsigned char output[1];
478 size_t capacity = sizeof( output );
479
480 if( usage & PSA_KEY_USAGE_DERIVE )
481 {
482 if( !mbedtls_test_psa_setup_key_derivation_wrap( &operation, key, alg,
483 input1, input1_length,
484 input2, input2_length,
485 capacity ) )
486 goto exit;
487
488 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
489 output,
490 capacity ) );
491 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
492 }
493
494 return( 1 );
495
496exit:
497 return( 0 );
498}
499
500/* We need two keys to exercise key agreement. Exercise the
501 * private key against its own public key. */
502psa_status_t mbedtls_test_psa_key_agreement_with_self(
503 psa_key_derivation_operation_t *operation,
504 mbedtls_svc_key_id_t key )
505{
506 psa_key_type_t private_key_type;
507 psa_key_type_t public_key_type;
508 size_t key_bits;
509 uint8_t *public_key = NULL;
510 size_t public_key_length;
511 /* Return GENERIC_ERROR if something other than the final call to
512 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
513 * but it's good enough: callers will report it as a failed test anyway. */
514 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
515 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
516
517 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
518 private_key_type = psa_get_key_type( &attributes );
519 key_bits = psa_get_key_bits( &attributes );
520 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armceface22021-01-21 12:26:17 +0100521 public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100522 ASSERT_ALLOC( public_key, public_key_length );
523 PSA_ASSERT( psa_export_public_key( key, public_key, public_key_length,
524 &public_key_length ) );
525
526 status = psa_key_derivation_key_agreement(
527 operation, PSA_KEY_DERIVATION_INPUT_SECRET, key,
528 public_key, public_key_length );
529exit:
530 /*
531 * Key attributes may have been returned by psa_get_key_attributes()
532 * thus reset them as required.
533 */
534 psa_reset_key_attributes( &attributes );
535
536 mbedtls_free( public_key );
537 return( status );
538}
539
540/* We need two keys to exercise key agreement. Exercise the
541 * private key against its own public key. */
542psa_status_t mbedtls_test_psa_raw_key_agreement_with_self(
543 psa_algorithm_t alg,
544 mbedtls_svc_key_id_t key )
545{
546 psa_key_type_t private_key_type;
547 psa_key_type_t public_key_type;
548 size_t key_bits;
549 uint8_t *public_key = NULL;
550 size_t public_key_length;
551 uint8_t output[1024];
552 size_t output_length;
553 /* Return GENERIC_ERROR if something other than the final call to
554 * psa_key_derivation_key_agreement fails. This isn't fully satisfactory,
555 * but it's good enough: callers will report it as a failed test anyway. */
556 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
557 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
558
559 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
560 private_key_type = psa_get_key_type( &attributes );
561 key_bits = psa_get_key_bits( &attributes );
562 public_key_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR( private_key_type );
gabor-mezei-armceface22021-01-21 12:26:17 +0100563 public_key_length = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_key_type, key_bits );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100564 ASSERT_ALLOC( public_key, public_key_length );
565 PSA_ASSERT( psa_export_public_key( key,
566 public_key, public_key_length,
567 &public_key_length ) );
568
569 status = psa_raw_key_agreement( alg, key,
570 public_key, public_key_length,
571 output, sizeof( output ), &output_length );
gabor-mezei-armceface22021-01-21 12:26:17 +0100572 if ( status == PSA_SUCCESS )
573 {
574 TEST_ASSERT( output_length <=
575 PSA_RAW_KEY_AGREEMENT_OUTPUT_SIZE( private_key_type,
576 key_bits ) );
577 TEST_ASSERT( output_length <=
578 PSA_RAW_KEY_AGREEMENT_OUTPUT_MAX_SIZE );
579 }
580
Gilles Peskinee78b0022021-02-13 00:41:11 +0100581exit:
582 /*
583 * Key attributes may have been returned by psa_get_key_attributes()
584 * thus reset them as required.
585 */
586 psa_reset_key_attributes( &attributes );
587
588 mbedtls_free( public_key );
589 return( status );
590}
591
592static int exercise_raw_key_agreement_key( mbedtls_svc_key_id_t key,
593 psa_key_usage_t usage,
594 psa_algorithm_t alg )
595{
596 int ok = 0;
597
598 if( usage & PSA_KEY_USAGE_DERIVE )
599 {
600 /* We need two keys to exercise key agreement. Exercise the
601 * private key against its own public key. */
602 PSA_ASSERT( mbedtls_test_psa_raw_key_agreement_with_self( alg, key ) );
603 }
604 ok = 1;
605
606exit:
607 return( ok );
608}
609
610static int exercise_key_agreement_key( mbedtls_svc_key_id_t key,
611 psa_key_usage_t usage,
612 psa_algorithm_t alg )
613{
614 psa_key_derivation_operation_t operation = PSA_KEY_DERIVATION_OPERATION_INIT;
615 unsigned char output[1];
616 int ok = 0;
617
618 if( usage & PSA_KEY_USAGE_DERIVE )
619 {
620 /* We need two keys to exercise key agreement. Exercise the
621 * private key against its own public key. */
622 PSA_ASSERT( psa_key_derivation_setup( &operation, alg ) );
623 PSA_ASSERT( mbedtls_test_psa_key_agreement_with_self( &operation, key ) );
624 PSA_ASSERT( psa_key_derivation_output_bytes( &operation,
625 output,
626 sizeof( output ) ) );
627 PSA_ASSERT( psa_key_derivation_abort( &operation ) );
628 }
629 ok = 1;
630
631exit:
632 return( ok );
633}
634
635int mbedtls_test_psa_exported_key_sanity_check(
636 psa_key_type_t type, size_t bits,
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100637 const uint8_t *exported, size_t exported_length )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100638{
Gilles Peskinecc9db302021-02-14 01:29:52 +0100639 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_OUTPUT_SIZE( type, bits ) );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100640
Gilles Peskinecc9db302021-02-14 01:29:52 +0100641 if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
642 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100643 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100644
Ronald Cron64df7382021-07-06 09:23:06 +0200645#if defined(MBEDTLS_ASN1_PARSE_C)
Gilles Peskinee78b0022021-02-13 00:41:11 +0100646 if( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
647 {
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100648 uint8_t *p = (uint8_t*) exported;
649 const uint8_t *end = exported + exported_length;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100650 size_t len;
651 /* RSAPrivateKey ::= SEQUENCE {
652 * version INTEGER, -- must be 0
653 * modulus INTEGER, -- n
654 * publicExponent INTEGER, -- e
655 * privateExponent INTEGER, -- d
656 * prime1 INTEGER, -- p
657 * prime2 INTEGER, -- q
658 * exponent1 INTEGER, -- d mod (p-1)
659 * exponent2 INTEGER, -- d mod (q-1)
660 * coefficient INTEGER, -- (inverse of q) mod p
661 * }
662 */
663 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
664 MBEDTLS_ASN1_SEQUENCE |
665 MBEDTLS_ASN1_CONSTRUCTED ), 0 );
Gilles Peskine89615ee2021-04-29 20:28:54 +0200666 TEST_EQUAL( len, end - p );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100667 if( ! mbedtls_test_asn1_skip_integer( &p, end, 0, 0, 0 ) )
668 goto exit;
669 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
670 goto exit;
671 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
672 goto exit;
673 /* Require d to be at least half the size of n. */
674 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits, 1 ) )
675 goto exit;
676 /* Require p and q to be at most half the size of n, rounded up. */
677 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
678 goto exit;
679 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits / 2, bits / 2 + 1, 1 ) )
680 goto exit;
681 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
682 goto exit;
683 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
684 goto exit;
685 if( ! mbedtls_test_asn1_skip_integer( &p, end, 1, bits / 2 + 1, 0 ) )
686 goto exit;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200687 TEST_EQUAL( p - end, 0 );
gabor-mezei-armceface22021-01-21 12:26:17 +0100688
689 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100690 }
691 else
Ronald Cron64df7382021-07-06 09:23:06 +0200692#endif /* MBEDTLS_ASN1_PARSE_C */
Gilles Peskinee78b0022021-02-13 00:41:11 +0100693
694#if defined(MBEDTLS_ECP_C)
695 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
696 {
697 /* Just the secret value */
698 TEST_EQUAL( exported_length, PSA_BITS_TO_BYTES( bits ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100699
700 TEST_ASSERT( exported_length <= PSA_EXPORT_KEY_PAIR_MAX_SIZE );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100701 }
702 else
703#endif /* MBEDTLS_ECP_C */
704
Ronald Cron64df7382021-07-06 09:23:06 +0200705#if defined(MBEDTLS_ASN1_PARSE_C)
Gilles Peskinead557e52021-02-14 01:19:21 +0100706 if( type == PSA_KEY_TYPE_RSA_PUBLIC_KEY )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100707 {
Gilles Peskine5c2665b2021-02-14 01:22:56 +0100708 uint8_t *p = (uint8_t*) exported;
709 const uint8_t *end = exported + exported_length;
Gilles Peskinead557e52021-02-14 01:19:21 +0100710 size_t len;
711 /* RSAPublicKey ::= SEQUENCE {
712 * modulus INTEGER, -- n
713 * publicExponent INTEGER } -- e
714 */
715 TEST_EQUAL( mbedtls_asn1_get_tag( &p, end, &len,
716 MBEDTLS_ASN1_SEQUENCE |
717 MBEDTLS_ASN1_CONSTRUCTED ),
718 0 );
Gilles Peskine89615ee2021-04-29 20:28:54 +0200719 TEST_EQUAL( len, end - p );
Gilles Peskinead557e52021-02-14 01:19:21 +0100720 if( ! mbedtls_test_asn1_skip_integer( &p, end, bits, bits, 1 ) )
721 goto exit;
722 if( ! mbedtls_test_asn1_skip_integer( &p, end, 2, bits, 1 ) )
723 goto exit;
Gilles Peskine89615ee2021-04-29 20:28:54 +0200724 TEST_EQUAL( p - end, 0 );
gabor-mezei-armceface22021-01-21 12:26:17 +0100725
726
727 TEST_ASSERT( exported_length <=
728 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( type, bits ) );
729 TEST_ASSERT( exported_length <=
730 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
Gilles Peskinead557e52021-02-14 01:19:21 +0100731 }
732 else
Ronald Cron64df7382021-07-06 09:23:06 +0200733#endif /* MBEDTLS_ASN1_PARSE_C */
Gilles Peskinead557e52021-02-14 01:19:21 +0100734
Gilles Peskinee78b0022021-02-13 00:41:11 +0100735#if defined(MBEDTLS_ECP_C)
Gilles Peskinead557e52021-02-14 01:19:21 +0100736 if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( type ) )
737 {
gabor-mezei-armceface22021-01-21 12:26:17 +0100738
739 TEST_ASSERT( exported_length <=
740 PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( type, bits ) );
741 TEST_ASSERT( exported_length <=
742 PSA_EXPORT_PUBLIC_KEY_MAX_SIZE );
743
Gilles Peskinead557e52021-02-14 01:19:21 +0100744 if( PSA_KEY_TYPE_ECC_GET_FAMILY( type ) == PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100745 {
Gilles Peskinead557e52021-02-14 01:19:21 +0100746 /* The representation of an ECC Montgomery public key is
747 * the raw compressed point */
748 TEST_EQUAL( PSA_BITS_TO_BYTES( bits ), exported_length );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100749 }
750 else
Gilles Peskinee78b0022021-02-13 00:41:11 +0100751 {
Gilles Peskinead557e52021-02-14 01:19:21 +0100752 /* The representation of an ECC Weierstrass public key is:
753 * - The byte 0x04;
754 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
755 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian;
756 * - where m is the bit size associated with the curve.
757 */
758 TEST_EQUAL( 1 + 2 * PSA_BITS_TO_BYTES( bits ), exported_length );
759 TEST_EQUAL( exported[0], 4 );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100760 }
761 }
762 else
Gilles Peskinead557e52021-02-14 01:19:21 +0100763#endif /* MBEDTLS_ECP_C */
764
Gilles Peskinead557e52021-02-14 01:19:21 +0100765 {
Andrzej Kurek57d2f132022-01-17 15:26:24 +0100766 (void) exported;
Gilles Peskinecc9db302021-02-14 01:29:52 +0100767 TEST_ASSERT( ! "Sanity check not implemented for this key type" );
Gilles Peskinead557e52021-02-14 01:19:21 +0100768 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100769
Gilles Peskinecc9db302021-02-14 01:29:52 +0100770#if defined(MBEDTLS_DES_C)
771 if( type == PSA_KEY_TYPE_DES )
Gilles Peskinee78b0022021-02-13 00:41:11 +0100772 {
Gilles Peskinecc9db302021-02-14 01:29:52 +0100773 /* Check the parity bits. */
774 unsigned i;
775 for( i = 0; i < bits / 8; i++ )
776 {
777 unsigned bit_count = 0;
778 unsigned m;
779 for( m = 1; m <= 0x100; m <<= 1 )
780 {
781 if( exported[i] & m )
782 ++bit_count;
783 }
784 TEST_ASSERT( bit_count % 2 != 0 );
785 }
Gilles Peskinee78b0022021-02-13 00:41:11 +0100786 }
Gilles Peskinecc9db302021-02-14 01:29:52 +0100787#endif
Gilles Peskinee78b0022021-02-13 00:41:11 +0100788
789 return( 1 );
790
791exit:
792 return( 0 );
793}
794
795static int exercise_export_key( mbedtls_svc_key_id_t key,
796 psa_key_usage_t usage )
797{
798 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
799 uint8_t *exported = NULL;
800 size_t exported_size = 0;
801 size_t exported_length = 0;
802 int ok = 0;
803
804 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
805
806 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
807 psa_get_key_type( &attributes ),
808 psa_get_key_bits( &attributes ) );
809 ASSERT_ALLOC( exported, exported_size );
810
811 if( ( usage & PSA_KEY_USAGE_EXPORT ) == 0 &&
812 ! PSA_KEY_TYPE_IS_PUBLIC_KEY( psa_get_key_type( &attributes ) ) )
813 {
814 TEST_EQUAL( psa_export_key( key, exported,
815 exported_size, &exported_length ),
816 PSA_ERROR_NOT_PERMITTED );
817 ok = 1;
818 goto exit;
819 }
820
821 PSA_ASSERT( psa_export_key( key,
822 exported, exported_size,
823 &exported_length ) );
824 ok = mbedtls_test_psa_exported_key_sanity_check(
825 psa_get_key_type( &attributes ), psa_get_key_bits( &attributes ),
826 exported, exported_length );
827
828exit:
829 /*
830 * Key attributes may have been returned by psa_get_key_attributes()
831 * thus reset them as required.
832 */
833 psa_reset_key_attributes( &attributes );
834
835 mbedtls_free( exported );
836 return( ok );
837}
838
839static int exercise_export_public_key( mbedtls_svc_key_id_t key )
840{
841 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
842 psa_key_type_t public_type;
843 uint8_t *exported = NULL;
844 size_t exported_size = 0;
845 size_t exported_length = 0;
846 int ok = 0;
847
848 PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
849 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( psa_get_key_type( &attributes ) ) )
850 {
851 exported_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
852 psa_get_key_type( &attributes ),
853 psa_get_key_bits( &attributes ) );
854 ASSERT_ALLOC( exported, exported_size );
855
856 TEST_EQUAL( psa_export_public_key( key, exported,
857 exported_size, &exported_length ),
858 PSA_ERROR_INVALID_ARGUMENT );
859 ok = 1;
860 goto exit;
861 }
862
863 public_type = PSA_KEY_TYPE_PUBLIC_KEY_OF_KEY_PAIR(
864 psa_get_key_type( &attributes ) );
gabor-mezei-armceface22021-01-21 12:26:17 +0100865 exported_size = PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE( public_type,
866 psa_get_key_bits( &attributes ) );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100867 ASSERT_ALLOC( exported, exported_size );
868
869 PSA_ASSERT( psa_export_public_key( key,
870 exported, exported_size,
871 &exported_length ) );
872 ok = mbedtls_test_psa_exported_key_sanity_check(
873 public_type, psa_get_key_bits( &attributes ),
874 exported, exported_length );
875
876exit:
877 /*
878 * Key attributes may have been returned by psa_get_key_attributes()
879 * thus reset them as required.
880 */
881 psa_reset_key_attributes( &attributes );
882
883 mbedtls_free( exported );
884 return( ok );
885}
886
887int mbedtls_test_psa_exercise_key( mbedtls_svc_key_id_t key,
888 psa_key_usage_t usage,
889 psa_algorithm_t alg )
890{
Gilles Peskine2385f712021-02-14 01:34:21 +0100891 int ok = 0;
Gilles Peskinee78b0022021-02-13 00:41:11 +0100892
893 if( ! check_key_attributes_sanity( key ) )
894 return( 0 );
895
896 if( alg == 0 )
897 ok = 1; /* If no algorihm, do nothing (used for raw data "keys"). */
898 else if( PSA_ALG_IS_MAC( alg ) )
899 ok = exercise_mac_key( key, usage, alg );
900 else if( PSA_ALG_IS_CIPHER( alg ) )
901 ok = exercise_cipher_key( key, usage, alg );
902 else if( PSA_ALG_IS_AEAD( alg ) )
903 ok = exercise_aead_key( key, usage, alg );
904 else if( PSA_ALG_IS_SIGN( alg ) )
905 ok = exercise_signature_key( key, usage, alg );
906 else if( PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
907 ok = exercise_asymmetric_encryption_key( key, usage, alg );
908 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
909 ok = exercise_key_derivation_key( key, usage, alg );
910 else if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
911 ok = exercise_raw_key_agreement_key( key, usage, alg );
912 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
913 ok = exercise_key_agreement_key( key, usage, alg );
914 else
Gilles Peskine2385f712021-02-14 01:34:21 +0100915 TEST_ASSERT( ! "No code to exercise this category of algorithm" );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100916
917 ok = ok && exercise_export_key( key, usage );
918 ok = ok && exercise_export_public_key( key );
919
Gilles Peskine2385f712021-02-14 01:34:21 +0100920exit:
Gilles Peskinee78b0022021-02-13 00:41:11 +0100921 return( ok );
922}
923
924psa_key_usage_t mbedtls_test_psa_usage_to_exercise( psa_key_type_t type,
925 psa_algorithm_t alg )
926{
927 if( PSA_ALG_IS_MAC( alg ) || PSA_ALG_IS_SIGN( alg ) )
928 {
Gilles Peskinef7b41372021-09-22 16:15:05 +0200929 if( PSA_ALG_IS_SIGN_HASH( alg ) )
gabor-mezei-arm041887b2021-05-11 13:29:24 +0200930 {
931 if( PSA_ALG_SIGN_GET_HASH( alg ) )
932 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
933 PSA_KEY_USAGE_VERIFY_HASH | PSA_KEY_USAGE_VERIFY_MESSAGE:
934 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH |
935 PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE );
936 }
937 else if( PSA_ALG_IS_SIGN_MESSAGE( alg) )
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200938 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
gabor-mezei-arm041887b2021-05-11 13:29:24 +0200939 PSA_KEY_USAGE_VERIFY_MESSAGE :
gabor-mezei-arm4c6a47a2021-04-26 20:12:17 +0200940 PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE );
941
gabor-mezei-arm041887b2021-05-11 13:29:24 +0200942 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
943 PSA_KEY_USAGE_VERIFY_HASH :
944 PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_VERIFY_HASH );
Gilles Peskinee78b0022021-02-13 00:41:11 +0100945 }
946 else if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) ||
947 PSA_ALG_IS_ASYMMETRIC_ENCRYPTION( alg ) )
948 {
949 return( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) ?
950 PSA_KEY_USAGE_ENCRYPT :
951 PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT );
952 }
953 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) ||
954 PSA_ALG_IS_KEY_AGREEMENT( alg ) )
955 {
956 return( PSA_KEY_USAGE_DERIVE );
957 }
958 else
959 {
960 return( 0 );
961 }
962
963}
Gilles Peskine66e7b902021-02-12 23:40:58 +0100964
965#endif /* MBEDTLS_PSA_CRYPTO_C */