Skip to content

Commit

Permalink
PSA storage: Implement additional flags, change ints to size_t
Browse files Browse the repository at this point in the history
  • Loading branch information
David Saada committed Jun 26, 2019
1 parent 8a7f591 commit d9c4889
Show file tree
Hide file tree
Showing 20 changed files with 162 additions and 86 deletions.
42 changes: 33 additions & 9 deletions TESTS/psa/its_ps/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ typedef enum {

extern "C" psa_status_t psa_ps_reset();

static psa_status_t set_func(storage_type_t stype, psa_storage_uid_t uid, uint32_t data_length,
static psa_status_t set_func(storage_type_t stype, psa_storage_uid_t uid, size_t data_length,
const void *p_data, psa_storage_create_flags_t create_flags)
{
return (stype == its) ?
psa_its_set(uid, data_length, p_data, create_flags) :
psa_ps_set(uid, data_length, p_data, create_flags);
}

static psa_status_t get_func(storage_type_t stype, psa_storage_uid_t uid, uint32_t data_offset,
uint32_t data_length, void *p_data)
static psa_status_t get_func(storage_type_t stype, psa_storage_uid_t uid, size_t data_offset,
size_t data_length, void *p_data, size_t *actual_length)
{
return (stype == its) ?
psa_its_get(uid, data_offset, data_length, p_data) :
psa_ps_get(uid, data_offset, data_length, p_data);
psa_its_get(uid, data_offset, data_length, p_data, actual_length) :
psa_ps_get(uid, data_offset, data_length, p_data, actual_length);
}

static psa_status_t get_info_func(storage_type_t stype, psa_storage_uid_t uid,
Expand All @@ -78,6 +78,8 @@ void pits_ps_test()
psa_status_t status = PSA_SUCCESS;
uint8_t write_buff[TEST_BUFF_SIZE] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
uint8_t read_buff[TEST_BUFF_SIZE] = {0};
size_t actual_size;
psa_storage_create_flags_t flags;
struct psa_storage_info_t info = {0, PSA_STORAGE_FLAG_WRITE_ONCE};
memset(read_buff, 0, TEST_BUFF_SIZE);

Expand All @@ -92,15 +94,15 @@ void pits_ps_test()
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size);
TEST_ASSERT_EQUAL(0, info.flags);

status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff);
status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff, &actual_size);
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE);

memset(read_buff, 0, TEST_BUFF_SIZE);
status = get_func(stype, 5, 1, TEST_BUFF_SIZE, read_buff);
status = get_func(stype, 5, 1, TEST_BUFF_SIZE, read_buff, &actual_size);
TEST_ASSERT_NOT_EQUAL(PSA_SUCCESS, status);

status = get_func(stype, 5, 1, TEST_BUFF_SIZE - 1, read_buff);
status = get_func(stype, 5, 1, TEST_BUFF_SIZE - 1, read_buff, &actual_size);
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
TEST_ASSERT_EQUAL_MEMORY(write_buff + 1, read_buff, TEST_BUFF_SIZE - 1);

Expand All @@ -109,6 +111,26 @@ void pits_ps_test()

status = get_info_func(stype, 5, &info);
TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);

if (stype == its) {
return;
}

flags = PSA_STORAGE_FLAG_NO_REPLAY_PROTECTION;
status = set_func(stype, 6, TEST_BUFF_SIZE, write_buff, flags);
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);

status = get_info_func(stype, 6, &info);
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
TEST_ASSERT_EQUAL(flags, info.flags);

flags = PSA_STORAGE_FLAG_NO_REPLAY_PROTECTION | PSA_STORAGE_FLAG_NO_CONFIDENTIALITY | PSA_STORAGE_FLAG_WRITE_ONCE;
status = set_func(stype, 6, TEST_BUFF_SIZE, write_buff, flags);
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);

status = get_info_func(stype, 6, &info);
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
TEST_ASSERT_EQUAL(flags, info.flags);
}

template <storage_type_t stype>
Expand All @@ -117,6 +139,7 @@ void pits_ps_write_once_test()
psa_status_t status = PSA_SUCCESS;
uint8_t write_buff[TEST_BUFF_SIZE] = {0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};
uint8_t read_buff[TEST_BUFF_SIZE] = {0};
size_t actual_size;
struct psa_storage_info_t info = {0, 0};

status = get_info_func(stype, 5, &info);
Expand All @@ -132,8 +155,9 @@ void pits_ps_write_once_test()
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size);
TEST_ASSERT_EQUAL(PSA_STORAGE_FLAG_WRITE_ONCE, info.flags);

status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff);
status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff, &actual_size);
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, actual_size);
TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE);

