blob: 30e847309186a706103757465f686cbe279eaf5c [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
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>
lh9ed821d2023-04-07 01:36:19 -070010#include "efuse.h"
11#include "drv_rsa.h"
12#include "drv_hash.h"
13
lh9ed821d2023-04-07 01:36:19 -070014#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 */
30static 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 */
55int SecureVerify(u32 puiSdrmStartAddr)
56{
57 u32 uiLen = 0;
58 u32 uiRet = -1;
59 image_header_t *puiLegacyImgAddr = NULL;
xf.liaa4d92f2023-09-13 00:18:58 -070060 sImageNewHeader *psImageHeader = NULL;
lh9ed821d2023-04-07 01:36:19 -070061 efuse_struct *psEfuseInfo = NULL;
62
63 u32 *puiDataLoadAddr = NULL;
lh9ed821d2023-04-07 01:36:19 -070064 u32 *puiArrHASH = NULL;
65 u32 uiHashResArr[4] = {0};
66 u32 uiHashResLen = 0;
67 u32 uiHashVerifySize = 0;
68 u32 uiRsaResArr[32] = {0};
xf.liaa4d92f2023-09-13 00:18:58 -070069 u32 puiArrPubKey[64] = {0};
lh9ed821d2023-04-07 01:36:19 -070070 int guiEfuseStatus = 1;
xf.liaa4d92f2023-09-13 00:18:58 -070071 int default_flag = 1;
lh9ed821d2023-04-07 01:36:19 -070072
lh9ed821d2023-04-07 01:36:19 -070073 T_Rsa_Paramter sRSAInput;
74 u32 *puiRsaResAddr = NULL;
75
76 if(0 == puiSdrmStartAddr)
77 {
78 return -1;
79 }
80
xf.liaa4d92f2023-09-13 00:18:58 -070081 psImageHeader = (sImageNewHeader *)puiSdrmStartAddr;
82 puiLegacyImgAddr = (image_header_t *)(puiSdrmStartAddr + sizeof(sImageNewHeader));
lh9ed821d2023-04-07 01:36:19 -070083 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.liaa4d92f2023-09-13 00:18:58 -070089 {
90 default_flag = 0;
91#if defined(CONFIG_ZX297520V3E_VEHICLE_DC) || defined(CONFIG_ZX297520V3E_VEHICLE_DC_REF)
92 return 0;
93#endif
94 }
95
lh9ed821d2023-04-07 01:36:19 -070096
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.liaa4d92f2023-09-13 00:18:58 -0700105 memcpy((puiArrPubKey+31), psImageHeader->uiPubKeyRsaE, 4);
106 memcpy((puiArrPubKey+32), psImageHeader->uiPubKeyRsaN, 128);
107
lh9ed821d2023-04-07 01:36:19 -0700108 uiRet = Hash_Calculate(HASH_MODE_MD5,
109 HASH_SMALL_ENDIAN,
xf.liaa4d92f2023-09-13 00:18:58 -0700110 puiArrPubKey,
lh9ed821d2023-04-07 01:36:19 -0700111 uiLen,
112 NULL,
113 0,
114 uiHashResArr,
115 &uiHashResLen);
116 if(uiRet != 0)
117 {
xf.liaa4d92f2023-09-13 00:18:58 -0700118 return 1;
lh9ed821d2023-04-07 01:36:19 -0700119 }
120
xf.liaa4d92f2023-09-13 00:18:58 -0700121 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
lh9ed821d2023-04-07 01:36:19 -0700132 puiArrHASH = psImageHeader->uiHashY;
lh9ed821d2023-04-07 01:36:19 -0700133
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.liaa4d92f2023-09-13 00:18:58 -0700141 sRSAInput.pudInputE = puiArrPubKey;
142 sRSAInput.pudInputN = (puiArrPubKey + 32);
lh9ed821d2023-04-07 01:36:19 -0700143 sRSAInput.pudOutputP = uiRsaResArr;
144
145 uiRet = Rsa_Calculate(sRSAInput);
146 if(uiRet != 0)
147 {
xf.liaa4d92f2023-09-13 00:18:58 -0700148 //printf("Rsa_Calculate fail!\n");
149 printf("3\n");
150 return 3;
lh9ed821d2023-04-07 01:36:19 -0700151 }
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.liaa4d92f2023-09-13 00:18:58 -0700168
lh9ed821d2023-04-07 01:36:19 -0700169 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.liaa4d92f2023-09-13 00:18:58 -0700179 //printf("Hash_Calculate Fail!\n");
180 printf("4\n");
181 return 4;
lh9ed821d2023-04-07 01:36:19 -0700182 }
183
184 if(data_cmp_word(puiRsaResAddr, uiHashResArr, uiHashResLen))
185 {
xf.liaa4d92f2023-09-13 00:18:58 -0700186 //printf("SignImage Verify Fail!\n");
187 printf("5\n");
188 return 5;
lh9ed821d2023-04-07 01:36:19 -0700189 }
lh9ed821d2023-04-07 01:36:19 -0700190 return 0;
191}
192
193
194
195