Skip to content

Commit 223f287

Browse files
committed
Add test to check that volatile external keys do not get persisted
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
1 parent bbeaf18 commit 223f287

File tree

2 files changed

+86
-19
lines changed

2 files changed

+86
-19
lines changed

tests/suites/test_suite_psa_crypto_se_driver_hal.data

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,29 @@ register_twice:3
2424
Register SE driver: maximum number of drivers
2525
register_max:
2626

27-
SE key import-export (p_allocate allows all slots)
28-
key_creation_import_export:0:0
27+
SE key import-export persistent (p_allocate allows all slots)
28+
key_creation_import_export:TEST_SE_PERSISTENT_LIFETIME:0:0
2929

30-
SE key import-export (p_allocate allows 1 slot)
31-
key_creation_import_export:ARRAY_LENGTH( ram_slots ) - 1:0
30+
SE key import-export persistent (p_allocate allows 1 slot)
31+
key_creation_import_export:TEST_SE_PERSISTENT_LIFETIME:ARRAY_LENGTH( ram_slots ) - 1:0
3232

33-
SE key import-export, check after restart (slot 0)
34-
key_creation_import_export:0:1
33+
SE key import-export persistent, check after restart (slot 0)
34+
key_creation_import_export:TEST_SE_PERSISTENT_LIFETIME:0:1
3535

36-
SE key import-export, check after restart (slot 3)
37-
key_creation_import_export:3:1
36+
SE key import-export persistent, check after restart (slot 3)
37+
key_creation_import_export:TEST_SE_PERSISTENT_LIFETIME:3:1
38+
39+
SE key import-export volatile (p_allocate allows all slots)
40+
key_creation_import_export:TEST_SE_VOLATILE_LIFETIME:0:0
41+
42+
SE key import-export volatile (p_allocate allows 1 slot)
43+
key_creation_import_export:TEST_SE_VOLATILE_LIFETIME:ARRAY_LENGTH( ram_slots ) - 1:0
44+
45+
SE key import-export volatile, check after restart (slot 0)
46+
key_creation_import_export:TEST_SE_VOLATILE_LIFETIME:0:1
47+
48+
SE key import-export volatile, check after restart (slot 3)
49+
key_creation_import_export:TEST_SE_VOLATILE_LIFETIME:3:1
3850

3951
Key creation in a specific slot (0)
4052
key_creation_in_chosen_slot:0:0:PSA_SUCCESS

tests/suites/test_suite_psa_crypto_se_driver_hal.function

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
( PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( \
2828
PSA_KEY_PERSISTENCE_DEFAULT, TEST_DRIVER_LOCATION ) )
2929

30+
#define TEST_SE_VOLATILE_LIFETIME \
31+
( PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( \
32+
PSA_KEY_PERSISTENCE_VOLATILE, TEST_DRIVER_LOCATION ) )
33+
3034
/** The driver detected a condition that shouldn't happen.
3135
* This is probably a bug in the library. */
3236
#define PSA_ERROR_DETECTED_BY_DRIVER ((psa_status_t)( -500 ))
@@ -609,6 +613,20 @@ exit:
609613
return( ok );
610614
}
611615

616+
/* Check that no persistent data exists for the given location. */
617+
static int check_no_persistent_data( psa_key_location_t location )
618+
{
619+
psa_storage_uid_t uid = file_uid_for_location( location );
620+
struct psa_storage_info_t info;
621+
int ok = 0;
622+
623+
TEST_ASSERT( psa_its_get_info( uid, &info ) == PSA_ERROR_DOES_NOT_EXIST );
624+
ok = 1;
625+
626+
exit:
627+
return( ok );
628+
}
629+
612630
/* Check that a function's return status is "smoke-free", i.e. that
613631
* it's an acceptable error code when calling an API function that operates
614632
* on a key with potentially bogus parameters. */
@@ -829,11 +847,11 @@ exit:
829847
/* END_CASE */
830848

831849
/* BEGIN_CASE */
832-
void key_creation_import_export( int min_slot, int restart )
850+
void key_creation_import_export( int lifetime_arg, int min_slot, int restart )
833851
{
834852
psa_drv_se_t driver;
835853
psa_drv_se_key_management_t key_management;
836-
psa_key_lifetime_t lifetime = TEST_SE_PERSISTENT_LIFETIME;
854+
psa_key_lifetime_t lifetime = (psa_key_lifetime_t) lifetime_arg;
837855
psa_key_location_t location = PSA_KEY_LIFETIME_GET_LOCATION( lifetime );
838856
psa_key_id_t id = 1;
839857
psa_key_handle_t handle = 0;
@@ -864,22 +882,59 @@ void key_creation_import_export( int min_slot, int restart )
864882
PSA_ASSERT( psa_import_key( &attributes,
865883
key_material, sizeof( key_material ),
866884
&handle ) );
867-
if( ! check_persistent_data( location,
868-
&ram_shadow_slot_usage,
869-
sizeof( ram_shadow_slot_usage ) ) )
870-
goto exit;
885+
886+
887+
if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
888+
{
889+
/* For volatile keys, check no persistent data was created */
890+
if( ! check_no_persistent_data( location ) )
891+
goto exit;
892+
}
893+
else
894+
{
895+
/* For persistent keys, check persistent data */
896+
if( ! check_persistent_data( location,
897+
&ram_shadow_slot_usage,
898+
sizeof( ram_shadow_slot_usage ) ) )
899+
goto exit;
900+
}
901+
902+
/* Test that the key was created in the expected slot. */
903+
TEST_ASSERT( ram_slots[min_slot].type == PSA_KEY_TYPE_RAW_DATA );
871904

872905
/* Maybe restart, to check that the information is saved correctly. */
873906
if( restart )
874907
{
875908
mbedtls_psa_crypto_free( );
876909
PSA_ASSERT( psa_register_se_driver( location, &driver ) );
877910
PSA_ASSERT( psa_crypto_init( ) );
878-
if( ! check_persistent_data( location,
879-
&ram_shadow_slot_usage,
880-
sizeof( ram_shadow_slot_usage ) ) )
881-
goto exit;
882-
PSA_ASSERT( psa_open_key( id, &handle ) );
911+
912+
if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
913+
{
914+
/* Check that the PSA core has no knowledge of the volatile key */
915+
TEST_ASSERT( psa_open_key( id, &handle ) == PSA_ERROR_DOES_NOT_EXIST );
916+
917+
/* Drop data from our mockup driver */
918+
ram_slots_reset();
919+
ram_min_slot = min_slot;
920+
921+
/* Re-import key */
922+
PSA_ASSERT( psa_import_key( &attributes,
923+
key_material, sizeof( key_material ),
924+
&handle ) );
925+
}
926+
else
927+
{
928+
929+
/* Check we can re-open the persistent key */
930+
if( ! check_persistent_data( location,
931+
&ram_shadow_slot_usage,
932+
sizeof( ram_shadow_slot_usage ) ) )
933+
goto exit;
934+
935+
/* Check that the PSA core still knows about the key */
936+
PSA_ASSERT( psa_open_key( id, &handle ) );
937+
}
883938
}
884939

885940
/* Test that the key was created in the expected slot. */

0 commit comments

Comments
 (0)