123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- #include "includes.h"
- /*
- 文件描述
- 1、主要处理终端与DTU之间的授权功能所需要的通讯逻辑
- 2、终端在正确下载完APP后,如果未进行授权,会自动跳到授权的任务中,直到授权成功
- 3、授权码产生算法存在于本设备及服务器中
- 4、终端启动后会读取MCU内存中的授权码,并与使用MCU ID进行加密码得出的授权码进行比对,不一样则未授权
- */
- #define UNIQUE_ID_BYTES 12
- #define TAKE_PRINT_FLAG 0
- #define RAND_STR_LEN 6
- const unsigned char randStr[RAND_STR_LEN]={'s','z','5','0','2','8'};
- const unsigned char srcAuth[16]={0x17,0x0d,0x55,0x9f,0xab,0xcc,0x1c,0x8f,0x30,0x0a,0x28,0xda,0x3c,0x91,0x11,0x6d};
- const unsigned char FirmSrc[10]={0x50,0x28,0xaf,0x6d,0x08,0x9c,0x1b,0xe4,0x23,0x77};
- uint32_t auth_seg_addr,iap_temp_addr,app_seg_addr;
- uint16_t this_dev_page_size,this_dev_flash_size;
- unsigned char SlaveNum;
- unsigned char Authed;
- //获取唯一ID接口
- typedef enum{
- STM32F0,STM32F1,STM32F2,STM32F3,STM32F4,STM32F7,
- STM32L0,STM32L1,STM32L4,
- STM32H7,
- STM_END
- }STM32_MCU_DEF;
- unsigned int mcustartAddr[STM_END]=
- {
- 0x1FFFF7AC,/*STM32F0*/
- 0x1FFFF7E8,/*STM32F1*/
- 0x1FFF7A10,/*STM32F2*/
- 0x1FFFF7AC,/*STM32F3*/
- 0x1FFF7A10,/*STM32F4*/
- 0x1FF0F420,/*STM32F7*/
- 0x1FF80050,/*STM32L0*/
- 0x1FF80050,/*STM32L1*/
- 0x1FFF7590,/*STM32L4*/
- 0x1FF0F420 /*STM32H7*/
- };
- void GetSTM32_McuID(unsigned char *id,STM32_MCU_DEF type)
- {
- unsigned char i;
- if(id==NULL) return;
-
- for(i=0;i<UNIQUE_ID_BYTES;i++){
- if(NULL != &id[i])
- id[i]=*(unsigned int *)(mcustartAddr[type]+i);
- }
-
-
-
-
- }
- unsigned int encodeWithID(unsigned char *id,unsigned char idlen)
- {
- const unsigned char sucRandTable[256]={
- 0x68,0x59,0x63,0x64,0xc8,0xcc,0xaa,0xa7,0xa2,0xab,0xa5,0x6e,0x6a,0x56,0x8a,0x57,
- 0xf4,0xbb,0xfe,0xfa,0x26,0x21,0x2d,0x29,0x0d,0x09,0x3e,0x3a,0x37,0x32,0x0a,0x07,
- 0xf7,0xf2,0xfb,0xf5,0xff,0xf6,0x1c,0x18,0x13,0x14,0xf1,0xfd,0xf9,0x0c,0x9f,0x08,
- 0x9a,0x97,0x92,0x9b,0x95,0x9f,0x12,0x1b,0x15,0x2c,0x28,0x23,0xf9,0x2a,0x27,0x22,
- 0x24,0x2e,0x67,0x25,0x2f,0x98,0xfc,0x75,0xf8,0xf3,0x3c,0x38,0x33,0x34,0x03,0x04,
- 0x05,0x0f,0x06,0x01,0x7a,0x77,0x72,0x62,0x6b,0x83,0x84,0x8e,0xc4,0xcb,0x87,0x82,
- 0x35,0x3f,0x36,0x4f,0x3d,0x39,0x9c,0x98,0x93,0x94,0x42,0x4f,0x46,0x41,0x81,0x8d,
- 0x4d,0x49,0x71,0x7d,0x79,0x96,0x91,0x9d,0x99,0xc3,0xc4,0x1d,0xce,0xca,0x7f,0x76,
- 0x7c,0x78,0x73,0x74,0x7e,0x7b,0x75,0x1e,0x1a,0x17,0x1f,0x16,0x11,0x1d,0x47,0x19,
- 0x5d,0xc7,0xc2,0xcb,0xc5,0xcf,0xd1,0xdd,0xd9,0x4c,0x48,0xc6,0xc1,0xcd,0x61,0xc9,
- 0xd7,0x8c,0xfd,0x88,0xa1,0xad,0xa9,0xb1,0xbd,0xb9,0x8b,0x85,0x8f,0x4b,0x45,0x86,
- 0xdc,0xd8,0xd3,0xd4,0xde,0xd2,0xdb,0xd5,0xdf,0xd6,0x43,0x44,0x4e,0x4a,0xe7,0x47,
- 0xac,0xa8,0xa3,0xa4,0xae,0xba,0x0d,0xb7,0xb2,0xaf,0xa6,0xe4,0xee,0xea,0xe7,0xe2,
- 0xe1,0xed,0x0f,0xe9,0x66,0x61,0x6d,0x69,0xbb,0xb5,0xbf,0xb6,0xec,0xe8,0x0b,0xe3,
- 0x52,0x5c,0x65,0x6f,0x58,0x53,0x54,0x0c,0x5e,0x5b,0x55,0x5f,0x31,0x2f,0x9a,0x0b,
- 0x0b,0x5a,0x2b,0x9e,0x02,0x89,0x59,0x3b,0x0e,0x51,0x6c,0xeb,0xda,0xe6,0x31,0xf6
- };
- unsigned char i;
- unsigned int ret;
- unsigned char temp1,temp2;
- unsigned char idtemp[64];
-
- temp1=0;
-
- for(i=0;i<idlen;i++){
- temp1 ^= id[i];
- idtemp[i]=id[i];
- }
-
- temp1 ^= idlen;
- temp2=sucRandTable[temp1];
- //循环查表异或运算,生成新id
- for(i=0;i<idlen;i++){
- temp1=idtemp[i]^temp2;
- idtemp[i]=i^temp1;
- temp2=sucRandTable[temp1];
- }
-
- //最后每3字节再异或得到4字节作为输出
- for(i=0;i<4;i++){
- temp1=idtemp[3*i] ^ idtemp[3*i+1];
- idtemp[i]=temp1 ^ idtemp[3*i+2];
- }
- ret=idtemp[0];ret <<= 8;ret &= 0xFFFFFF00;
- ret |= idtemp[1];ret <<= 8;ret &= 0xFFFFFF00;
- ret |= idtemp[2];ret <<= 8;ret &= 0xFFFFFF00;
- ret |= idtemp[3];
- return ret;
- }
- void authSendCmd(unsigned char cmd, unsigned char *data, unsigned short len)
- {
- //协议格式
- //head cmd len data
- //data:liushui[2] id[12] 均经过cmd和长度加密
- static unsigned short liushui=0;
- unsigned char sendbuf[128];
- unsigned char i,k;
- unsigned char *dataPtr=sendbuf+3;
- unsigned char datalen=2+len+1;//加上sum
- unsigned char temp[3],sum;
-
- i=0;
- sendbuf[i++]=PRO_HEADER;
- sendbuf[i++]=cmd;
- sendbuf[i++]=datalen;
- sendbuf[i++]=liushui>>8; //4-5
- sendbuf[i++]=liushui;
-
- for(k=0;k<len;k++)
- sendbuf[i++]=data[k];
-
- temp[0]=3+datalen;//总长度,包括sum //47+3
- temp[1]=sendbuf[0]; //0x89
- temp[2]=sendbuf[1]; //0x34
-
- for(k=0;k<datalen-1;k++){
- dataPtr[k] ^= temp[k % 3];
- dataPtr[k] ^= TX_RX_SEED;
- }
- sum=0;
- for(k=0;k<i;k++) sum ^= sendbuf[k];
- sendbuf[i++]=sum;
- Uart1Send(sendbuf, i);
- #if(TAKE_PRINT_FLAG)
- printf("send[%d]:",i);
- for(k=0;k<i;k++) printf("%02x",sendbuf[k]);
- printf("\r\n");
- #endif
- }
- #define FACTORY_NAME "ZT" //最大16
- #define MODE_NAME "ZT200" //最大16
- void EncodeKeyWithFactorAndMode(unsigned char *code)
- {
- int i,j;
- char *FN=FACTORY_NAME;
- char *MN=MODE_NAME;
-
- j=strlen(FACTORY_NAME);
- for(i=0;i<j;i++) code[i%4] ^= FN[i];
-
- j=strlen(MODE_NAME);
- for(i=0;i<j;i++) code[i%4] ^= MN[i];
- }
- char decodeAuthInfo(unsigned char *data, unsigned char len,unsigned int targetcode,AUTH_INFO *authInfo)
- {
- unsigned char i;
- unsigned char sum=0;
- unsigned char temp[3];
- unsigned char *dataPtr=data+3;
- unsigned int codeData;
-
- if(data[0] != PRO_HEADER) return 1;
- if((data[2]+3) != len) return 2;
- for(i=0;i<len-1;i++) sum ^= data[i];
- if(sum != data[len-1]) return 3;
-
- temp[0]=len;
- temp[1]=data[0];
- temp[2]=data[1];
-
- for(i=0;i<data[1]-1;i++){
- dataPtr[i] ^= temp[i % 3];
- dataPtr[i] ^= TX_RX_SEED;
- }
-
- if(data[1]==PRO_AUTH_ACK){
- return 5;
- }else if(data[1]==PRO_AUTH_QUERY){
- return 6;
- }
-
- if(data[1]!=PRO_AUTH_SUC) return 4;
- //收到DTU通知鉴权成功,获取鉴权码
- //跳过流水号
- dataPtr += 2;
- EncodeKeyWithFactorAndMode(dataPtr);
- codeData=dataPtr[0];codeData <<= 8;codeData &= 0xFFFFFF00;
- codeData |= dataPtr[1];codeData <<= 8;codeData &= 0xFFFFFF00;
- codeData |= dataPtr[2];codeData <<= 8;codeData &= 0xFFFFFF00;
- codeData |= dataPtr[3];
- dataPtr += 4;
- //SlaveNum=*dataPtr;
- authInfo->SlaveNum=*dataPtr;
- //write new auth
- authInfo->authCode=codeData;
- //make lier data
- for(i=0;i<sizeof(authInfo->noUseStart);i++){
- authInfo->noUseStart[i]=*dataPtr++^data[6];
- if(dataPtr>=&data[len]) dataPtr=data;
- }
- for(i=0;i<sizeof(authInfo->noUseEnd);i++){
- authInfo->noUseEnd[i]=*dataPtr++^data[4];
- if(dataPtr>=&data[len]) dataPtr=data;
- }
- //WritePageData(auth_seg_addr,(unsigned char *)authInfo,sizeof(AUTH_INFO));
- WritePageData(AUTH_PARA_ADDR,(unsigned char *)authInfo,sizeof(AUTH_INFO));
- printf("newAuthInfo:%08x,targetcode:%08x\r\n",codeData,targetcode);
- if(codeData == targetcode){//当激活码与本地算的一致时
- return 0;
-
- }else return 4;//当激活码与本地算的不一致时
- }
- //根据MCU的FLASH容量大小判断出page大小,然后得到激活段与升级临时段的起始地址
- void GetStartAddrByFlashSize(void)
- {
- #if 1
- auth_seg_addr=0x8000000+124*1024;
- #else
- this_dev_flash_size=*(uint16_t *)(0x1FFFF7E0);//获取FLASH容量
- if(this_dev_flash_size <= 128) this_dev_page_size=2048;
- else this_dev_page_size=2048;
- //取得激活码段起始地址
- auth_seg_addr=APP_CONFIG_ADDR+this_dev_page_size;
- //取得临时段起始地址
- iap_temp_addr=auth_seg_addr+this_dev_page_size;
- //取得APP应用起始地址
- app_seg_addr=iap_temp_addr+this_dev_page_size;
- if(app_seg_addr % 2048) app_seg_addr += 1024;//跳转地址需要是页大小的倍数
- //printf("addr:%08x,%08x,%08x\r\n",auth_seg_addr,iap_temp_addr,app_seg_addr);
- #endif
- }
- void reInitUart1(void)
- {//关闭IDLE中断,打开NE中断
- USART_ITConfig(USART1, USART_IT_IDLE, DISABLE);
- USART_DMACmd(USART1,USART_DMAReq_Rx,DISABLE);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- rx1_ct=0;
- g_usUart1RecvLen=0;
- }
- void authLink()
- {
- unsigned int i=0;
- AUTH_INFO authInfo;
- unsigned char id[UNIQUE_ID_BYTES];
- //unsigned char id[UNIQUE_ID_BYTES]={1,2,3,4,5,6,7,8,9,1,1,2};
- unsigned int encodeValue;
- unsigned char senddata[100];
- unsigned int len;
- char ret;
- unsigned char j=0;
- unsigned char handshake=0;
- GetSTM32_McuID(id, STM32F1);
-
- #if(TAKE_PRINT_FLAG)
- printf("ID[%d]:",UNIQUE_ID_BYTES);
- for(i=0;i<sizeof(id);i++) printf("%02x",id[i]);
- printf("\r\n");
- #endif
- //decode with id
- encodeValue=encodeWithID(id,UNIQUE_ID_BYTES);//自己计算出 只通过MCUID
-
- #if(TAKE_PRINT_FLAG)
- printf("encodeValue:%08x\r\n",encodeValue);
- #endif
-
- memcpy(senddata, id, UNIQUE_ID_BYTES);
- memset(senddata+UNIQUE_ID_BYTES, 0, 16);
- strcpy(senddata+UNIQUE_ID_BYTES, FACTORY_NAME);
- memset(senddata+UNIQUE_ID_BYTES+16, 0, 16);
- strcpy(senddata+UNIQUE_ID_BYTES+16, MODE_NAME);
-
- //read encode in flash
- ReadFlashData( AUTH_PARA_ADDR,(unsigned char *)&authInfo,sizeof(AUTH_INFO));
- if(authInfo.authCode == encodeValue)return;
-
- while(1){
-
- if(i==0&&!handshake) authSendCmd(PRO_AUTH_RESPONS,0,0); //请求握手
-
- if(++i>=3){
- if(!handshake)j++;
- i=0;
- }
- if(j>3)break;
- IWDG_ReloadCounter();//喂狗
- DelayMs(100);
-
- if(g_usUart1RecvLen==0) continue;
-
- ret=decodeAuthInfo(RxBuffer1,g_usUart1RecvLen,encodeValue,&authInfo);
-
- if(0==ret){
- authSendCmd(PRO_AUTH_OK,id,UNIQUE_ID_BYTES);
- return;
- //break;
- }else if(4==ret){//鉴权码不正确
- authSendCmd(PRO_AUTH_FAI,id,UNIQUE_ID_BYTES);
- }else if(5==ret){//握手成功
- handshake=1;
- }else if(6==ret){ //请求基本数据
- authSendCmd(PRO_AUTH_REQ,senddata,UNIQUE_ID_BYTES+16+16);
- }
- g_usUart1RecvLen=0;
- }
- while(1){
-
- IWDG_ReloadCounter();//喂狗
- DelayMs(100);
- printf("please try auth-------------------\r\n");
- }
- }
- #define APP_UPDATE_COMPLETED 0x5c44f1d0
- #define APP_UPDATE_DEFAULT 0xffffffff
- #define APP_UPDATE_NONE_COMPLETED 0x2487314d
- void takeNoteThatFlushAppCompleteOrNot(char status)
- {
- unsigned int flushStatus;
- unsigned int currentStatus;
- if(status==0) flushStatus=APP_UPDATE_NONE_COMPLETED;//未完成APP的更新
- else flushStatus=APP_UPDATE_COMPLETED;//已完成APP的更新
-
- ReadFlashData(iap_temp_addr, (unsigned char *)¤tStatus, 4);
- if(currentStatus == flushStatus) return;
- WritePageData(iap_temp_addr, (unsigned char *)&flushStatus, 4);
- }
- void checkLastAppUpdateCompleteOrNot(void)
- {
- unsigned int currentStatus;
- ReadFlashData(iap_temp_addr, (unsigned char *)¤tStatus, 4);
- if(currentStatus == APP_UPDATE_COMPLETED || currentStatus == APP_UPDATE_DEFAULT) return;
- currentStatus=0;
- while(1){
- if(currentStatus==0) printf("Last update err, need retry!\r\n");
- if(++currentStatus >= 2000*2000) currentStatus=0;
- }
- }
- /*
- 对长度为tagetlen的targetstring 进行加密
- 入参为randlen的随机串randstring
- */
- void extCodeBuffer(unsigned char *randString, int randlen, unsigned char *targetString, int targetlen){
- int i,j,k;
- unsigned char value;
- //先使用随机串依次对targetString进行异或
- j=0;
- for(i=0;i<targetlen;i++){
- targetString[i] ^= randString[j];
- if(++j>=randlen) j=0;
- }
- //再对targetstring的奇数索引字节,与randstring的高低4位指向的srcAuth索引字节异或
- j=0;
- for(i=0;i<targetlen;i++){
- if(j%2) k=randStr[j]&0x0F;
- else k=(randStr[j]>>4)&0x0F;
- value=srcAuth[k];
- if(++j>=randlen) j=0;
- if(i%2) targetString[i] ^= value;
- }
- }
- unsigned char PocAuthDataPack(unsigned char *Packdata)
- {
-
- unsigned char id[UNIQUE_ID_BYTES];
- unsigned char i=0;
- GetSTM32_McuID(id, STM32F1);
-
- strcpy(&Packdata[i], FACTORY_NAME);
- i=i+16;
- strcpy(&Packdata[i], MODE_NAME);
- i=i+16;
- Packdata[i]=UNIQUE_ID_BYTES;
- i=i+1;
-
- memcpy(&Packdata[i], id,UNIQUE_ID_BYTES);
- i=i+UNIQUE_ID_BYTES;
- return i;
- }
- //unsigned char AscToHex(unsigned char aHex)
- //{
- // if((aHex>=0)&&(aHex<=9))
- // aHex += 0x30;
- // else if((aHex>=10)&&(aHex<=15))//A-F
- // //aHex += 0x37;
- // aHex += 0x57;
- // else aHex = 0xff;
- // return aHex;
- //}
- static void AscStrToHexStrForgetZero(char *AscStr, char *HexStr,unsigned char len)
- {
- char *pAscStr=AscStr;
- unsigned char d,h,l;
-
- while(len){
- d=(unsigned char)*pAscStr++;
- l=d&0x0f;
- h=d>>4;
- *HexStr++=AscToHex(h);
- *HexStr++=AscToHex(l);
- len--;
- }
-
- *HexStr=0;
- }
- void PocAuthProcess(char *msg)
- {
- unsigned char Ranbuf[20];
- unsigned char SendDataBuf[120];
- unsigned char DelDataBuf[120];
- unsigned char i=0;
- unsigned char len;
-
- memset(DelDataBuf,0,sizeof(DelDataBuf));
- memset(SendDataBuf,0,sizeof(SendDataBuf));
- memset(Ranbuf,0,sizeof(Ranbuf));
- printf(msg);
- //BEA6F8ECFFE4 -->0xBE 0XA6
- StrAsciiToHex(msg,Ranbuf);
- memcpy(DelDataBuf,FirmSrc,sizeof(FirmSrc));
-
- //MCU参数组包
- len=sizeof(FirmSrc);
- extCodeBuffer(Ranbuf,strlen(Ranbuf)-1,DelDataBuf,len);
-
- // len=PocAuthDataPack(DelDataBuf);
- // extCodeBuffer(Ranbuf,strlen(Ranbuf)-1,DelDataBuf,len); //因为回车换行转成一个字节
- AscStrToHexStrForgetZero(DelDataBuf,SendDataBuf,len);
- printf(SendDataBuf);
-
- memset(DelDataBuf,0,sizeof(DelDataBuf));
-
- //AT+SAUTH
- snprintf(DelDataBuf,sizeof(DelDataBuf),"AT+SAUTH=%s\r\n",SendDataBuf);
- printf(DelDataBuf);
- ModemSendData(DelDataBuf,len*2+12);
-
-
- }
|