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
pull/9780/head
itayzafrir 2019-02-12 16:39:48 +02:00
parent 7656891179
commit 2146e74c84
9 changed files with 963 additions and 0 deletions

View File

@ -0,0 +1,89 @@
/*
* Copyright (c) 2019, Arm Limited and affiliates
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if ((!defined(TARGET_PSA)) || (!defined(MBEDTLS_PSA_CRYPTO_C)) || (!defined(COMPONENT_PSA_SRV_IPC)))
#error [NOT_SUPPORTED] These tests can run only on SPM-enabled targets and where Mbed Crypto is ON - skipping.
#endif
#include <stdio.h>
#include "mbed.h"
#include "greentea-client/test_env.h"
#include "unity.h"
#include "utest.h"
#include "psa/crypto.h"
#include "entropy.h"
#include "entropy_poll.h"
#include "test_partition_proxy.h"
#include "psa/lifecycle.h"
using namespace utest::v1;
#if defined(MBEDTLS_ENTROPY_NV_SEED) || defined(COMPONENT_PSA_SRV_IPC)
#if !defined(MAX)
#define MAX(a,b) (((a)>(b))?(a):(b))
#endif
#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE \
MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
void inject_entropy()
{
uint8_t seed[MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE] = { 0 };
for (int i = 0; i < MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE; ++i) {
seed[i] = i;
}
mbedtls_psa_inject_entropy(seed, MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE);
}
#endif // defined(MBEDTLS_ENTROPY_NV_SEED) || defined(COMPONENT_PSA_SRV_IPC)
utest::v1::status_t case_setup_handler(const Case *const source, const size_t index_of_case)
{
psa_status_t status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
status = psa_crypto_init();
#if defined(MBEDTLS_ENTROPY_NV_SEED) || defined(COMPONENT_PSA_SRV_IPC)
if (status == PSA_ERROR_INSUFFICIENT_ENTROPY) {
inject_entropy();
status = psa_crypto_init();
}
#endif
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
return greentea_case_setup_handler(source, index_of_case);
}
utest::v1::status_t case_teardown_handler(const Case *const source, const size_t passed,
const size_t failed, const failure_t failure)
{
psa_status_t status = mbed_psa_reboot_and_request_new_security_state(PSA_LIFECYCLE_ASSEMBLY_AND_TEST);
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
mbedtls_psa_crypto_free();
return greentea_case_teardown_handler(source, passed, failed, failure);
}
utest::v1::status_t test_setup(const size_t number_of_cases)
{
#ifndef NO_GREENTEA
GREENTEA_SETUP(120, "default_auto");
#endif
return verbose_test_setup_handler(number_of_cases);
}
int main(void)
{
return (1);
}

View File

@ -0,0 +1,119 @@
/*
* Copyright (c) 2019, Arm Limited and affiliates
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "psa/client.h"
#include "psa_test_partition_ifs.h"
#include "test_partition_proxy.h"
#define MINOR_VER 1
static psa_status_t invoke_ipc_call(uint32_t sid, psa_invec *in_vec, size_t in_vec_size,
psa_outvec *out_vec, size_t out_vec_size)
{
psa_status_t status;
psa_handle_t handle = psa_connect(sid, MINOR_VER);
if (handle <= 0) {
return (PSA_ERROR_COMMUNICATION_FAILURE);
}
status = psa_call(handle, in_vec, in_vec_size, out_vec, out_vec_size);
psa_close(handle);
if (status < 0) {
status = PSA_ERROR_COMMUNICATION_FAILURE;
}
return (status);
}
psa_status_t test_partition_crypto_create_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle)
{
psa_invec in_vec = { &key_id, sizeof(key_id) };
psa_outvec out_vec = { key_handle, sizeof(*key_handle) };
psa_status_t status = invoke_ipc_call(CRYPTO_CREATE_PERSISTENT_KEY, &in_vec, 1, &out_vec, 1);
return (status);
}
psa_status_t test_partition_crypto_set_key_policy(psa_key_handle_t key_handle, psa_key_usage_t key_usage,
psa_algorithm_t key_alg)
{
psa_invec in_vec[3] = {
{ &key_handle, sizeof(key_handle) },
{ &key_usage, sizeof(key_usage) },
{ &key_alg, sizeof(key_alg) }
};
psa_status_t status = invoke_ipc_call(CRYPTO_SET_KEY_POLICY, in_vec, 3, NULL, 0);
return (status);
}
psa_status_t test_partition_crypto_get_key_policy(psa_key_handle_t key_handle, psa_key_usage_t *key_usage,
psa_algorithm_t *key_alg)
{
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
psa_outvec out_vec[2] = {
{ key_usage, sizeof(*key_usage) },
{ key_alg, sizeof(*key_alg) }
};
psa_status_t status = invoke_ipc_call(CRYPTO_GET_KEY_POLICY, &in_vec, 1, out_vec, 2);
return (status);
}
psa_status_t test_partition_crypto_get_key_information(psa_key_handle_t key_handle, psa_key_type_t *key_type,
size_t *key_bits)
{
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
psa_outvec out_vec[2] = {
{ key_type, sizeof(*key_type) },
{ key_bits, sizeof(*key_bits) }
};
psa_status_t status = invoke_ipc_call(CRYPTO_GET_KEY_INFO, &in_vec, 1, out_vec, 2);
return (status);
}
psa_status_t test_partition_crypto_generate_key(psa_key_handle_t key_handle, psa_key_type_t key_type, size_t key_bits)
{
psa_invec in_vec[3] = {
{ &key_handle, sizeof(key_handle) },
{ &key_type, sizeof(key_type) },
{ &key_bits, sizeof(key_bits) }
};
psa_status_t status = invoke_ipc_call(CRYPTO_GENERATE_KEY, in_vec, 3, NULL, 0);
return (status);
}
psa_status_t test_partition_crypto_open_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle)
{
psa_invec in_vec = { &key_id, sizeof(key_id) };
psa_outvec out_vec = { key_handle, sizeof(*key_handle) };
psa_status_t status = invoke_ipc_call(CRYPTO_OPEN_PERSISTENT_KEY, &in_vec, 1, &out_vec, 1);
return (status);
}
psa_status_t test_partition_crypto_close_key(psa_key_handle_t key_handle)
{
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
psa_status_t status = invoke_ipc_call(CRYPTO_CLOSE_KEY, &in_vec, 1, NULL, 0);
return (status);
}
psa_status_t test_partition_crypto_destroy_key(psa_key_handle_t key_handle)
{
psa_invec in_vec = { &key_handle, sizeof(key_handle) };
psa_status_t status = invoke_ipc_call(CRYPTO_DESTROY_KEY, &in_vec, 1, NULL, 0);
return (status);
}

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2019, Arm Limited and affiliates
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TEST_PARTITION_PROXY_H
#define TEST_PARTITION_PROXY_H
#include "psa/crypto.h"
#ifdef __cplusplus
extern "C" {
#endif
psa_status_t test_partition_crypto_create_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle);
psa_status_t test_partition_crypto_set_key_policy(psa_key_handle_t key_handle, psa_key_usage_t key_usage,
psa_algorithm_t key_alg);
psa_status_t test_partition_crypto_get_key_policy(psa_key_handle_t key_handle, psa_key_usage_t *key_usage,
psa_algorithm_t *key_alg);
psa_status_t test_partition_crypto_get_key_information(psa_key_handle_t key_handle, psa_key_type_t *key_type,
size_t *key_bits);
psa_status_t test_partition_crypto_generate_key(psa_key_handle_t key_handle, psa_key_type_t key_type, size_t key_bits);
psa_status_t test_partition_crypto_open_persistent_key(psa_key_id_t key_id, psa_key_handle_t *key_handle);
psa_status_t test_partition_crypto_close_key(psa_key_handle_t key_handle);
psa_status_t test_partition_crypto_destroy_key(psa_key_handle_t key_handle);
#ifdef __cplusplus
}
#endif
#endif /* TEST_PARTITION_PROXY_H */

