~zooko/cryptopp/trunk

« back to all changes in this revision

Viewing changes to validat1.cpp

  • Committer: noloader
  • Date: 2015-06-29 13:37:03 UTC
  • Revision ID: svn-v4:57ff6487-cd31-0410-9ec3-f628ee90f5f0:trunk/c5:558
Cleared warning on operator precedence

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
#include "pch.h"
4
4
 
 
5
#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
5
6
#include "files.h"
 
7
#include "misc.h"
6
8
#include "hex.h"
7
9
#include "base32.h"
8
10
#include "base64.h"
15
17
#include "arc4.h"
16
18
#include "rc5.h"
17
19
#include "blowfish.h"
18
 
#include "wake.h"
19
20
#include "3way.h"
20
21
#include "safer.h"
21
22
#include "gost.h"
25
26
#include "seal.h"
26
27
#include "rc6.h"
27
28
#include "mars.h"
 
29
#include "aes.h"
28
30
#include "rijndael.h"
29
31
#include "twofish.h"
30
32
#include "serpent.h"
33
35
#include "camellia.h"
34
36
#include "osrng.h"
35
37
#include "zdeflate.h"
 
38
#include "cpu.h"
36
39
 
37
 
#include <stdlib.h>
38
40
#include <time.h>
39
41
#include <memory>
40
42
#include <iostream>
55
57
        pass=ValidateMD2() && pass;
56
58
        pass=ValidateMD5() && pass;
57
59
        pass=ValidateSHA() && pass;
58
 
        pass=ValidateSHA2() && pass;
59
 
        pass=ValidateHAVAL() && pass;
 
60
        pass=RunTestDataFile("TestVectors/sha3.txt") && pass;
60
61
        pass=ValidateTiger() && pass;
61
62
        pass=ValidateRIPEMD() && pass;
62
63
        pass=ValidatePanama() && pass;
63
64
        pass=ValidateWhirlpool() && pass;
64
65
 
65
 
        pass=ValidateMD5MAC() && pass;
66
66
        pass=ValidateHMAC() && pass;
67
 
        pass=ValidateXMACC() && pass;
68
67
        pass=ValidateTTMAC() && pass;
69
68
 
70
69
        pass=ValidatePBKDF() && pass;
92
91
        pass=ValidateSHACAL2() && pass;
93
92
        pass=ValidateCamellia() && pass;
94
93
        pass=ValidateSalsa() && pass;
 
94
        pass=ValidateSosemanuk() && pass;
 
95
        pass=ValidateVMAC() && pass;
 
96
        pass=ValidateCCM() && pass;
 
97
        pass=ValidateGCM() && pass;
 
98
        pass=ValidateCMAC() && pass;
 
99
        pass=RunTestDataFile("TestVectors/eax.txt") && pass;
 
100
        pass=RunTestDataFile("TestVectors/seed.txt") && pass;
95
101
 
96
102
        pass=ValidateBBS() && pass;
97
103
        pass=ValidateDH() && pass;
127
133
 
128
134
        cout << "\nTesting Settings...\n\n";
129
135
 
130
 
        if (*(word32 *)"\x01\x02\x03\x04" == 0x04030201L)
 
136
        word32 w;
 
137
        memcpy_s(&w, sizeof(w), "\x01\x02\x03\x04", 4);
 
138
 
 
139
        if (w == 0x04030201L)
131
140
        {
132
141
#ifdef IS_LITTLE_ENDIAN
133
142
                cout << "passed:  ";
137
146
#endif
138
147
                cout << "Your machine is little endian.\n";
139
148
        }
140
 
        else if (*(word32 *)"\x01\x02\x03\x04" == 0x01020304L)
 
149
        else if (w == 0x01020304L)
141
150
        {
142
151
#ifndef IS_LITTLE_ENDIAN
143
152
                cout << "passed:  ";
153
162
                pass = false;
154
163
        }
155
164
 
 
165
#ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
 
166
        byte testvals[10] = {1,2,2,3,3,3,3,2,2,1};
 
167
        if (*(word32 *)(testvals+3) == 0x03030303 && *(word64 *)(testvals+1) == W64LIT(0x0202030303030202))
 
168
                cout << "passed:  Your machine allows unaligned data access.\n";
 
169
        else
 
170
        {
 
171
                cout << "FAILED:  Unaligned data access gave incorrect results.\n";
 
172
                pass = false;
 
173
        }
 
