Skip to content

Commit 5b2b19f

Browse files
author
David Saada
committed
PSA storage: Implement additional flags, conform to PSA 1.0.0 spec release
- Add the no confidentiality & no replay protection flags - Conform to the "PSA 1.0.0" release of the PSA trusted storage API spec
1 parent 2d6db33 commit 5b2b19f

File tree

20 files changed

+162
-86
lines changed

20 files changed

+162
-86
lines changed

TESTS/psa/its_ps/main.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@ typedef enum {
4040

4141
extern "C" psa_status_t psa_ps_reset();
4242

43-
static psa_status_t set_func(storage_type_t stype, psa_storage_uid_t uid, uint32_t data_length,
43+
static psa_status_t set_func(storage_type_t stype, psa_storage_uid_t uid, size_t data_length,
4444
const void *p_data, psa_storage_create_flags_t create_flags)
4545
{
4646
return (stype == its) ?
4747
psa_its_set(uid, data_length, p_data, create_flags) :
4848
psa_ps_set(uid, data_length, p_data, create_flags);
4949
}
5050

51-
static psa_status_t get_func(storage_type_t stype, psa_storage_uid_t uid, uint32_t data_offset,
52-
uint32_t data_length, void *p_data)
51+
static psa_status_t get_func(storage_type_t stype, psa_storage_uid_t uid, size_t data_offset,
52+
size_t data_length, void *p_data, size_t *actual_length)
5353
{
5454
return (stype == its) ?
55-
psa_its_get(uid, data_offset, data_length, p_data) :
56-
psa_ps_get(uid, data_offset, data_length, p_data);
55+
psa_its_get(uid, data_offset, data_length, p_data, actual_length) :
56+
psa_ps_get(uid, data_offset, data_length, p_data, actual_length);
5757
}
5858

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

@@ -92,15 +94,15 @@ void pits_ps_test()
9294
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size);
9395
TEST_ASSERT_EQUAL(0, info.flags);
9496

95-
status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff);
97+
status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff, &actual_size);
9698
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
9799
TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE);
98100

99101
memset(read_buff, 0, TEST_BUFF_SIZE);
100-
status = get_func(stype, 5, 1, TEST_BUFF_SIZE, read_buff);
102+
status = get_func(stype, 5, 1, TEST_BUFF_SIZE, read_buff, &actual_size);
101103
TEST_ASSERT_NOT_EQUAL(PSA_SUCCESS, status);
102104

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

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

110112
status = get_info_func(stype, 5, &info);
111113
TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
114+
115+
if (stype == its) {
116+
return;
117+
}
118+
119+
flags = PSA_STORAGE_FLAG_NO_REPLAY_PROTECTION;
120+
status = set_func(stype, 6, TEST_BUFF_SIZE, write_buff, flags);
121+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
122+
123+
status = get_info_func(stype, 6, &info);
124+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
125+
TEST_ASSERT_EQUAL(flags, info.flags);
126+
127+
flags = PSA_STORAGE_FLAG_NO_REPLAY_PROTECTION | PSA_STORAGE_FLAG_NO_CONFIDENTIALITY | PSA_STORAGE_FLAG_WRITE_ONCE;
128+
status = set_func(stype, 6, TEST_BUFF_SIZE, write_buff, flags);
129+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
130+
131+
status = get_info_func(stype, 6, &info);
132+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
133+
TEST_ASSERT_EQUAL(flags, info.flags);
112134
}
113135

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

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

135-
status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff);
158+
status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff, &actual_size);
136159
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
160+
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, actual_size);
137161
TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE);
138162

139163
status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, PSA_STORAGE_FLAG_WRITE_ONCE);

