123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- /*******************************************************************
- * id.c
- *
- ********************************************************************/
- #define THIS_FILE_ID 3
- /* Includes ------------------------------------------------------------------*/
- #include <includes.h>
- #define ID_BASE_ADDR (0x1FFFF7E8ul)
- //
- //unsigned char aucSTM32ID1[12];
- //unsigned char aucSTM32SN1[12];
- //unsigned char aucSTM32ID2[12];
- //unsigned char aucSTM32SN2[12];
- //unsigned char aucSTM32ID3[12];
- //unsigned char aucSTM32SN3[12];
- //unsigned char g_aucSTM32ID[12];
- //
- ///***************************************************************
- //*
- //***************************************************************/
- //static const u32 apulSTM32IDAddr1[3]={
- // 0x115a7ef8ul,0x1F00123ul,ID_BASE_ADDR
- //};
- //static const unsigned char apucSNTab1[12]={
- // 0x12,0x98,0x32,0x76,0xa1,0x8b,0xf1,0x13,0x5a,0x21,0x01,0x32
- //};
- ///*
- //index must be 1
- //*/
- //void GetSTM32ID1(int index)
- //{
- // int i=index;
- // int j=i+1;
- // unsigned char data;
- // unsigned char *p;
- // u32 addr=apulSTM32IDAddr1[j];
- // for(i=0;i<12;i++){
- // p= (unsigned char *)addr;
- // data=*p;
- // aucSTM32ID1[i]=data ^ apucSNTab1[i];
- // p++;
- // }
- //}
- //void CheckSTM32ID1(void)
- //{
- // int i;
- // //read from eeprom
- //// EEPROM_ReadS(EEPROM_ID_SN1,aucSTM32SN1,12);
- // //check
- // for(i=0;i<12;i++){
- // if(aucSTM32SN1[i]!=aucSTM32ID1[i])break;
- // }
- // if(i!=12){
- // //while(1);
- // for(i=0;i<1000;i++){
- // if(i==900)i=0;
- // }
- // }
- //}
- //
- //
- //
- //
- //void GetSTM32ID(unsigned char *pID);
- //void CreateKey(unsigned char *pKey, unsigned char *pID);
- //unsigned char SaveKey(unsigned char *pKey);
- /***********************************************************
- * 函数:GetSTM32ID
- * 参数:1、*pID 读取的ID存放指针,12字节ID
- * 功能:获取本芯片内部出厂绑定的ID
- ***********************************************************/
- void GetSTM32ID(unsigned char *pID)
- {
- unsigned char i = 0;
- u32 addr = ID_BASE_ADDR;
- for(i = 0; i < 12; i++)
- {
- *pID = *(u32 *)addr;
- pID++;
- addr++;
- }
- }
- /************************************************************
- *
- *************************************************************/
- unsigned char GetRandBySTM32ID(void)
- {
- unsigned char id[12];
- unsigned char rand=0xab;//种子
- int i;
- GetSTM32ID(id);
- for(i=0;i<12;i++){
- rand^=id[i];
- }
- return rand;
- }
- /************************************************************
- *获取ASN
- *************************************************************/
- unsigned long GetASNBySTM32ID(void)
- {
- SUTDL dl;
- unsigned char id[12];
- unsigned char rand=0xab;//种子
- int i;
- GetSTM32ID(id);
- for(i=0;i<12;i++){
- rand^=id[i];
- }
- dl.Data.ucData.b1=id[8]^rand;
- dl.Data.ucData.b3=id[9]^rand;
- dl.Data.ucData.b4=id[10]^rand;
- dl.Data.ucData.b2=id[11]^rand;
- return dl.Data.ulData;
- }
- unsigned char fun1(unsigned char D,unsigned char r)
- {
- static const unsigned char randtable[256]={
- 101,43,147,83,94,201,168,16,140,192,97,190,103,20,67,117,
- 28,17,188,21,210,29,152,223,74,226,145,89,227,87,151,219,
- 102,213,27,77,114,193,57,194,72,165,208,84,124,40,118,71,
- 238,189,179,111,157,113,242,60,200,63,128,164,55,34,86,148,
- 62,9,4,2,10,81,235,150,120,180,96,22,36,135,166,217,
- 158,46,205,35,187,161,3,6,12,8,184,248,139,211,167,31,
- 149,69,195,249,13,225,61,32,23,18,26,30,127,121,116,112,
- 108,104,58,100,98,49,209,224,33,48,39,106,110,109,107,99,
- 68,138,131,134,191,185,214,14,215,243,246,252,51,136,64,247,
- 196,218,105,44,47,41,182,142,183,143,59,153,54,216,160,7,
- 137,186,132,130,212,178,38,24,220,232,239,233,133,221,141,37,
- 181,45,42,251,241,1,129,144,177,11,253,5,88,255,245,15,
- 197,125,203,19,198,204,75,207,70,76,79,202,206,73,66,199,
- 93,91,115,52,50,254,92,88,240,231,176,122,126,78,80,119,
- 85,173,171,123,230,237,65,95,229,250,82,90,234,146,154,244,
- 53,155,156,163,236,172,159,175,169,56,162,170,174,228,222,25
- };
- switch(r){
- case 0:
- D=randtable[D];
- case 1:
- D=randtable[D];
- case 2:
- D=randtable[D];
- case 3:
- D=randtable[D];
- break;
- }
- return D;
- }
- /************************************************************
- *根据PSN获取AKEY
- 注意,不同项目可以设置不同的keys1/keys2
- *************************************************************/
- const static char keys1[]= {18,4,12,9,22,-36,19,81,-20,-18,85,61,-36,52,-50,28};
- const static char keys2[]= {'R','a','t','e','l','a','n','-','Y','H','o','n','g','2','3','1'};
- unsigned long getAKeyByPSN(unsigned long PSN)
- {
- unsigned long bb[4];
- bb[0] = (unsigned long) (0xff & PSN);
- bb[1] = (unsigned long) ((0xff00 & PSN) >> 8);
- bb[2] = (unsigned long) ((0xff0000 & PSN) >> 16);
- bb[3] = (unsigned long) ((0xff000000 & PSN) >> 24);
-
- unsigned long p1=bb[0] ^ bb[1] ^ bb[2] ^ bb[3];
- unsigned long p11= 0x0f & p1;
- unsigned long p12= (0xf0 & p1)>>4;
-
- unsigned long p2=bb[0] + bb[1] + bb[2] + bb[3];
- unsigned long p21= 0x0f & p2;
- unsigned long p22= (0xf0 & p2)>>4;
-
- bb[0]=bb[0] ^ keys1[p11];
- bb[1]=bb[0] ^ keys1[p21];
- bb[2]=bb[1] ^ keys2[p22];
- bb[3]=bb[2] ^ keys2[p12];
- unsigned long Pass=(bb[3]&0xff)<<24;
- Pass+=(bb[2]&0xff)<<16;
- Pass+=(bb[1]&0xff)<<8;
- Pass+=(bb[0]&0xff);
- if(Pass<85613652)Pass+=85613652;
- return Pass;
- }
- //void testAKeyByPSN(void)
- //{
- // char buf[50];
- // unsigned long i,num,psn;
- // psn=218010001;
- // num=10;
- // SlwTrace(DEBUG,"--testAKeyByPSN begin--\r\n");
- // for(i=0;i<num;i++){
- // sprintf(buf,"PSN=%lu;AKEY=%lu;\r\n",psn,getAKeyByPSN(psn));
- // SlwTrace(DEBUG,buf);
- // psn++;
- // }
- // SlwTrace(DEBUG,"--testAKeyByPSN end--\r\n");
- //}
- /*****************************
- 根据MCU ID 生成MAC
- *******************************/
- void MakeNetMac(unsigned char *MAC)
- {
- unsigned char id[12];
- GetSTM32ID(id);
- MAC[0]=id[0]^id[11];
- MAC[1]=id[1]^id[10];
- MAC[2]=id[2]^id[9];
- MAC[3]=id[3]^id[8];
- MAC[4]=id[4]^id[7];
- MAC[5]=id[5]^id[6];
- }
- /*********************************************************************/
|