aboutsummaryrefslogtreecommitdiffstats
path: root/sha3sum.c
blob: a34f8aa061729c3fda6389db54095680e2e021db (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
/* See LICENSE file for copyright and license details. */
#include "common.h"
#include "arg.h"

#include <alloca.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


static void
usage(void)
{
	/*
	 * Since our main in this file validates the arguments,
	 * we can add -a here but leave it left out in common.c,
	 * which we like because the other commands do not have -a.
	 */

	fprintf(stderr, "usage: %s [-u | -l | -b | -c] [-a bits] [-R rate] [-C capacity] "
	                "[-N output-size] [-S state-size] [-W word-size] "
	                "[-Z squeeze-count] [-vxz] [file ...]\n", argv0);
	exit(2);
}

int
main(int argc, char *argv[])
{
	int bits = 224, orig_argc = argc;
	char **orig_argv = alloca((argc + 1) * sizeof(*argv));
	struct libkeccak_generalised_spec spec;

	libkeccak_generalised_spec_initialise(&spec);
	memcpy(orig_argv, argv, (argc + 1) * sizeof(*argv));

	ARGBEGIN {
	case 'R':
	case 'C':
	case 'N':
	case 'O':
	case 'S':
	case 'B':
	case 'W':
	case 'Z':
		(void) EARGF(usage());
		break;
	case 'u':
	case 'l':
	case 'b':
	case 'c':
	case 'v':
	case 'x':
	case 'z':
		break;
	case 'a':
		bits = atoi(EARGF(usage()));
		if (bits != 224 && bits != 256 && bits != 384 && bits != 512) {
			fprintf(stderr, "%s: valid arguments for -a are 224 (default), 256, 384, and 512\n", argv0);
			return 2;
		}
		break;
	default:
		usage();
	} ARGEND;

	libkeccak_spec_sha3((struct libkeccak_spec *)&spec, bits);
	return run(orig_argc, orig_argv, &spec, LIBKECCAK_SHA3_SUFFIX, 1);
}