forked from XKCP/XKCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
679 lines (614 loc) · 25.4 KB
/
main.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
The eXtended Keccak Code Package (XKCP)
https://github.com/XKCP/XKCP
Implementation by Gilles Van Assche and Ronny Van Keer, hereby denoted as "the implementer".
For more information, feedback or questions, please refer to the Keccak Team website:
https://keccak.team/
To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include "testPermutations.h"
#include "testSponge.h"
#include "testDuplex.h"
#include "testPRG.h"
#include "testSP800-185.h"
#include "testKangarooTwelve.h"
#include "testKravatte.h"
#include "testKravatteModes.h"
#include "testXoofff.h"
#include "testXoofffModes.h"
#include "testXoodyak.h"
#ifdef KeccakReference
#include "displayIntermediateValues.h"
#ifdef XKCP_has_KeccakP200
#include "KeccakP-200-SnP.h"
#include "KeccakP-200-reference.h"
void displayKeccakF200IntermediateValues()
{
unsigned char state[KeccakP200_stateSizeInBytes];
const char *fileName = "KeccakF-200-IntermediateValues.txt";
FILE *f;
f = fopen(fileName, "w");
if (f == NULL)
printf("Could not open %s\n", fileName);
else {
KeccakP200_StaticInitialize();
fprintf(f, "+++ The round constants +++\n");
fprintf(f, "\n");
KeccakP200_DisplayRoundConstants(f);
fprintf(f, "+++ The rho offsets +++\n");
fprintf(f, "\n");
KeccakP200_DisplayRhoOffsets(f);
displaySetIntermediateValueFile(f);
displaySetLevel(3);
fprintf(f, "+++ Example with the all-zero input +++\n");
fprintf(f, "\n");
KeccakP200_Initialize(state);
KeccakP200_Permute_18rounds(state);
fprintf(f, "+++ Example taking the previous output as input +++\n");
fprintf(f, "\n");
KeccakP200_Permute_18rounds(state);
fclose(f);
displaySetIntermediateValueFile(0);
}
}
#endif
#ifdef XKCP_has_KeccakP400
#include "KeccakP-400-SnP.h"
#include "KeccakP-400-reference.h"
void displayKeccakF400IntermediateValues()
{
unsigned char state[KeccakP400_stateSizeInBytes];
const char *fileName = "KeccakF-400-IntermediateValues.txt";
FILE *f;
f = fopen(fileName, "w");
if (f == NULL)
printf("Could not open %s\n", fileName);
else {
KeccakP400_StaticInitialize();
fprintf(f, "+++ The round constants +++\n");
fprintf(f, "\n");
KeccakP400_DisplayRoundConstants(f);
fprintf(f, "+++ The rho offsets +++\n");
fprintf(f, "\n");
KeccakP400_DisplayRhoOffsets(f);
displaySetIntermediateValueFile(f);
displaySetLevel(3);
fprintf(f, "+++ Example with the all-zero input +++\n");
fprintf(f, "\n");
KeccakP400_Initialize(state);
KeccakP400_Permute_20rounds(state);
fprintf(f, "+++ Example taking the previous output as input +++\n");
fprintf(f, "\n");
KeccakP400_Permute_20rounds(state);
fclose(f);
displaySetIntermediateValueFile(0);
}
}
#endif
#ifdef XKCP_has_KeccakP800
#include "KeccakP-800-SnP.h"
#include "KeccakP-800-reference.h"
void displayKeccakF800IntermediateValues()
{
unsigned char state[KeccakP800_stateSizeInBytes];
const char *fileName = "KeccakF-800-IntermediateValues.txt";
FILE *f;
f = fopen(fileName, "w");
if (f == NULL)
printf("Could not open %s\n", fileName);
else {
KeccakP800_StaticInitialize();
fprintf(f, "+++ The round constants +++\n");
fprintf(f, "\n");
KeccakP800_DisplayRoundConstants(f);
fprintf(f, "+++ The rho offsets +++\n");
fprintf(f, "\n");
KeccakP800_DisplayRhoOffsets(f);
displaySetIntermediateValueFile(f);
displaySetLevel(3);
fprintf(f, "+++ Example with the all-zero input +++\n");
fprintf(f, "\n");
KeccakP800_Initialize(state);
KeccakP800_Permute_22rounds(state);
fprintf(f, "+++ Example taking the previous output as input +++\n");
fprintf(f, "\n");
KeccakP800_Permute_22rounds(state);
fclose(f);
displaySetIntermediateValueFile(0);
}
}
#endif
#ifdef XKCP_has_KeccakP1600
#include "KeccakP-1600-SnP.h"
#include "KeccakP-1600-reference.h"
void displayKeccakF1600IntermediateValues()
{
KeccakP1600_state state;
#ifdef KeccakReference32BI
const char *fileName = "KeccakF-1600-IntermediateValues32BI.txt";
#else
const char *fileName = "KeccakF-1600-IntermediateValues.txt";
#endif
FILE *f;
f = fopen(fileName, "w");
if (f == NULL)
printf("Could not open %s\n", fileName);
else {
KeccakP1600_StaticInitialize();
fprintf(f, "+++ The round constants +++\n");
fprintf(f, "\n");
KeccakP1600_DisplayRoundConstants(f);
fprintf(f, "+++ The rho offsets +++\n");
fprintf(f, "\n");
KeccakP1600_DisplayRhoOffsets(f);
displaySetIntermediateValueFile(f);
displaySetLevel(3);
fprintf(f, "+++ Example with the all-zero input +++\n");
fprintf(f, "\n");
KeccakP1600_Initialize(&state);
KeccakP1600_Permute_24rounds(&state);
fprintf(f, "+++ Example taking the previous output as input +++\n");
fprintf(f, "\n");
KeccakP1600_Permute_24rounds(&state);
fclose(f);
displaySetIntermediateValueFile(0);
}
}
unsigned int appendSuffixToMessage(char *out, const char *in, unsigned int inputLengthInBits, unsigned char delimitedSuffix)
{
memcpy(out, in, (inputLengthInBits+7)/8);
if (delimitedSuffix == 0x00)
abort();
while(delimitedSuffix != 0x01) {
unsigned char bit = delimitedSuffix & 0x01;
out[inputLengthInBits/8] |= (bit << (inputLengthInBits%8));
inputLengthInBits++;
delimitedSuffix >>= 1;
}
return inputLengthInBits;
}
#include "KeccakSponge.h"
void displaySpongeIntermediateValuesOne(const unsigned char *message, unsigned int messageLength, unsigned char delimitedSuffix, unsigned int rate, unsigned int capacity, unsigned int outputLengthInBits)
{
KeccakWidth1600_SpongeInstance sponge;
unsigned char output[512];
unsigned char *messageWithSuffix;
unsigned int messageLengthWithSuffix;
displayBytes(1, "Input message (last byte aligned on LSB)", message, (messageLength+7)/8);
displayBits(2, "Input message (in bits)", message, messageLength, 0);
messageWithSuffix = malloc((messageLength+15)/8);
messageLengthWithSuffix = appendSuffixToMessage(messageWithSuffix, message, messageLength, delimitedSuffix);
if (delimitedSuffix != 0x01) {
unsigned char suffix[1];
suffix[0] = delimitedSuffix;
displayBytes(2, "Delimited suffix", suffix, 1);
displayBits(2, "Suffix (in bits)", suffix, messageLengthWithSuffix-messageLength, 0);
displayBits(2, "Input message with suffix appended to it (in bits)", messageWithSuffix, messageLengthWithSuffix, 0);
displayBytes(2, "Input message with suffix appended to it (last byte aligned on LSB)", messageWithSuffix, (messageLengthWithSuffix+7)/8);
}
KeccakWidth1600_SpongeInitialize(&sponge, rate, capacity);
displayStateAsBytes(1, "Initial state", (const unsigned char *)&sponge.state, 1600);
KeccakWidth1600_SpongeAbsorb(&sponge, messageWithSuffix, messageLengthWithSuffix/8);
if ((messageLengthWithSuffix % 8) != 0)
KeccakWidth1600_SpongeAbsorbLastFewBits(&sponge, messageWithSuffix[messageLengthWithSuffix/8] | (1 << (messageLengthWithSuffix % 8)));
if (outputLengthInBits <= 8*sizeof(output))
KeccakWidth1600_SpongeSqueeze(&sponge, output, (outputLengthInBits+7)/8);
else
abort();
}
void displaySpongeIntermediateValuesFew(const char *fileName, unsigned char delimitedSuffix, unsigned int rate, unsigned int capacity, unsigned int desiredOutputLengthInBits)
{
const unsigned char *message0 = (unsigned char *) "";
const unsigned char *message5 = (unsigned char *) "\x13"; /* 11001 */
const unsigned char *message30 = (unsigned char *) "\x53\x58\x7B\x19"; /* 110010100001101011011110100110 */
const unsigned char *message1600 = (unsigned char *)
"\xA3\xA3\xA3\xA3\xA3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xB9\x04\x76\xD3\x07\xCE\x80\x2E"
"\xC8\x2C\x1C\x49\xBC\x1B\xEC\x48\xC0\x67\x5E\xC2\xA6\xC6\xF3\xED"
"\x3E\x5B\x74\x1D\x13\x43\x70\x95\x70\x7C\x56\x5E\x10\xD8\xA2\x0B"
"\x8C\x20\x46\x8F\xF9\x51\x4F\xCF\x31\xB4\x24\x9C\xD8\x2D\xCE\xE5"
"\x8C\x0A\x2A\xF5\x38\xB2\x91\xA8\x7E\x33\x90\xD7\x37\x19\x1A\x07"
"\x48\x4A\x5D\x3F\x3F\xB8\xC8\xF1\x5C\xE0\x56\xE5\xE5\xF8\xFE\xBE"
"\x5E\x1F\xB5\x9D\x67\x40\x98\x0A\xA0\x6C\xA8\xA0\xC2\x0F\x57\x12"
"\xB4\xCD\xE5\xD0\x32\xE9\x2A\xB8\x9F\x0A\xE1";
FILE *f = fopen(fileName, "w");
if (f == NULL) {
printf("Could not open %s\n", fileName);
return;
}
displaySetIntermediateValueFile(f);
displaySetLevel(2);
fprintf(f, "+++ Example with a small message +++\n");
fprintf(f, "\n");
fprintf(f, "This is the empty string.\n");
fprintf(f, "\n");
displaySpongeIntermediateValuesOne(message0, 0, delimitedSuffix, rate, capacity, desiredOutputLengthInBits);
fprintf(f, "+++ Example with a small message +++\n");
fprintf(f, "\n");
fprintf(f, "This is the message of length 5 from http://csrc.nist.gov/groups/ST/toolkit/examples.html .\n");
fprintf(f, "\n");
displaySpongeIntermediateValuesOne(message5, 5, delimitedSuffix, rate, capacity, desiredOutputLengthInBits);
fprintf(f, "+++ Example with a small message +++\n");
fprintf(f, "\n");
fprintf(f, "This is the message of length 30 from http://csrc.nist.gov/groups/ST/toolkit/examples.html .\n");
fprintf(f, "\n");
displaySpongeIntermediateValuesOne(message30, 30, delimitedSuffix, rate, capacity, desiredOutputLengthInBits);
fprintf(f, "+++ Example with a larger message +++\n");
fprintf(f, "\n");
fprintf(f, "This is the message of length 1600 from http://csrc.nist.gov/groups/ST/toolkit/examples.html .\n");
fprintf(f, "\n");
displaySpongeIntermediateValuesOne(message1600, 1600, delimitedSuffix, rate, capacity, desiredOutputLengthInBits);
fprintf(f, "+++ Example with a larger message +++\n");
fprintf(f, "\n");
fprintf(f, "This is the message of length 1605 from http://csrc.nist.gov/groups/ST/toolkit/examples.html .\n");
fprintf(f, "\n");
displaySpongeIntermediateValuesOne(message1605, 1605, delimitedSuffix, rate, capacity, desiredOutputLengthInBits);
fprintf(f, "+++ Example with a larger message +++\n");
fprintf(f, "\n");
fprintf(f, "This is the message of length 1630 from http://csrc.nist.gov/groups/ST/toolkit/examples.html .\n");
fprintf(f, "\n");
displaySpongeIntermediateValuesOne(message1630, 1630, delimitedSuffix, rate, capacity, desiredOutputLengthInBits);
fprintf(f, "+++ Example with a larger message +++\n");
fprintf(f, "\n");
fprintf(f, "This is the message of length 2008 from ShortMsgKAT.txt.\n");
fprintf(f, "\n");
displaySpongeIntermediateValuesOne(message2008, 2008, delimitedSuffix, rate, capacity, desiredOutputLengthInBits);
fclose(f);
displaySetIntermediateValueFile(0);
}
void displaySpongeIntermediateValues(void)
{
displaySpongeIntermediateValuesFew("KeccakSpongeIntermediateValues_r1344c256.txt", 0x01, 1344, 256, 4096);
displaySpongeIntermediateValuesFew("KeccakSpongeIntermediateValues_SHAKE128.txt", 0x1F, 1344, 256, 4096);
displaySpongeIntermediateValuesFew("KeccakSpongeIntermediateValues_SHAKE256.txt", 0x1F, 1088, 512, 4096);
displaySpongeIntermediateValuesFew("KeccakSpongeIntermediateValues_SHA3-224.txt", 0x06, 1152, 448, 224);
displaySpongeIntermediateValuesFew("KeccakSpongeIntermediateValues_SHA3-256.txt", 0x06, 1088, 512, 256);
displaySpongeIntermediateValuesFew("KeccakSpongeIntermediateValues_SHA3-384.txt", 0x06, 832, 768, 384);
displaySpongeIntermediateValuesFew("KeccakSpongeIntermediateValues_SHA3-512.txt", 0x06, 576, 1024, 512);
}
#include "KeccakDuplex.h"
void displayDuplexIntermediateValuesOne(FILE *f, unsigned int rate, unsigned int capacity)
{
KeccakWidth1600_DuplexInstance duplex;
unsigned char input[512];
unsigned int inBitLen;
unsigned char output[512];
unsigned int outBitLen;
unsigned int i, j;
const unsigned int M = 239*251;
unsigned int x = 33;
KeccakWidth1600_DuplexInitialize(&duplex, rate, capacity);
displayStateAsBytes(1, "Initial state", (const unsigned char *)&duplex.state, 1600);
for(i=0; i<=rate+120; i+=123) {
inBitLen = i;
if (inBitLen > (rate-2)) inBitLen = rate-2;
memset(input, 0, 512);
for(j=0; j<inBitLen; j++) {
x = (x*x) % M;
if ((x % 2) != 0)
input[j/8] |= 1 << (j%8);
}
{
char text[100];
sprintf(text, "Input (%d bits)", inBitLen);
displayBytes(1, text, input, (inBitLen+7)/8);
}
outBitLen = rate;
if ((inBitLen%8) == 0)
KeccakWidth1600_Duplexing(&duplex, input, inBitLen/8, output, (outBitLen+7)/8, 0x01);
else
KeccakWidth1600_Duplexing(&duplex, input, inBitLen/8, output, (outBitLen+7)/8, input[inBitLen/8] | (1 << (inBitLen%8)));
{
char text[100];
sprintf(text, "Output (%d bits)", outBitLen);
displayBytes(1, text, output, (outBitLen+7)/8);
}
}
}
void displayDuplexIntermediateValues(void)
{
char fileName[256];
FILE *f;
unsigned int rate;
for(rate=1026; rate<=1027; rate++) {
unsigned int capacity = 1600-rate;
sprintf(fileName, "KeccakDuplexIntermediateValues_r%dc%d.txt", rate, capacity);
f = fopen(fileName, "w");
if (f == NULL)
printf("Could not open %s\n", fileName);
else {
displaySetIntermediateValueFile(f);
displaySetLevel(2);
displayDuplexIntermediateValuesOne(f, rate, capacity);
fclose(f);
displaySetIntermediateValueFile(0);
}
}
}
#endif
#endif /* defined KeccakReference */
#ifdef XKCP_has_FIPS202
#include "genKAT.h"
#include "SimpleFIPS202.h"
void testFIPS202()
{
const unsigned char *input = (const unsigned char *)"\x21\xF1\x34\xAC\x57";
const unsigned char *outputSHAKE128 = (const unsigned char *)
"\x7B\xFB\xB4\x0D\xA3\x70\x4A\x55\x82\x91\xB3\x9E\x1E\x56\xED\x9F"
"\x6F\x56\xAE\x78\x32\x70\xAB\x02\xA2\x02\x06\x0C\x91\x73\xFB\xB0"
"\xB4\x55\x75\xB3\x23\x48\xA6\xED\x2C\x92\x7A\x39\xA3\x0D\xA0\xA2"
"\xBB\xC1\x80\x74\x97\xAD\x50\xF2\x7A\x10\x77\x54\xAF\x62\x76\x2C";
const unsigned char *outputSHAKE256 = (const unsigned char *)
"\xBB\x8A\x84\x47\x51\x7B\xA9\xCA\x7F\xA3\x4E\xC9\x9A\x80\x00\x4F"
"\x22\x8A\xB2\x82\x47\x28\x41\xEB\x3D\x3A\x76\x22\x5C\x9D\xBE\x77"
"\xF7\xE4\x0A\x06\x67\x76\xD3\x2C\x74\x94\x12\x02\xF9\xF4\xAA\x43"
"\xD1\x2C\x62\x64\xAF\xA5\x96\x39\xC4\x4E\x11\xF5\xE1\x4F\x1E\x56";
const unsigned char *outputSHA3_224 = (const unsigned char *)
"\x10\xE5\x80\xA3\x21\x99\x59\x61\x69\x33\x1A\xD4\x3C\xFC\xF1\x02"
"\x64\xF8\x15\x65\x03\x70\x40\x02\x8A\x06\xB4\x58";
const unsigned char *outputSHA3_256 = (const unsigned char *)
"\x55\xBD\x92\x24\xAF\x4E\xED\x0D\x12\x11\x49\xE3\x7F\xF4\xD7\xDD"
"\x5B\xE2\x4B\xD9\xFB\xE5\x6E\x01\x71\xE8\x7D\xB7\xA6\xF4\xE0\x6D";
const unsigned char *outputSHA3_384 = (const unsigned char *)
"\xE2\x48\xD6\xFF\x34\x2D\x35\xA3\x0E\xC2\x30\xBA\x51\xCD\xB1\x61"
"\x02\x5D\x6F\x1C\x25\x1A\xCA\x6A\xE3\x53\x1F\x06\x82\xC1\x64\xA1"
"\xFC\x07\x25\xB1\xBE\xFF\x80\x8A\x20\x0C\x13\x15\x57\xA2\x28\x09";
const unsigned char *outputSHA3_512 = (const unsigned char *)
"\x58\x42\x19\xA8\x4E\x87\x96\x07\x6B\xF1\x17\x8B\x14\xB9\xD1\xE2"
"\xF9\x6A\x4B\x4E\xF1\x1F\x10\xCC\x51\x6F\xBE\x1A\x29\x63\x9D\x6B"
"\xA7\x4F\xB9\x28\x15\xF9\xE3\xC5\x19\x2E\xD4\xDC\xA2\x0A\xEA\x5B"
"\x10\x9D\x52\x23\x7C\x99\x56\x40\x1F\xD4\x4B\x22\x1F\x82\xAB\x37";
unsigned char buffer[64];
assert(SHAKE128(buffer, 64, input, 5) == 0);
assert(memcmp(buffer, outputSHAKE128, 64) == 0);
assert(SHAKE256(buffer, 64, input, 5) == 0);
assert(memcmp(buffer, outputSHAKE256, 64) == 0);
assert(SHA3_224(buffer, input, 5) == 0);
assert(memcmp(buffer, outputSHA3_224, 28) == 0);
assert(SHA3_256(buffer, input, 5) == 0);
assert(memcmp(buffer, outputSHA3_256, 32) == 0);
assert(SHA3_384(buffer, input, 5) == 0);
assert(memcmp(buffer, outputSHA3_384, 48) == 0);
assert(SHA3_512(buffer, input, 5) == 0);
assert(memcmp(buffer, outputSHA3_512, 64) == 0);
}
#endif
void printHelp()
{
printf("Usage: UnitTests command(s), where the commands can be\n");
printf(" --help or -h To display this page\n");
printf(" --all or -a All tests\n");
printf(" --SnP or -p Tests on all available permutations\n");
#if defined (XKCP_has_Sponge_Keccak) || defined(XKCP_has_Duplex_Keccak)
printf(" --Keccak or -c Tests on Keccak sponge and duplex\n");
#endif
#ifdef XKCP_has_Sponge_Keccak
printf(" --KeccakSponge Tests on Keccak sponge\n");
#endif
#ifdef XKCP_has_Duplex_Keccak
printf(" --KeccakDuplex Tests on Keccak duplex\n");
#endif
#ifdef XKCP_has_PRG_Keccak
printf(" --KeccakPRG Tests on KeccakPRG\n");
#endif
#ifdef XKCP_has_FIPS202
printf(" --FIPS202 or -f Tests on FIPS202 and ShortMsgKAT generation\n");
#endif
#ifdef XKCP_has_SP800_185
printf(" --SP800-185 Tests on SP800-185 functions\n");
#endif
#ifdef XKCP_has_KangarooTwelve
printf(" --KangarooTwelve or -K12 Tests on KangarooTwelve\n");
#endif
#ifdef XKCP_has_Kravatte
printf(" --Kravatte Tests on Kravatte\n");
#endif
#ifdef XKCP_has_Xoofff
printf(" --Xoofff Tests on Xoofff\n");
#endif
#ifdef XKCP_has_Xoodyak
printf(" --Xoodyak Tests on Xoodyak\n");
#endif
#ifdef KeccakReference
printf(" --examples or -e Generation of example files\n");
#endif
}
int process(int argc, char* argv[])
{
int i;
int help = 0;
int SnP = 0;
int KeccakSponge = 0;
int KeccakDuplex = 0;
int KeccakPRG = 0;
int FIPS202 = 0;
int KangarooTwelve = 0;
int Kravatte = 0;
int SP800_185 = 0;
int Xoofff = 0;
int Xoodyak = 0;
int examples = 0;
if (argc == 1)
help = 1;
for(i=1; i<argc; i++) {
if ((strcmp("--help", argv[i]) == 0) || (strcmp("-h", argv[i]) == 0))
help = 1;
else if ((strcmp("--all", argv[i]) == 0) || (strcmp("-a", argv[i]) == 0))
SnP = KeccakSponge = KeccakDuplex = KeccakPRG = FIPS202 = KangarooTwelve = Kravatte = SP800_185 = examples = Xoofff = Xoodyak = 1;
else if ((strcmp("--SnP", argv[i]) == 0) || (strcmp("-p", argv[i]) == 0))
SnP = 1;
#if defined (XKCP_has_Sponge_Keccak) || defined(XKCP_has_Duplex_Keccak)
else if ((strcmp("--Keccak", argv[i]) == 0) || (strcmp("-c", argv[i]) == 0))
KeccakSponge = KeccakDuplex = 1;
#endif
#ifdef XKCP_has_Sponge_Keccak
else if (strcmp("--KeccakSponge", argv[i]) == 0)
KeccakSponge = 1;
#endif
#ifdef XKCP_has_Duplex_Keccak
else if (strcmp("--KeccakDuplex", argv[i]) == 0)
KeccakDuplex = 1;
#endif
#ifdef XKCP_has_PRG_Keccak
else if (strcmp("--KeccakPRG", argv[i]) == 0)
KeccakPRG = 1;
#endif
#ifdef XKCP_has_FIPS202
else if ((strcmp("--FIPS202", argv[i]) == 0) || (strcmp("-f", argv[i]) == 0))
FIPS202 = 1;
#endif
else if ((strcmp("--KangarooTwelve", argv[i]) == 0) || (strcmp("-K12", argv[i]) == 0))
KangarooTwelve = 1;
#ifdef XKCP_has_Kravatte
else if (strcmp("--Kravatte", argv[i]) == 0)
Kravatte = 1;
#endif
#ifdef XKCP_has_SP800_185
else if (strcmp("--SP800-185", argv[i]) == 0)
SP800_185 = 1;
#endif
#ifdef XKCP_has_Xoofff
else if (strcmp("--Xoofff", argv[i]) == 0)
Xoofff = 1;
#endif
#ifdef XKCP_has_Xoodyak
else if (strcmp("--Xoodyak", argv[i]) == 0)
Xoodyak = 1;
#endif
#ifdef KeccakReference
else if ((strcmp("--examples", argv[i]) == 0) || (strcmp("-e", argv[i]) == 0))
examples = 1;
#endif
else {
printf("Unrecognized command '%s'\n", argv[i]);
return -1;
}
}
if (help) {
printHelp();
return 0;
}
if (SnP) {
testPermutations();
}
#ifdef XKCP_has_Sponge_Keccak
if (KeccakSponge) {
testSponge();
}
#endif
#ifdef XKCP_has_Duplex_Keccak
if (KeccakDuplex) {
testDuplex();
}
#endif
#ifdef XKCP_has_KangarooTwelve
if (KangarooTwelve) {
testKangarooTwelve();
}
#endif
#ifdef XKCP_has_SP800_185
if (SP800_185) {
testSP800_185();
}
#endif
#ifdef XKCP_has_PRG_Keccak
if (KeccakPRG) {
testPRG();
}
#endif
#ifdef XKCP_has_FIPS202
if (FIPS202) {
testFIPS202();
genKAT_main();
}
#endif
#ifdef XKCP_has_Kravatte
if (Kravatte) {
testKravatte();
testKravatteModes();
}
#endif
#ifdef KeccakReference
if (examples) {
#ifdef XKCP_has_KeccakP200
displayKeccakF200IntermediateValues();
#endif
#ifdef XKCP_has_KeccakP400
displayKeccakF400IntermediateValues();
#endif
#ifdef XKCP_has_KeccakP800
displayKeccakF800IntermediateValues();
#endif
#ifdef XKCP_has_KeccakP1600
displayKeccakF1600IntermediateValues();
#endif
displaySpongeIntermediateValues();
displayDuplexIntermediateValues();
}
#endif
#ifdef XKCP_has_Xoofff
if (Xoofff) {
testXoofff();
testXoofffModes();
}
#endif
#ifdef XKCP_has_Xoodyak
if (Xoodyak) {
testXoodyak();
}
#endif
return 0;
}
int main(int argc, char* argv[])
{
return process(argc, argv);
}