sc1161y.c 25 KB

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