View File

@ -0,0 +1,191 @@
/* Copyright (c) 2017-2019 ARM Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/***********************************************************************************************************************
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* THIS FILE IS AN AUTO-GENERATED FILE - DO NOT MODIFY IT.
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* Template Version 1.0
* Generated by tools/spm/generate_partition_code.py Version 1.0
**********************************************************************************************************************/
#include "cmsis.h"
#include "mbed_toolchain.h" /* For using MBED_ALIGN macro */
#include "rtx_os.h"
#include "spm_panic.h"
#include "spm_internal.h"
#include "psa_test_partition_partition.h"
#include "psa_test_partition_ifs.h"
#include "psa_crypto_srv_ifs.h"
/* Threads stacks */
MBED_ALIGN(8) uint8_t test_partition_thread_stack[512] = {0};
/* Threads control blocks */
osRtxThread_t test_partition_thread_cb = {0};
/* Thread attributes - for thread initialization */
osThreadAttr_t test_partition_thread_attr = {
.name = "test_partition",
.attr_bits = 0,
.cb_mem = &test_partition_thread_cb,
.cb_size = sizeof(test_partition_thread_cb),
.stack_mem = test_partition_thread_stack,
.stack_size = 512,
.priority = osPriorityNormal,
.tz_module = 0,
.reserved = 0
};
spm_rot_service_t test_partition_rot_services[TEST_PARTITION_ROT_SRV_COUNT] = {
{
.sid = CRYPTO_CREATE_PERSISTENT_KEY,
.mask = CRYPTO_CREATE_PERSISTENT_KEY_MSK,
.partition = NULL,
.min_version = 1,
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
.allow_nspe = true,
.queue = {
.head = NULL,
.tail = NULL
}
},
{
.sid = CRYPTO_GENERATE_KEY,
.mask = CRYPTO_GENERATE_KEY_MSK,
.partition = NULL,
.min_version = 1,
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
.allow_nspe = true,
.queue = {
.head = NULL,
.tail = NULL
}
},
{
.sid = CRYPTO_OPEN_PERSISTENT_KEY,
.mask = CRYPTO_OPEN_PERSISTENT_KEY_MSK,
.partition = NULL,
.min_version = 1,
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
.allow_nspe = true,
.queue = {
.head = NULL,
.tail = NULL
}
},
{
.sid = CRYPTO_CLOSE_KEY,
.mask = CRYPTO_CLOSE_KEY_MSK,
.partition = NULL,
.min_version = 1,
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
.allow_nspe = true,
.queue = {
.head = NULL,
.tail = NULL
}
},
{
.sid = CRYPTO_SET_KEY_POLICY,
.mask = CRYPTO_SET_KEY_POLICY_MSK,
.partition = NULL,
.min_version = 1,
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
.allow_nspe = true,
.queue = {
.head = NULL,
.tail = NULL
}
},
{
.sid = CRYPTO_DESTROY_KEY,
.mask = CRYPTO_DESTROY_KEY_MSK,
.partition = NULL,
.min_version = 1,
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
.allow_nspe = true,
.queue = {
.head = NULL,
.tail = NULL
}
},
{
.sid = CRYPTO_GET_KEY_INFO,
.mask = CRYPTO_GET_KEY_INFO_MSK,
.partition = NULL,
.min_version = 1,
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
.allow_nspe = true,
.queue = {
.head = NULL,
.tail = NULL
}
},
{
.sid = CRYPTO_GET_KEY_POLICY,
.mask = CRYPTO_GET_KEY_POLICY_MSK,
.partition = NULL,
.min_version = 1,
.min_version_policy = PSA_MINOR_VERSION_POLICY_RELAXED,
.allow_nspe = true,
.queue = {
.head = NULL,
.tail = NULL
}
},
};
/* External SIDs used by TEST_PARTITION */
const uint32_t test_partition_external_sids[1] = {
PSA_KEY_MNG_ID,
};
static osRtxMutex_t test_partition_mutex = {0};
static const osMutexAttr_t test_partition_mutex_attr = {
.name = "test_partition_mutex",
.attr_bits = osMutexRecursive | osMutexPrioInherit | osMutexRobust,
.cb_mem = &test_partition_mutex,
.cb_size = sizeof(test_partition_mutex),
};
extern void test_partition_main(void *ptr);
void test_partition_init(spm_partition_t *partition)
{
if (NULL == partition) {
SPM_PANIC("partition is NULL!\n");
}
partition->mutex = osMutexNew(&test_partition_mutex_attr);
if (NULL == partition->mutex) {
SPM_PANIC("Failed to create mutex for secure partition test_partition!\n");
}
for (uint32_t i = 0; i < TEST_PARTITION_ROT_SRV_COUNT; ++i) {
test_partition_rot_services[i].partition = partition;
}
partition->rot_services = test_partition_rot_services;
partition->thread_id = osThreadNew(test_partition_main, NULL, &test_partition_thread_attr);
if (NULL == partition->thread_id) {
SPM_PANIC("Failed to create start main thread of partition test_partition!\n");
}
}

