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