id.c 5.7 KB

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