View File

@ -0,0 +1,113 @@
/* Copyright (c) 2017-2019 ARM Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/***********************************************************************************************************************
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* THIS FILE IS AN AUTO-GENERATED FILE - DO NOT MODIFY IT.
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* Template Version 1.0
* Generated by tools/spm/generate_partition_code.py Version 1.0
**********************************************************************************************************************/
#include "spm_panic.h"
#include "spm_internal.h"
#include "handles_manager.h"
#include "cmsis.h"
#include "psa_test_partition_partition.h"
#include "psa_crypto_srv_partition.h"
#include "psa_platform_partition.h"
#include "psa_its_partition.h"
extern const uint32_t test_partition_external_sids[1];
extern const uint32_t crypto_srv_external_sids[4];
extern const uint32_t platform_external_sids[1];
spm_partition_t g_partitions[4] = {
{
.partition_id = TEST_PARTITION_ID,
.thread_id = 0,
.flags_rot_srv = TEST_PARTITION_WAIT_ANY_SID_MSK,
.flags_interrupts = 0,
.rot_services = NULL,
.rot_services_count = TEST_PARTITION_ROT_SRV_COUNT,
.extern_sids = test_partition_external_sids,
.extern_sids_count = TEST_PARTITION_EXT_ROT_SRV_COUNT,
.irq_mapper = NULL,
},
{
.partition_id = CRYPTO_SRV_ID,
.thread_id = 0,
.flags_rot_srv = CRYPTO_SRV_WAIT_ANY_SID_MSK,
.flags_interrupts = 0,
.rot_services = NULL,
.rot_services_count = CRYPTO_SRV_ROT_SRV_COUNT,
.extern_sids = crypto_srv_external_sids,
.extern_sids_count = CRYPTO_SRV_EXT_ROT_SRV_COUNT,
.irq_mapper = NULL,
},
{
.partition_id = PLATFORM_ID,
.thread_id = 0,
.flags_rot_srv = PLATFORM_WAIT_ANY_SID_MSK,
.flags_interrupts = 0,
.rot_services = NULL,
.rot_services_count = PLATFORM_ROT_SRV_COUNT,
.extern_sids = platform_external_sids,
.extern_sids_count = PLATFORM_EXT_ROT_SRV_COUNT,
.irq_mapper = NULL,
},
{
.partition_id = ITS_ID,
.thread_id = 0,
.flags_rot_srv = ITS_WAIT_ANY_SID_MSK,
.flags_interrupts = 0,
.rot_services = NULL,
.rot_services_count = ITS_ROT_SRV_COUNT,
.extern_sids = NULL,
.extern_sids_count = ITS_EXT_ROT_SRV_COUNT,
.irq_mapper = NULL,
},
};
/* Check all the defined memory regions for overlapping. */
/* A list of all the memory regions. */
const mem_region_t *mem_regions = NULL;
const uint32_t mem_region_count = 0;
// forward declaration of partition initializers
void test_partition_init(spm_partition_t *partition);
void crypto_srv_init(spm_partition_t *partition);
void platform_init(spm_partition_t *partition);
void its_init(spm_partition_t *partition);
uint32_t init_partitions(spm_partition_t **partitions)
{
if (NULL == partitions) {
SPM_PANIC("partitions is NULL!\n");
}
test_partition_init(&(g_partitions[0]));
crypto_srv_init(&(g_partitions[1]));
platform_init(&(g_partitions[2]));
its_init(&(g_partitions[3]));
*partitions = g_partitions;
return 4;
}

