Skip to content

Commit fdca1e3

Browse files
authored
Merge pull request #8730 from kfnta/its_kvstore_single_core
Add a new PSA Internal Trusted Storage APIs
2 parents 0cc1345 + aa7e1ce commit fdca1e3

File tree

12 files changed

+845
-2
lines changed

12 files changed

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

0 commit comments

Comments
 (0)