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