zuc.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 <errno.h>
  11. #include <string.h>
  12. #include <stdlib.h>
  13. #include <gmssl/mem.h>
  14. #include <gmssl/zuc.h>
  15. #include <gmssl/hex.h>
  16. static const char *options = "-key hex -iv hex [-in file] [-out file]";
  17. int zuc_main(int argc, char **argv)
  18. {
  19. int ret = 1;
  20. char *prog = argv[0];
  21. char *keyhex = NULL;
  22. char *ivhex = NULL;
  23. char *infile = NULL;
  24. char *outfile = NULL;
  25. uint8_t key[16];
  26. uint8_t iv[16];
  27. size_t keylen = sizeof(key);
  28. size_t ivlen = sizeof(iv);
  29. FILE *infp = stdin;
  30. FILE *outfp = stdout;
  31. ZUC_CTX zuc_ctx;
  32. uint8_t inbuf[4096];
  33. size_t inlen;
  34. uint8_t outbuf[4196];
  35. size_t outlen;
  36. argc--;
  37. argv++;
  38. if (argc < 1) {
  39. fprintf(stderr, "usage: %s %s\n", prog, options);
  40. return 1;
  41. }
  42. while (argc > 0) {
  43. if (!strcmp(*argv, "-help")) {
  44. printf("usage: %s %s\n", prog, options);
  45. ret = 0;
  46. goto end;
  47. } else if (!strcmp(*argv, "-key")) {
  48. if (--argc < 1) goto bad;
  49. keyhex = *(++argv);
  50. if (strlen(keyhex) != sizeof(key) * 2) {
  51. fprintf(stderr, "%s: invalid key length\n", prog);
  52. goto end;
  53. }
  54. if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
  55. fprintf(stderr, "%s: invalid HEX digits\n", prog);
  56. goto end;
  57. }
  58. } else if (!strcmp(*argv, "-iv")) {
  59. if (--argc < 1) goto bad;
  60. ivhex = *(++argv);
  61. if (strlen(ivhex) != sizeof(iv) * 2) {
  62. fprintf(stderr, "%s: invalid IV length\n", prog);
  63. goto end;
  64. }
  65. if (hex_to_bytes(ivhex, strlen(ivhex), iv, &ivlen) != 1) {
  66. fprintf(stderr, "%s: invalid HEX digits\n", prog);
  67. goto end;
  68. }
  69. } else if (!strcmp(*argv, "-in")) {
  70. if (--argc < 1) goto bad;
  71. infile = *(++argv);
  72. if (!(infp = fopen(infile, "rb"))) {
  73. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  74. goto end;
  75. }
  76. } else if (!strcmp(*argv, "-out")) {
  77. if (--argc < 1) goto bad;
  78. outfile = *(++argv);
  79. if (!(outfp = fopen(outfile, "wb"))) {
  80. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  81. goto end;
  82. }
  83. } else {
  84. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  85. goto end;
  86. bad:
  87. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  88. goto end;
  89. }
  90. argc--;
  91. argv++;
  92. }
  93. if (!keyhex) {
  94. fprintf(stderr, "%s: option '-key' missing\n", prog);
  95. goto end;
  96. }
  97. if (!ivhex) {
  98. fprintf(stderr, "%s: option '-iv' missing\n", prog);
  99. goto end;
  100. }
  101. if (zuc_encrypt_init(&zuc_ctx, key, iv) != 1) {
  102. fprintf(stderr, "%s: inner error\n", prog);
  103. goto end;
  104. }
  105. while ((inlen = fread(inbuf, 1, sizeof(inbuf), infp)) > 0) {
  106. if (zuc_encrypt_update(&zuc_ctx, inbuf, inlen, outbuf, &outlen) != 1) {
  107. fprintf(stderr, "%s: inner error\n", prog);
  108. goto end;
  109. }
  110. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  111. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  112. goto end;
  113. }
  114. }
  115. if (zuc_encrypt_finish(&zuc_ctx, outbuf, &outlen) != 1) {
  116. fprintf(stderr, "%s: inner error\n", prog);
  117. goto end;
  118. }
  119. if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
  120. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  121. goto end;
  122. }
  123. ret = 0;
  124. end:
  125. gmssl_secure_clear(&zuc_ctx, sizeof(zuc_ctx));
  126. gmssl_secure_clear(key, sizeof(key));
  127. gmssl_secure_clear(iv, sizeof(iv));
  128. gmssl_secure_clear(inbuf, sizeof(inbuf));
  129. gmssl_secure_clear(outbuf, sizeof(outbuf));
  130. if (infile && infp) fclose(infp);
  131. if (outfile && outfp) fclose(outfp);
  132. return ret;
  133. }