lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | *********************************************************** |
| 3 | */ |
| 4 | |
| 5 | #include <common.h> |
| 6 | #include <asm/io.h> |
| 7 | #include <asm/string.h> |
| 8 | #include <sdio.h> |
| 9 | #include <image.h> |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 10 | #include "efuse.h" |
| 11 | #include "drv_rsa.h" |
| 12 | #include "drv_hash.h" |
| 13 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 14 | #define E_N_LEN 256 |
| 15 | #define HASH_LEN 128 |
| 16 | |
| 17 | |
| 18 | |
| 19 | /* |
| 20 | ****************************************************************************** |
| 21 | * Function: |
| 22 | * Description: |
| 23 | * Parameters: |
| 24 | * Input: |
| 25 | * Output: |
| 26 | * Returns: |
| 27 | * Others: |
| 28 | ******************************************************************************* |
| 29 | */ |
| 30 | static u8 data_cmp_word(u32* src, u32* dst, u32 cnt) |
| 31 | { |
| 32 | u32 i; |
| 33 | for(i = 0; i < cnt; i++) |
| 34 | { |
| 35 | if(src[i] != dst[i]) |
| 36 | { |
| 37 | return 1; |
| 38 | } |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /* |
| 45 | ****************************************************************************** |
| 46 | * Function: |
| 47 | * Description: |
| 48 | * Parameters: |
| 49 | * Input: |
| 50 | * Output: |
| 51 | * Returns: |
| 52 | * Others: |
| 53 | ******************************************************************************* |
| 54 | */ |
| 55 | int SecureVerify(u32 puiSdrmStartAddr) |
| 56 | { |
| 57 | u32 uiLen = 0; |
| 58 | u32 uiRet = -1; |
| 59 | image_header_t *puiLegacyImgAddr = NULL; |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 60 | sImageNewHeader *psImageHeader = NULL; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 61 | efuse_struct *psEfuseInfo = NULL; |
| 62 | |
| 63 | u32 *puiDataLoadAddr = NULL; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 64 | u32 *puiArrHASH = NULL; |
| 65 | u32 uiHashResArr[4] = {0}; |
| 66 | u32 uiHashResLen = 0; |
| 67 | u32 uiHashVerifySize = 0; |
| 68 | u32 uiRsaResArr[32] = {0}; |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 69 | u32 puiArrPubKey[64] = {0}; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 70 | int guiEfuseStatus = 1; |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 71 | int default_flag = 1; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 72 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 73 | T_Rsa_Paramter sRSAInput; |
| 74 | u32 *puiRsaResAddr = NULL; |
| 75 | |
| 76 | if(0 == puiSdrmStartAddr) |
| 77 | { |
| 78 | return -1; |
| 79 | } |
| 80 | |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 81 | psImageHeader = (sImageNewHeader *)puiSdrmStartAddr; |
| 82 | puiLegacyImgAddr = (image_header_t *)(puiSdrmStartAddr + sizeof(sImageNewHeader)); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 83 | uiHashVerifySize = ___htonl(puiLegacyImgAddr->ih_size) + sizeof(image_header_t); |
| 84 | |
| 85 | guiEfuseStatus = get_secure_verify_status(); |
| 86 | if(guiEfuseStatus == 0) //efuse secure verify. |
| 87 | psEfuseInfo = (efuse_struct*)EFUSE_RAM_BASE; |
| 88 | else |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 89 | { |
| 90 | default_flag = 0; |
| 91 | #if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF) |
| 92 | return 0; |
| 93 | #endif |
| 94 | } |
| 95 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 96 | |
| 97 | /* |
| 98 | * 0. ¼ì²éPubKeyÊÇ·ñ±»´Û¸Ä¡£ |
| 99 | * - ¶ÔPubKeyÃ÷ÎĽøÐÐHASH_MD5ÔËË㣬 |
| 100 | * ²¢ÇÒÓëefuseÖб£´æµÄpuk_hash±È½Ï¡£ |
| 101 | * - ±È½ÏÊý¾ÝÏàͬ£¬·µ»Ø0£» |
| 102 | * - ²»Í¬£¬·µ»Ø1¡£ |
| 103 | */ |
| 104 | uiLen = E_N_LEN; //¹«Ô¿EºÍN£¬¹²256byte³¤¶È¡£ |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 105 | memcpy((puiArrPubKey+31), psImageHeader->uiPubKeyRsaE, 4); |
| 106 | memcpy((puiArrPubKey+32), psImageHeader->uiPubKeyRsaN, 128); |
| 107 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 108 | uiRet = Hash_Calculate(HASH_MODE_MD5, |
| 109 | HASH_SMALL_ENDIAN, |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 110 | puiArrPubKey, |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 111 | uiLen, |
| 112 | NULL, |
| 113 | 0, |
| 114 | uiHashResArr, |
| 115 | &uiHashResLen); |
| 116 | if(uiRet != 0) |
| 117 | { |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 118 | return 1; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 119 | } |
| 120 | |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 121 | if(1 == default_flag) |
| 122 | { |
| 123 | if(data_cmp_word((u32 *)psEfuseInfo->puk_hash, |
| 124 | uiHashResArr, uiHashResLen)) |
| 125 | { |
| 126 | //printf("Puk hash verify fail!\n"); |
| 127 | printf("2\n"); |
| 128 | return 2; |
| 129 | } |
| 130 | } |
| 131 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 132 | puiArrHASH = psImageHeader->uiHashY; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 133 | |
| 134 | /* |
| 135 | * 1. ÀûÓù«Ô¿¶ÔuiHashY'½øÐнâÃÜ£¬µÃµ½1024bit½á¹û¡£ |
| 136 | */ |
| 137 | sRSAInput.udCalMode = RSA_MOD_EXPO_WITH_INIT; |
| 138 | sRSAInput.udNbitLen = 1024; |
| 139 | sRSAInput.udEbitLen = 1024; |
| 140 | sRSAInput.pudInputM = puiArrHASH; |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 141 | sRSAInput.pudInputE = puiArrPubKey; |
| 142 | sRSAInput.pudInputN = (puiArrPubKey + 32); |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 143 | sRSAInput.pudOutputP = uiRsaResArr; |
| 144 | |
| 145 | uiRet = Rsa_Calculate(sRSAInput); |
| 146 | if(uiRet != 0) |
| 147 | { |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 148 | //printf("Rsa_Calculate fail!\n"); |
| 149 | printf("3\n"); |
| 150 | return 3; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | //È¡×îºó4×Ö½Ú×÷ΪPubKey½âÃܺóµÄHASH_MD5Öµ¡£ |
| 154 | puiRsaResAddr = sRSAInput.pudOutputP + (32 - uiHashResLen); |
| 155 | |
| 156 | /* |
| 157 | * 2. ¼ÆËãÏÂÒ»¼¶¾µÏñµÄHASH_MD5Öµ¡£ |
| 158 | * - ¶ÔLegacyImage(64 BytesµÄÍ·ÐÅÏ¢+°æ±¾Çø)½øÐÐhash¼ÆË㣬 |
| 159 | * ²¢ÇÒÓëPubKeyÑéÇ©µÄ½á¹û½øÐбȽϡ£ |
| 160 | */ |
| 161 | uiLen = uiHashVerifySize; |
| 162 | puiDataLoadAddr = (u32 *)(___htonl(puiLegacyImgAddr->ih_load) |
| 163 | - sizeof(image_header_t)); |
| 164 | |
| 165 | /* Cleanup Output Buffer. */ |
| 166 | uiHashResLen = 0; |
| 167 | memset(uiHashResArr, 0, 4*sizeof(uiHashResArr[0])); |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 168 | |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 169 | uiRet = Hash_Calculate(HASH_MODE_MD5, |
| 170 | HASH_SMALL_ENDIAN, |
| 171 | puiDataLoadAddr, |
| 172 | uiLen, |
| 173 | NULL, |
| 174 | 0, |
| 175 | uiHashResArr, |
| 176 | &uiHashResLen); |
| 177 | if(uiRet != 0) |
| 178 | { |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 179 | //printf("Hash_Calculate Fail!\n"); |
| 180 | printf("4\n"); |
| 181 | return 4; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if(data_cmp_word(puiRsaResAddr, uiHashResArr, uiHashResLen)) |
| 185 | { |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame^] | 186 | //printf("SignImage Verify Fail!\n"); |
| 187 | printf("5\n"); |
| 188 | return 5; |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 189 | } |
lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | |