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);
37 void help(const char *progname)
39 fprintf(stderr, "Calculates GOST R 34.11-2012 hash function\n\n");
40 fprintf(stderr, "%s [-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"
48 "digests.\n", progname);
56 int start_hash12(gost_hash_ctx * ctx)
58 GOST34112012Init(ctx, hashsize);
62 int hash12_block(gost_hash_ctx * ctx, const byte * block, size_t length)
64 GOST34112012Update(ctx, block, length);
68 int finish_hash12(gost_hash_ctx * ctx, byte * hashval)
70 GOST34112012Final(ctx, hashval);
74 int main(int argc, char **argv)
79 int open_mode = O_RDONLY;
80 FILE *check_file = NULL;
81 int filenames_from_stdin = 0;
84 while ((c = getopt(argc, argv, "bxlvc::")) != -1) {
87 open_mode = open_mode | O_BINARY;
96 filenames_from_stdin = 1;
100 check_file = fopen(optarg, "r");
110 fprintf(stderr, "invalid option %c", optopt);
115 char inhash[MAX_HASH_SIZE + 1], calcsum[MAX_HASH_SIZE + 1],
117 int failcount = 0, count = 0;;
118 if (check_file == stdin && optind < argc) {
119 check_file = fopen(argv[optind], "r");
121 perror(argv[optind]);
125 while (get_line(check_file, inhash, filename, verbose)) {
127 if (!hash_file(&ctx, filename, calcsum, open_mode)) {
131 if (!strncmp(calcsum, inhash, hashsize / 4 + 1)) {
133 fprintf(stderr, "%s\tOK\n", filename);
137 fprintf(stderr, "%s\tFAILED\n", filename);
140 "%s: GOST hash sum check failed for '%s'\n",
148 "%s: WARNING %d of %d file(s) cannot be processed\n",
149 argv[0], errors, count);
154 "%s: WARNING %d of %d file(s) failed GOST hash sum check\n",
155 argv[0], failcount, count - errors);
157 exit((failcount || errors) ? 1 : 0);
158 } else if (filenames_from_stdin) {
160 char filename[PATH_MAX + 1], *end;
161 while (!feof(stdin)) {
162 if (!fgets(filename, PATH_MAX, stdin))
164 for (end = filename; *end; end++) ;
166 for (; *end == '\n' || *end == '\r'; end--)
168 if (!hash_file(&ctx, filename, sum, open_mode)) {
171 printf("%s %s\n", sum, filename);
174 } else if (optind == argc) {
177 if (open_mode & O_BINARY) {
178 _setmode(fileno(stdin), O_BINARY);
181 if (!hash_stream(&ctx, fileno(stdin), sum)) {
185 printf("%s -\n", sum);
188 for (i = optind; i < argc; i++) {
190 if (!hash_file(&ctx, argv[i], sum, open_mode)) {
193 printf("%s %s\n", sum, argv[i]);
197 exit(errors ? 1 : 0);
200 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
203 if ((fd = open(filename, mode)) < 0) {
207 if (!hash_stream(ctx, fd, sum)) {
215 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
217 unsigned char buffer[BUF_SIZE];
222 while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
223 hash12_block(ctx, buffer, bytes);
228 finish_hash12(ctx, buffer);
229 for (i = 0; i < (hashsize / 8); i++) {
230 sprintf(sum + 2 * i, "%02x", buffer[hashsize/8-1-i]);
235 int get_line(FILE *f, char *hash, char *filename, int verbose)
238 int hashstrlen = hashsize / 4;
240 if (!fgets(filename, PATH_MAX, f))
242 len = strlen(filename);
243 if (len < hashstrlen + 2) {
246 if (filename[hashstrlen] != ' ') {
249 for (i = 0; i < hashstrlen; i++) {
250 if (filename[i] < '0' || (filename[i] > '9' && filename[i] < 'A')
251 || (filename[i] > 'F' && filename[i] < 'a')
252 || filename[i] > 'f') {
256 memcpy(hash, filename, hashstrlen);
257 hash[hashstrlen] = 0;
258 while (filename[--len] == '\n' || filename[len] == '\r')
260 memmove(filename, filename + hashstrlen + 1, len - hashstrlen + 1);
264 printf("%s\n", filename);