2018-11-30 18:53:28 +01:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2020-07-31 11:26:37 +02:00
|
|
|
#include "psa_crypto_slot_management.h"
|
2018-11-30 18:53:28 +01:00
|
|
|
#include "psa_crypto_storage.h"
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
2020-10-20 17:24:41 +02:00
|
|
|
/**< Close key(s) */
|
|
|
|
INVALIDATE_BY_CLOSING,
|
|
|
|
|
|
|
|
/**< Destroy key(s) */
|
|
|
|
INVALIDATE_BY_DESTROYING,
|
|
|
|
|
2020-10-20 17:50:24 +02:00
|
|
|
/**< Purge key(s) */
|
|
|
|
INVALIDATE_BY_PURGING,
|
|
|
|
|
2020-10-20 17:24:41 +02:00
|
|
|
/**< Terminate and reinitialize without closing/destroying keys */
|
|
|
|
INVALIDATE_BY_SHUTDOWN,
|
|
|
|
|
|
|
|
/**< Close key(s) then terminate and re-initialize */
|
|
|
|
INVALIDATE_BY_CLOSING_WITH_SHUTDOWN,
|
|
|
|
|
|
|
|
/**< Destroy key(s) then terminate and re-initialize */
|
|
|
|
INVALIDATE_BY_DESTROYING_WITH_SHUTDOWN,
|
2020-10-20 17:50:24 +02:00
|
|
|
|
|
|
|
/**< Purge key(s) then terminate and re-initialize */
|
|
|
|
INVALIDATE_BY_PURGING_WITH_SHUTDOWN,
|
2020-10-20 17:24:41 +02:00
|
|
|
} invalidate_method_t;
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
KEEP_OPEN,
|
|
|
|
CLOSE_BEFORE,
|
|
|
|
CLOSE_AFTER,
|
|
|
|
} reopen_policy_t;
|
|
|
|
|
2019-10-11 11:44:48 +02:00
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
INVALID_HANDLE_0,
|
|
|
|
INVALID_HANDLE_UNOPENED,
|
|
|
|
INVALID_HANDLE_CLOSED,
|
|
|
|
INVALID_HANDLE_HUGE,
|
|
|
|
} invalid_handle_construction_t;
|
|
|
|
|
2020-10-20 17:24:41 +02:00
|
|
|
/** Apply \p invalidate_method to invalidate the specified key:
|
2019-05-28 15:06:43 +02:00
|
|
|
* close it, destroy it, or do nothing;
|
|
|
|
*/
|
2020-10-20 17:24:41 +02:00
|
|
|
static int invalidate_key( invalidate_method_t invalidate_method,
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key )
|
2019-05-28 15:06:43 +02:00
|
|
|
{
|
2020-10-20 17:24:41 +02:00
|
|
|
switch( invalidate_method )
|
2019-05-28 15:06:43 +02:00
|
|
|
{
|
2020-11-10 18:08:03 +01:00
|
|
|
/* Closing the key invalidate only volatile keys, not persistent ones. */
|
2020-10-20 17:24:41 +02:00
|
|
|
case INVALIDATE_BY_CLOSING:
|
|
|
|
case INVALIDATE_BY_CLOSING_WITH_SHUTDOWN:
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_close_key( key ) );
|
2019-05-28 15:06:43 +02:00
|
|
|
break;
|
2020-10-20 17:24:41 +02:00
|
|
|
case INVALIDATE_BY_DESTROYING:
|
|
|
|
case INVALIDATE_BY_DESTROYING_WITH_SHUTDOWN:
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
2019-05-28 15:06:43 +02:00
|
|
|
break;
|
2020-11-10 17:45:56 +01:00
|
|
|
/* Purging the key just purges RAM data of persistent keys. */
|
2020-10-20 17:50:24 +02:00
|
|
|
case INVALIDATE_BY_PURGING:
|
|
|
|
case INVALIDATE_BY_PURGING_WITH_SHUTDOWN:
|
|
|
|
PSA_ASSERT( psa_purge_key( key ) );
|
|
|
|
break;
|
2020-10-20 17:24:41 +02:00
|
|
|
case INVALIDATE_BY_SHUTDOWN:
|
2019-05-28 15:06:43 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return( 1 );
|
|
|
|
exit:
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2020-10-20 17:24:41 +02:00
|
|
|
/** Restart the PSA subsystem if \p invalidate_method says so. */
|
|
|
|
static int invalidate_psa( invalidate_method_t invalidate_method )
|
2019-05-28 15:06:43 +02:00
|
|
|
{
|
2020-10-20 17:24:41 +02:00
|
|
|
switch( invalidate_method )
|
2019-05-28 15:06:43 +02:00
|
|
|
{
|
2020-10-20 17:24:41 +02:00
|
|
|
case INVALIDATE_BY_CLOSING:
|
|
|
|
case INVALIDATE_BY_DESTROYING:
|
2020-10-20 17:50:24 +02:00
|
|
|
case INVALIDATE_BY_PURGING:
|
2019-05-28 15:06:43 +02:00
|
|
|
return( 1 );
|
2020-10-20 17:24:41 +02:00
|
|
|
case INVALIDATE_BY_CLOSING_WITH_SHUTDOWN:
|
|
|
|
case INVALIDATE_BY_DESTROYING_WITH_SHUTDOWN:
|
2020-10-20 17:50:24 +02:00
|
|
|
case INVALIDATE_BY_PURGING_WITH_SHUTDOWN:
|
2019-05-28 15:06:43 +02:00
|
|
|
/* All keys must have been closed. */
|
2021-02-14 14:08:22 +01:00
|
|
|
PSA_SESSION_DONE( );
|
2019-05-28 15:06:43 +02:00
|
|
|
break;
|
2020-10-20 17:24:41 +02:00
|
|
|
case INVALIDATE_BY_SHUTDOWN:
|
2019-05-28 15:06:43 +02:00
|
|
|
/* Some keys may remain behind, and we're testing that this
|
|
|
|
* properly closes them. */
|
|
|
|
mbedtls_psa_crypto_free( );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
ASSERT_PSA_PRISTINE( );
|
|
|
|
return( 1 );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2018-11-30 18:53:28 +01:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_PSA_CRYPTO_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2020-10-16 15:32:23 +02:00
|
|
|
void transient_slot_lifecycle( int owner_id_arg,
|
|
|
|
int usage_arg, int alg_arg,
|
2019-01-19 12:20:52 +01:00
|
|
|
int type_arg, data_t *key_data,
|
2020-10-20 17:24:41 +02:00
|
|
|
int invalidate_method_arg )
|
2018-11-30 18:53:28 +01:00
|
|
|
{
|
|
|
|
psa_algorithm_t alg = alg_arg;
|
|
|
|
psa_key_usage_t usage_flags = usage_arg;
|
2019-01-19 12:20:52 +01:00
|
|
|
psa_key_type_t type = type_arg;
|
2020-10-20 17:24:41 +02:00
|
|
|
invalidate_method_t invalidate_method = invalidate_method_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2021-02-14 23:45:48 +01:00
|
|
|
mbedtls_test_set_step( 1 );
|
2018-11-30 18:53:28 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2019-04-19 18:20:30 +02:00
|
|
|
/* Import a key. */
|
2020-11-11 15:19:20 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
2020-10-16 15:32:23 +02:00
|
|
|
mbedtls_key_owner_id_t owner_id = owner_id_arg;
|
|
|
|
|
|
|
|
mbedtls_set_key_owner_id( &attributes, owner_id );
|
2020-11-11 15:19:20 +01:00
|
|
|
#else
|
2020-10-16 15:32:23 +02:00
|
|
|
(void)owner_id_arg;
|
2020-11-11 15:19:20 +01:00
|
|
|
#endif
|
2020-10-16 15:32:23 +02:00
|
|
|
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_set_key_usage_flags( &attributes, usage_flags );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
|
|
|
psa_set_key_type( &attributes, type );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&key ) );
|
|
|
|
TEST_ASSERT( ! mbedtls_svc_key_id_is_null( key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
2019-04-19 18:20:30 +02:00
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type );
|
2020-10-20 17:01:35 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
2020-11-11 15:19:20 +01:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
2020-10-16 15:32:23 +02:00
|
|
|
{
|
|
|
|
psa_key_handle_t handle;
|
|
|
|
mbedtls_svc_key_id_t key_with_invalid_owner =
|
|
|
|
mbedtls_svc_key_id_make( owner_id + 1,
|
|
|
|
MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) );
|
|
|
|
|
|
|
|
TEST_ASSERT( mbedtls_key_owner_id_equal(
|
|
|
|
owner_id,
|
|
|
|
MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( key ) ) );
|
|
|
|
TEST_EQUAL( psa_open_key( key_with_invalid_owner, &handle ),
|
|
|
|
PSA_ERROR_DOES_NOT_EXIST );
|
|
|
|
}
|
2020-11-11 15:19:20 +01:00
|
|
|
#endif
|
2020-10-16 15:32:23 +02:00
|
|
|
|
2020-10-20 17:01:35 +02:00
|
|
|
/*
|
|
|
|
* Purge the key and make sure that it is still valid, as purging a
|
|
|
|
* volatile key shouldn't invalidate/destroy it.
|
|
|
|
*/
|
|
|
|
PSA_ASSERT( psa_purge_key( key ) );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( key, &attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type );
|
|
|
|
psa_reset_key_attributes( &attributes );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
/* Do something that invalidates the key. */
|
2021-02-14 23:45:48 +01:00
|
|
|
mbedtls_test_set_step( 2 );
|
2020-10-20 17:24:41 +02:00
|
|
|
if( ! invalidate_key( invalidate_method, key ) )
|
2019-05-28 15:06:43 +02:00
|
|
|
goto exit;
|
2020-10-20 17:24:41 +02:00
|
|
|
if( ! invalidate_psa( invalidate_method ) )
|
2019-05-28 15:06:43 +02:00
|
|
|
goto exit;
|
2019-04-19 18:20:30 +02:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
/* Test that the key is now invalid. */
|
|
|
|
TEST_EQUAL( psa_get_key_attributes( key, &attributes ),
|
2021-03-17 17:11:05 +01:00
|
|
|
PSA_ERROR_INVALID_HANDLE );
|
2021-03-17 16:05:13 +01:00
|
|
|
TEST_EQUAL( psa_close_key( key ), PSA_ERROR_INVALID_HANDLE );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-11-30 18:53:28 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-12-03 18:19:39 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
2020-07-28 15:06:14 +02:00
|
|
|
void persistent_slot_lifecycle( int lifetime_arg, int owner_id_arg, int id_arg,
|
2019-05-13 14:32:11 +02:00
|
|
|
int usage_arg, int alg_arg, int alg2_arg,
|
2019-01-19 12:20:52 +01:00
|
|
|
int type_arg, data_t *key_data,
|
2020-10-20 17:24:41 +02:00
|
|
|
int invalidate_method_arg )
|
2018-11-30 18:53:28 +01:00
|
|
|
{
|
|
|
|
psa_key_lifetime_t lifetime = lifetime_arg;
|
2020-07-28 15:06:14 +02:00
|
|
|
mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
|
2018-11-30 18:53:28 +01:00
|
|
|
psa_algorithm_t alg = alg_arg;
|
2019-05-13 14:32:11 +02:00
|
|
|
psa_algorithm_t alg2 = alg2_arg;
|
2018-11-30 18:53:28 +01:00
|
|
|
psa_key_usage_t usage_flags = usage_arg;
|
2019-01-19 12:20:52 +01:00
|
|
|
psa_key_type_t type = type_arg;
|
2020-10-20 17:24:41 +02:00
|
|
|
invalidate_method_t invalidate_method = invalidate_method_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t returned_id = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-07-30 17:48:03 +02:00
|
|
|
psa_key_handle_t handle = PSA_KEY_HANDLE_INIT;
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-05-13 14:24:15 +02:00
|
|
|
psa_key_attributes_t read_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
uint8_t *reexported = NULL;
|
|
|
|
size_t reexported_length = -1;
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2020-07-29 15:24:05 +02:00
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
|
|
|
mbedtls_svc_key_id_t wrong_owner_id =
|
|
|
|
mbedtls_svc_key_id_make( owner_id_arg + 1, id_arg );
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t invalid_svc_key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-07-29 15:24:05 +02:00
|
|
|
#endif
|
|
|
|
|
2019-05-06 18:39:37 +02:00
|
|
|
TEST_USES_KEY_ID( id );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2021-02-14 23:45:48 +01:00
|
|
|
mbedtls_test_set_step( 1 );
|
2018-11-30 18:53:28 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2019-05-15 16:12:22 +02:00
|
|
|
psa_set_key_id( &attributes, id );
|
|
|
|
psa_set_key_lifetime( &attributes, lifetime );
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_set_key_type( &attributes, type );
|
|
|
|
psa_set_key_usage_flags( &attributes, usage_flags );
|
|
|
|
psa_set_key_algorithm( &attributes, alg );
|
2019-05-13 14:32:11 +02:00
|
|
|
psa_set_key_enrollment_algorithm( &attributes, alg2 );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, key_data->x, key_data->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&returned_id ) );
|
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal( id, returned_id ) );
|
2020-07-29 15:24:05 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_open_key( wrong_owner_id, &invalid_svc_key_id ),
|
2020-07-29 15:24:05 +02:00
|
|
|
PSA_ERROR_DOES_NOT_EXIST );
|
|
|
|
#endif
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( id, &attributes ) );
|
2019-05-13 14:24:15 +02:00
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
|
2020-07-23 17:13:42 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal(
|
|
|
|
psa_get_key_id( &attributes ), id ) );
|
2021-05-13 12:05:01 +02:00
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
|
2021-06-28 14:05:00 +02:00
|
|
|
mbedtls_test_update_key_usage_flags( usage_flags ) );
|
2019-05-13 14:24:15 +02:00
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
|
2019-05-13 14:32:11 +02:00
|
|
|
TEST_EQUAL( psa_get_key_enrollment_algorithm( &attributes ), alg2 );
|
2019-05-13 14:24:15 +02:00
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
/* Close the key and then open it. */
|
|
|
|
PSA_ASSERT( psa_close_key( id ) );
|
2020-07-29 15:24:05 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_open_key( wrong_owner_id, &invalid_svc_key_id ),
|
2020-07-29 15:24:05 +02:00
|
|
|
PSA_ERROR_DOES_NOT_EXIST );
|
|
|
|
#endif
|
|
|
|
|
2019-05-06 18:44:55 +02:00
|
|
|
PSA_ASSERT( psa_open_key( id, &handle ) );
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_ASSERT( ! psa_key_handle_is_null( handle ) );
|
2019-05-13 14:24:15 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
|
2020-07-23 17:13:42 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal(
|
|
|
|
psa_get_key_id( &attributes ), id ) );
|
2021-05-13 12:05:01 +02:00
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
|
2021-06-28 14:05:00 +02:00
|
|
|
mbedtls_test_update_key_usage_flags( usage_flags ) );
|
2019-05-13 14:24:15 +02:00
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), alg );
|
2019-05-13 14:32:11 +02:00
|
|
|
TEST_EQUAL( psa_get_key_enrollment_algorithm( &attributes ), alg2 );
|
2019-05-13 14:24:15 +02:00
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
/*
|
|
|
|
* Do something that wipes key data in volatile memory or destroy the
|
|
|
|
* key.
|
|
|
|
*/
|
2021-02-14 23:45:48 +01:00
|
|
|
mbedtls_test_set_step( 2 );
|
2020-10-20 17:24:41 +02:00
|
|
|
if( ! invalidate_key( invalidate_method, id ) )
|
2019-05-28 15:06:43 +02:00
|
|
|
goto exit;
|
2020-10-20 17:24:41 +02:00
|
|
|
if( ! invalidate_psa( invalidate_method ) )
|
2019-05-28 15:06:43 +02:00
|
|
|
goto exit;
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
/* Try to reaccess the key. If we destroyed it, check that it doesn't
|
2019-05-13 14:24:15 +02:00
|
|
|
* exist. Otherwise check that it still exists and has the expected
|
|
|
|
* content. */
|
2020-10-20 17:24:41 +02:00
|
|
|
switch( invalidate_method )
|
2018-11-30 18:53:28 +01:00
|
|
|
{
|
2020-10-20 17:24:41 +02:00
|
|
|
case INVALIDATE_BY_CLOSING:
|
|
|
|
case INVALIDATE_BY_CLOSING_WITH_SHUTDOWN:
|
2020-10-20 17:50:24 +02:00
|
|
|
case INVALIDATE_BY_PURGING:
|
|
|
|
case INVALIDATE_BY_PURGING_WITH_SHUTDOWN:
|
2020-10-20 17:24:41 +02:00
|
|
|
case INVALIDATE_BY_SHUTDOWN:
|
2019-05-06 18:44:55 +02:00
|
|
|
PSA_ASSERT( psa_open_key( id, &handle ) );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( id, &read_attributes ) );
|
2019-05-13 14:24:15 +02:00
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ),
|
|
|
|
psa_get_key_lifetime( &read_attributes ) );
|
2020-07-23 17:13:42 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal(
|
|
|
|
psa_get_key_id( &attributes ),
|
|
|
|
psa_get_key_id( &read_attributes ) ) );
|
2021-05-13 12:05:01 +02:00
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
|
2021-06-28 14:05:00 +02:00
|
|
|
mbedtls_test_update_key_usage_flags( usage_flags ) );
|
2019-05-13 14:24:15 +02:00
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ),
|
|
|
|
psa_get_key_algorithm( &read_attributes ) );
|
2019-05-13 14:32:11 +02:00
|
|
|
TEST_EQUAL( psa_get_key_enrollment_algorithm( &attributes ),
|
|
|
|
psa_get_key_enrollment_algorithm( &read_attributes ) );
|
2019-05-13 14:24:15 +02:00
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ),
|
|
|
|
psa_get_key_type( &read_attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ),
|
|
|
|
psa_get_key_bits( &read_attributes ) );
|
2021-01-18 13:32:28 +01:00
|
|
|
ASSERT_ALLOC( reexported, key_data->len );
|
2019-05-13 14:24:15 +02:00
|
|
|
if( usage_flags & PSA_KEY_USAGE_EXPORT )
|
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_export_key( id, reexported, key_data->len,
|
2019-05-13 14:24:15 +02:00
|
|
|
&reexported_length ) );
|
|
|
|
ASSERT_COMPARE( key_data->x, key_data->len,
|
|
|
|
reexported, reexported_length );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-18 13:32:28 +01:00
|
|
|
TEST_EQUAL( psa_export_key( id, reexported,
|
|
|
|
key_data->len, &reexported_length ),
|
2019-05-13 14:24:15 +02:00
|
|
|
PSA_ERROR_NOT_PERMITTED );
|
|
|
|
}
|
2019-05-28 14:08:50 +02:00
|
|
|
PSA_ASSERT( psa_close_key( handle ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
break;
|
2019-05-28 15:06:43 +02:00
|
|
|
|
2020-10-20 17:24:41 +02:00
|
|
|
case INVALIDATE_BY_DESTROYING:
|
|
|
|
case INVALIDATE_BY_DESTROYING_WITH_SHUTDOWN:
|
2020-08-04 14:58:35 +02:00
|
|
|
/*
|
|
|
|
* Test that the key handle and identifier are now not refering to an
|
|
|
|
* existing key.
|
|
|
|
*/
|
|
|
|
TEST_EQUAL( psa_get_key_attributes( handle, &read_attributes ),
|
2021-03-17 17:11:05 +01:00
|
|
|
PSA_ERROR_INVALID_HANDLE );
|
2021-03-17 16:05:13 +01:00
|
|
|
TEST_EQUAL( psa_close_key( handle ), PSA_ERROR_INVALID_HANDLE );
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_get_key_attributes( id, &read_attributes ),
|
2021-03-17 17:11:05 +01:00
|
|
|
PSA_ERROR_INVALID_HANDLE );
|
2018-11-30 18:53:28 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
psa_reset_key_attributes( &read_attributes );
|
|
|
|
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2019-05-13 14:24:15 +02:00
|
|
|
mbedtls_free( reexported );
|
2018-11-30 18:53:28 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-12-03 18:19:39 +01:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
2020-07-28 15:06:14 +02:00
|
|
|
void create_existent( int lifetime_arg, int owner_id_arg, int id_arg,
|
2018-11-30 18:53:28 +01:00
|
|
|
int reopen_policy_arg )
|
|
|
|
{
|
|
|
|
psa_key_lifetime_t lifetime = lifetime_arg;
|
2020-07-28 15:06:14 +02:00
|
|
|
mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( owner_id_arg, id_arg );
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t returned_id = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-11-30 18:53:28 +01:00
|
|
|
psa_key_type_t type1 = PSA_KEY_TYPE_RAW_DATA;
|
2019-04-19 18:20:30 +02:00
|
|
|
const uint8_t material1[5] = "a key";
|
|
|
|
const uint8_t material2[5] = "b key";
|
2018-11-30 18:53:28 +01:00
|
|
|
size_t bits1 = PSA_BYTES_TO_BITS( sizeof( material1 ) );
|
|
|
|
uint8_t reexported[sizeof( material1 )];
|
|
|
|
size_t reexported_length;
|
|
|
|
reopen_policy_t reopen_policy = reopen_policy_arg;
|
|
|
|
|
2019-05-06 18:39:37 +02:00
|
|
|
TEST_USES_KEY_ID( id );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
/* Create a key. */
|
2019-05-15 16:12:22 +02:00
|
|
|
psa_set_key_id( &attributes, id );
|
|
|
|
psa_set_key_lifetime( &attributes, lifetime );
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_set_key_type( &attributes, type1 );
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_algorithm( &attributes, 0 );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, material1, sizeof( material1 ),
|
2020-08-04 14:58:35 +02:00
|
|
|
&returned_id ) );
|
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal( id, returned_id ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
if( reopen_policy == CLOSE_BEFORE )
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_close_key( id ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
/* Attempt to create a new key in the same slot. */
|
2019-05-15 20:22:09 +02:00
|
|
|
TEST_EQUAL( psa_import_key( &attributes, material2, sizeof( material2 ),
|
2020-08-04 14:58:35 +02:00
|
|
|
&returned_id ),
|
2019-02-14 12:48:10 +01:00
|
|
|
PSA_ERROR_ALREADY_EXISTS );
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_is_null( returned_id ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
if( reopen_policy == CLOSE_AFTER )
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_close_key( id ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
/* Check that the original key hasn't changed. */
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( id, &attributes ) );
|
2020-07-23 17:13:42 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal(
|
|
|
|
psa_get_key_id( &attributes ), id ) );
|
2019-04-19 18:20:30 +02:00
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ), lifetime );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type1 );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ), bits1 );
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ), PSA_KEY_USAGE_EXPORT );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_export_key( id,
|
2018-11-30 18:53:28 +01:00
|
|
|
reexported, sizeof( reexported ),
|
|
|
|
&reexported_length ) );
|
|
|
|
ASSERT_COMPARE( material1, sizeof( material1 ),
|
|
|
|
reexported, reexported_length );
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_close_key( id ) );
|
2019-05-28 14:08:50 +02:00
|
|
|
|
2018-11-30 18:53:28 +01:00
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-11-30 18:53:28 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2019-05-06 18:44:55 +02:00
|
|
|
void open_fail( int id_arg,
|
2018-11-30 18:53:28 +01:00
|
|
|
int expected_status_arg )
|
|
|
|
{
|
2020-08-28 19:01:50 +02:00
|
|
|
mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( 1, id_arg );
|
2018-11-30 18:53:28 +01:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-07-31 11:26:37 +02:00
|
|
|
psa_key_handle_t handle = mbedtls_svc_key_id_make( 0xdead, 0xdead );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2019-05-06 18:44:55 +02:00
|
|
|
TEST_EQUAL( psa_open_key( id, &handle ), expected_status );
|
2020-09-01 10:51:51 +02:00
|
|
|
TEST_ASSERT( psa_key_handle_is_null( handle ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
exit:
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-11-30 18:53:28 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void create_fail( int lifetime_arg, int id_arg,
|
|
|
|
int expected_status_arg )
|
|
|
|
{
|
|
|
|
psa_key_lifetime_t lifetime = lifetime_arg;
|
2020-08-28 19:01:50 +02:00
|
|
|
mbedtls_svc_key_id_t id = mbedtls_svc_key_id_make( 1, id_arg );
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-11-30 18:53:28 +01:00
|
|
|
psa_status_t expected_status = expected_status_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t returned_id =
|
|
|
|
mbedtls_svc_key_id_make( 0xdead, 0xdead );
|
2019-04-19 18:20:30 +02:00
|
|
|
uint8_t material[1] = {'k'};
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2019-05-06 18:39:37 +02:00
|
|
|
TEST_USES_KEY_ID( id );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
2019-05-15 16:12:22 +02:00
|
|
|
psa_set_key_lifetime( &attributes, lifetime );
|
2020-10-23 17:11:13 +02:00
|
|
|
if( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Not possible to set a key identifier different from 0 through
|
|
|
|
* PSA key attributes APIs thus accessing to the attributes
|
|
|
|
* directly.
|
|
|
|
*/
|
|
|
|
attributes.core.id = id;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
psa_set_key_id( &attributes, id );
|
|
|
|
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
|
2019-05-15 20:22:09 +02:00
|
|
|
TEST_EQUAL( psa_import_key( &attributes, material, sizeof( material ),
|
2020-08-04 14:58:35 +02:00
|
|
|
&returned_id ),
|
2018-12-18 00:33:25 +01:00
|
|
|
expected_status );
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_is_null( returned_id ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
exit:
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2019-01-19 13:40:11 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2020-07-28 15:06:14 +02:00
|
|
|
void copy_across_lifetimes( int source_lifetime_arg, int source_owner_id_arg,
|
|
|
|
int source_id_arg, int source_usage_arg,
|
2019-05-20 17:28:11 +02:00
|
|
|
int source_alg_arg, int source_alg2_arg,
|
2019-01-19 13:40:11 +01:00
|
|
|
int type_arg, data_t *material,
|
2020-07-28 15:06:14 +02:00
|
|
|
int target_lifetime_arg, int target_owner_id_arg,
|
|
|
|
int target_id_arg, int target_usage_arg,
|
2019-05-20 17:28:11 +02:00
|
|
|
int target_alg_arg, int target_alg2_arg,
|
|
|
|
int expected_usage_arg,
|
|
|
|
int expected_alg_arg, int expected_alg2_arg )
|
2019-01-19 13:40:11 +01:00
|
|
|
{
|
|
|
|
psa_key_lifetime_t source_lifetime = source_lifetime_arg;
|
2020-08-28 19:01:50 +02:00
|
|
|
mbedtls_svc_key_id_t source_id =
|
2020-07-28 15:06:14 +02:00
|
|
|
mbedtls_svc_key_id_make( source_owner_id_arg, source_id_arg );
|
2019-01-19 13:40:11 +01:00
|
|
|
psa_key_usage_t source_usage = source_usage_arg;
|
|
|
|
psa_algorithm_t source_alg = source_alg_arg;
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_key_attributes_t source_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2019-01-19 13:40:11 +01:00
|
|
|
psa_key_type_t source_type = type_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t returned_source_id = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-01-19 13:40:11 +01:00
|
|
|
psa_key_lifetime_t target_lifetime = target_lifetime_arg;
|
2020-08-28 19:01:50 +02:00
|
|
|
mbedtls_svc_key_id_t target_id =
|
2020-07-28 15:06:14 +02:00
|
|
|
mbedtls_svc_key_id_make( target_owner_id_arg, target_id_arg );
|
2019-01-19 13:40:11 +01:00
|
|
|
psa_key_usage_t target_usage = target_usage_arg;
|
|
|
|
psa_algorithm_t target_alg = target_alg_arg;
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_key_attributes_t target_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t returned_target_id = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
psa_key_handle_t target_handle = PSA_KEY_HANDLE_INIT;
|
2019-01-19 13:40:11 +01:00
|
|
|
psa_key_usage_t expected_usage = expected_usage_arg;
|
|
|
|
psa_algorithm_t expected_alg = expected_alg_arg;
|
2019-05-20 17:28:11 +02:00
|
|
|
psa_algorithm_t expected_alg2 = expected_alg2_arg;
|
2019-01-19 13:40:11 +01:00
|
|
|
uint8_t *export_buffer = NULL;
|
|
|
|
|
2019-05-06 18:39:37 +02:00
|
|
|
TEST_USES_KEY_ID( source_id );
|
|
|
|
TEST_USES_KEY_ID( target_id );
|
2019-01-19 13:40:11 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
/* Populate the source slot. */
|
2020-07-23 17:13:42 +02:00
|
|
|
psa_set_key_id( &source_attributes, source_id );
|
|
|
|
psa_set_key_lifetime( &source_attributes, source_lifetime );
|
|
|
|
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_set_key_type( &source_attributes, source_type );
|
|
|
|
psa_set_key_usage_flags( &source_attributes, source_usage );
|
|
|
|
psa_set_key_algorithm( &source_attributes, source_alg );
|
2019-05-20 17:28:11 +02:00
|
|
|
psa_set_key_enrollment_algorithm( &source_attributes, source_alg2_arg );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &source_attributes,
|
|
|
|
material->x, material->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&returned_source_id ) );
|
2019-04-19 18:20:30 +02:00
|
|
|
/* Update the attributes with the bit size. */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( returned_source_id,
|
|
|
|
&source_attributes ) );
|
2019-01-19 13:40:11 +01:00
|
|
|
|
|
|
|
/* Prepare the target slot. */
|
2020-07-23 17:13:42 +02:00
|
|
|
psa_set_key_id( &target_attributes, target_id );
|
|
|
|
psa_set_key_lifetime( &target_attributes, target_lifetime );
|
|
|
|
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_set_key_usage_flags( &target_attributes, target_usage );
|
|
|
|
psa_set_key_algorithm( &target_attributes, target_alg );
|
2019-05-20 17:28:11 +02:00
|
|
|
psa_set_key_enrollment_algorithm( &target_attributes, target_alg2_arg );
|
2019-01-19 13:40:11 +01:00
|
|
|
|
|
|
|
/* Copy the key. */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_copy_key( returned_source_id,
|
|
|
|
&target_attributes, &returned_target_id ) );
|
2019-01-19 13:40:11 +01:00
|
|
|
|
|
|
|
/* Destroy the source to ensure that this doesn't affect the target. */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( returned_source_id ) );
|
2019-01-19 13:40:11 +01:00
|
|
|
|
|
|
|
/* If the target key is persistent, restart the system to make
|
|
|
|
* sure that the material is still alive. */
|
2020-10-19 08:44:19 +02:00
|
|
|
if( ! PSA_KEY_LIFETIME_IS_VOLATILE( target_lifetime ) )
|
2019-01-19 13:40:11 +01:00
|
|
|
{
|
2019-05-28 14:08:50 +02:00
|
|
|
mbedtls_psa_crypto_free( );
|
2019-01-19 13:40:11 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2019-05-06 18:44:55 +02:00
|
|
|
PSA_ASSERT( psa_open_key( target_id, &target_handle ) );
|
2019-01-19 13:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Test that the target slot has the expected content. */
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_reset_key_attributes( &target_attributes );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( returned_target_id,
|
|
|
|
&target_attributes ) );
|
2020-07-23 17:13:42 +02:00
|
|
|
|
2020-10-19 08:44:19 +02:00
|
|
|
if( ! PSA_KEY_LIFETIME_IS_VOLATILE( target_lifetime ) )
|
2020-07-23 17:13:42 +02:00
|
|
|
{
|
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal(
|
|
|
|
target_id, psa_get_key_id( &target_attributes ) ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( MBEDTLS_SVC_KEY_ID_GET_OWNER_ID( returned_target_id ),
|
2020-07-28 15:06:14 +02:00
|
|
|
target_owner_id_arg );
|
2020-07-23 17:13:42 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-04-19 18:20:30 +02:00
|
|
|
TEST_EQUAL( target_lifetime, psa_get_key_lifetime( &target_attributes ) );
|
|
|
|
TEST_EQUAL( source_type, psa_get_key_type( &target_attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &source_attributes ),
|
|
|
|
psa_get_key_bits( &target_attributes ) );
|
|
|
|
TEST_EQUAL( expected_usage, psa_get_key_usage_flags( &target_attributes ) );
|
|
|
|
TEST_EQUAL( expected_alg, psa_get_key_algorithm( &target_attributes ) );
|
2019-05-20 17:28:11 +02:00
|
|
|
TEST_EQUAL( expected_alg2,
|
|
|
|
psa_get_key_enrollment_algorithm( &target_attributes ) );
|
2019-01-19 13:40:11 +01:00
|
|
|
if( expected_usage & PSA_KEY_USAGE_EXPORT )
|
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
ASSERT_ALLOC( export_buffer, material->len );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_export_key( returned_target_id, export_buffer,
|
2019-01-19 13:40:11 +01:00
|
|
|
material->len, &length ) );
|
|
|
|
ASSERT_COMPARE( material->x, material->len,
|
|
|
|
export_buffer, length );
|
|
|
|
}
|
2019-01-28 13:03:09 +01:00
|
|
|
else
|
2019-04-19 18:20:30 +02:00
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
/* Check that the key is actually non-exportable. */
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_export_key( returned_target_id, export_buffer,
|
2019-04-19 18:20:30 +02:00
|
|
|
material->len, &length ),
|
|
|
|
PSA_ERROR_NOT_PERMITTED );
|
|
|
|
}
|
2019-01-28 13:03:09 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( returned_target_id ) );
|
2019-05-28 14:08:50 +02:00
|
|
|
|
2019-01-28 13:03:09 +01:00
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Source and target key attributes may have been returned by
|
|
|
|
* psa_get_key_attributes() thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &source_attributes );
|
|
|
|
psa_reset_key_attributes( &target_attributes );
|
|
|
|
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2019-04-19 18:20:30 +02:00
|
|
|
mbedtls_free( export_buffer );
|
2019-01-28 13:03:09 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void copy_to_occupied( int source_lifetime_arg, int source_id_arg,
|
|
|
|
int source_usage_arg, int source_alg_arg,
|
|
|
|
int source_type_arg, data_t *source_material,
|
|
|
|
int target_lifetime_arg, int target_id_arg,
|
|
|
|
int target_usage_arg, int target_alg_arg,
|
|
|
|
int target_type_arg, data_t *target_material )
|
|
|
|
{
|
|
|
|
psa_key_lifetime_t source_lifetime = source_lifetime_arg;
|
2020-08-28 19:01:50 +02:00
|
|
|
mbedtls_svc_key_id_t source_id =
|
|
|
|
mbedtls_svc_key_id_make( 1, source_id_arg );
|
2019-01-28 13:03:09 +01:00
|
|
|
psa_key_usage_t source_usage = source_usage_arg;
|
|
|
|
psa_algorithm_t source_alg = source_alg_arg;
|
|
|
|
psa_key_type_t source_type = source_type_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t returned_source_id = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-01-28 13:03:09 +01:00
|
|
|
psa_key_lifetime_t target_lifetime = target_lifetime_arg;
|
2020-08-28 19:01:50 +02:00
|
|
|
mbedtls_svc_key_id_t target_id =
|
|
|
|
mbedtls_svc_key_id_make( 1, target_id_arg );
|
2019-01-28 13:03:09 +01:00
|
|
|
psa_key_usage_t target_usage = target_usage_arg;
|
|
|
|
psa_algorithm_t target_alg = target_alg_arg;
|
|
|
|
psa_key_type_t target_type = target_type_arg;
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t returned_target_id = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t new_key = MBEDTLS_SVC_KEY_ID_INIT;
|
2019-01-28 13:03:09 +01:00
|
|
|
uint8_t *export_buffer = NULL;
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t attributes1 = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
psa_key_attributes_t attributes2 = PSA_KEY_ATTRIBUTES_INIT;
|
2019-01-28 13:03:09 +01:00
|
|
|
|
2019-05-06 18:39:37 +02:00
|
|
|
TEST_USES_KEY_ID( source_id );
|
|
|
|
TEST_USES_KEY_ID( target_id );
|
2019-01-28 13:03:09 +01:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
/* Populate the source slot. */
|
2020-10-19 08:44:19 +02:00
|
|
|
if( ! PSA_KEY_LIFETIME_IS_VOLATILE( source_lifetime ) )
|
2019-05-15 16:12:22 +02:00
|
|
|
{
|
|
|
|
psa_set_key_id( &attributes, source_id );
|
|
|
|
psa_set_key_lifetime( &attributes, source_lifetime );
|
|
|
|
}
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_set_key_type( &attributes, source_type );
|
|
|
|
psa_set_key_usage_flags( &attributes, source_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, source_alg );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
source_material->x, source_material->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&returned_source_id ) );
|
2019-01-28 13:03:09 +01:00
|
|
|
|
|
|
|
/* Populate the target slot. */
|
2020-07-23 17:13:42 +02:00
|
|
|
if( mbedtls_svc_key_id_equal( target_id, source_id ) )
|
2019-04-19 18:20:30 +02:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
returned_target_id = returned_source_id;
|
2019-04-19 18:20:30 +02:00
|
|
|
}
|
2019-01-28 13:03:09 +01:00
|
|
|
else
|
2019-04-19 18:20:30 +02:00
|
|
|
{
|
2019-05-15 16:12:22 +02:00
|
|
|
psa_set_key_id( &attributes1, target_id );
|
|
|
|
psa_set_key_lifetime( &attributes1, target_lifetime );
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_set_key_type( &attributes1, target_type );
|
|
|
|
psa_set_key_usage_flags( &attributes1, target_usage );
|
|
|
|
psa_set_key_algorithm( &attributes1, target_alg );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes1,
|
|
|
|
target_material->x, target_material->len,
|
2020-08-04 14:58:35 +02:00
|
|
|
&returned_target_id ) );
|
2019-04-19 18:20:30 +02:00
|
|
|
}
|
2020-08-04 14:58:35 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( returned_target_id, &attributes1 ) );
|
2019-01-28 13:03:09 +01:00
|
|
|
|
2019-04-19 18:20:30 +02:00
|
|
|
/* Make a copy attempt. */
|
2019-05-15 16:12:22 +02:00
|
|
|
psa_set_key_id( &attributes, target_id );
|
|
|
|
psa_set_key_lifetime( &attributes, target_lifetime );
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_EQUAL( psa_copy_key( returned_source_id,
|
|
|
|
&attributes, &new_key ),
|
2019-02-14 12:48:10 +01:00
|
|
|
PSA_ERROR_ALREADY_EXISTS );
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_is_null( new_key ) );
|
2019-01-28 13:03:09 +01:00
|
|
|
|
|
|
|
/* Test that the target slot is unaffected. */
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( returned_target_id, &attributes2 ) );
|
2020-07-23 17:13:42 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal(
|
|
|
|
psa_get_key_id( &attributes1 ),
|
|
|
|
psa_get_key_id( &attributes2 ) ) );
|
2019-04-19 18:20:30 +02:00
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes1 ),
|
|
|
|
psa_get_key_lifetime( &attributes2 ) );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes1 ),
|
|
|
|
psa_get_key_type( &attributes2 ) );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes1 ),
|
|
|
|
psa_get_key_bits( &attributes2 ) );
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes1 ),
|
|
|
|
psa_get_key_usage_flags( &attributes2 ) );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes1 ),
|
|
|
|
psa_get_key_algorithm( &attributes2 ) );
|
2019-01-28 13:03:09 +01:00
|
|
|
if( target_usage & PSA_KEY_USAGE_EXPORT )
|
|
|
|
{
|
|
|
|
size_t length;
|
|
|
|
ASSERT_ALLOC( export_buffer, target_material->len );
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_export_key( returned_target_id, export_buffer,
|
2019-01-28 13:03:09 +01:00
|
|
|
target_material->len, &length ) );
|
|
|
|
ASSERT_COMPARE( target_material->x, target_material->len,
|
|
|
|
export_buffer, length );
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_destroy_key( returned_source_id ) );
|
|
|
|
if( ! mbedtls_svc_key_id_equal( target_id, source_id ) )
|
|
|
|
PSA_ASSERT( psa_destroy_key( returned_target_id ) );
|
2019-05-28 14:08:50 +02:00
|
|
|
|
2019-01-28 13:03:09 +01:00
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes1 );
|
|
|
|
psa_reset_key_attributes( &attributes2 );
|
|
|
|
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2019-01-28 13:03:09 +01:00
|
|
|
mbedtls_free( export_buffer );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2018-11-30 18:53:28 +01:00
|
|
|
/* BEGIN_CASE */
|
2019-10-11 11:44:48 +02:00
|
|
|
void invalid_handle( int handle_construction,
|
2021-03-17 17:11:05 +01:00
|
|
|
int close_status_arg )
|
2018-11-30 18:53:28 +01:00
|
|
|
{
|
2020-07-30 17:48:03 +02:00
|
|
|
psa_key_handle_t valid_handle = PSA_KEY_HANDLE_INIT;
|
|
|
|
psa_key_handle_t invalid_handle = PSA_KEY_HANDLE_INIT;
|
2020-07-31 11:26:37 +02:00
|
|
|
psa_key_id_t key_id;
|
2019-10-11 11:44:48 +02:00
|
|
|
psa_status_t close_status = close_status_arg;
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-11-30 18:53:28 +01:00
|
|
|
uint8_t material[1] = "a";
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
/* Allocate a handle and store a key in it. */
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
|
|
|
|
psa_set_key_usage_flags( &attributes, 0 );
|
|
|
|
psa_set_key_algorithm( &attributes, 0 );
|
2019-05-15 20:22:09 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
material, sizeof( material ),
|
2019-10-11 11:44:48 +02:00
|
|
|
&valid_handle ) );
|
2020-09-01 10:51:51 +02:00
|
|
|
TEST_ASSERT( ! psa_key_handle_is_null( valid_handle ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2019-10-11 11:44:48 +02:00
|
|
|
/* Construct an invalid handle as specified in the test case data. */
|
|
|
|
switch( handle_construction )
|
2019-10-09 16:23:49 +02:00
|
|
|
{
|
2019-10-11 11:44:48 +02:00
|
|
|
case INVALID_HANDLE_0:
|
2020-07-30 17:48:03 +02:00
|
|
|
invalid_handle = PSA_KEY_HANDLE_INIT;
|
2019-10-11 11:44:48 +02:00
|
|
|
break;
|
|
|
|
case INVALID_HANDLE_UNOPENED:
|
2020-07-31 11:26:37 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MBEDTLS_SVC_KEY_ID_GET_KEY_ID( valid_handle ) is a volatile
|
|
|
|
* key identifier as the imported key is a volatile key. Volatile
|
|
|
|
* key identifiers are in the range from PSA_KEY_ID_VOLATILE_MIN
|
|
|
|
* to PSA_KEY_ID_VOLATILE_MAX included. Thus pick a key identifier
|
|
|
|
* in the range from PSA_KEY_ID_VOLATILE_MIN to
|
|
|
|
* PSA_KEY_ID_VOLATILE_MAX different from
|
|
|
|
* MBEDTLS_SVC_KEY_ID_GET_KEY_ID( valid_handle ) to build an
|
|
|
|
* unopened and thus invalid identifier.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( valid_handle ) ==
|
|
|
|
PSA_KEY_ID_VOLATILE_MIN )
|
|
|
|
key_id = PSA_KEY_ID_VOLATILE_MIN + 1;
|
2019-10-11 11:44:48 +02:00
|
|
|
else
|
2020-07-31 11:26:37 +02:00
|
|
|
key_id = MBEDTLS_SVC_KEY_ID_GET_KEY_ID( valid_handle ) - 1;
|
|
|
|
|
|
|
|
invalid_handle =
|
|
|
|
mbedtls_svc_key_id_make( 0, key_id );
|
2019-10-11 11:44:48 +02:00
|
|
|
break;
|
|
|
|
case INVALID_HANDLE_CLOSED:
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
material, sizeof( material ),
|
|
|
|
&invalid_handle ) );
|
|
|
|
PSA_ASSERT( psa_destroy_key( invalid_handle ) );
|
|
|
|
break;
|
|
|
|
case INVALID_HANDLE_HUGE:
|
2020-07-31 11:26:37 +02:00
|
|
|
invalid_handle =
|
|
|
|
mbedtls_svc_key_id_make( 0, PSA_KEY_ID_VENDOR_MAX + 1 );
|
2019-10-11 11:44:48 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TEST_ASSERT( ! "unknown handle construction" );
|
2019-10-09 16:23:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 11:44:48 +02:00
|
|
|
/* Attempt to use the invalid handle. */
|
|
|
|
TEST_EQUAL( psa_get_key_attributes( invalid_handle, &attributes ),
|
2021-03-17 17:11:05 +01:00
|
|
|
PSA_ERROR_INVALID_HANDLE );
|
2019-10-11 11:44:48 +02:00
|
|
|
TEST_EQUAL( psa_close_key( invalid_handle ), close_status );
|
|
|
|
TEST_EQUAL( psa_destroy_key( invalid_handle ), close_status );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
/* After all this, check that the original handle is intact. */
|
2019-10-11 11:44:48 +02:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( valid_handle, &attributes ) );
|
2019-05-27 14:53:07 +02:00
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), PSA_KEY_TYPE_RAW_DATA );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ),
|
|
|
|
PSA_BYTES_TO_BITS( sizeof( material ) ) );
|
2019-10-11 11:44:48 +02:00
|
|
|
PSA_ASSERT( psa_close_key( valid_handle ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2018-11-30 18:53:28 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2020-08-04 14:58:35 +02:00
|
|
|
void many_transient_keys( int max_keys_arg )
|
2018-11-30 18:53:28 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_svc_key_id_t *keys = NULL;
|
|
|
|
size_t max_keys = max_keys_arg;
|
2018-11-30 18:53:28 +01:00
|
|
|
size_t i, j;
|
|
|
|
psa_status_t status;
|
2019-04-19 18:20:30 +02:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-11-30 18:53:28 +01:00
|
|
|
uint8_t exported[sizeof( size_t )];
|
|
|
|
size_t exported_length;
|
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
ASSERT_ALLOC( keys, max_keys );
|
2018-11-30 18:53:28 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2019-04-19 18:20:30 +02:00
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_algorithm( &attributes, 0 );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
for( i = 0; i < max_keys; i++ )
|
2018-11-30 18:53:28 +01:00
|
|
|
{
|
2019-05-15 20:15:10 +02:00
|
|
|
status = psa_import_key( &attributes,
|
|
|
|
(uint8_t *) &i, sizeof( i ),
|
2020-08-04 14:58:35 +02:00
|
|
|
&keys[i] );
|
2018-11-30 18:53:28 +01:00
|
|
|
if( status == PSA_ERROR_INSUFFICIENT_MEMORY )
|
|
|
|
break;
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( status );
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_ASSERT( ! mbedtls_svc_key_id_is_null( keys[i] ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
for( j = 0; j < i; j++ )
|
2020-08-04 14:58:35 +02:00
|
|
|
TEST_ASSERT( ! mbedtls_svc_key_id_equal( keys[i], keys[j] ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
}
|
2020-08-04 14:58:35 +02:00
|
|
|
max_keys = i;
|
2018-11-30 18:53:28 +01:00
|
|
|
|
2020-08-04 14:58:35 +02:00
|
|
|
for( i = 1; i < max_keys; i++ )
|
2018-11-30 18:53:28 +01:00
|
|
|
{
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_close_key( keys[i - 1] ) );
|
|
|
|
PSA_ASSERT( psa_export_key( keys[i],
|
2018-11-30 18:53:28 +01:00
|
|
|
exported, sizeof( exported ),
|
|
|
|
&exported_length ) );
|
|
|
|
ASSERT_COMPARE( exported, exported_length,
|
|
|
|
(uint8_t *) &i, sizeof( i ) );
|
|
|
|
}
|
2020-08-04 14:58:35 +02:00
|
|
|
PSA_ASSERT( psa_close_key( keys[i - 1] ) );
|
2018-11-30 18:53:28 +01:00
|
|
|
|
|
|
|
exit:
|
2019-05-28 15:10:21 +02:00
|
|
|
PSA_DONE( );
|
2020-08-04 14:58:35 +02:00
|
|
|
mbedtls_free( keys );
|
2018-11-30 18:53:28 +01:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
2020-10-21 09:04:34 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
|
|
|
void key_slot_eviction_to_import_new_key( int lifetime_arg )
|
|
|
|
{
|
|
|
|
psa_key_lifetime_t lifetime = (psa_key_lifetime_t)lifetime_arg;
|
|
|
|
size_t i;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
uint8_t exported[sizeof( size_t )];
|
|
|
|
size_t exported_length;
|
|
|
|
mbedtls_svc_key_id_t key, returned_key_id;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
|
|
|
|
psa_set_key_algorithm( &attributes, 0 );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
|
|
|
|
|
|
|
|
/*
|
2021-02-15 14:03:19 +01:00
|
|
|
* Create MBEDTLS_PSA_KEY_SLOT_COUNT persistent keys.
|
2020-10-21 09:04:34 +02:00
|
|
|
*/
|
2021-02-15 14:03:19 +01:00
|
|
|
for( i = 0; i < MBEDTLS_PSA_KEY_SLOT_COUNT; i++ )
|
2020-10-21 09:04:34 +02:00
|
|
|
{
|
|
|
|
key = mbedtls_svc_key_id_make( i, i + 1 );
|
|
|
|
psa_set_key_id( &attributes, key );
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
(uint8_t *) &i, sizeof( i ),
|
|
|
|
&returned_key_id ) );
|
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal( returned_key_id, key ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new persistent or volatile key. When creating the key,
|
2020-11-18 17:21:22 +01:00
|
|
|
* one of the descriptions of the previously created persistent keys
|
2020-10-21 09:04:34 +02:00
|
|
|
* is removed from the RAM key slots. This makes room to store its
|
|
|
|
* description in RAM.
|
|
|
|
*/
|
2021-02-15 14:03:19 +01:00
|
|
|
i = MBEDTLS_PSA_KEY_SLOT_COUNT;
|
2020-10-21 09:04:34 +02:00
|
|
|
key = mbedtls_svc_key_id_make( i, i + 1 );
|
|
|
|
psa_set_key_id( &attributes, key );
|
2020-11-14 14:19:11 +01:00
|
|
|
psa_set_key_lifetime( &attributes, lifetime );
|
2020-10-21 09:04:34 +02:00
|
|
|
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
(uint8_t *) &i, sizeof( i ),
|
|
|
|
&returned_key_id ) );
|
|
|
|
if( lifetime != PSA_KEY_LIFETIME_VOLATILE )
|
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal( returned_key_id, key ) );
|
2020-11-14 14:19:11 +01:00
|
|
|
else
|
|
|
|
TEST_ASSERT( psa_key_id_is_volatile(
|
|
|
|
MBEDTLS_SVC_KEY_ID_GET_KEY_ID( returned_key_id ) ) );
|
2020-10-21 09:04:34 +02:00
|
|
|
|
|
|
|
/*
|
2021-02-15 14:03:19 +01:00
|
|
|
* Check that we can export all ( MBEDTLS_PSA_KEY_SLOT_COUNT + 1 ) keys,
|
2020-10-21 09:04:34 +02:00
|
|
|
* that they have the expected value and destroy them. In that process,
|
|
|
|
* the description of the persistent key that was evicted from the RAM
|
|
|
|
* slots when creating the last key is restored in a RAM slot to export
|
|
|
|
* its value.
|
|
|
|
*/
|
2021-02-15 14:03:19 +01:00
|
|
|
for( i = 0; i <= MBEDTLS_PSA_KEY_SLOT_COUNT; i++ )
|
2020-10-21 09:04:34 +02:00
|
|
|
{
|
2021-02-15 14:03:19 +01:00
|
|
|
if( i < MBEDTLS_PSA_KEY_SLOT_COUNT )
|
2020-10-21 09:04:34 +02:00
|
|
|
key = mbedtls_svc_key_id_make( i, i + 1 );
|
|
|
|
else
|
|
|
|
key = returned_key_id;
|
|
|
|
|
|
|
|
PSA_ASSERT( psa_export_key( key,
|
|
|
|
exported, sizeof( exported ),
|
|
|
|
&exported_length ) );
|
|
|
|
ASSERT_COMPARE( exported, exported_length,
|
|
|
|
(uint8_t *) &i, sizeof( i ) );
|
|
|
|
PSA_ASSERT( psa_destroy_key( key ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
PSA_DONE( );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PSA_CRYPTO_STORAGE_C */
|
|
|
|
void non_reusable_key_slots_integrity_in_case_of_key_slot_starvation( )
|
|
|
|
{
|
|
|
|
psa_status_t status;
|
|
|
|
size_t i;
|
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
|
|
|
uint8_t exported[sizeof( size_t )];
|
|
|
|
size_t exported_length;
|
2020-11-10 18:08:03 +01:00
|
|
|
mbedtls_svc_key_id_t persistent_key = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t persistent_key2 = MBEDTLS_SVC_KEY_ID_INIT;
|
2020-10-21 09:04:34 +02:00
|
|
|
mbedtls_svc_key_id_t returned_key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
mbedtls_svc_key_id_t *keys = NULL;
|
|
|
|
|
2021-02-15 14:03:19 +01:00
|
|
|
TEST_ASSERT( MBEDTLS_PSA_KEY_SLOT_COUNT >= 1 );
|
2020-10-21 09:04:34 +02:00
|
|
|
|
2021-02-15 14:03:19 +01:00
|
|
|
ASSERT_ALLOC( keys, MBEDTLS_PSA_KEY_SLOT_COUNT );
|
2020-10-21 09:04:34 +02:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
|
|
|
|
|
|
|
psa_set_key_usage_flags( &attributes,
|
|
|
|
PSA_KEY_USAGE_EXPORT | PSA_KEY_USAGE_COPY );
|
|
|
|
psa_set_key_algorithm( &attributes, 0 );
|
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
|
|
|
|
|
|
|
|
/*
|
2020-11-10 18:08:03 +01:00
|
|
|
* Create a persistent key
|
2020-10-21 09:04:34 +02:00
|
|
|
*/
|
2020-11-10 18:08:03 +01:00
|
|
|
persistent_key = mbedtls_svc_key_id_make( 0x100, 0x205 );
|
|
|
|
psa_set_key_id( &attributes, persistent_key );
|
2020-10-21 09:04:34 +02:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
2020-11-10 18:08:03 +01:00
|
|
|
(uint8_t *) &persistent_key,
|
|
|
|
sizeof( persistent_key ),
|
2020-10-21 09:04:34 +02:00
|
|
|
&returned_key_id ) );
|
2020-11-10 18:08:03 +01:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal( returned_key_id, persistent_key ) );
|
2020-10-21 09:04:34 +02:00
|
|
|
|
|
|
|
/*
|
2021-02-15 14:03:19 +01:00
|
|
|
* Create MBEDTLS_PSA_KEY_SLOT_COUNT volatile keys
|
2020-10-21 09:04:34 +02:00
|
|
|
*/
|
|
|
|
psa_set_key_lifetime( &attributes, PSA_KEY_LIFETIME_VOLATILE );
|
2021-02-15 14:03:19 +01:00
|
|
|
for( i = 0; i < MBEDTLS_PSA_KEY_SLOT_COUNT; i++ )
|
2020-10-21 09:04:34 +02:00
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_import_key( &attributes,
|
|
|
|
(uint8_t *) &i, sizeof( i ),
|
|
|
|
&keys[i]) );
|
|
|
|
}
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we cannot access the persistent key as all slots are
|
|
|
|
* occupied by volatile keys and the implementation needs to load the
|
|
|
|
* persistent key description in a slot to be able to access it.
|
|
|
|
*/
|
2020-11-10 18:08:03 +01:00
|
|
|
status = psa_get_key_attributes( persistent_key, &attributes );
|
2020-10-21 09:04:34 +02:00
|
|
|
TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check we can export the volatile key created last and that it has the
|
|
|
|
* expected value. Then, destroy it.
|
|
|
|
*/
|
2021-02-15 14:03:19 +01:00
|
|
|
PSA_ASSERT( psa_export_key( keys[MBEDTLS_PSA_KEY_SLOT_COUNT - 1],
|
2020-10-21 09:04:34 +02:00
|
|
|
exported, sizeof( exported ),
|
|
|
|
&exported_length ) );
|
2021-02-15 14:03:19 +01:00
|
|
|
i = MBEDTLS_PSA_KEY_SLOT_COUNT - 1;
|
2020-10-21 09:04:34 +02:00
|
|
|
ASSERT_COMPARE( exported, exported_length, (uint8_t *) &i, sizeof( i ) );
|
2021-02-15 14:03:19 +01:00
|
|
|
PSA_ASSERT( psa_destroy_key( keys[MBEDTLS_PSA_KEY_SLOT_COUNT - 1] ) );
|
2020-10-21 09:04:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we can now access the persistent key again.
|
|
|
|
*/
|
2020-11-10 18:08:03 +01:00
|
|
|
PSA_ASSERT( psa_get_key_attributes( persistent_key, &attributes ) );
|
2020-10-21 09:04:34 +02:00
|
|
|
TEST_ASSERT( mbedtls_svc_key_id_equal( attributes.core.id,
|
2020-11-10 18:08:03 +01:00
|
|
|
persistent_key ) );
|
2020-10-21 09:04:34 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that we cannot copy the persistent key as all slots are occupied
|
2020-11-10 18:08:03 +01:00
|
|
|
* by the persistent key and the volatile keys and the slot containing the
|
|
|
|
* persistent key cannot be reclaimed as it contains the key to copy.
|
2020-10-21 09:04:34 +02:00
|
|
|
*/
|
2020-11-10 18:08:03 +01:00
|
|
|
persistent_key2 = mbedtls_svc_key_id_make( 0x100, 0x204 );
|
|
|
|
psa_set_key_id( &attributes, persistent_key2 );
|
|
|
|
status = psa_copy_key( persistent_key, &attributes, &returned_key_id );
|
2020-10-21 09:04:34 +02:00
|
|
|
TEST_EQUAL( status, PSA_ERROR_INSUFFICIENT_MEMORY );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check we can export the remaining volatile keys and that they have the
|
|
|
|
* expected values.
|
|
|
|
*/
|
2021-02-15 14:03:19 +01:00
|
|
|
for( i = 0; i < ( MBEDTLS_PSA_KEY_SLOT_COUNT - 1 ); i++ )
|
2020-10-21 09:04:34 +02:00
|
|
|
{
|
|
|
|
PSA_ASSERT( psa_export_key( keys[i],
|
|
|
|
exported, sizeof( exported ),
|
|
|
|
&exported_length ) );
|
|
|
|
ASSERT_COMPARE( exported, exported_length,
|
|
|
|
(uint8_t *) &i, sizeof( i ) );
|
|
|
|
PSA_ASSERT( psa_destroy_key( keys[i] ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check we can export the persistent key and that it have the expected
|
|
|
|
* value.
|
|
|
|
*/
|
|
|
|
|
2020-11-10 18:08:03 +01:00
|
|
|
PSA_ASSERT( psa_export_key( persistent_key, exported, sizeof( exported ),
|
2020-10-21 09:04:34 +02:00
|
|
|
&exported_length ) );
|
|
|
|
ASSERT_COMPARE( exported, exported_length,
|
2020-11-10 18:08:03 +01:00
|
|
|
(uint8_t *) &persistent_key, sizeof( persistent_key ) );
|
2020-10-21 09:04:34 +02:00
|
|
|
exit:
|
2020-11-19 17:55:23 +01:00
|
|
|
/*
|
|
|
|
* Key attributes may have been returned by psa_get_key_attributes()
|
|
|
|
* thus reset them as required.
|
|
|
|
*/
|
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
|
2020-11-10 18:08:03 +01:00
|
|
|
psa_destroy_key( persistent_key );
|
2020-10-21 09:04:34 +02:00
|
|
|
PSA_DONE( );
|
|
|
|
mbedtls_free( keys );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|