Skip to content

Commit 486f4f5

Browse files
author
Cruz Monrreal
authored
Merge pull request #9780 from itayzafrir/crypto-access-control-tests
Crypto Service - keys access control TESTS
2 parents aaf3ce4 + 02f5918 commit 486f4f5

File tree

10 files changed

+12894
-0
lines changed

10 files changed

+12894
-0
lines changed

TESTS/psa/crypto_access_control/COMPONENT_NSPE/main.cpp

Lines changed: 501 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
return (status);
38+
}
39+
40+
psa_status_t test_partition_crypto_create_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle)
41+
{
42+
psa_invec in_vec = { &key_id, sizeof(key_id) };
43+
psa_outvec out_vec = { key_handle, sizeof(*key_handle) };
44+
psa_status_t status = invoke_ipc_call(CRYPTO_CREATE_PERSISTENT_KEY, &in_vec, 1, &out_vec, 1);
45+
return (status);
46+
}
47+
48+
psa_status_t test_partition_crypto_set_key_policy(psa_key_handle_t key_handle, psa_key_usage_t key_usage,
49+
psa_algorithm_t key_alg)
50+
{
51+
psa_invec in_vec[3] = {
52+
{ &key_handle, sizeof(key_handle) },
53+
{ &key_usage, sizeof(key_usage) },
54+
{ &key_alg, sizeof(key_alg) }
55+
};
56+
psa_status_t status = invoke_ipc_call(CRYPTO_SET_KEY_POLICY, in_vec, 3, NULL, 0);
57+
return (status);
58+
}
59+
60+
psa_status_t test_partition_crypto_get_key_policy(psa_key_handle_t key_handle, psa_key_usage_t *key_usage,
61+
psa_algorithm_t *key_alg)
62+
{
63+
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
64+
psa_outvec out_vec[2] = {
65+
{ key_usage, sizeof(*key_usage) },
66+
{ key_alg, sizeof(*key_alg) }
67+
};
68+
psa_status_t status = invoke_ipc_call(CRYPTO_GET_KEY_POLICY, &in_vec, 1, out_vec, 2);
69+
return (status);
70+
}
71+
72+
psa_status_t test_partition_crypto_get_key_information(psa_key_handle_t key_handle, psa_key_type_t *key_type,
73+
size_t *key_bits)
74+
{
75+
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
76+
psa_outvec out_vec[2] = {
77+
{ key_type, sizeof(*key_type) },
78+
{ key_bits, sizeof(*key_bits) }
79+
};
80+
psa_status_t status = invoke_ipc_call(CRYPTO_GET_KEY_INFO, &in_vec, 1, out_vec, 2);
81+
return (status);
82+
}
83+
84+
psa_status_t test_partition_crypto_generate_key(psa_key_handle_t key_handle, psa_key_type_t key_type, size_t key_bits)
85+
{
86+
psa_invec in_vec[3] = {
87+
{ &key_handle, sizeof(key_handle) },
88+
{ &key_type, sizeof(key_type) },
89+
{ &key_bits, sizeof(key_bits) }
90+
};
91+
psa_status_t status = invoke_ipc_call(CRYPTO_GENERATE_KEY, in_vec, 3, NULL, 0);
92+
return (status);
93+
}
94+
95+
psa_status_t test_partition_crypto_open_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle)
96+
{
97+
psa_invec in_vec = { &key_id, sizeof(key_id) };
98+
psa_outvec out_vec = { key_handle, sizeof(*key_handle) };
99+
psa_status_t status = invoke_ipc_call(CRYPTO_OPEN_PERSISTENT_KEY, &in_vec, 1, &out_vec, 1);
100+
return (status);
101+
}
102+
103+
psa_status_t test_partition_crypto_close_key(psa_key_handle_t key_handle)
104+
{
105+
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
106+
psa_status_t status = invoke_ipc_call(CRYPTO_CLOSE_KEY, &in_vec, 1, NULL, 0);
107+
return (status);
108+
}
109+
110+
psa_status_t test_partition_crypto_destroy_key(psa_key_handle_t key_handle)
111+
{
112+
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
113+
psa_status_t status = invoke_ipc_call(CRYPTO_DESTROY_KEY, &in_vec, 1, NULL, 0);
114+
return (status);
115+
}
116+
117+
psa_status_t test_partition_crypto_import_key(psa_key_handle_t key_handle, psa_key_type_t key_type,
118+
const unsigned char *key_data, size_t key_data_size)
119+
{
120+
psa_invec in_vec[4] = {
121+
{ &key_handle, sizeof(key_handle) },
122+
{ &key_type, sizeof(key_type) },
123+
{ &key_data_size, sizeof(key_data_size) },
124+
{ key_data, key_data_size }
125+
};
126+
psa_status_t status = invoke_ipc_call(CRYPTO_IMPORT_KEY, in_vec, 4, NULL, 0);
127+
return (status);
128+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
psa_status_t test_partition_crypto_import_key(psa_key_handle_t key_handle, psa_key_type_t key_type,
47+
const unsigned char *key_data, size_t key_data_size);
48+
49+
#ifdef __cplusplus
50+
}
51+
#endif
52+
53+
#endif /* TEST_PARTITION_PROXY_H */
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/* Copyright (c) 2017-2019 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+
/***********************************************************************************************************************
19+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20+
* THIS FILE IS AN AUTO-GENERATED FILE - DO NOT MODIFY IT.
21+
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
22+
* Template Version 1.0
23+
* Generated by tools/spm/generate_partition_code.py Version 1.0
24+
**********************************************************************************************************************/
25+
26+
#include "cmsis.h"
27+
#include "mbed_toolchain.h" /* For using MBED_ALIGN macro */
28+
#include "rtx_os.h"
29+
#include "spm_panic.h"
30+
#include "spm_internal.h"
31+
#include "psa_test_partition_partition.h"
32+
#include "psa_test_partition_ifs.h"
33+
#include "psa_crypto_srv_ifs.h"
34+
35+
36+
/* Threads stacks */
37+
MBED_ALIGN(8) uint8_t test_partition_thread_stack[512] = {0};
38+
39+
/* Threads control blocks */
40+
osRtxThread_t test_partition_thread_cb = {0};
41+
42+
/* Thread attributes - for thread initialization */
43+
osThreadAttr_t test_partition_thread_attr = {
44+
.name = "test_partition",
45+
.attr_bits = 0,
46+
.cb_mem = &test_partition_thread_cb,
47+
.cb_size = sizeof(test_partition_thread_cb),
48+
.stack_mem = test_partition_thread_stack,
49+
.stack_size = 512,
50+
.priority = osPriorityNormal,
51+
.tz_module = 0,
52+
.reserved = 0
53+
};
54+
55+
spm_rot_service_t test_partition_rot_services[TEST_PARTITION_ROT_SRV_COUNT] = {
56+
{
57+
.sid = CRYPTO_CREATE_PERSISTENT_KEY,
58+
.mask = CRYPTO_CREATE_PERSISTENT_KEY_MSK,
59+
.partition = NULL,
60+
.min_version = 1,
61+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
62+
.allow_nspe = true,
63+
.queue = {
64+
.head = NULL,
65+
.tail = NULL
66+
}
67+
},
68+
{
69+
.sid = CRYPTO_GENERATE_KEY,
70+
.mask = CRYPTO_GENERATE_KEY_MSK,
71+
.partition = NULL,
72+
.min_version = 1,
73+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
74+
.allow_nspe = true,
75+
.queue = {
76+
.head = NULL,
77+
.tail = NULL
78+
}
79+
},
80+
{
81+
.sid = CRYPTO_OPEN_PERSISTENT_KEY,
82+
.mask = CRYPTO_OPEN_PERSISTENT_KEY_MSK,
83+
.partition = NULL,
84+
.min_version = 1,
85+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
86+
.allow_nspe = true,
87+
.queue = {
88+
.head = NULL,
89+
.tail = NULL
90+
}
91+
},
92+
{
93+
.sid = CRYPTO_CLOSE_KEY,
94+
.mask = CRYPTO_CLOSE_KEY_MSK,
95+
.partition = NULL,
96+
.min_version = 1,
97+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
98+
.allow_nspe = true,
99+
.queue = {
100+
.head = NULL,
101+
.tail = NULL
102+
}
103+
},
104+
{
105+
.sid = CRYPTO_SET_KEY_POLICY,
106+
.mask = CRYPTO_SET_KEY_POLICY_MSK,
107+
.partition = NULL,
108+
.min_version = 1,
109+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
110+
.allow_nspe = true,
111+
.queue = {
112+
.head = NULL,
113+
.tail = NULL
114+
}
115+
},
116+
{
117+
.sid = CRYPTO_DESTROY_KEY,
118+
.mask = CRYPTO_DESTROY_KEY_MSK,
119+
.partition = NULL,
120+
.min_version = 1,
121+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
122+
.allow_nspe = true,
123+
.queue = {
124+
.head = NULL,
125+
.tail = NULL
126+
}
127+
},
128+
{
129+
.sid = CRYPTO_GET_KEY_INFO,
130+
.mask = CRYPTO_GET_KEY_INFO_MSK,
131+
.partition = NULL,
132+
.min_version = 1,
133+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
134+
.allow_nspe = true,
135+
.queue = {
136+
.head = NULL,
137+
.tail = NULL
138+
}
139+
},
140+
{
141+
.sid = CRYPTO_GET_KEY_POLICY,
142+
.mask = CRYPTO_GET_KEY_POLICY_MSK,
143+
.partition = NULL,
144+
.min_version = 1,
145+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
146+
.allow_nspe = true,
147+
.queue = {
148+
.head = NULL,
149+
.tail = NULL
150+
}
151+
},
152+
{
153+
.sid = CRYPTO_IMPORT_KEY,
154+
.mask = CRYPTO_IMPORT_KEY_MSK,
155+
.partition = NULL,
156+
.min_version = 1,
157+
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
158+
.allow_nspe = true,
159+
.queue = {
160+
.head = NULL,
161+
.tail = NULL
162+
}
163+
},
164+
};
165+
166+
/* External SIDs used by TEST_PARTITION */
167+
const uint32_t test_partition_external_sids[1] = {
168+
PSA_KEY_MNG_ID,
169+
};
170+
171+
static osRtxMutex_t test_partition_mutex = {0};
172+
static const osMutexAttr_t test_partition_mutex_attr = {
173+
.name = "test_partition_mutex",
174+
.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust,
175+
.cb_mem = &test_partition_mutex,
176+
.cb_size = sizeof(test_partition_mutex),
177+
};
178+
179+
180+
extern void test_partition_main(void *ptr);
181+
182+
void test_partition_init(spm_partition_t *partition)
183+
{
184+
if (NULL == partition) {
185+
SPM_PANIC("partition is NULL!\n");
186+
}
187+
188+
partition->mutex = osMutexNew(&test_partition_mutex_attr);
189+
if (NULL == partition->mutex) {
190+
SPM_PANIC("Failed to create mutex for secure partition test_partition!\n");
191+
}
192+
193+
for (uint32_t i = 0; i < TEST_PARTITION_ROT_SRV_COUNT; ++i) {
194+
test_partition_rot_services[i].partition = partition;
195+
}
196+
partition->rot_services = test_partition_rot_services;
197+
198+
partition->thread_id = osThreadNew(test_partition_main, NULL, &test_partition_thread_attr);
199+
if (NULL == partition->thread_id) {
200+
SPM_PANIC("Failed to create start main thread of partition test_partition!\n");
201+
}
202+
}
203+

0 commit comments

Comments
 (0)