Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 1 | /* |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, 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 | * Elliptic curve key type identifiers according to RFC8152 (COSE encoding) |
| 26 | * https://www.iana.org/assignments/cose/cose.xhtml#elliptic-curves |
| 27 | */ |
Raef Coles | 4d6ea2f | 2019-10-15 14:30:40 +0100 | [diff] [blame] | 28 | enum cose_ecc_curve_t { |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 29 | P_256 = 1, /* NIST P-256 also known as secp256r1 */ |
| 30 | P_384 = 2, /* NIST P-384 also known as secp384r1 */ |
| 31 | P_521 = 3, /* NIST P-521 also known as secp521r1 */ |
| 32 | X25519 = 4, /* X25519 for use with ECDH only */ |
| 33 | X448 = 5, /* X448 for use with ECDH only */ |
| 34 | ED25519 = 6, /* Ed25519 for use with EdDSA only */ |
| 35 | ED448 = 7, /* Ed448 for use with EdDSA only */ |
| 36 | }; |
| 37 | |
| 38 | /** |
| 39 | * Structure definition to carry pointer and size information about an Elliptic |
| 40 | * curve key which is stored in a buffer(key_buf) in raw format (without |
| 41 | * encoding): |
| 42 | * - priv_key Base address of the private key in key_buf. It must be |
| 43 | * present on the device. |
| 44 | * - priv_key_size Size of the private key in bytes. |
| 45 | * - pubx_key Base address of x-coordinate of the public key in key_buf. |
| 46 | * It can be empty, because it can be recomputed based on |
| 47 | * private key. |
| 48 | * - pubx_key_size Length of x-coordinate of the public key in key_buf. |
| 49 | * It can be empty, because it can be recomputed based on |
| 50 | * private key. |
| 51 | * - puby_key Base address of y-coordinate of the public key in key_buf. |
| 52 | * It can be empty, because either it can be recomputed based |
| 53 | * on private key or some curve type works without it. |
| 54 | * - puby_key_size Length of y-coordinate of the public key in key_buf. |
| 55 | */ |
| 56 | struct ecc_key_t { |
| 57 | uint8_t *priv_key; |
| 58 | uint32_t priv_key_size; |
| 59 | uint8_t *pubx_key; |
| 60 | uint32_t pubx_key_size; |
| 61 | uint8_t *puby_key; |
| 62 | uint32_t puby_key_size; |
| 63 | }; |
| 64 | |
| 65 | #define ECC_P_256_KEY_SIZE (96u) /* 3 x 32 = 96 bytes priv + pub-x + pub-y */ |
| 66 | |
Tamas Ban | 5db5753 | 2019-07-17 10:59:02 +0100 | [diff] [blame] | 67 | #define ROTPK_HASH_LEN (32u) /* SHA256 */ |
| 68 | |
| 69 | /** |
| 70 | * Structure to store the hard-coded (embedded in secure firmware) hash of ROTPK |
| 71 | * for firmware authentication. |
| 72 | * |
| 73 | * \note Just temporary solution, hard-coded key-hash values in firmware is not |
| 74 | * suited for use in production! |
| 75 | */ |
| 76 | struct tfm_plat_rotpk_t { |
| 77 | const uint8_t *key_hash; |
| 78 | const uint8_t hash_len; |
| 79 | }; |
| 80 | |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 81 | /** |
Jamie Fox | 104f750 | 2019-09-25 18:56:48 +0100 | [diff] [blame] | 82 | * \brief Gets key material derived from the hardware unique key. |
| 83 | * |
| 84 | * \param[in] label Label for KDF |
| 85 | * \param[in] label_size Size of the label |
| 86 | * \param[in] context Context for KDF |
| 87 | * \param[in] context_size Size of the context |
| 88 | * \param[out] key Buffer to output the derived key material |
| 89 | * \param[in] key_size Requested size of the derived key material and |
| 90 | * minimum size of the key buffer |
| 91 | * |
| 92 | * \return Returns error code specified in \ref tfm_plat_err_t |
| 93 | */ |
| 94 | enum tfm_plat_err_t tfm_plat_get_huk_derived_key(const uint8_t *label, |
| 95 | size_t label_size, |
| 96 | const uint8_t *context, |
| 97 | size_t context_size, |
| 98 | uint8_t *key, |
| 99 | size_t key_size); |
| 100 | |
| 101 | /** |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 102 | * \brief Get the initial attestation key |
| 103 | * |
| 104 | * The device MUST contain an initial attestation key, which is used to sign the |
| 105 | * token. Initial attestation service supports elliptic curve signing |
| 106 | * algorithms. Device maker can decide whether store only the private key on the |
| 107 | * device or store both (public and private) key. Public key can be recomputed |
| 108 | * based on private key. Keys must be provided in raw format, just binary data |
| 109 | * without any encoding (DER, COSE). Caller provides a buffer to copy all the |
| 110 | * available key components to there. Key components must be copied after |
| 111 | * each other to the buffer. The base address and the length of each key |
| 112 | * component must be indicating in the corresponding field of ecc_key |
| 113 | * (\ref struct ecc_key_t). |
| 114 | * Curve_type indicates to which curve belongs the key. |
| 115 | * |
| 116 | * |
| 117 | * Keys must be provided in |
| 118 | * |
| 119 | * \param[in/out] key_buf Buffer to store the initial attestation key. |
| 120 | * \param[in] size Size of the buffer. |
| 121 | * \param[out] ecc_key A structure to carry pointer and size information |
| 122 | * about the initial attestation key, which is |
| 123 | * stored in key_buf. |
| 124 | * \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] | 125 | * to according to \ref psa_ecc_curve_t |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 126 | * |
| 127 | * \return Returns error code specified in \ref tfm_plat_err_t |
| 128 | */ |
| 129 | enum tfm_plat_err_t |
| 130 | tfm_plat_get_initial_attest_key(uint8_t *key_buf, |
| 131 | uint32_t size, |
| 132 | struct ecc_key_t *ecc_key, |
Raef Coles | 4d6ea2f | 2019-10-15 14:30:40 +0100 | [diff] [blame] | 133 | psa_ecc_curve_t *curve_type); |
Tamas Ban | 8f33623 | 2018-12-21 14:54:11 +0000 | [diff] [blame] | 134 | |
Tamas Ban | 24f5598 | 2019-07-17 10:51:15 +0100 | [diff] [blame] | 135 | /** |
| 136 | * \brief Get the hash of the corresponding Root of Trust Public Key for |
| 137 | * firmware authentication. |
| 138 | * |
| 139 | * \param[in] image_id The identifier of firmware image |
| 140 | * \param[out] rotpk_hash Buffer to store the key-hash in |
| 141 | * \param[in,out] rotpk_hash_size As input the size of the buffer. As output |
| 142 | * the actual key-hash length. |
| 143 | */ |
| 144 | enum tfm_plat_err_t |
| 145 | tfm_plat_get_rotpk_hash(uint8_t image_id, |
| 146 | uint8_t *rotpk_hash, |
| 147 | uint32_t *rotpk_hash_size); |
| 148 | |
Marc Moreno Berengue | 20dab39 | 2017-11-29 13:18:58 +0000 | [diff] [blame] | 149 | #ifdef __cplusplus |
| 150 | } |
| 151 | #endif |
| 152 | |
Marc Moreno Berengue | ef20272 | 2018-08-10 13:43:43 +0100 | [diff] [blame] | 153 | #endif /* __TFM_PLAT_CRYPTO_KEYS_H__ */ |