]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost12sum.c
MSVC: Fix include files
[openssl-gost/engine.git] / gost12sum.c
1 /**********************************************************************
2  *                          gost12sum.c                                 *
3  *             Copyright (c) 2005-2019 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 #ifdef _MSC_VER
14 #include "getopt.h"
15 # ifndef PATH_MAX
16 #  define PATH_MAX _MAX_PATH
17 # endif
18 #include <BaseTsd.h>
19 typedef SSIZE_T ssize_t;
20 #else
21 #include <unistd.h>
22 #endif
23 #include <limits.h>
24 #include <fcntl.h>
25 #ifdef _WIN32
26 # include <io.h>
27 #endif
28 #include <string.h>
29 #include "gosthash2012.h"
30 #define BUF_SIZE 262144
31 #define MAX_HASH_TXT_BYTES 128
32 #define gost_hash_ctx gost2012_hash_ctx
33
34 typedef unsigned char byte;
35 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode,
36               int hashsize);
37 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum, int hashsize);
38 int get_line(FILE *f, char *hash, char *filename, int verbose, int *size);
39
40 void help()
41 {
42     fprintf(stderr, "Calculates GOST R 34.11-2012 hash function\n\n");
43     fprintf(stderr, "gost12sum [-vl] [-c [file]]| [files]|-x\n"
44             "\t-c check message digests (default is generate)\n"
45             "\t-v verbose, print file names when checking\n"
46             "\t-l use 512 bit hash (default 256 bit)\n"
47             "\t-x read filenames from stdin rather than from arguments (256 bit only)\n"
48             "\t-h print this help\n"
49             "The input for -c should be the list of message digests and file names\n"
50             "that is printed on stdout by this program when it generates digests.\n");
51     exit(3);
52 }
53
54 #ifndef O_BINARY
55 # define O_BINARY 0
56 #endif
57
58 int start_hash(gost_hash_ctx * ctx, int hashsize)
59 {
60     init_gost2012_hash_ctx(ctx, hashsize);
61     return 1;
62 }
63
64 int hash_block(gost_hash_ctx * ctx, const byte * block, size_t length)
65 {
66     gost2012_hash_block(ctx, block, length);
67     return 1;
68 }
69
70 int finish_hash(gost_hash_ctx * ctx, byte * hashval)
71 {
72     gost2012_finish_hash(ctx, hashval);
73     return 1;
74 }
75
76 int main(int argc, char **argv)
77 {
78     int c, i;
79     int verbose = 0;
80     int errors = 0;
81     int open_mode = O_RDONLY | O_BINARY;
82     FILE *check_file = NULL;
83     int filenames_from_stdin = 0;
84     int hashsize = 32;
85     gost_hash_ctx ctx;
86
87     while ((c = getopt(argc, argv, "hxlvc::")) != -1) {
88         switch (c) {
89         case 'h':
90             help();
91             exit(0);
92             break;
93         case 'v':
94             verbose = 1;
95             break;
96         case 'l':
97             hashsize = 64;
98             break;
99         case 'x':
100             filenames_from_stdin = 1;
101             break;
102         case 'c':
103             if (optarg) {
104                 check_file = fopen(optarg, "r");
105                 if (!check_file) {
106                     perror(optarg);
107                     exit(2);
108                 }
109             } else {
110                 check_file = stdin;
111             }
112             break;
113         default:
114             fprintf(stderr, "invalid option %c\n", optopt);
115             help();
116         }
117     }
118
119     if (check_file) {
120         char inhash[MAX_HASH_TXT_BYTES + 1], calcsum[MAX_HASH_TXT_BYTES + 1],
121             filename[PATH_MAX];
122         int failcount = 0, count = 0;
123         int expected_hash_size = 0;
124         if (check_file == stdin && optind < argc) {
125             check_file = fopen(argv[optind], "r");
126             if (!check_file) {
127                 perror(argv[optind]);
128                 exit(2);
129             }
130         }
131         while (get_line
132                (check_file, inhash, filename, verbose, &expected_hash_size)) {
133             int error = 0;
134             if (expected_hash_size == 0) {
135                 fprintf(stderr, "%s: invalid hash length\n", filename);
136                 errors++;
137                 count++;
138                 continue;
139             }
140
141             if (!hash_file
142                 (&ctx, filename, calcsum, open_mode, expected_hash_size)) {
143                 errors++;
144                 error = 1;
145             }
146             count++;
147             if (error)
148                 continue;
149
150             if (!strncmp(calcsum, inhash, expected_hash_size * 2 + 1)) {
151                 if (verbose) {
152                     fprintf(stderr, "%s\tOK\n", filename);
153                 }
154             } else {
155                 if (verbose) {
156                     fprintf(stderr, "%s\tFAILED\n", filename);
157                 } else {
158                     fprintf(stderr, "%s: GOST hash sum check failed\n",
159                             filename);
160                 }
161                 failcount++;
162             }
163         }
164         if (errors) {
165             fprintf(stderr,
166                     "%s: WARNING %d of %d file(s) cannot be processed\n",
167                     argv[0], errors, count);
168
169         }
170         if (failcount) {
171             fprintf(stderr,
172                     "%s: WARNING %d of %d processed file(s) failed GOST hash sum check\n",
173                     argv[0], failcount, count - errors);
174         }
175         exit((failcount || errors) ? 1 : 0);
176     } else if (filenames_from_stdin) {
177         char sum[MAX_HASH_TXT_BYTES + 1];
178         char filename[PATH_MAX + 1], *end;
179         while (!feof(stdin)) {
180             if (!fgets(filename, PATH_MAX, stdin))
181                 break;
182             for (end = filename; *end; end++) ;
183             end--;
184             for (; *end == '\n' || *end == '\r'; end--)
185                 *end = 0;
186             if (!hash_file(&ctx, filename, sum, open_mode, hashsize)) {
187                 errors++;
188             } else {
189                 printf("%s %s\n", sum, filename);
190             }
191         }
192
193     } else if (optind == argc) {
194         char sum[MAX_HASH_TXT_BYTES + 1];
195         if (!hash_stream(&ctx, fileno(stdin), sum, hashsize)) {
196             perror("stdin");
197             exit(1);
198         }
199         printf("%s -\n", sum);
200         exit(0);
201     } else {
202         for (i = optind; i < argc; i++) {
203             char sum[MAX_HASH_TXT_BYTES + 1];
204             if (!hash_file(&ctx, argv[i], sum, open_mode, hashsize)) {
205                 errors++;
206             } else {
207                 printf("%s %s\n", sum, argv[i]);
208             }
209         }
210     }
211     exit(errors ? 1 : 0);
212 }
213
214 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode,
215               int hashsize)
216 {
217     int fd;
218     if ((fd = open(filename, mode)) < 0) {
219         perror(filename);
220         return 0;
221     }
222     if (!hash_stream(ctx, fd, sum, hashsize)) {
223         perror(filename);
224         close(fd);
225         return 0;
226     }
227     close(fd);
228     return 1;
229 }
230
231 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum, int hashsize)
232 {
233     unsigned char buffer[BUF_SIZE];
234     ssize_t bytes;
235     int i;
236     start_hash(ctx, hashsize * 8);
237     while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
238         hash_block(ctx, buffer, bytes);
239     }
240     if (bytes < 0) {
241         return 0;
242     }
243     finish_hash(ctx, buffer);
244     for (i = 0; i < hashsize; i++) {
245         sprintf(sum + 2 * i, "%02x", buffer[i]);
246     }
247     return 1;
248 }
249
250 int get_line(FILE *f, char *hash, char *filename, int verbose, int *size)
251 {
252     int i, len;
253     char *ptr = filename;
254     char *spacepos = NULL;
255
256     while (!feof(f)) {
257         if (!fgets(filename, PATH_MAX, f))
258             return 0;
259         ptr = filename;
260         while (*ptr == ' ')
261             ptr++;
262
263         len = strlen(ptr);
264         while (ptr[--len] == '\n' || ptr[len] == '\r')
265             ptr[len] = 0;
266
267         if (len == 0)
268             goto nextline;
269
270         spacepos = strchr(ptr, ' ');
271         if (spacepos == NULL || strlen(spacepos + 1) == 0)
272             goto nextline;
273
274         *size = spacepos - ptr;
275
276         for (i = 0; i < *size; i++) {
277             if (ptr[i] < '0' || (ptr[i] > '9' && ptr[i] < 'A') ||
278                 (ptr[i] > 'F' && ptr[i] < 'a') || ptr[i] > 'f') {
279                 goto nextline;
280             }
281         }
282
283         if (*size > 128 || ((*size != 64) && (*size != 128))) {
284             *size = 0;
285             memset(hash, 0, MAX_HASH_TXT_BYTES + 1);
286         } else {
287             memcpy(hash, ptr, *size);
288             hash[*size] = 0;
289         }
290         memmove(filename, spacepos + 1, strlen(spacepos));
291
292         *size /= 2;
293         return 1;
294  nextline:
295         if (verbose)
296             printf("Skipping line %s\n", filename);
297     }
298     return 0;
299 }