174
#else
 
175
        cout << "passed:  CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is not defined. Will restrict to aligned data access.\n";
 
176
#endif
 
177
 
156
178
        if (sizeof(byte) == 1)
157
179
                cout << "passed:  ";
158
180
        else
180
202
        }
181
203
        cout << "sizeof(word32) == " << sizeof(word32) << endl;
182
204
 
183
 
#ifdef WORD64_AVAILABLE
184
205
        if (sizeof(word64) == 8)
185
206
                cout << "passed:  ";
186
207
        else
189
210
                pass = false;
190
211
        }
191
212
        cout << "sizeof(word64) == " << sizeof(word64) << endl;
192
 
#elif CRYPTOPP_NATIVE_DWORD_AVAILABLE
193
 
        if (sizeof(dword) >= 8)
 
213
 
 
214
#ifdef CRYPTOPP_WORD128_AVAILABLE
 
215
        if (sizeof(word128) == 16)
 
216
                cout << "passed:  ";
 
217
        else
194
218
        {
195
 
                cout << "FAILED:  sizeof(dword) >= 8, but WORD64_AVAILABLE not defined" << endl;
 
219
                cout << "FAILED:  ";
196
220
                pass = false;
197
221
        }
198
 
        else
199
 
                cout << "passed:  word64 not available" << endl;
 
222
        cout << "sizeof(word128) == " << sizeof(word128) << endl;
200
223
#endif
201
224
 
202
225
        if (sizeof(word) == 2*sizeof(hword)
216
239
#endif
217
240
        cout << endl;
218
241
 
 
242
#ifdef CRYPTOPP_CPUID_AVAILABLE
 
243
        bool hasMMX = HasMMX();
 
244
        bool hasISSE = HasISSE();
 
245
        bool hasSSE2 = HasSSE2();
 
246
        bool hasSSSE3 = HasSSSE3();
 
247
        bool isP4 = IsP4();
 
248
        int cacheLineSize = GetCacheLineSize();
 
249
 
 
250
        if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize)))
 
251
        {
 
252
                cout << "FAILED:  ";
 
253
                pass = false;
 
254
        }
 
255
        else
 
256
                cout << "passed:  ";
 
257
 
 
258
        cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasAESNI == " << HasAESNI() << ", hasCLMUL == " << HasCLMUL() << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize;
 
259
        cout << ", AESNI_INTRINSICS == " << CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE << endl;
 
260
#endif
 
261
 
219
262
        if (!pass)
220
263
        {
221
264
                cout << "Some critical setting in config.h is in error.  Please fix it and recompile." << endl;
260
303
                        cout << "passed:";
261
304
                cout << "  it took " << long(t1) << " seconds to generate " << total << " bytes" << endl;
262
305
 
 
306
#if 0   // disable this part. it's causing an unpredictable pause during the validation testing
263
307
                if (t1 < 2)
264
308
                {
265
309
                        // that was fast, are we really blocking?
280
324
                                total += 1;
281
325
                                length += 1;
282
326
                        }
283
 
                        // turn off this test because it fails on several systems, including Darwin
284
 
                        // they don't block, or gather entropy too fast?
285
 
                        if (false) // (length > 1024)
 
327
                        if (length > 1024)
286
328
                        {
287
329
                                cout << "FAILED:";
288
330
                                pass = false;
291
333
                                cout << "passed:";
292
334
                        cout << "  it generated " << length << " bytes in " << long(time(NULL) - t) << " seconds" << endl;
293
335
                }
 
336
#endif
294
337
 
295
338
                test.AttachedTransformation()->MessageEnd();
296
339
 
393
436
 
394
437
                apbt transE = cg.NewEncryption(key);
395
438
                transE->ProcessBlock(plain, out);
396
 
                fail = memcmp(out, cipher, cg.BlockSize()) != 0;
 
439
                fail = !VerifyBufsEqual(out, cipher, cg.BlockSize());
397
440
 
398
441
                apbt transD = cg.NewDecryption(key);
399
442
                transD->ProcessBlock(out, outplain);
400
 
                fail=fail || memcmp(outplain, plain, cg.BlockSize());
 
443
                fail=fail || !VerifyBufsEqual(outplain, plain, cg.BlockSize());
401
444
 
402
445
                pass = pass && !fail;
403
446
 
421
464
        {
422
465
                if (counter >= outputLen || validOutput[counter] != inByte)
423
466
                {
 
467
                        std::cerr << "incorrect output " << counter << ", " << (word16)validOutput[counter] << ", " << (word16)inByte << "\n";
424
468
                        fail = true;
425
469
                        assert(false);
426
470
                }
470
514
{
471
515
        cout << "\nDES validation suite running...\n\n";
472
516
 
473
 
        FileSource valdata("descert.dat", true, new HexDecoder);
 
517
        FileSource valdata("TestData/descert.dat", true, new HexDecoder);
474
518
        bool pass = BlockTransformationTest(FixedRoundsCipherFactory<DESEncryption, DESDecryption>(), valdata);
475
519
 
476
520
        cout << "\nTesting EDE2, EDE3, and XEX3 variants...\n\n";
477
521
 
478
 
        FileSource valdata1("3desval.dat", true, new HexDecoder);
 
522
        FileSource valdata1("TestData/3desval.dat", true, new HexDecoder);
479
523
        pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE2_Encryption, DES_EDE2_Decryption>(), valdata1, 1) && pass;
480
524
        pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE3_Encryption, DES_EDE3_Decryption>(), valdata1, 1) && pass;
481
525
        pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_XEX3_Encryption, DES_XEX3_Decryption>(), valdata1, 1) && pass;