View File

@ -0,0 +1,71 @@
/* Copyright (c) 2017-2019 ARM Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/***********************************************************************************************************************
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* THIS FILE IS AN AUTO-GENERATED FILE - DO NOT MODIFY IT.
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* Template Version 1.0
* Generated by tools/spm/generate_partition_code.py Version 1.0
**********************************************************************************************************************/
#ifndef PSA_TEST_PARTITION_PARTITION_H
#define PSA_TEST_PARTITION_PARTITION_H
#define TEST_PARTITION_ID 128
#define TEST_PARTITION_ROT_SRV_COUNT (8UL)
#define TEST_PARTITION_EXT_ROT_SRV_COUNT (1UL)
/* TEST_PARTITION event flags */
#define TEST_PARTITION_RESERVED1_POS (1UL)
#define TEST_PARTITION_RESERVED1_MSK (1UL << TEST_PARTITION_RESERVED1_POS)
#define TEST_PARTITION_RESERVED2_POS (2UL)
#define TEST_PARTITION_RESERVED2_MSK (1UL << TEST_PARTITION_RESERVED2_POS)
#define CRYPTO_CREATE_PERSISTENT_KEY_MSK_POS (4UL)
#define CRYPTO_CREATE_PERSISTENT_KEY_MSK (1UL << CRYPTO_CREATE_PERSISTENT_KEY_MSK_POS)
#define CRYPTO_GENERATE_KEY_MSK_POS (5UL)
#define CRYPTO_GENERATE_KEY_MSK (1UL << CRYPTO_GENERATE_KEY_MSK_POS)
#define CRYPTO_OPEN_PERSISTENT_KEY_MSK_POS (6UL)
#define CRYPTO_OPEN_PERSISTENT_KEY_MSK (1UL << CRYPTO_OPEN_PERSISTENT_KEY_MSK_POS)
#define CRYPTO_CLOSE_KEY_MSK_POS (7UL)
#define CRYPTO_CLOSE_KEY_MSK (1UL << CRYPTO_CLOSE_KEY_MSK_POS)
#define CRYPTO_SET_KEY_POLICY_MSK_POS (8UL)
#define CRYPTO_SET_KEY_POLICY_MSK (1UL << CRYPTO_SET_KEY_POLICY_MSK_POS)
#define CRYPTO_DESTROY_KEY_MSK_POS (9UL)
#define CRYPTO_DESTROY_KEY_MSK (1UL << CRYPTO_DESTROY_KEY_MSK_POS)
#define CRYPTO_GET_KEY_INFO_MSK_POS (10UL)
#define CRYPTO_GET_KEY_INFO_MSK (1UL << CRYPTO_GET_KEY_INFO_MSK_POS)
#define CRYPTO_GET_KEY_POLICY_MSK_POS (11UL)
#define CRYPTO_GET_KEY_POLICY_MSK (1UL << CRYPTO_GET_KEY_POLICY_MSK_POS)
#define TEST_PARTITION_WAIT_ANY_SID_MSK (\
CRYPTO_CREATE_PERSISTENT_KEY_MSK | \
CRYPTO_GENERATE_KEY_MSK | \
CRYPTO_OPEN_PERSISTENT_KEY_MSK | \
CRYPTO_CLOSE_KEY_MSK | \
CRYPTO_SET_KEY_POLICY_MSK | \
CRYPTO_DESTROY_KEY_MSK | \
CRYPTO_GET_KEY_INFO_MSK | \
CRYPTO_GET_KEY_POLICY_MSK)
#endif // PSA_TEST_PARTITION_PARTITION_H

