Skip to content

Commit 169de02

Browse files
committed
Fixups after review
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
1 parent 68e9556 commit 169de02

File tree

5 files changed

+104
-51
lines changed

5 files changed

+104
-51
lines changed

library/psa_crypto.c

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,43 +1499,12 @@ static psa_status_t psa_validate_key_attributes(
14991499
psa_se_drv_table_entry_t **p_drv )
15001500
{
15011501
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
1502-
psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
15031502

1504-
/* Check there is a proper handler for this lifetime */
1505-
if ( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
1506-
!= PSA_KEY_LOCATION_LOCAL_STORAGE )
1507-
{
1508-
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1509-
psa_se_drv_table_entry_t *p_drv_e = psa_get_se_driver_entry( lifetime );
1510-
if( p_drv_e == NULL )
1511-
status = PSA_ERROR_INVALID_ARGUMENT;
1512-
else
1513-
{
1514-
if (p_drv != NULL)
1515-
*p_drv = p_drv_e;
1516-
status = PSA_SUCCESS;
1517-
}
1518-
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1519-
}
1520-
else
1521-
{
1522-
if( ! PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) ) {
1523-
/* PSA Core needs storage to support persistent local keys */
1524-
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
1525-
psa_key_id_t key_id = psa_get_key_id( attributes );
1526-
if( PSA_KEY_ID_USER_MIN <= key_id && key_id <= PSA_KEY_ID_USER_MAX )
1527-
status = PSA_SUCCESS;
1528-
else
1529-
status = PSA_ERROR_INVALID_ARGUMENT;
1530-
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
1531-
status = PSA_ERROR_NOT_SUPPORTED;
1532-
#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
1533-
} else {
1534-
/* PSA Core is always able to store a volatile key internally */
1535-
status = PSA_SUCCESS;
1536-
}
1537-
}
1503+
status = psa_validate_key_location( attributes, p_drv );
1504+
if( status != PSA_SUCCESS )
1505+
return( status );
15381506

1507+
status = psa_validate_key_persistence( attributes );
15391508
if( status != PSA_SUCCESS )
15401509
return( status );
15411510

library/psa_crypto_slot_management.c

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,55 @@ static int psa_is_key_id_valid( psa_key_file_id_t file_id,
183183
}
184184
#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
185185

186+
psa_status_t psa_validate_key_location( const psa_key_attributes_t *attributes,
187+
psa_se_drv_table_entry_t **p_drv )
188+
{
189+
psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
190+
if ( psa_key_lifetime_is_external( lifetime ) )
191+
{
192+
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
193+
psa_se_drv_table_entry_t *p_drv_e = psa_get_se_driver_entry( lifetime );
194+
if( p_drv_e == NULL )
195+
return( PSA_ERROR_INVALID_ARGUMENT );
196+
else
197+
{
198+
if (p_drv != NULL)
199+
*p_drv = p_drv_e;
200+
return( PSA_SUCCESS );
201+
}
202+
#else
203+
return( PSA_ERROR_INVALID_ARGUMENT );
204+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
205+
}
206+
else
207+
/* Local/internal keys are always valid */
208+
return( PSA_SUCCESS );
209+
}
210+
211+
psa_status_t psa_validate_key_persistence( const psa_key_attributes_t *attributes )
212+
{
213+
psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
214+
215+
if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
216+
{
217+
/* Volatile keys are always supported */
218+
return( PSA_SUCCESS );
219+
}
220+
else
221+
{
222+
/* Persistent keys require storage support */
223+
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
224+
if( psa_is_key_id_valid( psa_get_key_id( attributes ),
225+
psa_key_lifetime_is_external( lifetime ) ) )
226+
return( PSA_SUCCESS );
227+
else
228+
return( PSA_ERROR_INVALID_ARGUMENT );
229+
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
230+
return( PSA_ERROR_NOT_SUPPORTED );
231+
#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
232+
}
233+
}
234+
186235
psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle )
187236
{
188237
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)

library/psa_crypto_slot_management.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,5 +92,33 @@ static inline int psa_key_lifetime_is_external( psa_key_lifetime_t lifetime )
9292
!= PSA_KEY_LOCATION_LOCAL_STORAGE );
9393
}
9494

95+
/** Validate that a key's attributes point to a known location.
96+
*
97+
* This function checks whether the key's attributes point to a location that
98+
* is known to the PSA Core, and returns the driver function table if the key
99+
* is to be found in an external location.
100+
*
101+
* \param[in] attributes The key attributes.
102+
* \param[out] p_drv On success, when a key is located in external
103+
* storage, returns a pointer to the driver table
104+
* associated with the key's storage location.
105+
*
106+
* \retval #PSA_SUCCESS
107+
* \retval #PSA_ERROR_INVALID_ARGUMENT
108+
*/
109+
psa_status_t psa_validate_key_location( const psa_key_attributes_t *attributes,
110+
psa_se_drv_table_entry_t **p_drv );
111+
112+
/** Validate that a key's persistence is consistent.
113+
*
114+
* This function checks whether a key's persistence attribute is consistent.
115+
*
116+
* \param[in] attributes The key attributes.
117+
*
118+
* \retval #PSA_SUCCESS
119+
* \retval #PSA_ERROR_INVALID_ARGUMENT
120+
*/
121+
psa_status_t psa_validate_key_persistence( const psa_key_attributes_t *attributes );
122+
95123

96124
#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */

