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;
65
pass=ValidateMD5MAC() && pass;
66
66
pass=ValidateHMAC() && pass;
67
pass=ValidateXMACC() && pass;
68
67
pass=ValidateTTMAC() && pass;
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;
96
102
pass=ValidateBBS() && pass;
97
103
pass=ValidateDH() && pass;
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";
171
cout << "FAILED: Unaligned data access gave incorrect results.\n";
175
cout << "passed: CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is not defined. Will restrict to aligned data access.\n";
156
178
if (sizeof(byte) == 1)
157
179
cout << "passed: ";
191
212
cout << "sizeof(word64) == " << sizeof(word64) << endl;
192
#elif CRYPTOPP_NATIVE_DWORD_AVAILABLE
193
if (sizeof(dword) >= 8)
214
#ifdef CRYPTOPP_WORD128_AVAILABLE
215
if (sizeof(word128) == 16)
195
cout << "FAILED: sizeof(dword) >= 8, but WORD64_AVAILABLE not defined" << endl;
199
cout << "passed: word64 not available" << endl;
222
cout << "sizeof(word128) == " << sizeof(word128) << endl;
202
225
if (sizeof(word) == 2*sizeof(hword)
242
#ifdef CRYPTOPP_CPUID_AVAILABLE
243
bool hasMMX = HasMMX();
244
bool hasISSE = HasISSE();
245
bool hasSSE2 = HasSSE2();
246
bool hasSSSE3 = HasSSSE3();
248
int cacheLineSize = GetCacheLineSize();
250
if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize)))
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;
221
264
cout << "Some critical setting in config.h is in error. Please fix it and recompile." << endl;
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());
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());
402
445
pass = pass && !fail;
471
515
cout << "\nDES validation suite running...\n\n";
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);
476
520
cout << "\nTesting EDE2, EDE3, and XEX3 variants...\n\n";
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;
486
530
bool TestModeIV(SymmetricCipher &e, SymmetricCipher &d)
532
SecByteBlock lastIV, iv(e.IVSize());
489
533
StreamTransformationFilter filter(e, new StreamTransformationFilter(d));
490
534
byte plaintext[20480];
492
536
for (unsigned int i=1; i<sizeof(plaintext); i*=2)
494
SecByteBlock iv(e.IVSize());
538
e.GetNextIV(GlobalRNG(), iv);
497
539
if (iv == lastIV)
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;
783
825
pass = pass && !fail;
784
826
cout << (fail ? "FAILED " : "passed ") << "DMAC" << endl;
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;
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;
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;
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;
792
862
cout << "\nIDEA validation suite running...\n\n";
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);
800
870
cout << "\nSAFER validation suite running...\n\n";
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;
813
883
cout << "\nRC2 validation suite running...\n\n";
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);
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);
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);
839
909
pass = pass && !fail;
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;
983
1053
cout << "\nARC4 validation suite running...\n\n";
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;
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;
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])
1001
1071
cout << (fail ? "FAILED" : "passed") << " Test 2" << endl;
1002
1072
pass = pass && !fail;
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])
1008
1078
cout << (fail ? "FAILED" : "passed") << " Test 3" << endl;
1009
1079
pass = pass && !fail;
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])
1023
1093
cout << "\nRC5 validation suite running...\n\n";
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);
1031
1101
cout << "\nRC6 validation suite running...\n\n";
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;
1043
1113
cout << "\nMARS validation suite running...\n\n";
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;
1053
1123
bool ValidateRijndael()
1055
cout << "\nRijndael validation suite running...\n\n";
1125
cout << "\nRijndael (AES) validation suite running...\n\n";
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;
1067
1138
cout << "\nTwofish validation suite running...\n\n";
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;
1079
1150
cout << "\nSerpent validation suite running...\n\n";
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;
1091
1162
cout << "\nBlowfish validation suite running...\n\n";
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];
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);
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;
1111
1182
cout << (fail ? "FAILED " : "passed ");
1125
1196
cout << "\n3-WAY validation suite running...\n\n";
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);
1133
1204
cout << "\nGOST validation suite running...\n\n";
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);
1141
1212
cout << "\nSHARK validation suite running...\n\n";
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);
1147
cout << "word64 not available, skipping SHARK validation." << endl;
1152
1218
bool ValidateCAST()
1156
1222
cout << "\nCAST-128 validation suite running...\n\n";
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;
1163
1229
cout << "\nCAST-256 validation suite running...\n\n";
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;
1175
1241
cout << "\nSquare validation suite running...\n\n";
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);
1183
1249
cout << "\nSKIPJACK validation suite running...\n\n";
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);
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);
1213
1279
cout << (pass ? "passed" : "FAILED") << endl;
1286
1352
cout << "\nSHACAL-2 validation suite running...\n\n";
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;
1297
1363
cout << "\nCamellia validation suite running...\n\n";
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;
1307
cout << "word64 not available, skipping Camellia validation." << endl;
1312
1373
bool ValidateSalsa()
1316
1377
return RunTestDataFile("TestVectors/salsa.txt");
1380
bool ValidateSosemanuk()
1382
cout << "\nSosemanuk validation suite running...\n";
1383
return RunTestDataFile("TestVectors/sosemanuk.txt");
1388
cout << "\nVMAC validation suite running...\n";
1389
return RunTestDataFile("TestVectors/vmac.txt");
1394
cout << "\nAES/CCM validation suite running...\n";
1395
return RunTestDataFile("TestVectors/ccm.txt");
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;
1409
cout << "\nCMAC validation suite running...\n";
1410
return RunTestDataFile("TestVectors/cmac.txt");