Skip to content

Commit f61a12c

Browse files
RAM test driver: improve key creation
Factor common code of ram_import and ram_fake_generate into a common auxiliary function. Reject key types that aren't supported by this test code. Report the bit size correctly for EC key pairs.
1 parent 871be87 commit f61a12c

File tree

1 file changed

+59
-26
lines changed

1 file changed

+59
-26
lines changed

tests/suites/test_suite_psa_crypto_se_driver_hal.function

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,35 @@ static void ram_slots_reset( void )
164164
ram_min_slot = 0;
165165
}
166166

167+
/* Common parts of key creation.
168+
*
169+
* In case of error, zero out ram_slots[slot_number]. But don't
170+
* do that if the error is PSA_ERROR_DETECTED_BY_DRIVER: in this case
171+
* you don't need to clean up (ram_slot_reset() will take care of it
172+
* in the test case function's cleanup code) and it might be wrong
173+
* (if slot_number is invalid).
174+
*/
175+
static psa_status_t ram_create_common( psa_drv_se_context_t *context,
176+
psa_key_slot_number_t slot_number,
177+
const psa_key_attributes_t *attributes,
178+
size_t required_storage )
179+
{
180+
(void) context;
181+
DRIVER_ASSERT_RETURN( slot_number < ARRAY_LENGTH( ram_slots ) );
182+
183+
ram_slots[slot_number].lifetime = psa_get_key_lifetime( attributes );
184+
ram_slots[slot_number].type = psa_get_key_type( attributes );
185+
ram_slots[slot_number].bits = psa_get_key_bits( attributes );
186+
187+
if( required_storage > sizeof( ram_slots[slot_number].content ) )
188+
{
189+
memset( &ram_slots[slot_number], 0, sizeof( ram_slots[slot_number] ) );
190+
return( PSA_ERROR_INSUFFICIENT_STORAGE );
191+
}
192+
193+
return( PSA_SUCCESS );
194+
}
195+
167196
/* This function does everything except actually generating key material.
168197
* After calling it, you must copy the desired key material to
169198
* ram_slots[slot_number].content. */
@@ -174,7 +203,10 @@ static psa_status_t ram_fake_generate( psa_drv_se_context_t *context,
174203
size_t pubkey_size,
175204
size_t *pubkey_length )
176205
{
177-
(void) context;
206+
psa_status_t status;
207+
size_t required_storage =
208+
PSA_KEY_EXPORT_MAX_SIZE( psa_get_key_type( attributes ),
209+
psa_get_key_bits( attributes ) );
178210

179211
DRIVER_ASSERT_RETURN( *pubkey_length == 0 );
180212
if( ! PSA_KEY_TYPE_IS_KEY_PAIR( psa_get_key_type( attributes ) ) )
@@ -183,21 +215,9 @@ static psa_status_t ram_fake_generate( psa_drv_se_context_t *context,
183215
DRIVER_ASSERT_RETURN( pubkey_size == 0 );
184216
}
185217

186-
{
187-
/* Check that the key can be stored in the memory slot.
188-
* This check only works for key in a "raw" representation:
189-
* symmetric keys or ECC are ok, but not RSA or FFDH. */
190-
size_t required_storage =
191-
PSA_BITS_TO_BYTES( psa_get_key_bits( attributes ) );
192-
size_t available_storage = sizeof( ram_slots[slot_number].content );
193-
if( required_storage > available_storage )
194-
return( PSA_ERROR_INSUFFICIENT_STORAGE );
195-
}
196-
197-
ram_slots[slot_number].lifetime = psa_get_key_lifetime( attributes );
198-
ram_slots[slot_number].type = psa_get_key_type( attributes );
199-
ram_slots[slot_number].bits = psa_get_key_bits( attributes );
200-
return( PSA_SUCCESS );
218+
status = ram_create_common( context, slot_number, attributes,
219+
required_storage );
220+
return( status );
201221
}
202222

203223
static psa_status_t ram_import( psa_drv_se_context_t *context,
@@ -207,23 +227,36 @@ static psa_status_t ram_import( psa_drv_se_context_t *context,
207227
size_t data_length,
208228
size_t *bits )
209229
{
210-
(void) context;
211-
DRIVER_ASSERT_RETURN( slot_number < ARRAY_LENGTH( ram_slots ) );
212-
if( data_length > sizeof( ram_slots[slot_number].content ) )
213-
return( PSA_ERROR_INSUFFICIENT_STORAGE );
214-
ram_slots[slot_number].lifetime = psa_get_key_lifetime( attributes );
215-
ram_slots[slot_number].type = psa_get_key_type( attributes );
216-
ram_slots[slot_number].bits = PSA_BYTES_TO_BITS( data_length );
217-
*bits = PSA_BYTES_TO_BITS( data_length );
230+
psa_key_type_t type = psa_get_key_type( attributes );
231+
psa_status_t status = ram_create_common( context, slot_number, attributes,
232+
data_length );
233+
if( status != PSA_SUCCESS )
234+
return( status );
235+
236+
/* The RAM driver only works for certain key types: raw keys,
237+
* and ECC key pairs. This is true in particular of the bit-size
238+
* calculation here. */
239+
if( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) )
240+
*bits = PSA_BYTES_TO_BITS( data_length );
241+
else if ( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( type ) )
242+
*bits = PSA_ECC_CURVE_BITS( PSA_KEY_TYPE_GET_CURVE( type ) );
243+
else
244+
{
245+
memset( &ram_slots[slot_number], 0, sizeof( ram_slots[slot_number] ) );
246+
return( PSA_ERROR_NOT_SUPPORTED );
247+
}
248+
249+
ram_slots[slot_number].bits = *bits;
218250
memcpy( ram_slots[slot_number].content, data, data_length );
251+
219252
return( PSA_SUCCESS );
220253
}
221254

222255
static psa_status_t ram_export( psa_drv_se_context_t *context,
223256
psa_key_slot_number_t slot_number,
224-
uint8_t *p_data,
257+
uint8_t *data,
225258
size_t data_size,
226-
size_t *p_data_length )
259+
size_t *data_length )
227260
{
228261
size_t actual_size;
229262
(void) context;

0 commit comments

Comments
 (0)