1 /**********************************************************************
3 * Copyright (c) 2005-2014 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 **********************************************************************/
20 #include "gosthash2012.h"
22 #define BUF_SIZE 262144
23 #define gost_hash_ctx gost2012_hash_ctx
24 #define GOST34112012Init init_gost2012_hash_ctx
25 #define GOST34112012Update gost2012_hash_block
26 #define GOST34112012Final gost2012_finish_hash
28 #define MAX_HASH_SIZE 128
30 typedef unsigned char byte;
33 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode);
34 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum);
35 int get_line(FILE *f, char *hash, char *filename, int verbose);
39 fprintf(stderr, "Calculates GOST R 34.11-2012 hash function\n\n");
40 fprintf(stderr, "gostsum12 [-bvl] [-c [file]]| [files]|-x\n"
41 "\t-c check message digests (default is generate)\n"
42 "\t-v verbose, print file names when checking\n"
43 "\t-b read files in binary mode\n"
44 "\t-l use 512 bit hash (default 256 bit)\n"
45 "\t-x read filenames from stdin rather than from arguments \n"
46 "The input for -c should be the list of message digests and file names\n"
47 "that is printed on stdout by this program when it generates digests.\n");
55 int start_hash12(gost_hash_ctx * ctx)
57 GOST34112012Init(ctx, hashsize);
61 int hash12_block(gost_hash_ctx * ctx, const byte * block, size_t length)
63 GOST34112012Update(ctx, block, length);
67 int finish_hash12(gost_hash_ctx * ctx, byte * hashval)
69 GOST34112012Final(ctx, hashval);
73 int main(int argc, char **argv)
78 int open_mode = O_RDONLY;
79 FILE *check_file = NULL;
80 int filenames_from_stdin = 0;
83 while ((c = getopt(argc, argv, "bxlvc::")) != -1) {
86 open_mode = open_mode | O_BINARY;
95 filenames_from_stdin = 1;
99 check_file = fopen(optarg, "r");
109 fprintf(stderr, "invalid option %c", optopt);
114 char inhash[MAX_HASH_SIZE + 1], calcsum[MAX_HASH_SIZE + 1],
116 int failcount = 0, count = 0;;
117 if (check_file == stdin && optind < argc) {
118 check_file = fopen(argv[optind], "r");
120 perror(argv[optind]);
124 while (get_line(check_file, inhash, filename, verbose)) {
126 if (!hash_file(&ctx, filename, calcsum, open_mode)) {
130 if (!strncmp(calcsum, inhash, hashsize / 4 + 1)) {
132 fprintf(stderr, "%s\tOK\n", filename);
136 fprintf(stderr, "%s\tFAILED\n", filename);
139 "%s: GOST hash sum check failed for '%s'\n",
147 "%s: WARNING %d of %d file(s) cannot be processed\n",
148 argv[0], errors, count);
153 "%s: WARNING %d of %d file(s) failed GOST hash sum check\n",
154 argv[0], failcount, count - errors);
156 exit((failcount || errors) ? 1 : 0);
157 } else if (filenames_from_stdin) {
159 char filename[PATH_MAX + 1], *end;
160 while (!feof(stdin)) {
161 if (!fgets(filename, PATH_MAX, stdin))
163 for (end = filename; *end; end++) ;
165 for (; *end == '\n' || *end == '\r'; end--)
167 if (!hash_file(&ctx, filename, sum, open_mode)) {
170 printf("%s %s\n", sum, filename);
173 } else if (optind == argc) {
176 if (open_mode & O_BINARY) {
177 _setmode(fileno(stdin), O_BINARY);
180 if (!hash_stream(&ctx, fileno(stdin), sum)) {
184 printf("%s -\n", sum);
187 for (i = optind; i < argc; i++) {
189 if (!hash_file(&ctx, argv[i], sum, open_mode)) {
192 printf("%s %s\n", sum, argv[i]);
196 exit(errors ? 1 : 0);
199 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
202 if ((fd = open(filename, mode)) < 0) {
206 if (!hash_stream(ctx, fd, sum)) {
214 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
216 unsigned char buffer[BUF_SIZE];
217 unsigned char reverted_buffer[BUF_SIZE];
221 while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
222 hash12_block(ctx, reverted_buffer, bytes);
227 finish_hash12(ctx, buffer);
228 for (i = 0; i < (hashsize / 8); i++) {
229 sprintf(sum + 2 * i, "%02x", buffer[i]);
234 int get_line(FILE *f, char *hash, char *filename, int verbose)
237 int hashstrlen = hashsize / 4;
239 if (!fgets(filename, PATH_MAX, f))
241 len = strlen(filename);
242 if (len < hashstrlen + 2) {
245 if (filename[hashstrlen] != ' ') {
248 for (i = 0; i < hashstrlen; i++) {
249 if (filename[i] < '0' || (filename[i] > '9' && filename[i] < 'A')
250 || (filename[i] > 'F' && filename[i] < 'a')
251 || filename[i] > 'f') {
255 memcpy(hash, filename, hashstrlen);
256 hash[hashstrlen] = 0;
257 while (filename[--len] == '\n' || filename[len] == '\r')
259 memmove(filename, filename + hashstrlen + 1, len - hashstrlen + 1);
263 printf("%s\n", filename);