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 **********************************************************************/
16 # define PATH_MAX _MAX_PATH
19 typedef SSIZE_T ssize_t;
29 #include "gosthash2012.h"
30 #define BUF_SIZE 262144
31 #define MAX_HASH_TXT_BYTES 128
32 #define gost_hash_ctx gost2012_hash_ctx
34 typedef unsigned char byte;
35 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode,
37 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum, int hashsize);
38 int get_line(FILE *f, char *hash, char *filename, int verbose, int *size);
42 fprintf(stderr, "Calculates GOST R 34.11-2012 hash function\n\n");
43 fprintf(stderr, "gost12sum [-vl] [-c [file]]| [files]|-x\n"
44 "\t-c check message digests (default is generate)\n"
45 "\t-v verbose, print file names when checking\n"
46 "\t-l use 512 bit hash (default 256 bit)\n"
47 "\t-x read filenames from stdin rather than from arguments (256 bit only)\n"
48 "\t-h print this help\n"
49 "The input for -c should be the list of message digests and file names\n"
50 "that is printed on stdout by this program when it generates digests.\n");
58 int start_hash(gost_hash_ctx * ctx, int hashsize)
60 init_gost2012_hash_ctx(ctx, hashsize);
64 int hash_block(gost_hash_ctx * ctx, const byte * block, size_t length)
66 gost2012_hash_block(ctx, block, length);
70 int finish_hash(gost_hash_ctx * ctx, byte * hashval)
72 gost2012_finish_hash(ctx, hashval);
76 int main(int argc, char **argv)
81 int open_mode = O_RDONLY | O_BINARY;
82 FILE *check_file = NULL;
83 int filenames_from_stdin = 0;
87 while ((c = getopt(argc, argv, "hxlvc::")) != -1) {
100 filenames_from_stdin = 1;
104 check_file = fopen(optarg, "r");
114 fprintf(stderr, "invalid option %c\n", optopt);
120 char inhash[MAX_HASH_TXT_BYTES + 1], calcsum[MAX_HASH_TXT_BYTES + 1],
122 int failcount = 0, count = 0;
123 int expected_hash_size = 0;
124 if (check_file == stdin && optind < argc) {
125 check_file = fopen(argv[optind], "r");
127 perror(argv[optind]);
132 (check_file, inhash, filename, verbose, &expected_hash_size)) {
134 if (expected_hash_size == 0) {
135 fprintf(stderr, "%s: invalid hash length\n", filename);
142 (&ctx, filename, calcsum, open_mode, expected_hash_size)) {
150 if (!strncmp(calcsum, inhash, expected_hash_size * 2 + 1)) {
152 fprintf(stderr, "%s\tOK\n", filename);
156 fprintf(stderr, "%s\tFAILED\n", filename);
158 fprintf(stderr, "%s: GOST hash sum check failed\n",
166 "%s: WARNING %d of %d file(s) cannot be processed\n",
167 argv[0], errors, count);
172 "%s: WARNING %d of %d processed file(s) failed GOST hash sum check\n",
173 argv[0], failcount, count - errors);
175 exit((failcount || errors) ? 1 : 0);
176 } else if (filenames_from_stdin) {
177 char sum[MAX_HASH_TXT_BYTES + 1];
178 char filename[PATH_MAX + 1], *end;
179 while (!feof(stdin)) {
180 if (!fgets(filename, PATH_MAX, stdin))
182 for (end = filename; *end; end++) ;
184 for (; *end == '\n' || *end == '\r'; end--)
186 if (!hash_file(&ctx, filename, sum, open_mode, hashsize)) {
189 printf("%s %s\n", sum, filename);
193 } else if (optind == argc) {
194 char sum[MAX_HASH_TXT_BYTES + 1];
195 if (!hash_stream(&ctx, fileno(stdin), sum, hashsize)) {
199 printf("%s -\n", sum);
202 for (i = optind; i < argc; i++) {
203 char sum[MAX_HASH_TXT_BYTES + 1];
204 if (!hash_file(&ctx, argv[i], sum, open_mode, hashsize)) {
207 printf("%s %s\n", sum, argv[i]);
211 exit(errors ? 1 : 0);
214 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode,
218 if ((fd = open(filename, mode)) < 0) {
222 if (!hash_stream(ctx, fd, sum, hashsize)) {
231 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum, int hashsize)
233 unsigned char buffer[BUF_SIZE];
236 start_hash(ctx, hashsize * 8);
237 while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
238 hash_block(ctx, buffer, bytes);
243 finish_hash(ctx, buffer);
244 for (i = 0; i < hashsize; i++) {
245 sprintf(sum + 2 * i, "%02x", buffer[i]);
250 int get_line(FILE *f, char *hash, char *filename, int verbose, int *size)
253 char *ptr = filename;
254 char *spacepos = NULL;
257 if (!fgets(filename, PATH_MAX, f))
264 while (ptr[--len] == '\n' || ptr[len] == '\r')
270 spacepos = strchr(ptr, ' ');
271 if (spacepos == NULL || strlen(spacepos + 1) == 0)
274 *size = spacepos - ptr;
276 for (i = 0; i < *size; i++) {
277 if (ptr[i] < '0' || (ptr[i] > '9' && ptr[i] < 'A') ||
278 (ptr[i] > 'F' && ptr[i] < 'a') || ptr[i] > 'f') {
283 if (*size > 128 || ((*size != 64) && (*size != 128))) {
285 memset(hash, 0, MAX_HASH_TXT_BYTES + 1);
287 memcpy(hash, ptr, *size);
290 memmove(filename, spacepos + 1, strlen(spacepos));
296 printf("Skipping line %s\n", filename);