Skip to content

Commit 62ec1d4

Browse files
committed
Split 'validate persistent key parameters' into independent validation of location and persistence
Signed-off-by: Steven Cooreman <steven.cooreman@silabs.com>
1 parent 5cbe193 commit 62ec1d4

File tree

3 files changed

+71
-64
lines changed

3 files changed

+71
-64
lines changed

library/psa_crypto.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,16 +1498,15 @@ static psa_status_t psa_validate_key_attributes(
14981498
const psa_key_attributes_t *attributes,
14991499
psa_se_drv_table_entry_t **p_drv )
15001500
{
1501-
psa_status_t status;
1501+
psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
15021502

1503-
if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
1504-
{
1505-
status = psa_validate_persistent_key_parameters(
1506-
attributes->core.lifetime, attributes->core.id,
1507-
p_drv, 1 );
1508-
if( status != PSA_SUCCESS )
1509-
return( status );
1510-
}
1503+
status = psa_validate_key_location( attributes, p_drv );
1504+
if( status != PSA_SUCCESS )
1505+
return( status );
1506+
1507+
status = psa_validate_key_persistence( attributes );
1508+
if( status != PSA_SUCCESS )
1509+
return( status );
15111510

15121511
status = psa_validate_key_policy( &attributes->core.policy );
15131512
if( status != PSA_SUCCESS )

library/psa_crypto_slot_management.c

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -183,39 +183,54 @@ 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_persistent_key_parameters(
187-
psa_key_lifetime_t lifetime,
188-
psa_key_file_id_t id,
189-
psa_se_drv_table_entry_t **p_drv,
190-
int creating )
186+
psa_status_t psa_validate_key_location( const psa_key_attributes_t *attributes,
187+
psa_se_drv_table_entry_t **p_drv )
191188
{
192-
if( p_drv != NULL )
193-
*p_drv = NULL;
194-
#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
195-
if( psa_key_lifetime_is_external( lifetime ) )
189+
psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
190+
if ( psa_key_lifetime_is_external( lifetime ) )
196191
{
197-
*p_drv = psa_get_se_driver_entry( lifetime );
198-
if( *p_drv == NULL )
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 )
199195
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+
(void) p_drv;
204+
return( PSA_ERROR_INVALID_ARGUMENT );
205+
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
200206
}
201207
else
202-
#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
203-
if( ( PSA_KEY_LIFETIME_GET_LOCATION( lifetime )
204-
!= PSA_KEY_LOCATION_LOCAL_STORAGE ) ||
205-
( PSA_KEY_LIFETIME_GET_PERSISTENCE( lifetime )
206-
!= PSA_KEY_PERSISTENCE_DEFAULT ) )
207-
return( PSA_ERROR_INVALID_ARGUMENT );
208+
/* Local/internal keys are always valid */
209+
return( PSA_SUCCESS );
210+
}
208211

209-
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
210-
if( ! psa_is_key_id_valid( id, ! creating ) )
211-
return( PSA_ERROR_INVALID_ARGUMENT );
212-
return( PSA_SUCCESS );
212+
psa_status_t psa_validate_key_persistence( const psa_key_attributes_t *attributes )
213+
{
214+
psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
213215

216+
if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
217+
{
218+
/* Volatile keys are always supported */
219+
return( PSA_SUCCESS );
220+
}
221+
else
222+
{
223+
/* Persistent keys require storage support */
224+
#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
225+
if( psa_is_key_id_valid( psa_get_key_id( attributes ),
226+
psa_key_lifetime_is_external( lifetime ) ) )
227+
return( PSA_SUCCESS );
228+
else
229+
return( PSA_ERROR_INVALID_ARGUMENT );
214230
#else /* MBEDTLS_PSA_CRYPTO_STORAGE_C */
215-
(void) id;
216-
(void) creating;
217-
return( PSA_ERROR_NOT_SUPPORTED );
231+
return( PSA_ERROR_NOT_SUPPORTED );
218232
#endif /* !MBEDTLS_PSA_CRYPTO_STORAGE_C */
233+
}
219234
}
220235

221236
psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle )
@@ -226,10 +241,8 @@ psa_status_t psa_open_key( psa_key_file_id_t id, psa_key_handle_t *handle )
226241

227242
*handle = 0;
228243

229-
status = psa_validate_persistent_key_parameters(
230-
PSA_KEY_LIFETIME_PERSISTENT, id, NULL, 0 );
231-
if( status != PSA_SUCCESS )
232-
return( status );
244+
if( ! psa_is_key_id_valid( id, 1 ) )
245+
return( PSA_ERROR_INVALID_ARGUMENT );
233246

234247
status = psa_get_empty_key_slot( handle, &slot );
235248
if( status != PSA_SUCCESS )

library/psa_crypto_slot_management.h

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

95-
/** Test whether the given parameters are acceptable for a persistent key.
95+
/** Validate that a key's attributes point to a known location.
9696
*
97-
* This function does not access the storage in any way. It only tests
98-
* whether the parameters are meaningful and permitted by general policy.
99-
* It does not test whether the a file by the given id exists or could be
100-
* created.
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.
101100
*
102-
* If the key is in external storage, this function returns the corresponding
103-
* driver.
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.
104105
*
105-
* \param lifetime The lifetime to test.
106-
* \param id The key id to test.
107-
* \param[out] p_drv On output, if \p lifetime designates a key
108-
* in an external processor, \c *p_drv is a pointer
109-
* to the driver table entry fot this lifetime.
110-
* If \p lifetime designates a transparent key,
111-
* \c *p_drv is \c NULL.
112-
* \param creating 0 if attempting to open an existing key.
113-
* Nonzero if attempting to create a key.
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.
114113
*
115-
* \retval PSA_SUCCESS
116-
* The given parameters are valid.
117-
* \retval PSA_ERROR_INVALID_ARGUMENT
118-
* \p lifetime is volatile or is invalid.
119-
* \retval PSA_ERROR_INVALID_ARGUMENT
120-
* \p id is invalid.
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
121120
*/
122-
psa_status_t psa_validate_persistent_key_parameters(
123-
psa_key_lifetime_t lifetime,
124-
psa_key_file_id_t id,
125-
psa_se_drv_table_entry_t **p_drv,
126-
int creating );
121+
psa_status_t psa_validate_key_persistence( const psa_key_attributes_t *attributes );
127122

128123

129124
#endif /* PSA_CRYPTO_SLOT_MANAGEMENT_H */

0 commit comments

Comments
 (0)