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