|
| 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 | +} |
0 commit comments