1 /*
2  * Copyright (c) 2017-2023 Arm Limited. All rights reserved.
3  *
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 
17 #include <string.h>
18 #include "tfm_plat_crypto_keys.h"
19 #include "tfm_builtin_key_ids.h"
20 #include "tfm_plat_otp.h"
21 #include "psa_manifest/pid.h"
22 #include "tfm_builtin_key_loader.h"
23 
24 #define NUMBER_OF_ELEMENTS_OF(x) sizeof(x)/sizeof(*x)
25 #define TFM_NS_PARTITION_ID -1
26 
tfm_plat_get_huk(uint8_t * buf,size_t buf_len,size_t * key_len,size_t * key_bits,psa_algorithm_t * algorithm,psa_key_type_t * type)27 static enum tfm_plat_err_t tfm_plat_get_huk(uint8_t *buf, size_t buf_len,
28                                             size_t *key_len,
29                                             size_t *key_bits,
30                                             psa_algorithm_t *algorithm,
31                                             psa_key_type_t *type)
32 {
33     enum tfm_plat_err_t err;
34 
35     err = tfm_plat_otp_read(PLAT_OTP_ID_HUK, buf_len, buf);
36     if (err != TFM_PLAT_ERR_SUCCESS) {
37         return err;
38     }
39 
40     err = tfm_plat_otp_get_size(PLAT_OTP_ID_HUK, key_len);
41     if (err != TFM_PLAT_ERR_SUCCESS) {
42         return err;
43     }
44 
45     *key_bits = *key_len * 8;
46     *algorithm = PSA_ALG_HKDF(PSA_ALG_SHA_256);
47     *type = PSA_KEY_TYPE_DERIVE;
48 
49     return TFM_PLAT_ERR_SUCCESS;
50 }
51 
52 #ifdef TFM_PARTITION_INITIAL_ATTESTATION
tfm_plat_get_iak(uint8_t * buf,size_t buf_len,size_t * key_len,size_t * key_bits,psa_algorithm_t * algorithm,psa_key_type_t * type)53 static enum tfm_plat_err_t tfm_plat_get_iak(uint8_t *buf, size_t buf_len,
54                                             size_t *key_len,
55                                             size_t *key_bits,
56                                             psa_algorithm_t *algorithm,
57                                             psa_key_type_t *type)
58 {
59     enum tfm_plat_err_t err;
60 #ifndef SYMMETRIC_INITIAL_ATTESTATION
61     psa_ecc_family_t curve_type;
62 #endif /* SYMMETRIC_INITIAL_ATTESTATION */
63 
64     err = tfm_plat_otp_read(PLAT_OTP_ID_IAK_LEN,
65                             sizeof(size_t), (uint8_t*)key_len);
66     if(err != TFM_PLAT_ERR_SUCCESS) {
67         return err;
68     }
69     *key_bits = *key_len * 8;
70 
71     if (buf_len < *key_len) {
72         return TFM_PLAT_ERR_SYSTEM_ERR;
73     }
74 
75 #ifdef SYMMETRIC_INITIAL_ATTESTATION
76     err = tfm_plat_otp_read(PLAT_OTP_ID_IAK_TYPE,
77                             sizeof(psa_algorithm_t), (uint8_t*)algorithm);
78     if(err != TFM_PLAT_ERR_SUCCESS) {
79         return err;
80     }
81 
82     *type = PSA_KEY_TYPE_HMAC;
83 #else /* SYMMETRIC_INITIAL_ATTESTATION */
84     err = tfm_plat_otp_read(PLAT_OTP_ID_IAK_TYPE, sizeof(psa_ecc_family_t),
85                             &curve_type);
86     if(err != TFM_PLAT_ERR_SUCCESS) {
87         return err;
88     }
89 
90     *algorithm = PSA_ALG_ECDSA(PSA_ALG_SHA_256);
91     *type = PSA_KEY_TYPE_ECC_KEY_PAIR(curve_type);
92 #endif /* SYMMETRIC_INITIAL_ATTESTATION */
93 
94     return tfm_plat_otp_read(PLAT_OTP_ID_IAK, *key_len, buf);
95 }
96 #endif /* TFM_PARTITION_INITIAL_ATTESTATION */
97 
98 #ifdef TFM_PARTITION_INITIAL_ATTESTATION
99 /**
100  * @brief Table describing per-user key policy for the IAK
101  *
102  */
103 static const tfm_plat_builtin_key_per_user_policy_t g_iak_per_user_policy[] = {
104     {.user = TFM_SP_INITIAL_ATTESTATION,
105 #ifdef SYMMETRIC_INITIAL_ATTESTATION
106         .usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_EXPORT,
107 #else
108         .usage = PSA_KEY_USAGE_SIGN_HASH,
109 #endif /* SYMMETRIC_INITIAL_ATTESTATION */
110     },
111 #ifdef TEST_S_ATTESTATION
112     {.user = TFM_SP_SECURE_TEST_PARTITION, .usage = PSA_KEY_USAGE_VERIFY_HASH},
113 #endif /* TEST_S_ATTESTATION */
114 #ifdef TEST_NS_ATTESTATION
115     {.user = TFM_NS_PARTITION_ID, .usage = PSA_KEY_USAGE_VERIFY_HASH},
116 #endif /* TEST_NS_ATTESTATION */
117 };
118 #endif /* TFM_PARTITION_INITIAL_ATTESTATION */
119 
120 /**
121  * @brief Table describing per-key user policies
122  *
123  */
124 static const tfm_plat_builtin_key_policy_t g_builtin_keys_policy[] = {
125     {.key_id = TFM_BUILTIN_KEY_ID_HUK, .per_user_policy = 0, .usage = PSA_KEY_USAGE_DERIVE},
126 #ifdef TFM_PARTITION_INITIAL_ATTESTATION
127     {.key_id = TFM_BUILTIN_KEY_ID_IAK,
128      .per_user_policy = NUMBER_OF_ELEMENTS_OF(g_iak_per_user_policy),
129      .policy_ptr = g_iak_per_user_policy},
130 #endif /* TFM_PARTITION_INITIAL_ATTESTATION */
131 };
132 
133 /**
134  * @brief Table describing the builtin-in keys (plaform keys) available in the platform. Note
135  *        that to bind the keys to the tfm_builtin_key_loader driver, the lifetime must be
136  *        explicitly set to the one associated to the driver, i.e. TFM_BUILTIN_KEY_LOADER_LIFETIME
137  */
138 static const tfm_plat_builtin_key_descriptor_t g_builtin_keys_desc[] = {
139     {.key_id = TFM_BUILTIN_KEY_ID_HUK,
140      .slot_number = TFM_BUILTIN_KEY_SLOT_HUK,
141      .lifetime = TFM_BUILTIN_KEY_LOADER_LIFETIME,
142      .loader_key_func = tfm_plat_get_huk},
143 #ifdef TFM_PARTITION_INITIAL_ATTESTATION
144     {.key_id = TFM_BUILTIN_KEY_ID_IAK,
145      .slot_number = TFM_BUILTIN_KEY_SLOT_IAK,
146      .lifetime = TFM_BUILTIN_KEY_LOADER_LIFETIME,
147      .loader_key_func = tfm_plat_get_iak},
148 #endif /* TFM_PARTITION_INITIAL_ATTESTATION */
149 };
150 
tfm_plat_builtin_key_get_policy_table_ptr(const tfm_plat_builtin_key_policy_t * desc_ptr[])151 size_t tfm_plat_builtin_key_get_policy_table_ptr(const tfm_plat_builtin_key_policy_t *desc_ptr[])
152 {
153     *desc_ptr = &g_builtin_keys_policy[0];
154     return NUMBER_OF_ELEMENTS_OF(g_builtin_keys_policy);
155 }
156 
tfm_plat_builtin_key_get_desc_table_ptr(const tfm_plat_builtin_key_descriptor_t * desc_ptr[])157 size_t tfm_plat_builtin_key_get_desc_table_ptr(const tfm_plat_builtin_key_descriptor_t *desc_ptr[])
158 {
159     *desc_ptr = &g_builtin_keys_desc[0];
160     return NUMBER_OF_ELEMENTS_OF(g_builtin_keys_desc);
161 }
162