• 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 <stdlib.h>
14 #include <string.h>
15 #include <gmssl/mem.h>
16 #include <gmssl/hex.h>
17 #include <gmssl/pbkdf2.h>
18 
19 
20 static const char *options = "-pass str -salt hex -iter num -outlen num [-bin|-hex] [-out file]";
21 
pbkdf2_main(int argc,char ** argv)22 int pbkdf2_main(int argc, char **argv)
23 {
24 	int ret = 1;
25 	char *prog = argv[0];
26 	char *pass = NULL;
27 	char *salthex = NULL;
28 	uint8_t salt[PBKDF2_MAX_SALT_SIZE];
29 	size_t saltlen;
30 	int iter = 0;
31 	int outlen = 0;
32 	int bin = 0;
33 	char *outfile = NULL;
34 	uint8_t outbuf[64];
35 	FILE *outfp = stdout;
36 	int i;
37 
38 	argc--;
39 	argv++;
40 
41 	if (argc < 1) {
42 		fprintf(stderr, "usage: %s %s\n", prog, options);
43 		return 1;
44 	}
45 
46 	while (argc > 0) {
47 		if (!strcmp(*argv, "-help")) {
48 			printf("usage: %s %s\n", prog, options);
49 			ret = 0;
50 			goto end;
51 		} else if (!strcmp(*argv, "-pass")) {
52 			if (--argc < 1) goto bad;
53 			pass = *(++argv);
54 		} else if (!strcmp(*argv, "-salt")) {
55 			if (--argc < 1) goto bad;
56 			salthex = *(++argv);
57 			if (strlen(salthex) > sizeof(salt) * 2) {
58 				fprintf(stderr, "%s: invalid salt length\n", prog);
59 				goto end;
60 			}
61 			if (hex_to_bytes(salthex, strlen(salthex), salt, &saltlen) != 1) {
62 				fprintf(stderr, "%s: invalid HEX digits\n", prog);
63 				goto end;
64 			}
65 		} else if (!strcmp(*argv, "-iter")) {
66 			if (--argc < 1) goto bad;
67 			iter = atoi(*(++argv));
68 			if (iter < PBKDF2_MIN_ITER || iter > INT_MAX) {
69 				fprintf(stderr, "%s: invalid '-iter' value\n", prog);
70 				goto end;
71 			}
72 		} else if (!strcmp(*argv, "-outlen")) {
73 			if (--argc < 1) goto bad;
74 			outlen = atoi(*(++argv));
75 			if (outlen < 1 || outlen > sizeof(outbuf)) {
76 				fprintf(stderr, "%s: invalid outlen\n", prog);
77 				goto end;
78 			}
79 		} else if (!strcmp(*argv, "-hex")) {
80 			bin = 0;
81 		} else if (!strcmp(*argv, "-bin")) {
82 			bin = 1;
83 		} else if (!strcmp(*argv, "-out")) {
84 			if (--argc < 1) goto bad;
85 			outfile = *(++argv);
86 			if (!(outfp = fopen(outfile, "w"))) {
87 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
88 				goto end;
89 			}
90 		} else {
91 			fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
92 			goto end;
93 bad:
94 			fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
95 			goto end;
96 		}
97 
98 		argc--;
99 		argv++;
100 	}
101 
102 	if (!pass) {
103 		fprintf(stderr, "%s: option '-pass' required\n", prog);
104 		goto end;
105 	}
106 	if (!salthex) {
107 		fprintf(stderr, "%s: option '-salt' required\n", prog);
108 		goto end;
109 	}
110 	if (!iter) {
111 		fprintf(stderr, "%s: option '-iter' required\n", prog);
112 		goto end;
113 	}
114 	if (!outlen) {
115 		fprintf(stderr, "%s: option '-outlen' required\n", prog);
116 		goto end;
117 	}
118 
119 	if (pbkdf2_hmac_sm3_genkey(pass, strlen(pass), salt, saltlen, iter, outlen, outbuf) != 1) {
120 		fprintf(stderr, "%s: inner error\n", prog);
121 		goto end;
122 	}
123 
124 	if (bin) {
125 		if (fwrite(outbuf, 1, outlen, outfp) != outlen) {
126 			fprintf(stderr, "%s: output failure : %s\n", prog, strerror(errno));
127 			goto end;
128 		}
129 	} else {
130 		for (i = 0; i < outlen; i++) {
131 			fprintf(outfp, "%02x", outbuf[i]);
132 		}
133 		fprintf(outfp, "\n");
134 	}
135 	ret = 0;
136 
137 end:
138 	gmssl_secure_clear(outbuf, sizeof(outbuf));
139 	gmssl_secure_clear(salt, sizeof(salt));
140 	if (outfile && outfp) fclose(outfp);
141 	return ret;
142 }
143