Skip to content

Commit 2227e33

Browse files
author
itayzafrir
committed
Add acl test - use other partitions' key - asymmetric encrypt decrypt
1 parent 5a7ed31 commit 2227e33

File tree

1 file changed

+73
-0
lines changed
  • TESTS/psa/crypto_access_control/COMPONENT_NSPE

1 file changed

+73
-0
lines changed

TESTS/psa/crypto_access_control/COMPONENT_NSPE/main.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,77 @@ void test_use_other_partition_key_asymmetric_sign_verify(void)
370370
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
371371
}
372372

373+
void test_use_other_partition_key_asymmetric_encrypt_decrypt(void)
374+
{
375+
static const psa_key_id_t key_id = 999;
376+
static const psa_key_type_t key_type = PSA_KEY_TYPE_RSA_KEYPAIR;
377+
static const psa_algorithm_t key_alg = PSA_ALG_RSA_PKCS1V15_CRYPT;
378+
static const psa_key_usage_t key_usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT;
379+
static const unsigned char input[] = "encrypt me!";
380+
static const unsigned char key_data[] = {
381+
0x30, 0x82, 0x01, 0x3b, 0x02, 0x01, 0x00, 0x02, 0x41, 0x00, 0xee, 0x2b,
382+
0x13, 0x1d, 0x6b, 0x18, 0x18, 0xa9, 0x4c, 0xa8, 0xe9, 0x1c, 0x42, 0x38,
383+
0x7e, 0xb1, 0x5a, 0x7c, 0x27, 0x1f, 0x57, 0xb8, 0x9e, 0x73, 0x36, 0xb1,
384+
0x44, 0xd4, 0x53, 0x5b, 0x16, 0xc8, 0x30, 0x97, 0xec, 0xde, 0xfb, 0xbb,
385+
0x92, 0xd1, 0xb5, 0x31, 0x3b, 0x5a, 0x37, 0x21, 0x4d, 0x0e, 0x8f, 0x25,
386+
0x92, 0x2d, 0xca, 0x77, 0x8b, 0x42, 0x4b, 0x25, 0x29, 0x5f, 0xc8, 0xa1,
387+
0xa7, 0x07, 0x02, 0x03, 0x01, 0x00, 0x01, 0x02, 0x41, 0x00, 0x97, 0x8a,
388+
0xc8, 0xea, 0xdb, 0x0d, 0xc6, 0x03, 0x53, 0x47, 0xd6, 0xab, 0xa8, 0x67,
389+
0x12, 0x15, 0xff, 0x21, 0x28, 0x33, 0x85, 0x39, 0x6f, 0x78, 0x97, 0xc0,
390+
0x4b, 0xaf, 0x5e, 0x2a, 0x83, 0x5f, 0x3b, 0x53, 0xef, 0x80, 0xa8, 0x2e,
391+
0xd3, 0x6a, 0xe6, 0x87, 0xa9, 0x25, 0x38, 0x0b, 0x55, 0xa0, 0xc7, 0x3e,
392+
0xb8, 0x56, 0x56, 0xe9, 0x89, 0xdc, 0xf0, 0xed, 0x7f, 0xb4, 0x88, 0x70,
393+
0x24, 0xe1, 0x02, 0x21, 0x00, 0xfd, 0xad, 0x8e, 0x1c, 0x68, 0x53, 0x56,
394+
0x3f, 0x8b, 0x92, 0x1d, 0x2d, 0x11, 0x24, 0x62, 0xae, 0x7d, 0x6b, 0x17,
395+
0x60, 0x82, 0xd2, 0xba, 0x43, 0xe8, 0x7e, 0x1a, 0x37, 0xfc, 0x1a, 0x8b,
396+
0x33, 0x02, 0x21, 0x00, 0xf0, 0x59, 0x2c, 0xf4, 0xc5, 0x5b, 0xa4, 0x43,
397+
0x07, 0xb1, 0x89, 0x81, 0xbc, 0xdb, 0xda, 0x37, 0x6c, 0x51, 0xe5, 0x90,
398+
0xff, 0xa5, 0x34, 0x5b, 0xa8, 0x66, 0xf6, 0x96, 0x2d, 0xca, 0x94, 0xdd,
399+
0x02, 0x20, 0x19, 0x95, 0xf1, 0xa9, 0x67, 0xd4, 0x4f, 0xf4, 0xa4, 0xcd,
400+
0x1d, 0xe8, 0x37, 0xbc, 0x65, 0xbf, 0x97, 0xa2, 0xbf, 0x7e, 0xda, 0x73,
401+
0x0a, 0x9a, 0x62, 0xce, 0xa5, 0x32, 0x54, 0x59, 0x11, 0x05, 0x02, 0x20,
402+
0x27, 0xf9, 0x6c, 0xf4, 0xb8, 0xee, 0x68, 0xff, 0x8d, 0x04, 0x06, 0x2e,
403+
0xc1, 0xce, 0x7f, 0x18, 0xc0, 0xb7, 0x4e, 0x4b, 0x33, 0x79, 0xb2, 0x9f,
404+
0x9b, 0xfe, 0xa3, 0xfc, 0x8e, 0x59, 0x27, 0x31, 0x02, 0x21, 0x00, 0xce,
405+
0xfa, 0x6d, 0x22, 0x04, 0x96, 0xb4, 0x3f, 0xeb, 0x83, 0x19, 0x42, 0x55,
406+
0xd8, 0xfb, 0x93, 0x0a, 0xfc, 0xf4, 0x6f, 0x36, 0x60, 0x6e, 0x3a, 0xa0,
407+
0xeb, 0x7a, 0x93, 0xad, 0x88, 0xc1, 0x0c
408+
};
409+
unsigned char encrypted[64] = { 0 };
410+
unsigned char decrypted[sizeof(input)] = { 0 };
411+
psa_key_handle_t key_handle = 0;
412+
size_t len;
413+
414+
/* via test partition - create a key without generating any key material */
415+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_create_persistent_key(key_id, &key_handle));
416+
TEST_ASSERT_NOT_EQUAL(0, key_handle);
417+
418+
/* via test partition - set key policy */
419+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_set_key_policy(key_handle, key_usage, key_alg));
420+
421+
/* via test partition - import key data for the key created by the test partition */
422+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_import_key(key_handle, key_type, key_data, sizeof(key_data)));
423+
424+
/* via test partition - close the key created by the test partition */
425+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
426+
427+
/* via test partition - reopen the key created by the test partition and keep it open */
428+
key_handle = 0;
429+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_open_persistent_key(key_id, &key_handle));
430+
TEST_ASSERT_NOT_EQUAL(0, key_handle);
431+
432+
/* try to asymmetric encrypt using the key that was created by the test partition */
433+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_asymmetric_encrypt(key_handle, key_alg, input, sizeof(input),
434+
NULL, 0, encrypted, sizeof(encrypted), &len));
435+
436+
/* try to asymmetric decrypt using the key that was created by the test partition */
437+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_asymmetric_decrypt(key_handle, key_alg, encrypted, len, NULL, 0,
438+
decrypted, sizeof(decrypted), &len));
439+
440+
/* via test partition - close the key created by the test partition */
441+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
442+
}
443+
373444
utest::v1::status_t case_setup_handler(const Case *const source, const size_t index_of_case)
374445
{
375446
psa_status_t status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
@@ -417,6 +488,8 @@ Case cases[] = {
417488
case_setup_handler, test_use_other_partition_key_aead, case_teardown_handler),
418489
Case("use other partitions' key - asymmetric sign verify",
419490
case_setup_handler, test_use_other_partition_key_asymmetric_sign_verify, case_teardown_handler),
491+
Case("use other partitions' key - asymmetric encrypt decrypt",
492+
case_setup_handler, test_use_other_partition_key_asymmetric_encrypt_decrypt, case_teardown_handler),
420493
};
421494

422495
Specification specification(test_setup, cases);

0 commit comments

Comments
 (0)