Skip to content

Commit 1549c5c

Browse files
author
Cruz Monrreal
authored
Merge pull request #10160 from itayzafrir/crypto-access-control-additional-tests
Crypto access control additional tests
2 parents d3d0622 + 75e4210 commit 1549c5c

File tree

1 file changed

+119
-23
lines changed
  • TESTS/psa/crypto_access_control/COMPONENT_NSPE

1 file changed

+119
-23
lines changed

TESTS/psa/crypto_access_control/COMPONENT_NSPE/main.cpp

Lines changed: 119 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ void test_open_other_partition_key(void)
8989

9090
/* try to open the key created by the test partition */
9191
TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, psa_open_key(PSA_KEY_LIFETIME_PERSISTENT, key_id, &key_handle));
92+
93+
/* via test partition - reopen the key created by the test partition and keep it open */
94+
key_handle = 0;
95+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_open_persistent_key(key_id, &key_handle));
96+
TEST_ASSERT_NOT_EQUAL(0, key_handle);
97+
98+
/* via test partition - destroy the key created by the test partition */
99+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
92100
}
93101

94102
void test_create_key_same_id_different_partitions(void)
@@ -158,11 +166,11 @@ void test_create_key_same_id_different_partitions(void)
158166
TEST_ASSERT_EQUAL(key_usage_local, policy.usage);
159167
TEST_ASSERT_EQUAL(key_alg, policy.alg);
160168

161-
/* via test partition - close the key created by the test partition */
162-
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle_remote));
169+
/* via test partition - destroy the key created by the test partition */
170+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle_remote));
163171

164-
/* close the key created by the current partition (NSPE) */
165-
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_close_key(key_handle_local));
172+
/* destroy the key created by the current partition (NSPE) */
173+
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_destroy_key(key_handle_local));
166174
}
167175

168176
void test_use_other_partition_key_manage_key(void)
@@ -235,8 +243,8 @@ void test_use_other_partition_key_manage_key(void)
235243
/* via test partition - import key data for the key created by the test partition */
236244
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_import_key(key_handle, key_type, key_data, sizeof(key_data)));
237245

238-
/* via test partition - close the key created by the test partition */
239-
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
246+
/* via test partition - destroy the key created by the test partition */
247+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
240248
}
241249

242250
void test_use_other_partition_key_mac(void)
@@ -266,8 +274,8 @@ void test_use_other_partition_key_mac(void)
266274
operation = psa_mac_operation_init();
267275
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_mac_verify_setup(&operation, key_handle, key_alg));
268276

269-
/* via test partition - close the key created by the test partition */
270-
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
277+
/* via test partition - destroy the key created by the test partition */
278+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
271279
}
272280

273281
void test_use_other_partition_key_cipher(void)
@@ -297,8 +305,8 @@ void test_use_other_partition_key_cipher(void)
297305
operation = psa_cipher_operation_init();
298306
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_cipher_decrypt_setup(&operation, key_handle, key_alg));
299307

300-
/* via test partition - close the key created by the test partition */
301-
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
308+
/* via test partition - destroy the key created by the test partition */
309+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
302310
}
303311

304312
void test_use_other_partition_key_aead(void)
@@ -333,8 +341,8 @@ void test_use_other_partition_key_aead(void)
333341
cipher_text, sizeof(cipher_text),
334342
plain_text, sizeof(plain_text), &len));
335343

336-
/* via test partition - close the key created by the test partition */
337-
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
344+
/* via test partition - destroy the key created by the test partition */
345+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
338346
}
339347

340348
void test_use_other_partition_key_asymmetric_sign_verify(void)
@@ -366,8 +374,8 @@ void test_use_other_partition_key_asymmetric_sign_verify(void)
366374
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_asymmetric_verify(key_handle, key_alg, input, sizeof(input),
367375
signature, sizeof(signature)));
368376

369-
/* via test partition - close the key created by the test partition */
370-
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
377+
/* via test partition - destroy the key created by the test partition */
378+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
371379
}
372380

373381
void test_use_other_partition_key_asymmetric_encrypt_decrypt(void)
@@ -438,8 +446,92 @@ void test_use_other_partition_key_asymmetric_encrypt_decrypt(void)
438446
encrypted, sizeof(encrypted), NULL, 0,
439447
decrypted, sizeof(decrypted), &len));
440448

