blob: e0052051799293e30118618c503167a9c2efb6eb [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
Mingyang Sunc9bdcd72020-06-04 11:44:49 +080017#include "tfm_plat_crypto_keys.h"
Marton Berke6fd21f12019-07-02 13:43:07 +020018#include <stddef.h>
Raef Coles4d6ea2f2019-10-15 14:30:40 +010019#include "psa/crypto_types.h"
Marton Berke6fd21f12019-07-02 13:43:07 +020020
21/* FIXME: Functions in this file should be implemented by platform vendor. For
22 * the security of the storage system, it is critical to use a hardware unique
23 * key. For the security of the attestation, it is critical to use a unique key
24 * pair and keep the private key is secret.
25 */
26
27#define TFM_KEY_LEN_BYTES 16
28
29static const uint8_t sample_tfm_key[TFM_KEY_LEN_BYTES] =
30 {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, \
31 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
32
David Hu10eddf62020-01-17 15:12:13 +080033#ifdef SYMMETRIC_INITIAL_ATTESTATION
34extern const psa_algorithm_t tfm_attest_hmac_sign_alg;
35extern const uint8_t initial_attestation_hmac_sha256_key[];
36extern const size_t initial_attestation_hmac_sha256_key_size;
37#else /* SYMMETRIC_INITIAL_ATTESTATION */
Raef Coles4d6ea2f2019-10-15 14:30:40 +010038extern const psa_ecc_curve_t initial_attestation_curve_type;
Marton Berke6fd21f12019-07-02 13:43:07 +020039extern const uint8_t initial_attestation_private_key[];
40extern const uint32_t initial_attestation_private_key_size;
David Hu10eddf62020-01-17 15:12:13 +080041#endif /* SYMMETRIC_INITIAL_ATTESTATION */
Marton Berke6fd21f12019-07-02 13:43:07 +020042
43extern const struct tfm_plat_rotpk_t device_rotpk[];
44extern const uint32_t rotpk_key_cnt;
45
46/**
47 * \brief Copy the key to the destination buffer
48 *
49 * \param[out] p_dst Pointer to buffer where to store the key
50 * \param[in] p_src Pointer to the key
51 * \param[in] size Length of the key
52 */
53static inline void copy_key(uint8_t *p_dst, const uint8_t *p_src, size_t size)
54{
55 uint32_t i;
56
57 for (i = size; i > 0; i--) {
58 *p_dst = *p_src;
59 p_src++;
60 p_dst++;
61 }
62}
63
Jamie Fox104f7502019-09-25 18:56:48 +010064enum tfm_plat_err_t tfm_plat_get_huk_derived_key(const uint8_t *label,
65 size_t label_size,
66 const uint8_t *context,
67 size_t context_size,
68 uint8_t *key,
69 size_t key_size)
70{
71 (void)label;
72 (void)label_size;
73 (void)context;
74 (void)context_size;
75
76 if (key_size > TFM_KEY_LEN_BYTES) {
77 return TFM_PLAT_ERR_SYSTEM_ERR;
78 }
79
80 /* FIXME: Do key derivation */
81 copy_key(key, sample_tfm_key, key_size);
82
83 return TFM_PLAT_ERR_SUCCESS;
84}
85
David Hu10eddf62020-01-17 15:12:13 +080086#ifdef SYMMETRIC_INITIAL_ATTESTATION
87enum tfm_plat_err_t tfm_plat_get_symmetric_iak(uint8_t *key_buf,
88 size_t buf_len,
89 size_t *key_len,
90 psa_algorithm_t *key_alg)
91{
92 if (!key_buf || !key_len || !key_alg) {
93 return TFM_PLAT_ERR_INVALID_INPUT;
94 }
95
96 if (buf_len < initial_attestation_hmac_sha256_key_size) {
97 return TFM_PLAT_ERR_INVALID_INPUT;
98 }
99
100 /*
101 * Actual implementation may derive a key with other input, other than
102 * directly providing provisioned symmetric initial attestation key.
103 */
104 copy_key(key_buf, initial_attestation_hmac_sha256_key,
105 initial_attestation_hmac_sha256_key_size);
106
107 *key_alg = tfm_attest_hmac_sign_alg;
108 *key_len = initial_attestation_hmac_sha256_key_size;
109
110 return TFM_PLAT_ERR_SUCCESS;
111}
112#else /* SYMMETRIC_INITIAL_ATTESTATION */
Marton Berke6fd21f12019-07-02 13:43:07 +0200113enum tfm_plat_err_t
114tfm_plat_get_initial_attest_key(uint8_t *key_buf,
115 uint32_t size,
116 struct ecc_key_t *ecc_key,
Raef Coles4d6ea2f2019-10-15 14:30:40 +0100117 psa_ecc_curve_t *curve_type)
Marton Berke6fd21f12019-07-02 13:43:07 +0200118{
119 uint8_t *key_dst;
120 const uint8_t *key_src;
121 uint32_t key_size;
Raef Colesf51cb2d2019-10-15 14:27:53 +0100122 uint32_t full_key_size = initial_attestation_private_key_size;
Marton Berke6fd21f12019-07-02 13:43:07 +0200123
124 if (size < full_key_size) {
125 return TFM_PLAT_ERR_SYSTEM_ERR;
126 }
127
128 /* Set the EC curve type which the key belongs to */
129 *curve_type = initial_attestation_curve_type;
130
131 /* Copy the private key to the buffer, it MUST be present */
132 key_dst = key_buf;
133 key_src = initial_attestation_private_key;
134 key_size = initial_attestation_private_key_size;
135 copy_key(key_dst, key_src, key_size);
136 ecc_key->priv_key = key_dst;
137 ecc_key->priv_key_size = key_size;
138
Raef Colesf51cb2d2019-10-15 14:27:53 +0100139 ecc_key->pubx_key = NULL;
140 ecc_key->pubx_key_size = 0;
141 ecc_key->puby_key = NULL;
142 ecc_key->puby_key_size = 0;
Marton Berke6fd21f12019-07-02 13:43:07 +0200143
144 return TFM_PLAT_ERR_SUCCESS;
145}
David Hu10eddf62020-01-17 15:12:13 +0800146#endif /* SYMMETRIC_INITIAL_ATTESTATION */
Marton Berke6fd21f12019-07-02 13:43:07 +0200147
148#ifdef BL2
149enum tfm_plat_err_t
150tfm_plat_get_rotpk_hash(uint8_t image_id,
151 uint8_t *rotpk_hash,
152 uint32_t *rotpk_hash_size)
153{
154 if(*rotpk_hash_size < ROTPK_HASH_LEN) {
155 return TFM_PLAT_ERR_SYSTEM_ERR;
156 }
157
158 if (image_id >= rotpk_key_cnt) {
159 return TFM_PLAT_ERR_SYSTEM_ERR;
160 }
161
162 *rotpk_hash_size = ROTPK_HASH_LEN;
163 copy_key(rotpk_hash, device_rotpk[image_id].key_hash, *rotpk_hash_size);
164
165 return TFM_PLAT_ERR_SUCCESS;
166}
167#endif