Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 1 | /* |
Mingyang Sun | c9bdcd7 | 2020-06-04 11:44:49 +0800 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. All rights reserved. |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 17 | #include <stddef.h> |
David Hu | 6d2bc65 | 2020-03-25 15:48:53 +0800 | [diff] [blame] | 18 | #include <string.h> |
| 19 | |
Raef Coles | 4d6ea2f | 2019-10-15 14:30:40 +0100 | [diff] [blame] | 20 | #include "psa/crypto_types.h" |
David Hu | 6d2bc65 | 2020-03-25 15:48:53 +0800 | [diff] [blame] | 21 | #include "tfm_plat_crypto_keys.h" |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 22 | |
| 23 | /* FIXME: Functions in this file should be implemented by platform vendor. For |
| 24 | * the security of the storage system, it is critical to use a hardware unique |
| 25 | * key. For the security of the attestation, it is critical to use a unique key |
| 26 | * pair and keep the private key is secret. |
| 27 | */ |
| 28 | |
| 29 | #define TFM_KEY_LEN_BYTES 16 |
| 30 | |
| 31 | static const uint8_t sample_tfm_key[TFM_KEY_LEN_BYTES] = |
| 32 | {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, \ |
| 33 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; |
| 34 | |
David Hu | 10eddf6 | 2020-01-17 15:12:13 +0800 | [diff] [blame] | 35 | #ifdef SYMMETRIC_INITIAL_ATTESTATION |
| 36 | extern const psa_algorithm_t tfm_attest_hmac_sign_alg; |
| 37 | extern const uint8_t initial_attestation_hmac_sha256_key[]; |
| 38 | extern const size_t initial_attestation_hmac_sha256_key_size; |
David Hu | 6d2bc65 | 2020-03-25 15:48:53 +0800 | [diff] [blame] | 39 | extern const char *initial_attestation_kid; |
David Hu | 10eddf6 | 2020-01-17 15:12:13 +0800 | [diff] [blame] | 40 | #else /* SYMMETRIC_INITIAL_ATTESTATION */ |
Summer Qin | 0e5b2e0 | 2020-10-22 11:23:39 +0800 | [diff] [blame] | 41 | extern const psa_ecc_family_t initial_attestation_curve_type; |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 42 | extern const uint8_t initial_attestation_private_key[]; |
| 43 | extern const uint32_t initial_attestation_private_key_size; |
David Hu | 10eddf6 | 2020-01-17 15:12:13 +0800 | [diff] [blame] | 44 | #endif /* SYMMETRIC_INITIAL_ATTESTATION */ |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 45 | |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 46 | /** |
| 47 | * \brief Copy the key to the destination buffer |
| 48 | * |
| 49 | * \param[out] p_dst Pointer to buffer where to store the key |
| 50 | * \param[in] p_src Pointer to the key |
| 51 | * \param[in] size Length of the key |
| 52 | */ |
| 53 | static inline void copy_key(uint8_t *p_dst, const uint8_t *p_src, size_t size) |
| 54 | { |
| 55 | uint32_t i; |
| 56 | |
| 57 | for (i = size; i > 0; i--) { |
| 58 | *p_dst = *p_src; |
| 59 | p_src++; |
| 60 | p_dst++; |
| 61 | } |
| 62 | } |
| 63 | |
Jamie Fox | 104f750 | 2019-09-25 18:56:48 +0100 | [diff] [blame] | 64 | enum tfm_plat_err_t tfm_plat_get_huk_derived_key(const uint8_t *label, |
| 65 | size_t label_size, |
| 66 | const uint8_t *context, |
| 67 | size_t context_size, |
| 68 | uint8_t *key, |
| 69 | size_t key_size) |
| 70 | { |
| 71 | (void)label; |
| 72 | (void)label_size; |
| 73 | (void)context; |
| 74 | (void)context_size; |
| 75 | |
| 76 | if (key_size > TFM_KEY_LEN_BYTES) { |
| 77 | return TFM_PLAT_ERR_SYSTEM_ERR; |
| 78 | } |
| 79 | |
| 80 | /* FIXME: Do key derivation */ |
| 81 | copy_key(key, sample_tfm_key, key_size); |
| 82 | |
| 83 | return TFM_PLAT_ERR_SUCCESS; |
| 84 | } |
| 85 | |
David Hu | 10eddf6 | 2020-01-17 15:12:13 +0800 | [diff] [blame] | 86 | #ifdef SYMMETRIC_INITIAL_ATTESTATION |
| 87 | enum tfm_plat_err_t tfm_plat_get_symmetric_iak(uint8_t *key_buf, |
| 88 | size_t buf_len, |
| 89 | size_t *key_len, |
| 90 | psa_algorithm_t *key_alg) |
| 91 | { |
| 92 | if (!key_buf || !key_len || !key_alg) { |
| 93 | return TFM_PLAT_ERR_INVALID_INPUT; |
| 94 | } |
| 95 | |
| 96 | if (buf_len < initial_attestation_hmac_sha256_key_size) { |
| 97 | return TFM_PLAT_ERR_INVALID_INPUT; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Actual implementation may derive a key with other input, other than |
| 102 | * directly providing provisioned symmetric initial attestation key. |
| 103 | */ |
| 104 | copy_key(key_buf, initial_attestation_hmac_sha256_key, |
| 105 | initial_attestation_hmac_sha256_key_size); |
| 106 | |
| 107 | *key_alg = tfm_attest_hmac_sign_alg; |
| 108 | *key_len = initial_attestation_hmac_sha256_key_size; |
| 109 | |
| 110 | return TFM_PLAT_ERR_SUCCESS; |
| 111 | } |
David Hu | 6d2bc65 | 2020-03-25 15:48:53 +0800 | [diff] [blame] | 112 | |
| 113 | enum tfm_plat_err_t tfm_plat_get_symmetric_iak_id(void *kid_buf, |
| 114 | size_t buf_len, |
| 115 | size_t *kid_len) |
| 116 | { |
| 117 | /* kid is string in this example. '\0' is ignore. */ |
| 118 | size_t len = strlen(initial_attestation_kid); |
| 119 | |
| 120 | if (!kid_buf || !kid_len || (buf_len < len)) { |
| 121 | return TFM_PLAT_ERR_INVALID_INPUT; |
| 122 | } |
| 123 | |
| 124 | copy_key(kid_buf, (const uint8_t *)initial_attestation_kid, len); |
| 125 | *kid_len = len; |
| 126 | |
| 127 | return TFM_PLAT_ERR_SUCCESS; |
| 128 | } |
David Hu | 10eddf6 | 2020-01-17 15:12:13 +0800 | [diff] [blame] | 129 | #else /* SYMMETRIC_INITIAL_ATTESTATION */ |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 130 | enum tfm_plat_err_t |
| 131 | tfm_plat_get_initial_attest_key(uint8_t *key_buf, |
| 132 | uint32_t size, |
| 133 | struct ecc_key_t *ecc_key, |
Summer Qin | 0e5b2e0 | 2020-10-22 11:23:39 +0800 | [diff] [blame] | 134 | psa_ecc_family_t *curve_type) |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 135 | { |
| 136 | uint8_t *key_dst; |
| 137 | const uint8_t *key_src; |
| 138 | uint32_t key_size; |
Raef Coles | f51cb2d | 2019-10-15 14:27:53 +0100 | [diff] [blame] | 139 | uint32_t full_key_size = initial_attestation_private_key_size; |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 140 | |
| 141 | if (size < full_key_size) { |
| 142 | return TFM_PLAT_ERR_SYSTEM_ERR; |
| 143 | } |
| 144 | |
| 145 | /* Set the EC curve type which the key belongs to */ |
| 146 | *curve_type = initial_attestation_curve_type; |
| 147 | |
| 148 | /* Copy the private key to the buffer, it MUST be present */ |
| 149 | key_dst = key_buf; |
| 150 | key_src = initial_attestation_private_key; |
| 151 | key_size = initial_attestation_private_key_size; |
| 152 | copy_key(key_dst, key_src, key_size); |
| 153 | ecc_key->priv_key = key_dst; |
| 154 | ecc_key->priv_key_size = key_size; |
| 155 | |
Raef Coles | f51cb2d | 2019-10-15 14:27:53 +0100 | [diff] [blame] | 156 | ecc_key->pubx_key = NULL; |
| 157 | ecc_key->pubx_key_size = 0; |
| 158 | ecc_key->puby_key = NULL; |
| 159 | ecc_key->puby_key_size = 0; |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 160 | |
| 161 | return TFM_PLAT_ERR_SUCCESS; |
| 162 | } |
David Hu | 10eddf6 | 2020-01-17 15:12:13 +0800 | [diff] [blame] | 163 | #endif /* SYMMETRIC_INITIAL_ATTESTATION */ |