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 **********************************************************************/
16 # define PATH_MAX _MAX_PATH
19 typedef SSIZE_T ssize_t;
29 #include "gosthash2012.h"
31 #define BUF_SIZE 262144
32 #define gost_hash_ctx gost2012_hash_ctx
33 #define GOST34112012Init init_gost2012_hash_ctx
34 #define GOST34112012Update gost2012_hash_block
35 #define GOST34112012Final gost2012_finish_hash
37 #define MAX_HASH_SIZE 128
39 typedef unsigned char byte;
42 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode);
43 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum);
44 int get_line(FILE *f, char *hash, char *filename, int verbose);
48 fprintf(stderr, "Calculates GOST R 34.11-2012 hash function\n\n");
49 fprintf(stderr, "gostsum12 [-bvl] [-c [file]]| [files]|-x\n"
50 "\t-c check message digests (default is generate)\n"
51 "\t-v verbose, print file names when checking\n"
52 "\t-b read files in binary mode\n"
53 "\t-l use 512 bit hash (default 256 bit)\n"
54 "\t-x read filenames from stdin rather than from arguments \n"
55 "The input for -c should be the list of message digests and file names\n"
56 "that is printed on stdout by this program when it generates digests.\n");
64 int start_hash12(gost_hash_ctx * ctx)
66 GOST34112012Init(ctx, hashsize);
70 int hash12_block(gost_hash_ctx * ctx, const byte * block, size_t length)
72 GOST34112012Update(ctx, block, length);
76 int finish_hash12(gost_hash_ctx * ctx, byte * hashval)
78 GOST34112012Final(ctx, hashval);
82 int main(int argc, char **argv)
87 int open_mode = O_RDONLY;
88 FILE *check_file = NULL;
89 int filenames_from_stdin = 0;
92 while ((c = getopt(argc, argv, "bxlvc::")) != -1) {
95 open_mode = open_mode | O_BINARY;
104 filenames_from_stdin = 1;
108 check_file = fopen(optarg, "r");
118 fprintf(stderr, "invalid option %c", optopt);
123 char inhash[MAX_HASH_SIZE + 1], calcsum[MAX_HASH_SIZE + 1],
125 int failcount = 0, count = 0;;
126 if (check_file == stdin && optind < argc) {
127 check_file = fopen(argv[optind], "r");
129 perror(argv[optind]);
133 while (get_line(check_file, inhash, filename, verbose)) {
135 if (!hash_file(&ctx, filename, calcsum, open_mode)) {
139 if (!strncmp(calcsum, inhash, hashsize / 4 + 1)) {
141 fprintf(stderr, "%s\tOK\n", filename);
145 fprintf(stderr, "%s\tFAILED\n", filename);
148 "%s: GOST hash sum check failed for '%s'\n",
156 "%s: WARNING %d of %d file(s) cannot be processed\n",
157 argv[0], errors, count);
162 "%s: WARNING %d of %d file(s) failed GOST hash sum check\n",
163 argv[0], failcount, count - errors);
165 exit((failcount || errors) ? 1 : 0);
166 } else if (filenames_from_stdin) {
168 char filename[PATH_MAX + 1], *end;
169 while (!feof(stdin)) {
170 if (!fgets(filename, PATH_MAX, stdin))
172 for (end = filename; *end; end++) ;
174 for (; *end == '\n' || *end == '\r'; end--)
176 if (!hash_file(&ctx, filename, sum, open_mode)) {
179 printf("%s %s\n", sum, filename);
182 } else if (optind == argc) {
185 if (open_mode & O_BINARY) {
186 _setmode(fileno(stdin), O_BINARY);
189 if (!hash_stream(&ctx, fileno(stdin), sum)) {
193 printf("%s -\n", sum);
196 for (i = optind; i < argc; i++) {
198 if (!hash_file(&ctx, argv[i], sum, open_mode)) {
201 printf("%s %s\n", sum, argv[i]);
205 exit(errors ? 1 : 0);
208 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
211 if ((fd = open(filename, mode)) < 0) {
215 if (!hash_stream(ctx, fd, sum)) {
223 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
225 unsigned char buffer[BUF_SIZE];
230 while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
231 hash12_block(ctx, buffer, bytes);
236 finish_hash12(ctx, buffer);
237 for (i = 0; i < (hashsize / 8); i++) {
238 sprintf(sum + 2 * i, "%02x", buffer[i]);
243 int get_line(FILE *f, char *hash, char *filename, int verbose)
246 int hashstrlen = hashsize / 4;
248 if (!fgets(filename, PATH_MAX, f))
250 len = strlen(filename);
251 if (len < hashstrlen + 2) {
254 if (filename[hashstrlen] != ' ') {
257 for (i = 0; i < hashstrlen; i++) {
258 if (filename[i] < '0' || (filename[i] > '9' && filename[i] < 'A')
259 || (filename[i] > 'F' && filename[i] < 'a')
260 || filename[i] > 'f') {
264 memcpy(hash, filename, hashstrlen);
265 hash[hashstrlen] = 0;
266 while (filename[--len] == '\n' || filename[len] == '\r')
268 memmove(filename, filename + hashstrlen + 1, len - hashstrlen + 1);
272 printf("%s\n", filename);