sm4.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright 2014-2023 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 <errno.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <gmssl/mem.h>
  14. #include <gmssl/sm4.h>
  15. #include <gmssl/hex.h>
  16. #include <gmssl/aead.h>
  17. #include <gmssl/error.h>
  18. #define SM4_MODE_CBC 1
  19. #define SM4_MODE_CTR 2
  20. #define SM4_MODE_GCM 3
  21. #define SM4_MODE_CBC_SM3_HMAC 4
  22. #define SM4_MODE_CTR_SM3_HMAC 5
  23. static const char *usage = "(-cbc|-ctr|-gcm|-cbc_sm3_hmac|-ctr_sm3_hmac) {-encrypt|-decrypt} -key hex -iv hex [-in file] [-out file]";
  24. static const char *options =
  25. "Options\n"
  26. "\n"
  27. " Modes\n"
  28. "\n"
  29. " -cbc CBC mode with padding, need 16-byte key and 16-byte iv\n"
  30. " -ctr CTR mode, need 16-byte key and 16-byte iv\n"
  31. " -gcm GCM mode, need 16-byte key and any iv length\n"
  32. " -cbc_sm3_hmac CBC mode with padding and HMAC-SM3 (encrypt-then-mac), need 48-byte key and 16-byte iv\n"
  33. " -ctr_sm3_hmac CTR mode with HMAC-SM3 (entrypt-then-mac), need 48-byte key and 16-byte iv\n"
  34. "\n"
  35. " -encrypt Encrypt\n"
  36. " -decrypt Decrypt\n"
  37. " -key hex Symmetric key in HEX format\n"
  38. " -iv hex IV in HEX format\n"
  39. " -in file | stdin Input data\n"
  40. " -out file | stdout Output data\n"
  41. "\n"
  42. "Examples"
  43. "\n"
  44. " echo \"hello\" | gmssl sm4 -gcm -encrypt -key 11223344556677881122334455667788 -iv 112233445566778811223344 -out ciphertext.bin\n"
  45. " gmssl sm4 -gcm -decrypt -key 11223344556677881122334455667788 -iv 112233445566778811223344 -in ciphertext.bin\n"
  46. "\n"
  47. " echo \"hello\" | gmssl sm4 -cbc_sm3_hmac -encrypt \\\n"
  48. " -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \\\n"
  49. " -iv 11223344556677881122334455667788 -out ciphertext.bin\n"
  50. " gmssl sm4 -cbc_sm3_hmac -decrypt \\\n"
  51. " -key 112233445566778811223344556677881122334455667788112233445566778811223344556677881122334455667788 \\\n"
  52. " -iv 11223344556677881122334455667788 -in ciphertext.bin\n"
  53. "\n";
  54. int sm4_main(int argc, char **argv)
  55. {
  56. int ret = 1;
  57. char *prog = argv[0];
  58. char *keyhex = NULL;
  59. char *ivhex = NULL;
  60. char *infile = NULL;
  61. char *outfile = NULL;
  62. uint8_t key[48];
  63. uint8_t iv[SM4_GCM_MAX_IV_SIZE];
  64. size_t keylen = sizeof(key);
  65. size_t ivlen = sizeof(iv);
  66. FILE *infp = stdin;
  67. FILE *outfp = stdout;
  68. int mode = 0;
  69. int enc = -1;
  70. int rv;
  71. union {
  72. SM4_CBC_CTX cbc;
  73. SM4_CTR_CTX ctr;
  74. SM4_CBC_SM3_HMAC_CTX cbc_sm3_hmac;
  75. SM4_CTR_SM3_HMAC_CTX ctr_sm3_hmac;
  76. SM4_GCM_CTX gcm;
  77. } sm4_ctx;
  78. uint8_t inbuf[4096];
  79. size_t inlen;
  80. uint8_t outbuf[4196];
  81. size_t outlen;
  82. argc--;
  83. argv++;
  84. if (argc < 1) {
  85. fprintf(stderr, "usage: %s %s\n", prog, usage);
  86. return 1;
  87. }
  88. while (argc > 0) {
  89. if (!strcmp(*argv, "-help")) {
  90. printf("usage: %s %s\n", prog, usage);
  91. printf("%s\n", options);
  92. ret = 0;
  93. goto end;
  94. } else if (!strcmp(*argv, "-key")) {
  95. if (--argc < 1) goto bad;
  96. keyhex = *(++argv);
  97. if (strlen(keyhex) > sizeof(key) * 2) {
  98. fprintf(stderr, "%s: invalid key length\n", prog);
  99. goto end;
  100. }
  101. if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
  102. fprintf(stderr, "%s: invalid key hex digits\n", prog);
  103. goto end;
  104. }
  105. } else if (!strcmp(*argv, "-iv")) {
  106. if (--argc < 1) goto bad;
  107. ivhex = *(++argv);
  108. if (strlen(ivhex) > sizeof(iv) * 2) {
  109. fprintf(stderr, "%s: IV length too long\n", prog);
  110. goto end;
  111. }
  112. if (hex_to_bytes(ivhex, strlen(ivhex), iv, &ivlen) != 1) {
  113. fprintf(stderr, "%s: invalid IV hex digits\n", prog);
  114. goto end;
  115. }
  116. } else if (!strcmp(*argv, "-encrypt")) {
  117. enc = 1;
  118. } else if (!strcmp(*argv, "-decrypt")) {
  119. enc = 0;
  120. } else if (!strcmp(*argv, "-cbc")) {
  121. if (mode) goto bad;
  122. mode = SM4_MODE_CBC;
  123. } else if (!strcmp(*argv, "-ctr")) {
  124. if (mode) goto bad;
  125. mode = SM4_MODE_CTR;
  126. } else if (!strcmp(*argv, "-cbc_sm3_hmac")) {
  127. if (mode) goto bad;
  128. mode = SM4_MODE_CBC_SM3_HMAC;
  129. } else if (!strcmp(*argv, "-ctr_sm3_hmac")) {
  130. if (mode) goto bad;
  131. mode = SM4_MODE_CTR_SM3_HMAC;
  132. } else if (!strcmp(*argv, "-gcm")) {
  133. if (mode) goto bad;
  134. mode = SM4_MODE_GCM;
  135. } else if (!strcmp(*argv, "-in")) {
  136. if (--argc < 1) goto bad;
  137. infile = *(++argv);
  138. if (!(infp = fopen(infile, "rb"))) {
  139. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  140. goto end;
  141. }
  142. } else if (!strcmp(*argv, "-out")) {
  143. if (--argc < 1) goto bad;
  144. outfile = *(++argv);
  145. if (!(outfp = fopen(outfile, "wb"))) {
  146. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  147. goto end;
  148. }
  149. } else {
  150. fprintf(stderr, "%s: illegal option `%s`\n", prog, *argv);
  151. goto end;
  152. bad:
  153. fprintf(stderr, "%s: `%s` option value missing\n", prog, *argv);
  154. goto end;
  155. }
  156. argc--;
  157. argv++;
  158. }
  159. if (!mode) {
  160. fprintf(stderr, "%s: mode not assigned, `-cbc`, `-ctr`, `-gcm`, `-cbc_sm3_hmac` or `-ctr_sm3_hmac` required\n", prog);
  161. goto end;
  162. }
  163. if (!keyhex) {
  164. fprintf(stderr, "%s: option `-key` missing\n", prog);
  165. goto end;
  166. }
  167. if (!ivhex) {
  168. fprintf(stderr, "%s: option `-iv` missing\n", prog);
  169. goto end;
  170. }
  171. switch (mode) {
  172. case SM4_MODE_CTR:
  173. case SM4_MODE_CBC:
  174. case SM4_MODE_GCM:
  175. if (keylen != 16) {
  176. fprintf(stderr, "%s: invalid key length, should be 32 hex digits\n", prog);
  177. goto end;
  178. }
  179. break;
  180. case SM4_MODE_CBC_SM3_HMAC:
  181. case SM4_MODE_CTR_SM3_HMAC:
  182. if (keylen != 48) {
  183. fprintf(stderr, "%s: invalid key length, should be 96 hex digits\n", prog);
  184. goto end;
  185. }
  186. break;
  187. }
  188. switch (mode) {
  189. case SM4_MODE_CTR:
  190. case SM4_MODE_CBC:
  191. case SM4_MODE_CBC_SM3_HMAC:
  192. case SM4_MODE_CTR_SM3_HMAC:
  193. if (ivlen != 16) {
  194. fprintf(stderr, "%s: invalid IV length, should be 32 hex digits\n", prog);
  195. goto end;
  196. }
  197. break;
  198. }
  199. if (mode == SM4_MODE_CTR) {
  200. if (sm4_ctr_encrypt_init(&sm4_ctx.ctr, key, iv) != 1) {
  201. error_print();
  202. goto end;
  203. }
  204. while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
  205. if (sm4_ctr_encrypt_update(&sm4_ctx.ctr, inbuf, inlen, outbuf, &outlen) != 1) {
  206. error_print();
  207. goto end;
  208. }
  209. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  210. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  211. goto end;
  212. }
  213. }
  214. if (sm4_ctr_encrypt_finish(&sm4_ctx.ctr, outbuf, &outlen) != 1) {
  215. error_print();
  216. goto end;
  217. }
  218. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  219. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  220. goto end;
  221. }
  222. ret = 0;
  223. goto end;
  224. }
  225. if (enc < 0) {
  226. fprintf(stderr, "%s: option -encrypt or -decrypt should be set\n", prog);
  227. goto end;
  228. }
  229. if (enc) {
  230. switch (mode) {
  231. case SM4_MODE_CBC: rv = sm4_cbc_encrypt_init(&sm4_ctx.cbc, key, iv); break;
  232. case SM4_MODE_GCM: rv = sm4_gcm_encrypt_init(&sm4_ctx.gcm, key, keylen, iv, ivlen, NULL, 0, GHASH_SIZE); break;
  233. case SM4_MODE_CBC_SM3_HMAC: rv = sm4_cbc_sm3_hmac_encrypt_init(&sm4_ctx.cbc_sm3_hmac, key, keylen, iv, ivlen, NULL, 0); break;
  234. case SM4_MODE_CTR_SM3_HMAC: rv = sm4_ctr_sm3_hmac_encrypt_init(&sm4_ctx.ctr_sm3_hmac, key, keylen, iv, ivlen, NULL, 0); break;
  235. }
  236. if (rv != 1) {
  237. error_print();
  238. goto end;
  239. }
  240. while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
  241. switch (mode) {
  242. case SM4_MODE_CBC: rv = sm4_cbc_encrypt_update(&sm4_ctx.cbc, inbuf, inlen, outbuf, &outlen); break;
  243. case SM4_MODE_GCM: rv = sm4_gcm_encrypt_update(&sm4_ctx.gcm, inbuf, inlen, outbuf, &outlen); break;
  244. case SM4_MODE_CBC_SM3_HMAC: rv = sm4_cbc_sm3_hmac_encrypt_update(&sm4_ctx.cbc_sm3_hmac, inbuf, inlen, outbuf, &outlen); break;
  245. case SM4_MODE_CTR_SM3_HMAC: rv = sm4_ctr_sm3_hmac_encrypt_update(&sm4_ctx.ctr_sm3_hmac, inbuf, inlen, outbuf, &outlen); break;
  246. }
  247. if (rv != 1) {
  248. error_print();
  249. goto end;
  250. }
  251. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  252. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  253. goto end;
  254. }
  255. }
  256. switch (mode) {
  257. case SM4_MODE_CBC: rv = sm4_cbc_encrypt_finish(&sm4_ctx.cbc, outbuf, &outlen); break;
  258. case SM4_MODE_GCM: rv = sm4_gcm_encrypt_finish(&sm4_ctx.gcm, outbuf, &outlen); break;
  259. case SM4_MODE_CBC_SM3_HMAC: rv = sm4_cbc_sm3_hmac_encrypt_finish(&sm4_ctx.cbc_sm3_hmac, outbuf, &outlen); break;
  260. case SM4_MODE_CTR_SM3_HMAC: rv = sm4_ctr_sm3_hmac_encrypt_finish(&sm4_ctx.ctr_sm3_hmac, outbuf, &outlen); break;
  261. }
  262. if (rv != 1) {
  263. error_print();
  264. goto end;
  265. }
  266. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  267. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  268. goto end;
  269. }
  270. } else {
  271. switch (mode) {
  272. case SM4_MODE_CBC: rv = sm4_cbc_decrypt_init(&sm4_ctx.cbc, key, iv); break;
  273. case SM4_MODE_GCM: rv = sm4_gcm_decrypt_init(&sm4_ctx.gcm, key, keylen, iv, ivlen, NULL, 0, GHASH_SIZE); break;
  274. case SM4_MODE_CBC_SM3_HMAC: rv = sm4_cbc_sm3_hmac_decrypt_init(&sm4_ctx.cbc_sm3_hmac, key, keylen, iv, ivlen, NULL, 0); break;
  275. case SM4_MODE_CTR_SM3_HMAC: rv = sm4_ctr_sm3_hmac_decrypt_init(&sm4_ctx.ctr_sm3_hmac, key, keylen, iv, ivlen, NULL, 0); break;
  276. }
  277. if (rv != 1) {
  278. error_print();
  279. goto end;
  280. }
  281. while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
  282. switch (mode) {
  283. case SM4_MODE_CBC: rv = sm4_cbc_decrypt_update(&sm4_ctx.cbc, inbuf, inlen, outbuf, &outlen); break;
  284. case SM4_MODE_GCM: rv = sm4_gcm_decrypt_update(&sm4_ctx.gcm, inbuf, inlen, outbuf, &outlen); break;
  285. case SM4_MODE_CBC_SM3_HMAC: rv = sm4_cbc_sm3_hmac_decrypt_update(&sm4_ctx.cbc_sm3_hmac, inbuf, inlen, outbuf, &outlen); break;
  286. case SM4_MODE_CTR_SM3_HMAC: rv = sm4_ctr_sm3_hmac_decrypt_update(&sm4_ctx.ctr_sm3_hmac, inbuf, inlen, outbuf, &outlen); break;
  287. }
  288. if (rv != 1) {
  289. error_print();
  290. goto end;
  291. }
  292. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  293. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  294. goto end;
  295. }
  296. }
  297. switch (mode) {
  298. case SM4_MODE_CBC: rv = sm4_cbc_decrypt_finish(&sm4_ctx.cbc, outbuf, &outlen); break;
  299. case SM4_MODE_GCM: rv = sm4_gcm_decrypt_finish(&sm4_ctx.gcm, outbuf, &outlen); break;
  300. case SM4_MODE_CBC_SM3_HMAC: rv = sm4_cbc_sm3_hmac_decrypt_finish(&sm4_ctx.cbc_sm3_hmac, outbuf, &outlen); break;
  301. case SM4_MODE_CTR_SM3_HMAC: rv = sm4_ctr_sm3_hmac_decrypt_finish(&sm4_ctx.ctr_sm3_hmac, outbuf, &outlen); break;
  302. }
  303. if (rv != 1) {
  304. error_print();
  305. goto end;
  306. }
  307. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  308. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  309. goto end;
  310. }
  311. }
  312. ret = 0;
  313. end:
  314. gmssl_secure_clear(&sm4_ctx, sizeof(sm4_ctx));
  315. gmssl_secure_clear(key, sizeof(key));
  316. gmssl_secure_clear(iv, sizeof(iv));
  317. gmssl_secure_clear(inbuf, sizeof(inbuf));
  318. gmssl_secure_clear(outbuf, sizeof(outbuf));
  319. if (infile && infp) fclose(infp);
  320. if (outfile && outfp) fclose(outfp);
  321. return ret;
  322. }