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 | |
Mingyang Sun | c9bdcd7 | 2020-06-04 11:44:49 +0800 | [diff] [blame^] | 17 | #include "tfm_plat_crypto_keys.h" |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 18 | #include <stddef.h> |
Raef Coles | 4d6ea2f | 2019-10-15 14:30:40 +0100 | [diff] [blame] | 19 | #include "psa/crypto_types.h" |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 20 | |
| 21 | /* FIXME: Functions in this file should be implemented by platform vendor. For |
| 22 | * the security of the storage system, it is critical to use a hardware unique |
| 23 | * key. For the security of the attestation, it is critical to use a unique key |
| 24 | * pair and keep the private key is secret. |
| 25 | */ |
| 26 | |
| 27 | #define TFM_KEY_LEN_BYTES 16 |
| 28 | |
| 29 | static const uint8_t sample_tfm_key[TFM_KEY_LEN_BYTES] = |
| 30 | {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, \ |
| 31 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F}; |
| 32 | |
Raef Coles | 4d6ea2f | 2019-10-15 14:30:40 +0100 | [diff] [blame] | 33 | extern const psa_ecc_curve_t initial_attestation_curve_type; |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 34 | extern const uint8_t initial_attestation_private_key[]; |
| 35 | extern const uint32_t initial_attestation_private_key_size; |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 36 | |
| 37 | extern const struct tfm_plat_rotpk_t device_rotpk[]; |
| 38 | extern const uint32_t rotpk_key_cnt; |
| 39 | |
| 40 | /** |
| 41 | * \brief Copy the key to the destination buffer |
| 42 | * |
| 43 | * \param[out] p_dst Pointer to buffer where to store the key |
| 44 | * \param[in] p_src Pointer to the key |
| 45 | * \param[in] size Length of the key |
| 46 | */ |
| 47 | static inline void copy_key(uint8_t *p_dst, const uint8_t *p_src, size_t size) |
| 48 | { |
| 49 | uint32_t i; |
| 50 | |
| 51 | for (i = size; i > 0; i--) { |
| 52 | *p_dst = *p_src; |
| 53 | p_src++; |
| 54 | p_dst++; |
| 55 | } |
| 56 | } |
| 57 | |
Jamie Fox | 104f750 | 2019-09-25 18:56:48 +0100 | [diff] [blame] | 58 | enum tfm_plat_err_t tfm_plat_get_huk_derived_key(const uint8_t *label, |
| 59 | size_t label_size, |
| 60 | const uint8_t *context, |
| 61 | size_t context_size, |
| 62 | uint8_t *key, |
| 63 | size_t key_size) |
| 64 | { |
| 65 | (void)label; |
| 66 | (void)label_size; |
| 67 | (void)context; |
| 68 | (void)context_size; |
| 69 | |
| 70 | if (key_size > TFM_KEY_LEN_BYTES) { |
| 71 | return TFM_PLAT_ERR_SYSTEM_ERR; |
| 72 | } |
| 73 | |
| 74 | /* FIXME: Do key derivation */ |
| 75 | copy_key(key, sample_tfm_key, key_size); |
| 76 | |
| 77 | return TFM_PLAT_ERR_SUCCESS; |
| 78 | } |
| 79 | |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 80 | enum tfm_plat_err_t |
| 81 | tfm_plat_get_initial_attest_key(uint8_t *key_buf, |
| 82 | uint32_t size, |
| 83 | struct ecc_key_t *ecc_key, |
Raef Coles | 4d6ea2f | 2019-10-15 14:30:40 +0100 | [diff] [blame] | 84 | psa_ecc_curve_t *curve_type) |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 85 | { |
| 86 | uint8_t *key_dst; |
| 87 | const uint8_t *key_src; |
| 88 | uint32_t key_size; |
Raef Coles | f51cb2d | 2019-10-15 14:27:53 +0100 | [diff] [blame] | 89 | uint32_t full_key_size = initial_attestation_private_key_size; |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 90 | |
| 91 | if (size < full_key_size) { |
| 92 | return TFM_PLAT_ERR_SYSTEM_ERR; |
| 93 | } |
| 94 | |
| 95 | /* Set the EC curve type which the key belongs to */ |
| 96 | *curve_type = initial_attestation_curve_type; |
| 97 | |
| 98 | /* Copy the private key to the buffer, it MUST be present */ |
| 99 | key_dst = key_buf; |
| 100 | key_src = initial_attestation_private_key; |
| 101 | key_size = initial_attestation_private_key_size; |
| 102 | copy_key(key_dst, key_src, key_size); |
| 103 | ecc_key->priv_key = key_dst; |
| 104 | ecc_key->priv_key_size = key_size; |
| 105 | |
Raef Coles | f51cb2d | 2019-10-15 14:27:53 +0100 | [diff] [blame] | 106 | ecc_key->pubx_key = NULL; |
| 107 | ecc_key->pubx_key_size = 0; |
| 108 | ecc_key->puby_key = NULL; |
| 109 | ecc_key->puby_key_size = 0; |
Marton Berke | 6fd21f1 | 2019-07-02 13:43:07 +0200 | [diff] [blame] | 110 | |
| 111 | return TFM_PLAT_ERR_SUCCESS; |
| 112 | } |
| 113 | |
| 114 | #ifdef BL2 |
| 115 | enum tfm_plat_err_t |
| 116 | tfm_plat_get_rotpk_hash(uint8_t image_id, |
| 117 | uint8_t *rotpk_hash, |
| 118 | uint32_t *rotpk_hash_size) |
| 119 | { |
| 120 | if(*rotpk_hash_size < ROTPK_HASH_LEN) { |
| 121 | return TFM_PLAT_ERR_SYSTEM_ERR; |
| 122 | } |
| 123 | |
| 124 | if (image_id >= rotpk_key_cnt) { |
| 125 | return TFM_PLAT_ERR_SYSTEM_ERR; |
| 126 | } |
| 127 | |
| 128 | *rotpk_hash_size = ROTPK_HASH_LEN; |
| 129 | copy_key(rotpk_hash, device_rotpk[image_id].key_hash, *rotpk_hash_size); |
| 130 | |
| 131 | return TFM_PLAT_ERR_SUCCESS; |
| 132 | } |
| 133 | #endif |