]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gostsum.c
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[openssl-gost/engine.git] / gostsum.c
1 /**********************************************************************
2  *                        gostsum.c                                   *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *        Almost drop-in replacement for md5sum and sha1sum           *
7  *          which computes GOST R 34.11-94 hashsum instead            *
8  *                                                                    *
9  **********************************************************************/
10 #include <stdio.h>
11 #include <stdlib.h>
12 #ifdef _MSC_VER
13 #include "getopt.h"
14 # ifndef PATH_MAX
15 #  define PATH_MAX _MAX_PATH
16 # endif
17 #include <BaseTsd.h>
18 typedef SSIZE_T ssize_t;
19 #else
20 #include <unistd.h>
21 #endif
22 #include <limits.h>
23 #include <fcntl.h>
24 #ifdef _WIN32
25 # include <io.h>
26 #endif
27 #include <string.h>
28 #include "gosthash.h"
29 #define BUF_SIZE 262144
30 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode);
31 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum);
32 int get_line(FILE *f, char *hash, char *filename);
33 void help()
34 {
35     fprintf(stderr, "gostsum [-bvt] [-c [file]]| [files]\n"
36             "\t-c check message digests (default is generate)\n"
37             "\t-v verbose, print file names when checking\n"
38             "\t-b read files in binary mode\n"
39             "\t-t use test GOST paramset (default is CryptoPro paramset)\n"
40             "The input for -c should be the list of message digests and file names\n"
41             "that is printed on stdout by this program when it generates digests.\n");
42     exit(3);
43 }
44
45 #ifndef O_BINARY
46 # define O_BINARY 0
47 #endif
48
49 int main(int argc, char **argv)
50 {
51     int c, i;
52     int verbose = 0;
53     int errors = 0;
54     int open_mode = O_RDONLY;
55     gost_subst_block *b = &GostR3411_94_CryptoProParamSet;
56     FILE *check_file = NULL;
57     gost_hash_ctx ctx;
58
59     while ((c = getopt(argc, argv, "bc::tv")) != -1) {
60         switch (c) {
61         case 'v':
62             verbose = 1;
63             break;
64         case 't':
65             b = &GostR3411_94_TestParamSet;
66             break;
67         case 'b':
68             open_mode |= O_BINARY;
69             break;
70         case 'c':
71             if (optarg) {
72                 check_file = fopen(optarg, "r");
73                 if (!check_file) {
74                     perror(optarg);
75                     exit(2);
76                 }
77             } else {
78                 check_file = stdin;
79             }
80             break;
81         default:
82             fprintf(stderr, "invalid option %c", optopt);
83             help();
84         }
85     }
86     init_gost_hash_ctx(&ctx, b);
87     if (check_file) {
88         char inhash[65], calcsum[65], filename[PATH_MAX];
89         int failcount = 0, count = 0;
90         errors = 0;
91         if (check_file == stdin && optind < argc) {
92             check_file = fopen(argv[optind], "r");
93             if (!check_file) {
94                 perror(argv[optind]);
95                 exit(2);
96             }
97         }
98         while (get_line(check_file, inhash, filename)) {
99             count++;
100             if (!hash_file(&ctx, filename, calcsum, open_mode)) {
101                 errors++;
102                 continue;
103             }
104             if (strncmp(calcsum, inhash, 65) == 0) {
105                 if (verbose) {
106                     fprintf(stderr, "%s\tOK\n", filename);
107                 }
108             } else {
109                 if (verbose) {
110                     fprintf(stderr, "%s\tFAILED\n", filename);
111                 } else {
112                     fprintf(stderr,
113                             "%s: GOST hash sum check failed for '%s'\n",
114                             argv[0], filename);
115                 }
116                 failcount++;
117             }
118         }
119         if (errors) {
120             fprintf(stderr,
121                     "%s: WARNING %d of %d file(s) cannot be processed\n",
122                     argv[0], errors, count);
123
124         }
125         if (verbose && failcount) {
126             fprintf(stderr,
127                     "%s: %d of %d file(f) failed GOST hash sum check\n",
128                     argv[0], failcount, count);
129         }
130         exit((failcount || errors) ? 1 : 0);
131     }
132     if (optind == argc) {
133         char sum[65];
134 #ifdef _WIN32
135         if (open_mode & O_BINARY) {
136             _setmode(fileno(stdin), O_BINARY);
137         }
138 #endif
139         if (!hash_stream(&ctx, fileno(stdin), sum)) {
140             perror("stdin");
141             exit(1);
142         }
143         printf("%s -\n", sum);
144         exit(0);
145     }
146     for (i = optind; i < argc; i++) {
147         char sum[65];
148         if (!hash_file(&ctx, argv[i], sum, open_mode)) {
149             errors++;
150         } else {
151             printf("%s %s\n", sum, argv[i]);
152         }
153     }
154     exit(errors ? 1 : 0);
155 }
156
157 int hash_file(gost_hash_ctx * ctx, char *filename, char *sum, int mode)
158 {
159     int fd;
160     if ((fd = open(filename, mode)) < 0) {
161         perror(filename);
162         return 0;
163     }
164     if (!hash_stream(ctx, fd, sum)) {
165         perror(filename);
166         close(fd);
167         return 0;
168     }
169     close(fd);
170     return 1;
171 }
172
173 int hash_stream(gost_hash_ctx * ctx, int fd, char *sum)
174 {
175     unsigned char buffer[BUF_SIZE];
176     ssize_t bytes;
177     int i;
178     start_hash(ctx);
179     while ((bytes = read(fd, buffer, BUF_SIZE)) > 0) {
180         hash_block(ctx, buffer, bytes);
181     }
182     if (bytes < 0) {
183         return 0;
184     }
185     finish_hash(ctx, buffer);
186     for (i = 0; i < 32; i++) {
187         sprintf(sum + 2 * i, "%02x", buffer[31 - i]);
188     }
189     return 1;
190 }
191
192 int get_line(FILE *f, char *hash, char *filename)
193 {
194     int i;
195     if (fread(hash, 1, 64, f) < 64)
196         return 0;
197     hash[64] = 0;
198     for (i = 0; i < 64; i++) {
199         if (hash[i] < '0' || (hash[i] > '9' && hash[i] < 'A')
200             || (hash[i] > 'F' && hash[i] < 'a') || hash[i] > 'f') {
201             fprintf(stderr, "Not a hash value '%s'\n", hash);
202             return 0;
203         }
204     }
205     if (fgetc(f) != ' ') {
206         fprintf(stderr, "Malformed input line\n");
207         return 0;
208     }
209     i = strlen(fgets(filename, PATH_MAX, f));
210     while (filename[--i] == '\n' || filename[i] == '\r')
211         filename[i] = 0;
212     return 1;
213 }