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 #ifndef __ENCRYPT_UPG_DUAL_PARTITION_H__ 17 #define __ENCRYPT_UPG_DUAL_PARTITION_H__ 18 19 #if defined(CONFIG_FLASH_ENCRYPT_SUPPORT) 20 #include <hi_config.h> 21 #include <hi_mem.h> 22 23 #ifdef CRYPTO_DEBUG 24 #include <hi_early_debug.h> 25 #define crypto_print(ftm...) \ 26 do { \ 27 printf(ftm); \ 28 } while (0); 29 #else 30 #define crypto_print(ftm...) 31 #endif 32 33 #define crypto_mem_free(sz) \ 34 do { \ 35 if ((sz) != HI_NULL) { \ 36 hi_free(HI_MOD_ID_CRYPTO, (sz)); \ 37 } \ 38 (sz) = HI_NULL; \ 39 } while (0) 40 41 #define IV_BYTE_LENGTH 16 42 #define ROOTKEY_IV_BYTE_LENGTH 32 43 44 #define DIE_ID_BYTE_LENGTH 24 45 46 #define KEY_BYTE_LENGTH 32 47 48 #define SHA_256_LENGTH 32 49 50 #define ROOT_SALT_LENGTH 32 51 52 #define CRYPTO_CNT_NUM 6 53 54 #define CRYPTO_KERNEL_LENGTH 4096 55 56 #define ENCRYPT_KDF_ITERATION_CNT 1024 57 58 #define MIN_CRYPTO_BLOCK_SIZE 16 59 60 #define HI_NV_FTM_KERNELA_WORK_ID 0x4 61 #define HI_NV_FTM_BACKUP_KERNELA_WORK_ID 0x5 62 #define HI_NV_FTM_KERNELB_WORK_ID 0x6 63 #define HI_NV_FTM_BACKUP_KERNELB_WORK_ID 0x7 64 65 typedef enum { 66 CRYPTO_WORKKEY_KERNEL_A = 0x1, 67 CRYPTO_WORKKEY_KERNEL_A_BACKUP = 0x2, 68 CRYPTO_WORKKEY_KERNEL_A_BOTH = 0x3, 69 CRYPTO_WORKKEY_KERNEL_B = 0x4, 70 CRYPTO_WORKKEY_KERNEL_B_BACKUP = 0x8, 71 CRYPTO_WORKKEY_KERNEL_B_BOTH = 0xC, 72 } crypto_workkey_partition; 73 74 typedef struct { 75 hi_u8 root_salt[ROOT_SALT_LENGTH]; 76 hi_u8 iv_nv[IV_BYTE_LENGTH]; 77 hi_u8 iv_content[IV_BYTE_LENGTH]; 78 hi_u8 work_text[KEY_BYTE_LENGTH]; 79 hi_u8 content_sh256[SHA_256_LENGTH]; 80 } hi_flash_crypto_content; 81 82 typedef struct { 83 uintptr_t kernel_start; 84 uintptr_t encrypt_offset; 85 hi_u32 encrypt_size; 86 hi_u8 upg_iv[IV_BYTE_LENGTH]; 87 hi_u8 upg_salt[IV_BYTE_LENGTH]; 88 hi_u8 *raw_buf; 89 } encrypt_ctx; 90 91 encrypt_ctx *encrypt_get_ctx(hi_void); 92 93 hi_u32 crypto_decrypt(encrypt_ctx *para); 94 95 hi_u32 encrypt_upg_data(encrypt_ctx *para); 96 97 hi_u32 crypto_encrypt_data_to_flash(uintptr_t kernel_offset); 98 99 #endif 100 101 #endif 102