sc1161y.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  1. /*******************************************************************************
  2. 版权所有:
  3. 文件名称: sc1161y.c
  4. 文件版本: 01.01
  5. 创建作者: xxxxxx
  6. 创建日期: 2016-10-23
  7. 功能说明: sc1161y加密芯片驱动程序。
  8. 其它说明:
  9. 注意事项:
  10. 芯片上电期间, SSN 引脚应维持高电平.SSN=0:将 SSN 置低; SSN=1:将 SSN 置高。
  11. Len1 代表长度的高字节, Len2 代表长度的低字节。
  12. LRC1 的计算方法:对 CLA INS P1 P2 Len1 Len2 DATA 数据,每个字节的异或值,再取反。
  13. LRC2 的计算方法:对 SW1 SW2 Len1 Len2 DATA 数据,每个字节的异或值,再取反。
  14. Len1 Len2 代表 DATA 域的长度,不包括 LRC1 或 LRC2。
  15. 接口设备接收 BUSY 状态字的最长等待时间为 3s。
  16. 严格按照流程中对 SSN 信号的处理方式,禁止在数据传输中,将 SSN 置高。
  17. SCK 推荐速率 5 MHz。(智芯确认SC1161Y最高25M)
  18. 接口设备发送/接收数据完成后,建议在 10μ s 内将 SSN 置高。
  19. 发送数据、接收数据字节间延时设为 1μ s。
  20. 在市电供电的情况下,为了保证和主站会话通道的连续性,正常情况下不建议接口设备断电。
  21. 在 SSN 置高后,将进入低功耗状态,高电平保持时间至少 10μ s 以上,才能置低。
  22. 接口设备将 SSN 再次置低后,需等待 50μ s 以上时间,才能开始发送数据。
  23. 1) 发送数据结构
  24. 发送数据的结构为: 0x55 CLA INS P1 P2 Len1 Len2 DATA LRC1,其中:
  25. 0x55 为发送命令结构的命令头;
  26. CLA 是命令类别;
  27. INS 是命令类别中的指令代码;
  28. P1、 P2 是一个完成指令代码的参考符号;
  29. Len1 Len2 是后续 DATA 的长度, 不包含 LRC1, 由两字节表示;
  30. DATA 是处理的数据输入;
  31. LRC1 是发送数据的校验值,计算方法见 SPI 通信流程说明。
  32. 2) 接收数据结构
  33. 接收数据的结构为: SW1 SW2 Len1 Len2 DATA LRC2,其中:
  34. SW1 SW2 是指令执行完毕后,从设备返回的状态字;
  35. Len1 Len2 是后续 DATA 的长度, 不包含 LRC2, 由两字节表示;
  36. DATA 是处理数据完毕后,返回的输出数据;
  37. LRC2 是接收数据的校验值,计算方法见 SPI 通信流程说明;
  38. 3) 状态字节
  39. SW1、 SW2 构成结束序列,结束序列在命令的结尾指示芯片的状态。
  40. SW1 SW2=‘9000’表示正常结束。
  41. 注:接口设备在接收数据时, MOSI 引脚应始终保持低电平。
  42. 修改记录:
  43. */
  44. #include "head.h"
  45. u16 g_esam_errno;
  46. u8 _esam_lrc(u8* buf0, u16 len0,u8* buf1,u16 len1)
  47. {
  48. u8 lrc;
  49. u16 i;
  50. lrc = buf0[0];
  51. for(i=1; i<len0; i++)
  52. {
  53. lrc ^= buf0[i];
  54. }
  55. for(i=0; i<len1; i++)
  56. {
  57. lrc ^= buf1[i];
  58. }
  59. return ~lrc;
  60. }
  61. int _esam_cmd(u8* cmd,u16 cmd_len,u8 *in,u16 in_len,u8 *out,u16 out_len)
  62. {
  63. u8 lrc,c,buf[5];
  64. int i,ret,len=0;
  65. uint32_t us0;
  66. // 计算发送LRC
  67. lrc = _esam_lrc(cmd,cmd_len,in,in_len);
  68. // 片选有效
  69. dspi_esam_cs(IOCTL_SPI_CS_LOW);
  70. // 发送前导字节0X55
  71. dspi_esam_send(0x55);
  72. // 发送命令
  73. for(i=0;i<cmd_len;i++)
  74. {
  75. dspi_esam_send(cmd[i]);
  76. }
  77. // 发送数据
  78. for(i=0;i<in_len;i++)
  79. {
  80. dspi_esam_send(in[i]);
  81. }
  82. // 发送校验LRC
  83. dspi_esam_send(lrc);
  84. // 片选无效
  85. // ustimer_delay(11*USTIMER_US);
  86. dspi_esam_cs(IOCTL_SPI_CS_HIGH);
  87. // 片选再次有效
  88. dspi_esam_cs(IOCTL_SPI_CS_LOW);
  89. // 接收BUSY状态字,超时3S
  90. c = 0;
  91. i = 0;
  92. us0 = ustimer_get_origin();
  93. while(ustimer_get_duration(us0) < 3*USTIMER_SEC)
  94. {
  95. i++;
  96. c = dspi_esam_recv();
  97. if(c == 0x55)
  98. {
  99. break;
  100. }
  101. else if( c != 0)
  102. {
  103. rt_printf("c=0x%02x,i=%d.\r\n",c,i);
  104. }
  105. }
  106. //如果不成功,直接返回错误
  107. if(c != 0x55)
  108. {
  109. rt_printf("%s:接收BUSY状态字超时(c=0x%02x).\r\n",__FUNCTION__,c);
  110. ret = -11;
  111. goto LABEL_RET;
  112. }
  113. // 接收固定的4个字节,并检查状态字和长度
  114. for(i=0; i<4; i++)
  115. {
  116. buf[i] = dspi_esam_recv();
  117. }
  118. if((buf[0] != 0x90) || (buf[1] != 0x00))
  119. {
  120. buf[i] = dspi_esam_recv();
  121. ret = -12;
  122. goto LABEL_RET;
  123. }
  124. len = (buf[2]<<8) + buf[3];
  125. if(len > out_len)
  126. {
  127. rt_printf("%s:输出缓冲空间不足,缓冲长度=%d,数据长度=%d.\r\n",__FUNCTION__,out_len,len);
  128. len = 0;
  129. ret = -13;
  130. goto LABEL_RET;
  131. }
  132. // 根据长度接收数据
  133. for(i=0; i<len; i++)
  134. {
  135. out[i] = dspi_esam_recv();
  136. }
  137. // 接收并检查LRC2
  138. c = dspi_esam_recv();
  139. lrc = _esam_lrc(buf,4,out,len);
  140. if(c != lrc)
  141. {
  142. rt_printf("%s:接收=0x%02x,计算=0x%02x.\r\n",__FUNCTION__,c,lrc);
  143. ret = -14;
  144. goto LABEL_RET;
  145. }
  146. // 所有正常,返回输出BUF中的数据长度
  147. ret = len;
  148. LABEL_RET:
  149. // 片选无效
  150. dspi_esam_cs(IOCTL_SPI_CS_HIGH);
  151. if(g_print_sys || ret < 0)
  152. {
  153. print_mem("esam_cmd:",cmd,cmd_len);
  154. print_mem("esam_in :",in, in_len);
  155. print_mem("esam_out:",out,len);
  156. }
  157. if(ret >= 0)
  158. {
  159. g_esam_errno = 0x9000;
  160. }
  161. else if(ret == -12)
  162. {
  163. rt_printf("%s:返回状态字错误(0x%02x,0x%02x,0x%02x,0x%02x,0x%02x)c=%02x.\r\n",__FUNCTION__,buf[0],buf[1],buf[2],buf[3],buf[4],c);
  164. g_esam_errno = (buf[0]<<8) | buf[1];
  165. }
  166. else
  167. {
  168. g_esam_errno = ret;
  169. }
  170. return ret;
  171. return 0;
  172. }
  173. // 得到芯片序列号
  174. int esam_get_sn(u8* out, u16 out_len)
  175. {
  176. //发送:00b0990500020008
  177. //返回:9000+0008+ID
  178. u8 cmd[] = {0x00, 0xb0, 0x99, 0x05, 0x00, 0x02, 0x00, 0x08};
  179. // 检查参数
  180. if(out == NULL)
  181. {
  182. return -1;
  183. }
  184. // 调用ESAM命令
  185. return _esam_cmd(cmd,sizeof(cmd),0,0,out,out_len);
  186. }
  187. // 得到密钥版本
  188. int esam_get_version(u8* out, u16 out_len)
  189. {
  190. //发送:801A00000000
  191. //返回:9000+0001+KeyVersion //Keyversion: 1 字节
  192. u8 cmd[] = {0x80, 0x1a, 0x00, 0x00, 0x00, 0x00};
  193. // 检查参数
  194. if(out == NULL)
  195. {
  196. return -1;
  197. }
  198. // 调用ESAM命令
  199. return _esam_cmd(cmd,sizeof(cmd),0,0,out,out_len);
  200. }
  201. // 随机数产生
  202. int esam_get_random(u8* out, u16 out_len)
  203. {
  204. //发送:008400080000
  205. //返回:9000+0008+R3 //R3:终端随机数, 8 字节
  206. u8 cmd[] = {0x00, 0x84, 0x00, 0x08, 0x00, 0x00};//len高字节在前
  207. // 检查参数
  208. if(out == NULL)
  209. {
  210. return -1;
  211. }
  212. // 调用ESAM命令
  213. return _esam_cmd(cmd,sizeof(cmd),0,0,out,out_len);
  214. }
  215. // 随机数签名
  216. // 签名密钥标识: 固定为 01
  217. int esam_sign_random(u8 *in,u16 in_len, u8* out, u16 out_len)
  218. {
  219. //发送:801600800008+R1 //R1:网关或主站随机数, 8 字节
  220. //返回:9000+0048+R2+ Sign1 // R2:终端随机数, 8 字节,Sign1:签名结果, 64 字节
  221. u8 cmd[] = {0x80, 0x16, 0x00, 0x80, in_len>>8, in_len};
  222. // 检查参数
  223. if(out == NULL)
  224. {
  225. return -1;
  226. }
  227. // 调用ESAM命令
  228. return _esam_cmd(cmd,sizeof(cmd),in,in_len,out,out_len);
  229. }
  230. // 网关、主站签名验证
  231. // 网关签名密钥索引: 0x05
  232. // 主站的签名密钥索引取值范围: 0x01,0x02,0x03,0x04
  233. int esam_sign_verify_zz(u8 key,u8 *in,u16 in_len)
  234. {
  235. //发送:801800050040+ Sign2 //Sign2:签名结果, 64 字节
  236. //返回:9000+0000
  237. u8 cmd[] = {0x80, 0x18, 0x00, key, in_len>>8, in_len};
  238. // 检查参数
  239. if(in == NULL)
  240. {
  241. return -1;
  242. }
  243. // 调用ESAM命令
  244. return _esam_cmd(cmd,sizeof(cmd),in,in_len,0,0);
  245. }
  246. // 遥控签名验证
  247. int esam_sign_verify_yk(u8 key,u8 *in,u16 in_len)
  248. {
  249. //发送:800800+asKID+Lc+TcsData + Date+StcsData //asKID:签名密钥索引。Lc:发送数据长度, 2 字节
  250. //返回:9000+0000
  251. u8 cmd[] = {0x80, 0x08, 0x00, key, in_len>>8, in_len};
  252. // 检查参数
  253. if(in == NULL)
  254. {
  255. return -1;
  256. }
  257. // 调用ESAM命令
  258. return _esam_cmd(cmd,sizeof(cmd),in,in_len,0,0);
  259. }
  260. // 主站报文加密
  261. int esam_encrypt(u8 *in,u16 in_len,u8 *out,u16 out_len)
  262. {
  263. //发送:80266001+Lc+IVData+ Data
  264. // Lc:表示长度, 2 字节
  265. // IVData:初始向量,随机数 R1+对 R1 按位取反, 16 字节
  266. // Data: 数据域(应用类型、报文长度和报文内容)
  267. //返回:9000+Le+enData+MAC
  268. // Data:终端明文数据, enData:终端数据密文值, MAC: enData 的 MAC值
  269. u8 cmd[] = {0x80, 0x26, 0x60, 0x01, in_len>>8, in_len};//len高字节在前
  270. // 检查参数
  271. if(in == NULL)
  272. {
  273. return -1;
  274. }
  275. // 调用ESAM命令
  276. return _esam_cmd(cmd,sizeof(cmd),in,in_len,out,out_len);
  277. }
  278. // 主站报文解密
  279. int esam_decrypt(u8 *in,u16 in_len,u8 *out,u16 out_len)
  280. {
  281. //发送:802C6001+Lc+IVData+enData+MAC
  282. // Lc:表示长度, 2 字节
  283. // IVData:初始向量,随机数 R1+对 R1 按位取反, 16 字节
  284. //返回:9000+Le+ Data
  285. // Le:芯片返回数据长度,2 字节 Data:明文数据
  286. u8 cmd[] = {0x80, 0x2c, 0x60, 0x01, in_len>>8, in_len};//len高字节在前
  287. // 检查参数
  288. if(in == NULL)
  289. {
  290. return -1;
  291. }
  292. // 调用ESAM命令
  293. return _esam_cmd(cmd,sizeof(cmd),in,in_len,out,out_len);
  294. }
  295. // 更新对称密钥
  296. // 密钥版本大于 0 为更新:更新命令头: 801C00
  297. // 密钥版本等于 0 为恢复:恢复命令头: 801C01
  298. int esam_key_update(u8 key,u8 *in,u16 in_len)
  299. {
  300. //发送:密钥更新或恢复命令头+asKID+Lc+newKeyVersion+UpdateKeyData+Skeydata
  301. // newKeyVersion:密钥版本, 1字节
  302. // UpdateKeyData:密钥数据,5*36 字节
  303. // Skeydata:签名结果, 64 字节
  304. // asKID:签名密钥索引, 1 字节
  305. //返回:9000+0000
  306. u8 cmd[] = {0x80, 0x1c, 0x00, key, in_len>>8, in_len};//len高字节在前
  307. // 检查参数
  308. if(in == NULL)
  309. {
  310. return -1;
  311. }
  312. if(*in == 0)
  313. {
  314. cmd[2] = 0x01;
  315. }
  316. // 调用ESAM命令
  317. return _esam_cmd(cmd,sizeof(cmd),in,in_len,0,0);
  318. }
  319. // 终端主站证书、认证网关证书远程更新
  320. //写入 CA 根证书(cer:0)
  321. //写入 主站证书(cer:1~4)
  322. //写入 网关证书(cer:5)
  323. int esam_cer_zz_update( u8 cer,u8 *in,u16 in_len)
  324. {
  325. //发送:8022+cerID+00+Lc+Cer
  326. //返回:9000+0000
  327. u8 cmd[] = {0x80, 0x22, cer,0x00, in_len>>8, in_len};//len高字节在前
  328. // 检查参数
  329. if(in == NULL)
  330. {
  331. return -1;
  332. }
  333. // 调用ESAM命令
  334. return _esam_cmd(cmd,sizeof(cmd),in,in_len,0,0);
  335. }
  336. // 终端证书远程下装
  337. int esam_cer_zd_update(u8 *in,u16 in_len)
  338. {
  339. //发送:80240000+Lc+Cer
  340. //返回:9000+0000
  341. u8 cmd[] = {0x80, 0x24, 0x00,0x00, in_len>>8, in_len};//len高字节在前
  342. // 检查参数
  343. if(in == NULL)
  344. {
  345. return -1;
  346. }
  347. // 调用ESAM命令
  348. return _esam_cmd(cmd,sizeof(cmd),in,in_len,0,0);
  349. }
  350. // 读取终端正式证书
  351. int esam_cer_zd_get(u8 *out,u16 out_len)
  352. {
  353. //发送:803001000000
  354. //返回:9000+Le+Cer//Le:表示长度, 2 个字节
  355. u8 cmd[] = {0x80, 0x30, 0x01,0x00, 0x00, 0x00};//len高字节在前
  356. // 检查参数
  357. if(out == NULL)
  358. {
  359. return -1;
  360. }
  361. // 调用ESAM命令
  362. return _esam_cmd(cmd,sizeof(cmd),0,0,out,out_len);
  363. }
  364. // 读取终端测试证书长度
  365. int esam_cer_zd_test_get_len(u8 *out,u16 out_len)
  366. {
  367. //发送:00b0810000020002
  368. //返回:9000+ 0002+Len //Len:证书长度, 2 个字节
  369. u8 cmd[] = {0x00, 0xb0, 0x81, 0x00, 0x00, 0x02, 0x00,0x02};//len高字节在前
  370. // 检查参数
  371. if(out == NULL)
  372. {
  373. return -1;
  374. }
  375. // 调用ESAM命令
  376. return _esam_cmd(cmd,sizeof(cmd),0,0,out,out_len);
  377. }
  378. // 读取终端测试证书
  379. int esam_cer_zd_test_get(u16 cer_len,u8 *out,u16 out_len)
  380. {
  381. //发送:00b081020002+Len
  382. //返回:9000+Le+Cer//Le:表示长度, 2 个字节
  383. u8 cmd[] = {0x00, 0xb0, 0x81, 0x02, 0x00, 0x02, cer_len>>8, cer_len};//len高字节在前
  384. // 调用ESAM命令
  385. return _esam_cmd(cmd,sizeof(cmd),0,0,out,out_len);
  386. }
  387. // 以下为证书管理工具指令
  388. // 验证证书管理工具证书的有效性
  389. int esam_yw_cer_verify(u8 *in,u16 in_len)
  390. {
  391. //发送:801E0000+Lc+Cer,Lc:表示长度, 2 字节
  392. //返回:9000+0000
  393. u8 cmd[] = {0x80, 0x1e, 0x00,0x00, in_len>>8, in_len};//len高字节在前
  394. // 检查参数
  395. if(in == NULL)
  396. {
  397. return -1;
  398. }
  399. // 调用ESAM命令
  400. return _esam_cmd(cmd,sizeof(cmd),in,in_len,0,0);
  401. }
  402. // 对证书请求文件进行签名
  403. // 终端签名,运维工具验证正确性
  404. int esam_yw_sign(u8 *in,u16 in_len,u8 *out,u16 out_len)
  405. {
  406. //发送:800A0080+Lc+TotalReqData
  407. //返回:9000+0040+ SrReq
  408. // SrReq: TotalReqData 签名结果
  409. u8 cmd[] = {0x80, 0x0a, 0x00, 0x80, in_len>>8, in_len};//len高字节在前
  410. // 检查参数
  411. if(in == NULL)
  412. {
  413. return -1;
  414. }
  415. // 调用ESAM命令
  416. return _esam_cmd(cmd,sizeof(cmd),in,in_len,out,out_len);
  417. }
  418. // 验证签名的正确性
  419. // 运维工具签名,终端验证正确性
  420. int esam_yw_sign_verify(u8 *in,u16 in_len)
  421. {
  422. //发送:802000000040+Sr1
  423. //返回:9000+0000
  424. u8 cmd[] = {0x80, 0x20, 0x00,0x00, in_len>>8, in_len};//len高字节在前
  425. // 检查参数
  426. if(in == NULL)
  427. {
  428. return -1;
  429. }
  430. // 调用ESAM命令
  431. return _esam_cmd(cmd,sizeof(cmd),in,in_len,0,0);
  432. }
  433. // 获取终端安全芯片公钥
  434. int esam_yw_get_pubkey(u8 *out,u16 out_len)
  435. {
  436. //发送:803000000000
  437. //返回:9000+0040+pubKey
  438. u8 cmd[] = {0x80, 0x30, 0x00, 0x00, 0x00, 0x00};//len高字节在前
  439. // 检查参数
  440. if(out == NULL)
  441. {
  442. return -1;
  443. }
  444. // 调用ESAM命令
  445. return _esam_cmd(cmd,sizeof(cmd),0,0,out,out_len);
  446. }
  447. // 运维加密
  448. int esam_yw_encrypt(u8 *in,u16 in_len,u8 *out,u16 out_len)
  449. {
  450. //发送:80266202+Lc+ID+R1+IVData+ Data
  451. // Lc:表示长度, 2 字节ID:证书管理工具 ID,8 字节;
  452. // R1:随机数,身份认证时终端芯片产生的随机数, 8 字节
  453. // IVData:初始向量,随机数 R1+对 R1 按位取反, 16 字节
  454. // Data: 数据域(应用类型、报文长度和报文内容)
  455. //返回:9000+Le+enData+MAC
  456. // Data:终端明文数据, enData:终端数据密文值, MAC: enData 的 MAC值
  457. u8 cmd[] = {0x80, 0x26, 0x62, 0x02, in_len>>8, in_len};//len高字节在前
  458. // 检查参数
  459. if(in == NULL)
  460. {
  461. return -1;
  462. }
  463. // 调用ESAM命令
  464. return _esam_cmd(cmd,sizeof(cmd),in,in_len,out,out_len);
  465. }
  466. // 运维解密
  467. int esam_yw_decrypt(u8 *in,u16 in_len,u8 *out,u16 out_len)
  468. {
  469. //发送:802C6202+Lc+ID+R1+IVData+enData+MAC
  470. // Lc:表示长度, 2 字节ID:证书管理工具 ID,8 字节;
  471. // R1:随机数,身份认证时终端芯片产生的随机数, 8 字节
  472. // IVData:初始向量,随机数 R1+对 R1 按位取反, 16 字节
  473. //返回:9000+Le+ Data
  474. // Le:芯片返回数据长度,2 字节 Data:明文数据
  475. u8 cmd[] = {0x80, 0x2c, 0x62, 0x02, in_len>>8, in_len};//len高字节在前
  476. // 检查参数
  477. if(in == NULL)
  478. {
  479. return -1;
  480. }
  481. // 调用ESAM命令
  482. return _esam_cmd(cmd,sizeof(cmd),in,in_len,out,out_len);
  483. }
  484. // 恢复终端对称密钥
  485. int esam_yw_key_restore(u8 *in,u16 in_len)
  486. {
  487. //发送:801C0200+Lc+KeyData,Lc: 2 字节, KeyData 长度,KeyData:密钥恢复数据包
  488. //返回:9000+0000
  489. u8 cmd[] = {0x80, 0x1c, 0x02, 0x00, in_len>>8, in_len};//len高字节在前
  490. // 检查参数
  491. if(in == NULL)
  492. {
  493. return -1;
  494. }
  495. // 调用ESAM命令
  496. return _esam_cmd(cmd,sizeof(cmd),in,in_len,0,0);
  497. }
  498. int esam_init(void)
  499. {
  500. int ret;
  501. u8 out;
  502. // 上电并延时1ms【放置到DSPI初始化之前】
  503. // GPIO_ESAM_POWER_INIT();
  504. // ustimer_delay(USTIMER_MS*10);
  505. #ifndef ENCRYPT_CHNL_EN
  506. if(pRunSet->bTT_ESAM == 0)
  507. #else
  508. if((pRunSet->bTT_ESAM_COM == 0)&&(pRunSet->bTT_ESAM_NET == 0))
  509. #endif
  510. {
  511. return 0;
  512. }
  513. // 版本
  514. ret = esam_get_version(&out,1);
  515. if(ret < 0)
  516. {
  517. rt_printf("加密芯片检查失败!");
  518. rt_err_set(ERR_CODE_ESAM,0);
  519. return ret;
  520. }
  521. rt_printf("加密芯片检查通过!");
  522. return 0;
  523. }
  524. int esam_exit(void)
  525. {
  526. #if defined(CPU_MCF54418) // sunxi 20220628
  527. // 关闭电源,确保重启时有上电过程
  528. GPIO_ESAM_POWER_HIGH();
  529. #endif // sunxi 20220628
  530. return 0;
  531. }
  532. u8 g_test_buf[1024];
  533. u8 g_decrypt_data[] =
  534. {
  535. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  536. 0x71,0x41,0xa3,0x9e,0x0e,0x58,0x54,0xe2,0x7c,0xce,0x04,0xc1,0x88,0xc8,0xc1,0x3c,
  537. 0x2c,0x09,0x74,0x26,0x5b,0x8c,0x46,0x7d,0xa8,0x48,0x8e,0xf7,0x46,0x2a,0x45,0x98,
  538. 0x7a,0xbe,0x8d,0x89,0xf2,0xf7,0x6f,0xcb,0x4c,0x86,0x5a,0x07,0xbb,0xe9,0x7c,0x67,
  539. 0x67,0x90,0x17,0x81,0x47,0x7c,0x2d,0x88,0x0a,0xf6,0x60,0x2a,0x4d,0xeb,0x8f,0x80,
  540. 0xda,0xb8,0x3d,0xec,0x52,0x1a,0xf0,0x75,0xc3,0xd7,0xc4,0x18,0x2e,0xcd,0x16,0x47,
  541. 0x53,0xca,0x3c,0x55,0xe5,0xba,0x6d,0x0c,0x88,0x57,0xde,0x06,0x66,0x04,0xf6,0x6d,
  542. 0xbe,0x7a,0xed,0x27
  543. };
  544. //esam_cmd:,0x80,0x16,0x00,0x80,0x00,0x08
  545. u8 g_cmd00[] =
  546. {
  547. 0x52,0xe6,0x77,0xfc,0xaf,0x12,0x4b,0x91
  548. };
  549. //esam_cmd:,0x80,0x18,0x00,0x05,0x00,0x40
  550. u8 g_cmd01[] =
  551. {
  552. 0x31,0xfd,0x52,0xf0,0x96,0x08,0xed,0xaf,0x3f,0x5b,0xa9,0x81,0x33,0xdc,0x0b,0xb4,
  553. 0x1e,0xd1,0x7e,0x71,0xc0,0x5f,0xb1,0x00,0xfe,0xaf,0x0e,0xdf,0xc7,0xf3,0x85,0xb5,
  554. 0x9a,0xb5,0xd4,0x47,0xe7,0xf0,0x1b,0x90,0xc1,0x0d,0x25,0x9a,0x90,0x4f,0xc5,0xe1,
  555. 0xb8,0x82,0x17,0x50,0x5a,0xc5,0x2f,0xa1,0xde,0x2b,0x79,0x0b,0x0c,0x29,0xa2,0x86,
  556. };
  557. //esam_cmd:,0x80,0x16,0x00,0x80,0x00,0x08
  558. u8 g_cmd02[] =
  559. {
  560. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7
  561. };
  562. //esam_cmd:,0x80,0x18,0x00,0x01,0x00,0x40
  563. u8 g_cmd03[] =
  564. {
  565. 0x99,0x00,0x4d,0x4e,0x76,0xb4,0x2f,0x16,0xb9,0x80,0xc6,0x8f,0xc6,0xf9,0x1c,0xd0,
  566. 0x04,0x38,0x30,0xd6,0x3b,0xb5,0x5a,0xef,0x21,0xa5,0x15,0xc2,0x36,0xe0,0xd9,0xaa,
  567. 0x31,0x26,0x11,0x8e,0xa1,0x8c,0x3e,0x66,0x2a,0xd2,0x9e,0xc9,0x0a,0x8f,0x5d,0xab,
  568. 0x9a,0xaf,0x43,0x62,0xcb,0x12,0x3f,0xb9,0xe4,0xc3,0xab,0x3d,0x2d,0x54,0x4f,0xa7,
  569. };
  570. //esam_cmd:,0x00,0xb0,0x99,0x05,0x00,0x02,0x00,0x08
  571. u8 g_cmd04[] =
  572. {
  573. };
  574. //esam_cmd:,0x80,0x1a,0x00,0x00,0x00,0x00
  575. u8 g_cmd05[] =
  576. {
  577. };
  578. //esam_cmd:,0x00,0x84,0x00,0x08,0x00,0x00
  579. u8 g_cmd06[] =
  580. {
  581. };
  582. //esam_cmd:,0x80,0x26,0x60,0x01,0x00,0x24
  583. u8 g_cmd07[] =
  584. {
  585. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  586. 0x00,0x10,0x68,0x0e,0x00,0x00,0x00,0x00,0x46,0x01,0x04,0x00,0x01,0x00,0x00,0x00,
  587. 0x00,0x02,0x00,0x00
  588. };
  589. //esam_cmd:,0x80,0x2c,0x60,0x01,0x00,0x34
  590. u8 g_cmd08[] =
  591. {
  592. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  593. 0xbb,0x0c,0xdb,0x0b,0x22,0x3a,0x4a,0x1e,0xc6,0xaf,0x92,0x40,0x40,0x41,0x76,0x82,
  594. 0x8c,0xa3,0xc5,0x6a,0x77,0xa0,0xb1,0xae,0x2b,0xeb,0xb1,0x78,0xca,0xbb,0x30,0x11,
  595. 0x90,0x74,0x5c,0x1f
  596. };
  597. //esam_cmd:,0x80,0x26,0x60,0x01,0x00,0x24
  598. u8 g_cmd09[] =
  599. {
  600. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  601. 0x00,0x10,0x68,0x0e,0x02,0x00,0x02,0x00,0x64,0x01,0x07,0x00,0x01,0x00,0x00,0x00,
  602. 0x00,0x14,0x00,0x00
  603. };
  604. //esam_cmd:,0x80,0x26,0x60,0x01,0x00,0x8c
  605. u8 g_cmd10[] =
  606. {
  607. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  608. 0x00,0x78,0x68,0x76,0x04,0x00,0x02,0x00,0x09,0xa3,0x14,0x00,0x01,0x00,0x01,0x40,
  609. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  610. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0a,
  611. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  612. 0x00,0x00,0x00,0x00,0x09,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  613. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
  614. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x09,0x00,
  615. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  616. };
  617. //esam_cmd:,0x80,0x26,0x60,0x01,0x00,0x41
  618. u8 g_cmd11[] =
  619. {
  620. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  621. 0x00,0x2d,0x68,0x2b,0x06,0x00,0x02,0x00,0x01,0x9e,0x14,0x00,0x01,0x00,0x01,0x00,
  622. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  623. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,
  624. 0x00
  625. };
  626. //esam_cmd:,0x80,0x26,0x60,0x01,0x00,0x24
  627. u8 g_cmd12[] =
  628. {
  629. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  630. 0x00,0x10,0x68,0x0e,0x08,0x00,0x02,0x00,0x64,0x01,0x0a,0x00,0x01,0x00,0x00,0x00,
  631. 0x00,0x14,0x00,0x00
  632. };
  633. //esam_cmd:,0x80,0x2c,0x60,0x01,0x00,0x34
  634. u8 g_cmd13[] =
  635. {
  636. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  637. 0x17,0xdc,0xd8,0x09,0xa0,0x2a,0x72,0xad,0xfc,0x69,0xfd,0xc1,0xe6,0x35,0x7b,0x74,
  638. 0xc8,0x72,0xc6,0xca,0x18,0x06,0x30,0xf7,0x1f,0x52,0xe3,0xc2,0xb4,0x8a,0xb0,0xb8,
  639. 0x93,0x54,0x8d,0xd0
  640. };
  641. //esam_cmd:,0x80,0x26,0x60,0x01,0x00,0x2a
  642. u8 g_cmd14[] =
  643. {
  644. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  645. 0x00,0x16,0x68,0x14,0x0a,0x00,0x04,0x00,0x67,0x01,0x07,0x00,0x01,0x00,0x00,0x00,
  646. 0x00,0xee,0xc9,0x0c,0x11,0x15,0x05,0x11,0x00,0x00
  647. };
  648. //esam_cmd:,0x80,0x2c,0x60,0x01,0x00,0x74
  649. u8 g_cmd15[] =
  650. {
  651. 0x64,0x50,0x82,0x18,0xc7,0xf4,0x3d,0xa7,0x9b,0xaf,0x7d,0xe7,0x38,0x0b,0xc2,0x58,
  652. 0x71,0x41,0xa3,0x9e,0x0e,0x58,0x54,0xe2,0x7c,0xce,0x04,0xc1,0x88,0xc8,0xc1,0x3c,
  653. 0x2c,0x09,0x74,0x26,0x5b,0x8c,0x46,0x7d,0xa8,0x48,0x8e,0xf7,0x46,0x2a,0x45,0x98,
  654. 0x7a,0xbe,0x8d,0x89,0xf2,0xf7,0x6f,0xcb,0x4c,0x86,0x5a,0x07,0xbb,0xe9,0x7c,0x67,
  655. 0x67,0x90,0x17,0x81,0x47,0x7c,0x2d,0x88,0x0a,0xf6,0x60,0x2a,0x4d,0xeb,0x8f,0x80,
  656. 0xda,0xb8,0x3d,0xec,0x52,0x1a,0xf0,0x75,0xc3,0xd7,0xc4,0x18,0x2e,0xcd,0x16,0x47,
  657. 0x53,0xca,0x3c,0x55,0xe5,0xba,0x6d,0x0c,0x88,0x57,0xde,0x06,0x66,0x04,0xf6,0x6d,
  658. 0xbe,0x7a,0xed,0x27
  659. };
  660. int esam_test(void)
  661. {
  662. int ret;
  663. #if 0
  664. // 加密
  665. ret = esam_encrypt(g_cmd07,sizeof(g_cmd07),g_test_buf,1024);
  666. rt_printf(" 加密[%d].\r\n\r\n",ret);
  667. // 加密
  668. ret = esam_decrypt(g_cmd08,sizeof(g_cmd08),g_test_buf,1024);
  669. rt_printf(" 加密[%d].\r\n\r\n",ret);
  670. // 加密
  671. ret = esam_encrypt(g_cmd09,sizeof(g_cmd09),g_test_buf,1024);
  672. rt_printf(" 加密[%d].\r\n\r\n",ret);
  673. // 加密
  674. ret = esam_encrypt(g_cmd10,sizeof(g_cmd10),g_test_buf,1024);
  675. rt_printf(" 加密[%d].\r\n\r\n",ret);
  676. // 加密
  677. ret = esam_encrypt(g_cmd11,sizeof(g_cmd11),g_test_buf,1024);
  678. rt_printf(" 加密[%d].\r\n\r\n",ret);
  679. // 加密
  680. ret = esam_encrypt(g_cmd12,sizeof(g_cmd12),g_test_buf,1024);
  681. rt_printf(" 加密[%d].\r\n\r\n",ret);
  682. // 对时解密
  683. ret = esam_decrypt(g_cmd13,sizeof(g_cmd13),g_test_buf,1024);
  684. rt_printf(" 解密[%d].\r\n\r\n",ret);
  685. #endif
  686. // 对时加密
  687. ret = esam_encrypt(g_cmd14,sizeof(g_cmd14),g_test_buf,1024);
  688. rt_printf(" 加密[%d].\r\n\r\n",ret);
  689. // 遥控解密
  690. ret = esam_decrypt(g_cmd15,sizeof(g_cmd15),g_test_buf,1024);
  691. rt_printf(" 解密[%d].\r\n\r\n",ret);
  692. return 0;
  693. }
  694. int esam_info_printf(void)
  695. {
  696. static int count=1;
  697. int ret;
  698. int i;
  699. static u8 in[256];
  700. static u8 out[128];
  701. s8 sz[64];
  702. u16 cer_len=0;
  703. int g_print_sys_old;
  704. g_print_sys_old = g_print_sys;
  705. g_print_sys = 1;
  706. rt_printf("esam info...\r\n");
  707. for(i=0;i<128;i++)
  708. {
  709. in[i] =i;
  710. }
  711. // 版本
  712. ret = esam_get_version(out,1);
  713. if(ret < 0)
  714. {
  715. rt_printf("检测不到加密芯片[%d]\r\n",ret);
  716. g_print_sys = g_print_sys_old;
  717. return -1;
  718. }
  719. rt_printf("版本[%d]:0x%02x.\r\n",ret,out[0]);
  720. // 序号
  721. for(i=0;i<1;i++)
  722. {
  723. ret = esam_get_sn(out,8);
  724. sprintf(sz,"%02d序号[%d]:",count,ret);
  725. print_mem(sz,out,8);
  726. if(ret < 0)
  727. {
  728. break;
  729. }
  730. }
  731. // 得到随机数
  732. ret = esam_get_random(out,8);
  733. rt_printf("随机数[%d].\r\n",ret);
  734. // 随机数签名
  735. ret = esam_sign_random(in,8,out,0x48);
  736. rt_printf("随机数签名[%d].\r\n",ret);
  737. // 终端公钥
  738. ret = esam_yw_get_pubkey(g_test_buf,1024);
  739. rt_printf("获取终端公钥[%d].\r\n",ret);
  740. #if 0
  741. // 主站签名验证
  742. ret = esam_sign_verify_zz(1,in,64);
  743. rt_printf("主站签名验证[%d].\r\n",ret);
  744. // 规约签名验证
  745. ret = esam_sign_verify_yk(2,in,128);
  746. rt_printf("规约签名验证[%d].\r\n",ret);
  747. // 加密
  748. ret = esam_encrypt(in,65,out,128);
  749. rt_printf("加密[%d].\r\n",ret);
  750. // 解密
  751. ret = esam_decrypt(out,ret,in,128);
  752. rt_printf("解密[%d].\r\n",ret);
  753. // 读取终端测试证书长度
  754. ret = esam_cer_zd_test_get_len((u8*)&cer_len,2);
  755. rt_printf("读取终端测试证书长度[%d].\r\n",ret);
  756. // 读取终端测试证书
  757. ret = esam_cer_zd_test_get(cer_len,g_test_buf,1024);
  758. rt_printf("读取终端测试证书[%d].\r\n",ret);
  759. #endif
  760. // 读取终端正式证书
  761. ret = esam_cer_zd_get(g_test_buf,1024);
  762. rt_printf("读取终端正式证书[%d].\r\n",ret);
  763. // 如果成功,将测试证书写入文件
  764. if(ret > 0)
  765. {
  766. loff_t pos;
  767. struct file * gfile;
  768. gfile = rt_file_open("/tmp/zd_test.cer",O_CREAT|O_RDWR|O_TRUNC,0);
  769. if(IS_ERR(gfile))
  770. {
  771. g_print_sys = g_print_sys_old;
  772. return -1;
  773. }
  774. pos = 0;
  775. rt_file_write(gfile,g_test_buf,ret,&pos);
  776. rt_file_close(gfile,NULL);
  777. rt_printf("证书输出路径:/tmp/zd_test.cer.\r\n");
  778. cer_len = ret;
  779. }
  780. // esam_test();
  781. ret = esam_cer_zd_update(g_test_buf,cer_len);
  782. rt_printf("更新终端正式证书[%d].\r\n",ret);
  783. g_print_sys = g_print_sys_old;
  784. return 0;
  785. }