blob: d4d755c6cef7de7ef331705b80d93567803cee06 [file] [log] [blame]
Marton Berke6fd21f12019-07-02 13:43:07 +02001/*
Mingyang Sunc9bdcd72020-06-04 11:44:49 +08002 * Copyright (c) 2017-2020 Arm Limited. All rights reserved.
Marton Berke6fd21f12019-07-02 13:43:07 +02003 *
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
Marton Berke6fd21f12019-07-02 13:43:07 +020017#include <stddef.h>
David Hu6d2bc652020-03-25 15:48:53 +080018#include <string.h>
19
Raef Coles4d6ea2f2019-10-15 14:30:40 +010020#include "psa/crypto_types.h"
David Hu6d2bc652020-03-25 15:48:53 +080021#include "tfm_plat_crypto_keys.h"
Marton Berke6fd21f12019-07-02 13:43:07 +020022
23/* FIXME: Functions in this file should be implemented by platform vendor. For
24 * the security of the storage system, it is critical to use a hardware unique
25 * key. For the security of the attestation, it is critical to use a unique key
26 * pair and keep the private key is secret.
27 */
28
29#define TFM_KEY_LEN_BYTES 16
30
31static const uint8_t sample_tfm_key[TFM_KEY_LEN_BYTES] =
32 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, \
33 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
34
David Hu10eddf62020-01-17 15:12:13 +080035#ifdef SYMMETRIC_INITIAL_ATTESTATION
36extern const psa_algorithm_t tfm_attest_hmac_sign_alg;
37extern const uint8_t initial_attestation_hmac_sha256_key[];
38extern const size_t initial_attestation_hmac_sha256_key_size;
David Hu6d2bc652020-03-25 15:48:53 +080039extern const char *initial_attestation_kid;
David Hu10eddf62020-01-17 15:12:13 +080040#else /* SYMMETRIC_INITIAL_ATTESTATION */
Raef Coles4d6ea2f2019-10-15 14:30:40 +010041extern const psa_ecc_curve_t initial_attestation_curve_type;
Marton Berke6fd21f12019-07-02 13:43:07 +020042extern const uint8_t initial_attestation_private_key[];
43extern const uint32_t initial_attestation_private_key_size;
David Hu10eddf62020-01-17 15:12:13 +080044#endif /* SYMMETRIC_INITIAL_ATTESTATION */
Marton Berke6fd21f12019-07-02 13:43:07 +020045
46extern const struct tfm_plat_rotpk_t device_rotpk[];
47extern const uint32_t rotpk_key_cnt;
48
49/**
50 * \brief Copy the key to the destination buffer
51 *
52 * \param[out] p_dst Pointer to buffer where to store the key
53 * \param[in] p_src Pointer to the key
54 * \param[in] size Length of the key
55 */
56static inline void copy_key(uint8_t *p_dst, const uint8_t *p_src, size_t size)
57{
58 uint32_t i;
59
60 for (i = size; i > 0; i--) {
61 *p_dst = *p_src;
62 p_src++;
63 p_dst++;
64 }
65}
66
Jamie Fox104f7502019-09-25 18:56:48 +010067enum tfm_plat_err_t tfm_plat_get_huk_derived_key(const uint8_t *label,
68 size_t label_size,
69 const uint8_t *context,
70 size_t context_size,
71 uint8_t *key,
72 size_t key_size)
73{
74 (void)label;
75 (void)label_size;
76 (void)context;
77 (void)context_size;
78
79 if (key_size > TFM_KEY_LEN_BYTES) {
80 return TFM_PLAT_ERR_SYSTEM_ERR;
81 }
82
83 /* FIXME: Do key derivation */
84 copy_key(key, sample_tfm_key, key_size);
85
86 return TFM_PLAT_ERR_SUCCESS;
87}
88
David Hu10eddf62020-01-17 15:12:13 +080089#ifdef SYMMETRIC_INITIAL_ATTESTATION
90enum tfm_plat_err_t tfm_plat_get_symmetric_iak(uint8_t *key_buf,
91 size_t buf_len,
92 size_t *key_len,
93 psa_algorithm_t *key_alg)
94{
95 if (!key_buf || !key_len || !key_alg) {
96 return TFM_PLAT_ERR_INVALID_INPUT;
97 }
98
99 if (buf_len < initial_attestation_hmac_sha256_key_size) {
100 return TFM_PLAT_ERR_INVALID_INPUT;
101 }
102
103 /*
104 * Actual implementation may derive a key with other input, other than
105 * directly providing provisioned symmetric initial attestation key.
106 */
107 copy_key(key_buf, initial_attestation_hmac_sha256_key,
108 initial_attestation_hmac_sha256_key_size);
109
110 *key_alg = tfm_attest_hmac_sign_alg;
111 *key_len = initial_attestation_hmac_sha256_key_size;
112
113 return TFM_PLAT_ERR_SUCCESS;
114}
David Hu6d2bc652020-03-25 15:48:53 +0800115
116enum tfm_plat_err_t tfm_plat_get_symmetric_iak_id(void *kid_buf,
117 size_t buf_len,
118 size_t *kid_len)
119{
120 /* kid is string in this example. '\0' is ignore. */
121 size_t len = strlen(initial_attestation_kid);
122
123 if (!kid_buf || !kid_len || (buf_len < len)) {
124 return TFM_PLAT_ERR_INVALID_INPUT;
125 }
126
127 copy_key(kid_buf, (const uint8_t *)initial_attestation_kid, len);
128 *kid_len = len;
129
130 return TFM_PLAT_ERR_SUCCESS;
131}
David Hu10eddf62020-01-17 15:12:13 +0800132#else /* SYMMETRIC_INITIAL_ATTESTATION */
Marton Berke6fd21f12019-07-02 13:43:07 +0200133enum tfm_plat_err_t
134tfm_plat_get_initial_attest_key(uint8_t *key_buf,
135 uint32_t size,
136 struct ecc_key_t *ecc_key,
Raef Coles4d6ea2f2019-10-15 14:30:40 +0100137 psa_ecc_curve_t *curve_type)
Marton Berke6fd21f12019-07-02 13:43:07 +0200138{
139 uint8_t *key_dst;
140 const uint8_t *key_src;
141 uint32_t key_size;
Raef Colesf51cb2d2019-10-15 14:27:53 +0100142 uint32_t full_key_size = initial_attestation_private_key_size;
Marton Berke6fd21f12019-07-02 13:43:07 +0200143
144 if (size < full_key_size) {
145 return TFM_PLAT_ERR_SYSTEM_ERR;
146 }
147
148 /* Set the EC curve type which the key belongs to */
149 *curve_type = initial_attestation_curve_type;
150
151 /* Copy the private key to the buffer, it MUST be present */
152 key_dst = key_buf;
153 key_src = initial_attestation_private_key;
154 key_size = initial_attestation_private_key_size;
155 copy_key(key_dst, key_src, key_size);
156 ecc_key->priv_key = key_dst;
157 ecc_key->priv_key_size = key_size;
158
Raef Colesf51cb2d2019-10-15 14:27:53 +0100159 ecc_key->pubx_key = NULL;
160 ecc_key->pubx_key_size = 0;
161 ecc_key->puby_key = NULL;
162 ecc_key->puby_key_size = 0;
Marton Berke6fd21f12019-07-02 13:43:07 +0200163
164 return TFM_PLAT_ERR_SUCCESS;
165}
David Hu10eddf62020-01-17 15:12:13 +0800166#endif /* SYMMETRIC_INITIAL_ATTESTATION */
Marton Berke6fd21f12019-07-02 13:43:07 +0200167
168#ifdef BL2
169enum tfm_plat_err_t
170tfm_plat_get_rotpk_hash(uint8_t image_id,
171 uint8_t *rotpk_hash,
172 uint32_t *rotpk_hash_size)
173{
174 if(*rotpk_hash_size < ROTPK_HASH_LEN) {
175 return TFM_PLAT_ERR_SYSTEM_ERR;
176 }
177
178 if (image_id >= rotpk_key_cnt) {
179 return TFM_PLAT_ERR_SYSTEM_ERR;
180 }
181
182 *rotpk_hash_size = ROTPK_HASH_LEN;
183 copy_key(rotpk_hash, device_rotpk[image_id].key_hash, *rotpk_hash_size);
184
185 return TFM_PLAT_ERR_SUCCESS;
186}
187#endif