2018-06-18 16:27:26 +00:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include <stdint.h>
|
2019-05-23 18:25:48 +00:00
|
|
|
|
2019-06-20 10:40:56 +00:00
|
|
|
#include "psa_crypto_helpers.h"
|
2018-06-18 16:27:26 +00:00
|
|
|
#include "psa_crypto_storage.h"
|
2019-05-23 18:25:48 +00:00
|
|
|
|
2018-06-18 16:27:26 +00:00
|
|
|
#include "mbedtls/md.h"
|
|
|
|
|
|
|
|
#define PSA_KEY_STORAGE_MAGIC_HEADER "PSA\0KEY"
|
|
|
|
#define PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH ( sizeof( PSA_KEY_STORAGE_MAGIC_HEADER ) )
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint8_t magic[PSA_KEY_STORAGE_MAGIC_HEADER_LENGTH];
|
|
|
|
uint8_t version[4];
|
2019-07-23 12:46:52 +00:00
|
|
|
uint8_t lifetime[sizeof( psa_key_lifetime_t )];
|
2019-12-04 16:18:41 +00:00
|
|
|
uint8_t type[4];
|
2018-06-18 16:27:26 +00:00
|
|
|
uint8_t policy[sizeof( psa_key_policy_t )];
|
|
|
|
uint8_t data_len[4];
|
|
|
|
uint8_t key_data[];
|
|
|
|
} psa_persistent_key_storage_format;
|
2018-11-07 16:05:30 +00:00
|
|
|
|
2018-06-18 16:27:26 +00:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_PSA_CRYPTO_C:MBEDTLS_PSA_CRYPTO_STORAGE_C
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void format_storage_data_check( data_t *key_data,
|
|
|
|
data_t *expected_file_data,
|
2019-07-23 12:46:52 +00:00
|
|
|
int key_lifetime, int key_type,
|
2019-05-14 09:16:10 +00:00
|
|
|
int key_usage, int key_alg, int key_alg2 )
|
2018-06-18 16:27:26 +00:00
|
|
|
{
|
2019-12-02 19:50:16 +00:00
|
|
|
uint8_t *file_data = NULL;
|
|
|
|
size_t file_data_length =
|
|
|
|
key_data->len + sizeof( psa_persistent_key_storage_format );
|
2019-07-23 09:58:03 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-07-23 12:46:52 +00:00
|
|
|
psa_set_key_lifetime( &attributes, key_lifetime );
|
2019-07-23 09:58:03 +00:00
|
|
|
psa_set_key_type( &attributes, key_type );
|
|
|
|
psa_set_key_usage_flags( &attributes, key_usage );
|
|
|
|
psa_set_key_algorithm( &attributes, key_alg );
|
|
|
|
psa_set_key_enrollment_algorithm( &attributes, key_alg2 );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-12-02 19:50:16 +00:00
|
|
|
ASSERT_ALLOC( file_data, file_data_length );
|
2018-06-18 16:27:26 +00:00
|
|
|
psa_format_key_data_for_storage( key_data->x, key_data->len,
|
2019-07-30 18:22:33 +00:00
|
|
|
&attributes.core,
|
2018-06-18 16:27:26 +00:00
|
|
|
file_data );
|
|
|
|
|
|
|
|
ASSERT_COMPARE( expected_file_data->x, expected_file_data->len,
|
|
|
|
file_data, file_data_length );
|
2019-12-02 19:50:16 +00:00
|
|
|
|
|
|
|
exit:
|
2018-06-18 16:27:26 +00:00
|
|
|
mbedtls_free( file_data );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void parse_storage_data_check( data_t *file_data,
|
|
|
|
data_t *expected_key_data,
|
2019-07-23 12:46:52 +00:00
|
|
|
int expected_key_lifetime,
|
2018-06-18 16:27:26 +00:00
|
|
|
int expected_key_type,
|
|
|
|
int expected_key_usage,
|
|
|
|
int expected_key_alg,
|
2019-05-14 09:16:10 +00:00
|
|
|
int expected_key_alg2,
|
2018-06-18 16:27:26 +00:00
|
|
|
int expected_status )
|
|
|
|
{
|
|
|
|
uint8_t *key_data = NULL;
|
|
|
|
size_t key_data_length = 0;
|
2019-07-23 09:58:03 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-18 16:27:26 +00:00
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
status = psa_parse_key_data_from_storage( file_data->x, file_data->len,
|
|
|
|
&key_data, &key_data_length,
|
2019-07-30 18:22:33 +00:00
|
|
|
&attributes.core );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2018-12-17 23:24:04 +00:00
|
|
|
TEST_EQUAL( status, expected_status );
|
2018-06-18 16:27:26 +00:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2019-07-23 12:46:52 +00:00
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ),
|
|
|
|
(psa_key_type_t) expected_key_lifetime );
|
2019-07-23 09:58:03 +00:00
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ),
|
|
|
|
(psa_key_type_t) expected_key_type );
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ),
|
|
|
|
(uint32_t) expected_key_usage );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ),
|
|
|
|
(uint32_t) expected_key_alg );
|
|
|
|
TEST_EQUAL( psa_get_key_enrollment_algorithm( &attributes ),
|
|
|
|
(uint32_t) expected_key_alg2 );
|
2018-06-18 16:27:26 +00:00
|
|
|
ASSERT_COMPARE( expected_key_data->x, expected_key_data->len,
|
|
|
|
key_data, key_data_length );
|
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( key_data );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2019-07-30 15:26:54 +00:00
|
|
|
void save_large_persistent_key( int data_length_arg, int expected_status )
|
2018-06-18 16:27:26 +00:00
|
|
|
{
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_key_id_t key_id = 42;
|
|
|
|
psa_key_handle_t handle = 0;
|
2018-06-18 16:27:26 +00:00
|
|
|
uint8_t *data = NULL;
|
2019-07-30 15:26:54 +00:00
|
|
|
size_t data_length = data_length_arg;
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
ASSERT_ALLOC( data, data_length );
|
|
|
|
|
2018-12-17 23:18:46 +00:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-05-15 14:12:22 +00:00
|
|
|
psa_set_key_id( &attributes, key_id );
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_set_key_type( &attributes, PSA_KEY_TYPE_RAW_DATA );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-05-15 18:15:10 +00:00
|
|
|
TEST_EQUAL( psa_import_key( &attributes, data, data_length, &handle ),
|
2018-12-17 23:33:25 +00:00
|
|
|
expected_status );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-05-28 12:08:50 +00:00
|
|
|
if( expected_status == PSA_SUCCESS )
|
|
|
|
PSA_ASSERT( psa_destroy_key( handle ) );
|
|
|
|
|
2018-06-18 16:27:26 +00:00
|
|
|
exit:
|
|
|
|
mbedtls_free( data );
|
2019-05-28 13:10:21 +00:00
|
|
|
PSA_DONE();
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2018-06-18 16:27:26 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2019-04-19 12:06:53 +00:00
|
|
|
void persistent_key_destroy( int key_id_arg, int restart,
|
2018-06-18 16:27:26 +00:00
|
|
|
int first_type_arg, data_t *first_data,
|
|
|
|
int second_type_arg, data_t *second_data )
|
|
|
|
{
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_key_id_t key_id = key_id_arg;
|
|
|
|
psa_key_handle_t handle = 0;
|
2018-06-18 16:27:26 +00:00
|
|
|
psa_key_type_t first_type = (psa_key_type_t) first_type_arg;
|
|
|
|
psa_key_type_t second_type = (psa_key_type_t) second_type_arg;
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2018-12-17 23:18:46 +00:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-05-15 14:12:22 +00:00
|
|
|
psa_set_key_id( &attributes, key_id );
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_set_key_type( &attributes, first_type );
|
2018-12-03 16:05:18 +00:00
|
|
|
|
2019-05-15 18:22:09 +00:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, first_data->x, first_data->len,
|
|
|
|
&handle ) );
|
2019-04-19 12:06:53 +00:00
|
|
|
|
|
|
|
if( restart )
|
2018-06-18 16:27:26 +00:00
|
|
|
{
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_close_key( handle );
|
2019-05-28 13:10:21 +00:00
|
|
|
PSA_DONE();
|
2019-04-19 12:06:53 +00:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2019-05-06 16:44:55 +00:00
|
|
|
PSA_ASSERT( psa_open_key( key_id, &handle ) );
|
2018-06-18 16:27:26 +00:00
|
|
|
}
|
2019-04-19 12:06:53 +00:00
|
|
|
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 1 );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
/* Destroy the key */
|
2018-12-17 23:18:46 +00:00
|
|
|
PSA_ASSERT( psa_destroy_key( handle ) );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
/* Check key slot storage is removed */
|
2018-12-17 23:24:04 +00:00
|
|
|
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
|
2019-05-06 16:44:55 +00:00
|
|
|
TEST_EQUAL( psa_open_key( key_id, &handle ), PSA_ERROR_DOES_NOT_EXIST );
|
2018-12-17 23:24:04 +00:00
|
|
|
TEST_EQUAL( handle, 0 );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
/* Shutdown and restart */
|
2019-05-28 13:10:21 +00:00
|
|
|
PSA_DONE();
|
2018-12-17 23:18:46 +00:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2018-12-03 16:05:18 +00:00
|
|
|
/* Create another key in the same slot */
|
2019-05-15 14:12:22 +00:00
|
|
|
psa_set_key_id( &attributes, key_id );
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_set_key_type( &attributes, second_type );
|
2019-05-15 18:22:09 +00:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, second_data->x, second_data->len,
|
|
|
|
&handle ) );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-05-28 12:08:50 +00:00
|
|
|
PSA_ASSERT( psa_destroy_key( handle ) );
|
|
|
|
|
2018-06-18 16:27:26 +00:00
|
|
|
exit:
|
2019-05-28 13:10:21 +00:00
|
|
|
PSA_DONE();
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2018-06-18 16:27:26 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-12-03 16:05:18 +00:00
|
|
|
void persistent_key_import( int key_id_arg, int type_arg, data_t *data,
|
2019-04-19 13:40:00 +00:00
|
|
|
int restart, int expected_status )
|
2018-06-18 16:27:26 +00:00
|
|
|
{
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_key_id_t key_id = (psa_key_id_t) key_id_arg;
|
2018-06-18 16:27:26 +00:00
|
|
|
psa_key_type_t type = (psa_key_type_t) type_arg;
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_key_handle_t handle = 0;
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2018-12-17 23:18:46 +00:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-05-15 14:12:22 +00:00
|
|
|
psa_set_key_id( &attributes, key_id );
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_set_key_type( &attributes, type );
|
2019-05-15 18:15:10 +00:00
|
|
|
TEST_EQUAL( psa_import_key( &attributes, data->x, data->len, &handle ),
|
2018-12-17 23:33:25 +00:00
|
|
|
expected_status );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
if( expected_status != PSA_SUCCESS )
|
|
|
|
{
|
2018-12-17 23:24:04 +00:00
|
|
|
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
|
2018-06-18 16:27:26 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2019-04-19 13:40:00 +00:00
|
|
|
if( restart )
|
|
|
|
{
|
|
|
|
psa_close_key( handle );
|
2019-05-28 13:10:21 +00:00
|
|
|
PSA_DONE();
|
2019-04-19 13:40:00 +00:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2019-05-06 16:44:55 +00:00
|
|
|
PSA_ASSERT( psa_open_key( key_id, &handle ) );
|
2019-04-19 13:40:00 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
|
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ),
|
|
|
|
PSA_KEY_LIFETIME_PERSISTENT );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type );
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ), 0 );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-05-28 12:08:50 +00:00
|
|
|
PSA_ASSERT( psa_destroy_key( handle ) );
|
|
|
|
|
2018-06-18 16:27:26 +00:00
|
|
|
exit:
|
2019-04-26 14:03:33 +00:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2019-05-28 13:10:21 +00:00
|
|
|
PSA_DONE();
|
2018-06-18 16:27:26 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void import_export_persistent_key( data_t *data, int type_arg,
|
2019-04-19 13:40:00 +00:00
|
|
|
int expected_bits,
|
|
|
|
int restart, int key_not_exist )
|
2018-06-18 16:27:26 +00:00
|
|
|
{
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_key_id_t key_id = 42;
|
2018-06-18 16:27:26 +00:00
|
|
|
psa_key_type_t type = (psa_key_type_t) type_arg;
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_key_handle_t handle = 0;
|
2018-06-18 16:27:26 +00:00
|
|
|
unsigned char *exported = NULL;
|
|
|
|
size_t export_size = data->len;
|
|
|
|
size_t exported_length;
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
ASSERT_ALLOC( exported, export_size );
|
|
|
|
|
2018-12-17 23:18:46 +00:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-05-15 14:12:22 +00:00
|
|
|
psa_set_key_id( &attributes, key_id );
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_set_key_type( &attributes, type );
|
|
|
|
psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_EXPORT );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
/* Import the key */
|
2019-05-15 18:15:10 +00:00
|
|
|
PSA_ASSERT( psa_import_key( &attributes, data->x, data->len, &handle ) );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2019-04-19 13:40:00 +00:00
|
|
|
|
|
|
|
if( restart )
|
|
|
|
{
|
|
|
|
psa_close_key( handle );
|
2019-05-28 13:10:21 +00:00
|
|
|
PSA_DONE();
|
2019-04-19 13:40:00 +00:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2019-05-06 16:44:55 +00:00
|
|
|
PSA_ASSERT( psa_open_key( key_id, &handle ) );
|
2019-04-19 13:40:00 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 16:27:26 +00:00
|
|
|
/* Test the key information */
|
2019-04-19 12:06:53 +00:00
|
|
|
psa_reset_key_attributes( &attributes );
|
|
|
|
PSA_ASSERT( psa_get_key_attributes( handle, &attributes ) );
|
|
|
|
TEST_EQUAL( psa_get_key_id( &attributes ), key_id );
|
|
|
|
TEST_EQUAL( psa_get_key_lifetime( &attributes ),
|
|
|
|
PSA_KEY_LIFETIME_PERSISTENT );
|
|
|
|
TEST_EQUAL( psa_get_key_type( &attributes ), type );
|
|
|
|
TEST_EQUAL( psa_get_key_bits( &attributes ), (size_t) expected_bits );
|
|
|
|
TEST_EQUAL( psa_get_key_usage_flags( &attributes ), PSA_KEY_USAGE_EXPORT );
|
|
|
|
TEST_EQUAL( psa_get_key_algorithm( &attributes ), 0 );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
2018-12-17 23:24:04 +00:00
|
|
|
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 1 );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
if( key_not_exist )
|
|
|
|
{
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2018-06-18 16:27:26 +00:00
|
|
|
}
|
|
|
|
/* Export the key */
|
2018-12-17 23:18:46 +00:00
|
|
|
PSA_ASSERT( psa_export_key( handle, exported, export_size,
|
|
|
|
&exported_length ) );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
ASSERT_COMPARE( data->x, data->len, exported, exported_length );
|
|
|
|
|
|
|
|
/* Destroy the key */
|
2018-12-17 23:18:46 +00:00
|
|
|
PSA_ASSERT( psa_destroy_key( handle ) );
|
2018-12-17 23:24:04 +00:00
|
|
|
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
|
2018-06-18 16:27:26 +00:00
|
|
|
|
|
|
|
exit:
|
2019-04-26 14:03:33 +00:00
|
|
|
psa_reset_key_attributes( &attributes );
|
2018-06-18 16:27:26 +00:00
|
|
|
mbedtls_free( exported );
|
2019-05-28 13:10:21 +00:00
|
|
|
PSA_DONE( );
|
2018-12-03 16:05:18 +00:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2018-06-18 16:27:26 +00:00
|
|
|
}
|
|
|
|
/* END_CASE */
|