]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost12sum.c
Fix
[openssl-gost/engine.git] / gost12sum.c
1 /**********************************************************************
2  *                          gostsum12.c                               *
3  *             Copyright (c) 2005-2014 Cryptocom LTD                  *
4  *         This file is distributed under same license as OpenSSL     *
5  *                                                                    *
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  **********************************************************************/
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <limits.h>
15 #include <fcntl.h>
16 #ifdef _WIN32
17 # include <io.h>
18 #endif
19 #include <string.h>
20 #include "gosthash2012.h"
21
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
27
28 #define MAX_HASH_SIZE 128
29
30 typedef unsigned char byte;
31
32 int hashsize = 256;
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);
36
37 void help()
38 {
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");
48     exit(3);
49 }
50
51 #ifndef O_BINARY
52 # define O_BINARY 0
53 #endif
54
55 int start_hash12(gost_hash_ctx * ctx)
56 {
57     GOST34112012Init(ctx, hashsize);
58     return 1;
59 }
60
61 int hash12_block(gost_hash_ctx * ctx, const byte * block, size_t length)
62 {
63     GOST34112012Update(ctx, block, length);
64     return 1;
65 }
66
67 int finish_hash12(gost_hash_ctx * ctx, byte * hashval)
68 {
69     GOST34112012Final(ctx, hashval);
70     return 1;
71 }
72
73 int main(int argc, char **argv)
74 {
75     int c, i;
76     int verbose = 0;
77     int errors = 0;
78     int open_mode = O_RDONLY;
79     FILE *check_file = NULL;
80     int filenames_from_stdin = 0;
81     gost_hash_ctx ctx;
82
83     while ((c = getopt(argc, argv, "bxlvc::")) != -1) {
84         switch (c) {
85         case 'b':
86             open_mode = open_mode | O_BINARY;
87             break;
88         case 'v':
89             verbose = 1;
90             break;
91         case 'l':
92             hashsize = 512;
93             break;
94         case 'x':
95             filenames_from_stdin = 1;
96             break;
97         case 'c':
98             if (optarg) {
99                 check_file = fopen(optarg, "r");
100                 if (!check_file) {
101                     perror(optarg);
102                     exit(2);
103                 }
104             } else {
105                 check_file = stdin;
106             }
107             break;
108         default:
109             fprintf(stderr, "invalid option %c", optopt);
110             help();
111         }
112     }
113     if (check_file) {
114         char inhash[MAX_HASH_SIZE + 1], calcsum[MAX_HASH_SIZE + 1],
115             filename[PATH_MAX];
116         int failcount = 0, count = 0;;
117         if (check_file == stdin && optind < argc) {
118             check_file = fopen(argv[optind], "r");
119             if (!check_file) {
120                 perror(argv[optind]);
121                 exit(2);
122             }
123         }
124         while (get_line(check_file, inhash, filename, verbose)) {
125             count++;
126             if (!hash_file(&ctx, filename, calcsum, open_mode)) {
127                 errors++;
128                 continue;
129             }
130             if (!strncmp(calcsum, inhash, hashsize / 4 + 1)) {
131                 if (verbose) {
132                     fprintf(stderr, "%s\tOK\n", filename);
133                 }
134             } else {
135                 if (verbose) {
136                     fprintf(stderr, "%s\tFAILED\n", filename);
137                 } else {
138                     fprintf(stderr,
139                             "%s: GOST hash sum check failed for '%s'\n",
140                             argv[0], filename);
141                 }
142                 failcount++;
143             }
144         }
145         if (errors) {
146             fprintf(stderr,
147                     "%s: WARNING %d of %d file(s) cannot be processed\n",
148                     argv[0], errors, count);
149
150         }
151         if (failcount) {
152             fprintf(stderr,
153                     "%s: WARNING %d of %d file(s) failed GOST hash sum check\n",
154                     argv[0], failcount, count - errors);
155         }
156         exit((failcount || errors) ? 1 : 0);
157     } else if (filenames_from_stdin) {
158         char sum[65];
159         char filename[PATH_MAX + 1], *end;
160         while (!feof(stdin)) {
161             if (!fgets(filename, PATH_MAX, stdin))
162                 break;
163             for (end = filename; *end; end++) ;
164             end--;
165             for (; *end == '\n' || *end == '\r'; end--)
166                 *end = 0;
167             if (!hash_file(&ctx, filename, sum, open_mode)) {
168                 errors++;
169             } else {
170                 printf("%s %s\n", sum, filename);
171             }
172         }
173     } else if (optind == argc) {
174         char sum[65];
175 #ifdef _WIN32
176         if (open_mode & O_BINARY) {
177             _setmode(fileno(stdin), O_BINARY);
178         }
179 #endif
180         if (!hash_stream(&ctx, fileno(stdin), sum)) {
181             perror("stdin");
182             exit(1);
183         }
184         printf("%s -\n", sum);
185         exit(0);
186     } else {
187         for (i = optind; i < argc; i++) {
188             char sum[65];
189             if (!hash_file(&ctx, argv[i], sum, open_mode)) {
190                 errors++;
191             } else {
192                 printf("%s %s\n", sum, argv[i]);
193             }
194         }
195     }
196     exit(errors ? 1 : 0);
197 }
198
199 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
200 {
201     int fd;
202     if ((fd = open(filename, mode)) < 0) {
203         perror(filename);
204         return 0;
205     }
206     if (!hash_stream(ctx, fd, sum)) {
207         perror(filename);
208         return 0;
209     }
210     close(fd);
211     return 1;
212 }
213
214 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
215 {
216     unsigned char buffer[BUF_SIZE];
217     ssize_t bytes;
218                 size_t i;
219
220     start_hash12(ctx);
221     while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
222         hash12_block(ctx, buffer, bytes);
223     }
224     if (bytes < 0) {
225         return 0;
226     }
227     finish_hash12(ctx, buffer);
228     for (i = 0; i < (hashsize / 8); i++) {
229         sprintf(sum + 2 * i, "%02x", buffer[i]);
230     }
231     return 1;
232 }
233
234 int get_line(FILE *f, char *hash, char *filename, int verbose)
235 {
236     int i, len;
237     int hashstrlen = hashsize / 4;
238     while (!feof(f)) {
239         if (!fgets(filename, PATH_MAX, f))
240             return 0;
241         len = strlen(filename);
242         if (len < hashstrlen + 2) {
243             goto nextline;
244         }
245         if (filename[hashstrlen] != ' ') {
246             goto nextline;
247         }
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') {
252                 goto nextline;
253             }
254         }
255         memcpy(hash, filename, hashstrlen);
256         hash[hashstrlen] = 0;
257         while (filename[--len] == '\n' || filename[len] == '\r')
258             filename[len] = 0;
259         memmove(filename, filename + hashstrlen + 1, len - hashstrlen + 1);
260         return 1;
261  nextline:
262         if (verbose)
263             printf("%s\n", filename);
264     }
265     return 0;
266 }