components/TARGET_PSA/inc/psa/protected_storage.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern "C" {
5454
* \retval PSA_ERROR_GENERIC_ERROR The operation failed because of an unspecified internal failure
5555
*/
5656
psa_status_t psa_ps_set(psa_storage_uid_t uid,
57-
uint32_t data_length,
57+
size_t data_length,
5858
const void *p_data,
5959
psa_storage_create_flags_t create_flags);
6060

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

8587
/**
8688
* \brief Retrieve the metadata about the provided uid
@@ -149,7 +151,7 @@ psa_status_t psa_ps_remove(psa_storage_uid_t uid);
149151
* \retval PSA_ERROR_GENERIC_ERROR The operation has failed due to an unspecified error
150152
*/
151153
psa_status_t psa_ps_create(psa_storage_uid_t uid,
152-
uint32_t size,
154+
size_t size,
153155
psa_storage_create_flags_t create_flags);
154156

155157
/**
@@ -179,8 +181,8 @@ psa_status_t psa_ps_create(psa_storage_uid_t uid,
179181
* \retval PSA_ERROR_INVALID_SIGNATURE The operation failed because the existing data failed authentication (MAC check failed)
180182
*/
181183
psa_status_t psa_ps_set_extended(psa_storage_uid_t uid,
182-
uint32_t data_offset,
183-
uint32_t data_length,
184+
size_t data_offset,
185+
size_t data_length,
184186
const void *p_data);
185187

186188
/**

components/TARGET_PSA/inc/psa/storage_common.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ extern "C" {
3333
*/
3434
typedef uint32_t psa_storage_create_flags_t;
3535

36-
#define PSA_STORAGE_FLAG_NONE 0 /**< No flags to pass */
37-
#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`*/
36+
#define PSA_STORAGE_FLAG_NONE 0 /**< No flags to pass */
37+
#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`*/
38+
#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 */
39+
#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. */
3840

3941
/** \brief A type for UIDs used for identifying data
4042
*/
@@ -44,7 +46,8 @@ typedef uint64_t psa_storage_uid_t;
4446
* \brief A container for metadata associated with a specific uid
4547
*/
4648
struct psa_storage_info_t {
47-
uint32_t size; /**< The size of the data associated with a uid **/
49+
size_t capacity; /**< The allocated capacity of the storage associated with a UID **/
50+
size_t size; /**< The size of the data associated with a uid **/
4851
psa_storage_create_flags_t flags; /**< The flags set when the uid was created **/
4952
};
5053

components/TARGET_PSA/services/storage/common/psa_storage_common_impl.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ static void generate_fn(char *tdb_filename, uint32_t tdb_filename_size, psa_stor
184184
}
185185

186186
psa_status_t psa_storage_set_impl(KVStore *kvstore, int32_t pid, psa_storage_uid_t uid,
187-
uint32_t data_length, const void *p_data,
187+
size_t data_length, const void *p_data,
188188
uint32_t kv_create_flags)
189189
{
190190
if (uid == 0) {
@@ -200,7 +200,7 @@ psa_status_t psa_storage_set_impl(KVStore *kvstore, int32_t pid, psa_storage_uid
200200
}
201201

202202
psa_status_t psa_storage_get_impl(KVStore *kvstore, int32_t pid, psa_storage_uid_t uid,
203-
uint32_t data_offset, uint32_t data_length, void *p_data)
203+
size_t data_offset, size_t data_length, void *p_data, size_t *p_data_length)
204204
{
205205
if (uid == 0) {
206206
return PSA_ERROR_INVALID_ARGUMENT;
@@ -227,18 +227,14 @@ psa_status_t psa_storage_get_impl(KVStore *kvstore, int32_t pid, psa_storage_uid
227227
return PSA_ERROR_BUFFER_TOO_SMALL;
228228
}
229229

230-
size_t actual_size = 0;
231-
status = kvstore->get(kv_key, p_data, data_length, &actual_size, data_offset);
232-
if ((status == MBED_SUCCESS) && (actual_size < data_length)) {
233-
return PSA_ERROR_BUFFER_TOO_SMALL;
234-
}
230+
status = kvstore->get(kv_key, p_data, data_length, p_data_length, data_offset);
235231
}
236232

237233
return convert_status(status);
238234
}
239235

240236
psa_status_t psa_storage_get_info_impl(KVStore *kvstore, int32_t pid, psa_storage_uid_t uid,
241-
struct psa_storage_info_t *p_info)
237+
struct psa_storage_info_t *p_info, uint32_t *kv_get_flags)
242238
{
243239

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

263261
return convert_status(status);

components/TARGET_PSA/services/storage/common/psa_storage_common_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ typedef psa_status_t (*migrate_func_t)(mbed::KVStore *kvstore, const psa_storage
3636

3737
void psa_storage_handle_version(mbed::KVStore *kvstore, const char *version_key, const psa_storage_version_t *version,
3838
migrate_func_t migrate_func);
39-
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);
40-
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);
41-
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);
39+
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);
40+
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);
41+
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);
4242
psa_status_t psa_storage_remove_impl(mbed::KVStore *kvstore, int32_t pid, psa_storage_uid_t uid);
4343
psa_status_t psa_storage_reset_impl(mbed::KVStore *kvstore);
4444

