|
| 1 | +/* |
| 2 | +* Copyright (c) 2019 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))) |
| 20 | +#error [NOT_SUPPORTED] Mbed 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 "psa_initial_attestation_api.h" |
| 27 | +#include "psa_attest_inject_key.h" |
| 28 | +#include <string.h> |
| 29 | +#include <stdlib.h> |
| 30 | + |
| 31 | +#include "entropy.h" |
| 32 | +#include "entropy_poll.h" |
| 33 | + |
| 34 | +/* MAX value support macro */ |
| 35 | +#if !defined(MAX) |
| 36 | +#define MAX(a,b) (((a)>(b))?(a):(b)) |
| 37 | +#endif |
| 38 | + |
| 39 | +#define MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE \ |
| 40 | +MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE) |
| 41 | + |
| 42 | +using namespace utest::v1; |
| 43 | + |
| 44 | +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
| 45 | +{ |
| 46 | +#ifndef NO_GREENTEA |
| 47 | + GREENTEA_SETUP(60, "default_auto"); |
| 48 | +#endif |
| 49 | + return greentea_test_setup_handler(number_of_cases); |
| 50 | +} |
| 51 | + |
| 52 | +#define PSA_ATTESTATION_PRIVATE_KEY_ID 17 |
| 53 | + |
| 54 | +static const uint8_t private_key_data[] = { |
| 55 | + 0x49, 0xc9, 0xa8, 0xc1, 0x8c, 0x4b, 0x88, 0x56, |
| 56 | + 0x38, 0xc4, 0x31, 0xcf, 0x1d, 0xf1, 0xc9, 0x94, |
| 57 | + 0x13, 0x16, 0x09, 0xb5, 0x80, 0xd4, 0xfd, 0x43, |
| 58 | + 0xa0, 0xca, 0xb1, 0x7d, 0xb2, 0xf1, 0x3e, 0xee |
| 59 | +}; |
| 60 | + |
| 61 | +static const uint8_t public_key_data[] = { |
| 62 | + 0x04, 0x77, 0x72, 0x65, 0x6f, 0x81, 0x4b, 0x39, |
| 63 | + 0x92, 0x79, 0xd5, 0xe1, 0xf1, 0x78, 0x1f, 0xac, |
| 64 | + 0x6f, 0x09, 0x9a, 0x3c, 0x5c, 0xa1, 0xb0, 0xe3, |
| 65 | + 0x53, 0x51, 0x83, 0x4b, 0x08, 0xb6, 0x5e, 0x0b, |
| 66 | + 0x57, 0x25, 0x90, 0xcd, 0xaf, 0x8f, 0x76, 0x93, |
| 67 | + 0x61, 0xbc, 0xf3, 0x4a, 0xcf, 0xc1, 0x1e, 0x5e, |
| 68 | + 0x07, 0x4e, 0x84, 0x26, 0xbd, 0xde, 0x04, 0xbe, |
| 69 | + 0x6e, 0x65, 0x39, 0x45, 0x44, 0x96, 0x17, 0xde, |
| 70 | + 0x45 |
| 71 | +}; |
| 72 | + |
| 73 | +#define TEST_TOKEN_SIZE (0x200) |
| 74 | +#define TEST_CHALLENGE_OBJ_SIZE (32u) |
| 75 | + |
| 76 | +#define CHALLENGE_FOR_TEST 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, \ |
| 77 | + 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, \ |
| 78 | + 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, \ |
| 79 | + 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF |
| 80 | + |
| 81 | +static uint8_t token_buffer[TEST_TOKEN_SIZE]; |
| 82 | +static uint8_t challenge_buffer[TEST_CHALLENGE_OBJ_SIZE] = {CHALLENGE_FOR_TEST}; |
| 83 | + |
| 84 | +static void check_initial_attestation_get_token() |
| 85 | +{ |
| 86 | + psa_status_t status = PSA_SUCCESS; |
| 87 | + size_t exported_length; |
| 88 | + uint8_t exported[sizeof(public_key_data)]; |
| 89 | + enum psa_attest_err_t attest_err = PSA_ATTEST_ERR_SUCCESS; |
| 90 | + uint32_t token_size; |
| 91 | + |
| 92 | + status = psa_crypto_init(); |
| 93 | + TEST_ASSERT_EQUAL(status, PSA_SUCCESS); |
| 94 | + status = psa_attestation_inject_key(private_key_data, |
| 95 | + sizeof(private_key_data), |
| 96 | + PSA_KEY_TYPE_ECC_KEYPAIR(PSA_ECC_CURVE_SECP256R1), |
| 97 | + exported, |
| 98 | + sizeof(exported), |
| 99 | + &exported_length); |
| 100 | + |
| 101 | + TEST_ASSERT_EQUAL(status, PSA_SUCCESS); |
| 102 | + TEST_ASSERT_EQUAL(sizeof(public_key_data), exported_length); |
| 103 | + TEST_ASSERT_EQUAL(memcmp(public_key_data, exported, exported_length), 0); |
| 104 | + |
| 105 | + attest_err = psa_initial_attest_get_token_size(TEST_CHALLENGE_OBJ_SIZE, |
| 106 | + &token_size); |
| 107 | + |
| 108 | + TEST_ASSERT_EQUAL(attest_err, PSA_ATTEST_ERR_SUCCESS); |
| 109 | + |
| 110 | + attest_err = psa_initial_attest_get_token(challenge_buffer, |
| 111 | + TEST_CHALLENGE_OBJ_SIZE, |
| 112 | + token_buffer, |
| 113 | + &token_size); |
| 114 | + |
| 115 | + TEST_ASSERT_EQUAL(attest_err, PSA_ATTEST_ERR_SUCCESS); |
| 116 | +} |
| 117 | +/***************************************************************************************/ |
| 118 | + |
| 119 | +utest::v1::status_t case_teardown_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t reason) |
| 120 | +{ |
| 121 | + const psa_key_id_t key_id = PSA_ATTESTATION_PRIVATE_KEY_ID; |
| 122 | + psa_key_handle_t handle = 0; |
| 123 | + psa_open_key(PSA_KEY_LIFETIME_PERSISTENT, key_id, &handle); |
| 124 | + psa_destroy_key(handle); |
| 125 | + mbedtls_psa_crypto_free(); |
| 126 | + return greentea_case_teardown_handler(source, passed, failed, reason); |
| 127 | +} |
| 128 | + |
| 129 | +utest::v1::status_t case_setup_handler(const Case *const source, const size_t index_of_case) |
| 130 | +{ |
| 131 | + return greentea_case_setup_handler(source, index_of_case); |
| 132 | +} |
| 133 | + |
| 134 | +Case cases[] = { |
| 135 | + Case("PSA attestation get token", check_initial_attestation_get_token, case_teardown_handler), |
| 136 | +}; |
| 137 | + |
| 138 | +Specification specification(greentea_test_setup, cases); |
| 139 | + |
| 140 | +int main() |
| 141 | +{ |
| 142 | +#if (defined(COMPONENT_PSA_SRV_IPC) || defined(MBEDTLS_ENTROPY_NV_SEED)) |
| 143 | + uint8_t seed[MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE] = {0}; |
| 144 | + /* inject some seed for test*/ |
| 145 | + for (int i = 0; i < MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE; ++i) { |
| 146 | + seed[i] = i; |
| 147 | + } |
| 148 | + |
| 149 | + /* don't really care if this succeeds this is just to make crypto init pass*/ |
| 150 | + mbedtls_psa_inject_entropy(seed, MBEDTLS_PSA_INJECT_ENTROPY_MIN_SIZE); |
| 151 | +#endif |
| 152 | + return !Harness::run(specification); |
| 153 | +} |
0 commit comments