485
529
 
486
530
bool TestModeIV(SymmetricCipher &e, SymmetricCipher &d)
487
531
{
488
 
        SecByteBlock lastIV;
 
532
        SecByteBlock lastIV, iv(e.IVSize());
489
533
        StreamTransformationFilter filter(e, new StreamTransformationFilter(d));
490
534
        byte plaintext[20480];
491
535
 
492
536
        for (unsigned int i=1; i<sizeof(plaintext); i*=2)
493
537
        {
494
 
                SecByteBlock iv(e.IVSize());
495
 
                e.GetNextIV(iv);
496
 
 
 
538
                e.GetNextIV(GlobalRNG(), iv);
497
539
                if (iv == lastIV)
498
540
                        return false;
499
541
                else
662
704
                modeE.SetStolenIV(stolenIV);
663
705
                fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
664
706
                        plain, 3, encrypted, sizeof(encrypted));
665
 
                fail = memcmp(stolenIV, decryptionIV, 8) != 0 || fail;
 
707
                fail = !VerifyBufsEqual(stolenIV, decryptionIV, 8) || fail;
666
708
                pass = pass && !fail;
667
709
                cout << (fail ? "FAILED   " : "passed   ") << "CBC encryption with ciphertext and IV stealing" << endl;
668
710
                
783
825
                pass = pass && !fail;
784
826
                cout << (fail ? "FAILED   " : "passed   ") << "DMAC" << endl;
785
827
        }
 
828
        {
 
829
                CTR_Mode<AES>::Encryption modeE(plain, 16, plain);
 
830
                CTR_Mode<AES>::Decryption modeD(plain, 16, plain);
 
831
                fail = !TestModeIV(modeE, modeD);
 
832
                pass = pass && !fail;
 
833
                cout << (fail ? "FAILED   " : "passed   ") << "AES CTR Mode" << endl;
 
834
        }
 
835
        {
 
836
                OFB_Mode<AES>::Encryption modeE(plain, 16, plain);
 
837
                OFB_Mode<AES>::Decryption modeD(plain, 16, plain);
 
838
                fail = !TestModeIV(modeE, modeD);
 
839
                pass = pass && !fail;
 
840
                cout << (fail ? "FAILED   " : "passed   ") << "AES OFB Mode" << endl;
 
841
        }
 
842
        {
 
843
                CFB_Mode<AES>::Encryption modeE(plain, 16, plain);
 
844
                CFB_Mode<AES>::Decryption modeD(plain, 16, plain);
 
845
                fail = !TestModeIV(modeE, modeD);
 
846
                pass = pass && !fail;
 
847
                cout << (fail ? "FAILED   " : "passed   ") << "AES CFB Mode" << endl;
 
848
        }
 
849
        {
 
850
                CBC_Mode<AES>::Encryption modeE(plain, 16, plain);
 
851
                CBC_Mode<AES>::Decryption modeD(plain, 16, plain);
 
852
                fail = !TestModeIV(modeE, modeD);
 
853
                pass = pass && !fail;
 
854
                cout << (fail ? "FAILED   " : "passed   ") << "AES CBC Mode" << endl;
 
855
        }
