CM_TEST.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635
  1. #include <stdio.h>
  2. #define CM_LIB_GLOBALES
  3. #define EXTERNMACRO 2
  4. #include "CM_LIB.h"
  5. #include "CM_I2C.h"
  6. #include "CM_I2C_L.h"
  7. #include <string.h>
  8. uint8_t ucData[16], ucCi[8], ucSk[8];
  9. // Zone Data
  10. uint8_t ucCM_UserZone;
  11. uint8_t ucCM_AntiTearing;
  12. // Chip state
  13. uint8_t ucCM_Encrypt;
  14. uint8_t ucCM_Authenticate;
  15. // Global data
  16. uint8_t ucCM_InsBuff[4];
  17. uint8_t authenticationModeFlag = 0;
  18. uint8_t passwordModeFlag = 0;
  19. uint8_t encryptModeFlag = 0;
  20. uint8_t errorGlobalFlag = 0;
  21. //default : four user zone use Secret Seed G1 and password write/read 1(all use set 1)
  22. //Secret Seed : it's used by cm_ActiveSecurity
  23. uint8_t ucG[8] = {0x11,/*0x22,*/0x23,0x34,0x45,0x56,0x67,0x78,0x89};
  24. //passworkd for write : it's used by cm_VerifyPassword
  25. uint8_t psword_w[3] = {0x22,0x34,0x56};
  26. //passworkd for read : it's used by cm_VerifyPassword
  27. uint8_t psword_r[3] = {0x88,0x9a,0xbc};
  28. uint8_t cm_Auth_Encrp_Personal(uint8_t pswMode,uint8_t authMode)
  29. {
  30. uint8_t ucReturn;
  31. uint8_t pwData[8] = {0};
  32. uint8_t seedData[8] = {0};
  33. uint8_t fab_id = 0x06;
  34. uint8_t cma_id = 0x04;
  35. uint8_t per_id = 0x00;
  36. uint8_t fuseData;
  37. int i;
  38. uint8_t configAll[240] = {0};
  39. uint8_t pwwr7[3] = {0xDD, 0x42, 0x97};
  40. ucReturn = cm_VerifySecureCode(pwwr7);
  41. if (ucReturn != SUCCESS)
  42. {
  43. printf("cm_VerifySecureCode 1 is error\n");
  44. errorGlobalFlag = 15;
  45. return -1;
  46. }
  47. // ----------------------------------------------write card mfg code 0x50 30 30 31
  48. uint8_t cmc[4] = {0x50,0x30,0x30,0x31};
  49. ucReturn = cm_WriteConfigZone(0x0C, cmc, 4, FALSE);
  50. if (ucReturn != SUCCESS)
  51. {
  52. printf("cm_WriteConfigZone 0x26 is error\n");
  53. errorGlobalFlag = 2;
  54. return -1;
  55. }
  56. // ----------------------------------------------Write Identification Number = 00000000012345
  57. uint8_t idnum[7] = {0x00,0x00,0x00,0x00,0x01,0x23,0x45};
  58. ucReturn = cm_WriteConfigZone(0x19, idnum, 7, FALSE);
  59. if (ucReturn != SUCCESS)
  60. {
  61. printf("cm_WriteConfigZone 0x26 is error\n");
  62. errorGlobalFlag = 3;
  63. return -1;
  64. }
  65. // ----------------------------------------------Write Issuer Code = STATION 035
  66. uint8_t issue[16] = {0x53,0x54,0x41,0x54,0x49,0x4F,0x4E,0x20,0x30,0x33,0x35,0x00,0x00,0x00,0x00,0x00};
  67. ucReturn = cm_WriteConfigZone(0x40, issue, 16, FALSE);
  68. if (ucReturn != SUCCESS)
  69. {
  70. printf("cm_WriteConfigZone 0x26 is error\n");
  71. errorGlobalFlag = 4;
  72. return -1;
  73. }
  74. //---------------------------------------------config all user zone use the same password/seed of the set 1
  75. // ---------------------------------------------Write Control for user zone 0
  76. ucData[0] = ((pswMode & 0x03) << 6) | ((authMode & 0x3) << 4) | 0x07; // AR[0-3] = 0101 0111 ---->
  77. //01 ----> after Authentication successful ,then write correct password
  78. //01 ----> the read and write need to Authentication
  79. //1 ----> the host access(read/write) user zone use encryption mode(set 0 lead to unlock config zone error,i dont know why?)
  80. //111----> the default data
  81. ucData[1] = 0x51;//Authentication set 1, POM set 1, Key 1 //PR[0-3] = 0101 0001
  82. //01 ----> the secret seed select set 1(secret seed zone)
  83. //01 ----> the POK select set 1(secret seed zone)
  84. //0001 ----> the password select set 1(password zone)
  85. ucReturn = cm_WriteConfigZone(0x20, ucData, 2, FALSE);
  86. if (ucReturn != SUCCESS)
  87. {
  88. printf("cm_WriteConfigZone 0x20 is error\n");
  89. errorGlobalFlag = 5;
  90. return -1;
  91. }
  92. // ---------------------------------------------Write Control for user zone 1
  93. ucReturn = cm_WriteConfigZone(0x22, ucData, 2, FALSE);
  94. if (ucReturn != SUCCESS)
  95. {
  96. printf("cm_WriteConfigZone 0x22 is error\n");
  97. errorGlobalFlag = 6;
  98. return -1;
  99. }
  100. // ---------------------------------------------Write Control for user zone 2
  101. ucReturn = cm_WriteConfigZone(0x24, ucData, 2, FALSE);
  102. if (ucReturn != SUCCESS)
  103. {
  104. printf("cm_WriteConfigZone 0x24 is error\n");
  105. errorGlobalFlag = 7;
  106. return -1;
  107. }
  108. // ---------------------------------------------Write Control for user zone 3
  109. ucReturn = cm_WriteConfigZone(0x26, ucData, 2, FALSE);
  110. if (ucReturn != SUCCESS)
  111. {
  112. printf("cm_WriteConfigZone 0x26 is error\n");
  113. errorGlobalFlag = 8;
  114. return -1;
  115. }
  116. //----------------------------------------------Set DCR to allow unlimited checksum reads
  117. ucData[0] = 0xff;//after finish personal chip , must recover to the default
  118. // ucData[0] = 0xbf;//set unlimited checksum reads, Unlimited Authentication Trials,Eight Trials Allowed during debug for avoid the chip lock dead (why config 0x8f is error)
  119. ucReturn = cm_WriteConfigZone(0x18, ucData, 1, FALSE);
  120. if (ucReturn != SUCCESS)
  121. {
  122. printf("cm_WriteConfigZone 0x18 is error\n");
  123. errorGlobalFlag = 9;
  124. return -1;
  125. }
  126. //---------------------------------------------- Write Ci1
  127. ucCi[0] = 0xFF;
  128. ucCi[1] = 0x01;
  129. ucCi[2] = 0x02;
  130. ucCi[3] = 0x03;
  131. ucCi[4] = 0x04;
  132. ucCi[5] = 0x05;
  133. ucCi[6] = 0x06;
  134. ucCi[7] = 0x07;
  135. ucReturn = cm_WriteConfigZone(0x60, ucCi, 8, FALSE);
  136. if (ucReturn != SUCCESS)
  137. {
  138. printf("cm_WriteConfigZone 0x60 is error\n");
  139. errorGlobalFlag = 10;
  140. return -1;
  141. }
  142. // ------------------------------------------------Write G1
  143. // user must set and remember this secret for Secret Seed,then must be same whih cm_Auth_Encrp_rw before access to user zone for write or read .
  144. seedData[0] = ucG[0];
  145. seedData[1] = ucG[1];
  146. seedData[2] = ucG[2];
  147. seedData[3] = ucG[3];
  148. seedData[4] = ucG[4];
  149. seedData[5] = ucG[5];
  150. seedData[6] = ucG[6];
  151. seedData[7] = ucG[7];
  152. ucReturn = cm_WriteConfigZone(0x98, seedData, 8, FALSE);
  153. if (ucReturn != SUCCESS)
  154. {
  155. printf("cm_WriteConfigZone 0x98 is error\n");
  156. errorGlobalFlag = 11;
  157. return -1;
  158. }
  159. //--------------------------------------------- Write PSW1 (both Write and Read)
  160. // user must set and remember this secret for password for write/read.then must be same whih cm_Auth_Encrp_rw before access to user zone for write or read .
  161. pwData[0] = 0xFF;
  162. pwData[1] = psword_w[0];
  163. pwData[2] = psword_w[1];
  164. pwData[3] = psword_w[2];
  165. pwData[4] = 0xFF;
  166. pwData[5] = psword_r[0];
  167. pwData[6] = psword_r[1];
  168. pwData[7] = psword_r[2];
  169. ucReturn = cm_WriteConfigZone(0xB8, pwData, 8, FALSE);
  170. if (ucReturn != SUCCESS)
  171. {
  172. printf("cm_WriteConfigZone 0xB8 is error\n");
  173. errorGlobalFlag = 12;
  174. return -1;
  175. }
  176. #if 1
  177. ucReturn = cm_BurnFuse(fab_id);
  178. if (ucReturn != SUCCESS)
  179. {
  180. printf("cm_BurnFuse fab_id is error\n");
  181. errorGlobalFlag = 12;
  182. }
  183. ucReturn = cm_BurnFuse(cma_id);
  184. if (ucReturn != SUCCESS)
  185. {
  186. printf("cm_BurnFuse cma_id is error\n");
  187. errorGlobalFlag = 12;
  188. }
  189. ucReturn = cm_BurnFuse(per_id);
  190. if (ucReturn != SUCCESS)
  191. {
  192. printf("cm_BurnFuse per_id is error\n");
  193. errorGlobalFlag = 12;
  194. }
  195. #endif
  196. //read fuse
  197. cm_ReadFuse(&fuseData);
  198. if (ucReturn != SUCCESS)
  199. {
  200. printf("cm_ReadFuse is error\n");
  201. errorGlobalFlag = 12;
  202. }
  203. if(fuseData == 0x00)
  204. {
  205. printf("FAB,CMA,PER------three fuse are burnned successful\n");
  206. }
  207. /************ Read all configs *******************/
  208. memset(configAll, 0, 240);
  209. ucReturn = cm_ReadConfigZone(0x00,configAll,240);
  210. if (ucReturn != SUCCESS)
  211. {
  212. printf("cm_ReadConfigZone all is error\n");
  213. errorGlobalFlag = 13;
  214. }
  215. else
  216. {
  217. printf("======== Configure Memory =========");
  218. for(i=0;i<240;i++)
  219. {
  220. if(i%8 == 0) printf("\n%04x: ", i);
  221. printf("%02X ", configAll[i]);
  222. }
  223. printf("\n");
  224. }
  225. if(errorGlobalFlag != 0)
  226. return FAILED;
  227. return SUCCESS;
  228. }
  229. /*
  230. *FUNC: To init the chip of AT88SCxxx
  231. *
  232. *PARAM:
  233. * authMode :
  234. * 3 : close Authentication mode;
  235. * 2 : write Authentication mode;
  236. * 1 : normal Authentication mode;
  237. * 0 : dual access mode;
  238. *
  239. * pswMode :
  240. * 3 : close password mode;
  241. * 2 : write password mode;
  242. * 0/1 : read and write password mode;
  243. *
  244. * encryptFlag :
  245. * TRUE : enable encrypt mode
  246. * FALSE : disable encrypt mode
  247. *
  248. */
  249. uint8_t cm_Auth_Encrp_Init(uint8_t authMode,uint8_t pswMode,uint8_t encryptFlag)
  250. {
  251. uint8_t ucReturn;
  252. uint8_t fuseData;
  253. int i;
  254. uint8_t ucData[4] = {0};
  255. uint8_t configAll[240] = {0};
  256. //init I2C pin
  257. GPIO_InitTypeDef GPIO_InitStruct;
  258. GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_5;
  259. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  260. GPIO_InitStruct.Pull = GPIO_PULLUP;
  261. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  262. stm32_gpio_init(GPIOE, &GPIO_InitStruct);
  263. if(pswMode == 0x0 || pswMode == 0x1)
  264. passwordModeFlag = 1;
  265. else if(pswMode == 0x2)
  266. passwordModeFlag = 2;
  267. else
  268. passwordModeFlag = 0;
  269. if(authMode == 0)
  270. authenticationModeFlag = 0;
  271. else if(authMode == 1)
  272. authenticationModeFlag = 1;
  273. else if(authMode == 2)
  274. authenticationModeFlag = 2;
  275. else if(authMode == 3)
  276. authenticationModeFlag = 3;
  277. if(encryptFlag == TRUE)
  278. encryptModeFlag = 1;
  279. else
  280. encryptModeFlag = 0;
  281. cm_PowerOn();
  282. /**************** Check AT88SC0104C Present******************/
  283. ucData[0] = 0x5A;
  284. ucData[1] = 0xA5;
  285. ucReturn = cm_WriteConfigZone(0x0A, ucData, 2, FALSE);
  286. if (ucReturn != SUCCESS)
  287. {
  288. printf("cm_WriteConfigZone 0x0a is error\n");
  289. errorGlobalFlag = 1;
  290. }
  291. usleep(1);
  292. // Read back data
  293. ucData[0] = 0x00;
  294. ucData[1] = 0x00;
  295. ucReturn = cm_ReadConfigZone(0x0A, ucData, 2);
  296. if (ucReturn != SUCCESS)
  297. {
  298. printf("cm_ReadConfigZone 0x0a is error\n");
  299. errorGlobalFlag = 1;
  300. }
  301. if ((ucData[0]!= 0x5A) || (ucData[1]!= 0xA5) )
  302. {
  303. printf("read back data is not same\n");
  304. errorGlobalFlag = 1;
  305. }
  306. if(errorGlobalFlag != 0)
  307. {
  308. printf("there is a error and the flag = %d,the poweroff\n",errorGlobalFlag);
  309. cm_PowerOff();
  310. return FAILED;
  311. }
  312. return ucReturn;
  313. }
  314. /*
  315. *cm_WR:
  316. * 0 : read
  317. * 1 : write
  318. *userZone:
  319. * select need to access the index of user zone in range of 0-3
  320. *startInZone:
  321. * the start location in user zone for user operation of write/read,and the default data is 0
  322. *pData:
  323. * the data buffer for store the write/read data
  324. *dataLen:
  325. * the byte number of write/read
  326. *
  327. */
  328. uint8_t cm_Auth_Encrp_rw(uint8_t cm_WR,uint8_t userZone,uint8_t startInZone,uint8_t *pData,uint8_t dataLen)
  329. {
  330. uint8_t ucReturn;
  331. int i;
  332. uint8_t userZoneTemp = userZone & 0x03;
  333. uint8_t seedSet = 1;//which set of Secret Seed
  334. uint8_t pwset = 1;//which set of password for write/read
  335. //if(authenticationModeFlag == 0 || authenticationModeFlag == 1 || authenticationModeFlag == 2 || authenticationModeFlag == 3)//all to authenticate
  336. {
  337. if(encryptModeFlag == 1)
  338. {
  339. ucReturn = cm_ActiveSecurity(seedSet, ucG, NULL, TRUE);
  340. }
  341. else
  342. {
  343. ucReturn = cm_ActiveSecurity(seedSet, ucG, NULL, FALSE);
  344. }
  345. }
  346. if (ucReturn != SUCCESS)
  347. {
  348. printf("cm_ActiveSecurity is error\n");
  349. return FAILED;
  350. }
  351. ucReturn = cm_SetUserZone(userZoneTemp, FALSE);
  352. if (ucReturn != SUCCESS)
  353. {
  354. printf("cm_SetUserZone is error\n");
  355. return FAILED;
  356. }
  357. if(passwordModeFlag == 3)
  358. {
  359. if(cm_WR==0)
  360. {
  361. // Read back data
  362. for (i = 0; i < dataLen; ++i)
  363. pData[i] = 0x00; //clear pData
  364. ucReturn = cm_ReadSmallZone(startInZone, pData, dataLen);
  365. if (ucReturn != SUCCESS)
  366. {
  367. printf("cm_ReadSmallZone is error\n");
  368. return FAILED;
  369. }
  370. }
  371. else
  372. {
  373. // Write data to user zone
  374. ucReturn = cm_WriteSmallZone(startInZone, pData, dataLen);
  375. if (ucReturn != SUCCESS)
  376. {
  377. printf("cm_WriteSmallZone is error\n");
  378. return FAILED;
  379. }
  380. // Send checksum
  381. ucReturn = cm_SendChecksum(NULL);
  382. if (ucReturn != SUCCESS)
  383. {
  384. printf("cm_SendChecksum is error\n");
  385. return FAILED;
  386. }
  387. }
  388. }
  389. else //(passwordModeFlag == 1 || passwordModeFlag == 2)
  390. {
  391. ucCM_Authenticate = TRUE;
  392. if(cm_WR==0)
  393. {
  394. // Read back data, firstly verify the read password
  395. ucReturn=cm_VerifyPassword(psword_r,pwset,CM_PWREAD);
  396. if (ucReturn != SUCCESS)
  397. {
  398. printf("cm_VerifyPassword is error\n");
  399. return FAILED;
  400. }
  401. for (i = 0; i < dataLen; ++i)
  402. pData[i] = 0x00; //clear pData
  403. ucReturn = cm_ReadSmallZone(startInZone, pData, dataLen);
  404. if (ucReturn != SUCCESS)
  405. {
  406. printf("cm_ReadSmallZone is error\n");
  407. return FAILED;
  408. }
  409. ucReturn =cm_ReadChecksum(NULL);
  410. if (ucReturn != SUCCESS)
  411. {
  412. printf("cm_ReadChecksum is error\n");
  413. return FAILED;
  414. }
  415. }
  416. else
  417. {
  418. // Write data to user zone,firstly verify the write password
  419. ucReturn=cm_VerifyPassword(psword_w,pwset,CM_PWWRITE);
  420. if (ucReturn != SUCCESS)
  421. {
  422. printf("cm_VerifyPassword is error\n");
  423. return FAILED;
  424. }
  425. ucReturn = cm_WriteSmallZone(startInZone, pData, dataLen);
  426. if (ucReturn != SUCCESS)
  427. {
  428. printf("cm_WriteSmallZone is error\n");
  429. return FAILED;
  430. }
  431. // Send checksum
  432. ucReturn = cm_SendChecksum(NULL);
  433. if (ucReturn != SUCCESS)
  434. {
  435. printf("cm_SendChecksum is error\n");
  436. return FAILED;
  437. }
  438. }
  439. }
  440. ucReturn = cm_DeactiveSecurity();
  441. if (ucReturn != SUCCESS)
  442. {
  443. printf("cm_DeactiveSecurity is error\n");
  444. return FAILED;
  445. }
  446. return SUCCESS;
  447. }
  448. uint8_t cm_Auth_Encrp_Read(uint8_t userZone,uint8_t startInZone,uint8_t* readData,uint8_t readLen)
  449. {
  450. uint8_t returnData = 0;
  451. returnData = cm_Auth_Encrp_rw(0,userZone,startInZone,readData,readLen);
  452. if(returnData != SUCCESS)
  453. return FAILED;
  454. else
  455. return SUCCESS;
  456. }
  457. uint8_t cm_Auth_Encrp_Write(uint8_t userZone,uint8_t startInZone,uint8_t* writeData,uint8_t writeLen)
  458. {
  459. uint8_t returnData = 0;
  460. returnData = cm_Auth_Encrp_rw(1,userZone,startInZone,writeData,writeLen);
  461. if(returnData != SUCCESS)
  462. return FAILED;
  463. else
  464. return SUCCESS;
  465. }
  466. uint8_t test_cryptomem(void)
  467. {
  468. uint8_t readData[16] = {0};
  469. uint8_t readLen = 16;
  470. uint8_t writeData[16] = {0};
  471. uint8_t writeLen = 16;
  472. uint8_t i;
  473. uint8_t returnData;
  474. uint8_t userZone = 1;//user need to access the index of user zone
  475. uint8_t startInZone = 0;//the start location of x user zone for write/read
  476. // returnData = cm_Auth_Encrp_Init(1,1,TRUE);
  477. // if(returnData != SUCCESS)
  478. // {
  479. // printf("cm_Auth_Encrp_Init is error\n");
  480. // return FAILED;
  481. // }
  482. printf("Write: \n");
  483. for(i=0;i<writeLen;i++)
  484. {
  485. writeData[i] = '6' + i;
  486. }
  487. printf("\n");
  488. returnData = cm_Auth_Encrp_Write(userZone,startInZone,writeData,writeLen);
  489. if(returnData != SUCCESS)
  490. {
  491. printf("cm_Auth_Encrp_Read is error\n");
  492. return FAILED;
  493. }
  494. returnData = cm_Auth_Encrp_Read(userZone,startInZone,readData,readLen);
  495. if(returnData != SUCCESS)
  496. {
  497. printf("cm_Auth_Encrp_Read is error\n");
  498. return FAILED;
  499. }
  500. return SUCCESS;
  501. }