sdfutil.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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/sm2.h>
  15. #include <gmssl/sm3.h>
  16. #include <gmssl/sdf.h>
  17. #define OP_NONE 0
  18. #define OP_DEVINFO 1
  19. #define OP_EXPORTPUBKEY 2
  20. #define OP_SIGN 3
  21. #define OP_RAND 4
  22. static void print_usage(FILE *fp, const char *prog)
  23. {
  24. fprintf(fp, "usage:\n");
  25. fprintf(fp, " %s -lib so_path -devinfo\n", prog);
  26. fprintf(fp, " %s -lib so_path -exportpubkey -key index [-out file]\n", prog);
  27. fprintf(fp, " %s -lib so_path -sign [-in file] [-out file]\n", prog);
  28. fprintf(fp, " %s -lib so_path -rand num [-out file]\n", prog);
  29. }
  30. int sdfutil_main(int argc, char **argv)
  31. {
  32. int ret = 1;
  33. char *prog = argv[0];
  34. char *lib = NULL;
  35. int op = 0;
  36. int keyindex = -1;
  37. char *pass = NULL;
  38. char *id = SM2_DEFAULT_ID;
  39. int num = 0;
  40. char *infile = NULL;
  41. char *outfile = NULL;
  42. FILE *infp = stdin;
  43. FILE *outfp = stdout;
  44. unsigned char buf[4096];
  45. size_t len;
  46. SDF_DEVICE dev;
  47. SDF_KEY key;
  48. int dev_opened = 0;
  49. int key_opened = 0;
  50. memset(&dev, 0, sizeof(dev));
  51. memset(&key, 0, sizeof(key));
  52. argc--;
  53. argv++;
  54. if (argc < 1) {
  55. print_usage(stderr, prog);
  56. return 1;
  57. }
  58. while (argc > 0) {
  59. if (!strcmp(*argv, "-help")) {
  60. print_usage(stdout, prog);
  61. goto end;
  62. } else if (!strcmp(*argv, "-lib")) {
  63. if (--argc < 1) goto bad;
  64. lib = *(++argv);
  65. } else if (!strcmp(*argv, "-devinfo")) {
  66. op = OP_DEVINFO;
  67. } else if (!strcmp(*argv, "-exportpubkey")) {
  68. op = OP_EXPORTPUBKEY;
  69. } else if (!strcmp(*argv, "-sign")) {
  70. op = OP_SIGN;
  71. } else if (!strcmp(*argv, "-key")) {
  72. if (--argc < 1) goto bad;
  73. keyindex = atoi(*(++argv));
  74. } else if (!strcmp(*argv, "-pass")) {
  75. if (--argc < 1) goto bad;
  76. pass = *(++argv);
  77. } else if (!strcmp(*argv, "-id")) {
  78. if (--argc < 1) goto bad;
  79. id = *(++argv);
  80. } else if (!strcmp(*argv, "-rand")) {
  81. if (--argc < 1) goto bad;
  82. len = atoi(*(++argv));
  83. } else if (!strcmp(*argv, "-in")) {
  84. if (--argc < 1) goto bad;
  85. infile = *(++argv);
  86. if (!(infp = fopen(infile, "rb"))) {
  87. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
  88. goto end;
  89. }
  90. } else if (!strcmp(*argv, "-out")) {
  91. if (--argc < 1) goto bad;
  92. outfile = *(++argv);
  93. if (!(outfp = fopen(outfile, "wb"))) {
  94. fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
  95. goto end;
  96. }
  97. } else {
  98. fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
  99. goto end;
  100. bad:
  101. fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
  102. goto end;
  103. }
  104. argc--;
  105. argv++;
  106. }
  107. if (!lib) {
  108. fprintf(stderr, "%s: option '-lib' required\n", prog);
  109. goto end;
  110. }
  111. if (sdf_load_library(lib, NULL) != 1) {
  112. fprintf(stderr, "%s: load library failure\n", prog);
  113. goto end;
  114. }
  115. if (sdf_open_device(&dev) != 1) {
  116. fprintf(stderr, "%s: open device failure\n", prog);
  117. goto end;
  118. }
  119. dev_opened = 1;
  120. switch (op) {
  121. case OP_DEVINFO:
  122. sdf_print_device_info(stdout, 0, 0, "SDF", &dev);
  123. break;
  124. case OP_EXPORTPUBKEY:
  125. if (keyindex < 0) {
  126. fprintf(stderr, "%s: invalid key index\n", prog);
  127. goto end;
  128. }
  129. if (sdf_load_sign_key(&dev, &key, keyindex, pass) != 1) {
  130. fprintf(stderr, "%s: load sign key failed\n", prog);
  131. goto end;
  132. }
  133. key_opened = 1;
  134. if (sm2_public_key_info_to_pem(&(key.public_key), outfp) != 1) {
  135. fprintf(stderr, "%s: output public key to PEM failed\n", prog);
  136. goto end;
  137. }
  138. break;
  139. case OP_SIGN:
  140. {
  141. SM3_CTX sm3_ctx;
  142. uint8_t dgst[32];
  143. uint8_t sig[SM2_MAX_SIGNATURE_SIZE];
  144. size_t siglen;
  145. if (sdf_load_sign_key(&dev, &key, keyindex, pass) != 1) {
  146. fprintf(stderr, "%s: load sign key failed\n", prog);
  147. goto end;
  148. }
  149. key_opened = 1;
  150. sm3_init(&sm3_ctx);
  151. sm2_compute_z(dgst, &(key.public_key.public_key), id, strlen(id));
  152. sm3_update(&sm3_ctx, dgst, sizeof(dgst));
  153. while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
  154. sm3_update(&sm3_ctx, buf, len);
  155. }
  156. sm3_finish(&sm3_ctx, dgst);
  157. if ((ret = sdf_sign(&key, dgst, sig, &siglen)) != 1) {
  158. fprintf(stderr, "%s: inner error\n", prog);
  159. goto end;
  160. }
  161. if (fwrite(sig, 1, siglen, outfp) != siglen) {
  162. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  163. goto end;
  164. }
  165. }
  166. break;
  167. case OP_RAND:
  168. if (sdf_rand_bytes(&dev, buf, len) != 1) {
  169. fprintf(stderr, "%s: inner error\n", prog);
  170. goto end;
  171. }
  172. if (fwrite(buf, 1, len, outfp) != len) {
  173. fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
  174. goto end;
  175. }
  176. break;
  177. default:
  178. fprintf(stderr, "%s: this should not happen\n", prog);
  179. goto end;
  180. }
  181. ret = 0;
  182. end:
  183. gmssl_secure_clear(buf, sizeof(buf));
  184. if (key_opened) sdf_release_key(&key);
  185. if (dev_opened) sdf_close_device(&dev);
  186. if (lib) sdf_unload_library();
  187. if (infile && infp) fclose(infp);
  188. if (outfile && outfp) fclose(outfp);
  189. return ret;
  190. }