sm3.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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/sm2.h>
  14. #include <gmssl/sm3.h>
  15. #include <gmssl/error.h>
  16. static const char *options = "[-hex|-bin] [-pubkey pem [-id str]] [-in file] [-out file]";
  17. int sm3_main(int argc, char **argv)
  18. {
  19. int ret = 1;
  20. char *prog = argv[0];
  21. int bin = 0;
  22. char *pubkeyfile = NULL;
  23. char *infile = NULL;
  24. char *outfile = NULL;
  25. char *id = NULL;
  26. FILE *pubkeyfp = NULL;
  27. FILE *infp = stdin;
  28. FILE *outfp = stdout;
  29. SM3_CTX sm3_ctx;
  30. uint8_t dgst[32];
  31. uint8_t buf[4096];
  32. size_t len;
  33. int i;
  34. argc--;
  35. argv++;
  36. while (argc > 0) {
  37. if (!strcmp(*argv, "-help")) {
  38. printf("usage: %s %s\n", prog, options);
  39. printf("usage: echo -n \"abc\" | %s\n", prog);
  40. ret = 0;
  41. goto end;
  42. } else if (!strcmp(*argv, "-hex")) {
  43. if (bin) {
  44. error_print();
  45. goto end;
  46. }
  47. bin = 0;
  48. } else if (!strcmp(*argv, "-bin")) {
  49. bin = 1;
  50. } else if (!strcmp(*argv, "-pubkey")) {
  51. if (--argc < 1) goto bad;
  52. pubkeyfile = *(++argv);
  53. if (!(pubkeyfp = fopen(pubkeyfile, "rb"))) {
  54. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, pubkeyfile, strerror(errno));
  55. goto end;
  56. }
  57. } else if (!strcmp(*argv, "-id")) {
  58. if (--argc < 1) goto bad;
  59. id = *(++argv);
  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. sm3_init(&sm3_ctx);
  85. if (pubkeyfile) {
  86. SM2_KEY sm2_key;
  87. uint8_t z[32];
  88. if (sm2_public_key_info_from_pem(&sm2_key, pubkeyfp) != 1) {
  89. fprintf(stderr, "%s: parse public key failed\n", prog);
  90. goto end;
  91. }
  92. if (!id) {
  93. id = SM2_DEFAULT_ID;
  94. }
  95. sm2_compute_z(z, (SM2_POINT *)&sm2_key, id, strlen(id));
  96. sm3_update(&sm3_ctx, z, sizeof(z));
  97. } else {
  98. if (id) {
  99. fprintf(stderr, "%s: option '-id' must be with '-pubkey'\n", prog);
  100. goto end;
  101. }
  102. }
  103. while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
  104. sm3_update(&sm3_ctx, buf, len);
  105. }
  106. sm3_finish(&sm3_ctx, dgst);
  107. if (bin) {
  108. if (fwrite(dgst, 1, sizeof(dgst), outfp) != sizeof(dgst)) {
  109. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  110. goto end;
  111. }
  112. } else {
  113. for (i = 0; i < sizeof(dgst); i++) {
  114. fprintf(outfp, "%02x", dgst[i]);
  115. }
  116. fprintf(outfp, "\n");
  117. }
  118. ret = 0;
  119. end:
  120. if (pubkeyfp) fclose(pubkeyfp);
  121. if (infile && infp) fclose(infp);
  122. if (outfile && outfp) fclose(outfp);
  123. return ret;
  124. }