sm3hmac.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 <stdlib.h>
  12. #include <string.h>
  13. #include <gmssl/mem.h>
  14. #include <gmssl/hex.h>
  15. #include <gmssl/sm3.h>
  16. static const char *options = "-key hex [-in file] [-bin|-hex] [-out file]";
  17. int sm3hmac_main(int argc, char **argv)
  18. {
  19. int ret = 1;
  20. char *prog = argv[0];
  21. char *keyhex = NULL;
  22. int bin = 0;
  23. char *infile = NULL;
  24. char *outfile = NULL;
  25. uint8_t key[SM3_DIGEST_SIZE];
  26. size_t keylen;
  27. FILE *infp = stdin;
  28. FILE *outfp = stdout;
  29. uint8_t buf[4096];
  30. size_t len;
  31. SM3_HMAC_CTX ctx;
  32. uint8_t mac[SM3_HMAC_SIZE];
  33. size_t i;
  34. argc--;
  35. argv++;
  36. if (argc < 1) {
  37. fprintf(stderr, "usage: %s %s\n", prog, options);
  38. return 1;
  39. }
  40. while (argc > 0) {
  41. if (!strcmp(*argv, "-help")) {
  42. printf("usage: %s %s\n", prog, options);
  43. ret = 0;
  44. goto end;
  45. } else if (!strcmp(*argv, "-key")) {
  46. if (--argc < 1) goto bad;
  47. keyhex = *(++argv);
  48. if (strlen(keyhex) > sizeof(key) * 2) {
  49. fprintf(stderr, "%s: key should be less than 64 digits (32 bytes)\n", prog);
  50. goto end;
  51. }
  52. if (hex_to_bytes(keyhex, strlen(keyhex), key, &keylen) != 1) {
  53. fprintf(stderr, "%s: invalid HEX digits\n", prog);
  54. goto end;
  55. }
  56. } else if (!strcmp(*argv, "-hex")) {
  57. bin = 0;
  58. } else if (!strcmp(*argv, "-bin")) {
  59. bin = 1;
  60. } else if (!strcmp(*argv, "-in")) {
  61. if (--argc < 1) goto bad;
  62. infile = *(++argv);
  63. if (!(infp = fopen(infile, "rb"))) {
  64. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  65. goto end;
  66. }
  67. } else if (!strcmp(*argv, "-out")) {
  68. if (--argc < 1) goto bad;
  69. outfile = *(++argv);
  70. if (!(outfp = fopen(outfile, "wb"))) {
  71. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  72. goto end;
  73. }
  74. } else {
  75. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  76. goto end;
  77. bad:
  78. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  79. goto end;
  80. }
  81. argc--;
  82. argv++;
  83. }
  84. if (!keyhex) {
  85. fprintf(stderr, "%s: option '-key' required\n", prog);
  86. goto end;
  87. }
  88. sm3_hmac_init(&ctx, key, keylen);
  89. while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
  90. sm3_hmac_update(&ctx, buf, len);
  91. }
  92. sm3_hmac_finish(&ctx, mac);
  93. if (bin) {
  94. if (fwrite(mac, 1, sizeof(mac), outfp) != sizeof(mac)) {
  95. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  96. goto end;
  97. }
  98. } else {
  99. for (i = 0; i < sizeof(mac); i++) {
  100. fprintf(outfp, "%02x", mac[i]);
  101. }
  102. fprintf(outfp, "\n");
  103. }
  104. ret = 0;
  105. end:
  106. gmssl_secure_clear(key, sizeof(key));
  107. gmssl_secure_clear(&ctx, sizeof(ctx));
  108. if (infile && infp) fclose(infp);
  109. if (outfile && outfp) fclose(outfp);
  110. return ret;
  111. }