Skip to content

Commit 1b7ff0e

Browse files
committed
crypto_se_driver: restructure tests to use mocking
Mock key importing and exporting
1 parent d6b198e commit 1b7ff0e

File tree

2 files changed

+111
-63
lines changed

2 files changed

+111
-63
lines changed

tests/suites/test_suite_psa_crypto_se_driver_hal.data

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,8 @@ register_twice:3
2727
Register SE driver: maximum number of drivers
2828
register_max:
2929

30-
Key creation smoke test (no p_allocate)
31-
key_creation_import_export:-1
30+
Key importing mock test
31+
mock_import:
3232

33-
Key creation smoke test (p_allocate allows all slots)
34-
key_creation_import_export:0
35-
36-
Key creation smoke test (p_allocate allows 1 slot)
37-
key_creation_import_export:ARRAY_LENGTH( mock_slots ) - 1
33+
Key exporting mock test
34+
mock_export:

tests/suites/test_suite_psa_crypto_se_driver_hal.function

Lines changed: 107 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,41 @@
1111
* This is probably a bug in the library. */
1212
#define PSA_ERROR_DETECTED_BY_DRIVER ((psa_status_t)( -500 ))
1313

14-
#define MOCK_MAX_KEY_SIZE 64
15-
typedef struct
14+
static struct
1615
{
16+
uint16_t called;
17+
psa_key_slot_number_t slot_number;
1718
psa_key_lifetime_t lifetime;
1819
psa_key_type_t type;
19-
size_t bits;
20-
uint8_t content[MOCK_MAX_KEY_SIZE];
21-
} mock_slot_t;
22-
static mock_slot_t mock_slots[16];
20+
psa_algorithm_t algorithm;
21+
psa_key_usage_t usage;
22+
size_t data_length;
23+
} mock_import_data;
2324

24-
static uint8_t mock_min_slot = 0;
25+
static struct
26+
{
27+
uint16_t called;
28+
psa_key_slot_number_t slot_number;
29+
size_t data_size;
30+
} mock_export_data;
31+
32+
static struct
33+
{
34+
uint16_t called;
35+
} mock_allocate_data;
2536

26-
static void mock_slots_reset( void )
37+
static struct
2738
{
28-
memset( mock_slots, 0, sizeof( mock_slots ) );
29-
mock_min_slot = 0;
39+
uint16_t called;
40+
psa_key_slot_number_t slot_number;
41+
} mock_destroy_data;
42+
43+
static void mock_teardown( void )
44+
{
45+
memset( &mock_import_data, 0, sizeof( mock_import_data ) );
46+
memset( &mock_export_data, 0, sizeof( mock_export_data ) );
47+
memset( &mock_allocate_data, 0, sizeof( mock_allocate_data ) );
48+
memset( &mock_destroy_data, 0, sizeof( mock_destroy_data ) );
3049
}
3150

