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