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 **********************************************************************/
17 #include "gosthash2012.h"
19 #define BUF_SIZE 262144
20 #define gost_hash_ctx gost2012_hash_ctx
21 #define GOST34112012Init init_gost2012_hash_ctx
22 #define GOST34112012Update gost2012_hash_block
23 #define GOST34112012Final gost2012_finish_hash
25 typedef unsigned char byte;
28 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode);
29 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum);
30 int get_line(FILE *f, char *hash, char *filename, int verbose);
34 fprintf(stderr, "Calculates GOST R 34.11-2012 hash function\n\n");
35 fprintf(stderr, "gostsum12 [-bvl] [-c [file]]| [files]|-x\n"
36 "\t-c check message digests (default is generate), 256 bit only\n"
37 "\t-v verbose, print file names when checking\n"
38 "\t-b read files in binary mode\n"
39 "\t-l use 512 bit hash (default 256 bit)\n"
40 "\t-x read filenames from stdin rather than from arguments (256 bit only)\n"
41 "The input for -c should be the list of message digests and file names\n"
42 "that is printed on stdout by this program when it generates digests.\n");
50 int start_hash12(gost_hash_ctx * ctx)
52 GOST34112012Init(ctx, hashsize);
56 int hash12_block(gost_hash_ctx * ctx, const byte * block, size_t length)
58 GOST34112012Update(ctx, block, length);
62 int finish_hash12(gost_hash_ctx * ctx, byte * hashval)
64 GOST34112012Final(ctx, hashval);
68 int main(int argc, char **argv)
73 int open_mode = O_RDONLY;
74 FILE *check_file = NULL;
75 int filenames_from_stdin = 0;
78 while ((c = getopt(argc, argv, "xlvc::")) != -1) {
87 filenames_from_stdin = 1;
91 check_file = fopen(optarg, "r");
101 fprintf(stderr, "invalid option %c", optopt);
105 if ((filenames_from_stdin == 1) && (hashsize != 256)) {
106 fprintf(stderr, "x and l are mutually exclusive\n");
109 if ((check_file != NULL) && (hashsize != 256)) {
110 fprintf(stderr, "c and l are mutually exclusive\n");
114 char inhash[65], calcsum[65], filename[PATH_MAX];
115 int failcount = 0, count = 0;;
116 if (check_file == stdin && optind < argc) {
117 check_file = fopen(argv[optind], "r");
119 perror(argv[optind]);
123 while (get_line(check_file, inhash, filename, verbose)) {
124 if (!hash_file(&ctx, filename, calcsum, open_mode)) {
128 if (!strncmp(calcsum, inhash, 65)) {
130 fprintf(stderr, "%s\tOK\n", filename);
134 fprintf(stderr, "%s\tFAILED\n", filename);
137 "%s: GOST hash sum check failed for '%s'\n",
145 "%s: WARNING %d of %d file(s) cannot be processed\n",
146 argv[0], errors, count);
151 "%s: WARNING %d of %d file(s) failed GOST hash sum check\n",
152 argv[0], failcount, count - errors);
154 exit((failcount || errors) ? 1 : 0);
155 } else if (filenames_from_stdin) {
157 char filename[PATH_MAX + 1], *end;
158 while (!feof(stdin)) {
159 if (!fgets(filename, PATH_MAX, stdin))
161 for (end = filename; *end; end++) ;
163 for (; *end == '\n' || *end == '\r'; end--)
165 if (!hash_file(&ctx, filename, sum, open_mode)) {
168 printf("%s %s\n", sum, filename);
171 } else if (optind == argc) {
173 if (!hash_stream(&ctx, fileno(stdin), sum)) {
177 printf("%s -\n", sum);
180 for (i = optind; i < argc; i++) {
182 if (!hash_file(&ctx, argv[i], sum, open_mode)) {
185 printf("%s %s\n", sum, argv[i]);
189 exit(errors ? 1 : 0);
192 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
195 if ((fd = open(filename, mode)) < 0) {
199 if (!hash_stream(ctx, fd, sum)) {
207 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
209 unsigned char buffer[BUF_SIZE];
213 while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
214 hash12_block(ctx, buffer, bytes);
219 finish_hash12(ctx, buffer);
220 for (i = 0; i < (hashsize / 8); i++) {
221 sprintf(sum + 2 * i, "%02x", buffer[i]);
226 int get_line(FILE *f, char *hash, char *filename, int verbose)
230 if (!fgets(filename, PATH_MAX, f))
232 len = strlen(filename);
236 if (filename[64] != ' ') {
239 for (i = 0; i < 64; i++) {
240 if (filename[i] < '0' || (filename[i] > '9' && filename[i] < 'A')
241 || (filename[i] > 'F' && filename[i] < 'a')
242 || filename[i] > 'f') {
246 memcpy(hash, filename, 64);
248 while (filename[--len] == '\n' || filename[len] == '\r')
250 memmove(filename, filename + 65, len - 63);