786
856
 
787
857
        return pass;
788
858
}
791
861
{
792
862
        cout << "\nIDEA validation suite running...\n\n";
793
863
 
794
 
        FileSource valdata("ideaval.dat", true, new HexDecoder);
 
864
        FileSource valdata("TestData/ideaval.dat", true, new HexDecoder);
795
865
        return BlockTransformationTest(FixedRoundsCipherFactory<IDEAEncryption, IDEADecryption>(), valdata);
796
866
}
797
867
 
799
869
{
800
870
        cout << "\nSAFER validation suite running...\n\n";
801
871
 
802
 
        FileSource valdata("saferval.dat", true, new HexDecoder);
 
872
        FileSource valdata("TestData/saferval.dat", true, new HexDecoder);
803
873
        bool pass = true;
804
874
        pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(8,6), valdata, 4) && pass;
805
875
        pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(16,12), valdata, 4) && pass;
812
882
{
813
883
        cout << "\nRC2 validation suite running...\n\n";
814
884
 
815
 
        FileSource valdata("rc2val.dat", true, new HexDecoder);
 
885
        FileSource valdata("TestData/rc2val.dat", true, new HexDecoder);
816
886
        HexEncoder output(new FileSink(cout));
817
887
        SecByteBlock plain(RC2Encryption::BLOCKSIZE), cipher(RC2Encryption::BLOCKSIZE), out(RC2Encryption::BLOCKSIZE), outplain(RC2Encryption::BLOCKSIZE);
818
888
        SecByteBlock key(128);
830
900
 
831
901
                apbt transE(new RC2Encryption(key, keyLen, effectiveLen));
832
902
                transE->ProcessBlock(plain, out);
833
 
                fail = memcmp(out, cipher, RC2Encryption::BLOCKSIZE) != 0;
 
903
                fail = !VerifyBufsEqual(out, cipher, RC2Encryption::BLOCKSIZE);
834
904
 
835
905
                apbt transD(new RC2Decryption(key, keyLen, effectiveLen));
836
906
                transD->ProcessBlock(out, outplain);
837
 
                fail=fail || memcmp(outplain, plain, RC2Encryption::BLOCKSIZE);
 
907
                fail=fail || !VerifyBufsEqual(outplain, plain, RC2Encryption::BLOCKSIZE);
838
908
 
839
909
                pass = pass && !fail;
840
910
 
976
1046
        0xc0};
977
1047
 
978
1048
        // VC60 workaround: auto_ptr lacks reset()
979
 
        member_ptr<ARC4> arc4;
 
1049
        member_ptr<Weak::ARC4> arc4;
980
1050
        bool pass=true, fail;
981
 
        int i;
 
1051
        size_t i;
982
1052
 
983
1053
        cout << "\nARC4 validation suite running...\n\n";
984
1054
 
985
 
        arc4.reset(new ARC4(Key0, sizeof(Key0)));
 
1055
        arc4.reset(new Weak::ARC4(Key0, sizeof(Key0)));
986
1056
        arc4->ProcessString(Input0, sizeof(Input0));
987
 
        fail = memcmp(Input0, Output0, sizeof(Input0)) != 0;
 
1057
        fail = !VerifyBufsEqual(Input0, Output0, sizeof(Input0));
988
1058
        cout << (fail ? "FAILED" : "passed") << "    Test 0" << endl;
989
1059
        pass = pass && !fail;
990
1060
 
991
 
        arc4.reset(new ARC4(Key1, sizeof(Key1)));
 
1061
        arc4.reset(new Weak::ARC4(Key1, sizeof(Key1)));
992
1062
        arc4->ProcessString(Key1, Input1, sizeof(Key1));
993
 
        fail = memcmp(Output1, Key1, sizeof(Key1)) != 0;
 
1063
        fail = !VerifyBufsEqual(Output1, Key1, sizeof(Key1));
994
1064
        cout << (fail ? "FAILED" : "passed") << "    Test 1" << endl;
995
1065
        pass = pass && !fail;
996
1066
 
997
 
        arc4.reset(new ARC4(Key2, sizeof(Key2)));
 
1067
        arc4.reset(new Weak::ARC4(Key2, sizeof(Key2)));
