Skip to content

Commit a665743

Browse files
danny4478Oren Cohen
authored andcommitted
Add a new PSA Internal Trusted Storage APIs
1 parent 304e0ca commit a665743

File tree

12 files changed

+820
-2
lines changed

12 files changed

+820
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Copyright (c) 2018 ARM Limited
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "test_pits.h"
17+
#include "test_pits_impl.h"
18+
19+
psa_its_status_t test_psa_its_reset(void)
20+
{
21+
return test_psa_its_reset_impl();
22+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Copyright (c) 2018 ARM Limited
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
#include <string.h>
16+
#include <stdlib.h>
17+
#include "psa_prot_internal_storage.h"
18+
#include "test_pits_impl.h"
19+
#include "kv_config.h"
20+
#include "KVMap.h"
21+
#include "KVStore.h"
22+
#include "mbed_error.h"
23+
24+
#ifdef __cplusplus
25+
extern "C"
26+
{
27+
#endif
28+
29+
using namespace mbed;
30+
31+
#define STR_EXPAND(tok) #tok
32+
33+
psa_its_status_t test_psa_its_reset_impl(void)
34+
{
35+
psa_its_status_t status = PSA_ITS_SUCCESS;
36+
37+
int kv_status = kv_init_storage_config();
38+
if(kv_status != MBED_SUCCESS) {
39+
return PSA_ITS_ERROR_STORAGE_FAILURE;
40+
}
41+
42+
KVMap &kv_map = KVMap::get_instance();
43+
KVStore *kvstore = kv_map.get_main_kv_instance(STR_EXPAND(MBED_CONF_STORAGE_DEFAULT_KV));
44+
if (!kvstore) {
45+
return PSA_ITS_ERROR_STORAGE_FAILURE;
46+
}
47+
48+
if (kvstore->reset() != MBED_SUCCESS) {
49+
status = PSA_ITS_ERROR_STORAGE_FAILURE;
50+
}
51+
52+
return status;
53+
}
54+
55+
#ifdef __cplusplus
56+
}
57+
#endif
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/* Copyright (c) 2018 ARM Limited
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#ifndef __PITS_IMPL_H__
17+
#define __PITS_IMPL_H__
18+
19+
#include "psa_prot_internal_storage.h"
20+
21+
#ifdef __cplusplus
22+
extern "C"
23+
{
24+
#endif
25+
26+
psa_its_status_t test_psa_its_reset_impl(void);
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif
31+
32+
#endif // __PITS_IMPL_H__
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Copyright (c) 2018 ARM Limited
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#ifndef __TEST_INTERNAL_TRUSTED_STORAGE_H__
17+
#define __TEST_INTERNAL_TRUSTED_STORAGE_H__
18+
19+
/** @file
20+
@brief This file describes the PSA Internal Trusted Storage API
21+
*/
22+
23+
#include <stddef.h>
24+
#include <stdint.h>
25+
#include "psa_prot_internal_storage.h"
26+
27+
#ifdef __cplusplus
28+
extern "C"
29+
{
30+
#endif
31+
32+
/**
33+
* \brief Remove the provided key and its associated data from the storage
34+
*
35+
* \param[in] uid The uid value
36+
*
37+
* \return A status indicating the success/failure of the operation
38+
*
39+
* \retval PSA_ITS_SUCCESS The operation completed successfully
40+
* \retval PSA_ITS_ERROR_STORAGE_FAILURE The operation failed because the physical storage has failed (Fatal error)
41+
*/
42+
psa_its_status_t test_psa_its_reset(void);
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif // __TEST_INTERNAL_TRUSTED_STORAGE_H__
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef TARGET_PSA
18+
#error [NOT_SUPPORTED] ITS tests can run only on PSA-enabled targets.
19+
#endif // TARGET_PSA
20+
21+
#include "greentea-client/test_env.h"
22+
#include "unity/unity.h"
23+
#include "utest/utest.h"
24+
#include "psa_prot_internal_storage.h"
25+
#include "test_pits.h"
26+
27+
using namespace utest::v1;
28+
29+
#define TEST_BUFF_SIZE 16
30+
31+
static void pits_test()
32+
{
33+
psa_its_status_t status = PSA_ITS_SUCCESS;
34+
uint8_t write_buff[TEST_BUFF_SIZE] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
35+
uint8_t read_buff[TEST_BUFF_SIZE] = {0};
36+
struct psa_its_info_t info = {0, PSA_ITS_WRITE_ONCE_FLAG};
37+
memset(read_buff, 0, TEST_BUFF_SIZE);
38+
39+
status = psa_its_get_info(5, &info);
40+
TEST_ASSERT_EQUAL(PSA_ITS_ERROR_KEY_NOT_FOUND, status);
41+
42+
status = psa_its_set(5, TEST_BUFF_SIZE, write_buff, 0);
43+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
44+
45+
status = psa_its_get_info(5, &info);
46+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
47+
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size);
48+
TEST_ASSERT_EQUAL(0, info.flags);
49+
50+
status = psa_its_get(5, 0, TEST_BUFF_SIZE, read_buff);
51+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
52+
TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE);
53+
54+
memset(read_buff, 0, TEST_BUFF_SIZE);
55+
status = psa_its_get(5, 1, TEST_BUFF_SIZE, read_buff);
56+
TEST_ASSERT_NOT_EQUAL(PSA_ITS_SUCCESS, status);
57+
58+
status = psa_its_get(5, 1, TEST_BUFF_SIZE - 1, read_buff);
59+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
60+
TEST_ASSERT_EQUAL_MEMORY(write_buff + 1, read_buff, TEST_BUFF_SIZE - 1);
61+
62+
status = psa_its_remove(5);
63+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
64+
65+
status = psa_its_get_info(5, &info);
66+
TEST_ASSERT_EQUAL(PSA_ITS_ERROR_KEY_NOT_FOUND, status);
67+
}
68+
69+
static void pits_write_once_test()
70+
{
71+
psa_its_status_t status = PSA_ITS_SUCCESS;
72+
uint8_t write_buff[TEST_BUFF_SIZE] = {0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};
73+
uint8_t read_buff[TEST_BUFF_SIZE] = {0};
74+
struct psa_its_info_t info = {0, 0};
75+
76+
status = test_psa_its_reset();
77+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
78+
79+
status = psa_its_get_info(5, &info);
80+
TEST_ASSERT_EQUAL(PSA_ITS_ERROR_KEY_NOT_FOUND, status);
81+
82+
status = psa_its_set(5, TEST_BUFF_SIZE, write_buff, PSA_ITS_WRITE_ONCE_FLAG);
83+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
84+
85+
info.size = 0;
86+
info.flags = 0;
87+
status = psa_its_get_info(5, &info);
88+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
89+
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size);
90+
TEST_ASSERT_EQUAL(PSA_ITS_WRITE_ONCE_FLAG, info.flags);
91+
92+
status = psa_its_get(5, 0, TEST_BUFF_SIZE, read_buff);
93+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
94+
TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE);
95+
96+
status = psa_its_set(5, TEST_BUFF_SIZE, write_buff, PSA_ITS_WRITE_ONCE_FLAG);
97+
TEST_ASSERT_NOT_EQUAL(PSA_ITS_SUCCESS, status);
98+
99+
status = psa_its_set(5, TEST_BUFF_SIZE, write_buff, 0);
100+
TEST_ASSERT_NOT_EQUAL(PSA_ITS_SUCCESS, status);
101+
102+
status = psa_its_remove(5);
103+
TEST_ASSERT_NOT_EQUAL(PSA_ITS_SUCCESS, status);
104+
105+
info.size = 0;
106+
info.flags = 0;
107+
status = psa_its_get_info(5, &info);
108+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
109+
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size);
110+
TEST_ASSERT_EQUAL(PSA_ITS_WRITE_ONCE_FLAG, info.flags);
111+
112+
status = test_psa_its_reset();
113+
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
114+
}
115+
116+
Case cases[] = {
117+
Case("PSA prot internal storage - Basic", pits_test),
118+
Case("PSA prot internal storage - Write-once", pits_write_once_test),
119+
};
120+
121+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
122+
{
123+
#ifndef NO_GREENTEA
124+
GREENTEA_SETUP(60, "default_auto");
125+
#endif
126+
return greentea_test_setup_handler(number_of_cases);
127+
}
128+
129+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
130+
131+
int main()
132+
{
133+
return !Harness::run(specification);
134+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/* Copyright (c) 2018 ARM Limited
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include <stdlib.h>
17+
#include <string.h>
18+
19+
#include "psa_prot_internal_storage.h"
20+
#include "pits_impl.h"
21+
#include "kv_config.h"
22+
#include "mbed_error.h"
23+
24+
#define PSA_ITS_EMUL_PID 1 // In EMUL world, there is no real partitioning, which makes the source partition irrelevant.
25+
// So here we set a global pid value to be used for when calling IMPL functions
26+
27+
psa_its_status_t psa_its_set(uint32_t uid, uint32_t data_length, const void *p_data, psa_its_create_flags_t create_flags)
28+
{
29+
if (!p_data && data_length) {
30+
return PSA_ITS_ERROR_BAD_POINTER;
31+
}
32+
33+
// KVStore initiation:
34+
// - In EMUL (non-secure single core) we do it here since we don't have another context to do it inside.
35+
// - Repeating calls has no effect
36+
int kv_status = kv_init_storage_config();
37+
if(kv_status != MBED_SUCCESS) {
38+
return PSA_ITS_ERROR_STORAGE_FAILURE;
39+
}
40+
41+
psa_its_status_t res = psa_its_set_impl(PSA_ITS_EMUL_PID, uid, data_length, p_data, create_flags);
42+
43+
return res;
44+
}
45+
46+
psa_its_status_t psa_its_get(uint32_t uid, uint32_t data_offset, uint32_t data_length, void *p_data)
47+
{
48+
if (!p_data && data_length) {
49+
return PSA_ITS_ERROR_BAD_POINTER;
50+
}
51+
52+
// KVStore initiation:
53+
// - In EMUL (non-secure single core) we do it here since we don't have another context to do it inside.
54+
// - Repeating calls has no effect
55+
int kv_status = kv_init_storage_config();
56+
if(kv_status != MBED_SUCCESS) {
57+
return PSA_ITS_ERROR_STORAGE_FAILURE;
58+
}
59+
60+
return psa_its_get_impl(PSA_ITS_EMUL_PID, uid, data_offset, data_length, p_data);
61+
}
62+
63+
psa_its_status_t psa_its_get_info(uint32_t uid, struct psa_its_info_t *p_info)
64+
{
65+
if (!p_info) {
66+
return PSA_ITS_ERROR_BAD_POINTER;
67+
}
68+
69+
// KVStore initiation:
70+
// - In EMUL (non-secure single core) we do it here since we don't have another context to do it inside.
71+
// - Repeating calls has no effect
72+
int kv_status = kv_init_storage_config();
73+
if(kv_status != MBED_SUCCESS) {
74+
return PSA_ITS_ERROR_STORAGE_FAILURE;
75+
}
76+
77+
return psa_its_get_info_impl(PSA_ITS_EMUL_PID, uid, p_info);
78+
}
79+
80+
psa_its_status_t psa_its_remove(uint32_t uid)
81+
{
82+
// KVStore initiation:
83+
// - In EMUL (non-secure single core) we do it here since we don't have another context to do it inside.
84+
// - Repeating calls has no effect
85+
int kv_status = kv_init_storage_config();
86+
if(kv_status != MBED_SUCCESS) {
87+
return PSA_ITS_ERROR_STORAGE_FAILURE;
88+
}
89+
90+
return psa_its_remove_impl(PSA_ITS_EMUL_PID, uid);
91+
}

0 commit comments

Comments
 (0)