auth.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. #include "includes.h"
  2. /*
  3. 文件描述
  4. 1、主要处理终端与DTU之间的授权功能所需要的通讯逻辑
  5. 2、终端在正确下载完APP后,如果未进行授权,会自动跳到授权的任务中,直到授权成功
  6. 3、授权码产生算法存在于本设备及服务器中
  7. 4、终端启动后会读取MCU内存中的授权码,并与使用MCU ID进行加密码得出的授权码进行比对,不一样则未授权
  8. */
  9. #define UNIQUE_ID_BYTES 12
  10. #define TAKE_PRINT_FLAG 0
  11. uint32_t auth_seg_addr,iap_temp_addr,app_seg_addr;
  12. uint16_t this_dev_page_size,this_dev_flash_size;
  13. unsigned char SlaveNum;
  14. unsigned char Authed=1;
  15. //获取唯一ID接口
  16. typedef enum{
  17. STM32F0,STM32F1,STM32F2,STM32F3,STM32F4,STM32F7,
  18. STM32L0,STM32L1,STM32L4,
  19. STM32H7,
  20. STM_END
  21. }STM32_MCU_DEF;
  22. unsigned int mcustartAddr[STM_END]=
  23. {
  24. 0x1FFFF7AC,/*STM32F0*/
  25. 0x1FFFF7E8,/*STM32F1*/
  26. 0x1FFF7A10,/*STM32F2*/
  27. 0x1FFFF7AC,/*STM32F3*/
  28. 0x1FFF7A10,/*STM32F4*/
  29. 0x1FF0F420,/*STM32F7*/
  30. 0x1FF80050,/*STM32L0*/
  31. 0x1FF80050,/*STM32L1*/
  32. 0x1FFF7590,/*STM32L4*/
  33. 0x1FF0F420 /*STM32H7*/
  34. };
  35. void GetSTM32_McuID(unsigned char *id,STM32_MCU_DEF type)
  36. {
  37. unsigned char i;
  38. if(id==NULL) return;
  39. for(i=0;i<UNIQUE_ID_BYTES;i++){
  40. if(NULL != &id[i])
  41. id[i]=*(unsigned int *)(mcustartAddr[type]+i);
  42. }
  43. }
  44. unsigned int encodeWithID(unsigned char *id,unsigned char idlen)
  45. {
  46. const unsigned char sucRandTable[256]={
  47. 0x68,0x59,0x63,0x64,0xc8,0xcc,0xaa,0xa7,0xa2,0xab,0xa5,0x6e,0x6a,0x56,0x8a,0x57,
  48. 0xf4,0xbb,0xfe,0xfa,0x26,0x21,0x2d,0x29,0x0d,0x09,0x3e,0x3a,0x37,0x32,0x0a,0x07,
  49. 0xf7,0xf2,0xfb,0xf5,0xff,0xf6,0x1c,0x18,0x13,0x14,0xf1,0xfd,0xf9,0x0c,0x9f,0x08,
  50. 0x9a,0x97,0x92,0x9b,0x95,0x9f,0x12,0x1b,0x15,0x2c,0x28,0x23,0xf9,0x2a,0x27,0x22,
  51. 0x24,0x2e,0x67,0x25,0x2f,0x98,0xfc,0x75,0xf8,0xf3,0x3c,0x38,0x33,0x34,0x03,0x04,
  52. 0x05,0x0f,0x06,0x01,0x7a,0x77,0x72,0x62,0x6b,0x83,0x84,0x8e,0xc4,0xcb,0x87,0x82,
  53. 0x35,0x3f,0x36,0x4f,0x3d,0x39,0x9c,0x98,0x93,0x94,0x42,0x4f,0x46,0x41,0x81,0x8d,
  54. 0x4d,0x49,0x71,0x7d,0x79,0x96,0x91,0x9d,0x99,0xc3,0xc4,0x1d,0xce,0xca,0x7f,0x76,
  55. 0x7c,0x78,0x73,0x74,0x7e,0x7b,0x75,0x1e,0x1a,0x17,0x1f,0x16,0x11,0x1d,0x47,0x19,
  56. 0x5d,0xc7,0xc2,0xcb,0xc5,0xcf,0xd1,0xdd,0xd9,0x4c,0x48,0xc6,0xc1,0xcd,0x61,0xc9,
  57. 0xd7,0x8c,0xfd,0x88,0xa1,0xad,0xa9,0xb1,0xbd,0xb9,0x8b,0x85,0x8f,0x4b,0x45,0x86,
  58. 0xdc,0xd8,0xd3,0xd4,0xde,0xd2,0xdb,0xd5,0xdf,0xd6,0x43,0x44,0x4e,0x4a,0xe7,0x47,
  59. 0xac,0xa8,0xa3,0xa4,0xae,0xba,0x0d,0xb7,0xb2,0xaf,0xa6,0xe4,0xee,0xea,0xe7,0xe2,
  60. 0xe1,0xed,0x0f,0xe9,0x66,0x61,0x6d,0x69,0xbb,0xb5,0xbf,0xb6,0xec,0xe8,0x0b,0xe3,
  61. 0x52,0x5c,0x65,0x6f,0x58,0x53,0x54,0x0c,0x5e,0x5b,0x55,0x5f,0x31,0x2f,0x9a,0x0b,
  62. 0x0b,0x5a,0x2b,0x9e,0x02,0x89,0x59,0x3b,0x0e,0x51,0x6c,0xeb,0xda,0xe6,0x31,0xf6
  63. };
  64. unsigned char i;
  65. unsigned int ret;
  66. unsigned char temp1,temp2;
  67. unsigned char idtemp[64];
  68. temp1=0;
  69. for(i=0;i<idlen;i++){
  70. temp1 ^= id[i];
  71. idtemp[i]=id[i];
  72. }
  73. temp1 ^= idlen;
  74. temp2=sucRandTable[temp1];
  75. //循环查表异或运算,生成新id
  76. for(i=0;i<idlen;i++){
  77. temp1=idtemp[i]^temp2;
  78. idtemp[i]=i^temp1;
  79. temp2=sucRandTable[temp1];
  80. }
  81. //最后每3字节再异或得到4字节作为输出
  82. for(i=0;i<4;i++){
  83. temp1=idtemp[3*i] ^ idtemp[3*i+1];
  84. idtemp[i]=temp1 ^ idtemp[3*i+2];
  85. }
  86. ret=idtemp[0];ret <<= 8;ret &= 0xFFFFFF00;
  87. ret |= idtemp[1];ret <<= 8;ret &= 0xFFFFFF00;
  88. ret |= idtemp[2];ret <<= 8;ret &= 0xFFFFFF00;
  89. ret |= idtemp[3];
  90. return ret;
  91. }
  92. void authSendCmd(unsigned char cmd, unsigned char *data, unsigned short len)
  93. {
  94. //协议格式
  95. //head cmd len data
  96. //data:liushui[2] id[12] 均经过cmd和长度加密
  97. static unsigned short liushui=0;
  98. unsigned char sendbuf[128];
  99. unsigned char i,k;
  100. unsigned char *dataPtr=sendbuf+3;
  101. unsigned char datalen=2+len+1;//加上sum
  102. unsigned char temp[3],sum;
  103. i=0;
  104. sendbuf[i++]=PRO_HEADER;
  105. sendbuf[i++]=cmd;
  106. sendbuf[i++]=datalen;
  107. sendbuf[i++]=liushui>>8; //4-5
  108. sendbuf[i++]=liushui;
  109. for(k=0;k<len;k++)
  110. sendbuf[i++]=data[k];
  111. temp[0]=3+datalen;//总长度,包括sum //47+3
  112. temp[1]=sendbuf[0]; //0x89
  113. temp[2]=sendbuf[1]; //0x34
  114. for(k=0;k<datalen-1;k++){
  115. dataPtr[k] ^= temp[k % 3];
  116. dataPtr[k] ^= TX_RX_SEED;
  117. }
  118. sum=0;
  119. for(k=0;k<i;k++) sum ^= sendbuf[k];
  120. sendbuf[i++]=sum;
  121. Uart1Send(sendbuf, i);
  122. #if(TAKE_PRINT_FLAG)
  123. printf("send[%d]:",i);
  124. for(k=0;k<i;k++) printf("%02x",sendbuf[k]);
  125. printf("\r\n");
  126. #endif
  127. }
  128. #define FACTORY_NAME "ZT" //最大16
  129. #define MODE_NAME "Q800" //最大16
  130. void EncodeKeyWithFactorAndMode(unsigned char *code)
  131. {
  132. int i,j;
  133. char *FN=FACTORY_NAME;
  134. char *MN=MODE_NAME;
  135. j=strlen(FACTORY_NAME);
  136. for(i=0;i<j;i++) code[i%4] ^= FN[i];
  137. j=strlen(MODE_NAME);
  138. for(i=0;i<j;i++) code[i%4] ^= MN[i];
  139. }
  140. char decodeAuthInfo(unsigned char *data, unsigned char len,unsigned int targetcode,AUTH_INFO *authInfo)
  141. {
  142. unsigned char i;
  143. unsigned char sum=0;
  144. unsigned char temp[3];
  145. unsigned char *dataPtr=data+3;
  146. unsigned int codeData;
  147. if(data[0] != PRO_HEADER) return 1;
  148. if((data[2]+3) != len) return 2;
  149. for(i=0;i<len-1;i++) sum ^= data[i];
  150. if(sum != data[len-1]) return 3;
  151. temp[0]=len;
  152. temp[1]=data[0];
  153. temp[2]=data[1];
  154. for(i=0;i<data[1]-1;i++){
  155. dataPtr[i] ^= temp[i % 3];
  156. dataPtr[i] ^= TX_RX_SEED;
  157. }
  158. if(data[1]==PRO_AUTH_ACK){
  159. return 5;
  160. }else if(data[1]==PRO_AUTH_QUERY){
  161. return 6;
  162. }
  163. if(data[1]!=PRO_AUTH_SUC) return 4;
  164. //收到DTU通知鉴权成功,获取鉴权码
  165. //跳过流水号
  166. dataPtr += 2;
  167. EncodeKeyWithFactorAndMode(dataPtr);
  168. codeData=dataPtr[0];codeData <<= 8;codeData &= 0xFFFFFF00;
  169. codeData |= dataPtr[1];codeData <<= 8;codeData &= 0xFFFFFF00;
  170. codeData |= dataPtr[2];codeData <<= 8;codeData &= 0xFFFFFF00;
  171. codeData |= dataPtr[3];
  172. dataPtr += 4;
  173. //SlaveNum=*dataPtr;
  174. authInfo->SlaveNum=*dataPtr;
  175. //write new auth
  176. authInfo->authCode=codeData;
  177. //make lier data
  178. for(i=0;i<sizeof(authInfo->noUseStart);i++){
  179. authInfo->noUseStart[i]=*dataPtr++^data[6];
  180. if(dataPtr>=&data[len]) dataPtr=data;
  181. }
  182. for(i=0;i<sizeof(authInfo->noUseEnd);i++){
  183. authInfo->noUseEnd[i]=*dataPtr++^data[4];
  184. if(dataPtr>=&data[len]) dataPtr=data;
  185. }
  186. //WritePageData(auth_seg_addr,(unsigned char *)authInfo,sizeof(AUTH_INFO));
  187. WritePageData(AUTH_PARA_ADDR,(unsigned char *)authInfo,sizeof(AUTH_INFO));
  188. printf("newAuthInfo:%08x,targetcode:%08x\r\n",codeData,targetcode);
  189. if(codeData == targetcode){//当激活码与本地算的一致时
  190. return 0;
  191. }else return 4;//当激活码与本地算的不一致时
  192. }
  193. //根据MCU的FLASH容量大小判断出page大小,然后得到激活段与升级临时段的起始地址
  194. void GetStartAddrByFlashSize(void)
  195. {
  196. #if 1
  197. auth_seg_addr=0x8000000+124*1024;
  198. #else
  199. this_dev_flash_size=*(uint16_t *)(0x1FFFF7E0);//获取FLASH容量
  200. if(this_dev_flash_size <= 128) this_dev_page_size=2048;
  201. else this_dev_page_size=2048;
  202. //取得激活码段起始地址
  203. auth_seg_addr=APP_CONFIG_ADDR+this_dev_page_size;
  204. //取得临时段起始地址
  205. iap_temp_addr=auth_seg_addr+this_dev_page_size;
  206. //取得APP应用起始地址
  207. app_seg_addr=iap_temp_addr+this_dev_page_size;
  208. if(app_seg_addr % 2048) app_seg_addr += 1024;//跳转地址需要是页大小的倍数
  209. //printf("addr:%08x,%08x,%08x\r\n",auth_seg_addr,iap_temp_addr,app_seg_addr);
  210. #endif
  211. }
  212. void reInitUart1(void)
  213. {//关闭IDLE中断,打开NE中断
  214. USART_ITConfig(USART1, USART_IT_IDLE, DISABLE);
  215. USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE);
  216. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  217. rx1_ct=0;
  218. g_usUart1RecvLen=0;
  219. }
  220. void authLink()
  221. {
  222. unsigned int i=0;
  223. AUTH_INFO authInfo;
  224. unsigned char id[UNIQUE_ID_BYTES];
  225. //unsigned char id[UNIQUE_ID_BYTES]={1,2,3,4,5,6,7,8,9,1,1,2};
  226. unsigned int encodeValue;
  227. unsigned char senddata[100];
  228. unsigned int len;
  229. char ret;
  230. unsigned char j=0;
  231. unsigned char handshake=0;
  232. GetSTM32_McuID(id, STM32F1);
  233. #if(TAKE_PRINT_FLAG)
  234. printf("ID[%d]:",UNIQUE_ID_BYTES);
  235. for(i=0;i<sizeof(id);i++) printf("%02x",id[i]);
  236. printf("\r\n");
  237. #endif
  238. //decode with id
  239. encodeValue=encodeWithID(id,UNIQUE_ID_BYTES);//自己计算出 只通过MCUID
  240. #if(TAKE_PRINT_FLAG)
  241. printf("encodeValue:%08x\r\n",encodeValue);
  242. #endif
  243. memcpy(senddata, id, UNIQUE_ID_BYTES);
  244. memset(senddata+UNIQUE_ID_BYTES, 0, 16);
  245. strcpy(senddata+UNIQUE_ID_BYTES, FACTORY_NAME);
  246. memset(senddata+UNIQUE_ID_BYTES+16, 0, 16);
  247. strcpy(senddata+UNIQUE_ID_BYTES+16, MODE_NAME);
  248. //read encode in flash
  249. ReadFlashData( AUTH_PARA_ADDR,(unsigned char *)&authInfo,sizeof(AUTH_INFO));
  250. if(authInfo.authCode == encodeValue)return;
  251. while(1){
  252. if(i==0&&!handshake) authSendCmd(PRO_AUTH_RESPONS,0,0); //请求握手
  253. if(++i>=3){
  254. if(!handshake)j++;
  255. i=0;
  256. }
  257. if(j>3)break;
  258. IWDG_ReloadCounter();//喂狗
  259. DelayMs(100);
  260. if(g_usUart1RecvLen==0) continue;
  261. ret=decodeAuthInfo(RxBuffer1,g_usUart1RecvLen,encodeValue,&authInfo);
  262. if(0==ret){
  263. authSendCmd(PRO_AUTH_OK,id,UNIQUE_ID_BYTES);
  264. return;
  265. //break;
  266. }else if(4==ret){//鉴权码不正确
  267. authSendCmd(PRO_AUTH_FAI,id,UNIQUE_ID_BYTES);
  268. }else if(5==ret){//握手成功
  269. handshake=1;
  270. }else if(6==ret){ //请求基本数据
  271. authSendCmd(PRO_AUTH_REQ,senddata,UNIQUE_ID_BYTES+16+16);
  272. }
  273. g_usUart1RecvLen=0;
  274. }
  275. while(1){
  276. IWDG_ReloadCounter();//喂狗
  277. DelayMs(100);
  278. printf("please try auth-------------------\r\n");
  279. }
  280. }
  281. #define APP_UPDATE_COMPLETED 0x5c44f1d0
  282. #define APP_UPDATE_DEFAULT 0xffffffff
  283. #define APP_UPDATE_NONE_COMPLETED 0x2487314d
  284. void takeNoteThatFlushAppCompleteOrNot(char status)
  285. {
  286. unsigned int flushStatus;
  287. unsigned int currentStatus;
  288. if(status==0) flushStatus=APP_UPDATE_NONE_COMPLETED;//未完成APP的更新
  289. else flushStatus=APP_UPDATE_COMPLETED;//已完成APP的更新
  290. ReadFlashData(iap_temp_addr, (unsigned char *)&currentStatus, 4);
  291. if(currentStatus == flushStatus) return;
  292. WritePageData(iap_temp_addr, (unsigned char *)&flushStatus, 4);
  293. }
  294. void checkLastAppUpdateCompleteOrNot(void)
  295. {
  296. unsigned int currentStatus;
  297. ReadFlashData(iap_temp_addr, (unsigned char *)&currentStatus, 4);
  298. if(currentStatus == APP_UPDATE_COMPLETED || currentStatus == APP_UPDATE_DEFAULT) return;
  299. currentStatus=0;
  300. while(1){
  301. if(currentStatus==0) printf("Last update err, need retry!\r\n");
  302. if(++currentStatus >= 2000*2000) currentStatus=0;
  303. }
  304. }