Skip to content

Commit 2146e74

Browse files
author
itayzafrir
committed
Prepare for crypto access control tests
- Add a secure side test partition - Add a proxy to communicate with the test partition - Add main test file
1 parent 7656891 commit 2146e74

File tree

9 files changed

+963
-0
lines changed

9 files changed

+963
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates
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+
#if ((!defined(TARGET_PSA)) || (!defined(MBEDTLS_PSA_CRYPTO_C)) || (!defined(COMPONENT_PSA_SRV_IPC)))
19+
#error [NOT_SUPPORTED] These tests can run only on SPM-enabled targets and where Mbed Crypto is ON - skipping.
20+
#endif
21+
22+
#include <stdio.h>
23+
#include "mbed.h"
24+
#include "greentea-client/test_env.h"
25+
#include "unity.h"
26+
#include "utest.h"
27+
#include "psa/crypto.h"
28+
#include "entropy.h"
29+
#include "entropy_poll.h"
30+
#include "test_partition_proxy.h"
31+
#include "psa/lifecycle.h"
32+
33+
using namespace utest::v1;
34+
35+
#if defined(MBEDTLS_ENTROPY_NV_SEED) || defined(COMPONENT_PSA_SRV_IPC)
36+
37+
#if !defined(MAX)
38+
#define MAX(a,b) (((a)>(b))?(a):(b))
39+
#endif
40+
41+
#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE \
42+
MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
43+
44+
void inject_entropy()
45+
{
46+
uint8_t seed[MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE] = { 0 };
47+
for (int i = 0; i < MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE; ++i) {
48+
seed[i] = i;
49+
}
50+
mbedtls_psa_inject_entropy(seed, MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE);
51+
}
52+
#endif // defined(MBEDTLS_ENTROPY_NV_SEED) || defined(COMPONENT_PSA_SRV_IPC)
53+
54+
utest::v1::status_t case_setup_handler(const Case *const source, const size_t index_of_case)
55+
{
56+
psa_status_t status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
57+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
58+
status = psa_crypto_init();
59+
#if defined(MBEDTLS_ENTROPY_NV_SEED) || defined(COMPONENT_PSA_SRV_IPC)
60+
if (status == PSA_ERROR_INSUFFICIENT_ENTROPY) {
61+
inject_entropy();
62+
status = psa_crypto_init();
63+
}
64+
#endif
65+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
66+
return greentea_case_setup_handler(source, index_of_case);
67+
}
68+
69+
utest::v1::status_t case_teardown_handler(const Case *const source, const size_t passed,
70+
const size_t failed, const failure_t failure)
71+
{
72+
psa_status_t status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
73+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
74+
mbedtls_psa_crypto_free();
75+
return greentea_case_teardown_handler(source, passed, failed, failure);
76+
}
77+
78+
utest::v1::status_t test_setup(const size_t number_of_cases)
79+
{
80+
#ifndef NO_GREENTEA
81+
GREENTEA_SETUP(120, "default_auto");
82+
#endif
83+
return verbose_test_setup_handler(number_of_cases);
84+
}
85+
86+
int main(void)
87+
{
88+
return (1);
89+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates
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+
#include "psa/client.h"
19+
#include "psa_test_partition_ifs.h"
20+
#include "test_partition_proxy.h"
21+
22+
#define MINOR_VER 1
23+
24+
static psa_status_t invoke_ipc_call(uint32_t sid, psa_invec *in_vec, size_t in_vec_size,
25+
psa_outvec *out_vec, size_t out_vec_size)
26+
{
27+
psa_status_t status;
28+
29+
psa_handle_t handle = psa_connect(sid, MINOR_VER);
30+
if (handle <= 0) {
31+
return (PSA_ERROR_COMMUNICATION_FAILURE);
32+
}
33+
34+
status = psa_call(handle, in_vec, in_vec_size, out_vec, out_vec_size);
35+
psa_close(handle);
36+
37+
if (status < 0) {
38+
status = PSA_ERROR_COMMUNICATION_FAILURE;
39+
}
40+
41+
return (status);
42+
}
43+
44+
psa_status_t test_partition_crypto_create_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle)
45+
{
46+
psa_invec in_vec = { &key_id, sizeof(key_id) };
47+
psa_outvec out_vec = { key_handle, sizeof(*key_handle) };
48+
psa_status_t status = invoke_ipc_call(CRYPTO_CREATE_PERSISTENT_KEY, &in_vec, 1, &out_vec, 1);
49+
return (status);
50+
}
51+
52+
psa_status_t test_partition_crypto_set_key_policy(psa_key_handle_t key_handle, psa_key_usage_t key_usage,
53+
psa_algorithm_t key_alg)
54+
{
55+
psa_invec in_vec[3] = {
56+
{ &key_handle, sizeof(key_handle) },
57+
{ &key_usage, sizeof(key_usage) },
58+
{ &key_alg, sizeof(key_alg) }
59+
};
60+
psa_status_t status = invoke_ipc_call(CRYPTO_SET_KEY_POLICY, in_vec, 3, NULL, 0);
61+
return (status);
62+
}
63+
64+
psa_status_t test_partition_crypto_get_key_policy(psa_key_handle_t key_handle, psa_key_usage_t *key_usage,
65+
psa_algorithm_t *key_alg)
66+
{
67+
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
68+
psa_outvec out_vec[2] = {
69+
{ key_usage, sizeof(*key_usage) },
70+
{ key_alg, sizeof(*key_alg) }
71+
};
72+
psa_status_t status = invoke_ipc_call(CRYPTO_GET_KEY_POLICY, &in_vec, 1, out_vec, 2);
73+
return (status);
74+
}
75+
76+
psa_status_t test_partition_crypto_get_key_information(psa_key_handle_t key_handle, psa_key_type_t *key_type,
77+
size_t *key_bits)
78+
{
79+
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
80+
psa_outvec out_vec[2] = {
81+
{ key_type, sizeof(*key_type) },
82+
{ key_bits, sizeof(*key_bits) }
83+
};
84+
psa_status_t status = invoke_ipc_call(CRYPTO_GET_KEY_INFO, &in_vec, 1, out_vec, 2);
85+
return (status);
86+
}
87+
88+
psa_status_t test_partition_crypto_generate_key(psa_key_handle_t key_handle, psa_key_type_t key_type, size_t key_bits)
89+
{
90+
psa_invec in_vec[3] = {
91+
{ &key_handle, sizeof(key_handle) },
92+
{ &key_type, sizeof(key_type) },
93+
{ &key_bits, sizeof(key_bits) }
94+
};
95+
psa_status_t status = invoke_ipc_call(CRYPTO_GENERATE_KEY, in_vec, 3, NULL, 0);
96+
return (status);
97+
}
98+
99+
psa_status_t test_partition_crypto_open_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle)
100+
{
101+
psa_invec in_vec = { &key_id, sizeof(key_id) };
102+
psa_outvec out_vec = { key_handle, sizeof(*key_handle) };
103+
psa_status_t status = invoke_ipc_call(CRYPTO_OPEN_PERSISTENT_KEY, &in_vec, 1, &out_vec, 1);
104+
return (status);
105+
}
106+
107+
psa_status_t test_partition_crypto_close_key(psa_key_handle_t key_handle)
108+
{
109+
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
110+
psa_status_t status = invoke_ipc_call(CRYPTO_CLOSE_KEY, &in_vec, 1, NULL, 0);
111+
return (status);
112+
}
113+
114+
psa_status_t test_partition_crypto_destroy_key(psa_key_handle_t key_handle)
115+
{
116+
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
117+
psa_status_t status = invoke_ipc_call(CRYPTO_DESTROY_KEY, &in_vec, 1, NULL, 0);
118+
return (status);
119+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2019, Arm Limited and affiliates
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_PARTITION_PROXY_H
19+
#define TEST_PARTITION_PROXY_H
20+
21+
#include "psa/crypto.h"
22+
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
27+
psa_status_t test_partition_crypto_create_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle);
28+
29+
psa_status_t test_partition_crypto_set_key_policy(psa_key_handle_t key_handle, psa_key_usage_t key_usage,
30+
psa_algorithm_t key_alg);
31+
32+
psa_status_t test_partition_crypto_get_key_policy(psa_key_handle_t key_handle, psa_key_usage_t *key_usage,
33+
psa_algorithm_t *key_alg);
34+
35+
psa_status_t test_partition_crypto_get_key_information(psa_key_handle_t key_handle, psa_key_type_t *key_type,
36+
size_t *key_bits);
37+
38+
psa_status_t test_partition_crypto_generate_key(psa_key_handle_t key_handle, psa_key_type_t key_type, size_t key_bits);
39+
40+
psa_status_t test_partition_crypto_open_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle);
41+
42+
psa_status_t test_partition_crypto_close_key(psa_key_handle_t key_handle);
43+
44+
psa_status_t test_partition_crypto_destroy_key(psa_key_handle_t key_handle);
45+
46+
#ifdef __cplusplus
47+
}
48+
#endif
49+
50+
#endif /* TEST_PARTITION_PROXY_H */

0 commit comments

Comments
 (0)