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