components/TARGET_PSA/services/storage/its/COMPONENT_PSA_SRV_EMUL/psa_prot_internal_storage.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// So here we set a global pid value to be used for when calling IMPL functions
2929
#define PSA_ITS_EMUL_PID 1
3030

31-
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)
31+
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)
3232
{
3333
if (!p_data && data_length) {
3434
return PSA_ERROR_INVALID_ARGUMENT;
@@ -47,9 +47,9 @@ psa_status_t psa_its_set(psa_storage_uid_t uid, uint32_t data_length, const void
4747
return res;
4848
}
4949

50-
psa_status_t psa_its_get(psa_storage_uid_t uid, uint32_t data_offset, uint32_t data_length, void *p_data)
50+
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)
5151
{
52-
if (!p_data && data_length) {
52+
if ((!p_data && data_length) || !p_data_length) {
5353
return PSA_ERROR_INVALID_ARGUMENT;
5454
}
5555

@@ -61,7 +61,7 @@ psa_status_t psa_its_get(psa_storage_uid_t uid, uint32_t data_offset, uint32_t d
6161
return PSA_ERROR_STORAGE_FAILURE;
6262
}
6363

64-
return psa_its_get_impl(PSA_ITS_EMUL_PID, uid, data_offset, data_length, p_data);
64+
return psa_its_get_impl(PSA_ITS_EMUL_PID, uid, data_offset, data_length, p_data, p_data_length);
6565
}
6666

6767
psa_status_t psa_its_get_info(psa_storage_uid_t uid, struct psa_storage_info_t *p_info)

components/TARGET_PSA/services/storage/its/COMPONENT_PSA_SRV_IMPL/pits_impl.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extern "C"
4343
#define ITS_VERSION_KEY "PSA_ITS_VERSION" // ITS version entry identifier in TDBStore
4444

4545
static KVStore *kvstore = NULL;
46-
46+
static bool initialized = false;
4747

4848

4949
MBED_WEAK psa_status_t its_version_migrate(KVStore *kvstore,
@@ -72,18 +72,20 @@ static void its_init(void)
7272
}
7373

7474
psa_storage_handle_version(kvstore, ITS_VERSION_KEY, &version, its_version_migrate);
75+
initialized = true;
7576
}
7677

7778
// used from test only
7879
void its_deinit(void)
7980
{
8081
kvstore = NULL;
82+
initialized = false;
8183
}
8284

8385

84-
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)
86+
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)
8587
{
86-
if (!kvstore) {
88+
if (!initialized) {
8789
its_init();
8890
}
8991

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

97-
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)
99+
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)
98100
{
99-
if (!kvstore) {
101+
if (!initialized) {
100102
its_init();
101103
}
102104

103-
return psa_storage_get_impl(kvstore, pid, uid, data_offset, data_length, p_data);
105+
return psa_storage_get_impl(kvstore, pid, uid, data_offset, data_length, p_data, p_data_length);
104106
}
105107

106108
psa_status_t psa_its_get_info_impl(int32_t pid, psa_storage_uid_t uid, struct psa_storage_info_t *p_info)
107109
{
108-
if (!kvstore) {
110+
uint32_t kv_get_flags;
111+
if (!initialized) {
109112
its_init();
110113
}
111114

112-
return psa_storage_get_info_impl(kvstore, pid, uid, p_info);
115+
return psa_storage_get_info_impl(kvstore, pid, uid, p_info, &kv_get_flags);
113116
}
114117

115118
psa_status_t psa_its_remove_impl(int32_t pid, psa_storage_uid_t uid)
116119
{
117-
if (!kvstore) {
120+
if (!initialized) {
118121
its_init();
119122
}
120123

components/TARGET_PSA/services/storage/its/COMPONENT_PSA_SRV_IMPL/pits_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ extern "C"
2626
{
2727
#endif
2828

29-
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);
30-
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);
29+
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);
30+
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);
3131
psa_status_t psa_its_get_info_impl(int32_t pid, psa_storage_uid_t uid, struct psa_storage_info_t *p_info);
3232
psa_status_t psa_its_remove_impl(int32_t pid, psa_storage_uid_t uid);
3333
psa_status_t psa_its_reset_impl();

0 commit comments

Comments
 (0)