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