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