1 /**
2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 * Description: cipher driver klad. \n
16 *
17 * History: \n
18 * 2023-03-22, Create file. \n
19 */
20 #include "drv_klad.h"
21
22 #include "crypto_drv_common.h"
23 #include "crypto_common_macro.h"
24
25 typedef struct {
26 crypto_kdf_hard_key_type hard_key_type;
27 crypto_klad_dest klad_dest;
28 crypto_klad_attr klad_attr;
29 td_handle keyslot_handle;
30 td_bool is_open;
31 td_bool is_attached;
32 td_bool is_set_attr;
33 } drv_klad_context;
34
35 static drv_klad_context g_klad_ctx = {0};
36
37 #define KLAD_VALID_HANDLE 0x2D3C4B5A
38
drv_klad_create(td_handle * klad_handle)39 td_s32 drv_klad_create(td_handle *klad_handle)
40 {
41 crypto_chk_return(klad_handle == TD_NULL, TD_FAILURE, "klad_handle is NULL\n");
42
43 (td_void)memset_s(&g_klad_ctx, sizeof(drv_klad_context), 0, sizeof(drv_klad_context));
44
45 g_klad_ctx.is_open = TD_TRUE;
46
47 *klad_handle = KLAD_VALID_HANDLE;
48 return TD_SUCCESS;
49 }
50
drv_klad_destroy(td_handle klad_handle)51 td_s32 drv_klad_destroy(td_handle klad_handle)
52 {
53 crypto_chk_return(klad_handle != KLAD_VALID_HANDLE, TD_FAILURE, "invalid klad_handle\n");
54 if (g_klad_ctx.is_open == TD_FALSE) {
55 return TD_SUCCESS;
56 }
57 (td_void)memset_s(&g_klad_ctx, sizeof(drv_klad_context), 0, sizeof(drv_klad_context));
58
59 return TD_SUCCESS;
60 }
61
drv_klad_attach(td_handle klad_handle,crypto_klad_dest klad_dest,td_handle keyslot_handle)62 td_s32 drv_klad_attach(td_handle klad_handle, crypto_klad_dest klad_dest, td_handle keyslot_handle)
63 {
64 volatile td_s32 ret = TD_FAILURE;
65 crypto_klad_flash_key_type flash_key_type = CRYPTO_KLAD_FLASH_KEY_TYPE_INVALID;
66
67 crypto_chk_return(klad_handle != KLAD_VALID_HANDLE, TD_FAILURE, "invalid klad_handle\n");
68 crypto_chk_return(g_klad_ctx.is_open == TD_FALSE, TD_FAILURE, "call create first\n");
69
70 if (klad_dest == CRYPTO_KLAD_DEST_FLASH) {
71 flash_key_type = (crypto_klad_flash_key_type)keyslot_handle;
72 } else {
73 ret = hal_klad_set_key_addr(klad_dest, keyslot_handle);
74 crypto_chk_return(ret != TD_SUCCESS, ret, "hal_klad_set_key_addr failed\n");
75 }
76
77 ret = hal_klad_set_key_dest_cfg(klad_dest, flash_key_type);
78 crypto_chk_return(ret != TD_SUCCESS, ret, "hal_klad_set_key_dest_cfg failed\n");
79
80 g_klad_ctx.keyslot_handle = keyslot_handle;
81 g_klad_ctx.klad_dest = klad_dest;
82 g_klad_ctx.is_attached = TD_TRUE;
83 return ret;
84 }
85
drv_klad_detach(td_handle klad_handle,crypto_klad_dest klad_dest,td_handle keyslot_handle)86 td_s32 drv_klad_detach(td_handle klad_handle, crypto_klad_dest klad_dest, td_handle keyslot_handle)
87 {
88 crypto_unused(keyslot_handle);
89 crypto_chk_return(klad_handle != KLAD_VALID_HANDLE, TD_FAILURE, "invalid klad_handle\n");
90 crypto_chk_return(g_klad_ctx.is_open == TD_FALSE, TD_FAILURE, "call create first\n");
91 crypto_chk_return(g_klad_ctx.klad_dest != klad_dest, TD_FAILURE, "invalid klad_dest\n");
92 crypto_chk_return(g_klad_ctx.keyslot_handle != keyslot_handle, TD_FAILURE, "invalid keyslot_handle\n");
93
94 if (g_klad_ctx.is_attached == TD_FALSE) {
95 return TD_SUCCESS;
96 }
97
98 g_klad_ctx.is_attached = TD_FALSE;
99 g_klad_ctx.keyslot_handle = 0;
100 g_klad_ctx.klad_dest = 0;
101
102 return TD_SUCCESS;
103 }
104
drv_klad_set_attr(td_handle klad_handle,const crypto_klad_attr * klad_attr)105 td_s32 drv_klad_set_attr(td_handle klad_handle, const crypto_klad_attr *klad_attr)
106 {
107 volatile td_s32 ret = TD_FAILURE;
108 const crypto_klad_key_config *key_cfg = TD_NULL;
109 const crypto_klad_key_secure_config *key_sec_cfg = TD_NULL;
110
111 crypto_chk_return(klad_attr == TD_NULL, TD_FAILURE, "klad_attr is NULL\n");
112 crypto_chk_return(klad_handle != KLAD_VALID_HANDLE, TD_FAILURE, "invalid klad_handle\n");
113 crypto_chk_return(g_klad_ctx.is_open == TD_FALSE, TD_FAILURE, "call create first\n");
114
115 ret = memcpy_s(&g_klad_ctx.klad_attr, sizeof(crypto_klad_attr), klad_attr, sizeof(crypto_klad_attr));
116 crypto_chk_return(ret != EOK, TD_FAILURE, "memcpy_s failed\n");
117
118 key_cfg = &(klad_attr->key_cfg);
119 key_sec_cfg = &(klad_attr->key_sec_cfg);
120
121 ret = hal_klad_set_key_crypto_cfg(key_cfg->encrypt_support, key_cfg->decrypt_support, key_cfg->engine);
122 crypto_chk_return(ret != TD_SUCCESS, ret, "hal_klad_set_key_crypto_cfg failed\n");
123
124 ret = hal_klad_set_key_secure_cfg(key_sec_cfg);
125 crypto_chk_return(ret != TD_SUCCESS, ret, "hal_klad_set_key_secure_cfg failed\n");
126
127 g_klad_ctx.hard_key_type = klad_attr->klad_cfg.rootkey_type;
128 g_klad_ctx.is_set_attr = TD_TRUE;
129
130 return ret;
131 }
132
drv_klad_set_clear_key(td_handle klad_handle,const crypto_klad_clear_key * clear_key)133 td_s32 drv_klad_set_clear_key(td_handle klad_handle, const crypto_klad_clear_key *clear_key)
134 {
135 volatile td_s32 ret = TD_FAILURE;
136
137 crypto_chk_return(klad_handle != KLAD_VALID_HANDLE, TD_FAILURE, "invalid klad_handle\n");
138 crypto_chk_return(g_klad_ctx.is_open == TD_FALSE, TD_FAILURE, "call create first\n");
139 crypto_chk_return(g_klad_ctx.is_set_attr == TD_FALSE, TD_FAILURE, "call set_attr first\n");
140
141 crypto_chk_return(clear_key == TD_NULL, TD_FAILURE, "clear_key is NULL\n");
142
143 ret = hal_klad_start_clr_route(g_klad_ctx.klad_dest, clear_key);
144 crypto_chk_return(ret != TD_SUCCESS, ret, "hal_klad_start_clr_route failed\n");
145
146 return ret;
147 }
148
drv_klad_set_effective_key(td_handle klad_handle,const crypto_klad_effective_key * content_key)149 td_s32 drv_klad_set_effective_key(td_handle klad_handle, const crypto_klad_effective_key *content_key)
150 {
151 volatile td_s32 ret = TD_FAILURE;
152 crypto_kdf_hard_calc_param param = {0};
153
154 crypto_chk_return(content_key == TD_NULL, TD_FAILURE, "content_key is NULL\n");
155 crypto_chk_return(content_key->salt == TD_NULL, TD_FAILURE, "content_key->salt is NULL\n");
156 if (content_key->kdf_hard_alg == CRYPTO_KDF_HARD_ALG_SM3) {
157 crypto_chk_return(crypto_sm_support(CRYPTO_SM_ALG_SM3) == TD_FALSE, TD_FAILURE,
158 "alg is unsupport\n");
159 }
160
161 crypto_chk_return(klad_handle != KLAD_VALID_HANDLE, TD_FAILURE, "invalid klad_handle\n");
162 crypto_chk_return(g_klad_ctx.is_open == TD_FALSE, TD_FAILURE, "call create first\n");
163 crypto_chk_return(g_klad_ctx.is_set_attr == TD_FALSE, TD_FAILURE, "call set_attr first\n");
164
165 switch (content_key->key_size) {
166 case CRYPTO_KLAD_KEY_SIZE_128BIT:
167 param.hard_key_size = CRYPTO_KDF_HARD_KEY_SIZE_128BIT;
168 break;
169 case CRYPTO_KLAD_KEY_SIZE_192BIT:
170 param.hard_key_size = CRYPTO_KDF_HARD_KEY_SIZE_192BIT;
171 break;
172 case CRYPTO_KLAD_KEY_SIZE_256BIT:
173 param.hard_key_size = CRYPTO_KDF_HARD_KEY_SIZE_256BIT;
174 break;
175 default:
176 crypto_log_err("invalid key_size\n");
177 return TD_FAILURE;
178 }
179
180 param.hard_key_type = g_klad_ctx.hard_key_type;
181 param.hard_alg = content_key->kdf_hard_alg;
182 param.salt = content_key->salt;
183 param.salt_length = content_key->salt_length;
184 param.is_oneway = content_key->oneway;
185
186 /* klad lock. */
187 ret = hal_klad_lock();
188 crypto_chk_return(ret != TD_SUCCESS, ret, "hal_klad_lock failed\n");
189
190 /* rkp lock. */
191 ret = hal_rkp_lock();
192 crypto_chk_goto(ret != TD_SUCCESS, exit_klad_unlock, "hal_rkp_lock failed\n");
193
194 /* rkp hard calculation. */
195 ret = hal_rkp_kdf_hard_calculation(¶m);
196 crypto_chk_goto(ret != TD_SUCCESS, exit_rkp_unlock, "hal_rkp_kdf_hard_calculation failed\n");
197
198 ret = hal_klad_start_com_route(param.hard_key_type, content_key, g_klad_ctx.klad_dest);
199 crypto_chk_goto(ret != TD_SUCCESS, exit_rkp_unlock, "hal_klad_start_com_route failed\n");
200
201 exit_rkp_unlock:
202 (td_void)hal_rkp_unlock();
203 exit_klad_unlock:
204 (td_void)hal_klad_unlock();
205 return ret;
206 }
207
drv_kdf_update(crypto_kdf_otp_key otp_key,crypto_kdf_update_alg alg)208 td_s32 drv_kdf_update(crypto_kdf_otp_key otp_key, crypto_kdf_update_alg alg)
209 {
210 volatile td_s32 ret = TD_FAILURE;
211
212 /* rkp lock. */
213 ret = hal_rkp_lock();
214 crypto_chk_return(ret != TD_SUCCESS, ret, "hal_rkp_lock failed\n");
215
216 ret = hal_rkp_deob_update(otp_key, alg);
217 crypto_chk_goto(ret != TD_SUCCESS, exit_rkp_unlock, "hal_rkp_deob_update failed\n");
218
219 exit_rkp_unlock:
220 (td_void)hal_rkp_unlock();
221 return ret;
222 }