• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 /*
17  * CONFIG_FLASH_ENCRYPT_SUPPORT:
18  * --Determine that the flash crypto function is supported;
19  *   This configuration can be modified in menuconfig.
20  */
21 #ifdef CONFIG_FLASH_ENCRYPT_SUPPORT
22 #include <hi_cipher.h>
23 #include "crypto.h"
24 #include <boot_upg_check.h>
25 #include <boot_upg_start_up.h>
26 
27 boot_crypto_ctx g_boot_crypto_param = {0};
28 boot_crypto_ctx g_boot_decrypt_param = {0};
boot_crypto_get_ctx(hi_void)29 boot_crypto_ctx *boot_crypto_get_ctx(hi_void)
30 {
31     return &g_boot_crypto_param;
32 }
33 
boot_decrypt_get_ctx(hi_void)34 boot_crypto_ctx *boot_decrypt_get_ctx(hi_void)
35 {
36     return &g_boot_decrypt_param;
37 }
38 
crypto_check_image_id(hi_u32 image_id)39 hi_u32 crypto_check_image_id(hi_u32 image_id)
40 {
41     if (image_id != PRODUCT_UPG_FILE_IMAGE_ID) {
42         boot_msg1("[crypto check image id]fail, id:", image_id);
43         return HI_PRINT_ERRNO_CRYPTO_CHECK_KERNEL_ERR;
44     }
45     return HI_ERR_SUCCESS;
46 }
47 
crypto_check_kernel_file(hi_u32 addr_start)48 hi_bool crypto_check_kernel_file(hi_u32 addr_start)
49 {
50     hi_u32 ret;
51     hi_upg_head upg_head;
52     ret = hi_flash_read(addr_start, sizeof(hi_upg_head), (hi_u8 *)&upg_head);
53     if (ret != HI_ERR_SUCCESS) {
54         return HI_FALSE;
55     }
56     hi_upg_common_head *common_head = &(upg_head.common);
57 
58     ret = crypto_check_image_id(common_head->image_id);
59     if (ret != HI_ERR_SUCCESS) {
60         return HI_FALSE;
61     }
62 
63     return HI_TRUE;
64 }
65 
change_kernel(hi_void)66 hi_void change_kernel(hi_void)
67 {
68     hi_nv_ftm_startup_cfg *cfg = boot_upg_get_cfg();
69     hi_flash_partition_table *partition = hi_get_partition_table();
70     uintptr_t kernel_a_addr = partition->table[HI_FLASH_PARTITON_KERNEL_A].addr;
71     uintptr_t kernel_b_addr = partition->table[HI_FLASH_PARTITON_KERNEL_B].addr;
72 
73     if (cfg->addr_start == kernel_a_addr) {
74         cfg->addr_start = kernel_b_addr;
75     } else {
76         cfg->addr_start = kernel_a_addr;
77     }
78 }
79 
boot_get_crypto_kernel_start(hi_u32 * flash_offset)80 hi_u32 boot_get_crypto_kernel_start(hi_u32 *flash_offset)
81 {
82     hi_nv_ftm_startup_cfg *cfg = boot_upg_get_cfg();
83     if (!crypto_check_kernel_file(cfg->addr_start)) {
84         return HI_PRINT_ERRNO_CRYPTO_CHECK_KERNEL_ERR;
85     }
86 
87     boot_get_start_addr_offset(cfg->addr_start, flash_offset);
88 
89     return HI_ERR_SUCCESS;
90 }
91 
crypto_clear_content(hi_u8 * content,hi_u32 content_len)92 hi_void crypto_clear_content(hi_u8 *content, hi_u32 content_len)
93 {
94     if ((content == HI_NULL) || (content_len == 0)) {
95         return;
96     }
97 
98     hi_u32 cs = (uintptr_t)content ^ content_len ^ 0x0 ^ content_len;
99     (hi_void)memset_s(content, content_len, 0x0, content_len, cs);
100 }
101 
crypto_load_salt(crypto_workkey_partition part,hi_flash_crypto_content * key_content)102 static hi_u32 crypto_load_salt(crypto_workkey_partition part, hi_flash_crypto_content *key_content)
103 {
104     hi_u32 ret = HI_ERR_SUCCESS;
105     hi_u8 salt_e[ROOT_SALT_LENGTH] = { 0 };
106 
107     hi_u32 cs = (uintptr_t)salt_e ^ (hi_u32)sizeof(salt_e) ^ 0x0 ^ ROOT_SALT_LENGTH;
108     (hi_void) memset_s(salt_e, sizeof(salt_e), 0x0, ROOT_SALT_LENGTH, cs);
109     if (part == CRYPTO_WORKKEY_KERNEL_A) {
110         ret = hi_factory_nv_read(HI_NV_FTM_KERNELA_WORK_ID, key_content, sizeof(hi_flash_crypto_content), 0);
111         if (ret != HI_ERR_SUCCESS) {
112             goto fail;
113         }
114     } else if (part == CRYPTO_WORKKEY_KERNEL_A_BACKUP) {
115         ret = hi_factory_nv_read(HI_NV_FTM_BACKUP_KERNELA_WORK_ID, key_content, sizeof(hi_flash_crypto_content), 0);
116         if (ret != HI_ERR_SUCCESS) {
117             goto fail;
118         }
119     }
120 
121     if (memcmp(key_content->root_salt, salt_e, ROOT_SALT_LENGTH) == HI_ERR_SUCCESS) {
122         ret = HI_PRINT_ERRNO_CRYPTO_SALT_EMPTY_ERR;
123         goto fail;
124     }
125 fail:
126     return ret;
127 }
128 
crypto_get_root_salt(hi_flash_crypto_content * key_content)129 static hi_u32 crypto_get_root_salt(hi_flash_crypto_content *key_content)
130 {
131     hi_u32 ret = crypto_load_salt(CRYPTO_WORKKEY_KERNEL_A, key_content);
132     if (ret != HI_ERR_SUCCESS) {
133         ret = crypto_load_salt(CRYPTO_WORKKEY_KERNEL_A_BACKUP, key_content);
134         if (ret != HI_ERR_SUCCESS) {
135             return ret;
136         }
137     }
138 
139     return HI_ERR_SUCCESS;
140 }
141 
crypto_prepare(hi_flash_crypto_content * save_content)142 static hi_u32 crypto_prepare(hi_flash_crypto_content *save_content)
143 {
144     hi_u32 ret;
145     hi_u8 rootkey_iv[ROOTKEY_IV_BYTE_LENGTH];
146     hi_cipher_kdf_ctrl ctrl;
147     hi_u32 cs;
148 
149     ret = hi_cipher_init();
150     if (ret != HI_ERR_SUCCESS) {
151         return ret;
152     }
153 
154     ret = crypto_get_root_salt(save_content);
155     if (ret != HI_ERR_SUCCESS) {
156         return ret;
157     }
158 
159     cs = (uintptr_t)rootkey_iv ^ sizeof(rootkey_iv) ^ ((uintptr_t)save_content->root_salt) ^ ROOT_SALT_LENGTH;
160     ret = memcpy_s(rootkey_iv, sizeof(rootkey_iv), save_content->root_salt, ROOT_SALT_LENGTH, cs);
161     if (ret != HI_ERR_SUCCESS) {
162         return ret;
163     }
164 
165     ctrl.salt = rootkey_iv;
166     ctrl.salt_len = sizeof(rootkey_iv);
167     ctrl.kdf_cnt = KDF_ITERATION_CNT;
168     /* The HUK value is automatically generated. The hardware directly obtains the HUK
169        from the eFUSE and generates a fixed root key. */
170     /* 自动生成HUK值的方式,硬件直接从EFUSE中获取HUK,生成根密钥固定 */
171     ctrl.kdf_mode = HI_CIPHER_SSS_KDF_KEY_STORAGE;
172     return hi_cipher_kdf_key_derive(&ctrl);
173 }
174 
crypto_destory(hi_void)175 static hi_u32 crypto_destory(hi_void)
176 {
177     return hi_cipher_deinit();
178 }
179 
is_upg_need_crypto(hi_void)180 hi_bool is_upg_need_crypto(hi_void)
181 {
182     hi_nv_ftm_startup_cfg *cfg = boot_upg_get_cfg();
183     if ((cfg->mode == HI_UPG_MODE_UPGRADE) &&
184         ((cfg->reset_cnt >= 1) && (cfg->reset_cnt < cfg->cnt_max))) {
185         return HI_TRUE;
186     }
187 
188     return HI_FALSE;
189 }
190 
crpto_set_aes_ctrl_default_value(hi_cipher_aes_ctrl * aes_ctrl)191 static hi_void crpto_set_aes_ctrl_default_value(hi_cipher_aes_ctrl *aes_ctrl)
192 {
193     if (aes_ctrl == HI_NULL) {
194         return;
195     }
196     aes_ctrl->random_en = HI_FALSE;
197     aes_ctrl->key_from = HI_CIPHER_AES_KEY_FROM_CPU;
198     aes_ctrl->work_mode = HI_CIPHER_AES_WORK_MODE_CBC;
199     aes_ctrl->key_len = HI_CIPHER_AES_KEY_LENGTH_256BIT;
200 }
201 
crypto_encrypt_hash(hi_flash_crypto_content * key_content)202 static hi_u32 crypto_encrypt_hash(hi_flash_crypto_content *key_content)
203 {
204     hi_cipher_aes_ctrl aes_ctrl;
205     hi_u32 ret;
206     hi_u32 content_size = (hi_u32)sizeof(hi_flash_crypto_content);
207     hi_u32 encrypt_size = content_size - ROOT_SALT_LENGTH - IV_BYTE_LENGTH;
208 
209     hi_flash_crypto_content *data_tmp = (hi_flash_crypto_content *)boot_malloc(content_size);
210     if (data_tmp == HI_NULL) {
211         return HI_PRINT_ERRNO_MALLOC_EXAUST_ERR;
212     }
213 
214     hi_u32 cs = (uintptr_t)(aes_ctrl.iv) ^ (hi_u32)sizeof(aes_ctrl.iv) ^ (uintptr_t)(key_content->iv_nv) ^
215         IV_BYTE_LENGTH;
216     ret = (hi_u32)memcpy_s(aes_ctrl.iv, sizeof(aes_ctrl.iv), key_content->iv_nv, IV_BYTE_LENGTH, cs);
217     if (ret != EOK) {
218         goto fail;
219     }
220 
221     aes_ctrl.random_en = HI_FALSE;
222     aes_ctrl.key_from = HI_CIPHER_AES_KEY_FROM_KDF;
223     aes_ctrl.work_mode = HI_CIPHER_AES_WORK_MODE_CBC;
224     aes_ctrl.key_len = HI_CIPHER_AES_KEY_LENGTH_256BIT;
225     ret = hi_cipher_aes_config(&aes_ctrl);
226     if (ret != HI_ERR_SUCCESS) {
227         goto crypto_fail;
228     }
229     ret = hi_cipher_aes_crypto((hi_u32)(uintptr_t)key_content->iv_content, (hi_u32)(uintptr_t)(data_tmp->iv_content),
230         encrypt_size, HI_TRUE);
231     if (ret != HI_ERR_SUCCESS) {
232         goto crypto_fail;
233     }
234 
235     hi_u8* key_content_ptr = key_content->iv_content;
236     hi_u8* data_tmp_ptr = data_tmp->iv_content;
237     cs = (uintptr_t)key_content_ptr ^ encrypt_size ^ (uintptr_t)data_tmp_ptr ^ encrypt_size;
238     ret = memcpy_s(key_content_ptr, encrypt_size, data_tmp_ptr, encrypt_size, cs);
239 
240 crypto_fail:
241     (hi_void) hi_cipher_aes_destroy_config();
242 fail:
243     crypto_clear_content((hi_u8 *)data_tmp, content_size);
244     crypto_mem_free(data_tmp);
245     return ret;
246 }
247 
crypto_decrypt_hash(hi_flash_crypto_content * key_content)248 static hi_u32 crypto_decrypt_hash(hi_flash_crypto_content *key_content)
249 {
250     hi_u32 ret;
251     hi_u32 cs;
252     hi_u32 content_size = (hi_u32)sizeof(hi_flash_crypto_content);
253 
254     hi_flash_crypto_content *content_tmp = (hi_flash_crypto_content *)boot_malloc(content_size);
255     if (content_tmp == HI_NULL) {
256         return HI_PRINT_ERRNO_MALLOC_EXAUST_ERR;
257     }
258 
259     cs = (uintptr_t)(content_tmp) ^ content_size ^ (uintptr_t)(key_content) ^ content_size;
260     ret = (hi_u32)memcpy_s(content_tmp, content_size, key_content, content_size, cs);
261     if (ret != EOK) {
262         goto fail;
263     }
264 
265     hi_cipher_aes_ctrl aes_ctrl = {
266         .random_en = HI_FALSE,
267         .key_from = HI_CIPHER_AES_KEY_FROM_KDF,
268         .work_mode = HI_CIPHER_AES_WORK_MODE_CBC,
269         .key_len = HI_CIPHER_AES_KEY_LENGTH_256BIT,
270     };
271     cs = (uintptr_t)(aes_ctrl.iv) ^ (hi_u32)sizeof(aes_ctrl.iv) ^ (uintptr_t)(content_tmp->iv_nv) ^ IV_BYTE_LENGTH;
272     ret = (hi_u32)memcpy_s(aes_ctrl.iv, sizeof(aes_ctrl.iv), content_tmp->iv_nv, IV_BYTE_LENGTH, cs);
273     if (ret != EOK) {
274         goto fail;
275     }
276     ret = hi_cipher_aes_config(&aes_ctrl);
277     if (ret != HI_ERR_SUCCESS) {
278         goto crypto_fail;
279     }
280     ret = hi_cipher_aes_crypto((uintptr_t)content_tmp->iv_content, (uintptr_t)key_content->iv_content,
281         content_size - ROOT_SALT_LENGTH - IV_BYTE_LENGTH, HI_FALSE);
282     if (ret != HI_ERR_SUCCESS) {
283         goto crypto_fail;
284     }
285 
286 crypto_fail:
287     (hi_void) hi_cipher_aes_destroy_config();
288 fail:
289     crypto_clear_content((hi_u8 *)content_tmp, content_size);
290     crypto_mem_free(content_tmp);
291     return ret;
292 }
293 
crypto_save_work_key(crypto_workkey_partition part,hi_flash_crypto_content * key_content)294 static hi_u32 crypto_save_work_key(crypto_workkey_partition part, hi_flash_crypto_content *key_content)
295 {
296     hi_u32 ret;
297     hi_u32 cs;
298     hi_u32 content_size = (hi_u32)sizeof(hi_flash_crypto_content);
299     hi_flash_crypto_content *content_tmp = (hi_flash_crypto_content *)boot_malloc(content_size);
300     if (content_tmp == HI_NULL) {
301         return HI_PRINT_ERRNO_MALLOC_EXAUST_ERR;
302     }
303 
304     cs = (uintptr_t)(content_tmp) ^ content_size ^ (uintptr_t)(key_content) ^ content_size;
305     ret = (hi_u32)memcpy_s(content_tmp, content_size, key_content, content_size, cs);
306     if (ret != EOK) {
307         goto fail;
308     }
309 
310     /* 先加密,再存到工厂区NV中 */
311     ret = crypto_encrypt_hash(content_tmp);
312     if (ret != HI_ERR_SUCCESS) {
313         goto fail;
314     }
315 
316     if ((hi_u32)part & CRYPTO_WORKKEY_KERNEL_A) {
317         ret = hi_factory_nv_write(HI_NV_FTM_KERNELA_WORK_ID, content_tmp, content_size, 0);
318         if (ret != HI_ERR_SUCCESS) {
319             ret = HI_PRINT_ERRNO_CRYPTO_KEY_SAVE_ERR;
320             goto fail;
321         }
322     }
323     if ((hi_u32)part & CRYPTO_WORKKEY_KERNEL_A_BACKUP) {
324         ret = hi_factory_nv_write(HI_NV_FTM_BACKUP_KERNELA_WORK_ID, content_tmp, content_size, 0);
325         if (ret != HI_ERR_SUCCESS) {
326             ret =  HI_PRINT_ERRNO_CRYPTO_KEY_SAVE_ERR;
327             goto fail;
328         }
329     }
330 
331 fail:
332     crypto_clear_content((hi_u8 *)content_tmp, content_size);
333     crypto_mem_free(content_tmp);
334     return ret;
335 }
336 
crypto_load_key_content(crypto_workkey_partition part,hi_flash_crypto_content * key_content)337 static hi_u32 crypto_load_key_content(crypto_workkey_partition part, hi_flash_crypto_content *key_content)
338 {
339     hi_u32 ret = HI_ERR_SUCCESS;
340     hi_u8 hash[SHA_256_LENGTH];
341     hi_u8 key_e[KEY_BYTE_LENGTH] = { 0 };
342 
343     hi_u32 cs = (uintptr_t)key_e ^ (hi_u32)sizeof(key_e) ^ 0x0 ^ KEY_BYTE_LENGTH;
344     (hi_void) memset_s(key_e, sizeof(key_e), 0x0, KEY_BYTE_LENGTH, cs);
345     if (part == CRYPTO_WORKKEY_KERNEL_A) {
346         ret = hi_factory_nv_read(HI_NV_FTM_KERNELA_WORK_ID, key_content, sizeof(hi_flash_crypto_content), 0);
347         if (ret != HI_ERR_SUCCESS) {
348             goto fail;
349         }
350     } else if (part == CRYPTO_WORKKEY_KERNEL_A_BACKUP) {
351         ret = hi_factory_nv_read(HI_NV_FTM_BACKUP_KERNELA_WORK_ID, key_content, sizeof(hi_flash_crypto_content), 0);
352         if (ret != HI_ERR_SUCCESS) {
353             goto fail;
354         }
355     } else if (part == CRYPTO_WORKKEY_KERNEL_B) {
356         ret = hi_factory_nv_read(HI_NV_FTM_KERNELB_WORK_ID, key_content, sizeof(hi_flash_crypto_content), 0);
357         if (ret != HI_ERR_SUCCESS) {
358             goto fail;
359         }
360     } else if (part == CRYPTO_WORKKEY_KERNEL_B_BACKUP) {
361         ret = hi_factory_nv_read(HI_NV_FTM_BACKUP_KERNELB_WORK_ID, key_content, sizeof(hi_flash_crypto_content), 0);
362         if (ret != HI_ERR_SUCCESS) {
363             goto fail;
364         }
365     }
366 
367     if (memcmp(key_content->work_text, key_e, KEY_BYTE_LENGTH) == HI_ERR_SUCCESS) {
368         ret = HI_PRINT_ERRNO_CRYPTO_KEY_EMPTY_ERR;
369         goto fail;
370     }
371 
372     ret = crypto_decrypt_hash(key_content);
373     if (ret != HI_ERR_SUCCESS) {
374         goto fail;
375     }
376 
377     ret = hi_cipher_hash_sha256((uintptr_t)(key_content->root_salt), sizeof(hi_flash_crypto_content) - SHA_256_LENGTH,
378                                 hash, SHA_256_LENGTH);
379     if (ret != HI_ERR_SUCCESS) {
380         goto fail;
381     }
382     if (memcmp(key_content->content_sh256, hash, SHA_256_LENGTH) != HI_ERR_SUCCESS) {
383         ret = HI_PRINT_ERRNO_CRYPTO_KEY_INVALID_ERR;
384         goto fail;
385     }
386 fail:
387     return ret;
388 }
389 
crypto_gen_key_content(hi_flash_crypto_content * key_content)390 static hi_u32 crypto_gen_key_content(hi_flash_crypto_content *key_content)
391 {
392     hi_u8 salt[IV_BYTE_LENGTH];
393     hi_u8 kdf_key[KEY_BYTE_LENGTH];
394     hi_cipher_kdf_ctrl ctrl;
395 
396     (hi_void)hi_cipher_trng_get_random_bytes(salt, IV_BYTE_LENGTH);
397     (hi_void)hi_cipher_trng_get_random_bytes(kdf_key, KEY_BYTE_LENGTH);
398     (hi_void)hi_cipher_trng_get_random_bytes(key_content->iv_nv, IV_BYTE_LENGTH);
399     (hi_void)hi_cipher_trng_get_random_bytes(key_content->iv_content, IV_BYTE_LENGTH);
400 
401     hi_u32 cs = (uintptr_t)(ctrl.key) ^ (hi_u32)sizeof(ctrl.key) ^ (uintptr_t)kdf_key ^ (hi_u32)sizeof(kdf_key);
402     if ((hi_u32)memcpy_s(ctrl.key, sizeof(ctrl.key), kdf_key, sizeof(kdf_key), cs) != EOK) {
403         return HI_ERR_FAILURE;
404     }
405     ctrl.salt = salt;
406     ctrl.salt_len = sizeof(salt);
407     ctrl.kdf_cnt = KDF_ITERATION_CNT;
408     ctrl.kdf_mode = HI_CIPHER_SSS_KDF_KEY_DEVICE; /* 用户提供HUK值的方式 */
409     if (hi_cipher_kdf_key_derive(&ctrl) != HI_ERR_SUCCESS) {
410         return HI_ERR_FAILURE;
411     }
412 
413     cs = (uintptr_t)(key_content->work_text) ^ KEY_BYTE_LENGTH ^ (uintptr_t)(ctrl.result) ^ (hi_u32)sizeof(ctrl.result);
414     if (memcpy_s(key_content->work_text, KEY_BYTE_LENGTH, ctrl.result, sizeof(ctrl.result), cs) != EOK) {
415         return HI_ERR_FAILURE;
416     }
417 
418     if (hi_cipher_hash_sha256((uintptr_t)(key_content->root_salt), sizeof(hi_flash_crypto_content) - SHA_256_LENGTH,
419         key_content->content_sh256, SHA_256_LENGTH) != HI_ERR_SUCCESS) {
420         return HI_ERR_FAILURE;
421     }
422 
423     return HI_ERR_SUCCESS;
424 }
425 
crypto_encrypt_data(hi_flash_crypto_content * content,boot_crypto_ctx * para)426 static hi_u32 crypto_encrypt_data(hi_flash_crypto_content *content, boot_crypto_ctx *para)
427 {
428     hi_u32 ret = HI_ERR_FAILURE;
429     hi_cipher_aes_ctrl aes_ctrl;
430 
431     hi_u8 *fw_cyp_data = boot_malloc(para->crypto_total_size);
432     if (fw_cyp_data == HI_NULL) {
433         return HI_PRINT_ERRNO_CRYPTO_PREPARE_ERR;
434     }
435 
436     hi_u32 cs = (uintptr_t)(aes_ctrl.key) ^ (hi_u32)sizeof(aes_ctrl.key) ^
437         (uintptr_t)(content->work_text) ^ KEY_BYTE_LENGTH;
438     if (memcpy_s(aes_ctrl.key, sizeof(aes_ctrl.key), content->work_text, KEY_BYTE_LENGTH, cs) != EOK) {
439         goto fail;
440     }
441 
442     cs = (uintptr_t)(aes_ctrl.iv) ^ (hi_u32)sizeof(aes_ctrl.iv) ^ (uintptr_t)(content->iv_content) ^ IV_BYTE_LENGTH;
443     if (memcpy_s(aes_ctrl.iv, sizeof(aes_ctrl.iv), content->iv_content, IV_BYTE_LENGTH, cs) != EOK) {
444         goto fail;
445     }
446 
447     crpto_set_aes_ctrl_default_value(&aes_ctrl);
448     ret = hi_cipher_aes_config(&aes_ctrl);
449     if (ret != HI_ERR_SUCCESS) {
450         goto fail;
451     }
452 
453     ret = hi_cipher_aes_crypto((uintptr_t)(para->buf), (uintptr_t)fw_cyp_data, para->crypto_total_size, HI_TRUE);
454     if (ret != HI_ERR_SUCCESS) {
455         goto crypto_fail;
456     }
457 
458     ret = g_flash_cmd_funcs.write(para->kernel_addr + para->crypto_start_addr, para->crypto_total_size,
459                                   (hi_u8 *)fw_cyp_data, HI_FALSE);
460     if (ret != HI_ERR_SUCCESS) {
461         goto crypto_fail;
462     }
463 
464 crypto_fail:
465     (hi_void) hi_cipher_aes_destroy_config();
466 fail:
467     crypto_mem_free(fw_cyp_data);
468     return ret;
469 }
470 
crypto_decrypt_kernel(hi_flash_crypto_content * content,hi_u32 ram_addr,hi_u32 ram_size)471 static hi_u32 crypto_decrypt_kernel(hi_flash_crypto_content *content, hi_u32 ram_addr, hi_u32 ram_size)
472 {
473     hi_u32 ret;
474     hi_cipher_aes_ctrl aes_ctrl;
475     hi_u8 *fw_raw_data = (hi_u8 *)(uintptr_t)ram_addr;
476     hi_u32 cs;
477     hi_u32 kernel_offset;
478     hi_unref_param(ram_size);
479     ret = boot_get_crypto_kernel_start(&kernel_offset);
480     if (ret != HI_ERR_SUCCESS) {
481         goto fail;
482     }
483 
484     cs = (uintptr_t)(aes_ctrl.key) ^ (hi_u32)sizeof(aes_ctrl.key) ^ (uintptr_t)(content->work_text) ^ KEY_BYTE_LENGTH;
485     ret = (hi_u32)memcpy_s(aes_ctrl.key, sizeof(aes_ctrl.key), content->work_text, KEY_BYTE_LENGTH, cs);
486     if (ret != HI_ERR_SUCCESS) {
487         goto fail;
488     }
489     cs = (uintptr_t)(aes_ctrl.iv) ^ (hi_u32)sizeof(aes_ctrl.iv) ^ (uintptr_t)(content->iv_content) ^ IV_BYTE_LENGTH;
490     ret = (hi_u32)memcpy_s(aes_ctrl.iv, sizeof(aes_ctrl.iv), content->iv_content, IV_BYTE_LENGTH, cs);
491     if (ret != HI_ERR_SUCCESS) {
492         goto fail;
493     }
494 
495     crpto_set_aes_ctrl_default_value(&aes_ctrl);
496     ret = hi_cipher_aes_config(&aes_ctrl);
497     if (ret != HI_ERR_SUCCESS) {
498         goto fail;
499     }
500 
501     ret = hi_cipher_aes_crypto(kernel_offset + SFC_BUFFER_BASE_ADDRESS, (hi_u32)(uintptr_t)fw_raw_data,
502         CRYPTO_KERNEL_LENGTH, HI_FALSE);
503     if (ret != HI_ERR_SUCCESS) {
504         goto crypto_fail;
505     }
506 
507 crypto_fail:
508     (hi_void) hi_cipher_aes_destroy_config();
509 fail:
510     return ret;
511 }
512 
513 
crypto_load_flash_raw(uintptr_t ram_addr,hi_u32 ram_size)514 hi_u32 crypto_load_flash_raw(uintptr_t ram_addr, hi_u32 ram_size)
515 {
516     hi_u32 ret;
517     hi_u32 flash_offset = 0;
518     uintptr_t addr_start;
519     if (ram_addr == 0 || ram_size == 0) {
520         return HI_PRINT_ERRNO_CRYPTO_PREPARE_ERR;
521     }
522 
523     hi_nv_ftm_startup_cfg *cfg = boot_upg_get_cfg();
524     addr_start = cfg->addr_start;
525 #if defined(CONFIG_COMPRESSION_OTA_SUPPORT)
526     addr_start = cfg->addr_write;
527 #endif
528     boot_get_start_addr_offset(addr_start, &flash_offset);
529 
530     ret = g_flash_cmd_funcs.read(flash_offset, ram_size, (hi_u8 *)ram_addr);
531 
532     return ret;
533 }
534 
crypto_content_id(crypto_workkey_partition * content,crypto_workkey_partition * content_bak)535 hi_u32 crypto_content_id(crypto_workkey_partition *content, crypto_workkey_partition *content_bak)
536 {
537     hi_flash_partition_table *partition = hi_get_partition_table();
538     hi_u32 kernel_a = partition->table[HI_FLASH_PARTITON_KERNEL_A].addr;
539     hi_u32 kernel_b = partition->table[HI_FLASH_PARTITON_KERNEL_B].addr;
540 
541     hi_nv_ftm_startup_cfg *cfg = boot_upg_get_cfg();
542     if (cfg->addr_start == kernel_a) {
543         *content = CRYPTO_WORKKEY_KERNEL_A;
544         *content_bak = CRYPTO_WORKKEY_KERNEL_A_BACKUP;
545     } else if (cfg->addr_start == kernel_b) {
546         *content = CRYPTO_WORKKEY_KERNEL_B;
547         *content_bak = CRYPTO_WORKKEY_KERNEL_B_BACKUP;
548     } else {
549 #ifdef CONFIG_FACTORY_TEST_SUPPORT
550         hi_u32 kernel_factory = 0;
551         hi_nv_ftm_factory_mode factory_mode_cfg = {0};
552         hi_u32 ret = hi_factory_nv_read(HI_NV_FTM_FACTORY_MODE, &factory_mode_cfg, sizeof(hi_nv_ftm_factory_mode), 0);
553         if (ret != HI_ERR_SUCCESS) {
554         } else {
555             if (factory_mode_cfg.factory_mode == 0x1) {
556                 boot_msg0("get ftm addr");
557                 kernel_factory = factory_mode_cfg.factory_addr_start; /* 0x0: factory mode, start addr:0x14D000. */
558             }
559         }
560 
561         if (cfg->addr_start == kernel_factory) {
562             *content = CRYPTO_WORKKEY_KERNEL_A;
563             *content_bak = CRYPTO_WORKKEY_KERNEL_A_BACKUP;
564         } else {
565             return HI_PRINT_ERRNO_CRYPTO_KERNEL_ADDR_ERR;
566         }
567 #else
568         return HI_PRINT_ERRNO_CRYPTO_KERNEL_ADDR_ERR;
569 #endif
570     }
571 
572     return HI_ERR_SUCCESS;
573 }
574 
crypto_decrypt(hi_u32 ram_addr,hi_u32 ram_size)575 hi_u32 crypto_decrypt(hi_u32 ram_addr, hi_u32 ram_size)
576 {
577     hi_bool is_backup_content = HI_FALSE;
578     crypto_workkey_partition work_content;
579     crypto_workkey_partition work_content_bak;
580     hi_u32 ret = crypto_content_id(&work_content, &work_content_bak);
581     if (ret != HI_ERR_SUCCESS) {
582         return ret;
583     }
584 
585     hi_flash_crypto_content *key_content = (hi_flash_crypto_content *)boot_malloc(sizeof(hi_flash_crypto_content));
586     if (key_content == HI_NULL) {
587         return HI_PRINT_ERRNO_CRYPTO_PREPARE_ERR;
588     }
589 
590     ret = crypto_prepare(key_content);
591     if (ret != HI_ERR_SUCCESS) {
592         crypto_clear_content((hi_u8 *)key_content, (hi_u32)sizeof(hi_flash_crypto_content));
593         crypto_mem_free(key_content);
594         return HI_PRINT_ERRNO_CRYPTO_PREPARE_ERR;
595     }
596 
597     ret = crypto_load_key_content(work_content, key_content);
598     if (ret != HI_ERR_SUCCESS) {
599         ret = crypto_load_key_content(work_content_bak, key_content);
600         if (ret != HI_ERR_SUCCESS) {
601             goto fail;
602         } else {
603             ret = crypto_save_work_key(work_content, key_content);
604             if (ret != HI_ERR_SUCCESS) {
605                 goto fail;
606             }
607             is_backup_content = HI_TRUE;
608         }
609     }
610 
611     ret = crypto_decrypt_kernel(key_content, ram_addr, ram_size);
612     if ((ret != HI_ERR_SUCCESS) && (is_backup_content == HI_FALSE)) {
613         ret = crypto_load_key_content(work_content_bak, key_content);
614         if (ret != HI_ERR_SUCCESS) {
615             goto fail;
616         }
617         ret = crypto_decrypt_kernel(key_content, ram_addr, ram_size);
618         if (ret != HI_ERR_SUCCESS) {
619             ret = HI_PRINT_ERRNO_CRYPTO_FW_DECRYPT_ERR;
620             goto fail;
621         }
622     }
623 fail:
624     crypto_clear_content((hi_u8 *)key_content, (hi_u32)sizeof(hi_flash_crypto_content));
625     crypto_mem_free(key_content);
626     crypto_destory();
627     return ret;
628 }
629 
crypto_check_decrypt(hi_void)630 hi_void crypto_check_decrypt(hi_void)
631 {
632     hi_u32 ret;
633     hi_nv_ftm_startup_cfg *cfg = boot_upg_get_cfg();
634     hi_bool upg_encrypt_flag = is_upg_need_crypto();
635     if ((upg_encrypt_flag == HI_TRUE) && (cfg->file_type == HI_UPG_FILE_KERNEL)) {
636 #if !defined(CONFIG_COMPRESSION_OTA_SUPPORT)
637         /* 双分区升级模式,将升级文件加密数据解密到ram中 */
638         ret = crypto_decrypt(KERNEL_RAM_ADDR, CRYPTO_KERNEL_LENGTH);
639         if (ret != HI_ERR_SUCCESS) {
640             boot_put_errno(ret);
641         }
642 #else
643         /* 压缩升级模式,将升级加密压缩包原始数据拷贝到ram中 */
644         ret = crypto_load_flash_raw(KERNEL_RAM_ADDR, CRYPTO_KERNEL_LENGTH);
645         if (ret != HI_ERR_SUCCESS) {
646             boot_put_errno(ret);
647         }
648 #endif
649     } else {
650         /* 普通模式,将运行区加密数据解密到ram中 */
651         ret = crypto_decrypt(KERNEL_RAM_ADDR, CRYPTO_KERNEL_LENGTH);
652         if (ret != HI_ERR_SUCCESS) {
653             boot_put_errno(ret);
654         }
655     }
656 }
657 
encrypt_upg_data(boot_crypto_ctx * para)658 hi_u32 encrypt_upg_data(boot_crypto_ctx *para)
659 {
660     hi_u32 ret;
661 
662     hi_flash_crypto_content *new_content = (hi_flash_crypto_content *)boot_malloc(sizeof(hi_flash_crypto_content));
663     if (new_content == HI_NULL) {
664         return HI_PRINT_ERRNO_CRYPTO_PREPARE_ERR;
665     }
666 
667     ret = crypto_prepare(new_content);
668     if (ret != HI_ERR_SUCCESS) {
669         crypto_clear_content((hi_u8 *)new_content, (hi_u32)sizeof(hi_flash_crypto_content));
670         crypto_mem_free(new_content);
671         return HI_PRINT_ERRNO_CRYPTO_PREPARE_ERR;
672     }
673 
674     hi_u32 cs = KERNEL_RAM_ADDR ^ CRYPTO_KERNEL_LENGTH ^ (uintptr_t)(para->buf) ^ CRYPTO_KERNEL_LENGTH;
675     ret = memcpy_s((hi_u8 *)KERNEL_RAM_ADDR, CRYPTO_KERNEL_LENGTH, para->buf, CRYPTO_KERNEL_LENGTH, cs);
676     if (ret != EOK) {
677         goto fail;
678     }
679 
680     /* 生成新密钥存放到密钥分区 */
681     ret = crypto_gen_key_content(new_content);
682     if (ret != HI_ERR_SUCCESS) {
683         goto fail;
684     }
685 
686     /* 更新密钥流程中,密钥备份区暂不更新,待旧kernel解密成功后再更新密钥备份区 */
687     ret = crypto_save_work_key(CRYPTO_WORKKEY_KERNEL_A, new_content);
688     if (ret != HI_ERR_SUCCESS) {
689         goto fail;
690     }
691 
692     ret = crypto_encrypt_data(new_content, para);
693     if (ret != HI_ERR_SUCCESS) {
694         goto fail;
695     }
696 
697     /* 新密钥加密数据完毕,更新密钥备份区 */
698     ret = crypto_save_work_key(CRYPTO_WORKKEY_KERNEL_A_BACKUP, new_content);
699     if (ret != HI_ERR_SUCCESS) {
700         goto fail;
701     }
702 
703 fail:
704     crypto_clear_content((hi_u8 *)new_content, (hi_u32)sizeof(hi_flash_crypto_content));
705     crypto_mem_free(new_content);
706     crypto_destory();
707     return ret;
708 }
709 
encrypt_para_init(boot_crypto_ctx * encrypt_para,hi_u32 start)710 hi_u32 encrypt_para_init(boot_crypto_ctx *encrypt_para, hi_u32 start)
711 {
712     encrypt_para->para_is_init = HI_TRUE;
713     encrypt_para->is_crypto_section = HI_TRUE;
714     encrypt_para->kernel_addr = start;
715     encrypt_para->crypto_start_addr = sizeof(hi_upg_file_head);
716     encrypt_para->crypto_total_size = CRYPTO_KERNEL_LENGTH;
717     encrypt_para->crypto_end_addr = encrypt_para->crypto_start_addr + encrypt_para->crypto_total_size;
718     encrypt_para->cryptoed_size = 0;
719     encrypt_para->buf = (hi_u8 *)boot_malloc(CRYPTO_KERNEL_LENGTH);
720     if (encrypt_para->buf == HI_NULL) {
721         return HI_PRINT_ERRNO_MALLOC_EXAUST_ERR;
722     }
723 
724     hi_u32 cs = (uintptr_t)(encrypt_para->buf) ^ CRYPTO_KERNEL_LENGTH ^ 0 ^ CRYPTO_KERNEL_LENGTH;
725     hi_u32 ret = memset_s(encrypt_para->buf, CRYPTO_KERNEL_LENGTH, 0, CRYPTO_KERNEL_LENGTH, cs);
726     if (ret != HI_ERR_SUCCESS) {
727         return HI_PRINT_ERRNO_MALLOC_EXAUST_ERR;
728     }
729 
730     return HI_ERR_SUCCESS;
731 }
732 
upg_check_encrypt(boot_crypto_ctx * para,hi_u8 * buf,hi_u32 buf_len)733 hi_u32 upg_check_encrypt(boot_crypto_ctx *para, hi_u8 *buf, hi_u32 buf_len)
734 {
735     hi_u32 ret;
736     hi_u32 cs;
737 
738     hi_u32 size = para->cryptoed_size + buf_len;
739     if (size > para->crypto_total_size) {
740         cs = (uintptr_t)(para->buf + para->cryptoed_size) ^ (para->crypto_total_size - para->cryptoed_size) ^
741             (uintptr_t)buf ^ (para->crypto_total_size - para->cryptoed_size);
742         if (memcpy_s(para->buf + para->cryptoed_size, para->crypto_total_size - para->cryptoed_size, buf,
743             para->crypto_total_size - para->cryptoed_size, cs) != EOK) {
744             return HI_PRINT_ERRNO_CRYPTO_KEY_EMPTY_ERR;
745         }
746 
747         ret = encrypt_upg_data(para);
748         if (ret != HI_ERR_SUCCESS) {
749             return ret;
750         }
751 
752         ret = hi_flash_write(para->kernel_addr + para->crypto_end_addr, size - para->crypto_total_size,
753             buf + (para->crypto_total_size - para->cryptoed_size), HI_FALSE);
754         if (ret != HI_ERR_SUCCESS) {
755             return ret;
756         }
757 
758         para->is_crypto_section = HI_FALSE;
759     } else if (size == para->crypto_total_size) {
760         cs = (uintptr_t)(para->buf + para->cryptoed_size) ^ buf_len ^ (uintptr_t)buf ^ buf_len;
761         if (memcpy_s(para->buf + para->cryptoed_size, buf_len, buf, buf_len, cs) != EOK) {
762             return HI_PRINT_ERRNO_CRYPTO_KEY_EMPTY_ERR;
763         }
764 
765         ret = encrypt_upg_data(para);
766         if (ret != HI_ERR_SUCCESS) {
767             return ret;
768         }
769 
770         para->is_crypto_section = HI_FALSE;
771     } else {
772         cs = (uintptr_t)(para->buf + para->cryptoed_size) ^ (para->crypto_total_size - para->cryptoed_size) ^
773             (uintptr_t)buf ^ buf_len;
774         if (memcpy_s(para->buf + para->cryptoed_size, para->crypto_total_size - para->cryptoed_size,
775             buf, buf_len, cs) != EOK) {
776             return HI_PRINT_ERRNO_CRYPTO_KEY_EMPTY_ERR;
777         }
778 
779         para->cryptoed_size += buf_len;
780     }
781 
782     return HI_ERR_SUCCESS;
783 }
784 
upg_encrypt_part_write(hi_u32 start,hi_u32 offset,hi_u8 * buf,hi_u32 buf_len)785 hi_u32 upg_encrypt_part_write(hi_u32 start, hi_u32 offset, hi_u8 *buf, hi_u32 buf_len)
786 {
787     hi_u32 ret;
788     boot_crypto_ctx *encrypt_para = boot_crypto_get_ctx();
789     if (encrypt_para->para_is_init == HI_FALSE) {
790         ret =  encrypt_para_init(encrypt_para, start);
791         if (ret != HI_ERR_SUCCESS) {
792             return ret;
793         }
794     }
795 
796     if (encrypt_para->crypto_start_addr == offset) {
797         ret = upg_check_encrypt(encrypt_para, buf, buf_len);
798         if (ret != HI_ERR_SUCCESS) {
799             return ret;
800         }
801     } else if ((encrypt_para->crypto_start_addr > offset) &&
802                (encrypt_para->crypto_start_addr < (offset + buf_len))) {
803         ret = hi_flash_write(start + offset, (encrypt_para->crypto_start_addr - offset), buf, HI_FALSE);
804         if (ret != HI_ERR_SUCCESS) {
805             return ret;
806         }
807         ret = upg_check_encrypt(encrypt_para, buf + encrypt_para->crypto_start_addr - offset,
808             buf_len - (encrypt_para->crypto_start_addr - offset));
809         if (ret != HI_ERR_SUCCESS) {
810             return ret;
811         }
812     } else {
813         ret = upg_check_encrypt(encrypt_para, buf, buf_len);
814         if (ret != HI_ERR_SUCCESS) {
815             return ret;
816         }
817     }
818 
819     if ((encrypt_para->is_crypto_section == HI_FALSE) && (encrypt_para->buf != HI_NULL)) {
820         crypto_mem_free(encrypt_para->buf);
821     }
822 
823     return HI_ERR_SUCCESS;
824 }
825 
826 static const hi_u8 g_upg_file_magic[IV_BYTE_LENGTH] = {
827     0xE4, 0xEE, 0x10, 0x0E, 0x43, 0x4D, 0x94, 0x24,
828     0xC7, 0x54, 0x6D, 0xFB, 0x15, 0xA1, 0x46, 0x97
829 };
830 
boot_crypto_upg_file_prepare(boot_crypto_ctx * para)831 hi_u32 boot_crypto_upg_file_prepare(boot_crypto_ctx *para)
832 {
833     hi_u32 ret;
834     hi_cipher_kdf_ctrl ctrl;
835     hi_u8 salt[ROOTKEY_IV_BYTE_LENGTH] = {0};
836 
837     hi_u32 cs = (uintptr_t)salt ^ sizeof(salt) ^ (uintptr_t)(para->upg_salt) ^ IV_BYTE_LENGTH;
838     ret = memcpy_s((hi_void *)salt, sizeof(salt), (hi_void *)(para->upg_salt), IV_BYTE_LENGTH, cs);
839     if (ret != EOK) {
840         return ret;
841     }
842 
843     cs = (uintptr_t)(salt + IV_BYTE_LENGTH) ^ (sizeof(salt) - IV_BYTE_LENGTH) ^
844         (uintptr_t)g_upg_file_magic ^ IV_BYTE_LENGTH;
845     ret = memcpy_s((hi_void *)(salt + IV_BYTE_LENGTH), sizeof(salt) - IV_BYTE_LENGTH,
846         (hi_void *)g_upg_file_magic, IV_BYTE_LENGTH, cs);
847     if (ret != EOK) {
848         return ret;
849     }
850 
851     ctrl.salt = salt;
852     ctrl.salt_len = ROOTKEY_IV_BYTE_LENGTH;
853     ctrl.kdf_cnt = KDF_ITERATION_CNT;
854     ctrl.kdf_mode = HI_CIPHER_SSS_KDF_KEY_STORAGE;
855     ret = hi_cipher_kdf_key_derive(&ctrl);
856     if (ret != HI_ERR_SUCCESS) {
857         return ret;
858     }
859 
860     return HI_ERR_SUCCESS;
861 }
862 
boot_crypto_upg_file_decrypt(boot_crypto_ctx * para)863 hi_u32 boot_crypto_upg_file_decrypt(boot_crypto_ctx *para)
864 {
865     hi_u32 ret;
866     hi_cipher_aes_ctrl aes_ctrl;
867 
868     ret = hi_cipher_init();
869     if (ret != HI_ERR_SUCCESS) {
870         return ret;
871     }
872 
873     hi_upg_head *fw_raw_data = (hi_upg_head *)boot_malloc(para->crypto_total_size);
874     if (fw_raw_data == HI_NULL) {
875         crypto_destory();
876         return HI_PRINT_ERRNO_MALLOC_EXAUST_ERR;
877     }
878 
879     ret = boot_crypto_upg_file_prepare(para);
880     if (ret != HI_ERR_SUCCESS) {
881         ret = HI_PRINT_ERRNO_UPG_CRYPTO_PREPARE_ERR;
882         goto fail;
883     }
884 
885     hi_u32 cs = (uintptr_t)(aes_ctrl.iv) ^ sizeof(aes_ctrl.iv) ^ (uintptr_t)(para->upg_iv) ^ IV_BYTE_LENGTH;
886     ret = memcpy_s(aes_ctrl.iv, sizeof(aes_ctrl.iv), para->upg_iv, IV_BYTE_LENGTH, cs);
887     if (ret != HI_ERR_SUCCESS) {
888         goto fail;
889     }
890 
891     aes_ctrl.random_en = HI_FALSE;
892     aes_ctrl.key_from = HI_CIPHER_AES_KEY_FROM_KDF;
893     aes_ctrl.work_mode = HI_CIPHER_AES_WORK_MODE_CBC;
894     aes_ctrl.key_len = HI_CIPHER_AES_KEY_LENGTH_256BIT;
895     ret = hi_cipher_aes_config(&aes_ctrl);
896     if (ret != HI_ERR_SUCCESS) {
897         goto fail;
898     }
899 
900     ret = hi_cipher_aes_crypto((uintptr_t)(para->buf), (uintptr_t)fw_raw_data, para->crypto_total_size, HI_FALSE);
901     if (ret != HI_ERR_SUCCESS) {
902         goto crypto_fail;
903     }
904 
905     cs = (uintptr_t)(para->buf) ^ para->crypto_total_size ^ (uintptr_t)(fw_raw_data) ^ para->crypto_total_size;
906     ret = memcpy_s(para->buf, para->crypto_total_size, fw_raw_data, para->crypto_total_size, cs);
907     if (ret != HI_ERR_SUCCESS) {
908         goto crypto_fail;
909     }
910 
911 crypto_fail:
912     (hi_void) hi_cipher_aes_destroy_config();
913 fail:
914     crypto_mem_free(fw_raw_data);
915     crypto_destory();
916     return ret;
917 }
918 
decrypt_para_init(boot_crypto_ctx * para,hi_u32 start)919 hi_u32 decrypt_para_init(boot_crypto_ctx *para, hi_u32 start)
920 {
921     hi_u32 ret;
922     uintptr_t addr_write = start - sizeof (hi_upg_file_head);
923     hi_upg_common_head *upg_head = (hi_upg_common_head *)boot_malloc(sizeof(hi_upg_common_head));
924     if (upg_head == HI_NULL) {
925         return HI_PRINT_ERRNO_MALLOC_EXAUST_ERR;
926     }
927 
928     ret = hi_flash_read(addr_write, sizeof(hi_upg_common_head), (hi_u8 *)upg_head);
929     if (ret != HI_ERR_SUCCESS) {
930         goto fail;
931     }
932 
933     para->kernel_addr = start;
934     para->crypto_start_addr = LZMA_HEAD_SIZE;
935     para->crypto_total_size = CRYPTO_KERNEL_LENGTH + sizeof(hi_upg_file_head);
936     para->crypto_end_addr = para->crypto_start_addr + para->crypto_total_size;
937 
938     hi_u32 cs = (uintptr_t)(para->upg_salt) ^ IV_BYTE_LENGTH ^ (uintptr_t)(upg_head->aes_key) ^
939         sizeof(upg_head->aes_key);
940     ret = memcpy_s((hi_void *)(para->upg_salt), IV_BYTE_LENGTH, (hi_void *)(upg_head->aes_key),
941         sizeof(upg_head->aes_key), cs);
942     if (ret != EOK) {
943         goto fail;
944     }
945 
946     cs = (uintptr_t)(para->upg_iv) ^ IV_BYTE_LENGTH ^ (uintptr_t)(upg_head->aes_iv) ^
947         sizeof(upg_head->aes_iv);
948     ret = memcpy_s((hi_void *)(para->upg_iv), IV_BYTE_LENGTH, (hi_void *)(upg_head->aes_iv),
949         sizeof(upg_head->aes_iv), cs);
950     if (ret != EOK) {
951         goto fail;
952     }
953 
954     para->buf = (hi_u8 *)boot_malloc(para->crypto_total_size);
955     if (para->buf == HI_NULL) {
956         ret = HI_PRINT_ERRNO_MALLOC_EXAUST_ERR;
957         goto fail;
958     }
959 
960     ret = hi_flash_read(para->kernel_addr + para->crypto_start_addr, para->crypto_total_size, para->buf);
961     if (ret != HI_ERR_SUCCESS) {
962         crypto_mem_free(para->buf);
963         goto fail;
964     }
965 
966 fail:
967     /* clear salt and iv. */
968     crypto_clear_content(upg_head->aes_key, (hi_u32)sizeof(upg_head->aes_key));
969     crypto_clear_content(upg_head->aes_iv, (hi_u32)sizeof(upg_head->aes_iv));
970     crypto_mem_free(upg_head);
971     return ret;
972 }
973 
set_upg_decrypt_flag(boot_crypto_ctx * para)974 hi_void set_upg_decrypt_flag(boot_crypto_ctx *para)
975 {
976     para->para_is_init = HI_TRUE;
977     para->is_crypto_section = HI_TRUE;
978     para->is_verify_byte = HI_FALSE;
979     para->data_medium = DATA_MEDIUM_NOT_INIT;
980     para->ram_offset = 0;
981     para->cryptoed_size = 0;
982 }
983 
upg_check_decrypt(boot_crypto_ctx * para,hi_u8 * buf,hi_u32 buf_len)984 hi_u32 upg_check_decrypt(boot_crypto_ctx *para, hi_u8 *buf, hi_u32 buf_len)
985 {
986     hi_u32 ret;
987     hi_u32 cs;
988 
989     hi_u32 size = para->cryptoed_size + buf_len;
990     if (size > para->crypto_total_size) {
991         cs = (uintptr_t)buf ^ (para->crypto_total_size - para->cryptoed_size) ^
992             (uintptr_t)(para->buf + para->cryptoed_size) ^ (para->crypto_total_size - para->cryptoed_size);
993         ret = memcpy_s(buf, para->crypto_total_size - para->cryptoed_size, para->buf + para->cryptoed_size,
994             para->crypto_total_size - para->cryptoed_size, cs);
995         if (ret != EOK) {
996             return ret;
997         }
998 
999         ret = hi_flash_read(para->kernel_addr + para->crypto_end_addr, size - para->crypto_total_size,
1000             buf + (para->crypto_total_size - para->cryptoed_size));
1001         if (ret != HI_ERR_SUCCESS) {
1002             return ret;
1003         }
1004 
1005         para->ram_offset = para->cryptoed_size;
1006         para->cryptoed_size = para->crypto_total_size;
1007     } else if (size == para->crypto_total_size) {
1008         cs = (uintptr_t)buf ^ buf_len ^ (uintptr_t)(para->buf + para->cryptoed_size) ^ buf_len;
1009         ret = memcpy_s(buf, buf_len, para->buf + para->cryptoed_size, buf_len, cs);
1010         if (ret != EOK) {
1011             return ret;
1012         }
1013 
1014         para->ram_offset = para->cryptoed_size;
1015         para->cryptoed_size = para->crypto_total_size;
1016     } else {
1017         cs = (uintptr_t)buf ^ buf_len ^ (uintptr_t)(para->buf + para->cryptoed_size) ^ buf_len;
1018         ret = memcpy_s(buf, buf_len, para->buf + para->cryptoed_size, buf_len, cs);
1019         if (ret != EOK) {
1020             return ret;
1021         }
1022 
1023         para->ram_offset = para->cryptoed_size;
1024         para->cryptoed_size += buf_len;
1025     }
1026 
1027     return HI_ERR_SUCCESS;
1028 }
1029 
upg_decrypt_part_read(hi_u32 start,hi_u32 offset,hi_u8 * buf,hi_u32 buf_len)1030 hi_u32 upg_decrypt_part_read(hi_u32 start, hi_u32 offset, hi_u8 *buf, hi_u32 buf_len)
1031 {
1032     hi_u32 ret;
1033     boot_crypto_ctx *decrypt_para = boot_decrypt_get_ctx();
1034     if (decrypt_para->para_is_init == HI_FALSE) {
1035         set_upg_decrypt_flag(decrypt_para);
1036     }
1037 
1038     if ((decrypt_para->is_verify_byte == HI_TRUE) && (buf_len == 1)) {
1039         decrypt_para->is_verify_byte = HI_FALSE;
1040         if (decrypt_para->cryptoed_size == decrypt_para->crypto_total_size) {
1041             decrypt_para->is_crypto_section = HI_FALSE;
1042             decrypt_para->para_is_init = HI_FALSE;
1043         }
1044 
1045         if (decrypt_para->data_medium == DATA_MEDIUM_RAM) {
1046             buf[0] = decrypt_para->buf[decrypt_para->ram_offset];
1047         } else if (decrypt_para->data_medium == DATA_MEDIUM_FLASH) {
1048             ret = hi_flash_read(start + offset, buf_len, buf);
1049             if (ret != HI_ERR_SUCCESS) {
1050                 return ret;
1051             }
1052         }
1053 
1054         return HI_ERR_SUCCESS;
1055     }
1056 
1057     if (decrypt_para->crypto_start_addr == offset) {
1058         ret = upg_check_decrypt(decrypt_para, buf, buf_len);
1059         if (ret != HI_ERR_SUCCESS) {
1060             return ret;
1061         }
1062 
1063         decrypt_para->data_medium = DATA_MEDIUM_RAM;
1064     } else if ((decrypt_para->crypto_start_addr > offset) && (decrypt_para->crypto_start_addr < (offset + buf_len))) {
1065         ret = hi_flash_read(start + offset, (decrypt_para->crypto_start_addr - offset), buf);
1066         if (ret != HI_ERR_SUCCESS) {
1067             return ret;
1068         }
1069         ret = upg_check_decrypt(decrypt_para, buf + decrypt_para->crypto_start_addr - offset,
1070             buf_len - (decrypt_para->crypto_start_addr - offset));
1071         if (ret != HI_ERR_SUCCESS) {
1072             return ret;
1073         }
1074 
1075         decrypt_para->data_medium = DATA_MEDIUM_FLASH;
1076     } else {
1077         ret = upg_check_decrypt(decrypt_para, buf, buf_len);
1078         if (ret != HI_ERR_SUCCESS) {
1079             return ret;
1080         }
1081 
1082         decrypt_para->data_medium = DATA_MEDIUM_RAM;
1083     }
1084     decrypt_para->is_verify_byte = HI_TRUE;
1085 
1086     return HI_ERR_SUCCESS;
1087 }
1088 
crypto_kernel_write(hi_u32 start,hi_u32 offset,hi_u8 * buf,hi_u32 buf_len)1089 hi_u32 crypto_kernel_write(hi_u32 start, hi_u32 offset, hi_u8 *buf, hi_u32 buf_len)
1090 {
1091     hi_u32 ret;
1092     boot_crypto_ctx *para = boot_crypto_get_ctx();
1093     hi_u32 code_offset = (hi_u32)sizeof(hi_upg_file_head);
1094 
1095     if (((code_offset >= offset) && (code_offset < (offset + buf_len))) || (para->is_crypto_section == HI_TRUE)) {
1096         ret = upg_encrypt_part_write(start, offset, buf, buf_len);
1097         if (ret != HI_ERR_SUCCESS) {
1098             boot_msg1("encrypt flash fail! Err: ", ret);
1099             return ret;
1100         }
1101     } else {
1102         ret = hi_flash_write(start + offset, buf_len, buf, HI_FALSE);
1103         if (ret != HI_ERR_SUCCESS) {
1104             return ret;
1105         }
1106     }
1107 
1108     return HI_ERR_SUCCESS;
1109 }
1110 
crypto_kernel_read(hi_u32 start,hi_u32 offset,hi_u8 * buf,hi_u32 buf_len)1111 hi_u32 crypto_kernel_read(hi_u32 start, hi_u32 offset, hi_u8 *buf, hi_u32 buf_len)
1112 {
1113     hi_u32 ret;
1114     boot_crypto_ctx *para = boot_decrypt_get_ctx();
1115     hi_u32 code_offset = LZMA_HEAD_SIZE;
1116 
1117     if (((code_offset >= offset) && (code_offset < (offset + buf_len))) || (para->is_crypto_section == HI_TRUE)) {
1118         ret = upg_decrypt_part_read(start, offset, buf, buf_len);
1119         if (ret != HI_ERR_SUCCESS) {
1120             boot_msg1("decrypt flash fail! Err: ", ret);
1121             return ret;
1122         }
1123     } else {
1124         ret = hi_flash_read(start + offset, buf_len, buf);
1125         if (ret != HI_ERR_SUCCESS) {
1126             return ret;
1127         }
1128     }
1129 
1130     return HI_ERR_SUCCESS;
1131 }
1132 
boot_decrypt_upg_file(hi_u32 addr_write,const hi_upg_section_head * section_head)1133 hi_u32 boot_decrypt_upg_file(hi_u32 addr_write, const hi_upg_section_head *section_head)
1134 {
1135     hi_u32 ret;
1136     hi_u32 start_offset = addr_write + section_head->section0_offset;
1137     boot_crypto_ctx *decrypt_para = boot_decrypt_get_ctx();
1138 
1139     ret =  decrypt_para_init(decrypt_para, start_offset);
1140     if (ret != HI_ERR_SUCCESS) {
1141         boot_msg1("init decrypt para, err: ", ret);
1142         crypto_mem_free(decrypt_para->buf);
1143         return ret;
1144     }
1145 
1146     ret = boot_crypto_upg_file_decrypt(decrypt_para);
1147     if (ret != HI_ERR_SUCCESS) {
1148         boot_msg1("decrypt upg file, err: ", ret);
1149         crypto_mem_free(decrypt_para->buf);
1150         return HI_PRINT_ERRNO_UPG_CRYPTO_DECRYPT_ERR;
1151     }
1152 
1153     return ret;
1154 }
1155 
boot_decrypt_free_memory(hi_void)1156 hi_void boot_decrypt_free_memory(hi_void)
1157 {
1158     boot_crypto_ctx *decrypt_para = boot_decrypt_get_ctx();
1159     crypto_mem_free(decrypt_para->buf);
1160 }
1161 
1162 #endif
1163