998
1068
        for (i=0, fail=false; i<sizeof(Input2); i++)
999
1069
                if (arc4->ProcessByte(Input2[i]) != Output2[i])
1000
1070
                        fail = true;
1001
1071
        cout << (fail ? "FAILED" : "passed") << "    Test 2" << endl;
1002
1072
        pass = pass && !fail;
1003
1073
 
1004
 
        arc4.reset(new ARC4(Key3, sizeof(Key3)));
 
1074
        arc4.reset(new Weak::ARC4(Key3, sizeof(Key3)));
1005
1075
        for (i=0, fail=false; i<sizeof(Input3); i++)
1006
1076
                if (arc4->ProcessByte(Input3[i]) != Output3[i])
1007
1077
                        fail = true;
1008
1078
        cout << (fail ? "FAILED" : "passed") << "    Test 3" << endl;
1009
1079
        pass = pass && !fail;
1010
1080
 
1011
 
        arc4.reset(new ARC4(Key4, sizeof(Key4)));
 
1081
        arc4.reset(new Weak::ARC4(Key4, sizeof(Key4)));
1012
1082
        for (i=0, fail=false; i<sizeof(Input4); i++)
1013
1083
                if (arc4->ProcessByte(Input4[i]) != Output4[i])
1014
1084
                        fail = true;
1022
1092
{
1023
1093
        cout << "\nRC5 validation suite running...\n\n";
1024
1094
 
1025
 
        FileSource valdata("rc5val.dat", true, new HexDecoder);
 
1095
        FileSource valdata("TestData/rc5val.dat", true, new HexDecoder);
1026
1096
        return BlockTransformationTest(VariableRoundsCipherFactory<RC5Encryption, RC5Decryption>(16, 12), valdata);
1027
1097
}
1028
1098
 
1030
1100
{
1031
1101
        cout << "\nRC6 validation suite running...\n\n";
1032
1102
 
1033
 
        FileSource valdata("rc6val.dat", true, new HexDecoder);
 
1103
        FileSource valdata("TestData/rc6val.dat", true, new HexDecoder);
1034
1104
        bool pass = true;
1035
1105
        pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(16), valdata, 2) && pass;
1036
1106
        pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(24), valdata, 2) && pass;
1042
1112
{
1043
1113
        cout << "\nMARS validation suite running...\n\n";
1044
1114
 
1045
 
        FileSource valdata("marsval.dat", true, new HexDecoder);
 
1115
        FileSource valdata("TestData/marsval.dat", true, new HexDecoder);
1046
1116
        bool pass = true;
1047
1117
        pass = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(16), valdata, 4) && pass;
1048
1118
        pass = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(24), valdata, 3) && pass;
1052
1122
 
1053
1123
bool ValidateRijndael()
1054
1124
{
1055
 
        cout << "\nRijndael validation suite running...\n\n";
 
1125
        cout << "\nRijndael (AES) validation suite running...\n\n";
1056
1126
 
1057
 
        FileSource valdata("rijndael.dat", true, new HexDecoder);
 
1127
        FileSource valdata("TestData/rijndael.dat", true, new HexDecoder);
1058
1128
        bool pass = true;
1059
1129
        pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(16), valdata, 4) && pass;
1060
1130
        pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(24), valdata, 3) && pass;
1061
1131
        pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(32), valdata, 2) && pass;
 
1132
        pass = RunTestDataFile("TestVectors/aes.txt") && pass;
1062
1133
        return pass;
1063
1134
}
1064
1135
 
1066
1137
{
1067
1138
        cout << "\nTwofish validation suite running...\n\n";
1068
1139
 
1069
 
        FileSource valdata("twofishv.dat", true, new HexDecoder);
 
1140
        FileSource valdata("TestData/twofishv.dat", true, new HexDecoder);
1070
1141
        bool pass = true;
1071
1142
        pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(16), valdata, 4) && pass;
1072
1143
        pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(24), valdata, 3) && pass;
1078
1149
{
1079
1150
        cout << "\nSerpent validation suite running...\n\n";
1080
1151
 
1081
 
        FileSource valdata("serpentv.dat", true, new HexDecoder);
 
1152
        FileSource valdata("TestData/serpentv.dat", true, new HexDecoder);
1082
1153
        bool pass = true;
1083
 
        pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(16), valdata, 4) && pass;
