1 /**********************************************************************
3 * Copyright (c) 2005-2019 Cryptocom LTD *
4 * This file is distributed under same license as OpenSSL *
6 * Implementation of GOST R 34.11-2012 hash function as *
7 * command line utility more or less interface *
8 * compatible with md5sum and sha1sum *
9 * Doesn't need OpenSSL *
10 **********************************************************************/
17 #include "gosthash2012.h"
18 #define BUF_SIZE 262144
19 #define MAX_HASH_TXT_BYTES 128
20 #define gost_hash_ctx gost2012_hash_ctx
22 typedef unsigned char byte;
23 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode,
25 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum, int hashsize);
26 int get_line(FILE *f, char *hash, char *filename, int verbose, int *size);
30 fprintf(stderr, "Calculates GOST R 34.11-2012 hash function\n\n");
31 fprintf(stderr, "gost12sum [-vl] [-c [file]]| [files]|-x\n"
32 "\t-c check message digests (default is generate)\n"
33 "\t-v verbose, print file names when checking\n"
34 "\t-l use 512 bit hash (default 256 bit)\n"
35 "\t-x read filenames from stdin rather than from arguments (256 bit only)\n"
36 "\t-h print this help\n"
37 "The input for -c should be the list of message digests and file names\n"
38 "that is printed on stdout by this program when it generates digests.\n");
46 int start_hash(gost_hash_ctx * ctx, int hashsize)
48 init_gost2012_hash_ctx(ctx, hashsize);
52 int hash_block(gost_hash_ctx * ctx, const byte * block, size_t length)
54 gost2012_hash_block(ctx, block, length);
58 int finish_hash(gost_hash_ctx * ctx, byte * hashval)
60 gost2012_finish_hash(ctx, hashval);
64 int main(int argc, char **argv)
69 int open_mode = O_RDONLY | O_BINARY;
70 FILE *check_file = NULL;
71 int filenames_from_stdin = 0;
75 while ((c = getopt(argc, argv, "hxlvc::")) != -1) {
88 filenames_from_stdin = 1;
92 check_file = fopen(optarg, "r");
102 fprintf(stderr, "invalid option %c\n", optopt);
108 char inhash[MAX_HASH_TXT_BYTES + 1], calcsum[MAX_HASH_TXT_BYTES + 1],
110 int failcount = 0, count = 0;
111 int expected_hash_size = 0;
112 if (check_file == stdin && optind < argc) {
113 check_file = fopen(argv[optind], "r");
115 perror(argv[optind]);
120 (check_file, inhash, filename, verbose, &expected_hash_size)) {
122 if (expected_hash_size == 0) {
123 fprintf(stderr, "%s: invalid hash length\n", filename);
130 (&ctx, filename, calcsum, open_mode, expected_hash_size)) {
138 if (!strncmp(calcsum, inhash, expected_hash_size * 2 + 1)) {
140 fprintf(stderr, "%s\tOK\n", filename);
144 fprintf(stderr, "%s\tFAILED\n", filename);
146 fprintf(stderr, "%s: GOST hash sum check failed\n",
154 "%s: WARNING %d of %d file(s) cannot be processed\n",
155 argv[0], errors, count);
160 "%s: WARNING %d of %d processed file(s) failed GOST hash sum check\n",
161 argv[0], failcount, count - errors);
163 exit((failcount || errors) ? 1 : 0);
164 } else if (filenames_from_stdin) {
165 char sum[MAX_HASH_TXT_BYTES + 1];
166 char filename[PATH_MAX + 1], *end;
167 while (!feof(stdin)) {
168 if (!fgets(filename, PATH_MAX, stdin))
170 for (end = filename; *end; end++) ;
172 for (; *end == '\n' || *end == '\r'; end--)
174 if (!hash_file(&ctx, filename, sum, open_mode, hashsize)) {
177 printf("%s %s\n", sum, filename);
181 } else if (optind == argc) {
182 char sum[MAX_HASH_TXT_BYTES + 1];
183 if (!hash_stream(&ctx, fileno(stdin), sum, hashsize)) {
187 printf("%s -\n", sum);
190 for (i = optind; i < argc; i++) {
191 char sum[MAX_HASH_TXT_BYTES + 1];
192 if (!hash_file(&ctx, argv[i], sum, open_mode, hashsize)) {
195 printf("%s %s\n", sum, argv[i]);
199 exit(errors ? 1 : 0);
202 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode,
206 if ((fd = open(filename, mode)) < 0) {
210 if (!hash_stream(ctx, fd, sum, hashsize)) {
218 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum, int hashsize)
220 unsigned char buffer[BUF_SIZE];
223 start_hash(ctx, hashsize * 8);
224 while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
225 hash_block(ctx, buffer, bytes);
230 finish_hash(ctx, buffer);
231 for (i = 0; i < hashsize; i++) {
232 sprintf(sum + 2 * i, "%02x", buffer[i]);
237 int get_line(FILE *f, char *hash, char *filename, int verbose, int *size)
240 char *ptr = filename;
241 char *spacepos = NULL;
244 if (!fgets(filename, PATH_MAX, f))
251 while (ptr[--len] == '\n' || ptr[len] == '\r')
257 spacepos = strchr(ptr, ' ');
258 if (spacepos == NULL || strlen(spacepos + 1) == 0)
261 *size = spacepos - ptr;
263 for (i = 0; i < *size; i++) {
264 if (ptr[i] < '0' || (ptr[i] > '9' && ptr[i] < 'A') ||
265 (ptr[i] > 'F' && ptr[i] < 'a') || ptr[i] > 'f') {
270 if (*size > 128 || ((*size != 64) && (*size != 128))) {
272 memset(hash, 0, MAX_HASH_TXT_BYTES + 1);
274 memcpy(hash, ptr, *size);
277 memmove(filename, spacepos + 1, strlen(spacepos));
283 printf("Skipping line %s\n", filename);