00001
00002
00003 #include "pch.h"
00004
00005 #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
00006 #include "files.h"
00007 #include "hex.h"
00008 #include "base32.h"
00009 #include "base64.h"
00010 #include "modes.h"
00011 #include "cbcmac.h"
00012 #include "dmac.h"
00013 #include "idea.h"
00014 #include "des.h"
00015 #include "rc2.h"
00016 #include "arc4.h"
00017 #include "rc5.h"
00018 #include "blowfish.h"
00019 #include "wake.h"
00020 #include "3way.h"
00021 #include "safer.h"
00022 #include "gost.h"
00023 #include "shark.h"
00024 #include "cast.h"
00025 #include "square.h"
00026 #include "seal.h"
00027 #include "rc6.h"
00028 #include "rijndael.h"
00029 #include "twofish.h"
00030 #include "serpent.h"
00031 #include "skipjack.h"
00032 #include "shacal2.h"
00033 #include "camellia.h"
00034 #include "osrng.h"
00035 #include "zdeflate.h"
00036 #include "cpu.h"
00037
00038 #include <stdlib.h>
00039 #include <time.h>
00040 #include <memory>
00041 #include <iostream>
00042 #include <iomanip>
00043
00044 #include "validate.h"
00045
00046 USING_NAMESPACE(CryptoPP)
00047 USING_NAMESPACE(std)
00048
00049 bool ValidateAll(bool thorough)
00050 {
00051 bool pass=TestSettings();
00052 pass=TestOS_RNG() && pass;
00053
00054 pass=ValidateCRC32() && pass;
00055 pass=ValidateAdler32() && pass;
00056 pass=ValidateMD2() && pass;
00057 pass=ValidateMD5() && pass;
00058 pass=ValidateSHA() && pass;
00059 pass=ValidateSHA2() && pass;
00060 pass=ValidateTiger() && pass;
00061 pass=ValidateRIPEMD() && pass;
00062 pass=ValidatePanama() && pass;
00063 pass=ValidateWhirlpool() && pass;
00064
00065 pass=ValidateHMAC() && pass;
00066 pass=ValidateTTMAC() && pass;
00067
00068 pass=ValidatePBKDF() && pass;
00069
00070 pass=ValidateDES() && pass;
00071 pass=ValidateCipherModes() && pass;
00072 pass=ValidateIDEA() && pass;
00073 pass=ValidateSAFER() && pass;
00074 pass=ValidateRC2() && pass;
00075 pass=ValidateARC4() && pass;
00076 pass=ValidateRC5() && pass;
00077 pass=ValidateBlowfish() && pass;
00078 pass=ValidateThreeWay() && pass;
00079 pass=ValidateGOST() && pass;
00080 pass=ValidateSHARK() && pass;
00081 pass=ValidateCAST() && pass;
00082 pass=ValidateSquare() && pass;
00083 pass=ValidateSKIPJACK() && pass;
00084 pass=ValidateSEAL() && pass;
00085 pass=ValidateRC6() && pass;
00086 pass=ValidateRijndael() && pass;
00087 pass=ValidateTwofish() && pass;
00088 pass=ValidateSerpent() && pass;
00089 pass=ValidateSHACAL2() && pass;
00090 pass=ValidateCamellia() && pass;
00091 pass=ValidateSalsa() && pass;
00092 pass=ValidateSosemanuk() && pass;
00093 pass=ValidateVMAC() && pass;
00094 pass=ValidateCCM() && pass;
00095 pass=ValidateGCM() && pass;
00096 pass=ValidateCMAC() && pass;
00097 pass=RunTestDataFile("TestVectors/eax.txt") && pass;
00098 pass=RunTestDataFile("TestVectors/seed.txt") && pass;
00099
00100 pass=ValidateBBS() && pass;
00101 pass=ValidateDH() && pass;
00102 pass=ValidateMQV() && pass;
00103 pass=ValidateRSA() && pass;
00104 pass=ValidateElGamal() && pass;
00105 pass=ValidateDLIES() && pass;
00106 pass=ValidateNR() && pass;
00107 pass=ValidateDSA(thorough) && pass;
00108 pass=ValidateLUC() && pass;
00109 pass=ValidateLUC_DH() && pass;
00110 pass=ValidateLUC_DL() && pass;
00111 pass=ValidateXTR_DH() && pass;
00112 pass=ValidateRabin() && pass;
00113 pass=ValidateRW() && pass;
00114
00115 pass=ValidateECP() && pass;
00116 pass=ValidateEC2N() && pass;
00117 pass=ValidateECDSA() && pass;
00118 pass=ValidateESIGN() && pass;
00119
00120 if (pass)
00121 cout << "\nAll tests passed!\n";
00122 else
00123 cout << "\nOops! Not all tests passed.\n";
00124
00125 return pass;
00126 }
00127
00128 bool TestSettings()
00129 {
00130 bool pass = true;
00131
00132 cout << "\nTesting Settings...\n\n";
00133
00134 if (*(word32 *)"\x01\x02\x03\x04" == 0x04030201L)
00135 {
00136 #ifdef IS_LITTLE_ENDIAN
00137 cout << "passed: ";
00138 #else
00139 cout << "FAILED: ";
00140 pass = false;
00141 #endif
00142 cout << "Your machine is little endian.\n";
00143 }
00144 else if (*(word32 *)"\x01\x02\x03\x04" == 0x01020304L)
00145 {
00146 #ifndef IS_LITTLE_ENDIAN
00147 cout << "passed: ";
00148 #else
00149 cout << "FAILED: ";
00150 pass = false;
00151 #endif
00152 cout << "Your machine is big endian.\n";
00153 }
00154 else
00155 {
00156 cout << "FAILED: Your machine is neither big endian nor little endian.\n";
00157 pass = false;
00158 }
00159
00160 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
00161 byte testvals[10] = {1,2,2,3,3,3,3,2,2,1};
00162 if (*(word32 *)(testvals+3) == 0x03030303 && *(word64 *)(testvals+1) == W64LIT(0x0202030303030202))
00163 cout << "passed: Your machine allows unaligned data access.\n";
00164 else
00165 {
00166 cout << "FAILED: Unaligned data access gave incorrect results.\n";
00167 pass = false;
00168 }
00169 #else
00170 cout << "passed: CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is not defined. Will restrict to aligned data access.\n";
00171 #endif
00172
00173 if (sizeof(byte) == 1)
00174 cout << "passed: ";
00175 else
00176 {
00177 cout << "FAILED: ";
00178 pass = false;
00179 }
00180 cout << "sizeof(byte) == " << sizeof(byte) << endl;
00181
00182 if (sizeof(word16) == 2)
00183 cout << "passed: ";
00184 else
00185 {
00186 cout << "FAILED: ";
00187 pass = false;
00188 }
00189 cout << "sizeof(word16) == " << sizeof(word16) << endl;
00190
00191 if (sizeof(word32) == 4)
00192 cout << "passed: ";
00193 else
00194 {
00195 cout << "FAILED: ";
00196 pass = false;
00197 }
00198 cout << "sizeof(word32) == " << sizeof(word32) << endl;
00199
00200 if (sizeof(word64) == 8)
00201 cout << "passed: ";
00202 else
00203 {
00204 cout << "FAILED: ";
00205 pass = false;
00206 }
00207 cout << "sizeof(word64) == " << sizeof(word64) << endl;
00208
00209 #ifdef CRYPTOPP_WORD128_AVAILABLE
00210 if (sizeof(word128) == 16)
00211 cout << "passed: ";
00212 else
00213 {
00214 cout << "FAILED: ";
00215 pass = false;
00216 }
00217 cout << "sizeof(word128) == " << sizeof(word128) << endl;
00218 #endif
00219
00220 if (sizeof(word) == 2*sizeof(hword)
00221 #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE
00222 && sizeof(dword) == 2*sizeof(word)
00223 #endif
00224 )
00225 cout << "passed: ";
00226 else
00227 {
00228 cout << "FAILED: ";
00229 pass = false;
00230 }
00231 cout << "sizeof(hword) == " << sizeof(hword) << ", sizeof(word) == " << sizeof(word);
00232 #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE
00233 cout << ", sizeof(dword) == " << sizeof(dword);
00234 #endif
00235 cout << endl;
00236
00237 bool hasMMX = HasMMX();
00238 bool hasISSE = HasISSE();
00239 bool hasSSE2 = HasSSE2();
00240 bool hasSSSE3 = HasSSSE3();
00241 bool isP4 = IsP4();
00242 int cacheLineSize = GetCacheLineSize();
00243
00244 if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize)))
00245 {
00246 cout << "FAILED: ";
00247 pass = false;
00248 }
00249 else
00250 cout << "passed: ";
00251
00252 cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize;
00253
00254 if (!pass)
00255 {
00256 cout << "Some critical setting in config.h is in error. Please fix it and recompile." << endl;
00257 abort();
00258 }
00259 return pass;
00260 }
00261
00262 bool TestOS_RNG()
00263 {
00264 bool pass = true;
00265
00266 member_ptr<RandomNumberGenerator> rng;
00267 #ifdef BLOCKING_RNG_AVAILABLE
00268 try {rng.reset(new BlockingRng);}
00269 catch (OS_RNG_Err &) {}
00270 #endif
00271
00272 if (rng.get())
00273 {
00274 cout << "\nTesting operating system provided blocking random number generator...\n\n";
00275
00276 ArraySink *sink;
00277 RandomNumberSource test(*rng, UINT_MAX, false, new Deflator(sink=new ArraySink(NULL,0)));
00278 unsigned long total=0, length=0;
00279 time_t t = time(NULL), t1 = 0;
00280
00281
00282 while (total < 16 && (t1 < 10 || total*8 > (unsigned long)t1))
00283 {
00284 test.Pump(1);
00285 total += 1;
00286 t1 = time(NULL) - t;
00287 }
00288
00289 if (total < 16)
00290 {
00291 cout << "FAILED:";
00292 pass = false;
00293 }
00294 else
00295 cout << "passed:";
00296 cout << " it took " << long(t1) << " seconds to generate " << total << " bytes" << endl;
00297
00298 #if 0 // disable this part. it's causing an unpredictable pause during the validation testing
00299 if (t1 < 2)
00300 {
00301
00302
00303 t = time(NULL);
00304 while (time(NULL) - t < 2)
00305 {
00306 test.Pump(1);
00307 total += 1;
00308 }
00309
00310
00311
00312 t = time(NULL);
00313 while (time(NULL) - t < 2)
00314 {
00315 test.Pump(1);
00316 total += 1;
00317 length += 1;
00318 }
00319 if (length > 1024)
00320 {
00321 cout << "FAILED:";
00322 pass = false;
00323 }
00324 else
00325 cout << "passed:";
00326 cout << " it generated " << length << " bytes in " << long(time(NULL) - t) << " seconds" << endl;
00327 }
00328 #endif
00329
00330 test.AttachedTransformation()->MessageEnd();
00331
00332 if (sink->TotalPutLength() < total)
00333 {
00334 cout << "FAILED:";
00335 pass = false;
00336 }
00337 else
00338 cout << "passed:";
00339 cout << " " << total << " generated bytes compressed to " << (size_t)sink->TotalPutLength() << " bytes by DEFLATE" << endl;
00340 }
00341 else
00342 cout << "\nNo operating system provided blocking random number generator, skipping test." << endl;
00343
00344 rng.reset(NULL);
00345 #ifdef NONBLOCKING_RNG_AVAILABLE
00346 try {rng.reset(new NonblockingRng);}
00347 catch (OS_RNG_Err &) {}
00348 #endif
00349
00350 if (rng.get())
00351 {
00352 cout << "\nTesting operating system provided nonblocking random number generator...\n\n";
00353
00354 ArraySink *sink;
00355 RandomNumberSource test(*rng, 100000, true, new Deflator(sink=new ArraySink(NULL, 0)));
00356
00357 if (sink->TotalPutLength() < 100000)
00358 {
00359 cout << "FAILED:";
00360 pass = false;
00361 }
00362 else
00363 cout << "passed:";
00364 cout << " 100000 generated bytes compressed to " << (size_t)sink->TotalPutLength() << " bytes by DEFLATE" << endl;
00365 }
00366 else
00367 cout << "\nNo operating system provided nonblocking random number generator, skipping test." << endl;
00368
00369 return pass;
00370 }
00371
00372
00373 typedef auto_ptr<BlockTransformation> apbt;
00374
00375 class CipherFactory
00376 {
00377 public:
00378 virtual unsigned int BlockSize() const =0;
00379 virtual unsigned int KeyLength() const =0;
00380
00381 virtual apbt NewEncryption(const byte *key) const =0;
00382 virtual apbt NewDecryption(const byte *key) const =0;
00383 };
00384
00385 template <class E, class D> class FixedRoundsCipherFactory : public CipherFactory
00386 {
00387 public:
00388 FixedRoundsCipherFactory(unsigned int keylen=0) : m_keylen(keylen?keylen:E::DEFAULT_KEYLENGTH) {}
00389 unsigned int BlockSize() const {return E::BLOCKSIZE;}
00390 unsigned int KeyLength() const {return m_keylen;}
00391
00392 apbt NewEncryption(const byte *key) const
00393 {return apbt(new E(key, m_keylen));}
00394 apbt NewDecryption(const byte *key) const
00395 {return apbt(new D(key, m_keylen));}
00396
00397 unsigned int m_keylen;
00398 };
00399
00400 template <class E, class D> class VariableRoundsCipherFactory : public CipherFactory
00401 {
00402 public:
00403 VariableRoundsCipherFactory(unsigned int keylen=0, unsigned int rounds=0)
00404 : m_keylen(keylen ? keylen : E::DEFAULT_KEYLENGTH), m_rounds(rounds ? rounds : E::DEFAULT_ROUNDS) {}
00405 unsigned int BlockSize() const {return E::BLOCKSIZE;}
00406 unsigned int KeyLength() const {return m_keylen;}
00407
00408 apbt NewEncryption(const byte *key) const
00409 {return apbt(new E(key, m_keylen, m_rounds));}
00410 apbt NewDecryption(const byte *key) const
00411 {return apbt(new D(key, m_keylen, m_rounds));}
00412
00413 unsigned int m_keylen, m_rounds;
00414 };
00415
00416 bool BlockTransformationTest(const CipherFactory &cg, BufferedTransformation &valdata, unsigned int tuples = 0xffff)
00417 {
00418 HexEncoder output(new FileSink(cout));
00419 SecByteBlock plain(cg.BlockSize()), cipher(cg.BlockSize()), out(cg.BlockSize()), outplain(cg.BlockSize());
00420 SecByteBlock key(cg.KeyLength());
00421 bool pass=true, fail;
00422
00423 while (valdata.MaxRetrievable() && tuples--)
00424 {
00425 valdata.Get(key, cg.KeyLength());
00426 valdata.Get(plain, cg.BlockSize());
00427 valdata.Get(cipher, cg.BlockSize());
00428
00429 apbt transE = cg.NewEncryption(key);
00430 transE->ProcessBlock(plain, out);
00431 fail = memcmp(out, cipher, cg.BlockSize()) != 0;
00432
00433 apbt transD = cg.NewDecryption(key);
00434 transD->ProcessBlock(out, outplain);
00435 fail=fail || memcmp(outplain, plain, cg.BlockSize());
00436
00437 pass = pass && !fail;
00438
00439 cout << (fail ? "FAILED " : "passed ");
00440 output.Put(key, cg.KeyLength());
00441 cout << " ";
00442 output.Put(outplain, cg.BlockSize());
00443 cout << " ";
00444 output.Put(out, cg.BlockSize());
00445 cout << endl;
00446 }
00447 return pass;
00448 }
00449
00450 class FilterTester : public Unflushable<Sink>
00451 {
00452 public:
00453 FilterTester(const byte *validOutput, size_t outputLen)
00454 : validOutput(validOutput), outputLen(outputLen), counter(0), fail(false) {}
00455 void PutByte(byte inByte)
00456 {
00457 if (counter >= outputLen || validOutput[counter] != inByte)
00458 {
00459 std::cerr << "incorrect output " << counter << ", " << (word16)validOutput[counter] << ", " << (word16)inByte << "\n";
00460 fail = true;
00461 assert(false);
00462 }
00463 counter++;
00464 }
00465 size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
00466 {
00467 while (length--)
00468 FilterTester::PutByte(*inString++);
00469
00470 if (messageEnd)
00471 if (counter != outputLen)
00472 {
00473 fail = true;
00474 assert(false);
00475 }
00476
00477 return 0;
00478 }
00479 bool GetResult()
00480 {
00481 return !fail;
00482 }
00483
00484 const byte *validOutput;
00485 size_t outputLen, counter;
00486 bool fail;
00487 };
00488
00489 bool TestFilter(BufferedTransformation &bt, const byte *in, size_t inLen, const byte *out, size_t outLen)
00490 {
00491 FilterTester *ft;
00492 bt.Attach(ft = new FilterTester(out, outLen));
00493
00494 while (inLen)
00495 {
00496 size_t randomLen = GlobalRNG().GenerateWord32(0, (word32)inLen);
00497 bt.Put(in, randomLen);
00498 in += randomLen;
00499 inLen -= randomLen;
00500 }
00501 bt.MessageEnd();
00502 return ft->GetResult();
00503 }
00504
00505 bool ValidateDES()
00506 {
00507 cout << "\nDES validation suite running...\n\n";
00508
00509 FileSource valdata("TestData/descert.dat", true, new HexDecoder);
00510 bool pass = BlockTransformationTest(FixedRoundsCipherFactory<DESEncryption, DESDecryption>(), valdata);
00511
00512 cout << "\nTesting EDE2, EDE3, and XEX3 variants...\n\n";
00513
00514 FileSource valdata1("TestData/3desval.dat", true, new HexDecoder);
00515 pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE2_Encryption, DES_EDE2_Decryption>(), valdata1, 1) && pass;
00516 pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE3_Encryption, DES_EDE3_Decryption>(), valdata1, 1) && pass;
00517 pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_XEX3_Encryption, DES_XEX3_Decryption>(), valdata1, 1) && pass;
00518
00519 return pass;
00520 }
00521
00522 bool TestModeIV(SymmetricCipher &e, SymmetricCipher &d)
00523 {
00524 SecByteBlock lastIV, iv(e.IVSize());
00525 StreamTransformationFilter filter(e, new StreamTransformationFilter(d));
00526 byte plaintext[20480];
00527
00528 for (unsigned int i=1; i<sizeof(plaintext); i*=2)
00529 {
00530 e.GetNextIV(GlobalRNG(), iv);
00531 if (iv == lastIV)
00532 return false;
00533 else
00534 lastIV = iv;
00535
00536 e.Resynchronize(iv);
00537 d.Resynchronize(iv);
00538
00539 unsigned int length = STDMAX(GlobalRNG().GenerateWord32(0, i), (word32)e.MinLastBlockSize());
00540 GlobalRNG().GenerateBlock(plaintext, length);
00541
00542 if (!TestFilter(filter, plaintext, length, plaintext, length))
00543 return false;
00544 }
00545
00546 return true;
00547 }
00548
00549 bool ValidateCipherModes()
00550 {
00551 cout << "\nTesting DES modes...\n\n";
00552 const byte key[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
00553 const byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
00554 const byte plain[] = {
00555 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
00556 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
00557 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};
00558 DESEncryption desE(key);
00559 DESDecryption desD(key);
00560 bool pass=true, fail;
00561
00562 {
00563
00564 const byte encrypted[] = {
00565 0x3f, 0xa4, 0x0e, 0x8a, 0x98, 0x4d, 0x48, 0x15,
00566 0x6a, 0x27, 0x17, 0x87, 0xab, 0x88, 0x83, 0xf9,
00567 0x89, 0x3d, 0x51, 0xec, 0x4b, 0x56, 0x3b, 0x53};
00568
00569 ECB_Mode_ExternalCipher::Encryption modeE(desE);
00570 fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
00571 plain, sizeof(plain), encrypted, sizeof(encrypted));
00572 pass = pass && !fail;
00573 cout << (fail ? "FAILED " : "passed ") << "ECB encryption" << endl;
00574
00575 ECB_Mode_ExternalCipher::Decryption modeD(desD);
00576 fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
00577 encrypted, sizeof(encrypted), plain, sizeof(plain));
00578 pass = pass && !fail;
00579 cout << (fail ? "FAILED " : "passed ") << "ECB decryption" << endl;
00580 }
00581 {
00582
00583 const byte encrypted[] = {
00584 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
00585 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
00586 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6};
00587
00588 CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
00589 fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
00590 plain, sizeof(plain), encrypted, sizeof(encrypted));
00591 pass = pass && !fail;
00592 cout << (fail ? "FAILED " : "passed ") << "CBC encryption with no padding" << endl;
00593
00594 CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
00595 fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
00596 encrypted, sizeof(encrypted), plain, sizeof(plain));
00597 pass = pass && !fail;
00598 cout << (fail ? "FAILED " : "passed ") << "CBC decryption with no padding" << endl;
00599
00600 fail = !TestModeIV(modeE, modeD);
00601 pass = pass && !fail;
00602 cout << (fail ? "FAILED " : "passed ") << "CBC mode IV generation" << endl;
00603 }
00604 {
00605
00606
00607 const byte encrypted[] = {
00608 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
00609 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
00610 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
00611 0x62, 0xC1, 0x6A, 0x27, 0xE4, 0xFC, 0xF2, 0x77};
00612
00613 CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
00614 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
00615 plain, sizeof(plain), encrypted, sizeof(encrypted));
00616 pass = pass && !fail;
00617 cout << (fail ? "FAILED " : "passed ") << "CBC encryption with PKCS #7 padding" << endl;
00618
00619 CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
00620 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
00621 encrypted, sizeof(encrypted), plain, sizeof(plain));
00622 pass = pass && !fail;
00623 cout << (fail ? "FAILED " : "passed ") << "CBC decryption with PKCS #7 padding" << endl;
00624 }
00625 {
00626
00627
00628 const byte encrypted[] = {
00629 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
00630 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
00631 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
00632 0xcf, 0xb7, 0xc7, 0x64, 0x0e, 0x7c, 0xd9, 0xa7};
00633
00634 CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
00635 fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::ONE_AND_ZEROS_PADDING).Ref(),
00636 plain, sizeof(plain), encrypted, sizeof(encrypted));
00637 pass = pass && !fail;
00638 cout << (fail ? "FAILED " : "passed ") << "CBC encryption with one-and-zeros padding" << endl;
00639
00640 CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
00641 fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::ONE_AND_ZEROS_PADDING).Ref(),
00642 encrypted, sizeof(encrypted), plain, sizeof(plain));
00643 pass = pass && !fail;
00644 cout << (fail ? "FAILED " : "passed ") << "CBC decryption with one-and-zeros padding" << endl;
00645 }
00646 {
00647 const byte plain[] = {'a', 0, 0, 0, 0, 0, 0, 0};
00648
00649 const byte encrypted[] = {
00650 0x9B, 0x47, 0x57, 0x59, 0xD6, 0x9C, 0xF6, 0xD0};
00651
00652 CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
00653 fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),
00654 plain, 1, encrypted, sizeof(encrypted));
00655 pass = pass && !fail;
00656 cout << (fail ? "FAILED " : "passed ") << "CBC encryption with zeros padding" << endl;
00657
00658 CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
00659 fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),
00660 encrypted, sizeof(encrypted), plain, sizeof(plain));
00661 pass = pass && !fail;
00662 cout << (fail ? "FAILED " : "passed ") << "CBC decryption with zeros padding" << endl;
00663 }
00664 {
00665
00666
00667 const byte encrypted[] = {
00668 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
00669 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
00670 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F};
00671
00672 CBC_CTS_Mode_ExternalCipher::Encryption modeE(desE, iv);
00673 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
00674 plain, sizeof(plain), encrypted, sizeof(encrypted));
00675 pass = pass && !fail;
00676 cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext stealing (CTS)" << endl;
00677
00678 CBC_CTS_Mode_ExternalCipher::Decryption modeD(desD, iv);
00679 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
00680 encrypted, sizeof(encrypted), plain, sizeof(plain));
00681 pass = pass && !fail;
00682 cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext stealing (CTS)" << endl;
00683
00684 fail = !TestModeIV(modeE, modeD);
00685 pass = pass && !fail;
00686 cout << (fail ? "FAILED " : "passed ") << "CBC CTS IV generation" << endl;
00687 }
00688 {
00689
00690 const byte decryptionIV[] = {0x4D, 0xD0, 0xAC, 0x8F, 0x47, 0xCF, 0x79, 0xCE};
00691 const byte encrypted[] = {0x12, 0x34, 0x56};
00692
00693 byte stolenIV[8];
00694
00695 CBC_CTS_Mode_ExternalCipher::Encryption modeE(desE, iv);
00696 modeE.SetStolenIV(stolenIV);
00697 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
00698 plain, 3, encrypted, sizeof(encrypted));
00699 fail = memcmp(stolenIV, decryptionIV, 8) != 0 || fail;
00700 pass = pass && !fail;
00701 cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext and IV stealing" << endl;
00702
00703 CBC_CTS_Mode_ExternalCipher::Decryption modeD(desD, stolenIV);
00704 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
00705 encrypted, sizeof(encrypted), plain, 3);
00706 pass = pass && !fail;
00707 cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext and IV stealing" << endl;
00708 }
00709 {
00710 const byte encrypted[] = {
00711 0xF3,0x09,0x62,0x49,0xC7,0xF4,0x6E,0x51,
00712 0xA6,0x9E,0x83,0x9B,0x1A,0x92,0xF7,0x84,
00713 0x03,0x46,0x71,0x33,0x89,0x8E,0xA6,0x22};
00714
00715 CFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
00716 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
00717 plain, sizeof(plain), encrypted, sizeof(encrypted));
00718 pass = pass && !fail;
00719 cout << (fail ? "FAILED " : "passed ") << "CFB encryption" << endl;
00720
00721 CFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
00722 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
00723 encrypted, sizeof(encrypted), plain, sizeof(plain));
00724 pass = pass && !fail;
00725 cout << (fail ? "FAILED " : "passed ") << "CFB decryption" << endl;
00726
00727 fail = !TestModeIV(modeE, modeD);
00728 pass = pass && !fail;
00729 cout << (fail ? "FAILED " : "passed ") << "CFB mode IV generation" << endl;
00730 }
00731 {
00732 const byte plain[] = {
00733 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,0x68,0x65};
00734 const byte encrypted[] = {
00735 0xf3,0x1f,0xda,0x07,0x01,0x14,0x62,0xee,0x18,0x7f};
00736
00737 CFB_Mode_ExternalCipher::Encryption modeE(desE, iv, 1);
00738 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
00739 plain, sizeof(plain), encrypted, sizeof(encrypted));
00740 pass = pass && !fail;
00741 cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) encryption" << endl;
00742
00743 CFB_Mode_ExternalCipher::Decryption modeD(desE, iv, 1);
00744 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
00745 encrypted, sizeof(encrypted), plain, sizeof(plain));
00746 pass = pass && !fail;
00747 cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) decryption" << endl;
00748
00749 fail = !TestModeIV(modeE, modeD);
00750 pass = pass && !fail;
00751 cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) IV generation" << endl;
00752 }
00753 {
00754 const byte encrypted[] = {
00755 0xf3,0x09,0x62,0x49,0xc7,0xf4,0x6e,0x51,
00756 0x35,0xf2,0x4a,0x24,0x2e,0xeb,0x3d,0x3f,
00757 0x3d,0x6d,0x5b,0xe3,0x25,0x5a,0xf8,0xc3};
00758
00759 OFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
00760 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
00761 plain, sizeof(plain), encrypted, sizeof(encrypted));
00762 pass = pass && !fail;
00763 cout << (fail ? "FAILED " : "passed ") << "OFB encryption" << endl;
00764
00765 OFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
00766 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
00767 encrypted, sizeof(encrypted), plain, sizeof(plain));
00768 pass = pass && !fail;
00769 cout << (fail ? "FAILED " : "passed ") << "OFB decryption" << endl;
00770
00771 fail = !TestModeIV(modeE, modeD);
00772 pass = pass && !fail;
00773 cout << (fail ? "FAILED " : "passed ") << "OFB IV generation" << endl;
00774 }
00775 {
00776 const byte encrypted[] = {
00777 0xF3, 0x09, 0x62, 0x49, 0xC7, 0xF4, 0x6E, 0x51,
00778 0x16, 0x3A, 0x8C, 0xA0, 0xFF, 0xC9, 0x4C, 0x27,
00779 0xFA, 0x2F, 0x80, 0xF4, 0x80, 0xB8, 0x6F, 0x75};
00780
00781 CTR_Mode_ExternalCipher::Encryption modeE(desE, iv);
00782 fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
00783 plain, sizeof(plain), encrypted, sizeof(encrypted));
00784 pass = pass && !fail;
00785 cout << (fail ? "FAILED " : "passed ") << "Counter Mode encryption" << endl;
00786
00787 CTR_Mode_ExternalCipher::Decryption modeD(desE, iv);
00788 fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
00789 encrypted, sizeof(encrypted), plain, sizeof(plain));
00790 pass = pass && !fail;
00791 cout << (fail ? "FAILED " : "passed ") << "Counter Mode decryption" << endl;
00792
00793 fail = !TestModeIV(modeE, modeD);
00794 pass = pass && !fail;
00795 cout << (fail ? "FAILED " : "passed ") << "Counter Mode IV generation" << endl;
00796 }
00797 {
00798 const byte plain[] = {
00799 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,
00800 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
00801 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
00802 0x66, 0x6f, 0x72, 0x20};
00803 const byte mac1[] = {
00804 0xf1, 0xd3, 0x0f, 0x68, 0x49, 0x31, 0x2c, 0xa4};
00805 const byte mac2[] = {
00806 0x35, 0x80, 0xC5, 0xC4, 0x6B, 0x81, 0x24, 0xE2};
00807
00808 CBC_MAC<DES> cbcmac(key);
00809 HashFilter cbcmacFilter(cbcmac);
00810 fail = !TestFilter(cbcmacFilter, plain, sizeof(plain), mac1, sizeof(mac1));
00811 pass = pass && !fail;
00812 cout << (fail ? "FAILED " : "passed ") << "CBC MAC" << endl;
00813
00814 DMAC<DES> dmac(key);
00815 HashFilter dmacFilter(dmac);
00816 fail = !TestFilter(dmacFilter, plain, sizeof(plain), mac2, sizeof(mac2));
00817 pass = pass && !fail;
00818 cout << (fail ? "FAILED " : "passed ") << "DMAC" << endl;
00819 }
00820 {
00821 CTR_Mode<AES>::Encryption modeE(plain, 16, plain);
00822 CTR_Mode<AES>::Decryption modeD(plain, 16, plain);
00823 fail = !TestModeIV(modeE, modeD);
00824 pass = pass && !fail;
00825 cout << (fail ? "FAILED " : "passed ") << "AES CTR Mode" << endl;
00826 }
00827 {
00828 OFB_Mode<AES>::Encryption modeE(plain, 16, plain);
00829 OFB_Mode<AES>::Decryption modeD(plain, 16, plain);
00830 fail = !TestModeIV(modeE, modeD);
00831 pass = pass && !fail;
00832 cout << (fail ? "FAILED " : "passed ") << "AES OFB Mode" << endl;
00833 }
00834 {
00835 CFB_Mode<AES>::Encryption modeE(plain, 16, plain);
00836 CFB_Mode<AES>::Decryption modeD(plain, 16, plain);
00837 fail = !TestModeIV(modeE, modeD);
00838 pass = pass && !fail;
00839 cout << (fail ? "FAILED " : "passed ") << "AES CFB Mode" << endl;
00840 }
00841 {
00842 CBC_Mode<AES>::Encryption modeE(plain, 16, plain);
00843 CBC_Mode<AES>::Decryption modeD(plain, 16, plain);
00844 fail = !TestModeIV(modeE, modeD);
00845 pass = pass && !fail;
00846 cout << (fail ? "FAILED " : "passed ") << "AES CBC Mode" << endl;
00847 }
00848
00849 return pass;
00850 }
00851
00852 bool ValidateIDEA()
00853 {
00854 cout << "\nIDEA validation suite running...\n\n";
00855
00856 FileSource valdata("TestData/ideaval.dat", true, new HexDecoder);
00857 return BlockTransformationTest(FixedRoundsCipherFactory<IDEAEncryption, IDEADecryption>(), valdata);
00858 }
00859
00860 bool ValidateSAFER()
00861 {
00862 cout << "\nSAFER validation suite running...\n\n";
00863
00864 FileSource valdata("TestData/saferval.dat", true, new HexDecoder);
00865 bool pass = true;
00866 pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(8,6), valdata, 4) && pass;
00867 pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(16,12), valdata, 4) && pass;
00868 pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(8,6), valdata, 4) && pass;
00869 pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(16,10), valdata, 4) && pass;
00870 return pass;
00871 }
00872
00873 bool ValidateRC2()
00874 {
00875 cout << "\nRC2 validation suite running...\n\n";
00876
00877 FileSource valdata("TestData/rc2val.dat", true, new HexDecoder);
00878 HexEncoder output(new FileSink(cout));
00879 SecByteBlock plain(RC2Encryption::BLOCKSIZE), cipher(RC2Encryption::BLOCKSIZE), out(RC2Encryption::BLOCKSIZE), outplain(RC2Encryption::BLOCKSIZE);
00880 SecByteBlock key(128);
00881 bool pass=true, fail;
00882
00883 while (valdata.MaxRetrievable())
00884 {
00885 byte keyLen, effectiveLen;
00886
00887 valdata.Get(keyLen);
00888 valdata.Get(effectiveLen);
00889 valdata.Get(key, keyLen);
00890 valdata.Get(plain, RC2Encryption::BLOCKSIZE);
00891 valdata.Get(cipher, RC2Encryption::BLOCKSIZE);
00892
00893 apbt transE(new RC2Encryption(key, keyLen, effectiveLen));
00894 transE->ProcessBlock(plain, out);
00895 fail = memcmp(out, cipher, RC2Encryption::BLOCKSIZE) != 0;
00896
00897 apbt transD(new RC2Decryption(key, keyLen, effectiveLen));
00898 transD->ProcessBlock(out, outplain);
00899 fail=fail || memcmp(outplain, plain, RC2Encryption::BLOCKSIZE);
00900
00901 pass = pass && !fail;
00902
00903 cout << (fail ? "FAILED " : "passed ");
00904 output.Put(key, keyLen);
00905 cout << " ";
00906 output.Put(outplain, RC2Encryption::BLOCKSIZE);
00907 cout << " ";
00908 output.Put(out, RC2Encryption::BLOCKSIZE);
00909 cout << endl;
00910 }
00911 return pass;
00912 }
00913
00914 bool ValidateARC4()
00915 {
00916 unsigned char Key0[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
00917 unsigned char Input0[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
00918 unsigned char Output0[] = {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96};
00919
00920 unsigned char Key1[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
00921 unsigned char Input1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
00922 unsigned char Output1[]={0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79};
00923
00924 unsigned char Key2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
00925 unsigned char Input2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
00926 unsigned char Output2[]={0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a};
00927
00928 unsigned char Key3[]={0xef,0x01,0x23,0x45};
00929 unsigned char Input3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
00930 unsigned char Output3[]={0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61};
00931
00932 unsigned char Key4[]={ 0x01,0x23,0x45,0x67,0x89,0xab, 0xcd,0xef };
00933 unsigned char Input4[] =
00934 {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00935 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00936 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00937 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00938 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00939 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00940 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00941 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00942 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00943 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00944 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00945 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00946 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00947 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00948 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00949 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00950 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00951 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00952 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00953 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00954 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00955 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00956 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00957 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00958 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00959 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00960 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00961 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00962 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00963 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00964 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00965 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00966 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00967 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00968 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00969 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00970 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00971 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00972 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00973 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00974 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00975 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00976 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00977 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00978 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00979 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00980 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00981 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00982 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00983 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00984 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
00985 0x01};
00986 unsigned char Output4[]= {
00987 0x75,0x95,0xc3,0xe6,0x11,0x4a,0x09,0x78,0x0c,0x4a,0xd4,
00988 0x52,0x33,0x8e,0x1f,0xfd,0x9a,0x1b,0xe9,0x49,0x8f,
00989 0x81,0x3d,0x76,0x53,0x34,0x49,0xb6,0x77,0x8d,0xca,
00990 0xd8,0xc7,0x8a,0x8d,0x2b,0xa9,0xac,0x66,0x08,0x5d,
00991 0x0e,0x53,0xd5,0x9c,0x26,0xc2,0xd1,0xc4,0x90,0xc1,
00992 0xeb,0xbe,0x0c,0xe6,0x6d,0x1b,0x6b,0x1b,0x13,0xb6,
00993 0xb9,0x19,0xb8,0x47,0xc2,0x5a,0x91,0x44,0x7a,0x95,
00994 0xe7,0x5e,0x4e,0xf1,0x67,0x79,0xcd,0xe8,0xbf,0x0a,
00995 0x95,0x85,0x0e,0x32,0xaf,0x96,0x89,0x44,0x4f,0xd3,
00996 0x77,0x10,0x8f,0x98,0xfd,0xcb,0xd4,0xe7,0x26,0x56,
00997 0x75,0x00,0x99,0x0b,0xcc,0x7e,0x0c,0xa3,0xc4,0xaa,
00998 0xa3,0x04,0xa3,0x87,0xd2,0x0f,0x3b,0x8f,0xbb,0xcd,
00999 0x42,0xa1,0xbd,0x31,0x1d,0x7a,0x43,0x03,0xdd,0xa5,
01000 0xab,0x07,0x88,0x96,0xae,0x80,0xc1,0x8b,0x0a,0xf6,
01001 0x6d,0xff,0x31,0x96,0x16,0xeb,0x78,0x4e,0x49,0x5a,
01002 0xd2,0xce,0x90,0xd7,0xf7,0x72,0xa8,0x17,0x47,0xb6,
01003 0x5f,0x62,0x09,0x3b,0x1e,0x0d,0xb9,0xe5,0xba,0x53,
01004 0x2f,0xaf,0xec,0x47,0x50,0x83,0x23,0xe6,0x71,0x32,
01005 0x7d,0xf9,0x44,0x44,0x32,0xcb,0x73,0x67,0xce,0xc8,
01006 0x2f,0x5d,0x44,0xc0,0xd0,0x0b,0x67,0xd6,0x50,0xa0,
01007 0x75,0xcd,0x4b,0x70,0xde,0xdd,0x77,0xeb,0x9b,0x10,
01008 0x23,0x1b,0x6b,0x5b,0x74,0x13,0x47,0x39,0x6d,0x62,
01009 0x89,0x74,0x21,0xd4,0x3d,0xf9,0xb4,0x2e,0x44,0x6e,
01010 0x35,0x8e,0x9c,0x11,0xa9,0xb2,0x18,0x4e,0xcb,0xef,
01011 0x0c,0xd8,0xe7,0xa8,0x77,0xef,0x96,0x8f,0x13,0x90,
01012 0xec,0x9b,0x3d,0x35,0xa5,0x58,0x5c,0xb0,0x09,0x29,
01013 0x0e,0x2f,0xcd,0xe7,0xb5,0xec,0x66,0xd9,0x08,0x4b,
01014 0xe4,0x40,0x55,0xa6,0x19,0xd9,0xdd,0x7f,0xc3,0x16,
01015 0x6f,0x94,0x87,0xf7,0xcb,0x27,0x29,0x12,0x42,0x64,
01016 0x45,0x99,0x85,0x14,0xc1,0x5d,0x53,0xa1,0x8c,0x86,
01017 0x4c,0xe3,0xa2,0xb7,0x55,0x57,0x93,0x98,0x81,0x26,
01018 0x52,0x0e,0xac,0xf2,0xe3,0x06,0x6e,0x23,0x0c,0x91,
01019 0xbe,0xe4,0xdd,0x53,0x04,0xf5,0xfd,0x04,0x05,0xb3,
01020 0x5b,0xd9,0x9c,0x73,0x13,0x5d,0x3d,0x9b,0xc3,0x35,
01021 0xee,0x04,0x9e,0xf6,0x9b,0x38,0x67,0xbf,0x2d,0x7b,
01022 0xd1,0xea,0xa5,0x95,0xd8,0xbf,0xc0,0x06,0x6f,0xf8,
01023 0xd3,0x15,0x09,0xeb,0x0c,0x6c,0xaa,0x00,0x6c,0x80,
01024 0x7a,0x62,0x3e,0xf8,0x4c,0x3d,0x33,0xc1,0x95,0xd2,
01025 0x3e,0xe3,0x20,0xc4,0x0d,0xe0,0x55,0x81,0x57,0xc8,
01026 0x22,0xd4,0xb8,0xc5,0x69,0xd8,0x49,0xae,0xd5,0x9d,
01027 0x4e,0x0f,0xd7,0xf3,0x79,0x58,0x6b,0x4b,0x7f,0xf6,
01028 0x84,0xed,0x6a,0x18,0x9f,0x74,0x86,0xd4,0x9b,0x9c,
01029 0x4b,0xad,0x9b,0xa2,0x4b,0x96,0xab,0xf9,0x24,0x37,
01030 0x2c,0x8a,0x8f,0xff,0xb1,0x0d,0x55,0x35,0x49,0x00,
01031 0xa7,0x7a,0x3d,0xb5,0xf2,0x05,0xe1,0xb9,0x9f,0xcd,
01032 0x86,0x60,0x86,0x3a,0x15,0x9a,0xd4,0xab,0xe4,0x0f,
01033 0xa4,0x89,0x34,0x16,0x3d,0xdd,0xe5,0x42,0xa6,0x58,
01034 0x55,0x40,0xfd,0x68,0x3c,0xbf,0xd8,0xc0,0x0f,0x12,
01035 0x12,0x9a,0x28,0x4d,0xea,0xcc,0x4c,0xde,0xfe,0x58,
01036 0xbe,0x71,0x37,0x54,0x1c,0x04,0x71,0x26,0xc8,0xd4,
01037 0x9e,0x27,0x55,0xab,0x18,0x1a,0xb7,0xe9,0x40,0xb0,
01038 0xc0};
01039
01040
01041 member_ptr<Weak::ARC4> arc4;
01042 bool pass=true, fail;
01043 int i;
01044
01045 cout << "\nARC4 validation suite running...\n\n";
01046
01047 arc4.reset(new Weak::ARC4(Key0, sizeof(Key0)));
01048 arc4->ProcessString(Input0, sizeof(Input0));
01049 fail = memcmp(Input0, Output0, sizeof(Input0)) != 0;
01050 cout << (fail ? "FAILED" : "passed") << " Test 0" << endl;
01051 pass = pass && !fail;
01052
01053 arc4.reset(new Weak::ARC4(Key1, sizeof(Key1)));
01054 arc4->ProcessString(Key1, Input1, sizeof(Key1));
01055 fail = memcmp(Output1, Key1, sizeof(Key1)) != 0;
01056 cout << (fail ? "FAILED" : "passed") << " Test 1" << endl;
01057 pass = pass && !fail;
01058
01059 arc4.reset(new Weak::ARC4(Key2, sizeof(Key2)));
01060 for (i=0, fail=false; i<sizeof(Input2); i++)
01061 if (arc4->ProcessByte(Input2[i]) != Output2[i])
01062 fail = true;
01063 cout << (fail ? "FAILED" : "passed") << " Test 2" << endl;
01064 pass = pass && !fail;
01065
01066 arc4.reset(new Weak::ARC4(Key3, sizeof(Key3)));
01067 for (i=0, fail=false; i<sizeof(Input3); i++)
01068 if (arc4->ProcessByte(Input3[i]) != Output3[i])
01069 fail = true;
01070 cout << (fail ? "FAILED" : "passed") << " Test 3" << endl;
01071 pass = pass && !fail;
01072
01073 arc4.reset(new Weak::ARC4(Key4, sizeof(Key4)));
01074 for (i=0, fail=false; i<sizeof(Input4); i++)
01075 if (arc4->ProcessByte(Input4[i]) != Output4[i])
01076 fail = true;
01077 cout << (fail ? "FAILED" : "passed") << " Test 4" << endl;
01078 pass = pass && !fail;
01079
01080 return pass;
01081 }
01082
01083 bool ValidateRC5()
01084 {
01085 cout << "\nRC5 validation suite running...\n\n";
01086
01087 FileSource valdata("TestData/rc5val.dat", true, new HexDecoder);
01088 return BlockTransformationTest(VariableRoundsCipherFactory<RC5Encryption, RC5Decryption>(16, 12), valdata);
01089 }
01090
01091 bool ValidateRC6()
01092 {
01093 cout << "\nRC6 validation suite running...\n\n";
01094
01095 FileSource valdata("TestData/rc6val.dat", true, new HexDecoder);
01096 bool pass = true;
01097 pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(16), valdata, 2) && pass;
01098 pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(24), valdata, 2) && pass;
01099 pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(32), valdata, 2) && pass;
01100 return pass;
01101 }
01102
01103 bool ValidateRijndael()
01104 {
01105 cout << "\nRijndael (AES) validation suite running...\n\n";
01106
01107 FileSource valdata("TestData/rijndael.dat", true, new HexDecoder);
01108 bool pass = true;
01109 pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(16), valdata, 4) && pass;
01110 pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(24), valdata, 3) && pass;
01111 pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(32), valdata, 2) && pass;
01112 pass = RunTestDataFile("TestVectors/aes.txt") && pass;
01113 return pass;
01114 }
01115
01116 bool ValidateTwofish()
01117 {
01118 cout << "\nTwofish validation suite running...\n\n";
01119
01120 FileSource valdata("TestData/twofishv.dat", true, new HexDecoder);
01121 bool pass = true;
01122 pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(16), valdata, 4) && pass;
01123 pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(24), valdata, 3) && pass;
01124 pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(32), valdata, 2) && pass;
01125 return pass;
01126 }
01127
01128 bool ValidateSerpent()
01129 {
01130 cout << "\nSerpent validation suite running...\n\n";
01131
01132 FileSource valdata("TestData/serpentv.dat", true, new HexDecoder);
01133 bool pass = true;
01134 pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(16), valdata, 4) && pass;
01135 pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(24), valdata, 3) && pass;
01136 pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(32), valdata, 2) && pass;
01137 return pass;
01138 }
01139
01140 bool ValidateBlowfish()
01141 {
01142 cout << "\nBlowfish validation suite running...\n\n";
01143
01144 HexEncoder output(new FileSink(cout));
01145 const char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"};
01146 byte *plain[]={(byte *)"BLOWFISH", (byte *)"\xfe\xdc\xba\x98\x76\x54\x32\x10"};
01147 byte *cipher[]={(byte *)"\x32\x4e\xd0\xfe\xf4\x13\xa2\x03", (byte *)"\xcc\x91\x73\x2b\x80\x22\xf6\x84"};
01148 byte out[8], outplain[8];
01149 bool pass=true, fail;
01150
01151 for (int i=0; i<2; i++)
01152 {
01153 ECB_Mode<Blowfish>::Encryption enc((byte *)key[i], strlen(key[i]));
01154 enc.ProcessData(out, plain[i], 8);
01155 fail = memcmp(out, cipher[i], 8) != 0;
01156
01157 ECB_Mode<Blowfish>::Decryption dec((byte *)key[i], strlen(key[i]));
01158 dec.ProcessData(outplain, cipher[i], 8);
01159 fail = fail || memcmp(outplain, plain[i], 8);
01160 pass = pass && !fail;
01161
01162 cout << (fail ? "FAILED " : "passed ");
01163 cout << '\"' << key[i] << '\"';
01164 for (int j=0; j<(signed int)(30-strlen(key[i])); j++)
01165 cout << ' ';
01166 output.Put(outplain, 8);
01167 cout << " ";
01168 output.Put(out, 8);
01169 cout << endl;
01170 }
01171 return pass;
01172 }
01173
01174 bool ValidateThreeWay()
01175 {
01176 cout << "\n3-WAY validation suite running...\n\n";
01177
01178 FileSource valdata("TestData/3wayval.dat", true, new HexDecoder);
01179 return BlockTransformationTest(FixedRoundsCipherFactory<ThreeWayEncryption, ThreeWayDecryption>(), valdata);
01180 }
01181
01182 bool ValidateGOST()
01183 {
01184 cout << "\nGOST validation suite running...\n\n";
01185
01186 FileSource valdata("TestData/gostval.dat", true, new HexDecoder);
01187 return BlockTransformationTest(FixedRoundsCipherFactory<GOSTEncryption, GOSTDecryption>(), valdata);
01188 }
01189
01190 bool ValidateSHARK()
01191 {
01192 cout << "\nSHARK validation suite running...\n\n";
01193
01194 FileSource valdata("TestData/sharkval.dat", true, new HexDecoder);
01195 return BlockTransformationTest(FixedRoundsCipherFactory<SHARKEncryption, SHARKDecryption>(), valdata);
01196 }
01197
01198 bool ValidateCAST()
01199 {
01200 bool pass = true;
01201
01202 cout << "\nCAST-128 validation suite running...\n\n";
01203
01204 FileSource val128("TestData/cast128v.dat", true, new HexDecoder);
01205 pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(16), val128, 1) && pass;
01206 pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(10), val128, 1) && pass;
01207 pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(5), val128, 1) && pass;
01208
01209 cout << "\nCAST-256 validation suite running...\n\n";
01210
01211 FileSource val256("TestData/cast256v.dat", true, new HexDecoder);
01212 pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(16), val256, 1) && pass;
01213 pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(24), val256, 1) && pass;
01214 pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(32), val256, 1) && pass;
01215
01216 return pass;
01217 }
01218
01219 bool ValidateSquare()
01220 {
01221 cout << "\nSquare validation suite running...\n\n";
01222
01223 FileSource valdata("TestData/squareva.dat", true, new HexDecoder);
01224 return BlockTransformationTest(FixedRoundsCipherFactory<SquareEncryption, SquareDecryption>(), valdata);
01225 }
01226
01227 bool ValidateSKIPJACK()
01228 {
01229 cout << "\nSKIPJACK validation suite running...\n\n";
01230
01231 FileSource valdata("TestData/skipjack.dat", true, new HexDecoder);
01232 return BlockTransformationTest(FixedRoundsCipherFactory<SKIPJACKEncryption, SKIPJACKDecryption>(), valdata);
01233 }
01234
01235 bool ValidateSEAL()
01236 {
01237 byte input[] = {0x37,0xa0,0x05,0x95,0x9b,0x84,0xc4,0x9c,0xa4,0xbe,0x1e,0x05,0x06,0x73,0x53,0x0f,0x5f,0xb0,0x97,0xfd,0xf6,0xa1,0x3f,0xbd,0x6c,0x2c,0xde,0xcd,0x81,0xfd,0xee,0x7c};
01238 byte output[32];
01239 byte key[] = {0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x98, 0xba, 0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76, 0xc3, 0xd2, 0xe1, 0xf0};
01240 byte iv[] = {0x01, 0x35, 0x77, 0xaf};
01241
01242 cout << "\nSEAL validation suite running...\n\n";
01243
01244 SEAL<>::Encryption seal(key, sizeof(key), iv);
01245 unsigned int size = sizeof(input);
01246 bool pass = true;
01247
01248 memset(output, 1, size);
01249 seal.ProcessString(output, input, size);
01250 for (unsigned int i=0; i<size; i++)
01251 if (output[i] != 0)
01252 pass = false;
01253
01254 seal.Seek(1);
01255 output[1] = seal.ProcessByte(output[1]);
01256 seal.ProcessString(output+2, size-2);
01257 pass = pass && memcmp(output+1, input+1, size-1) == 0;
01258
01259 cout << (pass ? "passed" : "FAILED") << endl;
01260 return pass;
01261 }
01262
01263 bool ValidateBaseCode()
01264 {
01265 bool pass = true, fail;
01266 byte data[255];
01267 for (unsigned int i=0; i<255; i++)
01268 data[i] = i;
01269 const char *hexEncoded =
01270 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"
01271 "28292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F"
01272 "505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F7071727374757677"
01273 "78797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F"
01274 "A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7"
01275 "C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
01276 "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFE";
01277 const char *base32Encoded =
01278 "AAASEA2EAWDAQCAJBIFS2DIQB6IBCESVCSKTNF22DEPBYHA7D2RUAIJCENUCKJTHFAWUWK3NFWZC8NBT"
01279 "GI3VIPJYG66DUQT5HS8V6R4AIFBEGTCFI3DWSUKKJPGE4VURKBIXEW4WKXMFQYC3MJPX2ZK8M7SGC2VD"
01280 "NTUYN35IPFXGY5DPP3ZZA6MUQP4HK7VZRB6ZW856RX9H9AEBSKB2JBNGS8EIVCWMTUG27D6SUGJJHFEX"
01281 "U4M3TGN4VQQJ5HW9WCS4FI7EWYVKRKFJXKX43MPQX82MDNXVYU45PP72ZG7MZRF7Z496BSQC2RCNMTYH"
01282 "3DE6XU8N3ZHN9WGT4MJ7JXQY49NPVYY55VQ77Z9A6HTQH3HF65V8T4RK7RYQ55ZR8D29F69W8Z5RR8H3"
01283 "9M7939R8";
01284 const char *base64AndHexEncoded =
01285 "41414543417751464267634943516F4C4441304F4478415245684D554652595847426B6147787764"
01286 "486838674953496A4A43556D4A7967704B6973734C5334764D4445794D7A51310A4E6A63344F546F"
01287 "375044302B50304242516B4E4552555A4853456C4B5330784E546B395155564A5456465657563168"
01288 "5A576C746358563566594746695932526C5A6D646F615770720A6247317562334278636E4E306458"
01289 "5A3365486C3665337839666E2B4167594B44684957476834694A696F754D6A5936506B4A47536B35"
01290 "53566C7065596D5A71626E4A32656E3643680A6F714F6B7061616E714B6D717136797472712B7773"
01291 "624B7A744C573274376935757275387662362F774D484377385446787366497963724C7A4D334F7A"
01292 "39445230745055316462580A324E6E6132397A6433742F6734654C6A354F586D352B6A7036757673"
01293 "3765377638504879382F5431397666342B6672372F50332B0A";
01294
01295 cout << "\nBase64, base32 and hex coding validation suite running...\n\n";
01296
01297 fail = !TestFilter(HexEncoder().Ref(), data, 255, (const byte *)hexEncoded, strlen(hexEncoded));
01298 cout << (fail ? "FAILED " : "passed ");
01299 cout << "Hex Encoding\n";
01300 pass = pass && !fail;
01301
01302 fail = !TestFilter(HexDecoder().Ref(), (const byte *)hexEncoded, strlen(hexEncoded), data, 255);
01303 cout << (fail ? "FAILED " : "passed ");
01304 cout << "Hex Decoding\n";
01305 pass = pass && !fail;
01306
01307 fail = !TestFilter(Base32Encoder().Ref(), data, 255, (const byte *)base32Encoded, strlen(base32Encoded));
01308 cout << (fail ? "FAILED " : "passed ");
01309 cout << "Base32 Encoding\n";
01310 pass = pass && !fail;
01311
01312 fail = !TestFilter(Base32Decoder().Ref(), (const byte *)base32Encoded, strlen(base32Encoded), data, 255);
01313 cout << (fail ? "FAILED " : "passed ");
01314 cout << "Base32 Decoding\n";
01315 pass = pass && !fail;
01316
01317 fail = !TestFilter(Base64Encoder(new HexEncoder).Ref(), data, 255, (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded));
01318 cout << (fail ? "FAILED " : "passed ");
01319 cout << "Base64 Encoding\n";
01320 pass = pass && !fail;
01321
01322 fail = !TestFilter(HexDecoder(new Base64Decoder).Ref(), (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded), data, 255);
01323 cout << (fail ? "FAILED " : "passed ");
01324 cout << "Base64 Decoding\n";
01325 pass = pass && !fail;
01326
01327 return pass;
01328 }
01329
01330 bool ValidateSHACAL2()
01331 {
01332 cout << "\nSHACAL-2 validation suite running...\n\n";
01333
01334 bool pass = true;
01335 FileSource valdata("TestData/shacal2v.dat", true, new HexDecoder);
01336 pass = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(16), valdata, 4) && pass;
01337 pass = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(64), valdata, 10) && pass;
01338 return pass;
01339 }
01340
01341 bool ValidateCamellia()
01342 {
01343 cout << "\nCamellia validation suite running...\n\n";
01344
01345 bool pass = true;
01346 FileSource valdata("TestData/camellia.dat", true, new HexDecoder);
01347 pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(16), valdata, 15) && pass;
01348 pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(24), valdata, 15) && pass;
01349 pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(32), valdata, 15) && pass;
01350 return pass;
01351 }
01352
01353 bool ValidateSalsa()
01354 {
01355 cout << "\nSalsa validation suite running...\n";
01356
01357 return RunTestDataFile("TestVectors/salsa.txt");
01358 }
01359
01360 bool ValidateSosemanuk()
01361 {
01362 cout << "\nSosemanuk validation suite running...\n";
01363 return RunTestDataFile("TestVectors/sosemanuk.txt");
01364 }
01365
01366 bool ValidateVMAC()
01367 {
01368 cout << "\nVMAC validation suite running...\n";
01369 return RunTestDataFile("TestVectors/vmac.txt");
01370 }
01371
01372 bool ValidateCCM()
01373 {
01374 cout << "\nAES/CCM validation suite running...\n";
01375 return RunTestDataFile("TestVectors/ccm.txt");
01376 }
01377
01378 bool ValidateGCM()
01379 {
01380 cout << "\nAES/GCM validation suite running...\n";
01381 cout << "\n2K tables:";
01382 bool pass = RunTestDataFile("TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)2048));
01383 cout << "\n64K tables:";
01384 return RunTestDataFile("TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)64*1024)) && pass;
01385 }
01386
01387 bool ValidateCMAC()
01388 {
01389 cout << "\nCMAC validation suite running...\n";
01390 return RunTestDataFile("TestVectors/cmac.txt");
01391 }