1084
 
        pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(24), valdata, 3) && pass;
1085
 
        pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(32), valdata, 2) && pass;
 
1154
        pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(16), valdata, 5) && pass;
 
1155
        pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(24), valdata, 4) && pass;
 
1156
        pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(32), valdata, 3) && pass;
1086
1157
        return pass;
1087
1158
}
1088
1159
 
1091
1162
        cout << "\nBlowfish validation suite running...\n\n";
1092
1163
 
1093
1164
        HexEncoder output(new FileSink(cout));
1094
 
        char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"};
 
1165
        const char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"};
1095
1166
        byte *plain[]={(byte *)"BLOWFISH", (byte *)"\xfe\xdc\xba\x98\x76\x54\x32\x10"};
1096
1167
        byte *cipher[]={(byte *)"\x32\x4e\xd0\xfe\xf4\x13\xa2\x03", (byte *)"\xcc\x91\x73\x2b\x80\x22\xf6\x84"};
1097
1168
        byte out[8], outplain[8];
1101
1172
        {
1102
1173
                ECB_Mode<Blowfish>::Encryption enc((byte *)key[i], strlen(key[i]));
1103
1174
                enc.ProcessData(out, plain[i], 8);
1104
 
                fail = memcmp(out, cipher[i], 8) != 0;
 
1175
                fail = !VerifyBufsEqual(out, cipher[i], 8);
1105
1176
 
1106
1177
                ECB_Mode<Blowfish>::Decryption dec((byte *)key[i], strlen(key[i]));
1107
1178
                dec.ProcessData(outplain, cipher[i], 8);
1108
 
                fail = fail || memcmp(outplain, plain[i], 8);
 
1179
                fail = fail || !VerifyBufsEqual(outplain, plain[i], 8);
1109
1180
                pass = pass && !fail;
1110
1181
 
1111
1182
                cout << (fail ? "FAILED    " : "passed    ");
1124
1195
{
1125
1196
        cout << "\n3-WAY validation suite running...\n\n";
1126
1197
 
1127
 
        FileSource valdata("3wayval.dat", true, new HexDecoder);
 
1198
        FileSource valdata("TestData/3wayval.dat", true, new HexDecoder);
1128
1199
        return BlockTransformationTest(FixedRoundsCipherFactory<ThreeWayEncryption, ThreeWayDecryption>(), valdata);
1129
1200
}
1130
1201
 
1132
1203
{
1133
1204
        cout << "\nGOST validation suite running...\n\n";
1134
1205
 
1135
 
        FileSource valdata("gostval.dat", true, new HexDecoder);
 
1206
        FileSource valdata("TestData/gostval.dat", true, new HexDecoder);
1136
1207
        return BlockTransformationTest(FixedRoundsCipherFactory<GOSTEncryption, GOSTDecryption>(), valdata);
1137
1208
}
1138
1209
 
1140
1211
{
1141
1212
        cout << "\nSHARK validation suite running...\n\n";
1142
1213
 
1143
 
#ifdef WORD64_AVAILABLE
1144
 
        FileSource valdata("sharkval.dat", true, new HexDecoder);
 
1214
        FileSource valdata("TestData/sharkval.dat", true, new HexDecoder);
1145
1215
        return BlockTransformationTest(FixedRoundsCipherFactory<SHARKEncryption, SHARKDecryption>(), valdata);
1146
 
#else
1147
 
        cout << "word64 not available, skipping SHARK validation." << endl;
1148
 
        return true;
1149
 
#endif
1150
1216
}
1151
1217
 
1152
1218
bool ValidateCAST()
1155
1221
 
1156
1222
        cout << "\nCAST-128 validation suite running...\n\n";
1157
1223
 
1158
 
        FileSource val128("cast128v.dat", true, new HexDecoder);
 
1224
        FileSource val128("TestData/cast128v.dat", true, new HexDecoder);
1159
1225
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(16), val128, 1) && pass;
1160
1226
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(10), val128, 1) && pass;
1161
1227
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(5), val128, 1) && pass;
1162
1228
 
1163
1229
        cout << "\nCAST-256 validation suite running...\n\n";
1164
1230
 
1165
 
        FileSource val256("cast256v.dat", true, new HexDecoder);
 
1231
        FileSource val256("TestData/cast256v.dat", true, new HexDecoder);
