CM_TEST.c 16 KB

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