tests/suites/test_suite_psa_crypto_se_driver_hal.data

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,22 +130,28 @@ Key generation smoke test: HMAC-SHA-256
130130
generate_key_smoke:PSA_KEY_TYPE_HMAC:256:PSA_ALG_HMAC( PSA_ALG_SHA_256 )
131131

132132
Key registration: smoke test
133-
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:1:PSA_SUCCESS
133+
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:1:1:PSA_SUCCESS
134134

135135
Key registration: invalid lifetime (volatile internal storage)
136-
register_key_smoke_test:PSA_KEY_LIFETIME_VOLATILE:1:PSA_ERROR_INVALID_ARGUMENT
136+
register_key_smoke_test:PSA_KEY_LIFETIME_VOLATILE:1:1:PSA_ERROR_INVALID_ARGUMENT
137137

138138
Key registration: invalid lifetime (internal storage)
139-
register_key_smoke_test:PSA_KEY_LIFETIME_PERSISTENT:1:PSA_ERROR_INVALID_ARGUMENT
139+
register_key_smoke_test:PSA_KEY_LIFETIME_PERSISTENT:1:1:PSA_ERROR_INVALID_ARGUMENT
140140

141141
Key registration: invalid lifetime (no registered driver)
142-
register_key_smoke_test:PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( PSA_KEY_PERSISTENCE_DEFAULT, TEST_DRIVER_LOCATION + 1 ):1:PSA_ERROR_INVALID_ARGUMENT
142+
register_key_smoke_test:PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( PSA_KEY_PERSISTENCE_DEFAULT, TEST_DRIVER_LOCATION + 1 ):1:1:PSA_ERROR_INVALID_ARGUMENT
143143

144144
Key registration: rejected
145-
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:0:PSA_ERROR_NOT_PERMITTED
145+
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:1:0:PSA_ERROR_NOT_PERMITTED
146146

147147
Key registration: not supported
148-
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:-1:PSA_ERROR_NOT_SUPPORTED
148+
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:1:-1:PSA_ERROR_NOT_SUPPORTED
149+
150+
Key registration: key id out of range
151+
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:PSA_KEY_ID_VENDOR_MAX+1:-1:PSA_ERROR_INVALID_ARGUMENT
152+
153+
Key registration: key id in vendor range
154+
register_key_smoke_test:TEST_SE_PERSISTENT_LIFETIME:PSA_KEY_ID_VENDOR_MAX:1:PSA_SUCCESS
149155

150156
Import-sign-verify: sign in driver, ECDSA
151157
depends_on:MBEDTLS_ECDSA_C:MBEDTLS_ECP_C:MBEDTLS_ECP_DP_SECP256R1_ENABLED

tests/suites/test_suite_psa_crypto_se_driver_hal.function

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ static int check_no_persistent_data( psa_key_location_t location )
620620
struct psa_storage_info_t info;
621621
int ok = 0;
622622

623-
TEST_ASSERT( psa_its_get_info( uid, &info ) != PSA_SUCCESS );
623+
TEST_ASSERT( psa_its_get_info( uid, &info ) == PSA_ERROR_DOES_NOT_EXIST );
624624
ok = 1;
625625

626626
exit:
@@ -884,18 +884,18 @@ void key_creation_import_export( int lifetime_arg, int min_slot, int restart )
884884
&handle ) );
885885

886886

887-
if( ! PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
887+
if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
888888
{
889-
/* For persistent keys, check persistent data */
890-
if( ! check_persistent_data( location,
891-
&ram_shadow_slot_usage,
892-
sizeof( ram_shadow_slot_usage ) ) )
889+
/* For volatile keys, check no persistent data was created */
890+
if( ! check_no_persistent_data( location ) )
893891
goto exit;
894892
}
895893
else
896894
{
897-
/* For volatile keys, check no persistent data was created */
898-
if( ! check_no_persistent_data( location ) )
895+
/* For persistent keys, check persistent data */
896+
if( ! check_persistent_data( location,
897+
&ram_shadow_slot_usage,
898+
sizeof( ram_shadow_slot_usage ) ) )
899899
goto exit;
900900
}
901901

@@ -912,7 +912,7 @@ void key_creation_import_export( int lifetime_arg, int min_slot, int restart )
912912
if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
913913
{
914914
/* Check that the PSA core has no knowledge of the volatile key */
915-
TEST_ASSERT( psa_open_key( id, &handle ) != PSA_SUCCESS );
915+
TEST_ASSERT( psa_open_key( id, &handle ) == PSA_ERROR_DOES_NOT_EXIST );
916916

917917
/* Drop data from our mockup driver */
918918
ram_slots_reset();
@@ -1410,6 +1410,7 @@ exit:
14101410

14111411
/* BEGIN_CASE */
14121412
void register_key_smoke_test( int lifetime_arg,
1413+
int id_arg,
14131414
int validate,
14141415
int expected_status_arg )
14151416
{
@@ -1419,7 +1420,7 @@ void register_key_smoke_test( int lifetime_arg,
14191420
psa_drv_se_t driver;
14201421
psa_drv_se_key_management_t key_management;
14211422
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1422-
psa_key_id_t id = 1;
1423+
psa_key_id_t id = id_arg;
14231424
size_t bit_size = 48;
14241425
psa_key_slot_number_t wanted_slot = 0x123456789;
14251426
psa_key_handle_t handle = 0;

0 commit comments

Comments
 (0)