Skip to content

Commit 2c1f0b3

Browse files
author
itayzafrir
committed
Add acl test - use other partitions' key - manage key
1 parent 8c21f10 commit 2c1f0b3

File tree

8 files changed

+146
-2
lines changed

8 files changed

+146
-2
lines changed

TESTS/psa/crypto_access_control/COMPONENT_NSPE/main.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,80 @@ void test_create_key_same_id_different_partitions(void)
155155
TEST_ASSERT_EQUAL(PSA_SUCCESS, psa_close_key(key_handle_local));
156156
}
157157

158+
void test_use_other_partition_key_manage_key(void)
159+
{
160+
static const psa_key_id_t key_id = 999;
161+
static const psa_key_type_t key_type = PSA_KEY_TYPE_AES;
162+
static const psa_algorithm_t key_alg = PSA_ALG_CBC_NO_PADDING;
163+
static const psa_key_usage_t key_usage = PSA_KEY_USAGE_EXPORT;
164+
static const size_t key_bits = 128;
165+
static const unsigned char key_data[] = {
166+
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
167+
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
168+
};
169+
psa_key_handle_t key_handle = 0;
170+
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
171+
unsigned char output[sizeof(key_data)] = { 0 };
172+
size_t len, got_key_bits;
173+
psa_key_type_t got_key_type;
174+
psa_key_lifetime_t got_lifetime;
175+
176+
/* via test partition - create a key without generating any key material */
177+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_create_persistent_key(key_id, &key_handle));
178+
TEST_ASSERT_NOT_EQUAL(0, key_handle);
179+
180+
/* try to set the key policy for the key that was created by the test partition */
181+
psa_key_policy_set_usage(&policy, key_usage, key_alg);
182+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_set_key_policy(key_handle, &policy));
183+
184+
/* via test partition - set key policy */
185+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_set_key_policy(key_handle, key_usage, key_alg));
186+
187+
/* try to generate key data for the key that was created by the test partition */
188+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_generate_key(key_handle, key_type, key_bits, NULL, 0));
189+
190+
/* via test partition - generate key material and close the key */
191+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_generate_key(key_handle, key_type, key_bits));
192+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
193+
194+
/* via test partition - reopen the key created by the test partition and keep it open */
195+
key_handle = 0;
196+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_open_persistent_key(key_id, &key_handle));
197+
TEST_ASSERT_NOT_EQUAL(0, key_handle);
198+
199+
/* try to work with the handle created for a key created by the test partition */
200+
got_key_type = 0;
201+
got_key_bits = 0;
202+
got_lifetime = 0;
203+
policy = psa_key_policy_init();
204+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_get_key_policy(key_handle, &policy));
205+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_get_key_lifetime(key_handle, &got_lifetime));
206+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_close_key(key_handle));
207+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_destroy_key(key_handle));
208+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_get_key_information(key_handle, &got_key_type, &got_key_bits));
209+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_export_key(key_handle, output, sizeof(output), &len));
210+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_export_public_key(key_handle, output, sizeof(output), &len));
211+
212+
/* via test partition - destroy the key created by the test partition */
213+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_destroy_key(key_handle));
214+
215+
/* via test partition - create a key, set key policy but no key material */
216+
key_handle = 0;
217+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_create_persistent_key(key_id, &key_handle));
218+
TEST_ASSERT_NOT_EQUAL(0, key_handle);
219+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_set_key_policy(key_handle, key_usage, key_alg));
220+
221+
/* try to import key data into the key that was created by the test partition */
222+
TEST_ASSERT_EQUAL(PSA_ERROR_INVALID_HANDLE, psa_import_key(key_handle, key_type,
223+
key_data, sizeof(key_data)));
224+
225+
/* via test partition - import key data for the key created by the test partition */
226+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_import_key(key_handle, key_type, key_data, sizeof(key_data)));
227+
228+
/* via test partition - close the key created by the test partition */
229+
TEST_ASSERT_EQUAL(PSA_SUCCESS, test_partition_crypto_close_key(key_handle));
230+
}
231+
158232
utest::v1::status_t case_setup_handler(const Case *const source, const size_t index_of_case)
159233
{
160234
psa_status_t status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
@@ -192,6 +266,8 @@ Case cases[] = {
192266
case_setup_handler, test_open_other_partition_key, case_teardown_handler),
193267
Case("create key with same id different partitions",
194268
case_setup_handler, test_create_key_same_id_different_partitions, case_teardown_handler),
269+
Case("use other partitions' key - key manage",
270+
case_setup_handler, test_use_other_partition_key_manage_key, case_teardown_handler),
195271
};
196272

197273
Specification specification(test_setup, cases);

TESTS/psa/crypto_access_control/COMPONENT_PSA_SRV_IPC/test_partition_proxy.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,16 @@ psa_status_t test_partition_crypto_destroy_key(psa_key_handle_t key_handle)
117117
psa_status_t status = invoke_ipc_call(CRYPTO_DESTROY_KEY, &in_vec, 1, NULL, 0);
118118
return (status);
119119
}
120+
121+
psa_status_t test_partition_crypto_import_key(psa_key_handle_t key_handle, psa_key_type_t key_type,
122+
const unsigned char *key_data, size_t key_data_size)
123+
{
124+
psa_invec in_vec[4] = {
125+
{ &key_handle, sizeof(key_handle) },
126+
{ &key_type, sizeof(key_type) },
127+
{ &key_data_size, sizeof(key_data_size) },
128+
{ key_data, key_data_size }
129+
};
130+
psa_status_t status = invoke_ipc_call(CRYPTO_IMPORT_KEY, in_vec, 4, NULL, 0);
131+
return (status);
132+
}

