Skip to content

Commit 870bd05

Browse files
author
Cruz Monrreal
authored
Merge pull request #9708 from davidsaada/david_protected_storage
Implement PSA protected storage & restructure PSA storage implementation
2 parents 50af2c3 + 3c5c205 commit 870bd05

File tree

48 files changed

+1822
-1132
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1822
-1132
lines changed

TESTS/mbed-crypto/sanity/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ void test_crypto_key_handles(void)
350350
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_destroy_key(key_handle));
351351

352352
key_handle = 0;
353-
TEST_ASSERT_EQUAL(PSA_ERROR_EMPTY_SLOT, psa_open_key(PSA_KEY_LIFETIME_PERSISTENT, id, &key_handle));
353+
TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, psa_open_key(PSA_KEY_LIFETIME_PERSISTENT, id, &key_handle));
354354
}
355355

356356
void test_crypto_hash_clone(void)

TESTS/psa/entropy_inject/main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ void validate_entropy_seed_injection(int seed_length_a,
7070

7171
void run_entropy_inject_with_crypto_init()
7272
{
73-
psa_its_status_t its_status;
7473
psa_status_t status;
7574
status = psa_crypto_init();
7675
TEST_ASSERT_EQUAL_INT(PSA_ERROR_INSUFFICIENT_ENTROPY, status);
@@ -158,7 +157,7 @@ utest::v1::status_t case_teardown_handler(const Case *const source, const size_t
158157
{
159158
psa_status_t status;
160159
status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
161-
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
160+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
162161
mbedtls_psa_crypto_free();
163162
return greentea_case_teardown_handler(source, passed, failed, reason);
164163
}
@@ -167,7 +166,7 @@ utest::v1::status_t case_setup_handler(const Case *const source, const size_t in
167166
{
168167
psa_status_t status;
169168
status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
170-
TEST_ASSERT_EQUAL(PSA_ITS_SUCCESS, status);
169+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
171170
return greentea_case_setup_handler(source, index_of_case);
172171
}
173172

TESTS/psa/its/main.cpp

Lines changed: 0 additions & 146 deletions
This file was deleted.

TESTS/psa/its_ps/main.cpp

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/*
2+
* Copyright (c) 2019 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/PS 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/error.h"
27+
#include "psa/storage_common.h"
28+
#include "psa/internal_trusted_storage.h"
29+
#include "psa/protected_storage.h"
30+
#include "psa/lifecycle.h"
31+
32+
using namespace utest::v1;
33+
34+
#define TEST_BUFF_SIZE 16
35+
36+
typedef enum {
37+
its,
38+
ps
39+
} storage_type_t;
40+
41+
extern "C" psa_status_t psa_ps_reset();
42+
43+
static psa_status_t set_func(storage_type_t stype, psa_storage_uid_t uid, uint32_t data_length,
44+
const void *p_data, psa_storage_create_flags_t create_flags)
45+
{
46+
return (stype == its) ?
47+
psa_its_set(uid, data_length, p_data, create_flags) :
48+
psa_ps_set(uid, data_length, p_data, create_flags);
49+
}
50+
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)
53+
{
54+
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);
57+
}
58+
59+
static psa_status_t get_info_func(storage_type_t stype, psa_storage_uid_t uid,
60+
struct psa_storage_info_t *p_info)
61+
{
62+
return (stype == its) ?
63+
psa_its_get_info(uid, p_info) :
64+
psa_ps_get_info(uid, p_info);
65+
}
66+
67+
static psa_status_t remove_func(storage_type_t stype, psa_storage_uid_t uid)
68+
{
69+
return (stype == its) ?
70+
psa_its_remove(uid) :
71+
psa_ps_remove(uid);
72+
}
73+
74+
75+
template <storage_type_t stype>
76+
void pits_ps_test()
77+
{
78+
psa_status_t status = PSA_SUCCESS;
79+
uint8_t write_buff[TEST_BUFF_SIZE] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
80+
uint8_t read_buff[TEST_BUFF_SIZE] = {0};
81+
struct psa_storage_info_t info = {0, PSA_STORAGE_FLAG_WRITE_ONCE};
82+
memset(read_buff, 0, TEST_BUFF_SIZE);
83+
84+
status = get_info_func(stype, 5, &info);
85+
TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
86+
87+
status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, 0);
88+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
89+
90+
status = get_info_func(stype, 5, &info);
91+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
92+
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size);
93+
TEST_ASSERT_EQUAL(0, info.flags);
94+
95+
status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff);
96+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
97+
TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE);
98+
99+
memset(read_buff, 0, TEST_BUFF_SIZE);
100+
status = get_func(stype, 5, 1, TEST_BUFF_SIZE, read_buff);
101+
TEST_ASSERT_NOT_EQUAL(PSA_SUCCESS, status);
102+
103+
status = get_func(stype, 5, 1, TEST_BUFF_SIZE - 1, read_buff);
104+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
105+
TEST_ASSERT_EQUAL_MEMORY(write_buff + 1, read_buff, TEST_BUFF_SIZE - 1);
106+
107+
status = remove_func(stype, 5);
108+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
109+
110+
status = get_info_func(stype, 5, &info);
111+
TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
112+
}
113+
114+
template <storage_type_t stype>
115+
void pits_ps_write_once_test()
116+
{
117+
psa_status_t status = PSA_SUCCESS;
118+
uint8_t write_buff[TEST_BUFF_SIZE] = {0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};
119+
uint8_t read_buff[TEST_BUFF_SIZE] = {0};
120+
struct psa_storage_info_t info = {0, 0};
121+
122+
status = get_info_func(stype, 5, &info);
123+
TEST_ASSERT_EQUAL(PSA_ERROR_DOES_NOT_EXIST, status);
124+
125+
status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, PSA_STORAGE_FLAG_WRITE_ONCE);
126+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
127+
128+
info.size = 0;
129+
info.flags = PSA_STORAGE_FLAG_WRITE_ONCE;
130+
status = get_info_func(stype, 5, &info);
131+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
132+
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size);
133+
TEST_ASSERT_EQUAL(PSA_STORAGE_FLAG_WRITE_ONCE, info.flags);
134+
135+
status = get_func(stype, 5, 0, TEST_BUFF_SIZE, read_buff);
136+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
137+
TEST_ASSERT_EQUAL_MEMORY(write_buff, read_buff, TEST_BUFF_SIZE);
138+
139+
status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, PSA_STORAGE_FLAG_WRITE_ONCE);
140+
TEST_ASSERT_EQUAL(PSA_ERROR_NOT_PERMITTED, status);
141+
142+
status = set_func(stype, 5, TEST_BUFF_SIZE, write_buff, 0);
143+
TEST_ASSERT_EQUAL(PSA_ERROR_NOT_PERMITTED, status);
144+
145+
status = remove_func(stype, 5);
146+
TEST_ASSERT_EQUAL(PSA_ERROR_NOT_PERMITTED, status);
147+
148+
info.size = 0;
149+
info.flags = PSA_STORAGE_FLAG_WRITE_ONCE;
150+
status = get_info_func(stype, 5, &info);
151+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
152+
TEST_ASSERT_EQUAL(TEST_BUFF_SIZE, info.size);
153+
TEST_ASSERT_EQUAL(PSA_STORAGE_FLAG_WRITE_ONCE, info.flags);
154+
}
155+
156+
utest::v1::status_t case_its_teardown_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t reason)
157+
{
158+
psa_status_t status;
159+
status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
160+
TEST_ASSERT_EQUAL(PSA_LIFECYCLE_SUCCESS, status);
161+
return greentea_case_teardown_handler(source, passed, failed, reason);
162+
}
163+
164+
template <storage_type_t stype>
165+
utest::v1::status_t case_its_setup_handler(const Case *const source, const size_t index_of_case)
166+
{
167+
psa_status_t status;
168+
if (stype == its) {
169+
status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
170+
TEST_ASSERT_EQUAL(PSA_LIFECYCLE_SUCCESS, status);
171+
} else {
172+
status = psa_ps_reset();
173+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
174+
}
175+
return greentea_case_setup_handler(source, index_of_case);
176+
}
177+
178+
Case cases[] = {
179+
Case("PSA prot internal storage - Basic", case_its_setup_handler<its>, pits_ps_test<its>, case_its_teardown_handler),
180+
Case("PSA prot internal storage - Write-once", case_its_setup_handler<its>, pits_ps_write_once_test<its>, case_its_teardown_handler),
181+
Case("PSA protected storage - Basic", case_its_setup_handler<ps>, pits_ps_test<ps>),
182+
Case("PSA protected storage - Write-once", case_its_setup_handler<ps>, pits_ps_write_once_test<ps>)
183+
};
184+
185+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
186+
{
187+
#ifndef NO_GREENTEA
188+
GREENTEA_SETUP(60, "default_auto");
189+
#endif
190+
return greentea_test_setup_handler(number_of_cases);
191+
}
192+
193+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
194+
195+
int main()
196+
{
197+
return !Harness::run(specification);
198+
}

0 commit comments

Comments
 (0)