1166
1232
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(16), val256, 1) && pass;
1167
1233
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(24), val256, 1) && pass;
1168
1234
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(32), val256, 1) && pass;
1174
1240
{
1175
1241
        cout << "\nSquare validation suite running...\n\n";
1176
1242
 
1177
 
        FileSource valdata("squareva.dat", true, new HexDecoder);
 
1243
        FileSource valdata("TestData/squareva.dat", true, new HexDecoder);
1178
1244
        return BlockTransformationTest(FixedRoundsCipherFactory<SquareEncryption, SquareDecryption>(), valdata);
1179
1245
}
1180
1246
 
1182
1248
{
1183
1249
        cout << "\nSKIPJACK validation suite running...\n\n";
1184
1250
 
1185
 
        FileSource valdata("skipjack.dat", true, new HexDecoder);
 
1251
        FileSource valdata("TestData/skipjack.dat", true, new HexDecoder);
1186
1252
        return BlockTransformationTest(FixedRoundsCipherFactory<SKIPJACKEncryption, SKIPJACKDecryption>(), valdata);
1187
1253
}
1188
1254
 
1208
1274
        seal.Seek(1);
1209
1275
        output[1] = seal.ProcessByte(output[1]);
1210
1276
        seal.ProcessString(output+2, size-2);
1211
 
        pass = pass && memcmp(output+1, input+1, size-1) == 0;
 
1277
        pass = pass && VerifyBufsEqual(output+1, input+1, size-1);
1212
1278
 
1213
1279
        cout << (pass ? "passed" : "FAILED") << endl;
1214
1280
        return pass;
1286
1352
        cout << "\nSHACAL-2 validation suite running...\n\n";
1287
1353
 
1288
1354
        bool pass = true;
1289
 
        FileSource valdata("shacal2v.dat", true, new HexDecoder);
 
1355
        FileSource valdata("TestData/shacal2v.dat", true, new HexDecoder);
1290
1356
        pass = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(16), valdata, 4) && pass;
1291
1357
        pass = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(64), valdata, 10) && pass;
1292
1358
        return pass;
1296
1362
{
1297
1363
        cout << "\nCamellia validation suite running...\n\n";
1298
1364
 
1299
 
#ifdef WORD64_AVAILABLE
1300
1365
        bool pass = true;
1301
 
        FileSource valdata("camellia.dat", true, new HexDecoder);
 
1366
        FileSource valdata("TestData/camellia.dat", true, new HexDecoder);
1302
1367
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(16), valdata, 15) && pass;
1303
1368
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(24), valdata, 15) && pass;
1304
1369
        pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(32), valdata, 15) && pass;
1305
1370
        return pass;
1306
 
#else
1307
 
        cout << "word64 not available, skipping Camellia validation." << endl;
1308
 
        return true;
1309
 
#endif
1310
1371
}
1311
1372
 
1312
1373
bool ValidateSalsa()
1315
1376
 
1316
1377
        return RunTestDataFile("TestVectors/salsa.txt");
1317
1378
}
 
1379
 
 
1380
bool ValidateSosemanuk()
 
1381
{
 
1382
        cout << "\nSosemanuk validation suite running...\n";
 
1383
        return RunTestDataFile("TestVectors/sosemanuk.txt");
 
1384
}
 
1385
 
 
1386
bool ValidateVMAC()
 
1387
{
 
1388
        cout << "\nVMAC validation suite running...\n";
 
1389
        return RunTestDataFile("TestVectors/vmac.txt");
 
1390
}
 
1391
 
 
1392
bool ValidateCCM()
 
1393
{
 
1394
        cout << "\nAES/CCM validation suite running...\n";
 
1395
        return RunTestDataFile("TestVectors/ccm.txt");
 
1396
}
 
1397
 
 
1398
bool ValidateGCM()
 
1399
{
 
1400
        cout << "\nAES/GCM validation suite running...\n";
 
1401
        cout << "\n2K tables:";
 
1402
        bool pass = RunTestDataFile("TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)2048));
 
1403
        cout << "\n64K tables:";
 
1404
        return RunTestDataFile("TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)64*1024)) && pass;
 
1405
}
 
1406
 
 
1407
bool ValidateCMAC()
 
1408
{
 
1409
        cout << "\nCMAC validation suite running...\n";
 
1410
        return RunTestDataFile("TestVectors/cmac.txt");
 
1411
}