blob: 5ceb59196b884e7ddc346e74b75ed48670a980e1 [file] [log] [blame]
Marc Moreno Berengue20dab392017-11-29 13:18:58 +00001/*
Tamas Banc1630532020-01-15 11:46:14 +00002 * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
Marc Moreno Berengue20dab392017-11-29 13:18:58 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Marc Moreno Berengueef202722018-08-10 13:43:43 +01008#ifndef __TFM_PLAT_CRYPTO_KEYS_H__
9#define __TFM_PLAT_CRYPTO_KEYS_H__
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000010/**
11 * \note The interfaces defined in this file must be implemented for each
12 * SoC.
13 */
Marc Moreno Berengueef202722018-08-10 13:43:43 +010014
Jamie Fox104f7502019-09-25 18:56:48 +010015#include <stddef.h>
Marc Moreno Berengueef202722018-08-10 13:43:43 +010016#include <stdint.h>
17#include "tfm_plat_defs.h"
Raef Coles4d6ea2f2019-10-15 14:30:40 +010018#include "psa/crypto.h"
Marc Moreno Berengueef202722018-08-10 13:43:43 +010019
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000020#ifdef __cplusplus
21extern "C" {
22#endif
23
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000024/**
Tamas Ban8f336232018-12-21 14:54:11 +000025 * 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 */
42struct 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 Ban5db57532019-07-17 10:59:02 +010051#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 */
60struct tfm_plat_rotpk_t {
61 const uint8_t *key_hash;
62 const uint8_t hash_len;
63};
64
Tamas Ban8f336232018-12-21 14:54:11 +000065/**
Jamie Fox104f7502019-09-25 18:56:48 +010066 * \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 */
78enum 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 Hu10eddf62020-01-17 15:12:13 +080085#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 */
103enum 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 Hu6d2bc652020-03-25 15:48:53 +0800107
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 */
122enum 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 Hu10eddf62020-01-17 15:12:13 +0800126#else /* SYMMETRIC_INITIAL_ATTESTATION */
Jamie Fox104f7502019-09-25 18:56:48 +0100127/**
Tamas Ban8f336232018-12-21 14:54:11 +0000128 * \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
Summer Qin0e5b2e02020-10-22 11:23:39 +0800151 * to according to \ref psa_ecc_family_t
Tamas Ban8f336232018-12-21 14:54:11 +0000152 *
153 * \return Returns error code specified in \ref tfm_plat_err_t
154 */
155enum tfm_plat_err_t
156tfm_plat_get_initial_attest_key(uint8_t *key_buf,
157 uint32_t size,
158 struct ecc_key_t *ecc_key,
Summer Qin0e5b2e02020-10-22 11:23:39 +0800159 psa_ecc_family_t *curve_type);
David Hu10eddf62020-01-17 15:12:13 +0800160#endif /* SYMMETRIC_INITIAL_ATTESTATION */
Tamas Ban8f336232018-12-21 14:54:11 +0000161
Tamas Ban24f55982019-07-17 10:51:15 +0100162/**
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 */
171enum tfm_plat_err_t
172tfm_plat_get_rotpk_hash(uint8_t image_id,
173 uint8_t *rotpk_hash,
174 uint32_t *rotpk_hash_size);
175
Marc Moreno Berengue20dab392017-11-29 13:18:58 +0000176#ifdef __cplusplus
177}
178#endif
179
Marc Moreno Berengueef202722018-08-10 13:43:43 +0100180#endif /* __TFM_PLAT_CRYPTO_KEYS_H__ */