sm4test.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. static int test_sm4(void)
  17. {
  18. const uint8_t user_key[16] = {
  19. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  20. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  21. };
  22. const uint32_t rk[32] = {
  23. 0xf12186f9, 0x41662b61, 0x5a6ab19a, 0x7ba92077,
  24. 0x367360f4, 0x776a0c61, 0xb6bb89b3, 0x24763151,
  25. 0xa520307c, 0xb7584dbd, 0xc30753ed, 0x7ee55b57,
  26. 0x6988608c, 0x30d895b7, 0x44ba14af, 0x104495a1,
  27. 0xd120b428, 0x73b55fa3, 0xcc874966, 0x92244439,
  28. 0xe89e641f, 0x98ca015a, 0xc7159060, 0x99e1fd2e,
  29. 0xb79bd80c, 0x1d2115b0, 0x0e228aeb, 0xf1780c81,
  30. 0x428d3654, 0x62293496, 0x01cf72e5, 0x9124a012,
  31. };
  32. const uint8_t plaintext[16] = {
  33. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  34. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  35. };
  36. const uint8_t ciphertext[16] = {
  37. 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e,
  38. 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46,
  39. };
  40. const uint8_t ciphertext1m[16] = {
  41. 0x59, 0x52, 0x98, 0xc7, 0xc6, 0xfd, 0x27, 0x1f,
  42. 0x04, 0x02, 0xf8, 0x04, 0xc3, 0x3d, 0x3f, 0x66,
  43. };
  44. SM4_KEY key;
  45. unsigned char buf[16];
  46. int i;
  47. /* test key scheduling */
  48. sm4_set_encrypt_key(&key, user_key);
  49. if (memcmp(key.rk, rk, sizeof(rk)) != 0) {
  50. fprintf(stderr, "sm4 key scheduling not passed!\n");
  51. return -1;
  52. }
  53. /* test encrypt once */
  54. sm4_encrypt(&key, plaintext, buf);
  55. if (memcmp(buf, ciphertext, sizeof(ciphertext)) != 0) {
  56. fprintf(stderr, "sm4 encrypt not pass!\n");
  57. return -1;
  58. }
  59. /* test encrypt 1000000 times */
  60. memcpy(buf, plaintext, sizeof(plaintext));
  61. for (i = 0; i < 1000000; i++) {
  62. sm4_encrypt(&key, buf, buf);
  63. }
  64. if (memcmp(buf, ciphertext1m, sizeof(ciphertext1m)) != 0) {
  65. fprintf(stderr, "sm4 encrypt 1000000 times not pass!\n");
  66. return -1;
  67. }
  68. /* test decrypt */
  69. memset(&key, 0, sizeof(key));
  70. memset(buf, 0, sizeof(buf));
  71. sm4_set_decrypt_key(&key, user_key);
  72. sm4_decrypt(&key, ciphertext, buf);
  73. if (memcmp(buf, plaintext, sizeof(plaintext)) != 0) {
  74. fprintf(stderr, "sm4 decrypt not pass!\n");
  75. return -1;
  76. }
  77. printf("%s() ok\n", __FUNCTION__);
  78. return 1;
  79. }
  80. static int test_sm4_cbc(void)
  81. {
  82. SM4_KEY sm4_key;
  83. uint8_t key[16] = {0};
  84. uint8_t iv[16] = {0};
  85. uint8_t buf1[32] = {0};
  86. uint8_t buf2[32] = {0};
  87. uint8_t buf3[32] = {0};
  88. sm4_set_encrypt_key(&sm4_key, key);
  89. sm4_cbc_encrypt(&sm4_key, iv, buf1, 2, buf2);
  90. sm4_set_decrypt_key(&sm4_key, key);
  91. sm4_cbc_decrypt(&sm4_key, iv, buf2, 2, buf3);
  92. if (memcmp(buf1, buf3, sizeof(buf3)) != 0) {
  93. fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
  94. return -1;
  95. }
  96. printf("%s() ok\n", __FUNCTION__);
  97. return 1;
  98. }
  99. static int test_sm4_cbc_padding(void)
  100. {
  101. SM4_KEY enc_key;
  102. SM4_KEY dec_key;
  103. uint8_t key[16] = {0};
  104. uint8_t iv[16] = {0};
  105. uint8_t buf1[64];
  106. uint8_t buf2[128];
  107. uint8_t buf3[128];
  108. size_t len1, len2, len3;
  109. sm4_set_encrypt_key(&enc_key, key);
  110. sm4_set_decrypt_key(&dec_key, key);
  111. len1 = 0;
  112. sm4_cbc_padding_encrypt(&enc_key, iv, buf1, len1, buf2, &len2);
  113. sm4_cbc_padding_decrypt(&dec_key, iv, buf2, len2, buf3, &len3);
  114. if (len1 != len3 || memcmp(buf1, buf3, len3) != 0) {
  115. fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
  116. return -1;
  117. }
  118. len1 = 7;
  119. sm4_cbc_padding_encrypt(&enc_key, iv, buf1, len1, buf2, &len2);
  120. sm4_cbc_padding_decrypt(&dec_key, iv, buf2, len2, buf3, &len3);
  121. if (len1 != len3 || memcmp(buf1, buf3, len3) != 0) {
  122. fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
  123. return -1;
  124. }
  125. len1 = 16;
  126. sm4_cbc_padding_encrypt(&enc_key, iv, buf1, len1, buf2, &len2);
  127. sm4_cbc_padding_decrypt(&dec_key, iv, buf2, len2, buf3, &len3);
  128. if (len1 != len3 || memcmp(buf1, buf3, len3) != 0) {
  129. fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
  130. return -1;
  131. }
  132. len1 = 33;
  133. sm4_cbc_padding_encrypt(&enc_key, iv, buf1, len1, buf2, &len2);
  134. sm4_cbc_padding_decrypt(&dec_key, iv, buf2, len2, buf3, &len3);
  135. if (len1 != len3 || memcmp(buf1, buf3, len3) != 0) {
  136. fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
  137. return -1;
  138. }
  139. len1 = sizeof(buf1);
  140. sm4_cbc_padding_encrypt(&enc_key, iv, buf1, len1, buf2, &len2);
  141. sm4_cbc_padding_decrypt(&dec_key, iv, buf2, len2, buf3, &len3);
  142. if (len1 != len3 || memcmp(buf1, buf3, len3) != 0) {
  143. fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
  144. return -1;
  145. }
  146. printf("%s() ok\n", __FUNCTION__);
  147. return 1;
  148. }
  149. static int test_sm4_ctr(void)
  150. {
  151. SM4_KEY sm4_key;
  152. uint8_t key[16] = {0};
  153. uint8_t ctr[16];
  154. uint8_t buf1[30] = {0};
  155. uint8_t buf2[30] = {0};
  156. uint8_t buf3[30] = {0};
  157. sm4_set_encrypt_key(&sm4_key, key);
  158. memset(ctr, 0, sizeof(ctr));
  159. sm4_ctr_encrypt(&sm4_key, ctr, buf1, sizeof(buf1), buf2);
  160. memset(ctr, 0, sizeof(ctr));
  161. sm4_ctr_decrypt(&sm4_key, ctr, buf2, sizeof(buf2), buf3);
  162. if (memcmp(buf1, buf3, sizeof(buf3)) != 0) {
  163. fprintf(stderr, "%s %d: error\n", __FILE__, __LINE__);
  164. return -1;
  165. }
  166. printf("%s() ok\n", __FUNCTION__);
  167. return 1;
  168. }
  169. static int test_sm4_ctr_with_carray(void)
  170. {
  171. const char *hex_key = "0123456789ABCDEFFEDCBA9876543210";
  172. const char *hex_ctr = "0000000000000000000000000000FFFF";
  173. const char *hex_in = "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB"
  174. "CCCCCCCCCCCCCCCCDDDDDDDDDDDD";
  175. const char *hex_out = "7EA678F9F0CBE2000917C63D4E77B4C8"
  176. "6E4E8532B0046E4AC1E97DA8B831";
  177. SM4_KEY sm4_key;
  178. uint8_t key[16] = {0};
  179. uint8_t ctr[16];
  180. uint8_t buf1[30] = {0};
  181. uint8_t buf2[30] = {0};
  182. uint8_t buf3[30] = {0};
  183. size_t keylen, ctrlen, inlen, outlen;
  184. hex_to_bytes(hex_key, strlen(hex_key), key, &keylen);
  185. hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
  186. hex_to_bytes(hex_in, strlen(hex_in), buf1, &inlen);
  187. hex_to_bytes(hex_out, strlen(hex_out), buf3, &outlen);
  188. sm4_set_encrypt_key(&sm4_key, key);
  189. sm4_ctr_encrypt(&sm4_key, ctr, buf1, sizeof(buf1), buf2);
  190. if (memcmp(buf2, buf3, sizeof(buf3)) != 0) {
  191. error_print();
  192. return -1;
  193. }
  194. hex_to_bytes(hex_ctr, strlen(hex_ctr), ctr, &ctrlen);
  195. sm4_ctr_decrypt(&sm4_key, ctr, buf3, sizeof(buf3), buf2);
  196. if (memcmp(buf2, buf1, sizeof(buf1)) != 0) {
  197. error_print();
  198. return -1;
  199. }
  200. printf("%s() ok\n", __FUNCTION__);
  201. return 1;
  202. }
  203. static int test_sm4_gcm(void)
  204. {
  205. // gcm test vectors from rfc 8998 A.1
  206. const char *hex_key = "0123456789ABCDEFFEDCBA9876543210";
  207. const char *hex_iv = "00001234567800000000ABCD";
  208. const char *hex_aad = "FEEDFACEDEADBEEFFEEDFACEDEADBEEF"
  209. "ABADDAD2";
  210. const char *hex_in = "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBB"
  211. "CCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDD"
  212. "EEEEEEEEEEEEEEEEFFFFFFFFFFFFFFFF"
  213. "EEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAA";
  214. const char *hex_out = "17F399F08C67D5EE19D0DC9969C4BB7D"
  215. "5FD46FD3756489069157B282BB200735"
  216. "D82710CA5C22F0CCFA7CBF93D496AC15"
  217. "A56834CBCF98C397B4024A2691233B8D";
  218. const char *hex_tag = "83DE3541E4C2B58177E065A9BF7B62EC";
  219. SM4_KEY sm4_key;
  220. uint8_t key[16];
  221. uint8_t iv[12];
  222. uint8_t aad[20];
  223. uint8_t in[64];
  224. uint8_t out[64];
  225. uint8_t tag[16];
  226. size_t keylen, ivlen, aadlen, inlen, outlen, taglen;
  227. uint8_t buf[64];
  228. uint8_t mac[16];
  229. hex_to_bytes(hex_key, strlen(hex_key), key, &keylen);
  230. hex_to_bytes(hex_iv, strlen(hex_iv), iv, &ivlen);
  231. hex_to_bytes(hex_aad, strlen(hex_aad), aad, &aadlen);
  232. hex_to_bytes(hex_in, strlen(hex_in), in, &inlen);
  233. hex_to_bytes(hex_out, strlen(hex_out), out, &outlen);
  234. hex_to_bytes(hex_tag, strlen(hex_tag), tag, &taglen);
  235. memset(buf, 0, sizeof(buf));
  236. memset(mac, 0, sizeof(mac));
  237. sm4_set_encrypt_key(&sm4_key, key);
  238. // test gcm encrypt
  239. sm4_gcm_encrypt(&sm4_key, iv, ivlen, aad, aadlen, in, inlen, buf, taglen, mac);
  240. if (memcmp(buf, out, outlen) != 0) {
  241. error_print();
  242. return -1;
  243. }
  244. if (memcmp(mac, tag, taglen) != 0) {
  245. error_print();
  246. return -1;
  247. }
  248. // test gcm decrypt
  249. memset(buf, 0, sizeof(buf));
  250. sm4_gcm_decrypt(&sm4_key, iv, ivlen, aad, aadlen, out, outlen, tag, taglen, buf);
  251. if (memcmp(buf, in, inlen) != 0) {
  252. error_print();
  253. return -1;
  254. }
  255. printf("%s() ok\n", __FUNCTION__);
  256. return 1;
  257. }
  258. static int test_sm4_gcm_gbt36624_1(void)
  259. {
  260. // gcm test vectors from GB/T 36624-2018 C.5
  261. const char *hex_key = "00000000000000000000000000000000";
  262. const char *hex_iv = "000000000000000000000000";
  263. const char *hex_aad = "";
  264. const char *hex_in = "";
  265. const char *hex_out = "";
  266. const char *hex_tag = "232F0CFE308B49EA6FC88229B5DC858D";
  267. SM4_KEY sm4_key;
  268. uint8_t key[16];
  269. uint8_t iv[12];
  270. uint8_t aad[20];
  271. uint8_t in[64];
  272. uint8_t out[64];
  273. uint8_t tag[16];
  274. size_t keylen, ivlen, aadlen, inlen, outlen, taglen;
  275. uint8_t buf[64];
  276. uint8_t mac[16];
  277. hex_to_bytes(hex_key, strlen(hex_key), key, &keylen);
  278. hex_to_bytes(hex_iv, strlen(hex_iv), iv, &ivlen);
  279. hex_to_bytes(hex_aad, strlen(hex_aad), aad, &aadlen);
  280. hex_to_bytes(hex_in, strlen(hex_in), in, &inlen);
  281. hex_to_bytes(hex_out, strlen(hex_out), out, &outlen);
  282. hex_to_bytes(hex_tag, strlen(hex_tag), tag, &taglen);
  283. memset(buf, 0, sizeof(buf));
  284. memset(mac, 0, sizeof(mac));
  285. sm4_set_encrypt_key(&sm4_key, key);
  286. // test gcm encrypt
  287. sm4_gcm_encrypt(&sm4_key, iv, ivlen, aad, aadlen, in, inlen, buf, taglen, mac);
  288. if (memcmp(buf, out, outlen) != 0) {
  289. error_print();
  290. return -1;
  291. }
  292. if (memcmp(mac, tag, taglen) != 0) {
  293. error_print();
  294. return -1;
  295. }
  296. // test gcm decrypt
  297. memset(buf, 0, sizeof(buf));
  298. sm4_gcm_decrypt(&sm4_key, iv, ivlen, aad, aadlen, out, outlen, tag, taglen, buf);
  299. if (memcmp(buf, in, inlen) != 0) {
  300. error_print();
  301. return -1;
  302. }
  303. printf("%s() ok\n", __FUNCTION__);
  304. return 1;
  305. }
  306. static int test_sm4_gcm_gbt36624_2(void)
  307. {
  308. // gcm test vectors from GB/T 36624-2018 C.5
  309. const char *hex_key = "00000000000000000000000000000000";
  310. const char *hex_iv = "000000000000000000000000";
  311. const char *hex_aad = "";
  312. const char *hex_in = "00000000000000000000000000000000";
  313. const char *hex_out = "7DE2AA7F1110188218063BE1BFEB6D89";
  314. const char *hex_tag = "B851B5F39493752BE508F1BB4482C557";
  315. SM4_KEY sm4_key;
  316. uint8_t key[16];
  317. uint8_t iv[12];
  318. uint8_t aad[20];
  319. uint8_t in[64];
  320. uint8_t out[64];
  321. uint8_t tag[16];
  322. size_t keylen, ivlen, aadlen, inlen, outlen, taglen;
  323. uint8_t buf[64];
  324. uint8_t mac[16];
  325. hex_to_bytes(hex_key, strlen(hex_key), key, &keylen);
  326. hex_to_bytes(hex_iv, strlen(hex_iv), iv, &ivlen);
  327. hex_to_bytes(hex_aad, strlen(hex_aad), aad, &aadlen);
  328. hex_to_bytes(hex_in, strlen(hex_in), in, &inlen);
  329. hex_to_bytes(hex_out, strlen(hex_out), out, &outlen);
  330. hex_to_bytes(hex_tag, strlen(hex_tag), tag, &taglen);
  331. memset(buf, 0, sizeof(buf));
  332. memset(mac, 0, sizeof(mac));
  333. sm4_set_encrypt_key(&sm4_key, key);
  334. // test gcm encrypt
  335. sm4_gcm_encrypt(&sm4_key, iv, ivlen, aad, aadlen, in, inlen, buf, taglen, mac);
  336. if (memcmp(buf, out, outlen) != 0) {
  337. error_print();
  338. return -1;
  339. }
  340. if (memcmp(mac, tag, taglen) != 0) {
  341. error_print();
  342. return -1;
  343. }
  344. // test gcm decrypt
  345. memset(buf, 0, sizeof(buf));
  346. sm4_gcm_decrypt(&sm4_key, iv, ivlen, aad, aadlen, out, outlen, tag, taglen, buf);
  347. if (memcmp(buf, in, inlen) != 0) {
  348. error_print();
  349. return -1;
  350. }
  351. printf("%s() ok\n", __FUNCTION__);
  352. return 1;
  353. }
  354. static int test_sm4_cbc_update(void)
  355. {
  356. SM4_KEY sm4_key;
  357. SM4_CBC_CTX enc_ctx;
  358. SM4_CBC_CTX dec_ctx;
  359. uint8_t key[16];
  360. uint8_t iv[16];
  361. uint8_t mbuf[16 * 10];
  362. uint8_t cbuf[16 * 11];
  363. uint8_t pbuf[16 * 11];
  364. size_t mlen = 0;
  365. size_t clen = 0;
  366. size_t plen = 0;
  367. uint8_t *in;
  368. uint8_t *out;
  369. size_t len;
  370. size_t lens[] = { 1,5,17,80 };
  371. int i;
  372. rand_bytes(key, sizeof(key));
  373. rand_bytes(iv, sizeof(iv));
  374. // first test
  375. mlen = 16;
  376. rand_bytes(mbuf, mlen);
  377. if (sm4_cbc_encrypt_init(&enc_ctx, key, iv) != 1
  378. || sm4_cbc_encrypt_update(&enc_ctx, mbuf, mlen, cbuf, &clen) != 1
  379. || sm4_cbc_encrypt_finish(&enc_ctx, cbuf + clen, &len) != 1) {
  380. error_print();
  381. return -1;
  382. }
  383. clen += len;
  384. // check ciphertext
  385. sm4_set_encrypt_key(&sm4_key, key);
  386. sm4_cbc_padding_encrypt(&sm4_key, iv, mbuf, mlen, pbuf, &plen);
  387. if (clen != plen || memcmp(cbuf, pbuf, plen) != 0) {
  388. error_print();
  389. return -1;
  390. }
  391. // check decrypt
  392. if (sm4_cbc_decrypt_init(&dec_ctx, key, iv) != 1
  393. || sm4_cbc_decrypt_update(&dec_ctx, cbuf, clen, pbuf, &plen) != 1
  394. || sm4_cbc_decrypt_finish(&dec_ctx, pbuf + plen, &len) != 1) {
  395. error_print();
  396. return -1;
  397. }
  398. plen += len;
  399. if (plen != mlen || memcmp(pbuf, mbuf, mlen) != 0) {
  400. error_print();
  401. return -1;
  402. }
  403. // second test
  404. rand_bytes(mbuf, sizeof(mbuf));
  405. if (sm4_cbc_encrypt_init(&enc_ctx, key, iv) != 1) {
  406. error_print();
  407. return -1;
  408. }
  409. in = mbuf;
  410. out = cbuf;
  411. mlen = 0;
  412. clen = 0;
  413. for (i = 0; i < sizeof(lens)/sizeof(lens[0]); i++) {
  414. if (sm4_cbc_encrypt_update(&enc_ctx, in, lens[i], out, &len) != 1) {
  415. error_print();
  416. return -1;
  417. }
  418. in += lens[i];
  419. mlen += lens[i];
  420. out += len;
  421. clen += len;
  422. }
  423. if (sm4_cbc_encrypt_finish(&enc_ctx, out, &len) != 1) {
  424. error_print();
  425. return -1;
  426. }
  427. clen += len;
  428. // check ciphertest
  429. sm4_cbc_padding_encrypt(&sm4_key, iv, mbuf, mlen, pbuf, &plen);
  430. if (plen != clen || memcmp(pbuf, cbuf, clen) != 0) {
  431. error_print();
  432. return -1;
  433. }
  434. // check decrypt
  435. if (sm4_cbc_decrypt_init(&dec_ctx, key, iv) != 1) {
  436. error_print();
  437. return -1;
  438. }
  439. plen = 0;
  440. in = cbuf;
  441. out = pbuf;
  442. for (i = 0; i < sizeof(lens)/sizeof(lens[0]); i++) {
  443. if (sm4_cbc_decrypt_update(&dec_ctx, in, lens[i], out, &len) != 1) {
  444. error_print();
  445. return -1;
  446. }
  447. in += lens[i];
  448. clen -= lens[i];
  449. out += len;
  450. plen += len;
  451. }
  452. if (sm4_cbc_decrypt_update(&dec_ctx, in, clen, out, &len) != 1) {
  453. error_print();
  454. return -1;
  455. }
  456. out += len;
  457. plen += len;
  458. if (sm4_cbc_decrypt_finish(&dec_ctx, out, &len) != 1) {
  459. error_print();
  460. return -1;
  461. }
  462. plen += len;
  463. if (plen != mlen || memcmp(pbuf, mbuf, mlen) != 0) {
  464. error_print();
  465. return -1;
  466. }
  467. printf("%s() ok\n", __FUNCTION__);
  468. return 1;
  469. }
  470. static int test_sm4_ctr_update(void)
  471. {
  472. SM4_KEY sm4_key;
  473. SM4_CTR_CTX enc_ctx;
  474. SM4_CTR_CTX dec_ctx;
  475. uint8_t key[16];
  476. uint8_t iv[16];
  477. uint8_t ctr[16];
  478. uint8_t mbuf[16 * 10];
  479. uint8_t cbuf[16 * 11];
  480. uint8_t pbuf[16 * 11];
  481. size_t mlen = 0;
  482. size_t clen = 0;
  483. size_t plen = 0;
  484. uint8_t *in;
  485. uint8_t *out;
  486. size_t len;
  487. size_t lens[] = { 1,5,17,80 };
  488. int i;
  489. rand_bytes(key, sizeof(key));
  490. rand_bytes(iv, sizeof(iv));
  491. // first test
  492. mlen = 16;
  493. rand_bytes(mbuf, mlen);
  494. memcpy(ctr, iv, sizeof(iv));
  495. if (sm4_ctr_encrypt_init(&enc_ctx, key, ctr) != 1
  496. || sm4_ctr_encrypt_update(&enc_ctx, mbuf, mlen, cbuf, &clen) != 1
  497. || sm4_ctr_encrypt_finish(&enc_ctx, cbuf + clen, &len) != 1) {
  498. error_print();
  499. return -1;
  500. }
  501. clen += len;
  502. // check ciphertext
  503. sm4_set_encrypt_key(&sm4_key, key);
  504. sm4_ctr_encrypt(&sm4_key, ctr, mbuf, mlen, pbuf); // 注意:sm4_ctr_encrypt() 会修改ctr的值
  505. memcpy(ctr, iv, sizeof(iv));
  506. if (memcmp(cbuf, pbuf, clen) != 0) {
  507. error_print();
  508. return -1;
  509. }
  510. // check decrypt
  511. if (sm4_ctr_decrypt_init(&dec_ctx, key, ctr) != 1
  512. || sm4_ctr_decrypt_update(&dec_ctx, cbuf, clen, pbuf, &plen) != 1
  513. || sm4_ctr_decrypt_finish(&dec_ctx, pbuf + plen, &len) != 1) {
  514. error_print();
  515. return -1;
  516. }
  517. plen += len;
  518. if (plen != mlen || memcmp(pbuf, mbuf, mlen) != 0) {
  519. error_print();
  520. return -1;
  521. }
  522. // second test
  523. rand_bytes(mbuf, sizeof(mbuf));
  524. if (sm4_ctr_encrypt_init(&enc_ctx, key, ctr) != 1) {
  525. error_print();
  526. return -1;
  527. }
  528. in = mbuf;
  529. out = cbuf;
  530. mlen = 0;
  531. clen = 0;
  532. for (i = 0; i < sizeof(lens)/sizeof(lens[0]); i++) {
  533. if (sm4_ctr_encrypt_update(&enc_ctx, in, lens[i], out, &len) != 1) {
  534. error_print();
  535. return -1;
  536. }
  537. in += lens[i];
  538. mlen += lens[i];
  539. out += len;
  540. clen += len;
  541. }
  542. if (sm4_ctr_encrypt_finish(&enc_ctx, out, &len) != 1) {
  543. error_print();
  544. return -1;
  545. }
  546. clen += len;
  547. // check ciphertest
  548. sm4_ctr_encrypt(&sm4_key, ctr, mbuf, mlen, pbuf);
  549. memcpy(ctr, iv, sizeof(iv));
  550. if (memcmp(pbuf, cbuf, mlen) != 0) {
  551. error_print();
  552. return -1;
  553. }
  554. // check decrypt
  555. if (sm4_ctr_decrypt_init(&dec_ctx, key, ctr) != 1) {
  556. error_print();
  557. return -1;
  558. }
  559. plen = 0;
  560. in = cbuf;
  561. out = pbuf;
  562. for (i = 0; i < sizeof(lens)/sizeof(lens[0]); i++) {
  563. if (sm4_ctr_decrypt_update(&dec_ctx, in, lens[i], out, &len) != 1) {
  564. error_print();
  565. return -1;
  566. }
  567. in += lens[i];
  568. clen -= lens[i];
  569. out += len;
  570. plen += len;
  571. }
  572. if (sm4_ctr_decrypt_update(&dec_ctx, in, clen, out, &len) != 1) {
  573. error_print();
  574. return -1;
  575. }
  576. out += len;
  577. plen += len;
  578. if (sm4_ctr_decrypt_finish(&dec_ctx, out, &len) != 1) {
  579. error_print();
  580. return -1;
  581. }
  582. plen += len;
  583. if (plen != mlen || memcmp(pbuf, mbuf, mlen) != 0) {
  584. error_print();
  585. return -1;
  586. }
  587. printf("%s() ok\n", __FUNCTION__);
  588. return 1;
  589. }
  590. int main(void)
  591. {
  592. if (test_sm4() != 1) goto err;
  593. if (test_sm4_cbc() != 1) goto err;
  594. if (test_sm4_cbc_padding() != 1) goto err;
  595. if (test_sm4_ctr() != 1) goto err;
  596. if (test_sm4_gcm() != 1) goto err;
  597. if (test_sm4_gcm_gbt36624_1() != 1) goto err;
  598. if (test_sm4_gcm_gbt36624_2() != 1) goto err;
  599. if (test_sm4_cbc_update() != 1) goto err;
  600. if (test_sm4_ctr_update() != 1) goto err;
  601. printf("%s all tests passed\n", __FILE__);
  602. return 0;
  603. err:
  604. error_print();
  605. return 1;
  606. }