Skip to content

Commit e1e77b9

Browse files
author
Netanel Gonen
committed
crypto init with multiple client guard & tests
1 parent 4487805 commit e1e77b9

File tree

2 files changed

+114
-1
lines changed

2 files changed

+114
-1
lines changed

TESTS/psa/crypto_init/main.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+
#include "greentea-client/test_env.h"
20+
#include "unity/unity.h"
21+
#include "utest/utest.h"
22+
#include "crypto.h"
23+
#include "entropy.h"
24+
#include "entropy_poll.h"
25+
26+
#if (!defined(TARGET_PSA))
27+
#error [NOT_SUPPORTED] PSA entropy injection tests can run only on PSA-enabled targets.
28+
#endif // TARGET_PSA
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+
/* don't really care if this succeed this is just to make crypto init pass*/
58+
mbedtls_psa_inject_entropy( seed, MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE );
59+
psa_status_t status = psa_crypto_init();
60+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
61+
status = psa_crypto_init();
62+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
63+
status = psa_generate_random(output, sizeof(output));
64+
TEST_ASSERT_NOT_EQUAL(PSA_ERROR_BAD_STATE, status);
65+
mbedtls_psa_crypto_free();
66+
status = psa_generate_random(output, sizeof(output));
67+
TEST_ASSERT_NOT_EQUAL(PSA_ERROR_BAD_STATE, status);
68+
mbedtls_psa_crypto_free();
69+
status = psa_generate_random(output, sizeof(output));
70+
TEST_ASSERT_EQUAL(PSA_ERROR_BAD_STATE, status);
71+
}
72+
73+
static void check_crypto_init_deinit()
74+
{
75+
psa_status_t status;
76+
uint8_t output[TEST_RANDOM_SIZE] = {0};
77+
uint8_t seed[MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE] = {0};
78+
/* inject some a seed for test*/
79+
for(int i; i < MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE; ++i)
80+
seed[i] = i;
81+
/* don't really care if this succeed this is just to make crypto init pass*/
82+
mbedtls_psa_inject_entropy( seed, MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE );
83+
status = psa_generate_random(output, sizeof(output));
84+
TEST_ASSERT_EQUAL(PSA_ERROR_BAD_STATE, status);
85+
status = psa_crypto_init();
86+
TEST_ASSERT_EQUAL(PSA_SUCCESS, status);
87+
status = psa_generate_random(output, sizeof(output));
88+
TEST_ASSERT_NOT_EQUAL(PSA_ERROR_BAD_STATE, status);
89+
mbedtls_psa_crypto_free();
90+
status = psa_generate_random(output, sizeof(output));
91+
TEST_ASSERT_EQUAL(PSA_ERROR_BAD_STATE, status);
92+
}
93+
94+
Case cases[] = {
95+
Case("PSA crypto-init De-init", check_crypto_init_deinit),
96+
Case("PSA crypto- multiple init De-init", check_multi_crypto_init_deinit),
97+
};
98+
99+
Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
100+
101+
int main()
102+
{
103+
return !Harness::run(specification);
104+
}

components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_crypto_partition.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#define mbedtls_calloc calloc
1717
#define mbedtls_free free
1818
#endif
19+
// ------------------------- Globals ---------------------------
20+
static psa_spm_init_refence_counter = 0;
1921

2022
// ------------------------- Partition's Main Thread ---------------------------
2123
static void psa_crypto_init_operation( void )
@@ -36,6 +38,8 @@ static void psa_crypto_init_operation( void )
3638
case PSA_IPC_CALL:
3739
{
3840
status = psa_crypto_init();
41+
if ( status == PSA_SUCCESS )
42+
++psa_spm_init_refence_counter;
3943
break;
4044
}
4145

@@ -65,7 +69,12 @@ static void psa_crypto_free_operation( void )
6569

6670
case PSA_IPC_CALL:
6771
{
68-
mbedtls_psa_crypto_free();
72+
/** perform crypto_free iff the number of init-s
73+
* is equal to the number of free-s
74+
*/
75+
--psa_spm_init_refence_counter;
76+
if (!psa_spm_init_refence_counter)
77+
mbedtls_psa_crypto_free();
6978
break;
7079
}
7180

0 commit comments

Comments
 (0)