2018-06-18 18:27:26 +02:00
|
|
|
/* BEGIN_HEADER */
|
|
|
|
#include <stdint.h>
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include "psa_crypto_storage.h"
|
|
|
|
#include "psa_crypto_storage_backend.h"
|
|
|
|
#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];
|
|
|
|
uint8_t type[sizeof( psa_key_type_t )];
|
|
|
|
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 17:05:30 +01:00
|
|
|
|
2018-06-18 18:27:26 +02: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,
|
|
|
|
int key_type, int key_usage, int key_alg )
|
|
|
|
{
|
|
|
|
uint8_t *file_data;
|
|
|
|
size_t file_data_length;
|
|
|
|
psa_key_policy_t key_policy;
|
|
|
|
|
|
|
|
key_policy.usage = (psa_key_usage_t) key_usage;
|
|
|
|
key_policy.alg = (psa_algorithm_t) key_alg;
|
|
|
|
|
|
|
|
file_data_length = key_data->len + sizeof( psa_persistent_key_storage_format );
|
|
|
|
file_data = mbedtls_calloc( 1, file_data_length );
|
|
|
|
psa_format_key_data_for_storage( key_data->x, key_data->len,
|
|
|
|
(psa_key_type_t) key_type, &key_policy,
|
|
|
|
file_data );
|
|
|
|
|
|
|
|
ASSERT_COMPARE( expected_file_data->x, expected_file_data->len,
|
|
|
|
file_data, file_data_length );
|
|
|
|
mbedtls_free( file_data );
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void parse_storage_data_check( data_t *file_data,
|
|
|
|
data_t *expected_key_data,
|
|
|
|
int expected_key_type,
|
|
|
|
int expected_key_usage,
|
|
|
|
int expected_key_alg,
|
|
|
|
int expected_status )
|
|
|
|
{
|
|
|
|
uint8_t *key_data = NULL;
|
|
|
|
size_t key_data_length = 0;
|
|
|
|
psa_key_type_t key_type = 0;
|
|
|
|
psa_key_policy_t key_policy;
|
|
|
|
psa_status_t status;
|
|
|
|
|
|
|
|
status = psa_parse_key_data_from_storage( file_data->x, file_data->len,
|
|
|
|
&key_data, &key_data_length,
|
|
|
|
&key_type, &key_policy );
|
|
|
|
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( status, expected_status );
|
2018-06-18 18:27:26 +02:00
|
|
|
if( status != PSA_SUCCESS )
|
|
|
|
goto exit;
|
|
|
|
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( key_type, (psa_key_type_t) expected_key_type );
|
|
|
|
TEST_EQUAL( key_policy.usage, (uint32_t) expected_key_usage );
|
|
|
|
TEST_EQUAL( key_policy.alg, (uint32_t) expected_key_alg );
|
2018-06-18 18:27:26 +02: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 */
|
|
|
|
void save_large_persistent_key( int data_too_large, int expected_status )
|
|
|
|
{
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_key_id_t key_id = 42;
|
|
|
|
psa_key_handle_t handle = 0;
|
2018-06-18 18:27:26 +02:00
|
|
|
uint8_t *data = NULL;
|
|
|
|
size_t data_length = PSA_CRYPTO_MAX_STORAGE_SIZE;
|
|
|
|
|
|
|
|
if( data_too_large )
|
|
|
|
data_length += 1;
|
|
|
|
|
|
|
|
ASSERT_ALLOC( data, data_length );
|
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
|
|
|
|
PSA_KEY_TYPE_RAW_DATA,
|
|
|
|
PSA_BYTES_TO_BITS( data_length ),
|
|
|
|
&handle ) );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( psa_import_key( handle, PSA_KEY_TYPE_RAW_DATA,
|
2018-12-18 00:33:25 +01:00
|
|
|
data, data_length ),
|
|
|
|
expected_status );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( data );
|
|
|
|
mbedtls_psa_crypto_free();
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2018-06-18 18:27:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-12-03 17:05:18 +01:00
|
|
|
void persistent_key_destroy( int key_id_arg, int should_store,
|
2018-06-18 18:27:26 +02:00
|
|
|
int first_type_arg, data_t *first_data,
|
|
|
|
int second_type_arg, data_t *second_data )
|
|
|
|
{
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_key_id_t key_id = key_id_arg;
|
|
|
|
psa_key_handle_t handle = 0;
|
2018-06-18 18:27:26 +02: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;
|
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
|
|
|
|
first_type,
|
|
|
|
PSA_BYTES_TO_BITS( first_data->len ),
|
|
|
|
&handle ) );
|
2018-12-03 17:05:18 +01:00
|
|
|
|
2018-06-18 18:27:26 +02:00
|
|
|
if( should_store == 1 )
|
|
|
|
{
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_import_key(
|
|
|
|
handle, first_type,
|
|
|
|
first_data->x, first_data->len ) );
|
2018-06-18 18:27:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Destroy the key */
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_destroy_key( handle ) );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
/* Check key slot storage is removed */
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
|
2018-12-18 00:33:25 +01:00
|
|
|
TEST_EQUAL( psa_open_key( PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle ),
|
|
|
|
PSA_ERROR_EMPTY_SLOT );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( handle, 0 );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
/* Shutdown and restart */
|
|
|
|
mbedtls_psa_crypto_free();
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-03 17:05:18 +01:00
|
|
|
/* Create another key in the same slot */
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
|
|
|
|
second_type,
|
|
|
|
PSA_BYTES_TO_BITS( second_data->len ),
|
|
|
|
&handle ) );
|
|
|
|
PSA_ASSERT( psa_import_key(
|
|
|
|
handle, second_type,
|
|
|
|
second_data->x, second_data->len ) );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_psa_crypto_free();
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2018-06-18 18:27:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2018-12-03 17:05:18 +01:00
|
|
|
void persistent_key_import( int key_id_arg, int type_arg, data_t *data,
|
2018-06-18 18:27:26 +02:00
|
|
|
int expected_status )
|
|
|
|
{
|
|
|
|
psa_key_lifetime_t lifetime;
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_key_id_t key_id = (psa_key_id_t) key_id_arg;
|
2018-06-18 18:27:26 +02:00
|
|
|
psa_key_type_t type = (psa_key_type_t) type_arg;
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_key_handle_t handle = 0;
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init() );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
|
|
|
|
type,
|
|
|
|
PSA_BYTES_TO_BITS( data->len ),
|
|
|
|
&handle ) );
|
2018-12-18 00:33:25 +01:00
|
|
|
TEST_EQUAL( psa_import_key( handle, type, data->x, data->len ),
|
|
|
|
expected_status );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
if( expected_status != PSA_SUCCESS )
|
|
|
|
{
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
|
2018-06-18 18:27:26 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_get_key_lifetime( handle, &lifetime ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( lifetime, PSA_KEY_LIFETIME_PERSISTENT );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
exit:
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2018-06-18 18:27:26 +02:00
|
|
|
mbedtls_psa_crypto_free();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
|
|
|
void import_export_persistent_key( data_t *data, int type_arg,
|
|
|
|
int expected_bits, int key_not_exist )
|
|
|
|
{
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_key_id_t key_id = 42;
|
2018-06-18 18:27:26 +02:00
|
|
|
psa_key_type_t type = (psa_key_type_t) type_arg;
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_key_handle_t handle = 0;
|
2018-06-18 18:27:26 +02:00
|
|
|
unsigned char *exported = NULL;
|
|
|
|
size_t export_size = data->len;
|
|
|
|
size_t exported_length;
|
|
|
|
psa_key_type_t got_type;
|
|
|
|
size_t got_bits;
|
|
|
|
psa_key_policy_t policy;
|
|
|
|
psa_key_lifetime_t lifetime_get;
|
|
|
|
|
|
|
|
ASSERT_ALLOC( exported, export_size );
|
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_crypto_init( ) );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_create_key( PSA_KEY_LIFETIME_PERSISTENT, key_id,
|
|
|
|
type,
|
|
|
|
PSA_BYTES_TO_BITS( data->len ),
|
|
|
|
&handle ) );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
psa_key_policy_init( &policy );
|
|
|
|
psa_key_policy_set_usage( &policy, PSA_KEY_USAGE_EXPORT,
|
|
|
|
PSA_ALG_VENDOR_FLAG );
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_set_key_policy( handle, &policy ) );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
/* Import the key */
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_import_key( handle, type,
|
|
|
|
data->x, data->len ) );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_get_key_lifetime( handle, &lifetime_get ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( lifetime_get, PSA_KEY_LIFETIME_PERSISTENT );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
/* Test the key information */
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_get_key_information(
|
|
|
|
handle, &got_type, &got_bits ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( got_type, type );
|
|
|
|
TEST_EQUAL( got_bits, (size_t) expected_bits );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 1 );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
if( key_not_exist )
|
|
|
|
{
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2018-06-18 18:27:26 +02:00
|
|
|
}
|
|
|
|
/* Export the key */
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_export_key( handle, exported, export_size,
|
|
|
|
&exported_length ) );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
ASSERT_COMPARE( data->x, data->len, exported, exported_length );
|
|
|
|
|
|
|
|
/* Destroy the key */
|
2018-12-18 00:18:46 +01:00
|
|
|
PSA_ASSERT( psa_destroy_key( handle ) );
|
2018-12-18 00:24:04 +01:00
|
|
|
TEST_EQUAL( psa_is_key_present_in_storage( key_id ), 0 );
|
2018-06-18 18:27:26 +02:00
|
|
|
|
|
|
|
exit:
|
|
|
|
mbedtls_free( exported );
|
|
|
|
mbedtls_psa_crypto_free( );
|
2018-12-03 17:05:18 +01:00
|
|
|
psa_destroy_persistent_key( key_id );
|
2018-06-18 18:27:26 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|