sm4speed.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <stdlib.h>
  12. #include <gmssl/hex.h>
  13. #include <gmssl/sm4.h>
  14. #include <gmssl/error.h>
  15. #include <gmssl/rand.h>
  16. #ifdef WIN32
  17. #include <wincrypt.h>
  18. static volatile int finish;
  19. VOID CALLBACK TimerProc_sm4(HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
  20. {
  21. finish = 0;
  22. }
  23. int test_sm4()
  24. {
  25. uint8_t user_key[16] = {
  26. 0x01,
  27. 0x23,
  28. 0x45,
  29. 0x67,
  30. 0x89,
  31. 0xab,
  32. 0xcd,
  33. 0xef,
  34. 0xfe,
  35. 0xdc,
  36. 0xba,
  37. 0x98,
  38. 0x76,
  39. 0x54,
  40. 0x32,
  41. 0x10,
  42. };
  43. uint8_t iv[16] = {
  44. 0x01,
  45. 0x23,
  46. 0x45,
  47. 0x67,
  48. 0x89,
  49. 0xab,
  50. 0xcd,
  51. 0xef,
  52. 0xfe,
  53. 0xdc,
  54. 0xba,
  55. 0x98,
  56. 0x76,
  57. 0x54,
  58. 0x32,
  59. 0x10,
  60. };
  61. uint8_t ctr[16] = {0};
  62. uint8_t mac[16] = {0};
  63. uint8_t aad[16] = {
  64. 0x01,
  65. 0x23,
  66. 0x45,
  67. 0x67,
  68. 0x89,
  69. 0xab,
  70. 0xcd,
  71. 0xef,
  72. 0xfe,
  73. 0xdc,
  74. 0xba,
  75. 0x98,
  76. 0x76,
  77. 0x54,
  78. 0x32,
  79. 0x10,
  80. };
  81. uint8_t out[16384] = {0};
  82. SM4_KEY key;
  83. int sizebox[] = {16, 64, 256, 1024, 8192, 16384};
  84. int countbox[18] = {0};
  85. uint8_t *testhex[];
  86. HCRYPTPROV hCryptProv;
  87. testhex = (uint8_t **)malloc(sizeof(uint8_t *) * 6);
  88. for (int i = 0; i < 6; i++)
  89. {
  90. testhex[i] = (uint8_t *)malloc(sizebox[i]);
  91. CryptGenRandom(hCryptProv, sizebox[i], testhex[i]);
  92. }
  93. int count;
  94. sm4_set_encrypt_key(&key, user_key);
  95. for (int i = 0; i < 6; i++)
  96. {
  97. finish = 1;
  98. count = 0;
  99. printf("Doing sm4-cbc for 3s on %d size blocks: ", sizebox[i]);
  100. UINT_PTR iTimerID = SetTimer(NULL, 0, 3000, TimerProc_sm4);
  101. while (finish)
  102. {
  103. sm4_cbc_encrypt(&key, iv, testhex[i], sizebox[i] / 16, out);
  104. count++;
  105. }
  106. KillTimer(NULL, iTimerID);
  107. countbox[i] = count;
  108. printf("%d sm4-cbc's in 3s\n", count);
  109. }
  110. for (int i = 0; i < 6; i++)
  111. {
  112. finish = 1;
  113. count = 0;
  114. printf("Doing sm4-ctr for 3s on %d size blocks: ", sizebox[i]);
  115. UINT_PTR iTimerID = SetTimer(NULL, 0, 3000, TimerProc);
  116. while (finish)
  117. {
  118. sm4_ctr_encrypt(&key, ctr, testhex[i], sizebox[i], out);
  119. count++;
  120. }
  121. KillTimer(NULL, iTimerID);
  122. countbox[i + 6] = count;
  123. printf("%d sm4-ctr's in 3s\n", count);
  124. }
  125. for (int i = 0; i < 6; i++)
  126. {
  127. finish = 1;
  128. count = 0;
  129. printf("Doing sm4-gcm for 3s on %d size blocks: ", sizebox[i]);
  130. UINT_PTR iTimerID = SetTimer(NULL, 0, 3000, TimerProc);
  131. while (finish)
  132. {
  133. sm4_gcm_encrypt(&key, iv, 16, aad, 16, testhex[i], sizebox[i], out, 16, mac);
  134. count++;
  135. }
  136. KillTimer(NULL, iTimerID);
  137. countbox[i + 12] = count;
  138. printf("%d sm4-gcm's in 3s\n", count);
  139. }
  140. printf("type\t\t16 bytes\t64 bytes\t256 bytes\t1024 bytes\t8192 bytes\t16384 bytes\n");
  141. printf("sm4-cbc\t");
  142. for (int i = 0; i < 6; i++)
  143. {
  144. printf("\t%.2fK", countbox[i] * sizebox[i] / 1024 / 3.00);
  145. }
  146. printf("\n");
  147. printf("sm4-ctr\t");
  148. for (int i = 0; i < 6; i++)
  149. {
  150. printf("\t%.2fK", countbox[i + 6] * sizebox[i] / 1024 / 3.00);
  151. }
  152. printf("\n");
  153. printf("sm4-gcm\t");
  154. for (int i = 0; i < 6; i++)
  155. {
  156. printf("\t%.2fK", countbox[i + 12] * sizebox[i] / 1024 / 3.00);
  157. }
  158. printf("\n");
  159. for (int i = 0; i < 6; i++)
  160. {
  161. free(testhex[i]);
  162. }
  163. free(testhex);
  164. return 1;
  165. }
  166. #else
  167. #include <signal.h>
  168. #include <sys/time.h>
  169. static volatile int finish;
  170. void sig_alm_handler_sm4(int sig_num)
  171. {
  172. if (sig_num = SIGALRM)
  173. finish = 0;
  174. }
  175. int test_sm4()
  176. {
  177. uint8_t user_key[16] = {
  178. 0x01,
  179. 0x23,
  180. 0x45,
  181. 0x67,
  182. 0x89,
  183. 0xab,
  184. 0xcd,
  185. 0xef,
  186. 0xfe,
  187. 0xdc,
  188. 0xba,
  189. 0x98,
  190. 0x76,
  191. 0x54,
  192. 0x32,
  193. 0x10,
  194. };
  195. uint8_t iv[16] = {
  196. 0x01,
  197. 0x23,
  198. 0x45,
  199. 0x67,
  200. 0x89,
  201. 0xab,
  202. 0xcd,
  203. 0xef,
  204. 0xfe,
  205. 0xdc,
  206. 0xba,
  207. 0x98,
  208. 0x76,
  209. 0x54,
  210. 0x32,
  211. 0x10,
  212. };
  213. uint8_t ctr[16] = {0};
  214. uint8_t mac[16] = {0};
  215. uint8_t aad[16] = {
  216. 0x01,
  217. 0x23,
  218. 0x45,
  219. 0x67,
  220. 0x89,
  221. 0xab,
  222. 0xcd,
  223. 0xef,
  224. 0xfe,
  225. 0xdc,
  226. 0xba,
  227. 0x98,
  228. 0x76,
  229. 0x54,
  230. 0x32,
  231. 0x10,
  232. };
  233. uint8_t out[16384] = {0};
  234. int count;
  235. SM4_KEY key;
  236. int sizebox[] = {16, 64, 256, 1024, 8192, 16384};
  237. int countbox[18] = {0};
  238. uint8_t **testhex;
  239. FILE *fs_p = fopen("/dev/urandom", "r");
  240. if (NULL == fs_p)
  241. {
  242. printf("Can not open /dev/urandom\n");
  243. return -1;
  244. }
  245. testhex = (uint8_t **)malloc(sizeof(uint8_t *) * 6);
  246. for (int i = 0; i < 6; i++)
  247. {
  248. testhex[i] = (uint8_t *)malloc(sizebox[i]);
  249. fread(testhex[i], sizebox[i], 1, fs_p);
  250. }
  251. fclose(fs_p);
  252. signal(SIGALRM, sig_alm_handler_sm4);
  253. struct itimerval new_value, old_value;
  254. new_value.it_value.tv_sec = 3;
  255. new_value.it_value.tv_usec = 0;
  256. new_value.it_interval.tv_sec = 0;
  257. new_value.it_interval.tv_usec = 0;
  258. sm4_set_encrypt_key(&key, user_key);
  259. for (int i = 0; i < 6; i++)
  260. {
  261. finish = 1;
  262. count = 0;
  263. printf("Doing sm4-cbc for 3s on %d size blocks: ", sizebox[i]);
  264. setitimer(ITIMER_REAL, &new_value, &old_value);
  265. while (finish)
  266. {
  267. sm4_cbc_encrypt(&key, iv, testhex[i], sizebox[i] / 16, out);
  268. count++;
  269. }
  270. countbox[i] = count;
  271. printf("%d sm4-cbc's in 3s\n", count);
  272. }
  273. for (int i = 0; i < 6; i++)
  274. {
  275. finish = 1;
  276. count = 0;
  277. printf("Doing sm4-ctr for 3s on %d size blocks: ", sizebox[i]);
  278. setitimer(ITIMER_REAL, &new_value, &old_value);
  279. while (finish)
  280. {
  281. sm4_ctr_encrypt(&key, ctr, testhex[i], sizebox[i], out);
  282. count++;
  283. }
  284. countbox[i + 6] = count;
  285. printf("%d sm4-ctr's in 3s\n", count);
  286. }
  287. for (int i = 0; i < 6; i++)
  288. {
  289. finish = 1;
  290. count = 0;
  291. printf("Doing sm4-gcm for 3s on %d size blocks: ", sizebox[i]);
  292. setitimer(ITIMER_REAL, &new_value, &old_value);
  293. while (finish)
  294. {
  295. sm4_gcm_encrypt(&key, iv, 16, aad, 16, testhex[i], sizebox[i], out, 16, mac);
  296. count++;
  297. }
  298. countbox[i + 12] = count;
  299. printf("%d sm4-gcm's in 3s\n", count);
  300. }
  301. printf("type\t\t16 bytes\t64 bytes\t256 bytes\t1024 bytes\t8192 bytes\t16384 bytes\n");
  302. printf("sm4-cbc\t");
  303. for (int i = 0; i < 6; i++)
  304. {
  305. printf("\t%.2fK", countbox[i] * sizebox[i] / 1024 / 3.00);
  306. }
  307. printf("\n");
  308. printf("sm4-ctr\t");
  309. for (int i = 0; i < 6; i++)
  310. {
  311. printf("\t%.2fK", countbox[i + 6] * sizebox[i] / 1024 / 3.00);
  312. }
  313. printf("\n");
  314. printf("sm4-gcm\t");
  315. for (int i = 0; i < 6; i++)
  316. {
  317. printf("\t%.2fK", countbox[i + 12] * sizebox[i] / 1024 / 3.00);
  318. }
  319. printf("\n");
  320. for (int i = 0; i < 6; i++)
  321. {
  322. free(testhex[i]);
  323. }
  324. free(testhex);
  325. return 1;
  326. }
  327. #endif
  328. int sm4speed_main(void)
  329. {
  330. test_sm4();
  331. return 1;
  332. }