status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, PSA_STORAGE_FLAG_WRITE_ONCE);
Expand Down
18 changes: 10 additions & 8 deletions components/TARGET_PSA/inc/psa/protected_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extern "C" {
* \retval PSA_ERROR_GENERIC_ERROR The operation failed because of an unspecified internal failure
*/
psa_status_t psa_ps_set(psa_storage_uid_t uid,
uint32_t data_length,
size_t data_length,
const void *p_data,
psa_storage_create_flags_t create_flags);

Expand All @@ -65,22 +65,24 @@ psa_status_t psa_ps_set(psa_storage_uid_t uid,
* \param[in] data_offset The offset within the data associated with the `uid` to start retrieving data
* \param[in] data_length The amount of data to read (and the minimum allocated size of the `p_data` buffer)
* \param[out] p_data The buffer where the data will be placed upon successful completion
* \param[out] p_data_length The actual amount of data returned
*
* \return A status indicating the success/failure of the operation
*
* \retval PSA_SUCCESS The operation completed successfully
* \retval PSA_ERROR_INVALID_ARGUMENT The operation failed because one or more of the given arguments were invalid (null pointer, wrong flags etc.)
* \retval PSA_ERROR_DOES_NOT_EXIST The operation failed because the provided uid value was not found in the storage
* \retval PSA_ERROR_BUFFER_TOO_SMALL The operation failed because the data associated with provided uid is not the same size as `data_size`
* \retval PSA_ERROR_BUFFER_TOO_SMALL The operation failed because the data associated with provided uid does not fit `data_size`
* \retval PSA_ERROR_STORAGE_FAILURE The operation failed because the physical storage has failed (Fatal error)
* \retval PSA_ERROR_GENERIC_ERROR The operation failed because of an unspecified internal failure
* \retval PSA_ERROR_DATA_CORRUPT The operation failed because of an authentication failure when attempting to get the key
* \retval PSA_ERROR_INVALID_SIGNATURE The operation failed because the data associated with the UID failed authentication
*/
psa_status_t psa_ps_get(psa_storage_uid_t uid,
uint32_t data_offset,
uint32_t data_length,
void *p_data);
size_t data_offset,
size_t data_length,
void *p_data,
size_t *p_data_length);

/**
* \brief Retrieve the metadata about the provided uid
Expand Down Expand Up @@ -149,7 +151,7 @@ psa_status_t psa_ps_remove(psa_storage_uid_t uid);
* \retval PSA_ERROR_GENERIC_ERROR The operation has failed due to an unspecified error
*/
psa_status_t psa_ps_create(psa_storage_uid_t uid,
uint32_t size,
size_t size,
psa_storage_create_flags_t create_flags);

/**
Expand Down Expand Up @@ -179,8 +181,8 @@ psa_status_t psa_ps_create(psa_storage_uid_t uid,
* \retval PSA_ERROR_INVALID_SIGNATURE The operation failed because the existing data failed authentication (MAC check failed)
*/
psa_status_t psa_ps_set_extended(psa_storage_uid_t uid,
uint32_t data_offset,
uint32_t data_length,
size_t data_offset,
size_t data_length,
const void *p_data);

/**
Expand Down
9 changes: 6 additions & 3 deletions components/TARGET_PSA/inc/psa/storage_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ extern "C" {
*/
typedef uint32_t psa_storage_create_flags_t;

#define PSA_STORAGE_FLAG_NONE 0 /**< No flags to pass */
#define PSA_STORAGE_FLAG_WRITE_ONCE (1 << 0) /**< The data associated with the uid will not be able to be modified or deleted. Intended to be used to set bits in `psa_storage_create_flags_t`*/
#define PSA_STORAGE_FLAG_NONE 0 /**< No flags to pass */
#define PSA_STORAGE_FLAG_WRITE_ONCE (1 << 0) /**< The data associated with the uid will not be able to be modified or deleted. Intended to be used to set bits in `psa_storage_create_flags_t`*/
#define PSA_STORAGE_FLAG_NO_CONFIDENTIALITY (1 << 1) /**< The data associated with the uid is public and therefore does not require confidentiality. It therefore only needs to be integrity protected */
#define PSA_STORAGE_FLAG_NO_REPLAY_PROTECTION (1 << 2) /**< The data associated with the uid does not require replay protection. This may permit faster storage - but it permits an attecker with physical access to revert to an earlier version of the data. */

/** \brief A type for UIDs used for identifying data
*/
Expand All @@ -44,7 +46,8 @@ typedef uint64_t psa_storage_uid_t;
* \brief A container for metadata associated with a specific uid
*/
struct psa_storage_info_t {
uint32_t size; /**< The size of the data associated with a uid **/
size_t capacity; /**< The allocated capacity of the storage associated with a UID **/
size_t size; /**< The size of the data associated with a uid **/
psa_storage_create_flags_t flags; /**< The flags set when the uid was created **/
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ static void generate_fn(char *tdb_filename, uint32_t tdb_filename_size, psa_stor
}

psa_status_t psa_storage_set_impl(KVStore *kvstore, int32_t pid, psa_storage_uid_t uid,
uint32_t data_length, const void *p_data,
size_t data_length, const void *p_data,
uint32_t kv_create_flags)
{
if (uid == 0) {
Expand All @@ -200,7 +200,7 @@ psa_status_t psa_storage_set_impl(KVStore *kvstore, int32_t pid, psa_storage_uid
}

psa_status_t psa_storage_get_impl(KVStore *kvstore, int32_t pid, psa_storage_uid_t uid,
uint32_t data_offset, uint32_t data_length, void *p_data)
size_t data_offset, size_t data_length, void *p_data, size_t *p_data_length)
{
if (uid == 0) {
return PSA_ERROR_INVALID_ARGUMENT;
Expand All @@ -227,18 +227,14 @@ psa_status_t psa_storage_get_impl(KVStore *kvstore, int32_t pid, psa_storage_uid
return PSA_ERROR_BUFFER_TOO_SMALL;
}

size_t actual_size = 0;
status = kvstore->get(kv_key, p_data, data_length, &actual_size, data_offset);
if ((status == MBED_SUCCESS) && (actual_size < data_length)) {
return PSA_ERROR_BUFFER_TOO_SMALL;
}
status = kvstore->get(kv_key, p_data, data_length, p_data_length, data_offset);
}

return convert_status(status);
}

psa_status_t psa_storage_get_info_impl(KVStore *kvstore, int32_t pid, psa_storage_uid_t uid,
struct psa_storage_info_t *p_info)
struct psa_storage_info_t *p_info, uint32_t *kv_get_flags)
{

if (uid == 0) {
Expand All @@ -257,7 +253,9 @@ psa_status_t psa_storage_get_info_impl(KVStore *kvstore, int32_t pid, psa_storag
if (kv_info.flags & KVStore::WRITE_ONCE_FLAG) {
p_info->flags |= PSA_STORAGE_FLAG_WRITE_ONCE;
}
p_info->size = (uint32_t)(kv_info.size); // kv_info.size is of type size_t
*kv_get_flags = kv_info.flags;
p_info->size = kv_info.size;
p_info->capacity = kv_info.size;
}

return convert_status(status);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ typedef psa_status_t (*migrate_func_t)(mbed::KVStore *kvstore, const psa_storage

void psa_storage_handle_version(mbed::KVStore *kvstore, const char *version_key, const psa_storage_version_t *version,
migrate_func_t migrate_func);
psa_status_t psa_storage_set_impl(mbed::KVStore *kvstore, int32_t pid, psa_storage_uid_t uid, uint32_t data_length, const void *p_data, uint32_t kv_create_flags);
psa_status_t psa_storage_get_impl(mbed::KVStore *kvstore, int32_t pid, psa_storage_uid_t uid, uint32_t data_offset, uint32_t data_length, void *p_data);
psa_status_t psa_storage_get_info_impl(mbed::KVStore *kvstore, int32_t pid, psa_storage_uid_t uid, struct psa_storage_info_t *p_info);
psa_status_t psa_storage_set_impl(mbed::KVStore *kvstore, int32_t pid, psa_storage_uid_t uid, size_t data_length, const void *p_data, uint32_t kv_create_flags);
psa_status_t psa_storage_get_impl(mbed::KVStore *kvstore, int32_t pid, psa_storage_uid_t uid, size_t data_offset, size_t data_length, void *p_data, size_t *p_data_length);
psa_status_t psa_storage_get_info_impl(mbed::KVStore *kvstore, int32_t pid, psa_storage_uid_t uid, struct psa_storage_info_t *p_info, uint32_t *kv_get_flags);
psa_status_t psa_storage_remove_impl(mbed::KVStore *kvstore, int32_t pid, psa_storage_uid_t uid);
psa_status_t psa_storage_reset_impl(mbed::KVStore *kvstore);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// So here we set a global pid value to be used for when calling IMPL functions
#define PSA_ITS_EMUL_PID 1

psa_status_t psa_its_set(psa_storage_uid_t uid, uint32_t data_length, const void *p_data, psa_storage_create_flags_t create_flags)
psa_status_t psa_its_set(psa_storage_uid_t uid, size_t data_length, const void *p_data, psa_storage_create_flags_t create_flags)
{
if (!p_data && data_length) {
return PSA_ERROR_INVALID_ARGUMENT;
Expand All @@ -47,9 +47,9 @@ psa_status_t psa_its_set(psa_storage_uid_t uid, uint32_t data_length, const void
return res;
}

psa_status_t psa_its_get(psa_storage_uid_t uid, uint32_t data_offset, uint32_t data_length, void *p_data)
psa_status_t psa_its_get(psa_storage_uid_t uid, size_t data_offset, size_t data_length, void *p_data, size_t *p_data_length)
{
if (!p_data && data_length) {
if ((!p_data && data_length) || !p_data_length) {
return PSA_ERROR_INVALID_ARGUMENT;
}

Expand All @@ -61,7 +61,7 @@ psa_status_t psa_its_get(psa_storage_uid_t uid, uint32_t data_offset, uint32_t d
return PSA_ERROR_STORAGE_FAILURE;
}

return psa_its_get_impl(PSA_ITS_EMUL_PID, uid, data_offset, data_length, p_data);
return psa_its_get_impl(PSA_ITS_EMUL_PID, uid, data_offset, data_length, p_data, p_data_length);
}

psa_status_t psa_its_get_info(psa_storage_uid_t uid, struct psa_storage_info_t *p_info)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extern "C"
#define ITS_VERSION_KEY "PSA_ITS_VERSION" // ITS version entry identifier in TDBStore

static KVStore *kvstore = NULL;

static bool initialized = false;


MBED_WEAK psa_status_t its_version_migrate(KVStore *kvstore,
Expand Down Expand Up @@ -72,18 +72,20 @@ static void its_init(void)
}

psa_storage_handle_version(kvstore, ITS_VERSION_KEY, &version, its_version_migrate);
initialized = true;
}

// used from test only
void its_deinit(void)
{
kvstore = NULL;
initialized = false;
}


psa_status_t psa_its_set_impl(int32_t pid, psa_storage_uid_t uid, uint32_t data_length, const void *p_data, psa_storage_create_flags_t create_flags)
psa_status_t psa_its_set_impl(int32_t pid, psa_storage_uid_t uid, size_t data_length, const void *p_data, psa_storage_create_flags_t create_flags)
{
if (!kvstore) {
if (!initialized) {
its_init();
}

Expand All @@ -94,27 +96,28 @@ psa_status_t psa_its_set_impl(int32_t pid, psa_storage_uid_t uid, uint32_t data_
return psa_storage_set_impl(kvstore, pid, uid, data_length, p_data, create_flags);
}

psa_status_t psa_its_get_impl(int32_t pid, psa_storage_uid_t uid, uint32_t data_offset, uint32_t data_length, void *p_data)
psa_status_t psa_its_get_impl(int32_t pid, psa_storage_uid_t uid, size_t data_offset, size_t data_length, void *p_data, size_t *p_data_length)
{
if (!kvstore) {
if (!initialized) {
its_init();
}

return psa_storage_get_impl(kvstore, pid, uid, data_offset, data_length, p_data);
return psa_storage_get_impl(kvstore, pid, uid, data_offset, data_length, p_data, p_data_length);
}

psa_status_t psa_its_get_info_impl(int32_t pid, psa_storage_uid_t uid, struct psa_storage_info_t *p_info)
{
if (!kvstore) {
uint32_t kv_get_flags;
if (!initialized) {
its_init();
}

return psa_storage_get_info_impl(kvstore, pid, uid, p_info);
return psa_storage_get_info_impl(kvstore, pid, uid, p_info, &kv_get_flags);
}

psa_status_t psa_its_remove_impl(int32_t pid, psa_storage_uid_t uid)
{
if (!kvstore) {
if (!initialized) {
its_init();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ extern "C"
{
#endif

psa_status_t psa_its_set_impl(int32_t pid, psa_storage_uid_t uid, uint32_t data_length, const void *p_data, psa_storage_create_flags_t create_flags);
psa_status_t psa_its_get_impl(int32_t pid, psa_storage_uid_t uid, uint32_t data_offset, uint32_t data_length, void *p_data);
psa_status_t psa_its_set_impl(int32_t pid, psa_storage_uid_t uid, size_t data_length, const void *p_data, psa_storage_create_flags_t create_flags);
psa_status_t psa_its_get_impl(int32_t pid, psa_storage_uid_t uid, size_t data_offset, size_t data_length, void *p_data, size_t *p_data_length);
psa_status_t psa_its_get_info_impl(int32_t pid, psa_storage_uid_t uid, struct psa_storage_info_t *p_info);
psa_status_t psa_its_remove_impl(int32_t pid, psa_storage_uid_t uid);
psa_status_t psa_its_reset_impl();
Expand Down
Loading

0 comments on commit d9c4889

Please sign in to comment.