Skip to content

Commit e62abd8

Browse files
authored
Merge pull request #8804 from mohammad1603/inject_entropy_spm
PSA Crypto SPM
2 parents 4758ddd + 5f36447 commit e62abd8

File tree

33 files changed

+4634
-11
lines changed

33 files changed

+4634
-11
lines changed

TESTS/psa/crypto_init/main.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2018 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+
#if ((!defined(TARGET_PSA)) || (!defined(MBEDTLS_PSA_CRYPTO_C)) || (!defined(MBEDTLS_PSA_CRYPTO_SPM )))
20+
#error [NOT_SUPPORTED] Mbed SPM Crypto is OFF - skipping.
21+
#endif // TARGET_PSA
22+
23+
#include "greentea-client/test_env.h"
24+
#include "unity/unity.h"
25+
#include "utest/utest.h"
26+
#include "crypto.h"
27+
#include "entropy.h"
28+
#include "entropy_poll.h"
29+
30+
#define TEST_RANDOM_SIZE 64
31+
32+
#if !defined(MAX)
33+
#define MAX(a,b) (((a)>(b))?(a):(b))
34+
#endif
35+
36+
/* Calculating the minimum allowed entropy size in bytes */
37+
#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE \
38+
MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
39+
40+
using namespace utest::v1;
41+
42+
utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
43+
{
44+
#ifndef NO_GREENTEA
45+
GREENTEA_SETUP(60, "default_auto");
46+
#endif
47+
return greentea_test_setup_handler(number_of_cases);
48+
}
49+
50+
static void check_multi_crypto_init_deinit()
51+
{
52+
uint8_t output[TEST_RANDOM_SIZE] = {0};
53+
uint8_t seed[MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE] = {0};
54+
/* inject some a seed for test*/
55+
for (int i; i < MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE; ++i) {
56+
seed[i] = i;
57+
}
58+
/* don't really care if this succeed this is just to make crypto init pass*/
59+
mbedtls_psa_inject_entropy(seed, MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE);
60+
psa_status_t status = psa_crypto_init();
61+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
62+
status = psa_crypto_init();
63+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
64+
status = psa_generate_random(output, sizeof(output));
65+
TEST_ASSERT_NOT_EQUAL(PSA_ERROR_BAD_STATE, status);
66+
mbedtls_psa_crypto_free();
67+
status = psa_generate_random(output, sizeof(output));
68+
TEST_ASSERT_NOT_EQUAL(PSA_ERROR_BAD_STATE, status);
69+
mbedtls_psa_crypto_free();
70+
status = psa_generate_random(output, sizeof(output));
71+
TEST_ASSERT_EQUAL(PSA_ERROR_BAD_STATE, status);
72+
}
73+
74+
static void check_crypto_init_deinit()
75+
{
76+
psa_status_t status;
77+
uint8_t output[TEST_RANDOM_SIZE] = {0};
78+
uint8_t seed[MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE] = {0};
79+
/* inject some a seed for test*/
80+
for (int i; i < MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE; ++i) {
81+
seed[i] = i;
82+
}
83+
/* don't really care if this succeed this is just to make crypto init pass*/
84+
mbedtls_psa_inject_entropy(seed, MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE);
85+
status = psa_generate_random(output, sizeof(output));
86+
TEST_ASSERT_EQUAL(PSA_ERROR_BAD_STATE, status);
87+
status = psa_crypto_init();
88+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
89+
status = psa_generate_random(output, sizeof(output));
90+
TEST_ASSERT_NOT_EQUAL(PSA_ERROR_BAD_STATE, status);
91+
mbedtls_psa_crypto_free();
92+
status = psa_generate_random(output, sizeof(output));
93+
TEST_ASSERT_EQUAL(PSA_ERROR_BAD_STATE, status);
94+
}
95+
96+
Case cases[] = {
97+
Case("PSA crypto-init De-init", check_crypto_init_deinit),
98+
Case("PSA crypto- multiple init De-init", check_multi_crypto_init_deinit),
99+
};
100+
101+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
102+
103+
int main()
104+
{
105+
return !Harness::run(specification);
106+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* Copyright (c) 2017-2018 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+
**********************************************************************************************************************/
23+
24+
#include "spm_panic.h"
25+
#include "spm_internal.h"
26+
#include "handles_manager.h"
27+
#include "cmsis.h"
28+
#include "psa_test_its_reset_partition.h"
29+
#include "psa_its_partition.h"
30+
#include "psa_psa_f_partition.h"
31+
32+
extern const uint32_t psa_f_external_sids[4];
33+
34+
spm_partition_t g_partitions[3] = {
35+
{
36+
.partition_id = TEST_ITS_RESET_ID,
37+
.thread_id = 0,
38+
.flags_rot_srv = TEST_ITS_RESET_WAIT_ANY_SID_MSK,
39+
.flags_interrupts = 0,
40+
.rot_services = NULL,
41+
.rot_services_count = TEST_ITS_RESET_ROT_SRV_COUNT,
42+
.extern_sids = NULL,
43+
.extern_sids_count = TEST_ITS_RESET_EXT_ROT_SRV_COUNT,
44+
.irq_mapper = NULL,
45+
},
46+
{
47+
.partition_id = ITS_ID,
48+
.thread_id = 0,
49+
.flags_rot_srv = ITS_WAIT_ANY_SID_MSK,
50+
.flags_interrupts = 0,
51+
.rot_services = NULL,
52+
.rot_services_count = ITS_ROT_SRV_COUNT,
53+
.extern_sids = NULL,
54+
.extern_sids_count = ITS_EXT_ROT_SRV_COUNT,
55+
.irq_mapper = NULL,
56+
},
57+
{
58+
.partition_id = PSA_F_ID,
59+
.thread_id = 0,
60+
.flags_rot_srv = PSA_F_WAIT_ANY_SID_MSK,
61+
.flags_interrupts = 0,
62+
.rot_services = NULL,
63+
.rot_services_count = PSA_F_ROT_SRV_COUNT,
64+
.extern_sids = psa_f_external_sids,
65+
.extern_sids_count = PSA_F_EXT_ROT_SRV_COUNT,
66+
.irq_mapper = NULL,
67+
},
68+
};
69+
70+
/* Check all the defined memory regions for overlapping. */
71+
72+
/* A list of all the memory regions. */
73+
const mem_region_t *mem_regions = NULL;
74+
75+
const uint32_t mem_region_count = 0;
76+
77+
// forward declaration of partition initializers
78+
void test_its_reset_init(spm_partition_t *partition);
79+
void its_init(spm_partition_t *partition);
80+
void psa_f_init(spm_partition_t *partition);
81+
82+
uint32_t init_partitions(spm_partition_t **partitions)
83+
{
84+
if (NULL == partitions) {
85+
SPM_PANIC("partitions is NULL!\n");
86+
}
87+
88+
test_its_reset_init(&(g_partitions[0]));
89+
its_init(&(g_partitions[1]));
90+
psa_f_init(&(g_partitions[2]));
91+
92+
*partitions = g_partitions;
93+
return 3;
94+
}
95+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* Copyright (c) 2018 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+
#ifndef TARGET_PSA
19+
#error [NOT_SUPPORTED] ITS tests can run only on PSA-enabled targets.
20+
#endif // TARGET_PSA
21+
22+
#include "test_pits.h"
23+
#include "test_pits_impl.h"
24+
25+
psa_its_status_t test_psa_its_reset(void)
26+
{
27+
return test_psa_its_reset_impl();
28+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Copyright (c) 2018 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+
#include <string.h>
18+
#include <stdlib.h>
19+
#include "psa_prot_internal_storage.h"
20+
#include "test_pits_impl.h"
21+
#include "kv_config.h"
22+
#include "KVMap.h"
23+
#include "KVStore.h"
24+
#include "mbed_error.h"
25+
26+
#ifdef __cplusplus
27+
extern "C"
28+
{
29+
#endif
30+
31+
using namespace mbed;
32+
33+
#define STR_EXPAND(tok) #tok
34+
35+
psa_its_status_t test_psa_its_reset_impl(void)
36+
{
37+
psa_its_status_t status = PSA_ITS_SUCCESS;
38+
39+
int kv_status = kv_init_storage_config();
40+
if (kv_status != MBED_SUCCESS) {
41+
return PSA_ITS_ERROR_STORAGE_FAILURE;
42+
}
43+
44+
KVMap &kv_map = KVMap::get_instance();
45+
KVStore *kvstore = kv_map.get_main_kv_instance(STR_EXPAND(MBED_CONF_STORAGE_DEFAULT_KV));
46+
if (!kvstore) {
47+
return PSA_ITS_ERROR_STORAGE_FAILURE;
48+
}
49+
50+
if (kvstore->reset() != MBED_SUCCESS) {
51+
status = PSA_ITS_ERROR_STORAGE_FAILURE;
52+
}
53+
54+
return status;
55+
}
56+
57+
#ifdef __cplusplus
58+
}
59+
#endif
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Copyright (c) 2018 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+
#ifndef __PITS_IMPL_H__
19+
#define __PITS_IMPL_H__
20+
21+
#include "psa_prot_internal_storage.h"
22+
23+
#ifdef __cplusplus
24+
extern "C"
25+
{
26+
#endif
27+
28+
psa_its_status_t test_psa_its_reset_impl(void);
29+
30+
#ifdef __cplusplus
31+
}
32+
#endif
33+
34+
#endif // __PITS_IMPL_H__
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* Copyright (c) 2017-2018 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+
#include "spm_client.h"
19+
#include "psa_prot_internal_storage.h"
20+
#include "test_pits.h"
21+
#include "psa_test_its_reset_ifs.h"
22+
23+
psa_its_status_t test_psa_its_reset(void)
24+
{
25+
psa_handle_t conn = psa_connect(TEST_PSA_ITS_RESET, 1);
26+
if (conn <= PSA_NULL_HANDLE) {
27+
return PSA_ITS_ERROR_STORAGE_FAILURE;
28+
}
29+
30+
psa_error_t status = psa_call(conn, NULL, 0, NULL, 0);
31+
if (status == PSA_DROP_CONNECTION) {
32+
status = PSA_ITS_ERROR_STORAGE_FAILURE;
33+
}
34+
35+
psa_close(conn);
36+
return status;
37+
}

0 commit comments

Comments
 (0)