TESTS/psa/crypto_access_control/COMPONENT_PSA_SRV_IPC/test_partition_proxy.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ psa_status_t test_partition_crypto_close_key(psa_key_handle_t key_handle);
4343

4444
psa_status_t test_partition_crypto_destroy_key(psa_key_handle_t key_handle);
4545

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+
4649
#ifdef __cplusplus
4750
}
4851
#endif

TESTS/psa/crypto_access_control/COMPONENT_SPE/TARGET_MBED_SPM/psa_test_partition_partition.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,18 @@ spm_rot_service_t test_partition_rot_services[TEST_PARTITION_ROT_SRV_COUNT] = {
149149
.tail = NULL
150150
}
151151
},
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+
},
152164
};
153165

154166
/* External SIDs used by TEST_PARTITION */

TESTS/psa/crypto_access_control/COMPONENT_SPE/psa_test_partition_partition.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#define TEST_PARTITION_ID 128
3030

31-
#define TEST_PARTITION_ROT_SRV_COUNT (8UL)
31+
#define TEST_PARTITION_ROT_SRV_COUNT (9UL)
3232
#define TEST_PARTITION_EXT_ROT_SRV_COUNT (1UL)
3333

3434
/* TEST_PARTITION event flags */
@@ -56,6 +56,8 @@
5656
#define CRYPTO_GET_KEY_INFO_MSK (1UL << CRYPTO_GET_KEY_INFO_MSK_POS)
5757
#define CRYPTO_GET_KEY_POLICY_MSK_POS (11UL)
5858
#define CRYPTO_GET_KEY_POLICY_MSK (1UL << CRYPTO_GET_KEY_POLICY_MSK_POS)
59+
#define CRYPTO_IMPORT_KEY_MSK_POS (12UL)
60+
#define CRYPTO_IMPORT_KEY_MSK (1UL << CRYPTO_IMPORT_KEY_MSK_POS)
5961

6062
#define TEST_PARTITION_WAIT_ANY_SID_MSK (\
6163
CRYPTO_CREATE_PERSISTENT_KEY_MSK | \
@@ -65,7 +67,8 @@
6567
CRYPTO_SET_KEY_POLICY_MSK | \
6668
CRYPTO_DESTROY_KEY_MSK | \
6769
CRYPTO_GET_KEY_INFO_MSK | \
68-
CRYPTO_GET_KEY_POLICY_MSK)
70+
CRYPTO_GET_KEY_POLICY_MSK | \
71+
CRYPTO_IMPORT_KEY_MSK)
6972

7073

7174
#endif // PSA_TEST_PARTITION_PARTITION_H

TESTS/psa/crypto_access_control/COMPONENT_SPE/test_partition.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
#include <stdlib.h>
1819
#include "psa_test_partition_partition.h"
1920
#include "psa/service.h"
2021
#include "psa/client.h"
@@ -147,6 +148,29 @@ static psa_status_t crypto_get_key_policy(psa_msg_t *msg)
147148
return (status);
148149
}
149150

151+
static psa_status_t crypto_import_key(psa_msg_t *msg)
152+
{
153+
psa_status_t status;
154+
psa_key_handle_t key_handle;
155+
psa_key_type_t key_type;
156+
size_t key_data_size;
157+
unsigned char *key_data = NULL;
158+
159+
read_input_param_from_message(msg, 0, &key_handle);
160+
read_input_param_from_message(msg, 1, &key_type);
161+
read_input_param_from_message(msg, 2, &key_data_size);
162+
163+
key_data = calloc(1, key_data_size);
164+
if (key_data == NULL) {
165+
return (PSA_ERROR_INSUFFICIENT_MEMORY);
166+
}
167+
168+
read_input_param_from_message(msg, 3, key_data);
169+
170+
status = psa_import_key(key_handle, key_type, key_data, key_data_size);
171+
return (status);
172+
}
173+
150174
static void message_handler(psa_msg_t *msg, SignalHandler handler)
151175
{
152176
psa_status_t status = 0;
@@ -207,5 +231,9 @@ void test_partition_main(void)
207231
psa_get(CRYPTO_GET_KEY_POLICY_MSK, &msg);
208232
message_handler(&msg, crypto_get_key_policy);
209233
}
234+
if (signal & CRYPTO_IMPORT_KEY_MSK) {
235+
psa_get(CRYPTO_IMPORT_KEY_MSK, &msg);
236+
message_handler(&msg, crypto_import_key);
237+
}
210238
}
211239
}

TESTS/psa/crypto_access_control/crypto_acl_test_partition_psa.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@
7070
"non_secure_clients": true,
7171
"minor_version": 1,
7272
"minor_policy": "RELAXED"
73+
},
74+
{
75+
"name": "CRYPTO_IMPORT_KEY",
76+
"identifier": "0x00000208",
77+
"signal": "CRYPTO_IMPORT_KEY_MSK",
78+
"non_secure_clients": true,
79+
"minor_version": 1,
80+
"minor_policy": "RELAXED"
7381
}
7482
],
7583
"extern_sids": [

TESTS/psa/crypto_access_control/psa_test_partition_ifs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,6 @@
3434
#define CRYPTO_DESTROY_KEY 0x00000205
3535
#define CRYPTO_GET_KEY_INFO 0x00000206
3636
#define CRYPTO_GET_KEY_POLICY 0x00000207
37+
#define CRYPTO_IMPORT_KEY 0x00000208
3738

3839
#endif // PSA_TEST_PARTITION_PARTITION_ROT_SERVICES_H

0 commit comments

Comments
 (0)