blob: 81c54a7c6bc87746b241d9ca9ec96c3bc18b4dc5 [file] [log] [blame]
Marc Moreno Berengue20dab392017-11-29 13:18:58 +00001/*
Tamas Ban8f336232018-12-21 14:54:11 +00002 * Copyright (c) 2017-2019, 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
15#include <stdint.h>
16#include "tfm_plat_defs.h"
Raef Coles4d6ea2f2019-10-15 14:30:40 +010017#include "psa/crypto.h"
Marc Moreno Berengueef202722018-08-10 13:43:43 +010018
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000019#ifdef __cplusplus
20extern "C" {
21#endif
22
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000023/**
Tamas Ban8f336232018-12-21 14:54:11 +000024 * Elliptic curve key type identifiers according to RFC8152 (COSE encoding)
25 * https://www.iana.org/assignments/cose/cose.xhtml#elliptic-curves
26 */
Raef Coles4d6ea2f2019-10-15 14:30:40 +010027enum cose_ecc_curve_t {
Tamas Ban8f336232018-12-21 14:54:11 +000028 P_256 = 1, /* NIST P-256 also known as secp256r1 */
29 P_384 = 2, /* NIST P-384 also known as secp384r1 */
30 P_521 = 3, /* NIST P-521 also known as secp521r1 */
31 X25519 = 4, /* X25519 for use with ECDH only */
32 X448 = 5, /* X448 for use with ECDH only */
33 ED25519 = 6, /* Ed25519 for use with EdDSA only */
34 ED448 = 7, /* Ed448 for use with EdDSA only */
35};
36
37/**
38 * Structure definition to carry pointer and size information about an Elliptic
39 * curve key which is stored in a buffer(key_buf) in raw format (without
40 * encoding):
41 * - priv_key Base address of the private key in key_buf. It must be
42 * present on the device.
43 * - priv_key_size Size of the private key in bytes.
44 * - pubx_key Base address of x-coordinate of the public key in key_buf.
45 * It can be empty, because it can be recomputed based on
46 * private key.
47 * - pubx_key_size Length of x-coordinate of the public key in key_buf.
48 * It can be empty, because it can be recomputed based on
49 * private key.
50 * - puby_key Base address of y-coordinate of the public key in key_buf.
51 * It can be empty, because either it can be recomputed based
52 * on private key or some curve type works without it.
53 * - puby_key_size Length of y-coordinate of the public key in key_buf.
54 */
55struct ecc_key_t {
56 uint8_t *priv_key;
57 uint32_t priv_key_size;
58 uint8_t *pubx_key;
59 uint32_t pubx_key_size;
60 uint8_t *puby_key;
61 uint32_t puby_key_size;
62};
63
64#define ECC_P_256_KEY_SIZE (96u) /* 3 x 32 = 96 bytes priv + pub-x + pub-y */
65
Tamas Ban5db57532019-07-17 10:59:02 +010066#define ROTPK_HASH_LEN (32u) /* SHA256 */
67
68/**
69 * Structure to store the hard-coded (embedded in secure firmware) hash of ROTPK
70 * for firmware authentication.
71 *
72 * \note Just temporary solution, hard-coded key-hash values in firmware is not
73 * suited for use in production!
74 */
75struct tfm_plat_rotpk_t {
76 const uint8_t *key_hash;
77 const uint8_t hash_len;
78};
79
Tamas Ban8f336232018-12-21 14:54:11 +000080/**
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000081 * \brief Gets hardware unique key for encryption
82 *
83 * \param[out] key Buf to store the key in
84 * \param[in] size Size of the buffer
85 *
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010086 * \return Returns error code specified in \ref tfm_plat_err_t
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000087 */
Marc Moreno Berengue9926df82018-08-10 13:45:52 +010088enum tfm_plat_err_t tfm_plat_get_crypto_huk(uint8_t *key, uint32_t size);
Marc Moreno Berengue20dab392017-11-29 13:18:58 +000089
Tamas Ban8f336232018-12-21 14:54:11 +000090/**
91 * \brief Get the initial attestation key
92 *
93 * The device MUST contain an initial attestation key, which is used to sign the
94 * token. Initial attestation service supports elliptic curve signing
95 * algorithms. Device maker can decide whether store only the private key on the
96 * device or store both (public and private) key. Public key can be recomputed
97 * based on private key. Keys must be provided in raw format, just binary data
98 * without any encoding (DER, COSE). Caller provides a buffer to copy all the
99 * available key components to there. Key components must be copied after
100 * each other to the buffer. The base address and the length of each key
101 * component must be indicating in the corresponding field of ecc_key
102 * (\ref struct ecc_key_t).
103 * Curve_type indicates to which curve belongs the key.
104 *
105 *
106 * Keys must be provided in
107 *
108 * \param[in/out] key_buf Buffer to store the initial attestation key.
109 * \param[in] size Size of the buffer.
110 * \param[out] ecc_key A structure to carry pointer and size information
111 * about the initial attestation key, which is
112 * stored in key_buf.
113 * \param[out] curve_type The type of the EC curve, which the key belongs
Raef Coles4d6ea2f2019-10-15 14:30:40 +0100114 * to according to \ref psa_ecc_curve_t
Tamas Ban8f336232018-12-21 14:54:11 +0000115 *
116 * \return Returns error code specified in \ref tfm_plat_err_t
117 */
118enum tfm_plat_err_t
119tfm_plat_get_initial_attest_key(uint8_t *key_buf,
120 uint32_t size,
121 struct ecc_key_t *ecc_key,
Raef Coles4d6ea2f2019-10-15 14:30:40 +0100122 psa_ecc_curve_t *curve_type);
Tamas Ban8f336232018-12-21 14:54:11 +0000123
Tamas Ban24f55982019-07-17 10:51:15 +0100124/**
125 * \brief Get the hash of the corresponding Root of Trust Public Key for
126 * firmware authentication.
127 *
128 * \param[in] image_id The identifier of firmware image
129 * \param[out] rotpk_hash Buffer to store the key-hash in
130 * \param[in,out] rotpk_hash_size As input the size of the buffer. As output
131 * the actual key-hash length.
132 */
133enum tfm_plat_err_t
134tfm_plat_get_rotpk_hash(uint8_t image_id,
135 uint8_t *rotpk_hash,
136 uint32_t *rotpk_hash_size);
137
Marc Moreno Berengue20dab392017-11-29 13:18:58 +0000138#ifdef __cplusplus
139}
140#endif
141
Marc Moreno Berengueef202722018-08-10 13:43:43 +0100142#endif /* __TFM_PLAT_CRYPTO_KEYS_H__ */