-
Notifications
You must be signed in to change notification settings - Fork 25
/
gost3411-2012.c
420 lines (344 loc) · 10.3 KB
/
gost3411-2012.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* Copyright (c) 2013, Alexey Degtyarev <alexey@renatasystems.org>.
* All rights reserved.
*
* GOST 34.11-2012 hash function with 512/256 bits digest.
*
* $Id$
*/
#include <err.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include "gost3411-2012-core.h"
/* For benchmarking */
#include <sys/resource.h>
#include <sys/time.h>
#include <sys/types.h>
#define READ_BUFFER_SIZE 65536
#define TEST_BLOCK_LEN 8192
#ifdef __GOST3411_HAS_SSE2__
#define TEST_BLOCK_COUNT 50000
#else
#define TEST_BLOCK_COUNT 10000
#endif
#define DEFAULT_DIGEST_SIZE 512
#define ALGNAME "GOST R 34.11-2012"
#define VERSION "0.13"
GOST34112012Context *CTX;
unsigned char digest[64];
unsigned char hexdigest[129];
unsigned int digest_size = DEFAULT_DIGEST_SIZE;
const union uint512_u GOSTTestInput = {
#ifndef __GOST3411_BIG_ENDIAN__
{
0x3736353433323130ULL,
0x3534333231303938ULL,
0x3332313039383736ULL,
0x3130393837363534ULL,
0x3938373635343332ULL,
0x3736353433323130ULL,
0x3534333231303938ULL,
0x0032313039383736ULL
}
#else
{
0x3031323334353637ULL,
0x3839303132333435ULL,
0x3637383930313233ULL,
0x3435363738393031ULL,
0x3233343536373839ULL,
0x3031323334353637ULL,
0x3839303132333435ULL,
0x3637383930313200ULL
}
#endif
};
static void
usage_short(void)
{
fprintf(stderr, "Usage: [-25bhvqrte] [-s string] [files ...]\n");
exit(255);
}
static void
usage(void)
{
fprintf(stdout, "The program outputs GOST R 34.11-2012 hash digest in hexadecimal format.\n");
fprintf(stdout, "Each file listed on the command line is processed and hash is printed\n");
fprintf(stdout, "for each one. Stdin is read as input when executed without arguments.\n");
fprintf(stdout, "\n");
fprintf(stdout, "Usage: gost3411-2012 [-25bhvqrte] [-s string] [files ...]\n");
fprintf(stdout, "\n");
fprintf(stdout, "Options:\n");
fprintf(stdout, " -2 Output 256-bit digest.\n");
fprintf(stdout, " -5 Output 512-bit digest (default).\n");
fprintf(stdout, " -t Testing mode to produce hash of example\n");
fprintf(stdout, " messages defined in standard.\n");
fprintf(stdout, " -b Benchmark mode (to see how fast or slow\n");
fprintf(stdout, " this implementation).\n");
fprintf(stdout, " -s string Print a digest of the given string.\n");
fprintf(stdout, " -r Reverses the format of the output.\n");
fprintf(stdout, " This helps with visual diffs.\n");
fprintf(stdout, " -q Quiet mode - only the digest is printed\n");
fprintf(stdout, " out.\n");
fprintf(stdout, " -e Switch endianness when printing out\n");
fprintf(stdout, " resulting hash. Default: least significant\n");
fprintf(stdout, " first. With this options set all bytes in\n");
fprintf(stdout, " resulting hash are printed in reverse\n");
fprintf(stdout, " order, more precisely, most significant\n");
fprintf(stdout, " first.\n");
fprintf(stdout, " -v Print version info and exit.\n");
fprintf(stdout, " -h Print this help and exit.\n");
fprintf(stdout, "\n");
fprintf(stdout, "Report bugs to <alexey@renatasystems.org>\n");
exit(0);
}
static void
version(void)
{
fprintf(stdout, "gost3411-2012 %s\n", VERSION);
fprintf(stdout, "\n");
fprintf(stdout, "Written by Alexey Degtyarev.\n");
exit(0);
}
static void *
memalloc(const size_t size)
{
void *p;
/* Ensure p is on a 64-bit boundary. */
if (posix_memalign(&p, (size_t) 64, size))
err(EX_OSERR, NULL);
return p;
}
static void
reverse_order(unsigned char *in, size_t len)
{
unsigned char c;
unsigned int i, j;
for (i = 0, j = (unsigned int) len - 1; i < j; i++, j--)
{
c = in[i];
in[i] = in[j];
in[j] = c;
}
}
static void
convert_to_hex(unsigned char *in, unsigned char *out, size_t len,
const unsigned int eflag)
{
unsigned int i;
char ch[3];
if (len > 64) len = 64;
memset(out, 0, 129);
/* eflag is set when little-endian output requested */
if (eflag) reverse_order(in, len);
for (i = 0; i < len; i++)
{
sprintf(ch, "%02x", (unsigned char) in[i]);
memcpy(&out[i * 2], ch, 2);
}
}
static void
onfile(FILE *file)
{
unsigned char *buffer;
size_t len;
GOST34112012Init(CTX, digest_size);
buffer = memalloc((size_t) READ_BUFFER_SIZE);
while ((len = fread(buffer, (size_t) 1, (size_t) READ_BUFFER_SIZE, file)))
GOST34112012Update(CTX, buffer, len);
if (ferror(file))
err(EX_IOERR, NULL);
free(buffer);
GOST34112012Final(CTX, &digest[0]);
}
static void
onstring(const unsigned char *string)
{
unsigned char *buf __attribute__((aligned(16)));
size_t size;
GOST34112012Init(CTX, digest_size);
size = strnlen((const char *) string, (size_t) 4096);
buf = memalloc(size);
memcpy(buf, string, size);
GOST34112012Update(CTX, buf, size);
GOST34112012Final(CTX, &digest[0]);
}
static void
testing(const unsigned int eflag)
{
printf("M1: 012345678901234567890123456789012345678901234567890123456789012\n");
GOST34112012Init(CTX, 512);
memcpy(CTX->buffer, &GOSTTestInput, sizeof uint512_u);
CTX->bufsize = 63;
GOST34112012Final(CTX, &digest[0]);
convert_to_hex(&digest[0], &hexdigest[0], 64, eflag);
printf("%s 512 bit digest (M1): 0x%s\n", ALGNAME, hexdigest);
GOST34112012Cleanup(CTX);
GOST34112012Init(CTX, 256);
memcpy(CTX->buffer, &GOSTTestInput, sizeof uint512_u);
CTX->bufsize = 63;
GOST34112012Final(CTX, &digest[0]);
convert_to_hex(&digest[0], &hexdigest[0], 32, eflag);
printf("%s 256 bit digest (M1): 0x%s\n", ALGNAME, hexdigest);
GOST34112012Cleanup(CTX);
exit(EXIT_SUCCESS);
}
static void
benchmark(const unsigned int eflag)
{
struct rusage before, after;
struct timeval total;
float seconds;
unsigned char block[TEST_BLOCK_LEN] __attribute__((aligned(16)));
unsigned int i;
printf("%s timing benchmark.\n", ALGNAME);
printf("Digesting %d %d-byte blocks with 512 bits digest...\n",
TEST_BLOCK_COUNT, TEST_BLOCK_LEN);
fflush(stdout);
for (i = 0; i < TEST_BLOCK_LEN; i++)
block[i] = (unsigned char) (i & 0xff);
getrusage(RUSAGE_SELF, &before);
GOST34112012Init(CTX, 512);
for (i = 0; i < TEST_BLOCK_COUNT; i++)
GOST34112012Update(CTX, block, (size_t) TEST_BLOCK_LEN);
GOST34112012Final(CTX, digest);
getrusage(RUSAGE_SELF, &after);
timersub(&after.ru_utime, &before.ru_utime, &total);
seconds = (float) total.tv_sec + (float) total.tv_usec / 1000000;
convert_to_hex(digest, hexdigest, 64, eflag);
printf("Digest = %s", hexdigest);
printf("\nTime = %f seconds\n", seconds);
printf("Speed = %.2f bytes/second\n",
(float) TEST_BLOCK_LEN * (float) TEST_BLOCK_COUNT / seconds);
exit(EXIT_SUCCESS);
}
static void
shutdown(void)
{
if (CTX != NULL)
GOST34112012Cleanup(CTX);
}
#ifdef SUPERCOP
#include "crypto_hash.h"
int
crypto_hash(unsigned char *out, const unsigned char *in,
unsigned long long inlen)
{
CTX = memalloc(sizeof(GOST34112012Context));
GOST34112012Init(CTX, 512);
GOST34112012Update(CTX, in, (size_t) inlen);
GOST34112012Final(CTX, out);
return 0;
}
#else
int
main(int argc, char *argv[])
{
int ch;
unsigned char uflag, qflag, rflag, eflag;
unsigned char excode;
FILE *f;
excode = EXIT_SUCCESS;
atexit(shutdown);
CTX = memalloc(sizeof(GOST34112012Context));
qflag = 0;
rflag = 0;
uflag = 0;
eflag = 0;
while ((ch = getopt(argc, argv, "25bhvqrs:te")) != -1)
{
switch (ch)
{
case 'b':
benchmark(eflag);
break;
case '2':
digest_size = 256;
break;
case '5':
digest_size = 512;
break;
case 'q':
qflag = 1;
break;
case 's':
onstring((unsigned char *) optarg);
if (digest_size == 256)
convert_to_hex(digest, hexdigest, 32, eflag);
else
convert_to_hex(digest, hexdigest, 64, eflag);
if (qflag)
printf("%s\n", hexdigest);
else if (rflag)
printf("%s \"%s\"\n", hexdigest, optarg);
else
printf("%s (\"%s\") = %s\n", ALGNAME, optarg,
hexdigest);
uflag = 1;
break;
case 'r':
rflag = 1;
break;
case 't':
testing(eflag);
break;
case 'e':
eflag = 1;
break;
case 'h':
usage();
break;
case 'v':
version();
break;
default:
usage_short();
}
}
argc -= optind;
argv += optind;
if (*argv)
{
do
{
if ((f = fopen(*argv, "rb")) == NULL)
{
warn("%s", *argv);
excode = EX_OSFILE;
continue;
}
onfile(f);
fclose(f);
uflag = 1;
if (digest_size == 256)
convert_to_hex(digest, hexdigest, 32, eflag);
else
convert_to_hex(digest, hexdigest, 64, eflag);
if (qflag)
printf("%s\n", hexdigest);
else if (rflag)
printf("%s \"%s\"\n", hexdigest, *argv);
else
printf("%s (%s) = %s\n", ALGNAME, *argv, hexdigest);
}
while (*++argv);
}
else if (!uflag)
{
onfile(stdin);
if (digest_size == 256)
convert_to_hex(digest, hexdigest, 32, eflag);
else
convert_to_hex(digest, hexdigest, 64, eflag);
printf("%s\n", hexdigest);
uflag = 1;
}
if (!uflag)
usage();
return excode;
}
#endif