3251
static psa_status_t mock_import( psa_key_slot_number_t slot_number,
@@ -37,16 +56,14 @@ static psa_status_t mock_import( psa_key_slot_number_t slot_number,
3756
const uint8_t *p_data,
3857
size_t data_length )
3958
{
40-
if( slot_number > ARRAY_LENGTH( mock_slots ) )
41-
return( PSA_ERROR_DETECTED_BY_DRIVER );
42-
if( data_length > sizeof( mock_slots[slot_number].content ) )
43-
return( PSA_ERROR_INSUFFICIENT_STORAGE );
44-
mock_slots[slot_number].lifetime = lifetime;
45-
mock_slots[slot_number].type = type;
46-
mock_slots[slot_number].bits = PSA_BYTES_TO_BITS( data_length );
47-
(void) algorithm;
48-
(void) usage;
49-
memcpy( mock_slots[slot_number].content, p_data, data_length );
59+
(void) p_data;
60+
mock_import_data.called++;
61+
mock_import_data.slot_number = slot_number;
62+
mock_import_data.lifetime = lifetime;
63+
mock_import_data.type = type;
64+
mock_import_data.algorithm = algorithm;
65+
mock_import_data.usage = usage;
66+
mock_import_data.data_length = data_length;
5067
return( PSA_SUCCESS );
5168
}
5269

@@ -55,22 +72,11 @@ psa_status_t mock_export( psa_key_slot_number_t slot_number,
5572
size_t data_size,
5673
size_t *p_data_length )
5774
{
58-
size_t actual_size;
59-
if( slot_number > ARRAY_LENGTH( mock_slots ) )
60-
return( PSA_ERROR_DETECTED_BY_DRIVER );
61-
actual_size = PSA_BITS_TO_BYTES( mock_slots[slot_number].bits );
62-
if( actual_size > data_size )
63-
return( PSA_ERROR_BUFFER_TOO_SMALL );
64-
*p_data_length = actual_size;
65-
memcpy( p_data, mock_slots[slot_number].content, actual_size );
66-
return( PSA_SUCCESS );
67-
}
68-
69-
psa_status_t mock_destroy( psa_key_slot_number_t slot_number )
70-
{
71-
if( slot_number > ARRAY_LENGTH( mock_slots ) )
72-
return( PSA_ERROR_DETECTED_BY_DRIVER );
73-
memset( &mock_slots[slot_number], 0, sizeof( mock_slots[slot_number] ) );
75+
(void) p_data;
76+
(void) p_data_length;
77+
mock_export_data.called++;
78+
mock_export_data.slot_number = slot_number;
79+
mock_export_data.data_size = data_size;
7480
return( PSA_SUCCESS );
7581
}
7682

@@ -79,9 +85,18 @@ psa_status_t mock_allocate( const psa_key_attributes_t *attributes,
7985
psa_key_slot_number_t *slot_number )
8086
{
8187
(void) attributes;
82-
return( psa_drv_cb_find_free_slot( slot_usage,
83-
mock_min_slot, -1,
84-
slot_number ) );
88+
(void) slot_usage;
89+
(void) slot_number;
90+
mock_allocate_data.called++;
91+
*slot_number = 0;
92+
return( PSA_SUCCESS );
93+
}
94+
95+
psa_status_t mock_destroy( psa_key_slot_number_t slot_number )
96+
{
97+
mock_destroy_data.called++;
98+
mock_destroy_data.slot_number = slot_number;
99+
return( PSA_SUCCESS );
85100
}
86101

87102
/* END_HEADER */
@@ -157,7 +172,52 @@ exit:
157172
/* END_CASE */
158173

159174
/* BEGIN_CASE */
160-
void key_creation_import_export( int min_slot )
175+
void mock_import( )
176+
{
177+
psa_drv_se_t driver;
178+
psa_drv_se_key_management_t key_management;
179+
psa_key_lifetime_t lifetime = 2;
180+
psa_key_id_t id = 1;
181+
psa_key_handle_t handle = 0;
182+
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
183+
const uint8_t key_material[3] = {0xfa, 0xca, 0xde};
184+
185+
memset( &driver, 0, sizeof( driver ) );
186+
memset( &key_management, 0, sizeof( key_management ) );
187+
driver.hal_version = PSA_DRV_SE_HAL_VERSION;
188+
driver.key_management = &key_management;
189+
key_management.slot_count = 1;
190+
key_management.p_import = mock_import;
191+
key_management.p_destroy = mock_destroy;
192+
key_management.p_allocate = mock_allocate;
193+
194+
PSA_ASSERT( psa_register_se_driver( lifetime, &driver ) );
195+
PSA_ASSERT( psa_crypto_init( ) );
196+
197+
psa_set_key_id( &attributes, id );
198+
psa_set_key_lifetime( &attributes, lifetime );
199+
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
200+
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
201+
PSA_ASSERT( psa_import_key( &attributes,
202+
key_material, sizeof( key_material ),
203+
&handle ) );
204+
205+
TEST_ASSERT( mock_allocate_data.called == 1 );
206+
TEST_ASSERT( mock_import_data.called == 1 );
207+
TEST_ASSERT( mock_import_data.type == PSA_KEY_TYPE_RAW_DATA);
208+
209+
PSA_ASSERT( psa_destroy_key( handle ) );
210+
211+
TEST_ASSERT( mock_destroy_data.called == 1 );
212+
213+
exit:
214+
PSA_DONE( );
215+
mock_teardown( );
216+
}
217+
/* END_CASE */
218+
219+
/* BEGIN_CASE */
220+
void mock_export( )
161221
{
162222
psa_drv_se_t driver;
163223
psa_drv_se_key_management_t key_management;
@@ -168,21 +228,16 @@ void key_creation_import_export( int min_slot )
168228
const uint8_t key_material[3] = {0xfa, 0xca, 0xde};
169229
uint8_t exported[sizeof( key_material )];
170230
size_t exported_length;
171-
psa_key_slot_number_t expected_slot = ( min_slot >= 0 ? min_slot : 0 );
172231

173232
memset( &driver, 0, sizeof( driver ) );
174233
memset( &key_management, 0, sizeof( key_management ) );
175234
driver.hal_version = PSA_DRV_SE_HAL_VERSION;
176235
driver.key_management = &key_management;
177-
key_management.slot_count = ARRAY_LENGTH( mock_slots );
236+
key_management.slot_count = 1;
178237
key_management.p_import = mock_import;
179238
key_management.p_export = mock_export;
180239
key_management.p_destroy = mock_destroy;
181-
if( min_slot >= 0 )
182-
{
183-
key_management.p_allocate = mock_allocate;
184-
mock_min_slot = min_slot;
185-
}
240+
key_management.p_allocate = mock_allocate;
186241

187242
PSA_ASSERT( psa_register_se_driver( lifetime, &driver ) );
188243
PSA_ASSERT( psa_crypto_init( ) );
@@ -195,22 +250,18 @@ void key_creation_import_export( int min_slot )
195250
key_material, sizeof( key_material ),
196251
&handle ) );
197252

198-
/* Test that the key was created in the designated slot. */
199-
TEST_ASSERT( mock_slots[expected_slot].type == PSA_KEY_TYPE_RAW_DATA );
200-
201253
PSA_ASSERT( psa_export_key( handle,
202254
exported, sizeof( exported ),
203255
&exported_length ) );
204-
ASSERT_COMPARE( key_material, sizeof( key_material ),
205-
exported, exported_length );
206-
256+
257+
TEST_ASSERT( mock_export_data.called == 1 );
258+
207259
PSA_ASSERT( psa_destroy_key( handle ) );
208260

209-
/* Test that the key has been erased from the designated slot. */
210-
TEST_ASSERT( mock_slots[expected_slot].type == 0 );
261+
TEST_ASSERT( mock_destroy_data.called == 1 );
211262

212263
exit:
213264
PSA_DONE( );
214-
mock_slots_reset( );
265+
mock_teardown( );
215266
}
216267
/* END_CASE */

0 commit comments

Comments
 (0)