当前位置:   article > 正文

各种加解密接口自测_dongle_not_found

dongle_not_found
以下为常见加解密接口自测资料,整理备忘:

  1. // rockey_test.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "SeKey.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include "mbedtls/des.h"
  9. #include "mbedtls/sha1.h"
  10. #include "mbedtls/sha256.h"
  11. #include "mbedtls/sha512.h"
  12. #include "mbedtls/aes.h"
  13. #include "mbedtls/rsa.h"
  14. #include "mbedtls/config.h"
  15. //1
  16. int test_find_open_close()
  17. {
  18. DONGLE_HANDLE hDongle;
  19. unsigned long did = 0XFFFFFFFA;
  20. unsigned short count = 0;
  21. unsigned long ret_status = 0;
  22. printf("test_find_open_close()\n");
  23. //Dongle_Find1 当开发商ID输入错误时
  24. ret_status = Dongle_Find(did, &count);
  25. if(ret_status != DONGLE_NOT_FOUND)
  26. {
  27. printf("Dongle_Find未找到指定的设备\n");
  28. printf("错误处101\n");
  29. return 101;
  30. }
  31. did = 0XFFFFFFFF;
  32. //Dongle_Find2 当开发商ID输入正确时
  33. ret_status = Dongle_Find(did, &count);
  34. if(ret_status == DONGLE_SUCCESS)
  35. {
  36. printf("Dongle_Find操作成功\n");
  37. }
  38. // 从1开始的索引值。表示打开找到的第几把加密锁。打开方式:0为独占打开,1为共享打开
  39. //Dongle_Open1 当索引值输入错误,其他参数输入正确时
  40. ret_status = Dongle_Open(2, 1, &hDongle);
  41. if(ret_status == DONGLE_SUCCESS)
  42. {
  43. printf("错误处102\n");
  44. return 102;
  45. }
  46. //Dongle_Open2 当索引值输入正确,打开方式输入错误时
  47. ret_status = Dongle_Open(1, 2, &hDongle);
  48. if(ret_status == DONGLE_SUCCESS)
  49. {
  50. printf("错误处103\n");
  51. return 103;
  52. }
  53. //Dongle_Open3 当索引值输入正确,其他参数输入正确时
  54. ret_status = Dongle_Open(1, 0, &hDongle);
  55. if(ret_status == DONGLE_SUCCESS)
  56. {
  57. printf("Dongle_Open操作成功\n");
  58. }
  59. ret_status = Dongle_Close(hDongle);
  60. if(ret_status == DONGLE_SUCCESS)
  61. {
  62. printf("Dongle_Close操作成功\n");
  63. }
  64. else
  65. {
  66. printf("错误处104\n");
  67. return 104;
  68. }
  69. printf("\n\n");
  70. return 0;
  71. }
  72. //2
  73. int test_verifyPin()
  74. {
  75. DONGLE_HANDLE hDongle;
  76. unsigned long did = 0XFFFFFFFF;
  77. unsigned short count = 0;
  78. unsigned long ret_status = 0;
  79. //char* upin = "00000000";
  80. unsigned char upin[8] = {0};
  81. //char* dpin = "FFFFFFFFFFFFFFFFFFFFFFFF";
  82. unsigned char dpin[24] = {0};
  83. int i = 0;
  84. for(i = 0; i < 24; i++)
  85. {
  86. dpin[i] = 0xFF;
  87. }
  88. unsigned char upin1[8] = {0,1,2,3,4,5,6,7};
  89. unsigned char dpin0[24] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4};
  90. unsigned char pTryCount;
  91. //写
  92. char *pBuffer = "123456789abc";
  93. char rec_buffer[256] = {0};
  94. unsigned short wOffset = 0;
  95. unsigned short wLen = 0;
  96. ret_status = Dongle_Find(did, &count);
  97. ret_status = Dongle_Open(1, 0, &hDongle);
  98. //1表示用户pin,0表示开发商pin 用户PIN码(8字节,出厂默认全0)和开发商PIN码(24字节,出厂默认全F)
  99. //Dongle_VerifyPIN1 表示用户参数输入错误时
  100. ret_status = Dongle_VerifyPIN(hDongle, 3, upin, (BYTE *)&pTryCount);
  101. if(ret_status == DONGLE_SUCCESS)
  102. {
  103. printf("错误处201\n");
  104. return 201;
  105. }
  106. //Dongle_VerifyPIN2 开发商pin输入错误时
  107. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin0, (BYTE *)&pTryCount);
  108. if(ret_status == DONGLE_SUCCESS)
  109. {
  110. printf("错误处202\n");
  111. return 202;
  112. }
  113. //Dongle_VerifyPIN3 用户pin输入错误时
  114. ret_status = Dongle_VerifyPIN(hDongle, 1, upin1, (BYTE *)&pTryCount);
  115. if(ret_status == DONGLE_SUCCESS)
  116. {
  117. printf("错误处203\n");
  118. return 203;
  119. }
  120. //Dongle_VerifyPIN4 开发商pin输入正确时
  121. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  122. if(ret_status != DONGLE_SUCCESS)
  123. {
  124. printf("错误处204\n");
  125. return 204;
  126. }
  127. //Dongle_VerifyPIN5 用户pin输入正确时
  128. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  129. if(ret_status != DONGLE_SUCCESS)
  130. {
  131. printf("错误处205\n");
  132. return 205;
  133. }
  134. ret_status = Dongle_ResetState(hDongle);
  135. Dongle_Close(hDongle);
  136. return 0;
  137. }
  138. //3
  139. int test_write()
  140. {
  141. DONGLE_HANDLE hDongle;
  142. unsigned long did = 0XFFFFFFFF;
  143. unsigned short count = 0;
  144. unsigned long ret_status = 0;
  145. unsigned char pTryCount;
  146. //写
  147. char pBuffer[256] = "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
  148. "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
  149. "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
  150. "123456789abcdefghijklmnopqrstuvwxyz123456789a";
  151. //读
  152. char rBuffer[256] = {0};
  153. unsigned char upin[8] = {0};
  154. unsigned char dpin[24] = {0};
  155. int i = 0;
  156. for(i = 0; i < 24; i++)
  157. {
  158. dpin[i] = 0xFF;
  159. }
  160. ret_status = Dongle_Find(did, &count);
  161. ret_status = Dongle_Open(1, 0, &hDongle);
  162. //验证用户权限写,参数传入错误
  163. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  164. //Dongle_WriteData1 用户权限写,参数错误 偏移量和长度都为0 返回错误
  165. ret_status = Dongle_WriteData(hDongle, 0, 0, (BYTE *)pBuffer);
  166. if(ret_status == DONGLE_SUCCESS)
  167. {
  168. printf("错误处301\n");
  169. return 301;
  170. }
  171. //验证开发商权限写,参数传入错误
  172. ret_status = Dongle_ResetState(hDongle);
  173. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  174. //Dongle_WriteData2 开发商权限写,参数错误 偏移量和长度都为0 返回错误
  175. ret_status = Dongle_WriteData(hDongle, 0, 0, (BYTE *)pBuffer);
  176. if(ret_status == DONGLE_SUCCESS)
  177. {
  178. printf("错误处302\n");
  179. return 302;
  180. }
  181. //验证用户权限写高128字节,返回错误
  182. ret_status = Dongle_ResetState(hDongle);
  183. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  184. //Dongle_WriteData3 用户权限写高128字节,返回错误
  185. ret_status = Dongle_WriteData(hDongle, 128, 128, (BYTE *)pBuffer);
  186. if(ret_status == DONGLE_SUCCESS)
  187. {
  188. printf("错误处303\n");
  189. return 303;
  190. }
  191. //恢复匿名权限写高128字节,返回错误
  192. ret_status = Dongle_ResetState(hDongle);
  193. //Dongle_WriteData4 匿名权限写高128字节,返回错误
  194. ret_status = Dongle_WriteData(hDongle, 128, 128, (BYTE *)pBuffer);
  195. if(ret_status == DONGLE_SUCCESS)
  196. {
  197. printf("错误处304\n");
  198. return 304;
  199. }
  200. //验证开发商权限写高128字节,返回成功
  201. ret_status = Dongle_ResetState(hDongle);
  202. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  203. //Dongle_WriteData5 开发商权限写高128字节,返回成功
  204. ret_status = Dongle_WriteData(hDongle, 128, 128, (BYTE *)pBuffer);
  205. if(ret_status != DONGLE_SUCCESS)
  206. {
  207. printf("错误处305\n");
  208. return 305;
  209. }
  210. ret_status = Dongle_ReadData(hDongle, 128, 128, (BYTE *)rBuffer);
  211. if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
  212. {
  213. }
  214. else
  215. {
  216. printf("开发商权限写高128字节,写入数据错误\n");
  217. return 306;
  218. }
  219. //恢复匿名权限写低128字节,返回错误
  220. ret_status = Dongle_ResetState(hDongle);
  221. //Dongle_WriteData6 匿名权限写低128字节,返回错误
  222. ret_status = Dongle_WriteData(hDongle, 0, 128, (BYTE *)pBuffer);
  223. if(ret_status == DONGLE_SUCCESS)
  224. {
  225. printf("错误处307\n");
  226. return 307;
  227. }
  228. //验证开发商权限写低128字节,返回成功
  229. ret_status = Dongle_ResetState(hDongle);
  230. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  231. //Dongle_WriteData7 开发商权限写低128字节,返回成功
  232. ret_status = Dongle_WriteData(hDongle, 0, 128, (BYTE *)pBuffer);
  233. if(ret_status != DONGLE_SUCCESS)
  234. {
  235. printf("错误处308\n");
  236. return 308;
  237. }
  238. ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
  239. if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
  240. {
  241. }
  242. else
  243. {
  244. printf("开发商权限写低128字节,写入数据错误\n");
  245. return 309;
  246. }
  247. //验证用户权限写低128字节,返回成功
  248. ret_status = Dongle_ResetState(hDongle);
  249. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  250. //Dongle_WriteData8 用户权限写低128字节,返回成功
  251. ret_status = Dongle_WriteData(hDongle, 0, 128, (BYTE *)pBuffer);
  252. if(ret_status != DONGLE_SUCCESS)
  253. {
  254. printf("错误处310\n");
  255. return 310;
  256. }
  257. ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
  258. if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
  259. {
  260. }
  261. else
  262. {
  263. printf("用户权限写低128字节,写入数据错误\n");
  264. return 311;
  265. }
  266. //验证开发商权限写全部256字节,返回成功
  267. ret_status = Dongle_ResetState(hDongle);
  268. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  269. //Dongle_WriteData9 开发商权限写全部256字节,返回成功
  270. ret_status = Dongle_WriteData(hDongle, 0, 256, (BYTE *)pBuffer);
  271. if(ret_status != DONGLE_SUCCESS)
  272. {
  273. printf("错误处312\n");
  274. return 312;
  275. }
  276. Dongle_Close(hDongle);
  277. return 0;
  278. }
  279. //4
  280. int test_read()
  281. {
  282. DONGLE_HANDLE hDongle;
  283. unsigned long did = 0XFFFFFFFF;
  284. unsigned short count = 0;
  285. unsigned long ret_status = 0;
  286. unsigned char pTryCount;
  287. //写
  288. char pBuffer[256] = "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
  289. "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
  290. "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
  291. "123456789abcdefghijklmnopqrstuvwxyz123456789a";
  292. //读
  293. char rBuffer[256] = {0};
  294. unsigned char upin[8] = {0};
  295. unsigned char dpin[24] = {0};
  296. int i = 0;
  297. for(i = 0; i < 24; i++)
  298. {
  299. dpin[i] = 0xFF;
  300. }
  301. ret_status = Dongle_Find(did, &count);
  302. ret_status = Dongle_Open(1, 0, &hDongle);
  303. //开发商权限写全部256字节,返回成功
  304. ret_status = Dongle_ResetState(hDongle);
  305. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  306. //Dongle_WriteData 开发商权限写全部256字节,返回成功
  307. ret_status = Dongle_WriteData(hDongle, 0, 256, (BYTE *)pBuffer);
  308. //1验证用户权限读,参数传入错误
  309. ret_status = Dongle_ResetState(hDongle);
  310. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  311. //Dongle_ReadData1 用户权限读,参数错误 偏移量和长度都为0 返回错误
  312. ret_status = Dongle_ReadData(hDongle, 0, 0, (BYTE *)rBuffer);
  313. if(ret_status == DONGLE_SUCCESS)
  314. {
  315. printf("错误处401\n");
  316. return 401;
  317. }
  318. //2验证开发商权限读,参数传入错误
  319. ret_status = Dongle_ResetState(hDongle);
  320. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  321. //Dongle_ReadData2 开发商权限读,参数错误 偏移量和长度都为0 返回错误
  322. ret_status = Dongle_ReadData(hDongle, 0, 0, (BYTE *)rBuffer);
  323. if(ret_status == DONGLE_SUCCESS)
  324. {
  325. printf("错误处402\n");
  326. return 402;
  327. }
  328. //3验证用户权限读高128字节,返回成功
  329. memset(rBuffer, 0, 256);
  330. ret_status = Dongle_ResetState(hDongle);
  331. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  332. //Dongle_ReadData3 用户权限读高128字节,返回成功
  333. ret_status = Dongle_ReadData(hDongle, 128, 128, (BYTE *)rBuffer);
  334. if(ret_status != DONGLE_SUCCESS)
  335. {
  336. printf("错误处403\n");
  337. return 403;
  338. }
  339. if(0 == strcmp(rBuffer, "opqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789a"))
  340. {
  341. }
  342. else
  343. {
  344. printf("用户权限读高128字节,读取数据错误\n");
  345. return 404;
  346. }
  347. //4恢复匿名权限读高128字节,返回错误
  348. memset(rBuffer, 0, 256);
  349. ret_status = Dongle_ResetState(hDongle);
  350. //Dongle_ReadData4 匿名权限读高128字节,返回错误
  351. ret_status = Dongle_ReadData(hDongle, 128, 128, (BYTE *)rBuffer);
  352. if(ret_status == DONGLE_SUCCESS)
  353. {
  354. printf("错误处405\n");
  355. return 405;
  356. }
  357. //5验证开发商权限读高128字节,返回成功
  358. memset(rBuffer, 0, 256);
  359. ret_status = Dongle_ResetState(hDongle);
  360. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  361. //Dongle_ReadData5 开发商权限读高128字节,返回成功
  362. ret_status = Dongle_ReadData(hDongle, 128, 128, (BYTE *)rBuffer);
  363. if(ret_status != DONGLE_SUCCESS)
  364. {
  365. printf("错误处406\n");
  366. return 406;
  367. }
  368. if(0 == strcmp(rBuffer, "opqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789a"))
  369. {
  370. }
  371. else
  372. {
  373. printf("开发商权限读高128字节,读取数据错误\n");
  374. return 407;
  375. }
  376. //6恢复匿名权限读低128字节,返回成功
  377. memset(rBuffer, 0, 256);
  378. ret_status = Dongle_ResetState(hDongle);
  379. //Dongle_ReadData6 匿名权限读低128字节,返回成功
  380. ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
  381. if(ret_status != DONGLE_SUCCESS)
  382. {
  383. printf("错误处408\n");
  384. return 408;
  385. }
  386. if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
  387. {
  388. }
  389. else
  390. {
  391. printf("匿名权限读低128字节,读取数据错误\n");
  392. return 409;
  393. }
  394. //7验证开发商权限读低128字节,返回成功
  395. memset(rBuffer, 0, 256);
  396. ret_status = Dongle_ResetState(hDongle);
  397. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  398. //Dongle_ReadData7 开发商权限读低128字节,返回成功
  399. ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
  400. if(ret_status != DONGLE_SUCCESS)
  401. {
  402. printf("错误处410\n");
  403. return 410;
  404. }
  405. if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
  406. {
  407. }
  408. else
  409. {
  410. printf("开发商权限读低128字节,读取数据错误\n");
  411. return 411;
  412. }
  413. //8验证用户权限读低128字节,返回成功
  414. memset(rBuffer, 0, 256);
  415. ret_status = Dongle_ResetState(hDongle);
  416. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  417. //Dongle_ReadData8 用户权限读低128字节,返回成功
  418. ret_status = Dongle_ReadData(hDongle, 0, 128, (BYTE *)rBuffer);
  419. if(ret_status != DONGLE_SUCCESS)
  420. {
  421. printf("错误处412\n");
  422. return 412;
  423. }
  424. if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"))
  425. {
  426. }
  427. else
  428. {
  429. printf("用户权限读低128字节,读取数据错误\n");
  430. return 413;
  431. }
  432. //9验证开发商权限读全部256字节,返回成功
  433. memset(rBuffer, 0, 256);
  434. ret_status = Dongle_ResetState(hDongle);
  435. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  436. //Dongle_ReadData9 开发商权限读全部256字节,返回成功
  437. ret_status = Dongle_ReadData(hDongle, 0, 256, (BYTE *)rBuffer);
  438. if(ret_status != DONGLE_SUCCESS)
  439. {
  440. printf("错误处414\n");
  441. return 414;
  442. }
  443. if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
  444. "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"
  445. "opqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abc"
  446. "defghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789a"))
  447. {
  448. }
  449. else
  450. {
  451. printf("开发商权限读全部256字节,读取数据错误\n");
  452. return 415;
  453. }
  454. //10验证用户权限读全部256字节,返回成功
  455. memset(rBuffer, 0, 256);
  456. ret_status = Dongle_ResetState(hDongle);
  457. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  458. //Dongle_ReadData10 用户权限读全部256字节,返回成功
  459. ret_status = Dongle_ReadData(hDongle, 0, 256, (BYTE *)rBuffer);
  460. if(ret_status != DONGLE_SUCCESS)
  461. {
  462. printf("错误处416\n");
  463. return 416;
  464. }
  465. if(0 == strcmp(rBuffer, "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz"
  466. "123456789abcdefghijklmnopqrstuvwxyz123456789abcdefghijklmn"
  467. "opqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789abc"
  468. "defghijklmnopqrstuvwxyz123456789abcdefghijklmnopqrstuvwxyz123456789a"))
  469. {
  470. }
  471. else
  472. {
  473. printf("用户权限读全部256字节,读取数据错误\n");
  474. return 417;
  475. }
  476. Dongle_Close(hDongle);
  477. return 0;
  478. }
  479. //5
  480. int test_set3DESKey()
  481. {
  482. DONGLE_HANDLE hDongle;
  483. unsigned long did = 0XFFFFFFFF;
  484. unsigned short count = 0;
  485. unsigned long ret_status = 0;
  486. unsigned char pTryCount;
  487. //密钥数值
  488. unsigned char key3[24] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,0,1,2,3,4,5,6,7,8,9};
  489. unsigned char key2[16] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
  490. unsigned char key1[8] = {1,2,3,4,5,6,7,8};
  491. unsigned char pLv[8] = {4,3,2,1,8,7,5,6};
  492. unsigned char OutsideKeyPlv[8] = {4,3,2,1,8,7,5,6};
  493. unsigned char buf[24] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,1,1,1};
  494. unsigned char OutsideKeyBuf[24] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,1,1,1};
  495. //明文
  496. unsigned char plain[24] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,1,1,1};
  497. unsigned char plain7[24] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,1,1,1};
  498. //密文
  499. unsigned char dec_plain[24] = {0};
  500. unsigned char mbedtlsPlain[24] = {0};
  501. //mbedTLS中关于3DES相关配置
  502. mbedtls_des3_context des3_ctx;
  503. mbedtls_des_context des_ctx;
  504. unsigned char upin[8] = {0};
  505. unsigned char dpin[24] = {0};
  506. int i = 0;
  507. for(i = 0; i < 24; i++)
  508. {
  509. dpin[i] = 0xFF;
  510. }
  511. mbedtls_des3_init(&des3_ctx);
  512. mbedtls_des_init(&des_ctx);
  513. ret_status = Dongle_Find(did, &count);
  514. ret_status = Dongle_Open(1, 0, &hDongle);
  515. //设置3DES密钥
  516. //1匿名状态设置3DES密钥 返回失败
  517. ret_status = Dongle_ResetState(hDongle);
  518. ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
  519. if(ret_status == DONGLE_SUCCESS)
  520. {
  521. printf("错误处501\n");
  522. return 501;
  523. }
  524. //2用户权限设置3DES密钥 返回失败
  525. ret_status = Dongle_ResetState(hDongle);
  526. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  527. ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
  528. if(ret_status == DONGLE_SUCCESS)
  529. {
  530. printf("错误处502\n");
  531. return 502;
  532. }
  533. //3开发商权限设置3DES密钥 密钥ID设置为1,返回失败
  534. ret_status = Dongle_ResetState(hDongle);
  535. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  536. ret_status = Dongle_Set3DESKey(hDongle, 1, key3, 24);
  537. if(ret_status == DONGLE_SUCCESS)
  538. {
  539. printf("错误处503\n");
  540. return 503;
  541. }
  542. //4开发商权限设置3DES密钥 密钥ID设置为0,返回成功
  543. ret_status = Dongle_ResetState(hDongle);
  544. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  545. ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
  546. if(ret_status != DONGLE_SUCCESS)
  547. {
  548. printf("错误处504\n");
  549. return 504;
  550. }
  551. //5开发商权限设置3DES密钥 密钥ID设置为1,返回失败
  552. ret_status = Dongle_ResetState(hDongle);
  553. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  554. ret_status = Dongle_Set3DESKey(hDongle, 1, key3, 24);
  555. if(ret_status == DONGLE_SUCCESS)
  556. {
  557. printf("错误处505\n");
  558. return 505;
  559. }
  560. //3DES运算
  561. //6参数验证,3DES运算函数传参,用户权限下传入密钥ID 1,返回失败
  562. ret_status = Dongle_ResetState(hDongle);
  563. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  564. // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
  565. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 1, 0, pLv, plain, 24);
  566. if(ret_status == DONGLE_SUCCESS)
  567. {
  568. printf("错误处506\n");
  569. return 506;
  570. }
  571. //7参数验证,3DES运算函数传参,用户权限下传入运算类型2,返回失败
  572. ret_status = Dongle_ResetState(hDongle);
  573. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  574. // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
  575. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 2, pLv, plain7, 24);
  576. if(ret_status == DONGLE_SUCCESS)
  577. {
  578. printf("错误处507\n");
  579. //return 507;
  580. }
  581. //8参数验证,3DES运算函数传参,用户权限下传入运算类型2,返回失败
  582. ret_status = Dongle_ResetState(hDongle);
  583. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  584. // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
  585. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 2, pLv, plain7, 24);
  586. if(ret_status == DONGLE_SUCCESS)
  587. {
  588. printf("错误处508\n");
  589. //return 508;
  590. }
  591. //9权限验证,3DES运算函数传参,匿名状态下进行加密运算,返回失败
  592. ret_status = Dongle_ResetState(hDongle);
  593. // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
  594. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0, pLv, plain, 24);
  595. if(ret_status == DONGLE_SUCCESS)
  596. {
  597. printf("错误处509\n");
  598. return 509;
  599. }
  600. //一 3DES运算 ecb模式 8字节key
  601. ret_status = Dongle_ResetState(hDongle);
  602. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  603. ret_status = Dongle_Set3DESKey(hDongle, 0, key1, 8);
  604. //10权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
  605. ret_status = Dongle_ResetState(hDongle);
  606. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  607. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  608. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 24);
  609. if(ret_status != DONGLE_SUCCESS)
  610. {
  611. printf("错误处510\n");
  612. return 510;
  613. }
  614. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  615. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 0x00, OutsideKeyPlv, OutsideKeyBuf, 24);
  616. if(ret_status != DONGLE_SUCCESS)
  617. {
  618. printf("错误处511\n");
  619. return 511;
  620. }
  621. //10 ecb模式 8字节key 外库加密
  622. mbedtls_des_setkey_enc(&des_ctx, key1);
  623. for(i = 0; i < 3; i++)
  624. {
  625. mbedtls_des_crypt_ecb( &des_ctx, plain + (8*i), dec_plain + (8*i));
  626. }
  627. //外库与锁内 加密验证
  628. for(i = 0; i < 24; i++)
  629. {
  630. if(buf[i] != dec_plain[i])
  631. {
  632. printf("ecb模式 8字节key enc出错 i = %d\n", i);
  633. return 512;
  634. }
  635. }
  636. //锁内与锁外 加密验证
  637. for(i = 0; i < 24; i++)
  638. {
  639. if(buf[i] != OutsideKeyBuf[i])
  640. {
  641. printf("ecb模式 8字节key enc出错 i = %d\n", i);
  642. return 513;
  643. }
  644. }
  645. //11权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
  646. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  647. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 24);
  648. if(ret_status != DONGLE_SUCCESS)
  649. {
  650. printf("错误处514\n");
  651. return 514;
  652. }
  653. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  654. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 0x01, OutsideKeyPlv, OutsideKeyBuf, 24);
  655. if(ret_status != DONGLE_SUCCESS)
  656. {
  657. printf("错误处515\n");
  658. return 515;
  659. }
  660. //12 ecb模式 8字节key 外库解密
  661. mbedtls_des_setkey_dec(&des_ctx, key1);
  662. for(i = 0; i < 3; i++)
  663. {
  664. mbedtls_des_crypt_ecb( &des_ctx, dec_plain + (8*i), mbedtlsPlain + (8*i));
  665. }
  666. //外库与锁内 解密验证
  667. for(i = 0; i < 24; i++)
  668. {
  669. if(buf[i] != mbedtlsPlain[i])
  670. {
  671. printf("ecb模式 8字节key dec出错 i = %d\n", i);
  672. return 516;
  673. }
  674. }
  675. //锁内与锁外 解密验证
  676. for(i = 0; i < 24; i++)
  677. {
  678. if(buf[i] != OutsideKeyBuf[i])
  679. {
  680. printf("ecb模式 8字节key dec出错 i = %d\n", i);
  681. return 517;
  682. }
  683. }
  684. //二 3DES运算 ecb模式 16字节key
  685. ret_status = Dongle_ResetState(hDongle);
  686. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  687. ret_status = Dongle_Set3DESKey(hDongle, 0, key2, 16);
  688. //13权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
  689. ret_status = Dongle_ResetState(hDongle);
  690. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  691. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  692. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 24);
  693. if(ret_status != DONGLE_SUCCESS)
  694. {
  695. printf("错误处518\n");
  696. return 518;
  697. }
  698. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  699. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key2, 16, 0x00, OutsideKeyPlv, OutsideKeyBuf, 24);
  700. if(ret_status != DONGLE_SUCCESS)
  701. {
  702. printf("错误处519\n");
  703. return 519;
  704. }
  705. //14 ecb模式 16字节key 外库加密
  706. memset(dec_plain, 0, 24);
  707. mbedtls_des3_set2key_enc(&des3_ctx, key2);
  708. for(i = 0; i < 3; i++)
  709. {
  710. mbedtls_des3_crypt_ecb( &des3_ctx, plain + (8*i), dec_plain + (8*i));
  711. }
  712. //外库与锁内 加密验证
  713. for(i = 0; i < 24; i++)
  714. {
  715. if(buf[i] != dec_plain[i])
  716. {
  717. printf("ecb模式 16字节key enc出错 i = %d\n", i);
  718. return 520;
  719. }
  720. }
  721. //锁内与锁外 加密验证
  722. for(i = 0; i < 24; i++)
  723. {
  724. if(buf[i] != OutsideKeyBuf[i])
  725. {
  726. printf("ecb模式 16字节key enc出错 i = %d\n", i);
  727. return 521;
  728. }
  729. }
  730. //15权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
  731. // 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  732. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 24);
  733. if(ret_status != DONGLE_SUCCESS)
  734. {
  735. printf("错误处522\n");
  736. return 522;
  737. }
  738. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  739. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key2, 16, 0x01, OutsideKeyPlv, OutsideKeyBuf, 24);
  740. if(ret_status != DONGLE_SUCCESS)
  741. {
  742. printf("错误处523\n");
  743. return 523;
  744. }
  745. //16 ecb模式 16字节key 外库解密
  746. memset(mbedtlsPlain, 0, 24);
  747. mbedtls_des3_set2key_dec(&des3_ctx, key2);
  748. for(i = 0; i < 3; i++)
  749. {
  750. mbedtls_des3_crypt_ecb( &des3_ctx, dec_plain + (8*i), mbedtlsPlain + (8*i));
  751. }
  752. //外库与锁内 解密验证
  753. for(i = 0; i < 24; i++)
  754. {
  755. if(buf[i] != mbedtlsPlain[i])
  756. {
  757. printf("ecb模式 16字节key dec出错 i = %d\n", i);
  758. return 524;
  759. }
  760. }
  761. //锁内与锁外 解密验证
  762. for(i = 0; i < 24; i++)
  763. {
  764. if(buf[i] != OutsideKeyBuf[i])
  765. {
  766. printf("ecb模式 16字节key dec出错 i = %d\n", i);
  767. return 525;
  768. }
  769. }
  770. //三 3DES运算 ecb模式 24字节key
  771. ret_status = Dongle_ResetState(hDongle);
  772. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  773. ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
  774. //17权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
  775. ret_status = Dongle_ResetState(hDongle);
  776. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  777. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  778. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 24);
  779. if(ret_status != DONGLE_SUCCESS)
  780. {
  781. printf("错误处526\n");
  782. return 526;
  783. }
  784. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  785. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key3, 24, 0x00, OutsideKeyPlv, OutsideKeyBuf, 24);
  786. if(ret_status != DONGLE_SUCCESS)
  787. {
  788. printf("错误处527\n");
  789. return 527;
  790. }
  791. //18 ecb模式 24字节key 外库加密
  792. memset(dec_plain, 0, 24);
  793. mbedtls_des3_set3key_enc(&des3_ctx, key3);
  794. for(i = 0; i < 3; i++)
  795. {
  796. mbedtls_des3_crypt_ecb( &des3_ctx, plain + (8*i), dec_plain + (8*i));
  797. }
  798. //外库与锁内 加密验证
  799. for(i = 0; i < 24; i++)
  800. {
  801. if(buf[i] != dec_plain[i])
  802. {
  803. printf("ecb模式 24字节key enc出错 i = %d\n", i);
  804. return 528;
  805. }
  806. }
  807. //锁内与锁外 加密验证
  808. for(i = 0; i < 24; i++)
  809. {
  810. if(buf[i] != OutsideKeyBuf[i])
  811. {
  812. printf("ecb模式 24字节key enc出错 i = %d\n", i);
  813. return 528;
  814. }
  815. }
  816. //19 权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
  817. // 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  818. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 24);
  819. if(ret_status != DONGLE_SUCCESS)
  820. {
  821. printf("错误处529\n");
  822. return 529;
  823. }
  824. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  825. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key3, 24, 0x01, OutsideKeyPlv, OutsideKeyBuf, 24);
  826. if(ret_status != DONGLE_SUCCESS)
  827. {
  828. printf("错误处530\n");
  829. return 530;
  830. }
  831. //20 ecb模式 24字节key 外库解密
  832. memset(mbedtlsPlain, 0, 24);
  833. mbedtls_des3_set3key_dec(&des3_ctx, key3);
  834. for(i = 0; i < 3; i++)
  835. {
  836. mbedtls_des3_crypt_ecb( &des3_ctx, dec_plain + (8*i), mbedtlsPlain + (8*i));
  837. }
  838. //外库与锁内 解密验证
  839. for(i = 0; i < 24; i++)
  840. {
  841. if(buf[i] != mbedtlsPlain[i])
  842. {
  843. printf("ecb模式 24字节key dec出错 i = %d\n", i);
  844. return 531;
  845. }
  846. }
  847. //锁内与锁外 解密验证
  848. for(i = 0; i < 24; i++)
  849. {
  850. if(buf[i] != OutsideKeyBuf[i])
  851. {
  852. printf("ecb模式 24字节key dec出错 i = %d\n", i);
  853. return 532;
  854. }
  855. }
  856. //四 3DES运算 cbc模式 8字节key
  857. ret_status = Dongle_ResetState(hDongle);
  858. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  859. ret_status = Dongle_Set3DESKey(hDongle, 0, key1, 8);
  860. //21 权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
  861. ret_status = Dongle_ResetState(hDongle);
  862. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  863. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  864. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 24);
  865. if(ret_status != DONGLE_SUCCESS)
  866. {
  867. printf("错误处533\n");
  868. return 533;
  869. }
  870. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  871. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 0x10, OutsideKeyPlv, OutsideKeyBuf, 24);
  872. if(ret_status != DONGLE_SUCCESS)
  873. {
  874. printf("错误处534\n");
  875. return 534;
  876. }
  877. //22 cbc模式 8字节key 外库加密
  878. memset(dec_plain, 0, 24);
  879. unsigned char mbedtls_enc_pLv1[8] = {4,3,2,1,8,7,5,6};
  880. unsigned char mbedtls_dec_pLv1[8] = {4,3,2,1,8,7,5,6};
  881. mbedtls_des_setkey_enc(&des_ctx, key1);
  882. for(i = 0; i < 3; i++)
  883. {
  884. mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_ENCRYPT, 8, mbedtls_enc_pLv1, plain + (8*i), dec_plain + (8*i));
  885. }
  886. //外库与锁内 加密验证
  887. for(i = 0; i < 24; i++)
  888. {
  889. if(buf[i] != dec_plain[i])
  890. {
  891. printf("cbc模式 8字节key enc出错 i = %d\n", i);
  892. return 535;
  893. }
  894. }
  895. //锁内与锁外 加密验证
  896. for(i = 0; i < 24; i++)
  897. {
  898. if(buf[i] != OutsideKeyBuf[i])
  899. {
  900. printf("cbc模式 8字节key enc出错 i = %d\n", i);
  901. return 536;
  902. }
  903. }
  904. //23 权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
  905. // 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  906. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 24);
  907. if(ret_status != DONGLE_SUCCESS)
  908. {
  909. printf("错误处537\n");
  910. return 537;
  911. }
  912. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  913. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key1, 8, 0x11, OutsideKeyPlv, OutsideKeyBuf, 24);
  914. if(ret_status != DONGLE_SUCCESS)
  915. {
  916. printf("错误处538\n");
  917. return 538;
  918. }
  919. //24 cbc模式 8字节key 外库解密
  920. memset(mbedtlsPlain, 0, 24);
  921. mbedtls_des_setkey_dec(&des_ctx, key1);
  922. for(i = 0; i < 3; i++)
  923. {
  924. mbedtls_des_crypt_cbc( &des_ctx, MBEDTLS_DES_DECRYPT, 8, mbedtls_dec_pLv1, dec_plain + (8*i), mbedtlsPlain + (8*i));
  925. }
  926. //外库与锁内 解密验证
  927. for(i = 0; i < 24; i++)
  928. {
  929. if(buf[i] != mbedtlsPlain[i])
  930. {
  931. printf("cbc模式 8字节key dec出错 i = %d\n", i);
  932. return 539;
  933. }
  934. }
  935. //锁内与锁外 解密验证
  936. for(i = 0; i < 24; i++)
  937. {
  938. if(buf[i] != OutsideKeyBuf[i])
  939. {
  940. printf("cbc模式 8字节key dec出错 i = %d\n", i);
  941. return 540;
  942. }
  943. }
  944. //五 3DES运算 cbc模式 16字节key
  945. ret_status = Dongle_ResetState(hDongle);
  946. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  947. ret_status = Dongle_Set3DESKey(hDongle, 0, key2, 16);
  948. //25 权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
  949. ret_status = Dongle_ResetState(hDongle);
  950. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  951. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  952. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 24);
  953. if(ret_status != DONGLE_SUCCESS)
  954. {
  955. printf("错误处541\n");
  956. return 541;
  957. }
  958. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  959. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key2, 16, 0x10, OutsideKeyPlv, OutsideKeyBuf, 24);
  960. if(ret_status != DONGLE_SUCCESS)
  961. {
  962. printf("错误处542\n");
  963. return 542;
  964. }
  965. //26 cbc模式 16字节key 外库加密
  966. memset(dec_plain, 0, 24);
  967. unsigned char mbedtls_enc_pLv2[8] = {4,3,2,1,8,7,5,6};
  968. unsigned char mbedtls_dec_pLv2[8] = {4,3,2,1,8,7,5,6};
  969. mbedtls_des3_set2key_enc(&des3_ctx, key2);
  970. for(i = 0; i < 3; i++)
  971. {
  972. mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_ENCRYPT, 8, mbedtls_enc_pLv2, plain + (8*i), dec_plain + (8*i));
  973. }
  974. //外库与锁内 加密验证
  975. for(i = 0; i < 24; i++)
  976. {
  977. if(buf[i] != dec_plain[i])
  978. {
  979. printf("cbc模式 16字节key enc出错 i = %d\n", i);
  980. return 543;
  981. }
  982. }
  983. //锁内与锁外 加密验证
  984. for(i = 0; i < 24; i++)
  985. {
  986. if(buf[i] != OutsideKeyBuf[i])
  987. {
  988. printf("cbc模式 16字节key enc出错 i = %d\n", i);
  989. return 544;
  990. }
  991. }
  992. //27 权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
  993. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  994. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 24);
  995. if(ret_status != DONGLE_SUCCESS)
  996. {
  997. printf("错误处545\n");
  998. return 545;
  999. }
  1000. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  1001. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key2, 16, 0x11, OutsideKeyPlv, OutsideKeyBuf, 24);
  1002. if(ret_status != DONGLE_SUCCESS)
  1003. {
  1004. printf("错误处546\n");
  1005. return 546;
  1006. }
  1007. //28 cbc模式 16字节key 外库解密
  1008. memset(mbedtlsPlain, 0, 24);
  1009. mbedtls_des3_set2key_dec(&des3_ctx, key2);
  1010. for(i = 0; i < 3; i++)
  1011. {
  1012. mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, 8, mbedtls_dec_pLv2, dec_plain + (8*i), mbedtlsPlain + (8*i));
  1013. }
  1014. //外库与锁内 解密验证
  1015. for(i = 0; i < 24; i++)
  1016. {
  1017. if(buf[i] != mbedtlsPlain[i])
  1018. {
  1019. printf("cbc模式 16字节key dec出错 i = %d\n", i);
  1020. return 547;
  1021. }
  1022. }
  1023. //锁内与锁外 解密验证
  1024. for(i = 0; i < 24; i++)
  1025. {
  1026. if(buf[i] != OutsideKeyBuf[i])
  1027. {
  1028. printf("cbc模式 16字节key dec出错 i = %d\n", i);
  1029. return 548;
  1030. }
  1031. }
  1032. //六 3DES运算 cbc模式 24字节key
  1033. ret_status = Dongle_ResetState(hDongle);
  1034. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1035. ret_status = Dongle_Set3DESKey(hDongle, 0, key3, 24);
  1036. //29 权限验证,3DES运算函数传参,用户权限下进行加密运算,返回成功
  1037. ret_status = Dongle_ResetState(hDongle);
  1038. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1039. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  1040. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 24);
  1041. if(ret_status != DONGLE_SUCCESS)
  1042. {
  1043. printf("错误处549\n");
  1044. return 549;
  1045. }
  1046. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  1047. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key3, 24, 0x10, OutsideKeyPlv, OutsideKeyBuf, 24);
  1048. if(ret_status != DONGLE_SUCCESS)
  1049. {
  1050. printf("错误处550\n");
  1051. return 550;
  1052. }
  1053. //30 cbc模式 24字节key 外库加密验证
  1054. memset(dec_plain, 0, 24);
  1055. unsigned char mbedtls_enc_pLv3[8] = {4,3,2,1,8,7,5,6};
  1056. unsigned char mbedtls_dec_pLv3[8] = {4,3,2,1,8,7,5,6};
  1057. mbedtls_des3_set3key_enc(&des3_ctx, key3);
  1058. for(i = 0; i < 3; i++)
  1059. {
  1060. mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_ENCRYPT, 8, mbedtls_enc_pLv3, plain + (8*i), dec_plain + (8*i));
  1061. }
  1062. //外库与锁内 加密验证
  1063. for(i = 0; i < 24; i++)
  1064. {
  1065. if(buf[i] != dec_plain[i])
  1066. {
  1067. printf("cbc模式 24字节key enc出错 i = %d\n", i);
  1068. return 551;
  1069. }
  1070. }
  1071. //锁内与锁外 加密验证
  1072. for(i = 0; i < 24; i++)
  1073. {
  1074. if(buf[i] != OutsideKeyBuf[i])
  1075. {
  1076. printf("cbc模式 24字节key enc出错 i = %d\n", i);
  1077. return 552;
  1078. }
  1079. }
  1080. //31 权限验证,3DES运算函数传参,用户权限下进行解密运算,返回成功
  1081. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  1082. ret_status = Dongle_3DES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 24);
  1083. if(ret_status != DONGLE_SUCCESS)
  1084. {
  1085. printf("错误处553\n");
  1086. return 553;
  1087. }
  1088. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  1089. ret_status = Dongle_3DES_Crypt_ByOutsideKey(hDongle, key3, 24, 0x11, OutsideKeyPlv, OutsideKeyBuf, 24);
  1090. if(ret_status != DONGLE_SUCCESS)
  1091. {
  1092. printf("错误处554\n");
  1093. return 554;
  1094. }
  1095. //32 cbc模式 24字节key 外库解密
  1096. memset(mbedtlsPlain, 0, 24);
  1097. mbedtls_des3_set3key_dec(&des3_ctx, key3);
  1098. for(i = 0; i < 3; i++)
  1099. {
  1100. mbedtls_des3_crypt_cbc( &des3_ctx, MBEDTLS_DES_DECRYPT, 8, mbedtls_dec_pLv3, dec_plain + (8*i), mbedtlsPlain + (8*i));
  1101. }
  1102. //外库与锁内 解密验证
  1103. for(i = 0; i < 24; i++)
  1104. {
  1105. if(buf[i] != mbedtlsPlain[i])
  1106. {
  1107. printf("cbc模式 24字节key dec出错 i = %d\n", i);
  1108. return 555;
  1109. }
  1110. }
  1111. //锁内与锁外 解密验证
  1112. for(i = 0; i < 24; i++)
  1113. {
  1114. if(buf[i] != OutsideKeyBuf[i])
  1115. {
  1116. printf("cbc模式 24字节key dec出错 i = %d\n", i);
  1117. return 556;
  1118. }
  1119. }
  1120. Dongle_Close(hDongle);
  1121. return 0;
  1122. }
  1123. //6
  1124. int test_userID()
  1125. {
  1126. DONGLE_HANDLE hDongle;
  1127. unsigned long did = 0XFFFFFFFF;
  1128. unsigned short count = 0;
  1129. unsigned long ret_status = 0;
  1130. unsigned char pTryCount;
  1131. unsigned char pUID[8] = {1,2,3,4,5,6,7,8};
  1132. unsigned char get_pUID[8] = {0};
  1133. unsigned char bySN[8] = {0};
  1134. unsigned char upin[8] = {0};
  1135. unsigned char dpin[24] = {0};
  1136. int i = 0;
  1137. for(i = 0; i < 24; i++)
  1138. {
  1139. dpin[i] = 0xFF;
  1140. }
  1141. ret_status = Dongle_Find(did, &count);
  1142. ret_status = Dongle_Open(1, 0, &hDongle);
  1143. //设置用户ID号
  1144. //1验证匿名状态设置用户ID号,返回失败
  1145. ret_status = Dongle_ResetState(hDongle);
  1146. //ret_status = Dongle_SetUserID(hDongle, pUID);
  1147. if(ret_status == DONGLE_SUCCESS)
  1148. {
  1149. printf("错误处601\n");
  1150. return 601;
  1151. }
  1152. //2验证用户权限设置用户ID号,返回失败
  1153. ret_status = Dongle_ResetState(hDongle);
  1154. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1155. //ret_status = Dongle_SetUserID(hDongle, pUID);
  1156. if(ret_status == DONGLE_SUCCESS)
  1157. {
  1158. printf("错误处602\n");
  1159. return 602;
  1160. }
  1161. //3验证开发商权限设置用户ID号,返回成功
  1162. ret_status = Dongle_ResetState(hDongle);
  1163. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1164. //ret_status = Dongle_SetUserID(hDongle, pUID);
  1165. if(ret_status != DONGLE_SUCCESS)
  1166. {
  1167. printf("错误处603\n");
  1168. return 603;
  1169. }
  1170. //获取用户ID号
  1171. //4验证匿名状态获取用户ID号,返回成功
  1172. ret_status = Dongle_ResetState(hDongle);
  1173. //ret_status = Dongle_GetUserID(hDongle, get_pUID);
  1174. if(ret_status != DONGLE_SUCCESS)
  1175. {
  1176. printf("错误处604\n");
  1177. return 604;
  1178. }
  1179. //5验证匿名状态获取用户ID号的数据是否正确
  1180. for(i = 0; i < 8; i++)
  1181. {
  1182. if(get_pUID[i] != pUID[i])
  1183. {
  1184. printf("匿名状态获取用户ID号出错。\n");
  1185. return 605;
  1186. }
  1187. }
  1188. //6验证用户权限获取用户ID号,返回成功
  1189. ret_status = Dongle_ResetState(hDongle);
  1190. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1191. //ret_status = Dongle_GetUserID(hDongle, get_pUID);
  1192. if(ret_status != DONGLE_SUCCESS)
  1193. {
  1194. printf("错误处606\n");
  1195. return 606;
  1196. }
  1197. //7验证用户权限获取用户ID号的数据是否正确
  1198. for(i = 0; i < 8; i++)
  1199. {
  1200. if(get_pUID[i] != pUID[i])
  1201. {
  1202. printf("用户权限获取用户ID号出错。\n");
  1203. return 607;
  1204. }
  1205. }
  1206. //8验证开发商权限获取用户ID号,返回成功
  1207. ret_status = Dongle_ResetState(hDongle);
  1208. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1209. // ret_status = Dongle_GetUserID(hDongle, get_pUID);
  1210. if(ret_status != DONGLE_SUCCESS)
  1211. {
  1212. printf("错误处608\n");
  1213. return 608;
  1214. }
  1215. //9验证开发商权限获取用户ID号的数据是否正确
  1216. for(i = 0; i < 8; i++)
  1217. {
  1218. if(get_pUID[i] != pUID[i])
  1219. {
  1220. printf("开发商权限获取用户ID号出错。\n");
  1221. return 609;
  1222. }
  1223. }
  1224. //获取设备硬件序列号
  1225. //10验证匿名状态获取设备硬件序列号,返回成功
  1226. ret_status = Dongle_ResetState(hDongle);
  1227. ret_status = Dongle_GetSerialNumber(hDongle, bySN);
  1228. if(ret_status != DONGLE_SUCCESS)
  1229. {
  1230. printf("错误处610\n");
  1231. return 610;
  1232. }
  1233. //11验证用户权限获取设备硬件序列号,返回成功
  1234. ret_status = Dongle_ResetState(hDongle);
  1235. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1236. ret_status = Dongle_GetSerialNumber(hDongle, bySN);
  1237. if(ret_status != DONGLE_SUCCESS)
  1238. {
  1239. printf("错误处611\n");
  1240. return 611;
  1241. }
  1242. //9验证开发商权限获取设备硬件序列号,返回成功
  1243. ret_status = Dongle_ResetState(hDongle);
  1244. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1245. ret_status = Dongle_GetSerialNumber(hDongle, bySN);
  1246. if(ret_status != DONGLE_SUCCESS)
  1247. {
  1248. printf("错误处612\n");
  1249. return 612;
  1250. }
  1251. Dongle_Close(hDongle);
  1252. return 0;
  1253. }
  1254. //7
  1255. int test_moduleCount()
  1256. {
  1257. DONGLE_HANDLE hDongle;
  1258. unsigned long did = 0XFFFFFFFF;
  1259. unsigned short count = 0;
  1260. unsigned long ret_status = 0;
  1261. unsigned char pTryCount;
  1262. unsigned char upin[8] = {0};
  1263. unsigned char dpin[24] = {0};
  1264. int i = 0;
  1265. for(i = 0; i < 24; i++)
  1266. {
  1267. dpin[i] = 0xFF;
  1268. }
  1269. unsigned long pwCount = 0;
  1270. int pbLic = 0;
  1271. ret_status = Dongle_Find(did, &count);
  1272. ret_status = Dongle_Open(1, 0, &hDongle);
  1273. //设置
  1274. //1验证匿名状态设置指定计次模块的模块值,权限不够,返回失败
  1275. ret_status = Dongle_ResetState(hDongle);
  1276. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1277. ret_status = Dongle_SetModuleCount(hDongle, 0, 1, 0);
  1278. if(ret_status == DONGLE_SUCCESS)
  1279. {
  1280. printf("错误处701\n");
  1281. return 701;
  1282. }
  1283. //2验证用户权限设置指定计次模块的模块值,权限不够,返回失败
  1284. ret_status = Dongle_ResetState(hDongle);
  1285. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1286. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1287. ret_status = Dongle_SetModuleCount(hDongle, 0, 1, 0);
  1288. if(ret_status == DONGLE_SUCCESS)
  1289. {
  1290. printf("错误处702\n");
  1291. return 702;
  1292. }
  1293. //3验证开发商权限设置指定计次模块的模块值,模块号为0,返回成功
  1294. ret_status = Dongle_ResetState(hDongle);
  1295. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1296. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1297. ret_status = Dongle_SetModuleCount(hDongle, 0, 65535, 0);
  1298. if(ret_status != DONGLE_SUCCESS)
  1299. {
  1300. printf("错误处703\n");
  1301. return 703;
  1302. }
  1303. //4验证开发商权限设置指定计次模块的模块值,参数二传入错误,返回失败
  1304. ret_status = Dongle_ResetState(hDongle);
  1305. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1306. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1307. ret_status = Dongle_SetModuleCount(hDongle, 4, 0, 0);
  1308. if(ret_status == DONGLE_SUCCESS)
  1309. {
  1310. printf("错误处704\n");
  1311. return 704;
  1312. }
  1313. //问题1
  1314. //5验证开发商权限设置指定计次模块的模块值,参数三传入错误,返回失败
  1315. ret_status = Dongle_ResetState(hDongle);
  1316. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1317. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1318. ret_status = Dongle_SetModuleCount(hDongle, 0, 0, 0);
  1319. if(ret_status == DONGLE_SUCCESS)
  1320. {
  1321. printf("错误处705\n");
  1322. //return 705;
  1323. }
  1324. //问题2
  1325. //6验证开发商权限设置指定计次模块的模块值,参数三传入错误,返回失败
  1326. ret_status = Dongle_ResetState(hDongle);
  1327. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1328. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1329. ret_status = Dongle_SetModuleCount(hDongle, 0, 65536, 0);
  1330. if(ret_status == DONGLE_SUCCESS)
  1331. {
  1332. printf("错误处706\n");
  1333. //return 706;
  1334. }
  1335. //问题3
  1336. //7验证开发商权限设置指定计次模块的模块值,参数四传入错误,返回失败
  1337. ret_status = Dongle_ResetState(hDongle);
  1338. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1339. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1340. ret_status = Dongle_SetModuleCount(hDongle, 0, 0, 3);
  1341. if(ret_status == DONGLE_SUCCESS)
  1342. {
  1343. printf("错误处707\n");
  1344. //return 707;
  1345. }
  1346. //8验证开发商权限设置指定计次模块的模块值,模块号为1,返回成功
  1347. ret_status = Dongle_ResetState(hDongle);
  1348. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1349. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1350. ret_status = Dongle_SetModuleCount(hDongle, 1, 65536, 1);
  1351. if(ret_status != DONGLE_SUCCESS)
  1352. {
  1353. printf("错误处708\n");
  1354. return 708;
  1355. }
  1356. //9验证开发商权限设置指定计次模块的模块值,模块号为2,返回成功
  1357. ret_status = Dongle_ResetState(hDongle);
  1358. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1359. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1360. ret_status = Dongle_SetModuleCount(hDongle, 2, 65536, 2);
  1361. if(ret_status != DONGLE_SUCCESS)
  1362. {
  1363. printf("错误处709\n");
  1364. return 709;
  1365. }
  1366. //10验证开发商权限设置指定计次模块的模块值,模块号为3,返回成功
  1367. ret_status = Dongle_ResetState(hDongle);
  1368. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1369. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1370. ret_status = Dongle_SetModuleCount(hDongle, 3, 65536, 0);
  1371. if(ret_status != DONGLE_SUCCESS)
  1372. {
  1373. printf("错误处710\n");
  1374. return 710;
  1375. }
  1376. //获取
  1377. //12验证匿名状态获取指定计次模块的模块值,权限不够,返回失败
  1378. ret_status = Dongle_ResetState(hDongle);
  1379. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1380. ret_status = Dongle_GetModuleCount(hDongle, 0, &pwCount, (BYTE *)&pbLic);
  1381. if(ret_status == DONGLE_SUCCESS)
  1382. {
  1383. printf("错误处712\n");
  1384. return 712;
  1385. }
  1386. //13验证用户权限获取指定计次模块的模块值,返回成功
  1387. ret_status = Dongle_ResetState(hDongle);
  1388. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1389. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1390. ret_status = Dongle_GetModuleCount(hDongle, 1, &pwCount, (BYTE *)&pbLic);
  1391. if(ret_status != DONGLE_SUCCESS)
  1392. {
  1393. printf("错误处713\n");
  1394. return 713;
  1395. }
  1396. //14验证开发商权限获取指定计次模块的模块值,返回成功
  1397. ret_status = Dongle_ResetState(hDongle);
  1398. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1399. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1400. ret_status = Dongle_GetModuleCount(hDongle, 2, &pwCount, (BYTE *)&pbLic);
  1401. if(ret_status != DONGLE_SUCCESS)
  1402. {
  1403. printf("错误处714\n");
  1404. return 714;
  1405. }
  1406. //15验证用户权限获取指定计次模块的模块值,参数二传入错误,返回失败
  1407. ret_status = Dongle_ResetState(hDongle);
  1408. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1409. //模块号,计次模块值(范围为 1~65535),模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  1410. ret_status = Dongle_GetModuleCount(hDongle, 4, &pwCount, (BYTE *)&pbLic);
  1411. if(ret_status == DONGLE_SUCCESS)
  1412. {
  1413. printf("错误处715\n");
  1414. return 715;
  1415. }
  1416. //递减
  1417. //16验证匿名状态递减指定计次模块的模块值,权限不够,返回失败
  1418. ret_status = Dongle_ResetState(hDongle);
  1419. //模块号
  1420. ret_status = Dongle_DecModuleCount(hDongle, 0);
  1421. if(ret_status == DONGLE_SUCCESS)
  1422. {
  1423. printf("错误处716\n");
  1424. return 716;
  1425. }
  1426. //17验证用户权限递减指定计次模块的模块值,返回成功
  1427. ret_status = Dongle_ResetState(hDongle);
  1428. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1429. //模块号
  1430. ret_status = Dongle_DecModuleCount(hDongle, 0);
  1431. if(ret_status != DONGLE_SUCCESS)
  1432. {
  1433. printf("错误处717\n");
  1434. return 717;
  1435. }
  1436. //18验证开发商权限递减指定计次模块的模块值,返回成功
  1437. ret_status = Dongle_ResetState(hDongle);
  1438. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1439. //模块号
  1440. ret_status = Dongle_DecModuleCount(hDongle, 0);
  1441. if(ret_status != DONGLE_SUCCESS)
  1442. {
  1443. printf("错误处718\n");
  1444. return 718;
  1445. }
  1446. //问题4
  1447. //19验证用户权限递减指定计次模块的模块值,参数二传入错误,返回失败
  1448. ret_status = Dongle_ResetState(hDongle);
  1449. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1450. //模块号
  1451. ret_status = Dongle_DecModuleCount(hDongle, 1);
  1452. if(ret_status == DONGLE_SUCCESS)
  1453. {
  1454. printf("错误处719\n");
  1455. //return 719;
  1456. }
  1457. //问题5
  1458. //20验证用户权限递减指定计次模块的模块值,参数二传入错误,返回失败
  1459. ret_status = Dongle_ResetState(hDongle);
  1460. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1461. //模块号
  1462. ret_status = Dongle_DecModuleCount(hDongle, 2);
  1463. if(ret_status == DONGLE_SUCCESS)
  1464. {
  1465. printf("错误处720\n");
  1466. //return 720;
  1467. }
  1468. Dongle_Close(hDongle);
  1469. return 0;
  1470. }
  1471. void dump_buf(const char *title, unsigned char *buf, size_t len)
  1472. {
  1473. size_t i;
  1474. printf("%s", title);
  1475. for (i = 0; i < len; i++)
  1476. printf("%c%c", "0123456789ABCDEF"[buf[i] / 16],
  1477. "0123456789ABCDEF"[buf[i] % 16]);
  1478. printf("\n");
  1479. }
  1480. //8
  1481. int test_seed_sha1_getRandom()
  1482. {
  1483. DONGLE_HANDLE hDongle;
  1484. unsigned long did = 0XFFFFFFFF;
  1485. unsigned short count = 0;
  1486. unsigned long ret_status = 0;
  1487. unsigned char pTryCount;
  1488. mbedtls_sha1_context sha1_1ctx;
  1489. mbedtls_sha1_context sha1_2ctx;
  1490. mbedtls_sha256_context sha256_1ctx;
  1491. mbedtls_sha256_context sha256_2ctx;
  1492. mbedtls_sha512_context sha512_1ctx;
  1493. mbedtls_sha512_context sha512_2ctx;
  1494. unsigned char upin[8] = {0};
  1495. unsigned char dpin[24] = {0};
  1496. int i = 0;
  1497. for(i = 0; i < 24; i++)
  1498. {
  1499. dpin[i] = 0xFF;
  1500. }
  1501. //sha1摘要算法
  1502. unsigned char pSeed1[1024] = {0,1,2,3,4,5,6,7,8,9};
  1503. unsigned char mbedtls_input1[1024] = {0,1,2,3,4,5,6,7,8,9};
  1504. //unsigned char pSeed1[25] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5};
  1505. //unsigned char mbedtls_input1[25] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5};
  1506. unsigned char pSeed2[1] = {1};
  1507. unsigned char mbedtls_input2[1] = {1};
  1508. unsigned char pSeed3[1025] = {0,1,2,3};
  1509. unsigned char pSeed4[25] = {0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,1,2,3,4,5};
  1510. //sha1摘要算法 输出
  1511. unsigned char pSeedResult1[20] = {0};
  1512. unsigned char pSeedResult2[32] = {0};
  1513. unsigned char pSeedResult3[64] = {0};
  1514. unsigned char mbedtls_output1[20] = {0};
  1515. unsigned char mbedtls_output2[32] = {0};
  1516. unsigned char mbedtls_output3[64] = {0};
  1517. //种子码运算
  1518. unsigned char pSeedOrResult[255] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5};
  1519. int bSeedLen = 255;
  1520. unsigned char pSeedOrResult1[1025] = {0};
  1521. int bSeedLen1 = 1025;
  1522. //产生一个指定长度的随机数
  1523. unsigned char pRandom[255] = {0};
  1524. unsigned char pRandom1[256] = {0};
  1525. ret_status = Dongle_Find(did, &count);
  1526. ret_status = Dongle_Open(1, 0, &hDongle);
  1527. //种子码运算
  1528. //1验证匿名状态设置种子码运算,权限不够,返回失败
  1529. ret_status = Dongle_ResetState(hDongle);
  1530. ret_status = Dongle_Seed(hDongle, pSeedOrResult, bSeedLen);
  1531. if(ret_status == DONGLE_SUCCESS)
  1532. {
  1533. printf("错误处801\n");
  1534. return 801;
  1535. }
  1536. //2验证用户权限设置种子码运算,返回成功
  1537. ret_status = Dongle_ResetState(hDongle);
  1538. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1539. ret_status = Dongle_Seed(hDongle, pSeedOrResult, bSeedLen);
  1540. if(ret_status != DONGLE_SUCCESS)
  1541. {
  1542. printf("错误处802\n");
  1543. return 802;
  1544. }
  1545. //3验证开发商权限设置种子码运算,返回成功
  1546. ret_status = Dongle_ResetState(hDongle);
  1547. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1548. ret_status = Dongle_Seed(hDongle, pSeedOrResult, bSeedLen);
  1549. if(ret_status != DONGLE_SUCCESS)
  1550. {
  1551. printf("错误处803\n");
  1552. return 803;
  1553. }
  1554. //4验证用户权限设置种子码运算,参数三传入错误,返回失败
  1555. ret_status = Dongle_ResetState(hDongle);
  1556. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1557. ret_status = Dongle_Seed(hDongle, pSeedOrResult, 0);
  1558. if(ret_status == DONGLE_SUCCESS)
  1559. {
  1560. printf("错误处804\n");
  1561. return 804;
  1562. }
  1563. //5验证用户权限设置种子码运算,参数三传入错误,返回失败
  1564. ret_status = Dongle_ResetState(hDongle);
  1565. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1566. ret_status = Dongle_Seed(hDongle, pSeedOrResult1, bSeedLen1);
  1567. if(ret_status == DONGLE_SUCCESS)
  1568. {
  1569. printf("错误处805\n");
  1570. return 805;
  1571. }
  1572. //Sha1摘要算法
  1573. mbedtls_sha1_init( &sha1_1ctx );
  1574. mbedtls_sha1_starts( &sha1_1ctx );
  1575. mbedtls_sha1_init( &sha1_2ctx );
  1576. mbedtls_sha1_starts( &sha1_2ctx );
  1577. mbedtls_sha256_init( &sha256_1ctx );
  1578. mbedtls_sha256_starts( &sha256_1ctx, 0);
  1579. mbedtls_sha256_init( &sha256_2ctx );
  1580. mbedtls_sha256_starts( &sha256_2ctx, 0);
  1581. mbedtls_sha512_init( &sha512_1ctx );
  1582. mbedtls_sha512_starts( &sha512_1ctx, 0);
  1583. mbedtls_sha512_init( &sha512_2ctx );
  1584. mbedtls_sha512_starts( &sha512_2ctx, 0);
  1585. //6验证匿名状态设置Sha1摘要算法,参数2传入错误, 返回失败
  1586. ret_status = Dongle_ResetState(hDongle);
  1587. //运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512 输出数据缓冲区。20字节、32字节、64字节。
  1588. ret_status = Dongle_Sha(hDongle, 0, pSeed4, 25, pSeedResult1);
  1589. if(ret_status == DONGLE_SUCCESS)
  1590. {
  1591. printf("错误处806\n");
  1592. //return 806;
  1593. }
  1594. //7验证匿名状态设置Sha1摘要算法,参数2传入错误, 返回失败
  1595. ret_status = Dongle_ResetState(hDongle);
  1596. //运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512 输出数据缓冲区。20字节、32字节、64字节。
  1597. ret_status = Dongle_Sha(hDongle, 4, pSeed4, 25, pSeedResult1);
  1598. if(ret_status == DONGLE_SUCCESS)
  1599. {
  1600. printf("错误处807\n");
  1601. //return 807;
  1602. }
  1603. //8验证匿名状态设置Sha1摘要算法,参数4传入错误, 返回失败
  1604. ret_status = Dongle_ResetState(hDongle);
  1605. //运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512 输出数据缓冲区。20字节、32字节、64字节。
  1606. ret_status = Dongle_Sha(hDongle, 0x01, pSeed4, 0, pSeedResult1);
  1607. if(ret_status == DONGLE_SUCCESS)
  1608. {
  1609. printf("错误处808\n");
  1610. //return 808;
  1611. }
  1612. /*
  1613. //9验证匿名状态设置Sha1摘要算法,参数4传入错误, 返回失败
  1614. ret_status = Dongle_ResetState(hDongle);
  1615. //运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512 输出数据缓冲区。20字节、32字节、64字节。
  1616. ret_status = Dongle_Sha(hDongle, 0x01, pSeed3, 1025, pSeedResult1);
  1617. if(ret_status == DONGLE_SUCCESS)
  1618. {
  1619. printf("错误处809\n");
  1620. //0xf0006700
  1621. //return 809;
  1622. }
  1623. */
  1624. //10验证用户权限设置Sha1摘要算法,返回成功
  1625. memset(pSeedResult1, 0, 20);
  1626. ret_status = Dongle_ResetState(hDongle);
  1627. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1628. ret_status = Dongle_Sha(hDongle, 0x01, pSeed1, 1024, pSeedResult1);
  1629. if(ret_status != DONGLE_SUCCESS)
  1630. {
  1631. printf("错误处810\n");
  1632. return 810;
  1633. }
  1634. //11验证开发商权限设置Sha1摘要算法,返回成功
  1635. memset(pSeedResult1, 0, 20);
  1636. ret_status = Dongle_ResetState(hDongle);
  1637. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1638. ret_status = Dongle_Sha(hDongle, 0x01, pSeed1, 1024, pSeedResult1);
  1639. if(ret_status != DONGLE_SUCCESS)
  1640. {
  1641. printf("错误处811\n");
  1642. return 811;
  1643. }
  1644. //一 SHA-1 摘要算法外库验证
  1645. //1 摘要数据为1个字节
  1646. ret_status = Dongle_ResetState(hDongle);
  1647. memset(pSeedResult1, 0, 20);
  1648. memset(mbedtls_output1, 0, 20);
  1649. ret_status = Dongle_Sha(hDongle, 0x01, pSeed2, 1, pSeedResult1);
  1650. if(ret_status != DONGLE_SUCCESS)
  1651. {
  1652. printf("错误处812\n");
  1653. return 812;
  1654. }
  1655. dump_buf("result0:", pSeedResult1, 20);
  1656. mbedtls_sha1_update( &sha1_1ctx, mbedtls_input2, 1);
  1657. mbedtls_sha1_finish( &sha1_1ctx, mbedtls_output1);
  1658. dump_buf("result1:", mbedtls_output1, 20);
  1659. //摘要算法外库验证
  1660. for(i = 0; i < 20; i++)
  1661. {
  1662. if(pSeedResult1[i] != mbedtls_output1[i])
  1663. {
  1664. printf("SHA-1 摘要数据为1个字节算法外库验证出错 i = %d\n", i);
  1665. return 813;
  1666. }
  1667. }
  1668. printf("\n\n");
  1669. mbedtls_sha1_free( &sha1_1ctx );
  1670. //2 摘要数据为1024个字节
  1671. ret_status = Dongle_ResetState(hDongle);
  1672. memset(pSeedResult1, 0, 20);
  1673. memset(mbedtls_output1, 0, 20);
  1674. ret_status = Dongle_Sha(hDongle, 0x01, pSeed1, 1024, pSeedResult1);
  1675. if(ret_status != DONGLE_SUCCESS)
  1676. {
  1677. printf("错误处814\n");
  1678. return 814;
  1679. }
  1680. dump_buf("result2:", pSeedResult1, 20);
  1681. mbedtls_sha1_update( &sha1_2ctx, mbedtls_input1, 1024);
  1682. mbedtls_sha1_finish( &sha1_2ctx, mbedtls_output1);
  1683. dump_buf("result3:", mbedtls_output1, 20);
  1684. //摘要算法外库验证
  1685. for(i = 0; i < 20; i++)
  1686. {
  1687. if(pSeedResult1[i] != mbedtls_output1[i])
  1688. {
  1689. printf("SHA-1 摘要数据为1024个字节算法外库验证出错 i = %d\n", i);
  1690. return 815;
  1691. }
  1692. }
  1693. mbedtls_sha1_free( &sha1_2ctx );
  1694. //二 SHA-256 摘要算法外库验证
  1695. //1 摘要数据为1个字节
  1696. ret_status = Dongle_Sha(hDongle, 0x02, pSeed2, 1, pSeedResult2);
  1697. if(ret_status != DONGLE_SUCCESS)
  1698. {
  1699. printf("错误处816\n");
  1700. return 816;
  1701. }
  1702. mbedtls_sha256_update( &sha256_1ctx, mbedtls_input2, 1 );
  1703. mbedtls_sha256_finish( &sha256_1ctx, mbedtls_output2);
  1704. //摘要算法外库验证
  1705. for(i = 0; i < 32; i++)
  1706. {
  1707. if(pSeedResult2[i] != mbedtls_output2[i])
  1708. {
  1709. printf("SHA-256 1个字节摘要算法外库验证出错 i = %d\n", i);
  1710. return 817;
  1711. }
  1712. }
  1713. mbedtls_sha256_free( &sha256_1ctx );
  1714. //2 摘要数据为1024个字节
  1715. ret_status = Dongle_Sha(hDongle, 0x02, pSeed1, 1024, pSeedResult2);
  1716. if(ret_status != DONGLE_SUCCESS)
  1717. {
  1718. printf("错误处818\n");
  1719. return 818;
  1720. }
  1721. mbedtls_sha256_update( &sha256_2ctx, mbedtls_input1, 1024 );
  1722. mbedtls_sha256_finish( &sha256_2ctx, mbedtls_output2);
  1723. //摘要算法外库验证
  1724. for(i = 0; i < 32; i++)
  1725. {
  1726. if(pSeedResult2[i] != mbedtls_output2[i])
  1727. {
  1728. printf("SHA-256 1024个字节摘要算法外库验证出错 i = %d\n", i);
  1729. return 819;
  1730. }
  1731. }
  1732. mbedtls_sha256_free( &sha256_2ctx );
  1733. //三 SHA-512 摘要算法外库验证
  1734. //1 摘要数据为1个字节
  1735. ret_status = Dongle_Sha(hDongle, 0x03, pSeed2, 1, pSeedResult3);
  1736. if(ret_status != DONGLE_SUCCESS)
  1737. {
  1738. printf("错误处820\n");
  1739. return 820;
  1740. }
  1741. mbedtls_sha512_update( &sha512_1ctx, mbedtls_input2, 1 );
  1742. mbedtls_sha512_finish( &sha512_1ctx, mbedtls_output3);
  1743. //摘要算法外库验证
  1744. for(i = 0; i < 64; i++)
  1745. {
  1746. if(pSeedResult2[i] != mbedtls_output2[i])
  1747. {
  1748. printf("SHA-512 1个字节摘要算法外库验证出错 i = %d\n", i);
  1749. return 821;
  1750. }
  1751. }
  1752. mbedtls_sha512_free( &sha512_1ctx );
  1753. //2 摘要数据为1024个字节
  1754. ret_status = Dongle_Sha(hDongle, 0x02, pSeed1, 1024, pSeedResult3);
  1755. if(ret_status != DONGLE_SUCCESS)
  1756. {
  1757. printf("错误处822\n");
  1758. return 822;
  1759. }
  1760. mbedtls_sha512_update( &sha512_2ctx, mbedtls_input1, 1024 );
  1761. mbedtls_sha512_finish( &sha512_2ctx, mbedtls_output3);
  1762. //摘要算法外库验证
  1763. for(i = 0; i < 64; i++)
  1764. {
  1765. if(pSeedResult2[i] != mbedtls_output2[i])
  1766. {
  1767. printf("SHA-512 1024个字节摘要算法外库验证出错 i = %d\n", i);
  1768. return 823;
  1769. }
  1770. }
  1771. mbedtls_sha512_free( &sha512_2ctx );
  1772. //随机数
  1773. //12验证匿名状态产生一个指定长度的随机数,参数传入错误, 返回失败
  1774. ret_status = Dongle_ResetState(hDongle);
  1775. //随机数长度。取值范围为0--255字节。
  1776. ret_status = Dongle_Get_Random(hDongle, 256, pRandom1);
  1777. if(ret_status == DONGLE_SUCCESS)
  1778. {
  1779. printf("错误处824\n");
  1780. //return 824;
  1781. }
  1782. //13验证匿名状态产生一个指定长度的随机数,返回成功
  1783. ret_status = Dongle_ResetState(hDongle);
  1784. ret_status = Dongle_Get_Random(hDongle, 255, pRandom);
  1785. if(ret_status != DONGLE_SUCCESS)
  1786. {
  1787. printf("错误处825\n");
  1788. return 825;
  1789. }
  1790. //14验证用户权限产生一个指定长度的随机数,返回成功
  1791. ret_status = Dongle_ResetState(hDongle);
  1792. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1793. ret_status = Dongle_Get_Random(hDongle, 255, pRandom);
  1794. if(ret_status != DONGLE_SUCCESS)
  1795. {
  1796. printf("错误处826\n");
  1797. return 826;
  1798. }
  1799. //15验证开发商权限产生一个指定长度的随机数,返回成功
  1800. ret_status = Dongle_ResetState(hDongle);
  1801. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1802. ret_status = Dongle_Get_Random(hDongle, 255, pRandom);
  1803. if(ret_status != DONGLE_SUCCESS)
  1804. {
  1805. printf("错误处827\n");
  1806. return 827;
  1807. }
  1808. Dongle_Close(hDongle);
  1809. return 0;
  1810. }
  1811. static int myrand( void *rng_state, unsigned char *output, size_t len )
  1812. {
  1813. #if !defined(__OpenBSD__)
  1814. size_t i;
  1815. if( rng_state != NULL )
  1816. rng_state = NULL;
  1817. for( i = 0; i < len; ++i )
  1818. output[i] = rand();
  1819. #else
  1820. if( rng_state != NULL )
  1821. rng_state = NULL;
  1822. arc4random_buf( output, len );
  1823. #endif /* !OpenBSD */
  1824. return( 0 );
  1825. }
  1826. //9
  1827. int test_RSA()
  1828. {
  1829. DONGLE_HANDLE hDongle;
  1830. unsigned long did = 0XFFFFFFFF;
  1831. unsigned short count = 0;
  1832. unsigned long ret_status = 0;
  1833. unsigned char pTryCount;
  1834. unsigned char upin[8] = {0};
  1835. unsigned char dpin[24] = {0};
  1836. int i = 0;
  1837. for(i = 0; i < 24; i++)
  1838. {
  1839. dpin[i] = 0xFF;
  1840. }
  1841. RSA_KEY pKey;
  1842. unsigned char buf1[128] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,2,2,2,2};
  1843. unsigned char buf2[256] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,3,3,3,3};
  1844. //1024位密钥
  1845. unsigned char plain1[128] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,2,2,2,2};
  1846. unsigned char dec_plain1[128] = {0};
  1847. unsigned char mbedtlsPlain1[128] = {0};
  1848. //2048位密钥
  1849. unsigned char plain2[256] = {1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,3,3,3,3};
  1850. unsigned char dec_plain2[256] = {0};
  1851. unsigned char mbedtlsPlain2[256] = {0};
  1852. int ret_mbedtls = 0;
  1853. mbedtls_rsa_context rsa_ctx;
  1854. mbedtls_rsa_init( &rsa_ctx, 0, 0);
  1855. ret_status = Dongle_Find(did, &count);
  1856. ret_status = Dongle_Open(1, 0, &hDongle);
  1857. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1858. //设置RSA加解密密钥
  1859. ret_mbedtls = mbedtls_rsa_gen_key( &rsa_ctx, myrand, NULL, 1024, 65537);
  1860. memset(pKey.n, 0, 256);
  1861. memset(pKey.e, 0, 4);
  1862. memset(pKey.p, 0, 128);
  1863. memset(pKey.q, 0, 128);
  1864. memset(pKey.dp, 0, 128);
  1865. memset(pKey.dq, 0, 128);
  1866. memset(pKey.InvQ, 0, 128);
  1867. pKey.len = 0x0400;
  1868. mbedtls_mpi_write_binary(&rsa_ctx.N , pKey.n, 128);
  1869. mbedtls_mpi_write_binary(&rsa_ctx.E , pKey.e, 4);
  1870. mbedtls_mpi_write_binary(&rsa_ctx.P , pKey.p, 64);
  1871. mbedtls_mpi_write_binary(&rsa_ctx.Q , pKey.q, 64);
  1872. mbedtls_mpi_write_binary(&rsa_ctx.DP , pKey.dp, 64);
  1873. mbedtls_mpi_write_binary(&rsa_ctx.DQ , pKey.dq, 64);
  1874. mbedtls_mpi_write_binary(&rsa_ctx.QP , pKey.InvQ, 64);
  1875. //1验证匿名状态 设置RSA加解密密钥,权限不够, 返回失败
  1876. ret_status = Dongle_ResetState(hDongle);
  1877. ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
  1878. if(ret_status == DONGLE_SUCCESS)
  1879. {
  1880. printf("错误处901\n");
  1881. return 901;
  1882. }
  1883. //2验证用户权限 设置RSA加解密密钥,权限不够, 返回失败
  1884. ret_status = Dongle_ResetState(hDongle);
  1885. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1886. ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
  1887. if(ret_status == DONGLE_SUCCESS)
  1888. {
  1889. printf("错误处902\n");
  1890. return 902;
  1891. }
  1892. //3验证开发商权限 设置RSA加解密密钥,参数传入正确, 返回成功
  1893. ret_status = Dongle_ResetState(hDongle);
  1894. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1895. ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
  1896. if(ret_status != DONGLE_SUCCESS)
  1897. {
  1898. printf("错误处903\n");
  1899. return 903;
  1900. }
  1901. //4验证开发商权限 设置RSA加解密密钥,参数二传入错误, 返回失败
  1902. ret_status = Dongle_SetRSAKey(hDongle, 1, &pKey);
  1903. if(ret_status == DONGLE_SUCCESS)
  1904. {
  1905. printf("错误处904\n");
  1906. return 904;
  1907. }
  1908. //生成RSA密钥对
  1909. //5验证匿名状态 生成RSA密钥对,权限不够, 返回失败
  1910. ret_status = Dongle_ResetState(hDongle);
  1911. //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
  1912. ret_status = Dongle_GenRSAKey(hDongle, 0, 0x02, 0x01, &pKey);
  1913. if(ret_status == DONGLE_SUCCESS)
  1914. {
  1915. printf("错误处905\n");
  1916. //return 905;
  1917. }
  1918. //6验证用户权限 生成RSA密钥对,权限不够, 返回失败
  1919. ret_status = Dongle_ResetState(hDongle);
  1920. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1921. //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
  1922. ret_status = Dongle_GenRSAKey(hDongle, 0, 0x02, 0x01, &pKey);
  1923. if(ret_status == DONGLE_SUCCESS)
  1924. {
  1925. printf("错误处906\n");
  1926. //return 906;
  1927. }
  1928. //7验证开发商权限 生成RSA密钥对,参数传入正确, 返回成功
  1929. ret_status = Dongle_ResetState(hDongle);
  1930. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1931. //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
  1932. ret_status = Dongle_GenRSAKey(hDongle, 0, 0x02, 0x01, &pKey);
  1933. if(ret_status != DONGLE_SUCCESS)
  1934. {
  1935. printf("错误处907\n");
  1936. return 907;
  1937. }
  1938. //8验证开发商权限 生成RSA密钥对,参数二传入错误, 返回失败
  1939. ret_status = Dongle_ResetState(hDongle);
  1940. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1941. //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
  1942. ret_status = Dongle_GenRSAKey(hDongle, 1, 0x02, 0x01, &pKey);
  1943. if(ret_status == DONGLE_SUCCESS)
  1944. {
  1945. printf("错误处908\n");
  1946. return 908;
  1947. }
  1948. //9验证开发商权限 生成RSA密钥对,参数三传入错误, 返回失败
  1949. ret_status = Dongle_ResetState(hDongle);
  1950. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1951. //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
  1952. ret_status = Dongle_GenRSAKey(hDongle, 1, 0x01, 0x01, &pKey);
  1953. if(ret_status == DONGLE_SUCCESS)
  1954. {
  1955. printf("错误处909\n");
  1956. return 909;
  1957. }
  1958. //10验证开发商权限 生成RSA密钥对,参数四传入错误, 返回失败
  1959. ret_status = Dongle_ResetState(hDongle);
  1960. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1961. //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
  1962. ret_status = Dongle_GenRSAKey(hDongle, 1, 0x01, 0x10, &pKey);
  1963. if(ret_status == DONGLE_SUCCESS)
  1964. {
  1965. printf("错误处910\n");
  1966. return 910;
  1967. }
  1968. //RSA运算
  1969. //1024位密钥 RSA运算
  1970. ret_status = Dongle_ResetState(hDongle);
  1971. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  1972. //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
  1973. ret_status = Dongle_GenRSAKey(hDongle, 0, 0x02, 0x01, &pKey);
  1974. //11验证匿名状态 进行RSA运算,权限不够, 返回失败
  1975. ret_status = Dongle_ResetState(hDongle);
  1976. // 运算类型。0x00公钥运算, 0x01私钥运算
  1977. ret_status = Dongle_RSA(hDongle, 0, 0x00, buf1, 128);
  1978. if(ret_status == DONGLE_SUCCESS)
  1979. {
  1980. printf("错误处911\n");
  1981. return 911;
  1982. }
  1983. // 运算类型。0x00公钥运算, 0x01私钥运算
  1984. ret_status = Dongle_RSA(hDongle, 0, 0x01, buf1, 128);
  1985. if(ret_status == DONGLE_SUCCESS)
  1986. {
  1987. printf("错误处912\n");
  1988. return 912;
  1989. }
  1990. //12验证用户权限 进行RSA运算,参数传入正确, 返回成功
  1991. ret_status = Dongle_ResetState(hDongle);
  1992. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  1993. // 运算类型。0x00公钥运算, 0x01私钥运算
  1994. ret_status = Dongle_RSA(hDongle, 0, 0x00, buf1, 128);
  1995. if(ret_status != DONGLE_SUCCESS)
  1996. {
  1997. printf("错误处913\n");
  1998. return 913;
  1999. }
  2000. // 运算类型。0x00公钥运算, 0x01私钥运算
  2001. ret_status = Dongle_RSA(hDongle, 0, 0x01, buf1, 128);
  2002. if(ret_status != DONGLE_SUCCESS)
  2003. {
  2004. printf("错误处914\n");
  2005. return 914;
  2006. }
  2007. //ret_mbedtls = mbedtls_rsa_gen_key( &rsa_ctx, myrand, NULL, 1024, 65537);
  2008. //一,1024位密钥 用自己的库产生的公私钥赋值给mbedtls中的公私钥
  2009. unsigned char pKey_n[128]= {0xD7,0x09,0x21,0xC9,0x2E,0xF9,0xF1,0x48,0x68,0x10,0x59,0x76,0x73,0xCD,0x16,0x31,0x9C,0x00,0x97,0xAD,0x59,0xDD,0x62,0x58,0xEC,0x4F,0x13,0xDA,0xD5,0x96,0x67,0x41,0xBA,0x9D,0x08,0x2B,0x65,0xBE,0x51,0x29,0x87,0xFA,0x54,0x21,0x3E,0x46,0x6F,0xF1,0x7A,0x3C,0xF7,0xDE,0x38,0xD3,0x56,0x76,0x9A,0xD7,0x81,0x13,0xB1,0x9D,0xE6,0xC8,0x01,0x64,0x3E,0x16,0x62,0x13,0x15,0x81,0x06,0x97,0x15,0xBC,0xCE,0x4C,0xC7,0x1B,0xDC,0x70,0xF0,0x61,0xB1,0x00,0xA2,0xB0,0x15,0x4D,0xDA,0xCF,0x9A,0x49,0x58,0x68,0x71,0x5C,0x36,0xFF,0x2B,0xC3,0xF8,0xCF,0xCC,0x91,0x94,0xA1,0xDD,0xA9,0xB0,0xDA,0x3D,0x69,0x48,0xC0,0xED,0x11,0xAF,0x81,0x40,0xAA,0x92,0x1D,0x05,0x55,0x92,0xBF};
  2010. unsigned char pKey_e[4] = {0x00,0x01,0x00,0x01};
  2011. unsigned char pKey_d[128] = {0xC2,0x1D,0x7B,0x43,0x5A,0xC5,0xC4,0x5B,0x21,0x9D,0xC8,0xB4,0xD6,0x5A,0x87,0xB3,0x66,0xE9,0x5E,0x02,0xDE,0x01,0xAC,0x5B,0x2B,0x8F,0x43,0x14,0x24,0x05,0x6E,0xD6,0xF5,0xF7,0xF5,0xBC,0xFF,0x4E,0xAB,0x83,0xBB,0x3D,0x6E,0x1E,0xC8,0x51,0xDB,0x91,0xF8,0x4F,0xFB,0x4D,0xBE,0xAC,0x80,0xFB,0xF6,0x51,0x9E,0x32,0xC1,0xFF,0x11,0xA9,0x48,0x71,0xC5,0xCB,0x70,0x41,0xE8,0x2F,0xA1,0x5F,0x9D,0xFF,0x6A,0xEC,0xEF,0x27,0x53,0xF0,0x88,0x00,0xCB,0x2A,0x5D,0xD7,0x31,0x49,0x3B,0x1F,0x03,0x43,0x61,0x0F,0x1A,0x25,0x2E,0xF8,0xB5,0x3F,0x75,0x62,0x60,0x09,0xF2,0x03,0xC0,0x32,0x5E,0x3B,0xA1,0x85,0x48,0x1A,0xB9,0x4C,0x89,0x0D,0xB6,0x93,0x27,0x12,0x29,0xD2,0x27,0xB9};
  2012. unsigned char pKey_p[64] = {0xDA,0x7A,0xF6,0xF3,0x0A,0xAF,0x6F,0x91,0xE7,0xD6,0x7D,0x83,0x7C,0xA8,0xC0,0x15,0x2E,0x0F,0x52,0x6C,0x81,0x2A,0x2C,0xFF,0x64,0x77,0x58,0x61,0x8F,0xFE,0x9E,0xDB,0x5B,0x3F,0x1A,0x66,0x4E,0x71,0xCE,0xB2,0x92,0x10,0x7D,0x8D,0xB5,0x83,0xF8,0xD8,0xCE,0xEB,0xE2,0xC0,0x3B,0x64,0x1E,0xC0,0x75,0xFE,0x07,0xD1,0xAD,0x69,0xE2,0x1B};
  2013. unsigned char pKey_q[64] = {0xFB,0xF6,0xBA,0xE3,0x9A,0x12,0xE7,0xCF,0x03,0xAF,0x4D,0x37,0x6C,0x87,0x3D,0x08,0x4D,0xAF,0xD6,0xF0,0xE0,0x53,0x40,0x48,0x23,0x13,0xED,0x7A,0x90,0xD2,0x73,0x18,0x0E,0x9A,0xD6,0x57,0x72,0x02,0xB8,0x1B,0x58,0x29,0x4C,0xD9,0x7A,0xBB,0x20,0x28,0x07,0xF4,0x17,0x14,0x4F,0xCC,0xED,0x62,0x44,0x23,0x6C,0x16,0xD9,0xD7,0xBC,0x2D};
  2014. unsigned char pKey_dp[64] = {0x83,0x8D,0xE3,0x40,0xF9,0x40,0x25,0xE8,0x2F,0x37,0x42,0x20,0x78,0x61,0x8C,0x37,0xB4,0xA2,0x59,0x39,0x28,0x95,0x62,0xBE,0xED,0x99,0xC7,0x48,0x07,0xD2,0x83,0x89,0x05,0xEA,0x7B,0x54,0xFB,0x0B,0x20,0x30,0xFA,0x2E,0x9F,0xCB,0x42,0x6F,0x12,0xDA,0x49,0xCE,0x53,0x9C,0xF8,0xA6,0x12,0xB6,0x44,0xD6,0x0F,0x93,0x3B,0xDE,0x33,0xDD};
  2015. unsigned char pKey_dq[64] = {0x53,0x14,0xF6,0x67,0x1A,0x64,0xE5,0x7F,0x03,0x6E,0x08,0x7E,0x20,0x11,0x2F,0xF4,0xCE,0xF9,0x0E,0x5C,0xFA,0xFB,0x38,0x95,0x10,0xCB,0xFF,0x60,0x8C,0xE4,0x78,0x03,0x14,0xBD,0xC5,0xB3,0xAB,0xEA,0x6D,0x9E,0xC6,0xA1,0x47,0xAF,0xE6,0xA2,0x51,0x66,0x21,0xC9,0x40,0xDA,0xF7,0xBD,0x51,0x01,0x38,0x0A,0x9E,0x17,0xA2,0xD9,0x5A,0x39};
  2016. unsigned char pKey_InvQ[64] = {0x45,0x6F,0xEA,0x63,0x6C,0x21,0x2E,0xAC,0x92,0xBA,0xED,0xDA,0xA9,0x3A,0x03,0xEC,0x55,0xCB,0x78,0x95,0xC2,0x8C,0xE3,0x16,0xD2,0xC5,0x12,0xB1,0x6A,0x51,0x75,0xD4,0x03,0x19,0x4B,0xC1,0xA2,0xD9,0xDD,0x9A,0xB7,0xBC,0xEE,0x7F,0xD0,0x40,0xE6,0x7E,0x76,0xEE,0x1C,0x85,0x0E,0x0B,0x63,0x01,0x1D,0x43,0x0C,0x4A,0xA1,0xF4,0x73,0x64};
  2017. rsa_ctx.len = 128;
  2018. mbedtls_mpi_read_binary(&rsa_ctx.N , pKey_n, 128);
  2019. mbedtls_mpi_read_binary(&rsa_ctx.E , pKey_e, 4);
  2020. mbedtls_mpi_read_binary(&rsa_ctx.D , pKey_d, 128);
  2021. mbedtls_mpi_read_binary(&rsa_ctx.P , pKey_p, 64);
  2022. mbedtls_mpi_read_binary(&rsa_ctx.Q , pKey_q, 64);
  2023. mbedtls_mpi_read_binary(&rsa_ctx.DP , pKey_dp, 64);
  2024. mbedtls_mpi_read_binary(&rsa_ctx.DQ ,pKey_dq, 64);
  2025. mbedtls_mpi_read_binary(&rsa_ctx.QP , pKey_InvQ, 64);
  2026. ret_mbedtls = mbedtls_rsa_check_pubkey( &rsa_ctx );
  2027. if(ret_mbedtls != DONGLE_SUCCESS)
  2028. {
  2029. printf("错误处915\n");
  2030. return 915;
  2031. }
  2032. ret_mbedtls = mbedtls_rsa_check_privkey( &rsa_ctx );
  2033. if(ret_mbedtls != DONGLE_SUCCESS)
  2034. {
  2035. printf("错误处916\n");
  2036. return 916;
  2037. }
  2038. ret_mbedtls = mbedtls_rsa_public(&rsa_ctx, plain1, dec_plain1);
  2039. if(ret_mbedtls != DONGLE_SUCCESS)
  2040. {
  2041. printf("错误处917\n");
  2042. return 917;
  2043. }
  2044. ret_mbedtls = mbedtls_rsa_private(&rsa_ctx, NULL, NULL, dec_plain1, mbedtlsPlain1);
  2045. if(ret_mbedtls != DONGLE_SUCCESS)
  2046. {
  2047. printf("错误处918\n");
  2048. return 918;
  2049. }
  2050. //2.1024位密钥 外面的库产生的公私钥验证自己库
  2051. ret_mbedtls = mbedtls_rsa_gen_key( &rsa_ctx, myrand, NULL, 1024, 65537);
  2052. memset(pKey.n, 0, 256);
  2053. memset(pKey.e, 0, 4);
  2054. memset(pKey.p, 0, 128);
  2055. memset(pKey.q, 0, 128);
  2056. memset(pKey.dp, 0, 128);
  2057. memset(pKey.dq, 0, 128);
  2058. memset(pKey.InvQ, 0, 128);
  2059. pKey.len = 0x0400;
  2060. mbedtls_mpi_write_binary(&rsa_ctx.N , pKey.n, 128);
  2061. mbedtls_mpi_write_binary(&rsa_ctx.E , pKey.e, 4);
  2062. mbedtls_mpi_write_binary(&rsa_ctx.P , pKey.p, 64);
  2063. mbedtls_mpi_write_binary(&rsa_ctx.Q , pKey.q, 64);
  2064. mbedtls_mpi_write_binary(&rsa_ctx.DP , pKey.dp, 64);
  2065. mbedtls_mpi_write_binary(&rsa_ctx.DQ , pKey.dq, 64);
  2066. mbedtls_mpi_write_binary(&rsa_ctx.QP , pKey.InvQ, 64);
  2067. ret_status = Dongle_ResetState(hDongle);
  2068. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2069. //设置密钥
  2070. ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
  2071. // 运算类型。0x00公钥运算, 0x01私钥运算
  2072. ret_status = Dongle_RSA(hDongle, 0, 0x00, buf1, 128);
  2073. if(ret_status != DONGLE_SUCCESS)
  2074. {
  2075. printf("错误处919\n");
  2076. return 919;
  2077. }
  2078. // 运算类型。0x00公钥运算, 0x01私钥运算
  2079. ret_status = Dongle_RSA(hDongle, 0, 0x01, buf1, 128);
  2080. if(ret_status != DONGLE_SUCCESS)
  2081. {
  2082. printf("错误处920\n");
  2083. return 920;
  2084. }
  2085. //2048位密钥 RSA运算
  2086. ret_status = Dongle_ResetState(hDongle);
  2087. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2088. //生成RSA密钥对 密钥位数:0x02=1024位;0x04=2048位 运算类型。0x00只导出公钥, 0x01导出公私鈅
  2089. ret_status = Dongle_GenRSAKey(hDongle, 0, 0x04, 0x01, &pKey);
  2090. //21验证匿名状态 进行RSA运算,权限不够, 返回失败
  2091. ret_status = Dongle_ResetState(hDongle);
  2092. // 运算类型。0x00公钥运算, 0x01私钥运算
  2093. ret_status = Dongle_RSA(hDongle, 0, 0x00, buf2, 256);
  2094. if(ret_status == DONGLE_SUCCESS)
  2095. {
  2096. printf("错误处921\n");
  2097. return 921;
  2098. }
  2099. // 运算类型。0x00公钥运算, 0x01私钥运算
  2100. ret_status = Dongle_RSA(hDongle, 0, 0x01, buf2, 256);
  2101. if(ret_status == DONGLE_SUCCESS)
  2102. {
  2103. printf("错误处922\n");
  2104. return 922;
  2105. }
  2106. //22验证用户权限 进行RSA运算,参数传入正确, 返回成功
  2107. ret_status = Dongle_ResetState(hDongle);
  2108. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2109. // 运算类型。0x00公钥运算, 0x01私钥运算
  2110. ret_status = Dongle_RSA(hDongle, 0, 0x00, buf2, 256);
  2111. if(ret_status != DONGLE_SUCCESS)
  2112. {
  2113. printf("错误处923\n");
  2114. return 923;
  2115. }
  2116. // 运算类型。0x00公钥运算, 0x01私钥运算
  2117. ret_status = Dongle_RSA(hDongle, 0, 0x01, buf2, 256);
  2118. if(ret_status != DONGLE_SUCCESS)
  2119. {
  2120. printf("错误处924\n");
  2121. return 924;
  2122. }
  2123. //二,2048位密钥 用自己的库产生的公私钥赋值给mbedtls中的公私钥
  2124. //2048位密钥
  2125. unsigned char pKey_n2[256]= {0xB1,0x8E,0x62,0x2E,0x72,0xB9,0xA0,0xFC,0x99,0x6D,0x0A,0x8C,0x77,0xDE,0x40,0x3E,0xC7,0x71,0x3C,0xB6,0x7D,0x79,0x9A,0xFF,0xF8,0xB2,0x2C,0x83,0xD1,0x92,0xBD,0xE2,0x80,0xCC,0x4C,0x7A,0xBB,0xCB,0xA6,0xFE,0xCC,0x5F,0xBA,0x99,0xD1,0x18,0x5A,0x39,0x40,0xC0,0xDE,0x64,0x3A,0xD4,0x90,0xD0,0x82,0x04,0x0F,0xFE,0xAC,0x9D,0x78,0x74,0x1D,0xF6,0xFD,0xCC,0xC6,0xEA,0x6D,0x42,0xE1,0x72,0x4B,0x8E,0x6A,0x2D,0xEE,0xF6,0xEF,0x7D,0xA1,0xA4,0xA5,0x1E,0x34,0x4E,0xF1,0x32,0x98,0xE5,0xB9,0x3C,0x05,0xAC,0x20,0xD6,0xA7,0xEC,0x41,0xA3,0x1A,0xBA,0x6A,0x51,0x5E,0xFF,0xC2,0xD3,0x4A,0x39,0x4F,0x2C,0x95,0x34,0xAA,0xA5,0x4A,0xD6,0xEA,0x9E,0x0A,0xEB,0x5A,0xE5,0x29,0xAD,0x12,0x67,0xBC,0x17,0xDC,0x0F,0x3F,0x34,0x4E,0x8A,0x7F,0xD1,0x5A,0x8C,0xD0,0xD3,0xC0,0x6A,0xD5,0x37,0x36,0x5B,0x41,0xB1,0xC4,0x4A,0x54,0x80,0x1B,0x48,0xCB,0xDC,0x3F,0x4C,0xA5,0x7D,0x1F,0xF4,0x32,0x66,0xDB,0x8E,0xFF,0x27,0x3C,0xF6,0x2F,0x6E,0x81,0x54,0x5D,0xE2,0xD2,0x07,0xD0,0x17,0x97,0x0A,0x62,0xA4,0x58,0x1A,0xDF,0x0A,0x85,0x00,0x7E,0xCA,0x18,0x96,0xED,0xE8,0xDC,0xD4,0x48,0x92,0x5C,0x65,0xF2,0x84,0x15,0xC7,0xEF,0x56,0xF1,0xC1,0xC1,0x07,0x9A,0x46,0x97,0x73,0x57,0xD2,0xFF,0x7D,0x62,0x6E,0xAF,0x82,0x6A,0x27,0xB7,0x86,0x3F,0xB4,0x42,0xBD,0x86,0x87,0xB6,0xEE,0x29,0xE9,0x29,0xD8,0xB0,0x25,0x3F,0xFF,0xBC,0x00,0x7F,0x4A,0xD5,0x05,0x38,0x09};
  2126. unsigned char pKey_e2[4] = {0x00,0x01,0x00,0x01};
  2127. unsigned char pKey_d2[256] = {0x44,0xFA,0xF1,0x0F,0xE4,0x88,0x53,0x8F,0xEF,0x37,0x5A,0xB2,0xC9,0xDA,0xF1,0x90,0xB2,0x41,0x3E,0xFA,0x12,0xF3,0x4C,0x91,0x87,0x98,0x09,0x0D,0x49,0x3D,0x3E,0x42,0x22,0x11,0xBC,0xE0,0xAD,0xD6,0xE7,0xD0,0xF3,0xAB,0x79,0x3E,0x4A,0xEA,0xB0,0x1A,0xB0,0x1A,0x7E,0x0B,0xE8,0x83,0x2E,0xA3,0xA2,0xEF,0x5B,0x1B,0x71,0x0A,0x58,0x82,0x9C,0x25,0x99,0xE2,0x95,0xA0,0xFB,0xC3,0xD0,0xFC,0x6B,0x78,0x2F,0xAE,0x1A,0xB9,0x9A,0xBF,0x6F,0x2E,0x1C,0xEC,0x08,0x1C,0x67,0x3E,0x66,0x02,0x51,0x9D,0x8D,0x02,0x9B,0x5B,0x69,0xAF,0xB2,0xAC,0x60,0x34,0xEE,0x1B,0x20,0xD2,0x32,0x69,0x8D,0xDB,0x87,0xF3,0xE5,0x1F,0xA9,0xF0,0x3F,0x8C,0x11,0x68,0x47,0x0B,0xBA,0xAB,0x6F,0x2D,0xE4,0x89,0x4D,0x82,0x9D,0x67,0x34,0xE0,0x75,0x22,0x8D,0x55,0x62,0x8C,0x16,0x5B,0xC9,0x4F,0x97,0x96,0x62,0xEB,0x8E,0x3C,0xCD,0x6E,0xA0,0x26,0x60,0x64,0x42,0xE1,0x9B,0x6E,0x1D,0xB5,0x3A,0xAC,0xBC,0xE6,0x7E,0xE5,0x80,0x86,0x11,0xB5,0x64,0x7B,0xE9,0xFA,0xB5,0xF4,0x4E,0x70,0xBB,0x24,0x4B,0xBC,0x0F,0xC4,0x52,0xE9,0x4A,0xAA,0x9D,0xF1,0x56,0xE3,0xBD,0xB3,0x1A,0xEF,0x61,0xAC,0x3C,0xE5,0xEB,0x20,0x8E,0x9B,0x3D,0xD6,0x40,0x18,0x7F,0x3A,0x99,0xBF,0xAC,0x4D,0x26,0xC7,0xD9,0x40,0x4A,0x48,0x5D,0x81,0xE7,0x35,0x5E,0x93,0x42,0x76,0x74,0xB4,0x33,0x05,0x17,0x08,0x08,0xC7,0x95,0xB9,0x52,0xAB,0x7B,0x4A,0xFC,0x02,0x61,0x31,0xB7,0xC3,0x14,0xA7,0x68,0x15};
  2128. unsigned char pKey_p2[128] = {0xC0,0xC0,0x17,0x7E,0x5A,0x99,0x22,0x81,0x5E,0xFE,0x02,0x31,0xD6,0xE3,0xE1,0xB1,0x73,0xF6,0x9C,0xB7,0xE9,0x9A,0xB7,0x28,0xF0,0x5A,0xBD,0x6C,0x88,0x5E,0xAD,0x33,0xC1,0x74,0xFC,0x29,0xBF,0x80,0xB0,0xA9,0xEB,0xDC,0x87,0x3C,0xB7,0x27,0x40,0x63,0x1E,0x63,0xFD,0x59,0x08,0x6D,0x37,0xD8,0x0E,0x35,0x55,0xF8,0x8D,0xFC,0x49,0x43,0xBA,0xB7,0xBD,0x99,0xB0,0xD7,0x1E,0xAF,0x81,0x38,0x30,0xA6,0x26,0xDD,0x4E,0xDD,0x67,0x6A,0xAB,0x7E,0x8D,0xFF,0x59,0x08,0xF1,0x3F,0x09,0x10,0xF2,0x2D,0xF0,0x60,0xAF,0xFF,0xAB,0x59,0x4C,0x91,0xB0,0x20,0xEE,0x83,0x5D,0x4B,0xF9,0x6D,0x5D,0x74,0x17,0xA3,0x6A,0xB1,0xA8,0xC9,0xD2,0x9E,0x42,0x76,0x44,0x88,0x07,0xD9,0x61,0xDB};
  2129. unsigned char pKey_q2[128] = {0xEB,0xD1,0xE9,0x79,0x73,0xD9,0x68,0x90,0x72,0xC7,0xFF,0xC7,0xCB,0x50,0x71,0xD0,0x96,0x9A,0x51,0x8D,0xB3,0xF3,0xFA,0x96,0xDE,0x0E,0x7B,0x67,0xFC,0x9F,0x42,0xB4,0x88,0xA9,0xF8,0xD2,0xEA,0x59,0xAB,0xB6,0xAF,0x34,0xFE,0x30,0x80,0x15,0x27,0x32,0x40,0xBC,0xAD,0x53,0x32,0x79,0x11,0x1D,0x3A,0xBE,0xA1,0xFC,0xE9,0xC9,0x06,0x54,0x1C,0x85,0x6A,0x6A,0xC9,0x4F,0x7E,0x47,0x99,0x5E,0x6E,0xD5,0x41,0x14,0xE9,0xDF,0xD8,0xD9,0x9E,0xF3,0xD5,0xE0,0xC8,0x08,0x92,0xD7,0x3E,0x38,0x16,0x45,0xBC,0x72,0x5A,0xAC,0xA9,0xF0,0x20,0xD8,0xF3,0xAE,0x7B,0x95,0xAA,0x8A,0x74,0xB5,0x90,0x6D,0xD4,0x72,0x9B,0x38,0x24,0x5C,0x25,0xED,0xDD,0x71,0x00,0xC4,0xA0,0x90,0x6C,0xEB};
  2130. unsigned char pKey_dp2[128] = {0xA9,0x4A,0xCE,0x17,0xAB,0x49,0x1E,0xB6,0x54,0x11,0x73,0x2A,0x05,0x1C,0x87,0x89,0x0D,0x5A,0x07,0xD1,0xBC,0x7A,0xA7,0xEE,0x6C,0x14,0xB6,0x94,0xD1,0xBD,0x9E,0x10,0x7C,0x14,0xDC,0xB9,0x6A,0x3A,0x84,0x6F,0xE9,0xF9,0xFA,0x49,0xC8,0x6F,0xD0,0x2C,0x7E,0x6F,0xC1,0xFF,0xE5,0xD7,0xCF,0x99,0x92,0xF9,0x23,0x76,0x38,0x76,0xE2,0x7E,0x7B,0x4B,0x90,0x5A,0xA1,0xA8,0x7F,0x8A,0x14,0x4B,0x9E,0xE6,0xFF,0xF1,0x96,0xA7,0xFD,0x82,0x01,0xF8,0x18,0x8D,0xB2,0xCC,0x98,0xDF,0x0D,0x7E,0x75,0xC7,0x05,0x7E,0x56,0x76,0x8F,0x2F,0xD2,0x81,0x8F,0x19,0x8D,0x83,0x91,0x78,0xFE,0x2D,0x21,0x4C,0xAA,0xCA,0x5C,0x16,0x88,0x32,0x23,0x2B,0xDF,0x21,0xF3,0x15,0x2B,0x17,0xB1,0xCB};
  2131. unsigned char pKey_dq2[128] = {0xA9,0xED,0xAC,0x18,0xF9,0x93,0x0B,0x57,0x33,0x6E,0xCC,0xF0,0xB2,0xA0,0x38,0x33,0x7F,0xA2,0x99,0xEB,0xCB,0x65,0x9F,0x08,0x41,0xE1,0x1B,0xA9,0xF6,0x0E,0xE9,0x29,0x60,0x65,0x62,0xC8,0xD1,0x58,0x66,0x75,0x10,0xD1,0xD5,0xED,0x2B,0x0E,0x93,0x1D,0x12,0x2B,0x23,0x96,0x95,0x03,0x4A,0x82,0xCE,0x86,0xFA,0x01,0x5E,0x91,0x9D,0xFE,0xD4,0x42,0x8B,0x49,0x81,0xE0,0xC7,0x75,0x5E,0x9D,0xFE,0xAD,0xAA,0xE5,0x20,0x16,0x4E,0x93,0x9A,0x87,0xC8,0x71,0xDE,0x92,0xD7,0x05,0x45,0x6F,0x73,0x15,0x33,0x53,0x0F,0xE5,0xE5,0xDB,0x04,0x40,0x71,0x70,0x24,0xDB,0xD7,0x75,0x47,0xDE,0x61,0xC1,0xD2,0xD9,0xF6,0xE1,0xC0,0xC9,0xAA,0x07,0x3A,0x8F,0xA8,0x67,0x07,0xF0,0xAA,0x9B};
  2132. unsigned char pKey_InvQ2[128] = {0x5D,0xCE,0x67,0xC9,0x34,0x96,0x0E,0x0E,0x28,0xA5,0x72,0xA9,0x62,0xD4,0xA5,0xA7,0x39,0x9E,0x3F,0x4A,0x1C,0x52,0xD1,0xC1,0x4A,0xB8,0xEA,0x59,0x64,0xCB,0x3F,0xE0,0x59,0x40,0x18,0x5C,0xBA,0xD9,0xD4,0x3C,0xE5,0x66,0x39,0xA4,0x76,0x08,0xDE,0x3C,0x41,0x35,0xF7,0x37,0x55,0x9B,0xB7,0x94,0xFB,0x49,0xD3,0xB0,0xE5,0x1D,0xDE,0x94,0x27,0xD5,0x54,0xD8,0x61,0x71,0x88,0x9A,0x5D,0x15,0x61,0x76,0xA5,0xFC,0x3C,0x36,0x0B,0x51,0x23,0x51,0x84,0x15,0xD5,0x78,0x23,0x25,0xE8,0x69,0xB8,0x27,0x36,0xFB,0x75,0x8A,0x0C,0x78,0x33,0x6A,0x55,0x9C,0x2E,0x3F,0x09,0x03,0x35,0xBD,0x97,0x4A,0x7B,0x38,0x74,0x7F,0xA5,0x6A,0x77,0xAC,0x5A,0x78,0xEC,0xC6,0xEF,0x50,0xB9,0x1E};
  2133. rsa_ctx.len = 256;
  2134. mbedtls_mpi_read_binary(&rsa_ctx.N , pKey_n2, 256);
  2135. mbedtls_mpi_read_binary(&rsa_ctx.E , pKey_e2, 4);
  2136. mbedtls_mpi_read_binary(&rsa_ctx.D , pKey_d2, 256);
  2137. mbedtls_mpi_read_binary(&rsa_ctx.P , pKey_p2, 128);
  2138. mbedtls_mpi_read_binary(&rsa_ctx.Q , pKey_q2, 128);
  2139. mbedtls_mpi_read_binary(&rsa_ctx.DP , pKey_dp2, 128);
  2140. mbedtls_mpi_read_binary(&rsa_ctx.DQ , pKey_dq2, 128);
  2141. mbedtls_mpi_read_binary(&rsa_ctx.QP , pKey_InvQ2, 128);
  2142. ret_mbedtls = mbedtls_rsa_check_pubkey( &rsa_ctx );
  2143. if(ret_mbedtls != DONGLE_SUCCESS)
  2144. {
  2145. printf("错误处925\n");
  2146. return 925;
  2147. }
  2148. ret_mbedtls = mbedtls_rsa_check_privkey( &rsa_ctx );
  2149. if(ret_mbedtls != DONGLE_SUCCESS)
  2150. {
  2151. printf("错误处926\n");
  2152. return 926;
  2153. }
  2154. ret_mbedtls = mbedtls_rsa_public(&rsa_ctx, plain2, dec_plain2);
  2155. if(ret_mbedtls != DONGLE_SUCCESS)
  2156. {
  2157. printf("错误处927\n");
  2158. return 927;
  2159. }
  2160. ret_mbedtls = mbedtls_rsa_private(&rsa_ctx, NULL, NULL, dec_plain2, mbedtlsPlain2);
  2161. if(ret_mbedtls != DONGLE_SUCCESS)
  2162. {
  2163. printf("错误处928\n");
  2164. return 928;
  2165. }
  2166. //2.1024位密钥 外面的库产生的公私钥验证自己库
  2167. ret_mbedtls = mbedtls_rsa_gen_key( &rsa_ctx, myrand, NULL, 2048, 65537);
  2168. memset(pKey.n, 0, 256);
  2169. memset(pKey.e, 0, 4);
  2170. memset(pKey.p, 0, 128);
  2171. memset(pKey.q, 0, 128);
  2172. memset(pKey.dp, 0, 128);
  2173. memset(pKey.dq, 0, 128);
  2174. memset(pKey.InvQ, 0, 128);
  2175. pKey.len = 0x0800;
  2176. mbedtls_mpi_write_binary(&rsa_ctx.N , pKey.n, 256);
  2177. mbedtls_mpi_write_binary(&rsa_ctx.E , pKey.e, 4);
  2178. mbedtls_mpi_write_binary(&rsa_ctx.P , pKey.p, 128);
  2179. mbedtls_mpi_write_binary(&rsa_ctx.Q , pKey.q, 128);
  2180. mbedtls_mpi_write_binary(&rsa_ctx.DP , pKey.dp, 128);
  2181. mbedtls_mpi_write_binary(&rsa_ctx.DQ , pKey.dq, 128);
  2182. mbedtls_mpi_write_binary(&rsa_ctx.QP , pKey.InvQ, 128);
  2183. ret_status = Dongle_ResetState(hDongle);
  2184. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2185. ret_status = Dongle_SetRSAKey(hDongle, 0, &pKey);
  2186. // 运算类型。0x00公钥运算, 0x01私钥运算
  2187. ret_status = Dongle_RSA(hDongle, 0, 0x00, buf2, 256);
  2188. if(ret_mbedtls != DONGLE_SUCCESS)
  2189. {
  2190. printf("错误处929\n");
  2191. return 929;
  2192. }
  2193. // 运算类型。0x00公钥运算, 0x01私钥运算
  2194. ret_status = Dongle_RSA(hDongle, 0, 0x01, buf2, 256);
  2195. if(ret_mbedtls != DONGLE_SUCCESS)
  2196. {
  2197. printf("错误处930\n");
  2198. return 930;
  2199. }
  2200. mbedtls_rsa_free(&rsa_ctx);
  2201. Dongle_Close(hDongle);
  2202. return 0;
  2203. }
  2204. //10
  2205. int test_PIN()
  2206. {
  2207. DONGLE_HANDLE hDongle;
  2208. unsigned long did = 0XFFFFFFFF;
  2209. unsigned short count = 0;
  2210. unsigned long ret_status = 0;
  2211. unsigned char pTryCount;
  2212. //种子码产生用户PIN码
  2213. char szUserPIN[8] = {0};
  2214. unsigned char bySeed[1023] = {1,2,3,4,5,6,7,8};
  2215. unsigned char bySeed1[1024] = {1,2,3,4,5,6,7,8};
  2216. //修改开发商PIN码
  2217. char szNewPin[24] = {0};
  2218. unsigned char upin[8] = {0};
  2219. unsigned char dpin[24] = {0};
  2220. int i = 0;
  2221. for(i = 0; i < 24; i++)
  2222. {
  2223. dpin[i] = 0xFF;
  2224. }
  2225. ret_status = Dongle_Find(did, &count);
  2226. ret_status = Dongle_Open(1, 0, &hDongle);
  2227. //种子码产生用户PIN码
  2228. //1验证匿名状态 种子码产生用户PIN码,权限不够,返回失败
  2229. ret_status = Dongle_ResetState(hDongle);
  2230. ret_status = Dongle_GenUserPIN(hDongle, bySeed, 1023, 255, szUserPIN);
  2231. if(ret_status == DONGLE_SUCCESS)
  2232. {
  2233. printf("错误处1001\n");
  2234. return 1001;
  2235. }
  2236. //2验证用户权限 种子码产生用户PIN码,权限不够,返回失败
  2237. ret_status = Dongle_ResetState(hDongle);
  2238. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2239. ret_status = Dongle_GenUserPIN(hDongle, bySeed, 1023, 255, szUserPIN);
  2240. if(ret_status == DONGLE_SUCCESS)
  2241. {
  2242. printf("错误处1002\n");
  2243. return 1002;
  2244. }
  2245. //3验证开发商权限 种子码产生用户PIN码,参数三传入错误,返回失败
  2246. ret_status = Dongle_ResetState(hDongle);
  2247. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2248. ret_status = Dongle_GenUserPIN(hDongle, bySeed1, 1024, 255, szUserPIN);
  2249. if(ret_status != DONGLE_SUCCESS)
  2250. {
  2251. printf("错误处1003\n");
  2252. return 1003;
  2253. }
  2254. //4验证开发商权限 种子码产生用户PIN码,参数四传入错误,返回失败
  2255. ret_status = Dongle_ResetState(hDongle);
  2256. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2257. ret_status = Dongle_GenUserPIN(hDongle, bySeed, 1023, 256, szUserPIN);
  2258. if(ret_status != DONGLE_SUCCESS)
  2259. {
  2260. printf("错误处1004\n");
  2261. return 1004;
  2262. }
  2263. //5验证开发商权限 种子码产生用户PIN码,返回成功
  2264. ret_status = Dongle_ResetState(hDongle);
  2265. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2266. //ret_status = Dongle_GenUserPIN(hDongle, bySeed, 1023, 255, szUserPIN);
  2267. if(ret_status != DONGLE_SUCCESS)
  2268. {
  2269. printf("错误处1005\n");
  2270. return 1005;
  2271. }
  2272. //修改开发商PIN码
  2273. //6验证匿名状态 修改开发商PIN码,权限不够,返回失败
  2274. ret_status = Dongle_ResetState(hDongle);
  2275. ret_status = Dongle_ChangeAdminPIN(hDongle, (char *)dpin, szNewPin);
  2276. if(ret_status == DONGLE_SUCCESS)
  2277. {
  2278. printf("错误处1006\n");
  2279. return 1006;
  2280. }
  2281. //7验证用户权限 修改开发商PIN码,权限不够,返回失败
  2282. ret_status = Dongle_ResetState(hDongle);
  2283. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2284. ret_status = Dongle_ChangeAdminPIN(hDongle, (char *)dpin, szNewPin);
  2285. if(ret_status == DONGLE_SUCCESS)
  2286. {
  2287. printf("错误处1007\n");
  2288. return 1007;
  2289. }
  2290. //8验证开发商权限 修改开发商PIN码,返回成功
  2291. ret_status = Dongle_ResetState(hDongle);
  2292. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2293. //ret_status = Dongle_ChangeAdminPIN(hDongle, (char *)dpin, szNewPin);
  2294. if(ret_status != DONGLE_SUCCESS)
  2295. {
  2296. printf("错误处1008\n");
  2297. return 1008;
  2298. }
  2299. Dongle_Close(hDongle);
  2300. return 0;
  2301. }
  2302. //11
  2303. int test_setAESKey()
  2304. {
  2305. DONGLE_HANDLE hDongle;
  2306. unsigned long did = 0XFFFFFFFF;
  2307. unsigned short count = 0;
  2308. unsigned long ret_status = 0;
  2309. unsigned char pTryCount;
  2310. //密钥数值
  2311. unsigned char key1[16] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
  2312. unsigned char key2[24] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,0,1,2,3,4,5,6,7,8,9};
  2313. unsigned char key3[32] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2};
  2314. unsigned char buf[32] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,2,3};
  2315. unsigned char OutsideKeyBuf[32] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,2,3};
  2316. //明文
  2317. unsigned char plain[32] = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6,7,8,9,0,2,3};
  2318. //密文
  2319. unsigned char dec_plain[32] = {0};
  2320. unsigned char mbedtlsPlain[32] = {0};
  2321. //初始化向量
  2322. unsigned char pLv[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
  2323. unsigned char OutsideKeyPlv[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
  2324. //mbedTLS中关于3DES相关配置
  2325. mbedtls_aes_context aes_ctx;
  2326. unsigned char upin[8] = {0};
  2327. unsigned char dpin[24] = {0};
  2328. int i = 0;
  2329. for(i = 0; i < 24; i++)
  2330. {
  2331. dpin[i] = 0xFF;
  2332. }
  2333. mbedtls_aes_init(&aes_ctx);
  2334. ret_status = Dongle_Find(did, &count);
  2335. ret_status = Dongle_Open(1, 0, &hDongle);
  2336. //设置AES密钥
  2337. //1匿名状态设置AES密钥 返回失败
  2338. ret_status = Dongle_ResetState(hDongle);
  2339. ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
  2340. if(ret_status == DONGLE_SUCCESS)
  2341. {
  2342. printf("错误处1101\n");
  2343. return 1101;
  2344. }
  2345. //2用户权限设置AES密钥 返回失败
  2346. ret_status = Dongle_ResetState(hDongle);
  2347. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2348. ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
  2349. if(ret_status == DONGLE_SUCCESS)
  2350. {
  2351. printf("错误处1102\n");
  2352. return 1102;
  2353. }
  2354. //3开发商权限设置AES密钥 密钥ID设置为1,返回失败
  2355. ret_status = Dongle_ResetState(hDongle);
  2356. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2357. ret_status = Dongle_SetAESKey(hDongle, 1, key3, 32);
  2358. if(ret_status == DONGLE_SUCCESS)
  2359. {
  2360. printf("错误处1103\n");
  2361. return 1103;
  2362. }
  2363. //4开发商权限设置AES密钥 密钥ID设置为0,返回成功
  2364. ret_status = Dongle_ResetState(hDongle);
  2365. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2366. ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
  2367. if(ret_status != DONGLE_SUCCESS)
  2368. {
  2369. printf("错误处1104\n");
  2370. return 1104;
  2371. }
  2372. //5开发商权限设置AES密钥 密钥ID设置为1,返回失败
  2373. ret_status = Dongle_ResetState(hDongle);
  2374. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2375. ret_status = Dongle_SetAESKey(hDongle, 1, key3, 32);
  2376. if(ret_status == DONGLE_SUCCESS)
  2377. {
  2378. printf("错误处1105\n");
  2379. return 1105;
  2380. }
  2381. //AES运算
  2382. //6参数验证,AES运算函数传参,用户权限下传入密钥ID 1,返回失败
  2383. ret_status = Dongle_ResetState(hDongle);
  2384. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2385. // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
  2386. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 1, 0x00, pLv, plain, 32);
  2387. if(ret_status == DONGLE_SUCCESS)
  2388. {
  2389. printf("错误处1106\n");
  2390. return 1106;
  2391. }
  2392. //7参数验证,AES运算函数传参,用户权限下传入运算类型2,返回失败
  2393. ret_status = Dongle_ResetState(hDongle);
  2394. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2395. // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
  2396. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 2, pLv, plain, 32);
  2397. if(ret_status == DONGLE_SUCCESS)
  2398. {
  2399. printf("错误处1107\n");
  2400. //return 1107;
  2401. }
  2402. //8权限验证,AES运算函数传参,匿名状态下进行加密运算,返回失败
  2403. ret_status = Dongle_ResetState(hDongle);
  2404. // 密钥ID 0, 运算类型。0x00表示加密,0x01表示解密
  2405. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, plain, 32);
  2406. if(ret_status == DONGLE_SUCCESS)
  2407. {
  2408. printf("错误处1108\n");
  2409. return 1108;
  2410. }
  2411. //一 AES运算 ecb模式 16字节key
  2412. ret_status = Dongle_ResetState(hDongle);
  2413. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2414. ret_status = Dongle_SetAESKey(hDongle, 0, key1, 16);
  2415. //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
  2416. ret_status = Dongle_ResetState(hDongle);
  2417. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2418. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2419. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 32);
  2420. if(ret_status != DONGLE_SUCCESS)
  2421. {
  2422. printf("错误处1109\n");
  2423. return 1109;
  2424. }
  2425. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2426. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key1, 16, 0x00, OutsideKeyPlv, OutsideKeyBuf, 32);
  2427. if(ret_status != DONGLE_SUCCESS)
  2428. {
  2429. printf("错误处1110\n");
  2430. return 1110;
  2431. }
  2432. //10 ecb模式 16字节key 外库加密
  2433. mbedtls_aes_setkey_enc( &aes_ctx, key1, 128);
  2434. for(i = 0; i < 2; i++)
  2435. {
  2436. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, plain + (16*i), dec_plain + (16*i));
  2437. }
  2438. //外库与锁内 加密验证
  2439. for(i = 0; i < 32; i++)
  2440. {
  2441. if(buf[i] != dec_plain[i])
  2442. {
  2443. printf("ecb模式 16字节key enc出错 i = %d\n", i);
  2444. return 1111;
  2445. }
  2446. }
  2447. //锁内与锁外 加密验证
  2448. for(i = 0; i < 32; i++)
  2449. {
  2450. if(buf[i] != OutsideKeyBuf[i])
  2451. {
  2452. printf("ecb模式 16字节key enc出错 i = %d\n", i);
  2453. return 1112;
  2454. }
  2455. }
  2456. //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
  2457. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2458. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 32);
  2459. if(ret_status != DONGLE_SUCCESS)
  2460. {
  2461. printf("错误处1113\n");
  2462. return 1113;
  2463. }
  2464. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2465. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key1, 16, 0x01, OutsideKeyPlv, OutsideKeyBuf, 32);
  2466. if(ret_status != DONGLE_SUCCESS)
  2467. {
  2468. printf("错误处1114\n");
  2469. return 1114;
  2470. }
  2471. //12 ecb模式 16字节key 外库解密验证
  2472. mbedtls_aes_setkey_dec(&aes_ctx, key1, 128);
  2473. for(i = 0; i < 2; i++)
  2474. {
  2475. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, dec_plain + (16*i), mbedtlsPlain + (16*i));
  2476. }
  2477. //外库与锁内 解密验证
  2478. for(i = 0; i < 32; i++)
  2479. {
  2480. if(buf[i] != mbedtlsPlain[i])
  2481. {
  2482. printf("ecb模式 16字节key dec出错 i = %d\n", i);
  2483. return 1115;
  2484. }
  2485. }
  2486. //锁内与锁外 解密验证
  2487. for(i = 0; i < 32; i++)
  2488. {
  2489. if(buf[i] != OutsideKeyBuf[i])
  2490. {
  2491. printf("ecb模式 16字节key dec出错 i = %d\n", i);
  2492. return 1116;
  2493. }
  2494. }
  2495. //二 AES运算 ecb模式 24字节key
  2496. ret_status = Dongle_ResetState(hDongle);
  2497. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2498. ret_status = Dongle_SetAESKey(hDongle, 0, key2, 24);
  2499. //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
  2500. ret_status = Dongle_ResetState(hDongle);
  2501. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2502. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2503. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 32);
  2504. if(ret_status != DONGLE_SUCCESS)
  2505. {
  2506. printf("错误处1117\n");
  2507. return 1117;
  2508. }
  2509. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2510. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key2, 24, 0x00, OutsideKeyPlv, OutsideKeyBuf, 32);
  2511. if(ret_status != DONGLE_SUCCESS)
  2512. {
  2513. printf("错误处1118\n");
  2514. return 1118;
  2515. }
  2516. //10 ecb模式 24字节key 外库加密must be 128, 192 or 256
  2517. mbedtls_aes_setkey_enc( &aes_ctx, key2, 192);
  2518. for(i = 0; i < 2; i++)
  2519. {
  2520. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, plain + (16*i), dec_plain + (16*i));
  2521. }
  2522. //外库与锁内 加密验证
  2523. for(i = 0; i < 32; i++)
  2524. {
  2525. if(buf[i] != dec_plain[i])
  2526. {
  2527. printf("ecb模式 24字节key enc出错 i = %d\n", i);
  2528. return 1119;
  2529. }
  2530. }
  2531. //锁内与锁外 加密验证
  2532. for(i = 0; i < 32; i++)
  2533. {
  2534. if(buf[i] != OutsideKeyBuf[i])
  2535. {
  2536. printf("ecb模式 24字节key enc出错 i = %d\n", i);
  2537. return 1120;
  2538. }
  2539. }
  2540. //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
  2541. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2542. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 32);
  2543. if(ret_status != DONGLE_SUCCESS)
  2544. {
  2545. printf("错误处1121\n");
  2546. return 1121;
  2547. }
  2548. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2549. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key2, 24, 0x01, OutsideKeyPlv, OutsideKeyBuf, 32);
  2550. if(ret_status != DONGLE_SUCCESS)
  2551. {
  2552. printf("错误处1122\n");
  2553. return 1122;
  2554. }
  2555. //12 ecb模式 24字节key 外库解密验证must be 128, 192 or 256
  2556. mbedtls_aes_setkey_dec(&aes_ctx, key2, 192);
  2557. for(i = 0; i < 2; i++)
  2558. {
  2559. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, dec_plain + (16*i), mbedtlsPlain + (16*i));
  2560. }
  2561. //外库与锁内 解密验证
  2562. for(i = 0; i < 32; i++)
  2563. {
  2564. if(buf[i] != mbedtlsPlain[i])
  2565. {
  2566. printf("ecb模式 24字节key dec出错 i = %d\n", i);
  2567. return 1123;
  2568. }
  2569. }
  2570. //锁内与锁外 解密验证
  2571. for(i = 0; i < 32; i++)
  2572. {
  2573. if(buf[i] != OutsideKeyBuf[i])
  2574. {
  2575. printf("ecb模式 24字节key dec出错 i = %d\n", i);
  2576. return 1124;
  2577. }
  2578. }
  2579. //三 AES运算 ecb模式 32字节key
  2580. ret_status = Dongle_ResetState(hDongle);
  2581. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2582. ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
  2583. //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
  2584. ret_status = Dongle_ResetState(hDongle);
  2585. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2586. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2587. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x00, pLv, buf, 32);
  2588. if(ret_status != DONGLE_SUCCESS)
  2589. {
  2590. printf("错误处1125\n");
  2591. return 1125;
  2592. }
  2593. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2594. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key3, 32, 0x00, OutsideKeyPlv, OutsideKeyBuf, 32);
  2595. if(ret_status != DONGLE_SUCCESS)
  2596. {
  2597. printf("错误处1126\n");
  2598. return 1126;
  2599. }
  2600. //10 ecb模式 32字节key 外库加密must be 128, 192 or 256
  2601. mbedtls_aes_setkey_enc( &aes_ctx, key3, 256);
  2602. for(i = 0; i < 2; i++)
  2603. {
  2604. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, plain + (16*i), dec_plain + (16*i));
  2605. }
  2606. //外库与锁内 加密验证
  2607. for(i = 0; i < 32; i++)
  2608. {
  2609. if(buf[i] != dec_plain[i])
  2610. {
  2611. printf("ecb模式 32字节key enc出错 i = %d\n", i);
  2612. return 1127;
  2613. }
  2614. }
  2615. //锁内与锁外 加密验证
  2616. for(i = 0; i < 32; i++)
  2617. {
  2618. if(buf[i] != OutsideKeyBuf[i])
  2619. {
  2620. printf("ecb模式 32字节key enc出错 i = %d\n", i);
  2621. return 1128;
  2622. }
  2623. }
  2624. //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
  2625. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2626. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x01, pLv, buf, 32);
  2627. if(ret_status != DONGLE_SUCCESS)
  2628. {
  2629. printf("错误处1129\n");
  2630. return 1129;
  2631. }
  2632. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2633. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key3, 32, 0x01, OutsideKeyPlv, OutsideKeyBuf, 32);
  2634. if(ret_status != DONGLE_SUCCESS)
  2635. {
  2636. printf("错误处1130\n");
  2637. return 1130;
  2638. }
  2639. //12 ecb模式 32字节key 外库解密验证must be 128, 192 or 256
  2640. mbedtls_aes_setkey_dec(&aes_ctx, key3, 256);
  2641. for(i = 0; i < 2; i++)
  2642. {
  2643. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, dec_plain + (16*i), mbedtlsPlain + (16*i));
  2644. }
  2645. //外库与锁内 解密验证
  2646. for(i = 0; i < 32; i++)
  2647. {
  2648. if(buf[i] != mbedtlsPlain[i])
  2649. {
  2650. printf("ecb模式 32字节key dec出错 i = %d\n", i);
  2651. return 1131;
  2652. }
  2653. }
  2654. //锁内与锁外 解密验证
  2655. for(i = 0; i < 32; i++)
  2656. {
  2657. if(buf[i] != OutsideKeyBuf[i])
  2658. {
  2659. printf("ecb模式 32字节key dec出错 i = %d\n", i);
  2660. return 1132;
  2661. }
  2662. }
  2663. //四 AES运算 cbc模式 16字节key
  2664. ret_status = Dongle_ResetState(hDongle);
  2665. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2666. ret_status = Dongle_SetAESKey(hDongle, 0, key1, 16);
  2667. //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
  2668. ret_status = Dongle_ResetState(hDongle);
  2669. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2670. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2671. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 32);
  2672. if(ret_status != DONGLE_SUCCESS)
  2673. {
  2674. printf("错误处1133\n");
  2675. return 1133;
  2676. }
  2677. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2678. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key1, 16, 0x10, OutsideKeyPlv, OutsideKeyBuf, 32);
  2679. if(ret_status != DONGLE_SUCCESS)
  2680. {
  2681. printf("错误处1134\n");
  2682. return 1134;
  2683. }
  2684. //10 cbc模式 16字节key 外库加密must be 128, 192 or 256
  2685. memset(dec_plain, 0, 32);
  2686. unsigned char mbedtls_enc_pLv1[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
  2687. unsigned char mbedtls_dec_pLv1[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
  2688. mbedtls_aes_setkey_enc( &aes_ctx, key1, 128);
  2689. for(i = 0; i < 2; i++)
  2690. {
  2691. mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16, mbedtls_enc_pLv1, plain + (16*i), dec_plain + (16*i));
  2692. }
  2693. //外库与锁内 加密验证
  2694. for(i = 0; i < 32; i++)
  2695. {
  2696. if(buf[i] != dec_plain[i])
  2697. {
  2698. printf("cbc模式 16字节key enc出错 i = %d\n", i);
  2699. return 1135;
  2700. }
  2701. }
  2702. //锁内与锁外 加密验证
  2703. for(i = 0; i < 32; i++)
  2704. {
  2705. if(buf[i] != OutsideKeyBuf[i])
  2706. {
  2707. printf("cbc模式 16字节key enc出错 i = %d\n", i);
  2708. return 1136;
  2709. }
  2710. }
  2711. //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
  2712. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2713. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 32);
  2714. if(ret_status != DONGLE_SUCCESS)
  2715. {
  2716. printf("错误处1137\n");
  2717. return 1137;
  2718. }
  2719. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2720. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key1, 16, 0x11, OutsideKeyPlv, OutsideKeyBuf, 32);
  2721. if(ret_status != DONGLE_SUCCESS)
  2722. {
  2723. printf("错误处1138\n");
  2724. return 1138;
  2725. }
  2726. //12 cbc模式 16字节key 外库解密验证must be 128, 192 or 256
  2727. mbedtls_aes_setkey_dec(&aes_ctx, key1, 128);
  2728. for(i = 0; i < 2; i++)
  2729. {
  2730. mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, 16, mbedtls_dec_pLv1, dec_plain + (16*i), mbedtlsPlain + (16*i));
  2731. }
  2732. //外库与锁内 解密验证
  2733. for(i = 0; i < 32; i++)
  2734. {
  2735. if(buf[i] != mbedtlsPlain[i])
  2736. {
  2737. printf("cbc模式 16字节key dec出错 i = %d\n", i);
  2738. return 1139;
  2739. }
  2740. }
  2741. //锁内与锁外 解密验证
  2742. for(i = 0; i < 32; i++)
  2743. {
  2744. if(buf[i] != OutsideKeyBuf[i])
  2745. {
  2746. printf("cbc模式 16字节key dec出错 i = %d\n", i);
  2747. return 1140;
  2748. }
  2749. }
  2750. //五 AES运算 cbc模式 24字节key
  2751. ret_status = Dongle_ResetState(hDongle);
  2752. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2753. ret_status = Dongle_SetAESKey(hDongle, 0, key2, 24);
  2754. //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
  2755. ret_status = Dongle_ResetState(hDongle);
  2756. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2757. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2758. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 32);
  2759. if(ret_status != DONGLE_SUCCESS)
  2760. {
  2761. printf("错误处1141\n");
  2762. return 1141;
  2763. }
  2764. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2765. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key2, 24, 0x10, OutsideKeyPlv, OutsideKeyBuf, 32);
  2766. if(ret_status != DONGLE_SUCCESS)
  2767. {
  2768. printf("错误处1142\n");
  2769. return 1142;
  2770. }
  2771. //10 cbc模式 24字节key 外库加密must be 128, 192 or 256
  2772. memset(dec_plain, 0, 32);
  2773. unsigned char mbedtls_enc_pLv2[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
  2774. unsigned char mbedtls_dec_pLv2[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
  2775. mbedtls_aes_setkey_enc( &aes_ctx, key2, 192);
  2776. for(i = 0; i < 2; i++)
  2777. {
  2778. mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16, mbedtls_enc_pLv2, plain + (16*i), dec_plain + (16*i));
  2779. }
  2780. //外库与锁内 加密验证
  2781. for(i = 0; i < 32; i++)
  2782. {
  2783. if(buf[i] != dec_plain[i])
  2784. {
  2785. printf("cbc模式 24字节key enc出错 i = %d\n", i);
  2786. return 1143;
  2787. }
  2788. }
  2789. //锁内与锁外 加密验证
  2790. for(i = 0; i < 32; i++)
  2791. {
  2792. if(buf[i] != OutsideKeyBuf[i])
  2793. {
  2794. printf("cbc模式 24字节key enc出错 i = %d\n", i);
  2795. return 1144;
  2796. }
  2797. }
  2798. //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
  2799. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2800. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 32);
  2801. if(ret_status != DONGLE_SUCCESS)
  2802. {
  2803. printf("错误处1145\n");
  2804. return 1145;
  2805. }
  2806. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2807. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key2,24, 0x11, OutsideKeyPlv, OutsideKeyBuf, 32);
  2808. if(ret_status != DONGLE_SUCCESS)
  2809. {
  2810. printf("错误处1146\n");
  2811. return 1146;
  2812. }
  2813. //12 cbc模式 24字节key 外库解密验证must be 128, 192 or 256
  2814. mbedtls_aes_setkey_dec(&aes_ctx, key2, 192);
  2815. for(i = 0; i < 2; i++)
  2816. {
  2817. mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, 16, mbedtls_dec_pLv2, dec_plain + (16*i), mbedtlsPlain + (16*i));
  2818. }
  2819. //外库与锁内 解密验证
  2820. for(i = 0; i < 32; i++)
  2821. {
  2822. if(buf[i] != mbedtlsPlain[i])
  2823. {
  2824. printf("cbc模式 24字节key dec出错 i = %d\n", i);
  2825. return 1147;
  2826. }
  2827. }
  2828. //锁内与锁外 解密验证
  2829. for(i = 0; i < 32; i++)
  2830. {
  2831. if(buf[i] != OutsideKeyBuf[i])
  2832. {
  2833. printf("cbc模式 24字节key dec出错 i = %d\n", i);
  2834. return 1148;
  2835. }
  2836. }
  2837. //六 AES运算 cbc模式 32字节key
  2838. ret_status = Dongle_ResetState(hDongle);
  2839. ret_status = Dongle_VerifyPIN(hDongle, 0, dpin, (BYTE *)&pTryCount);
  2840. ret_status = Dongle_SetAESKey(hDongle, 0, key3, 32);
  2841. //9权限验证,AES运算函数传参,用户权限下进行加密运算,返回成功
  2842. ret_status = Dongle_ResetState(hDongle);
  2843. ret_status = Dongle_VerifyPIN(hDongle, 1, upin, (BYTE *)&pTryCount);
  2844. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2845. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x10, pLv, buf, 32);
  2846. if(ret_status != DONGLE_SUCCESS)
  2847. {
  2848. printf("错误处1149\n");
  2849. return 1149;
  2850. }
  2851. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2852. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key3, 32, 0x10, OutsideKeyPlv, OutsideKeyBuf, 32);
  2853. if(ret_status != DONGLE_SUCCESS)
  2854. {
  2855. printf("错误处1150\n");
  2856. return 1150;
  2857. }
  2858. //10 cbc模式 32字节key 外库加密must be 128, 192 or 256
  2859. memset(dec_plain, 0, 32);
  2860. unsigned char mbedtls_enc_pLv3[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
  2861. unsigned char mbedtls_dec_pLv3[16] = {4,3,2,1,8,7,5,6,4,3,2,1,8,7,5,6};
  2862. mbedtls_aes_setkey_enc( &aes_ctx, key3, 256);
  2863. for(i = 0; i < 2; i++)
  2864. {
  2865. mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, 16, mbedtls_enc_pLv3, plain + (16*i), dec_plain + (16*i));
  2866. }
  2867. //外库与锁内 加密验证
  2868. for(i = 0; i < 32; i++)
  2869. {
  2870. if(buf[i] != dec_plain[i])
  2871. {
  2872. printf("cbc模式 32字节key enc出错 i = %d\n", i);
  2873. return 1151;
  2874. }
  2875. }
  2876. //锁内与锁外 加密验证
  2877. for(i = 0; i < 32; i++)
  2878. {
  2879. if(buf[i] != OutsideKeyBuf[i])
  2880. {
  2881. printf("cbc模式 32字节key enc出错 i = %d\n", i);
  2882. return 1152;
  2883. }
  2884. }
  2885. //11权限验证,AES运算函数传参,用户权限下进行解密运算,返回成功
  2886. // 锁内 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2887. ret_status = Dongle_AES_Crypt_ByInsideKey(hDongle, 0, 0x11, pLv, buf, 32);
  2888. if(ret_status != DONGLE_SUCCESS)
  2889. {
  2890. printf("错误处1153\n");
  2891. return 1153;
  2892. }
  2893. // 锁外 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  2894. ret_status = Dongle_AES_Crypt_ByOutsideKey(hDongle, key3, 32, 0x11, OutsideKeyPlv, OutsideKeyBuf, 32);
  2895. if(ret_status != DONGLE_SUCCESS)
  2896. {
  2897. printf("错误处1154\n");
  2898. return 1154;
  2899. }
  2900. //12 cbc模式 32字节key 外库解密验证must be 128, 192 or 256
  2901. mbedtls_aes_setkey_dec(&aes_ctx, key3, 256);
  2902. for(i = 0; i < 2; i++)
  2903. {
  2904. mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_DECRYPT, 16, mbedtls_dec_pLv3, dec_plain + (16*i), mbedtlsPlain + (16*i));
  2905. }
  2906. //外库与锁内 解密验证
  2907. for(i = 0; i < 32; i++)
  2908. {
  2909. if(buf[i] != mbedtlsPlain[i])
  2910. {
  2911. printf("cbc模式 32字节key dec出错 i = %d\n", i);
  2912. return 1155;
  2913. }
  2914. }
  2915. //锁内与锁外 解密验证
  2916. for(i = 0; i < 32; i++)
  2917. {
  2918. if(buf[i] != OutsideKeyBuf[i])
  2919. {
  2920. printf("cbc模式 32字节key dec出错 i = %d\n", i);
  2921. return 1156;
  2922. }
  2923. }
  2924. Dongle_Close(hDongle);
  2925. return 0;
  2926. }
  2927. int main(int argc, char * argv[])
  2928. {
  2929. //1 测试Dongle_Find、Dongle_Open、Dongle_Close函数功能
  2930. //test_find_open_close();
  2931. //2 测试Dongle_VerifyPIN函数功能
  2932. //test_verifyPin();
  2933. //3 测试Dongle_WriteData、Dongle_ResetState函数功能
  2934. test_write();
  2935. //4 测试Dongle_ReadData函数功能
  2936. //test_read();
  2937. //5 测试Dongle_Set3DESKey、Dongle_3DES_Crypt函数功能
  2938. test_set3DESKey();
  2939. //6 测试Dongle_SetUserID、Dongle_GetUserID、Dongle_GetSerialNumber函数功能
  2940. //test_userID();
  2941. //7 测试Dongle_SetModuleCount、Dongle_GetModuleCount、Dongle_DecModuleCount函数功能
  2942. test_moduleCount();
  2943. //8 测试Dongle_Seed、Dongle_Sha1、Dongle_Get_Random函数功能
  2944. test_seed_sha1_getRandom();
  2945. //9 测试Dongle_SetRSAKey、Dongle_RSA、Dongle_GenRSAKey函数功能
  2946. test_RSA();
  2947. //10 测试Dongle_GenUserPIN、Dongle_ChangeAdminPIN函数功能
  2948. //test_PIN();
  2949. //11 测试Dongle_SetAESKey、Dongle_AES_Crypt_ByInsideKey、Dongle_AES_Crypt_ByOutsideKey函数功能
  2950. test_setAESKey();
  2951. //12 测试Dongle_MakeUpdatePacket、Dongle_Update函数功能
  2952. //test_update();
  2953. //13 测试Dongle_ClearCos、Dongle_Init函数功能
  2954. //test_clearCos();
  2955. return 0;
  2956. }

  1. #ifndef _SEKEY_H_
  2. #define _SEKEY_H_
  3. #include <windows.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. typedef void * DONGLE_HANDLE; //加密锁句柄
  8. typedef struct _RSA_KEY
  9. {
  10. WORD len;
  11. BYTE e[4];
  12. BYTE n[256];
  13. BYTE p[128];
  14. BYTE q[128];
  15. BYTE dp[128];
  16. BYTE dq[128];
  17. BYTE InvQ[128];
  18. }RSA_KEY,*PRSA_KEY;
  19. //错误码
  20. #define DONGLE_SUCCESS 0x00000000 // 操作成功
  21. #define DONGLE_NOT_FOUND 0xF0000001 // 未找到指定的设备
  22. #define DONGLE_INVALID_HANDLE 0xF0000002 // 无效的句柄
  23. #define DONGLE_INVALID_PARAMETER 0xF0000003 // 参数错误
  24. #define DONGLE_COMM_ERROR 0xF0000004 // 通讯错误
  25. #define DONGLE_GETC_ERROR 0xF0000005 // 获取通信密钥错误
  26. #define DONGLE_ERROR_UNKNOWN 0xFFFFFFFF // 未知的错误
  27. /**
  28. * @brief 查找指定产品ID的加密锁设备。
  29. *
  30. * @param dwDID [in] 开发商ID,即DID
  31. * @param pwCount [out] 存放加密锁设备个数
  32. *
  33. * @return DONGLE_SUCCESS 表示找到加密锁设备
  34. */
  35. DWORD WINAPI Dongle_Find(DWORD dwDID, WORD *pwCount);
  36. /**
  37. * @brief 关闭加密锁。
  38. *
  39. * @param hDongle [in] 打开的加密锁的句柄
  40. *
  41. * @return DONGLE_SUCCESS 表示关闭加密锁成功
  42. */
  43. DWORD WINAPI Dongle_Close(DONGLE_HANDLE hDongle);
  44. /**
  45. * @brief 打开加密锁。
  46. *
  47. * @param nIndex [in] 从1开始的索引值。表示打开找到的第几把加密锁。
  48. * @param bMode [in] 打开方式:0为独占打开,1为共享打开
  49. * @param phDongle [out] 存放打开后加密锁的句柄
  50. *
  51. * @return DONGLE_SUCCESS 表示打开加密锁成功
  52. */
  53. DWORD WINAPI Dongle_Open(int nIndex, BYTE bMode, DONGLE_HANDLE * phDongle);
  54. /**
  55. * @brief 将加密锁状态恢复到匿名态。最小调用权限:匿名。
  56. *
  57. * @param hDongle [in] 打开的加密锁的句柄
  58. *
  59. * @return DONGLE_SUCCESS 重置加密锁状态成功
  60. */
  61. DWORD WINAPI Dongle_ResetState(DONGLE_HANDLE hDongle);
  62. /**
  63. * @brief 校验PIN码。PIN分为用户PIN码(8字节,出厂默认全0)和开发商PIN码(24字节,出厂默认全F),
  64. * 校验用户PIN码成功后,加密锁获得用户权限,校验开发商PIN码后,加密锁获得开发商权限,
  65. * 不校验PIN码,加密锁为匿名权限。
  66. *
  67. * @param hDongle [in] 打开的加密锁的句柄
  68. * @param nFlag [in] PIN码类型, 1表示用户pin,0表示开发商pin
  69. * @param pbPin [in] PIN码的值
  70. * @param pTryCount [out] 剩余重试次数,返回0表示已锁死;1~253表示剩余次数;255表示不限制
  71. *
  72. * @return DONGLE_SUCCESS 表示校验PIN码成功
  73. */
  74. DWORD WINAPI Dongle_VerifyPIN(DONGLE_HANDLE hDongle, int nFlag, BYTE * pbPin, BYTE * pTryCount);
  75. /**
  76. * @brief 种子码产生用户PIN码。最小调用权限:开发商。输入种子码产生用户PIN码,用户PIN码只能用种子码产生,且不可修改,但是
  77. * 可以开发商权限下重复生成。
  78. *
  79. * @param hDongle [in] 打开加密锁的句柄 。
  80. * @param bySeed [in] 种子码数据缓冲区指针。
  81. * @param nSeedLen [in] 参数bySeed的缓冲区长度,大于0且小于等于1023。
  82. * @param bTryCount [in] 用户PIN重试次数。取值范围为0~255,255表示不限重试次数
  83. * @param szUserPIN [out] 用户PIN码。存放产生的用户PIN码数据。该缓冲区大小至少应该为8字节,
  84. * 返回字符串长度为8字节的以0结尾的ansi字符串。
  85. *
  86. * @return DONGLE_SUCCESS 产生用户PIN码成功。
  87. */
  88. DWORD WINAPI Dongle_GenUserPIN(DONGLE_HANDLE hDongle, BYTE * bySeed, WORD wSeedLen, BYTE bTryCount, char * szUserPIN);
  89. /**
  90. * @brief 读加密锁的数据区。数据区大小根据不同类型的锁分为256字节、1024字节、4096字节;数据区平分位高低数据空间
  91. * 低数据空间任何权限可读,用户以上权限可写;高数据空间用户以上权限可读,只有开发商可写。
  92. *
  93. * @param hDongle [in] 打开的加密锁的句柄
  94. * @param wOffset [in] 数据区的偏移值
  95. * @param wLen [in] 读取数据区的长度
  96. * @param pBuffer [out] 存放读取的数据
  97. *
  98. * @return DONGLE_SUCCESS 表示读数据区成功
  99. */
  100. DWORD WINAPI Dongle_ReadData(DONGLE_HANDLE hDongle, WORD wOffset, WORD wLen, BYTE * pBuffer);
  101. /**
  102. * @brief 写加密锁的数据区。数据区大小根据不同类型的锁分为256字节、1024字节、4096字节;数据区平分位高低数据空间
  103. * 低数据空间任何权限可读,用户以上权限可写;高数据空间用户以上权限可读,只有开发商可写。
  104. *
  105. * @param hDongle [in] 打开的加密锁的句柄
  106. * @param wOffset [in] 数据区的偏移值
  107. * @param wLen [in] 读取数据区的长度
  108. * @param pBuffer [in] 写入数据区的数据
  109. *
  110. * @return DONGLE_SUCCESS 表示写数据区成功
  111. */
  112. DWORD WINAPI Dongle_WriteData(DONGLE_HANDLE hDongle, WORD wOffset, WORD wLen, BYTE * pBuffer);
  113. /**
  114. * @brief 获取设备硬件序列号。硬件序列号长度固定为8字节大小。的最小调用权限:匿名。
  115. *
  116. * @param hDongle [in] 打开的加密锁句柄
  117. * @param bySN [out] 存放获取的硬件序列号,至少分配8字节空间
  118. */
  119. DWORD WINAPI Dongle_GetSerialNumber(DONGLE_HANDLE hDongle, BYTE * bySN);
  120. /**
  121. * @brief 获取用户ID号。最小调用权限:匿名。
  122. *
  123. * @param hDongle [in] 打开的加密锁的句柄
  124. * @param pUID [out] 存放用户ID值,8字节
  125. *
  126. * @return DONGLE_SUCCESS 表示获取用户ID成功
  127. */
  128. DWORD WINAPI Dongle_GetUserID(DONGLE_HANDLE hDongle, BYTE * pUID);
  129. /**
  130. * @brief 设置用户ID号。最小调用权限:开发商。
  131. *
  132. * @param hDongle [in] 打开的加密锁的句柄
  133. * @param nUID [in] 用户ID值,8字节
  134. *
  135. * @return DONGLE_SUCCESS 表示设置用户ID成功
  136. */
  137. DWORD WINAPI Dongle_SetUserID(DONGLE_HANDLE hDongle, BYTE * pUID);
  138. /**
  139. * @brief 修改开发商PIN码。最小调用权限:开发商权限。
  140. *
  141. * @param hDongle [in] 打开的加密锁的句柄
  142. * @param szOldPin [in] 旧的开发商PIN码值
  143. * @param szNewPin [in] 新的开发商PIN码值
  144. *
  145. * @return DONGLE_SUCCESS 表示修改PIN码成功
  146. */
  147. DWORD WINAPI Dongle_ChangeAdminPIN(DONGLE_HANDLE hDongle, char * szOldPin, char * szNewPin);
  148. /**
  149. * @brief 设置指定计次模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:开发商。
  150. *
  151. * @param hDongle [in] 打开的加密锁的句柄
  152. * @param bModule [in] 模块号
  153. * @param wCount [in] 计次模块值,范围为 1~65535
  154. * @param bLic [in] 模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  155. *
  156. * @return DONGLE_SUCCESS 设置计次模块值成功
  157. */
  158. DWORD WINAPI Dongle_SetModuleCount(DONGLE_HANDLE hDongle, BYTE bModule, WORD wCount, BYTE bLic);
  159. /**
  160. * @brief 获取指定计次模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:用户。
  161. *
  162. * @param hDongle [in] 打开的加密锁的句柄
  163. * @param bModule [in] 模块号
  164. * @param pwCount [in] 存放计次模块值
  165. * @param pbLic [out] 模块属性。0 (普通模块), 1 (自动递减模块), 2(永久有效模块)
  166. *
  167. * @return DONGLE_SUCCESS 获取计次模块值成功
  168. */
  169. DWORD WINAPI Dongle_GetModuleCount(DONGLE_HANDLE hDongle, BYTE bModule, DWORD * pwCount, BYTE *pbLic);
  170. /**
  171. * @brief 递减指定计次模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:用户。
  172. *
  173. * @param hDongle [in] 打开的加密锁的句柄
  174. * @param bModule [in] 模块号
  175. *
  176. * @return DONGLE_SUCCESS 递减指定计次模块值成功
  177. */
  178. DWORD WINAPI Dongle_DecModuleCount(DONGLE_HANDLE hDongle, BYTE bModule);
  179. /**
  180. * @brief 设置指定计时模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:开发商。
  181. *
  182. * @param hDongle [in] 打开的加密锁的句柄
  183. * @param bModule [in] 模块号
  184. * @param dwStartTime [in] 计时模块开始时间值
  185. * @param dwEndTime [in] 计时模块结束之间值
  186. * @param dwTimeLength [in] 计时模块有效时长值
  187. * @param bLic [in] 模块属性。1 (起止时间型), 2(有效时长型)
  188. *
  189. * @return DONGLE_SUCCESS 设置计时模块值成功
  190. */
  191. DWORD WINAPI Dongle_SetTimeModule(DONGLE_HANDLE hDongle, BYTE bModule, DWORD dwStartTime, DWORD dwEndTime, DWORD dwTimeLength, BYTE bLic);
  192. /**
  193. * @brief 获取指定计时模块的模块值。计次模块根据不同类型的锁分为4个、16个、64个。最小调用权限:用户。
  194. *
  195. * @param hDongle [in] 打开的加密锁的句柄
  196. * @param wModule [in] 模块号
  197. * @param pdwTimeLength [in] 剩余有效时长
  198. * @param pbLic [out] 模块属性。1 (起止时间型), 2(有效时长型)
  199. *
  200. * @return DONGLE_SUCCESS 获取计次模块值成功
  201. */
  202. DWORD WINAPI Dongle_GetTimeModule(DONGLE_HANDLE hDongle, BYTE bModule, DWORD * pdwTimeLength, BYTE *pbLic);
  203. /**
  204. * @brief 设置3DES加密解密钥。根据不同类型的锁可以设置1个、3个、5个。最小调用权限:开发商。
  205. *
  206. * @param hDongle [in] 打开的加密锁的句柄
  207. * @param bKeyID [in] 密钥ID,ID从0号开始
  208. * @param pKey [in] 密钥
  209. * @param bKeyLen [in] 密钥长度8字节,16字节,24字节
  210. *
  211. * @return DONGLE_SUCCESS 设置3DES密钥成功
  212. */
  213. DWORD WINAPI Dongle_Set3DESKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE * pKey, BYTE bKeyLen);
  214. /**
  215. * @brief 3DES加解密运算。最小调用权限:用户。
  216. *
  217. * @param hDongle [in] 打开的加密锁的句柄
  218. * @param bKeyID [in] 密钥ID
  219. * @param bFlag [in] 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  220. * @param pLv [in] CBC运算模式8字节初始化向量
  221. * @param pInOutData [in,out] 输入输出数据
  222. * @param wLen [in] 输入输出数据的有效长度。有效数据长度必须为8的倍数,最大1024字节
  223. *
  224. * @return DONGLE_SUCCESS 3DES运算成功
  225. */
  226. DWORD WINAPI Dongle_3DES_Crypt_ByInsideKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE bFlag, BYTE *pLv, BYTE * pInOutData, WORD wLen);
  227. /**
  228. * @brief 3DES加解密运算。最小调用权限:用户。
  229. *
  230. * @param hDongle [in] 打开的加密锁的句柄
  231. * @param pKey [in] 密钥
  232. * @param bKeyLen [in] 密钥长度8字节,16字节,24字节
  233. * @param bFlag [in] 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  234. * @param pLv [in] CBC运算模式8字节初始化向量
  235. * @param pInOutData [in,out] 输入输出数据
  236. * @param wLen [in] 输入输出数据的有效长度。有效数据长度必须为8的倍数,最大1024字节
  237. *
  238. * @return DONGLE_SUCCESS 3DES运算成功
  239. */
  240. DWORD WINAPI Dongle_3DES_Crypt_ByOutsideKey(DONGLE_HANDLE hDongle, BYTE * pKey, BYTE bKeyLen, BYTE bFlag, BYTE *pLv, BYTE * pInOutData, WORD wLen);
  241. /**
  242. * @brief 设置AES加密解密钥。根据不同类型的锁可以设置1个、3个、5个。最小调用权限:开发商。
  243. *
  244. * @param hDongle [in] 打开的加密锁的句柄
  245. * @param bKeyID [in] 密钥ID,ID从0号开始
  246. * @param pKey [in] 密钥
  247. * @param bKeyLen [in] 密钥长度16字节,24字节,32字节
  248. *
  249. * @return DONGLE_SUCCESS 设置AES密钥成功
  250. */
  251. DWORD WINAPI Dongle_SetAESKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE * pKey, BYTE bKeyLen);
  252. /**
  253. * @brief AES加解密运算。最小调用权限:用户。
  254. *
  255. * @param hDongle [in] 打开的加密锁的句柄
  256. * @param bKeyID [in] 密钥ID
  257. * @param bFlag [in] 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  258. * @param pLv [in] CBC运算模式16字节初始化向量
  259. * @param pInOutData [in,out] 输入输出数据
  260. * @param wLen [in] 输入输出数据的有效长度。有效数据长度必须为16的倍数,最大1024字节
  261. *
  262. * @return DONGLE_SUCCESS AES运算成功
  263. */
  264. DWORD WINAPI Dongle_AES_Crypt_ByInsideKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE bFlag, BYTE *pLv, BYTE * pInOutData, WORD wLen);
  265. /**
  266. * @brief AES加解密运算。最小调用权限:用户。
  267. *
  268. * @param hDongle [in] 打开的加密锁的句柄
  269. * @param pKey [in] 密钥
  270. * @param bKeyLen [in] 密钥长度16字节,24字节,32字节
  271. * @param bFlag [in] 运算类型。0x00表示ECB模式加密,0x01表示ECB模式解密,0x10表示CBC模式加密,0x11表示CBC模式解密
  272. * @param pLv [in] CBC运算模式16字节初始化向量
  273. * @param pInOutData [in,out] 输入输出数据
  274. * @param wLen [in] 输入输出数据的有效长度。有效数据长度必须为16的倍数,最大1024字节
  275. *
  276. * @return DONGLE_SUCCESS AES运算成功
  277. */
  278. DWORD WINAPI Dongle_AES_Crypt_ByOutsideKey(DONGLE_HANDLE hDongle, BYTE * pKey, BYTE bKeyLen, BYTE bFlag, BYTE *pLv, BYTE * pInOutData, WORD wLen);
  279. /**
  280. * @brief 种子码运算。此算法与用户PIN和PID有关,这两个值有变化的话同样的种子运算出的结果不同。最小调用权限:用户。
  281. *
  282. * @param hDongle [in] 打开的加密锁的句柄。
  283. * @param pSeedOrResult [inout] 种子码及运算结果数据。
  284. * @param bSeedLen [in] 种子码数据有效长度。取值范围为1~1024字节。
  285. *
  286. * @return DONGLE_SUCCESS 种子码运算成功
  287. */
  288. DWORD WINAPI Dongle_Seed(DONGLE_HANDLE hDongle, BYTE * pSeedOrResult, BYTE bSeedLen);
  289. /**
  290. * @brief Sha摘要算法,最小调用权限:匿名。
  291. *
  292. * @param hDongle [in] 打开的加密锁的句柄。
  293. * @param bFlag [in] 运算类型。0x01表示SHA-1,0x02表示SHA-256,0x03表示SHA-512
  294. * @param pSrc [in] 摘要数据。
  295. * @param wSrcLen [in] 摘要数据有效长度。取值范围为1~1024字节。
  296. * @param pResult [out] 输出数据缓冲区。20字节、32字节、64字节。
  297. *
  298. * @return DONGLE_SUCCESS 运算成功
  299. */
  300. DWORD WINAPI Dongle_Sha(DONGLE_HANDLE hDongle, BYTE bFlag, BYTE * pSrc, WORD wSrcLen, BYTE * pResult);
  301. /**
  302. * @brief 产生一个指定长度的随机数。最小调用权限:匿名。
  303. *
  304. * @param hDongle [in] 打开的加密锁的句柄。
  305. * @param bRandLen [in] 随机数长度。取值范围为0--255字节。
  306. * @param pRandom [out] 输出数据缓冲区。
  307. *
  308. * @return DONGLE_SUCCESS 产生随机数成功
  309. */
  310. DWORD WINAPI Dongle_Get_Random(DONGLE_HANDLE hDongle, BYTE bRandLen, BYTE * pRandom);
  311. /**
  312. * @brief 设置RSA加解密密钥。根据不同类型的锁可以设置1个、3个、5个。最小调用权限:开发商。
  313. *
  314. * @param hDongle [in] 打开的加密锁的句柄
  315. * @param bKeyID [in] 密钥ID
  316. * @param pKey [in] RSA密钥
  317. *
  318. * @return DONGLE_SUCCESS 设置RSA密钥成功
  319. */
  320. DWORD WINAPI Dongle_SetRSAKey(DONGLE_HANDLE hDongle, BYTE bKeyID, RSA_KEY *pKey);
  321. /**
  322. * @brief RSA运算。最小调用权限:用户。
  323. *
  324. * @param hDongle [in] 打开的加密锁的句柄
  325. * @param bKeyID [in] 密钥ID
  326. * @param bFlag [in] 运算类型。0x00公钥运算, 0x01私钥运算
  327. * @param pData [in] 输入数据缓冲区
  328. * @param wDataLen [in] 输入数据有效长度
  329. *
  330. * @return DONGLE_SUCCESS 运算成功
  331. */
  332. DWORD WINAPI Dongle_RSA(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE bFlag, BYTE * pData, WORD wDataLen);
  333. /**
  334. * @brief 生成RSA密钥对。最小调用权限:开发商。
  335. *
  336. * @param hDongle [in] 打开的加密锁的句柄
  337. * @param bKeyID [in] 密钥ID
  338. * @param bBits [in] 密钥位数:0x01=512位;0x02=1024位;0x04=2048位
  339. * @param bOperation [in] 运算类型。0x00只导出公钥, 0x01导出公私鈅
  340. * @param pKey [in] 输缓冲区
  341. *
  342. * @return DONGLE_SUCCESS 运算成功
  343. */
  344. DWORD WINAPI Dongle_GenRSAKey(DONGLE_HANDLE hDongle, BYTE bKeyID, BYTE bBits, BYTE bOperation, RSA_KEY *pKey);
  345. typedef struct _UPDATE_PACKET
  346. {
  347. WORD len;
  348. }UPDATE_PACKET,*PUPDATE_PACKET;
  349. /**
  350. * @brief 制作升级数据包。
  351. *
  352. * @param pHID [in] 需要绑定的硬件序列号SN,为NULL则表示不绑定。绑定了SN后,只有该SN的加密锁才能进行升级。
  353. * @param pUpdatePacket [in] 升级配置。
  354. * @param pOut [in] 输出数据缓冲区
  355. * @param nInLen [in] 输出数据缓冲区长度和返回升级包的有效长度
  356. *
  357. * @return DONGLE_SUCCESS 制作远程升级包成功
  358. */
  359. DWORD WINAPI Dongle_MakeUpdatePacket(char * pSN, UPDATE_PACKET *pUpdatePacket, BYTE *pOut, int * pOutLen);
  360. /**
  361. * @brief 执行远程升级。最小调用权限:匿名。
  362. *
  363. * @param hDongle [in] 打开的加密锁的句柄
  364. * @param pBuffer [in] 升级数据
  365. * @param nBufferLen [in] 升级数据长度
  366. *
  367. * @return DONGLE_SUCCESS 执行升级成功
  368. */
  369. DWORD WINAPI Dongle_Update(DONGLE_HANDLE hDongle, BYTE * pBuffer, int nBufferLen);
  370. /**
  371. * @brief 清COS, 不需要任何权限。
  372. *
  373. * @param hDongle [in] 打开的加密锁的句柄
  374. */
  375. DWORD WINAPI Dongle_ClearCos(DONGLE_HANDLE hDongle);
  376. /**
  377. * @brief 加密锁出厂设置。
  378. *
  379. * @param hDongle [in] 打开的加密锁句柄。
  380. * @param byInitBuf [in] 初始化数据。
  381. *
  382. * @return DONGLE_SUCCESS 初始化锁成功。
  383. */
  384. DWORD WINAPI Dongle_Init(DONGLE_HANDLE hDongle, BYTE * byInitBuf);
  385. #ifdef __cplusplus
  386. }
  387. #endif
  388. #endif


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/325054
推荐阅读
相关标签
  

闽ICP备14008679号