441-
/* via test partition - close the key created by the test partition */
442-
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
449+
/* via test partition - destroy the key created by the test partition */
450+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
451+
}
452+
453+
void test_use_other_partition_key_derivation_setup(void)
454+
{
455+
static const psa_key_id_t key_id = 999;
456+
static const psa_algorithm_t key_alg = PSA_ALG_HKDF(PSA_ALG_SHA_256);
457+
static const psa_key_usage_t key_usage = PSA_KEY_USAGE_DERIVE;
458+
static const psa_key_type_t key_type = PSA_KEY_TYPE_DERIVE;
459+
static const unsigned char key_data[] = {
460+
0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, 0xee, 0x2b,
461+
0x13, 0x1d, 0x6b, 0x18, 0x18, 0xa9, 0x4c, 0xa8, 0xe9, 0x1c, 0x42, 0x38
462+
};
463+
static const unsigned char salt[] = {
464+
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
465+
0x0c
466+
};
467+
static const unsigned char label[] = {
468+
0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9
469+
};
470+
471+
psa_key_handle_t key_handle = 0;
472+
psa_crypto_generator_t generator = psa_crypto_generator_init();
473+
size_t bits = 128;
474+
475+
/* via test partition - create a key without generating any key material */
476+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_create_persistent_key(key_id, &key_handle));
477+
TEST_ASSERT_NOT_EQUAL(0, key_handle);
478+
479+
/* via test partition - set key policy */
480+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_set_key_policy(key_handle, key_usage, key_alg));
481+
482+
/* via test partition - import key data for the key created by the test partition */
483+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_import_key(key_handle, key_type, key_data, sizeof(key_data)));
484+
485+
/* try to setup key derivation using the key that was created by the test partition */
486+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_key_derivation(&generator, key_handle, key_alg,
487+
(unsigned char *)salt, sizeof(salt),
488+
(unsigned char *)label, sizeof(label),
489+
PSA_BITS_TO_BYTES(bits)));
490+
491+
/* via test partition - destroy the key created by the test partition */
492+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
493+
}
494+
495+
void test_use_other_partition_key_agreement_setup(void)
496+
{
497+
static const psa_key_id_t key_id = 999;
498+
static const psa_algorithm_t key_alg = PSA_ALG_ECDH(PSA_ALG_SELECT_RAW);
499+
static const psa_key_usage_t key_usage = PSA_KEY_USAGE_DERIVE;
500+
static const psa_key_type_t key_type = PSA_KEY_TYPE_ECC_KEYPAIR(PSA_ECC_CURVE_SECP256R1);
501+
static const unsigned char key_data[] = {
502+
0xc8, 0x8f, 0x01, 0xf5, 0x10, 0xd9, 0xac, 0x3f, 0x70, 0xa2, 0x92, 0xda,
503+
0xa2, 0x31, 0x6d, 0xe5, 0x44, 0xe9, 0xaa, 0xb8, 0xaf, 0xe8, 0x40, 0x49,
504+
0xc6, 0x2a, 0x9c, 0x57, 0x86, 0x2d, 0x14, 0x33
505+
};
506+
static const unsigned char peer_key_data[] = {
507+
0x04, 0xd1, 0x2d, 0xfb, 0x52, 0x89, 0xc8, 0xd4, 0xf8, 0x12, 0x08, 0xb7,
508+
0x02, 0x70, 0x39, 0x8c, 0x34, 0x22, 0x96, 0x97, 0x0a, 0x0b, 0xcc, 0xb7,
509+
0x4c, 0x73, 0x6f, 0xc7, 0x55, 0x44, 0x94, 0xbf, 0x63, 0x56, 0xfb, 0xf3,
510+
0xca, 0x36, 0x6c, 0xc2, 0x3e, 0x81, 0x57, 0x85, 0x4c, 0x13, 0xc5, 0x8d,
511+
0x6a, 0xac, 0x23, 0xf0, 0x46, 0xad, 0xa3, 0x0f, 0x83, 0x53, 0xe7, 0x4f,
512+
0x33, 0x03, 0x98, 0x72, 0xab
513+
};
514+
515+
psa_key_handle_t key_handle = 0;
516+
psa_crypto_generator_t generator = psa_crypto_generator_init();
517+
518+
/* via test partition - create a key without generating any key material */
519+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_create_persistent_key(key_id, &key_handle));
520+
TEST_ASSERT_NOT_EQUAL(0, key_handle);
521+
522+
/* via test partition - set key policy */
523+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_set_key_policy(key_handle, key_usage, key_alg));
524+
525+
/* via test partition - import key data for the key created by the test partition */
526+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_import_key(key_handle, key_type, key_data, sizeof(key_data)));
527+
528+
/* try to setup key agreement using the key that was created by the test partition */
529+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_key_agreement(&generator, key_handle,
530+
(unsigned char *)peer_key_data, sizeof(peer_key_data),
531+
key_alg));
532+
533+
/* via test partition - destroy the key created by the test partition */
534+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
443535
}
444536

445537
utest::v1::status_t case_setup_handler(const Case *const source, const size_t index_of_case)
@@ -473,22 +565,26 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
473565
}
474566

475567
Case cases[] = {
476-
Case("open other partitions' key",
568+
Case("open other partition's key",
477569
case_setup_handler, test_open_other_partition_key, case_teardown_handler),
478570
Case("create key with same id different partitions",
479571
case_setup_handler, test_create_key_same_id_different_partitions, case_teardown_handler),
480-
Case("use other partitions' key - key manage",
572+
Case("use other partition's key - key manage",
481573
case_setup_handler, test_use_other_partition_key_manage_key, case_teardown_handler),
482-
Case("use other partitions' key - mac",
574+
Case("use other partition's key - mac",
483575
case_setup_handler, test_use_other_partition_key_mac, case_teardown_handler),
484-
Case("use other partitions' key - cipher",
576+
Case("use other partition's key - cipher",
485577
case_setup_handler, test_use_other_partition_key_cipher, case_teardown_handler),
486-
Case("use other partitions' key - aead",
578+
Case("use other partition's key - aead",
487579
case_setup_handler, test_use_other_partition_key_aead, case_teardown_handler),
488-
Case("use other partitions' key - asymmetric sign verify",
580+
Case("use other partition's key - asymmetric sign verify",
489581
case_setup_handler, test_use_other_partition_key_asymmetric_sign_verify, case_teardown_handler),
490-
Case("use other partitions' key - asymmetric encrypt decrypt",
582+
Case("use other partition's key - asymmetric encrypt decrypt",
491583
case_setup_handler, test_use_other_partition_key_asymmetric_encrypt_decrypt, case_teardown_handler),
584+
Case("use other partition's key - key derivation setup",
585+
case_setup_handler, test_use_other_partition_key_derivation_setup, case_teardown_handler),
586+
Case("use other partition's key - key agreement setup",
587+
case_setup_handler, test_use_other_partition_key_agreement_setup, case_teardown_handler),
492588
};
493589

494590
Specification specification(test_setup, cases);

0 commit comments

Comments
 (0)