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