aboutsummaryrefslogtreecommitdiffstats
path: root/cmdline_sha3sum.c
blob: 7dc0757cace22c81ea5210425793a8750bc7e72b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifdef SUPPORT_SHA3
#include <libkeccak.h>
#endif


static void
intopt(long int *valp, char flag, const char *arg)
{
	if (*valp)
		usage();
	if (!isdigit(*arg))
		goto invalid;
	for (; isdigit(*arg); arg++) {
		if (*valp > (LONG_MAX - (*arg & 15)) / 10)
			goto invalid;
		*valp = *valp * 10 + (*arg & 15);
	}
	if (*arg || !*valp) {
	invalid:
		eprintf("invalid value for -%c option, expected integer within [1, %li]", flag, LONG_MAX);
	}
}


static char *
fmtalgostr(const char *f, long int r, long int c, long int n, long int z)
{
	char *ret, *p;
	int len;
	len = snprintf(NULL, 0, "%s[r=%li,c=%li,n=%li,z=%li]", f, r, c, n, z);
	if (len <= 0)
		abort();
	ret = emalloc((size_t)len + 1U);
	p = stpcpy(ret, f);
	*p++ = '[';
	if (r)
		p += sprintf(p, "r=%li%s", r, (c | n | z) ? "," : "");
	if (c)
		p += sprintf(p, "c=%li%s", c, (n | z) ? "," : "");
	if (n)
		p += sprintf(p, "n=%li%s", n, z ? "," : "");
	if (z)
		p += sprintf(p, "z=%li", z);
	*p++ = ']';
	*p = '\0';
	return ret;
}


