Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 1 | /* |
Tamas Ban | c163053 | 2020-01-15 11:46:14 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, Arm Limited. All rights reserved. |
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Marc Moreno Berengue | ef20272 | 2018-08-10 13:43:43 +0100 | [diff] [blame] | 8 | #ifndef __TFM_PLAT_CRYPTO_KEYS_H__ |
| 9 | #define __TFM_PLAT_CRYPTO_KEYS_H__ |
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 10 | /** |
| 11 | * \note The interfaces defined in this file must be implemented for each |
| 12 | * SoC. |
| 13 | */ |
Marc Moreno Berengue | ef20272 | 2018-08-10 13:43:43 +0100 | [diff] [blame] | 14 | |
Jamie Fox | 104f750 | 2019-09-25 18:56:48 +0100 | [diff] [blame] | 15 | #include <stddef.h> |
Marc Moreno Berengue | ef20272 | 2018-08-10 13:43:43 +0100 | [diff] [blame] | 16 | #include <stdint.h> |
| 17 | #include "tfm_plat_defs.h" |
Raef Coles | 4d6ea2f | 2019-10-15 14:30:40 +0100 | [diff] [blame] | 18 | #include "psa/crypto.h" |
Marc Moreno Berengue | ef20272 | 2018-08-10 13:43:43 +0100 | [diff] [blame] | 19 | |
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 24 | /** |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 25 | * Structure definition to carry pointer and size information about an Elliptic |
| 26 | * curve key which is stored in a buffer(key_buf) in raw format (without |
| 27 | * encoding): |
| 28 | * - priv_key Base address of the private key in key_buf. It must be |
| 29 | * present on the device. |
| 30 | * - priv_key_size Size of the private key in bytes. |
| 31 | * - pubx_key Base address of x-coordinate of the public key in key_buf. |
| 32 | * It can be empty, because it can be recomputed based on |
| 33 | * private key. |
| 34 | * - pubx_key_size Length of x-coordinate of the public key in key_buf. |
| 35 | * It can be empty, because it can be recomputed based on |
| 36 | * private key. |
| 37 | * - puby_key Base address of y-coordinate of the public key in key_buf. |
| 38 | * It can be empty, because either it can be recomputed based |
| 39 | * on private key or some curve type works without it. |
| 40 | * - puby_key_size Length of y-coordinate of the public key in key_buf. |
| 41 | */ |
| 42 | struct ecc_key_t { |
| 43 | uint8_t *priv_key; |
| 44 | uint32_t priv_key_size; |
| 45 | uint8_t *pubx_key; |
| 46 | uint32_t pubx_key_size; |
| 47 | uint8_t *puby_key; |
| 48 | uint32_t puby_key_size; |
| 49 | }; |
| 50 | |
Tamas Ban | 5db5753 | 2019-07-17 10:59:02 +0100 | [diff] [blame] | 51 | #define ROTPK_HASH_LEN (32u) /* SHA256 */ |
| 52 | |
| 53 | /** |
| 54 | * Structure to store the hard-coded (embedded in secure firmware) hash of ROTPK |
| 55 | * for firmware authentication. |
| 56 | * |
| 57 | * \note Just temporary solution, hard-coded key-hash values in firmware is not |
| 58 | * suited for use in production! |
| 59 | */ |
| 60 | struct tfm_plat_rotpk_t { |
| 61 | const uint8_t *key_hash; |
| 62 | const uint8_t hash_len; |
| 63 | }; |
| 64 | |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 65 | /** |
Jamie Fox | 104f750 | 2019-09-25 18:56:48 +0100 | [diff] [blame] | 66 | * \brief Gets key material derived from the hardware unique key. |
| 67 | * |
| 68 | * \param[in] label Label for KDF |
| 69 | * \param[in] label_size Size of the label |
| 70 | * \param[in] context Context for KDF |
| 71 | * \param[in] context_size Size of the context |
| 72 | * \param[out] key Buffer to output the derived key material |
| 73 | * \param[in] key_size Requested size of the derived key material and |
| 74 | * minimum size of the key buffer |
| 75 | * |
| 76 | * \return Returns error code specified in \ref tfm_plat_err_t |
| 77 | */ |
| 78 | enum tfm_plat_err_t tfm_plat_get_huk_derived_key(const uint8_t *label, |
| 79 | size_t label_size, |
| 80 | const uint8_t *context, |
| 81 | size_t context_size, |
| 82 | uint8_t *key, |
| 83 | size_t key_size); |
| 84 | |
David Hu | 10eddf6 | 2020-01-17 15:12:13 +0800 | [diff] [blame] | 85 | #ifdef SYMMETRIC_INITIAL_ATTESTATION |
| 86 | /** |
| 87 | * \brief Get the symmetric Initial Attestation Key (IAK) |
| 88 | * |
| 89 | * The device MUST contain a symmetric IAK, which is used to sign the token. |
| 90 | * So far only HMAC is supported in symmetric key algorithm based Initial |
| 91 | * Attestation. |
| 92 | * Keys must be provided in raw format, just binary data without any encoding |
| 93 | * (DER, COSE). Caller provides a buffer to copy all the raw data. |
| 94 | * |
| 95 | * \param[out] key_buf Buffer to store the initial attestation key. |
| 96 | * \param[in] buf_len The length of buffer. |
| 97 | * \param[out] key_len Buffer to carry the length of the initial |
| 98 | * attestation key. |
| 99 | * \param[out] key_alg The key algorithm. Only HMAC is supported so far. |
| 100 | * |
| 101 | * \return Returns error code specified in \ref tfm_plat_err_t |
| 102 | */ |
| 103 | enum tfm_plat_err_t tfm_plat_get_symmetric_iak(uint8_t *key_buf, |
| 104 | size_t buf_len, |
| 105 | size_t *key_len, |
| 106 | psa_algorithm_t *key_alg); |
David Hu | 6d2bc65 | 2020-03-25 15:48:53 +0800 | [diff] [blame] | 107 | |
| 108 | #ifdef INCLUDE_COSE_KEY_ID |
| 109 | /** |
| 110 | * \brief Get the key identifier of the symmetric Initial Attestation Key as the |
| 111 | * 'kid' parameter in COSE Header. |
| 112 | * |
| 113 | * \note This `kid` parameter is included in COSE Header. Please don't confuse |
| 114 | * it with that `kid` in COSE_Key structure. |
| 115 | * |
| 116 | * \param[out] kid_buf The buffer to be written with key id |
| 117 | * \param[in] buf_len The length of kid_buf |
| 118 | * \param[out] kid_len The length of key id |
| 119 | * |
| 120 | * \return Returns error code specified in \ref tfm_plat_err_t. |
| 121 | */ |
| 122 | enum tfm_plat_err_t tfm_plat_get_symmetric_iak_id(void *kid_buf, |
| 123 | size_t buf_len, |
| 124 | size_t *kid_len); |
| 125 | #endif |
David Hu | 10eddf6 | 2020-01-17 15:12:13 +0800 | [diff] [blame] | 126 | #else /* SYMMETRIC_INITIAL_ATTESTATION */ |
Jamie Fox | 104f750 | 2019-09-25 18:56:48 +0100 | [diff] [blame] | 127 | /** |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 128 | * \brief Get the initial attestation key |
| 129 | * |
| 130 | * The device MUST contain an initial attestation key, which is used to sign the |
| 131 | * token. Initial attestation service supports elliptic curve signing |
| 132 | * algorithms. Device maker can decide whether store only the private key on the |
| 133 | * device or store both (public and private) key. Public key can be recomputed |
| 134 | * based on private key. Keys must be provided in raw format, just binary data |
| 135 | * without any encoding (DER, COSE). Caller provides a buffer to copy all the |
| 136 | * available key components to there. Key components must be copied after |
| 137 | * each other to the buffer. The base address and the length of each key |
| 138 | * component must be indicating in the corresponding field of ecc_key |
| 139 | * (\ref struct ecc_key_t). |
| 140 | * Curve_type indicates to which curve belongs the key. |
| 141 | * |
| 142 | * |
| 143 | * Keys must be provided in |
| 144 | * |
| 145 | * \param[in/out] key_buf Buffer to store the initial attestation key. |
| 146 | * \param[in] size Size of the buffer. |
| 147 | * \param[out] ecc_key A structure to carry pointer and size information |
| 148 | * about the initial attestation key, which is |
| 149 | * stored in key_buf. |
| 150 | * \param[out] curve_type The type of the EC curve, which the key belongs |
Raef Coles | 4d6ea2f | 2019-10-15 14:30:40 +0100 | [diff] [blame] | 151 | * to according to \ref psa_ecc_curve_t |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 152 | * |
| 153 | * \return Returns error code specified in \ref tfm_plat_err_t |
| 154 | */ |
| 155 | enum tfm_plat_err_t |
| 156 | tfm_plat_get_initial_attest_key(uint8_t *key_buf, |
| 157 | uint32_t size, |
| 158 | struct ecc_key_t *ecc_key, |
Raef Coles | 4d6ea2f | 2019-10-15 14:30:40 +0100 | [diff] [blame] | 159 | psa_ecc_curve_t *curve_type); |
David Hu | 10eddf6 | 2020-01-17 15:12:13 +0800 | [diff] [blame] | 160 | #endif /* SYMMETRIC_INITIAL_ATTESTATION */ |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 161 | |
Tamas Ban | 24f5598 | 2019-07-17 10:51:15 +0100 | [diff] [blame] | 162 | /** |
| 163 | * \brief Get the hash of the corresponding Root of Trust Public Key for |
| 164 | * firmware authentication. |
| 165 | * |
| 166 | * \param[in] image_id The identifier of firmware image |
| 167 | * \param[out] rotpk_hash Buffer to store the key-hash in |
| 168 | * \param[in,out] rotpk_hash_size As input the size of the buffer. As output |
| 169 | * the actual key-hash length. |
| 170 | */ |
| 171 | enum tfm_plat_err_t |
| 172 | tfm_plat_get_rotpk_hash(uint8_t image_id, |
| 173 | uint8_t *rotpk_hash, |
| 174 | uint32_t *rotpk_hash_size); |
| 175 | |
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 176 | #ifdef __cplusplus |
| 177 | } |
| 178 | #endif |
| 179 | |
Marc Moreno Berengue | ef20272 | 2018-08-10 13:43:43 +0100 | [diff] [blame] | 180 | #endif /* __TFM_PLAT_CRYPTO_KEYS_H__ */ |