• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string.h>
14 #include <stdlib.h>
15 #include <gmssl/sm2.h>
16 #include <gmssl/sm3.h>
17 #include <gmssl/error.h>
18 
19 
20 static const char *options = "[-hex|-bin] [-pubkey pem [-id str]] [-in file] [-out file]";
21 
sm3_main(int argc,char ** argv)22 int sm3_main(int argc, char **argv)
23 {
24 	int ret = 1;
25 	char *prog = argv[0];
26 	int bin = 0;
27 	char *pubkeyfile = NULL;
28 	char *infile = NULL;
29 	char *outfile = NULL;
30 	char *id = NULL;
31 	FILE *pubkeyfp = NULL;
32 	FILE *infp = stdin;
33 	FILE *outfp = stdout;
34 	SM3_CTX sm3_ctx;
35 	uint8_t dgst[32];
36 	uint8_t buf[4096];
37 	ssize_t len;
38 	int i;
39 
40 	argc--;
41 	argv++;
42 
43 	while (argc > 0) {
44 		if (!strcmp(*argv, "-help")) {
45 			printf("usage: %s %s\n", prog, options);
46 			printf("usage: echo -n \"abc\" | %s\n", prog);
47 			ret = 0;
48 			goto end;
49 		} else if (!strcmp(*argv, "-hex")) {
50 			if (bin) {
51 				error_print();
52 				goto end;
53 			}
54 			bin = 0;
55 		} else if (!strcmp(*argv, "-bin")) {
56 			bin = 1;
57 		} else if (!strcmp(*argv, "-pubkey")) {
58 			if (--argc < 1) goto bad;
59 			pubkeyfile = *(++argv);
60 			if (!(pubkeyfp = fopen(pubkeyfile, "r"))) {
61 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, pubkeyfile, strerror(errno));
62 				goto end;
63 			}
64 		} else if (!strcmp(*argv, "-id")) {
65 			if (--argc < 1) goto bad;
66 			id = *(++argv);
67 		} else if (!strcmp(*argv, "-in")) {
68 			if (--argc < 1) goto bad;
69 			infile = *(++argv);
70 			if (!(infp = fopen(infile, "r"))) {
71 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
72 				goto end;
73 			}
74 		} else if (!strcmp(*argv, "-out")) {
75 			if (--argc < 1) goto bad;
76 			outfile = *(++argv);
77 			if (!(outfp = fopen(outfile, "r"))) {
78 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
79 				goto end;
80 			}
81 		} else {
82 			fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
83 			goto end;
84 bad:
85 			fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
86 			goto end;
87 		}
88 
89 		argc--;
90 		argv++;
91 	}
92 
93 	sm3_init(&sm3_ctx);
94 
95 	if (pubkeyfile) {
96 		SM2_KEY sm2_key;
97 		uint8_t z[32];
98 
99 		if (sm2_public_key_info_from_pem(&sm2_key, pubkeyfp) != 1) {
100 			fprintf(stderr, "%s: parse public key failed\n", prog);
101 			goto end;
102 		}
103 		if (!id) {
104 			id = SM2_DEFAULT_ID;
105 		}
106 
107 		sm2_compute_z(z, (SM2_POINT *)&sm2_key, id, strlen(id));
108 		sm3_update(&sm3_ctx, z, sizeof(z));
109 	} else {
110 		if (id) {
111 			fprintf(stderr, "%s: option '-id' must be with '-pubkey'\n", prog);
112 			goto end;
113 		}
114 	}
115 
116 	while ((len = fread(buf, 1, sizeof(buf), infp)) > 0) {
117 		sm3_update(&sm3_ctx, buf, len);
118 	}
119 	sm3_finish(&sm3_ctx, dgst);
120 
121 	if (bin) {
122 		if (fwrite(dgst, 1, sizeof(dgst), outfp) != sizeof(dgst)) {
123 			fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
124 			goto end;
125 		}
126 	} else {
127 		for (i = 0; i < sizeof(dgst); i++) {
128 			fprintf(outfp, "%02x", dgst[i]);
129 		}
130 		fprintf(outfp, "\n");
131 	}
132 	ret = 0;
133 end:
134 	if (pubkeyfp) fclose(pubkeyfp);
135 	if (infile && infp) fclose(infp);
136 	if (outfile && outfp) fclose(outfp);
137 	return ret;
138 }
139