]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost12sum.c
Merge branch 'gost89-cbc'
[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], filename[PATH_MAX];
115         int failcount = 0, count = 0;;
116         if (check_file == stdin && optind < argc) {
117             check_file = fopen(argv[optind], "r");
118             if (!check_file) {
119                 perror(argv[optind]);
120                 exit(2);
121             }
122         }
123         while (get_line(check_file, inhash, filename, verbose)) {
124             count++;
125             if (!hash_file(&ctx, filename, calcsum, open_mode)) {
126                 errors++;
127                                 continue;
128             }
129             if (!strncmp(calcsum, inhash, hashsize/4+1)) {
130                 if (verbose) {
131                     fprintf(stderr, "%s\tOK\n", filename);
132                 }
133             } else {
134                 if (verbose) {
135                     fprintf(stderr, "%s\tFAILED\n", filename);
136                 } else {
137                     fprintf(stderr,
138                             "%s: GOST hash sum check failed for '%s'\n",
139                             argv[0], filename);
140                 }
141                 failcount++;
142             }
143         }
144         if (errors) {
145             fprintf(stderr,
146                     "%s: WARNING %d of %d file(s) cannot be processed\n",
147                     argv[0], errors, count);
148
149         }
150         if (failcount) {
151             fprintf(stderr,
152                     "%s: WARNING %d of %d file(s) failed GOST hash sum check\n",
153                     argv[0], failcount, count - errors);
154         }
155         exit((failcount || errors) ? 1 : 0);
156     } else if (filenames_from_stdin) {
157         char sum[65];
158         char filename[PATH_MAX + 1], *end;
159         while (!feof(stdin)) {
160             if (!fgets(filename, PATH_MAX, stdin))
161                 break;
162             for (end = filename; *end; end++) ;
163             end--;
164             for (; *end == '\n' || *end == '\r'; end--)
165                 *end = 0;
166             if (!hash_file(&ctx, filename, sum, open_mode)) {
167                 errors++;
168             } else {
169                 printf("%s %s\n", sum, filename);
170             }
171         }
172     } else if (optind == argc) {
173         char sum[65];
174 #ifdef _WIN32
175                 if (open_mode & O_BINARY) {
176                         _setmode(fileno(stdin), O_BINARY);
177                 }
178 #endif
179         if (!hash_stream(&ctx, fileno(stdin), sum)) {
180             perror("stdin");
181             exit(1);
182         }
183         printf("%s -\n", sum);
184         exit(0);
185     } else {
186         for (i = optind; i < argc; i++) {
187             char sum[65];
188             if (!hash_file(&ctx, argv[i], sum, open_mode)) {
189                 errors++;
190             } else {
191                 printf("%s %s\n", sum, argv[i]);
192             }
193         }
194     }
195     exit(errors ? 1 : 0);
196 }
197
198 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
199 {
200     int fd;
201     if ((fd = open(filename, mode)) < 0) {
202         perror(filename);
203         return 0;
204     }
205     if (!hash_stream(ctx, fd, sum)) {
206         perror(filename);
207         return 0;
208     }
209     close(fd);
210     return 1;
211 }
212
213 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
214 {
215     unsigned char buffer[BUF_SIZE];
216         unsigned char reverted_buffer[BUF_SIZE];
217     ssize_t bytes;
218     int i,j,k;
219     start_hash12(ctx);
220     while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
221         hash12_block(ctx, reverted_buffer, bytes);
222     }
223     if (bytes < 0) {
224         return 0;
225     }
226     finish_hash12(ctx, buffer);
227     for (i = 0; i < (hashsize / 8); i++) {
228         sprintf(sum + 2 * i, "%02x", buffer[i]);
229     }
230     return 1;
231 }
232
233 int get_line(FILE *f, char *hash, char *filename, int verbose)
234 {
235     int i, len;
236         int hashstrlen = hashsize/4;
237     while (!feof(f)) {
238         if (!fgets(filename, PATH_MAX, f))
239             return 0;
240         len = strlen(filename);
241         if (len < hashstrlen+2) {
242             goto nextline;
243         }
244         if (filename[hashstrlen] != ' ') {
245             goto nextline;
246         }
247         for (i = 0; i < hashstrlen; i++) {
248             if (filename[i] < '0' || (filename[i] > '9' && filename[i] < 'A')
249                 || (filename[i] > 'F' && filename[i] < 'a')
250                 || filename[i] > 'f') {
251                 goto nextline;
252             }
253         }
254         memcpy(hash, filename, hashstrlen);
255         hash[hashstrlen] = 0;
256         while (filename[--len] == '\n' || filename[len] == '\r')
257             filename[len] = 0;
258         memmove(filename, filename + hashstrlen+1, len - hashstrlen +1);
259         return 1;
260     nextline:
261         if (verbose)
262             printf(filename);
263     }
264     return 0;
265 }