int
cmdline_sha3sum(int argc, char **argv, enum command command, struct config *config,
                const char **algostr, enum libhashsum_algorithm *algorithm, char **algostrbuf)
{
	long int r = 0, c = 0, n = 0, s = 0, w = 0, z = 0, bits;
	char *arg, *p;
	int verbose = 0;
#ifdef SUPPORT_SHA3
	struct libkeccak_generalised_spec gspec;
	struct libkeccak_spec spec;
	const char *suffix;
#endif

	*algostrbuf = NULL;

	ARGBEGIN {
	case 'c':
		config->verify = 1;
		break;

	case 'w':
		config->warn_improper_format = 1;
		break;

	case 'z':
		config->format &= (enum format)~WITH_LF;
		config->format |= WITH_NUL;
		break;

	case 'a':
		if (command != SHA3SUM)
			usage();
		arg = ARG();
		if      (!strcmp(arg, "224")) *algostr = "sha3-224", *algorithm = LIBHASHSUM_SHA3_224;
		else if (!strcmp(arg, "256")) *algostr = "sha3-256", *algorithm = LIBHASHSUM_SHA3_256;
		else if (!strcmp(arg, "384")) *algostr = "sha3-384", *algorithm = LIBHASHSUM_SHA3_384;
		else if (!strcmp(arg, "512")) *algostr = "sha3-512", *algorithm = LIBHASHSUM_SHA3_512;
		else
			eprintf("unsupport value on -a option, expected 224, 256, 384, or 512");
		break;

	case 'b': /* (compat) */
		config->format &= (enum format)~FORMAT_MASK;
		config->format |= BINARY;
		break;

	case 'l': /* (compat) */
		config->format &= (enum format)~FORMAT_MASK;
		config->format |= LOWERCASE_HEX;
		break;

	case 'u': /* (compat) */
		config->format &= (enum format)~FORMAT_MASK;
		config->format |= UPPERCASE_HEX;
		break;

	case 'x': /* (compat) */
		config->hexinput = 1;
		break;

	case 'v': /* (compat) */
		verbose = 1;
		break;

	case 'R':
		intopt(&r, FLAG(), ARG());
		break;

	case 'C':
		intopt(&c, FLAG(), ARG());
		break;

	case 'N':
	case 'O': /* (compat) */
		intopt(&n, FLAG(), ARG());
		break;

	case 'S':
	case 'B': /* (compat) */
		intopt(&s, FLAG(), ARG());
		break;

	case 'Z':
		intopt(&z, FLAG(), ARG());
		break;

	case 'W':
		p = arg = ARG();
		while (isdigit(*p))
			p++;
		if (*arg && !*p) {
			intopt(&w, FLAG(), arg);
		} else {
			arg = parseopts(config, arg, &parseopt_vendor);
			if (*arg)
				eprintf("unsupported -W options: %s", arg);
		}
		break;

	default:
		usage();
	} ARGEND;

#ifdef SUPPORT_SHA3
	libkeccak_generalised_spec_initialise(&gspec);
#endif

	switch (*algorithm) {
	case LIBHASHSUM_KECCAK:
#ifdef SUPPORT_SHA3
		suffix = "";
#endif
		break;
	case LIBHASHSUM_KECCAK_224: bits = 224; goto spec_keccak;
	case LIBHASHSUM_KECCAK_256: bits = 256; goto spec_keccak;
	case LIBHASHSUM_KECCAK_384: bits = 384; goto spec_keccak;
	case LIBHASHSUM_KECCAK_512: bits = 512; spec_keccak:
		if (r | c | n | s | w | z)
			usage();
#ifdef SUPPORT_SHA3
		libkeccak_spec_sha3((struct libkeccak_spec *)&gspec, bits);
		suffix = "";
#endif
		break;
	case LIBHASHSUM_SHA3_224: bits = 224; goto spec_sha3;
	case LIBHASHSUM_SHA3_256: bits = 256; goto spec_sha3;
	case LIBHASHSUM_SHA3_384: bits = 384; goto spec_sha3;
	case LIBHASHSUM_SHA3_512: bits = 512; spec_sha3:
		if (r | c | n | s | w | z)
			usage();
#ifdef SUPPORT_SHA3
		libkeccak_spec_sha3((struct libkeccak_spec *)&gspec, bits);
		suffix = LIBKECCAK_SHA3_SUFFIX;
#endif
		break;
	case LIBHASHSUM_SHAKE128: bits = 128; goto spec_shake;
	case LIBHASHSUM_SHAKE256: bits = 256; goto spec_shake;
	case LIBHASHSUM_SHAKE512: bits = 512; spec_shake:
		if (r | c | s | w | z)
			usage();
#ifdef SUPPORT_SHA3
		libkeccak_spec_shake((struct libkeccak_spec *)&gspec, bits, bits);
		suffix = LIBKECCAK_SHAKE_SUFFIX;
#endif
		break;
	case LIBHASHSUM_RAWSHAKE128: bits = 128; goto spec_rawshake;
	case LIBHASHSUM_RAWSHAKE256: bits = 256; goto spec_rawshake;
	case LIBHASHSUM_RAWSHAKE512: bits = 512; spec_rawshake:
		if (r | c | s | w | z)
			usage();
#ifdef SUPPORT_SHA3
		libkeccak_spec_rawshake((struct libkeccak_spec *)&gspec, bits, bits);
		suffix = LIBKECCAK_RAWSHAKE_SUFFIX;
#endif
		break;
	default:
	case LIBHASHSUM_MD2:
	case LIBHASHSUM_MD4:
	case LIBHASHSUM_MD5:
	case LIBHASHSUM_RIPEMD_128:
	case LIBHASHSUM_RIPEMD_160:
	case LIBHASHSUM_RIPEMD_256:
	case LIBHASHSUM_RIPEMD_320:
	case LIBHASHSUM_SHA0:
	case LIBHASHSUM_SHA1:
	case LIBHASHSUM_SHA_224:
	case LIBHASHSUM_SHA_256:
	case LIBHASHSUM_SHA_384:
	case LIBHASHSUM_SHA_512:
	case LIBHASHSUM_SHA_512_224:
	case LIBHASHSUM_SHA_512_256:
	case LIBHASHSUM_BLAKE224:
	case LIBHASHSUM_BLAKE256:
	case LIBHASHSUM_BLAKE384:
	case LIBHASHSUM_BLAKE512:
		abort();
	}

	z = z ? z : 1U;

#ifdef SUPPORT_SHA3
	gspec.bitrate    = r ? r : gspec.bitrate;
	gspec.capacity   = c ? c : gspec.capacity;
	gspec.output     = n ? n : gspec.output;
	gspec.state_size = s ? s : gspec.state_size;
	gspec.word_size  = w ? w : gspec.word_size;

# define CASE(N, S) case LIBKECCAK_GENERALISED_SPEC_ERROR_##N: eprintf("%s", S)
	switch (libkeccak_degeneralise_spec(&gspec, &spec)) {
	case 0:
		break;
	CASE(STATE_NONPOSITIVE,                    "the state size must be positive");
	CASE(STATE_TOO_LARGE,                      "the state size is too large, may not exceed 1600");
	CASE(STATE_MOD_25,                         "the state size must be a multiple of 25");
	CASE(WORD_NONPOSITIVE,                     "the word size must be positive");
	CASE(WORD_TOO_LARGE,                       "the word size is too large, may not exceed 64");
	CASE(STATE_WORD_INCOHERENCY,               "the state size must be exactly 25 times the word size");
	CASE(CAPACITY_NONPOSITIVE,                 "the capacity must be positive");
	CASE(CAPACITY_MOD_8,                       "the capacity must be a multiple of 8");
	CASE(BITRATE_NONPOSITIVE,                  "the rate must be positive");
	CASE(BITRATE_MOD_8,                        "the rate must be a multiple of 8");
	CASE(OUTPUT_NONPOSITIVE,                   "the output size must be positive");
	CASE(STATE_BITRATE_CAPACITY_INCONSISTENCY, "the sum of the rate and capacity must equal"
	                                           " the state size (25 times the word size)");
	default:
		eprintf("unknown error in algorithm parameters");
	}
# undef CASE

# define CASE(N, S) case LIBKECCAK_SPEC_ERROR_##N: eprintf("%s", S)
	switch (libkeccak_spec_check(&spec)) {
	case 0:
		break;
	CASE(BITRATE_NONPOSITIVE,  "the rate size must be positive");
	CASE(BITRATE_MOD_8,        "the rate must be a multiple of 8");
	CASE(CAPACITY_NONPOSITIVE, "the capacity must be positive");
	CASE(CAPACITY_MOD_8,       "the capacity must be a multiple of 8");
	CASE(OUTPUT_NONPOSITIVE,   "the output size must be positive");
	CASE(STATE_TOO_LARGE,      "the state size is too large, may not exceed 1600");
	CASE(STATE_MOD_25,         "the state size must be a multiple of 25");
	CASE(WORD_NON_2_POTENT,    "the word size must be a power of 2");
	CASE(WORD_MOD_8,           "the word size must be a multiple of 8");
	default:
		eprintf("unknown error in algorithm parameters");
	}
# undef TEST

	if (verbose) {
		fprintf(stderr,        "rate: %li\n", gspec.bitrate);
		fprintf(stderr,    "capacity: %li\n", gspec.capacity);
		fprintf(stderr, "output size: %li\n", gspec.output);
		fprintf(stderr,  "state size: %li\n", gspec.state_size);
		fprintf(stderr,   "word size: %li\n", gspec.word_size);
		fprintf(stderr,    "squeezes: %li\n", z);
		fprintf(stderr,      "suffix: %s\n",  suffix);
	}
#else
	(void) verbose;
#endif

	if (*algorithm == LIBHASHSUM_KECCAK && !s && !w)
		*algostr = *algostrbuf = fmtalgostr(*algostr, r, c, n, z);

#ifdef SUPPORT_SHA3
	r = spec.bitrate;
	c = spec.capacity;
	n = spec.output;
#else	
	if (w && !s)
		s = w * 25;
	if (!!r + !!c == 2 && !s) {
		n = n ? n : MAX(c * 2, 8);
	} else if (!!r + !!c == 1) {
		c = c ? c : (s ? s : 1600) - r;
		r = r ? r : (s ? s : 1600) - c;
		n = n ? n : MAX(c * 2, 8);
	} else {
		c = (s ? s : 1600) * 9 / 25;
		r = (s ? s : 1600) - c;
		n = n ? n : MAX(r / 2, 8);
	}
#endif

	if (*algorithm == LIBHASHSUM_KECCAK && (s || w))
		*algostr = *algostrbuf = fmtalgostr(*algostr, r, c, n, z);
	else if (*algorithm != LIBHASHSUM_KECCAK && n != bits)
		*algostr = *algostrbuf = fmtalgostr(*algostr, 0, 0, n, 0);

	return argc;
}