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