View File

@ -0,0 +1,211 @@
/*
* Copyright (c) 2019, Arm Limited and affiliates
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "psa_test_partition_partition.h"
#include "psa/service.h"
#include "psa/client.h"
#include "psa/crypto.h"
typedef psa_status_t (*SignalHandler)(psa_msg_t *);
static void read_input_param_from_message(psa_msg_t *msg, uint8_t param_index, void *param_ptr)
{
size_t bytes_read = psa_read(msg->handle, param_index, param_ptr, msg->in_size[param_index]);
if (bytes_read != msg->in_size[param_index]) {
SPM_PANIC("SPM read length mismatch");
}
}
static psa_status_t crypto_create_persistent_key(psa_msg_t *msg)
{
psa_status_t status;
psa_key_id_t key_id = 0;
psa_key_handle_t key_handle = 0;
read_input_param_from_message(msg, 0, &key_id);
status = psa_create_key(PSA_KEY_LIFETIME_PERSISTENT, key_id, &key_handle);
if (status == PSA_SUCCESS) {
psa_write(msg->handle, 0, &key_handle, sizeof(key_handle));
}
return (status);
}
static psa_status_t crypto_generate_key(psa_msg_t *msg)
{
psa_status_t status;
psa_key_handle_t key_handle = 0;
psa_key_type_t key_type = 0;
size_t key_bits = 0;
read_input_param_from_message(msg, 0, &key_handle);
read_input_param_from_message(msg, 1, &key_type);
read_input_param_from_message(msg, 2, &key_bits);
status = psa_generate_key(key_handle, key_type, key_bits, NULL, 0);
return (status);
}
static psa_status_t crypto_open_persistent_key(psa_msg_t *msg)
{
psa_status_t status;
psa_key_id_t key_id;
psa_key_handle_t key_handle;
read_input_param_from_message(msg, 0, &key_id);
status = psa_open_key(PSA_KEY_LIFETIME_PERSISTENT, key_id, &key_handle);
if (status == PSA_SUCCESS) {
psa_write(msg->handle, 0, &key_handle, sizeof(key_handle));
}
return (status);
}
static psa_status_t crypto_close_key(psa_msg_t *msg)
{
psa_status_t status;
psa_key_handle_t key_handle;
read_input_param_from_message(msg, 0, &key_handle);
status = psa_close_key(key_handle);
return (status);
}
static psa_status_t crypto_set_key_policy(psa_msg_t *msg)
{
psa_status_t status;
psa_key_handle_t key_handle;
psa_key_usage_t key_usage;
psa_algorithm_t key_alg;
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
read_input_param_from_message(msg, 0, &key_handle);
read_input_param_from_message(msg, 1, &key_usage);
read_input_param_from_message(msg, 2, &key_alg);
psa_key_policy_set_usage(&policy, key_usage, key_alg);
status = psa_set_key_policy(key_handle, &policy);
return (status);
}
static psa_status_t crypto_destroy_key(psa_msg_t *msg)
{
psa_status_t status;
psa_key_handle_t key_handle;
read_input_param_from_message(msg, 0, &key_handle);
status = psa_destroy_key(key_handle);
return (status);
}
static psa_status_t crypto_get_key_info(psa_msg_t *msg)
{
psa_status_t status;
psa_key_handle_t key_handle;
psa_key_type_t key_type = 0;
size_t key_bits = 0;
read_input_param_from_message(msg, 0, &key_handle);
status = psa_get_key_information(key_handle, &key_type, &key_bits);
if (status == PSA_SUCCESS) {
psa_write(msg->handle, 0, &key_type, sizeof(key_type));
psa_write(msg->handle, 1, &key_bits, sizeof(key_bits));
}
return (status);
}
static psa_status_t crypto_get_key_policy(psa_msg_t *msg)
{
psa_status_t status;
psa_key_handle_t key_handle;
psa_key_policy_t policy = PSA_KEY_POLICY_INIT;
read_input_param_from_message(msg, 0, &key_handle);
status = psa_get_key_policy(key_handle, &policy);
if (status == PSA_SUCCESS) {
psa_write(msg->handle, 0, &(policy.usage), sizeof(policy.usage));
psa_write(msg->handle, 1, &(policy.alg), sizeof(policy.alg));
}
return (status);
}
static void message_handler(psa_msg_t *msg, SignalHandler handler)
{
psa_status_t status = 0;
switch (msg->type) {
case PSA_IPC_CONNECT:
case PSA_IPC_DISCONNECT: {
break;
}
case PSA_IPC_CALL: {
status = handler(msg);
break;
}
default: {
SPM_PANIC("Unexpected message type %d!", (int)(msg->type));
break;
}
}
psa_reply(msg->handle, status);
}
void test_partition_main(void)
{
psa_signal_t signal;
psa_msg_t msg = {0};
while (1) {
signal = psa_wait_any(PSA_BLOCK);
if (signal & CRYPTO_CREATE_PERSISTENT_KEY_MSK) {
psa_get(CRYPTO_CREATE_PERSISTENT_KEY_MSK, &msg);
message_handler(&msg, crypto_create_persistent_key);
}
if (signal & CRYPTO_GENERATE_KEY_MSK) {
psa_get(CRYPTO_GENERATE_KEY_MSK, &msg);
message_handler(&msg, crypto_generate_key);
}
if (signal & CRYPTO_OPEN_PERSISTENT_KEY_MSK) {
psa_get(CRYPTO_OPEN_PERSISTENT_KEY_MSK, &msg);
message_handler(&msg, crypto_open_persistent_key);
}
if (signal & CRYPTO_CLOSE_KEY_MSK) {
psa_get(CRYPTO_CLOSE_KEY_MSK, &msg);
message_handler(&msg, crypto_close_key);
}
if (signal & CRYPTO_SET_KEY_POLICY_MSK) {
psa_get(CRYPTO_SET_KEY_POLICY_MSK, &msg);
message_handler(&msg, crypto_set_key_policy);
}
if (signal & CRYPTO_DESTROY_KEY_MSK) {
psa_get(CRYPTO_DESTROY_KEY_MSK, &msg);
message_handler(&msg, crypto_destroy_key);
}
if (signal & CRYPTO_GET_KEY_INFO_MSK) {
psa_get(CRYPTO_GET_KEY_INFO_MSK, &msg);
message_handler(&msg, crypto_get_key_info);
}
if (signal & CRYPTO_GET_KEY_POLICY_MSK) {
psa_get(CRYPTO_GET_KEY_POLICY_MSK, &msg);
message_handler(&msg, crypto_get_key_policy);
}
}
}

View File

@ -0,0 +1,81 @@
{
"name": "TEST_PARTITION",
"type": "APPLICATION-ROT",
"priority": "NORMAL",
"id": "0x00000080",
"entry_point": "test_partition_main",
"stack_size": "0x200",
"heap_size": "0x400",
"services": [
{
"name": "CRYPTO_CREATE_PERSISTENT_KEY",
"identifier": "0x00000200",
"signal": "CRYPTO_CREATE_PERSISTENT_KEY_MSK",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "RELAXED"
},
{
"name": "CRYPTO_GENERATE_KEY",
"identifier": "0x00000201",
"signal": "CRYPTO_GENERATE_KEY_MSK",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "RELAXED"
},
{
"name": "CRYPTO_OPEN_PERSISTENT_KEY",
"identifier": "0x00000202",
"signal": "CRYPTO_OPEN_PERSISTENT_KEY_MSK",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "RELAXED"
},
{
"name": "CRYPTO_CLOSE_KEY",
"identifier": "0x00000203",
"signal": "CRYPTO_CLOSE_KEY_MSK",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "RELAXED"
},
{
"name": "CRYPTO_SET_KEY_POLICY",
"identifier": "0x00000204",
"signal": "CRYPTO_SET_KEY_POLICY_MSK",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "RELAXED"
},
{
"name": "CRYPTO_DESTROY_KEY",
"identifier": "0x00000205",
"signal": "CRYPTO_DESTROY_KEY_MSK",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "RELAXED"
},
{
"name": "CRYPTO_GET_KEY_INFO",
"identifier": "0x00000206",
"signal": "CRYPTO_GET_KEY_INFO_MSK",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "RELAXED"
},
{
"name": "CRYPTO_GET_KEY_POLICY",
"identifier": "0x00000207",
"signal": "CRYPTO_GET_KEY_POLICY_MSK",
"non_secure_clients": true,
"minor_version": 1,
"minor_policy": "RELAXED"
}
],
"extern_sids": [
"PSA_KEY_MNG_ID"
],
"source_files": [
"COMPONENT_SPE/test_partition.c"
]
}

View File

@ -0,0 +1,38 @@
/* Copyright (c) 2017-2019 ARM Limited
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/***********************************************************************************************************************
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* THIS FILE IS AN AUTO-GENERATED FILE - DO NOT MODIFY IT.
* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
* Template Version 1.0
* Generated by tools/spm/generate_partition_code.py Version 1.0
**********************************************************************************************************************/
#ifndef PSA_TEST_PARTITION_PARTITION_ROT_SERVICES_H
#define PSA_TEST_PARTITION_PARTITION_ROT_SERVICES_H
#define CRYPTO_CREATE_PERSISTENT_KEY 0x00000200
#define CRYPTO_GENERATE_KEY 0x00000201
#define CRYPTO_OPEN_PERSISTENT_KEY 0x00000202
#define CRYPTO_CLOSE_KEY 0x00000203
#define CRYPTO_SET_KEY_POLICY 0x00000204
#define CRYPTO_DESTROY_KEY 0x00000205
#define CRYPTO_GET_KEY_INFO 0x00000206
#define CRYPTO_GET_KEY_POLICY 0x00000207
#endif // PSA_TEST_PARTITION_PARTITION_ROT_SERVICES_H