Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1639 lines
53 KiB

  1. // validat1.cpp - written and placed in the public domain by Wei Dai
  2. #include "pch.h"
  3. #define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
  4. #include "cryptlib.h"
  5. #include "pubkey.h"
  6. #include "gfpcrypt.h"
  7. #include "eccrypto.h"
  8. #include "filters.h"
  9. #include "files.h"
  10. #include "hex.h"
  11. #include "base32.h"
  12. #include "base64.h"
  13. #include "modes.h"
  14. #include "cbcmac.h"
  15. #include "dmac.h"
  16. #include "idea.h"
  17. #include "des.h"
  18. #include "rc2.h"
  19. #include "arc4.h"
  20. #include "rc5.h"
  21. #include "blowfish.h"
  22. #include "3way.h"
  23. #include "safer.h"
  24. #include "gost.h"
  25. #include "shark.h"
  26. #include "cast.h"
  27. #include "square.h"
  28. #include "seal.h"
  29. #include "rc6.h"
  30. #include "mars.h"
  31. #include "aes.h"
  32. #include "cpu.h"
  33. #include "rng.h"
  34. #include "rijndael.h"
  35. #include "twofish.h"
  36. #include "serpent.h"
  37. #include "skipjack.h"
  38. #include "shacal2.h"
  39. #include "camellia.h"
  40. #include "osrng.h"
  41. #include "rdrand.h"
  42. #include "zdeflate.h"
  43. #include "smartptr.h"
  44. #include "channels.h"
  45. #include <time.h>
  46. #include <memory>
  47. #include <iostream>
  48. #include <iomanip>
  49. #include "validate.h"
  50. // Aggressive stack checking with VS2005 SP1 and above.
  51. #if (CRYPTOPP_MSC_VERSION >= 1410)
  52. # pragma strict_gs_check (on)
  53. #endif
  54. USING_NAMESPACE(CryptoPP)
  55. USING_NAMESPACE(std)
  56. bool ValidateAll(bool thorough)
  57. {
  58. bool pass=TestSettings();
  59. pass=TestOS_RNG() && pass;
  60. pass=TestAutoSeeded() && pass;
  61. #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
  62. pass=TestRDRAND() && pass;
  63. pass=TestRDSEED() && pass;
  64. #else
  65. #endif
  66. #if !defined(NDEBUG) && !defined(CRYPTOPP_IMPORTS)
  67. // http://github.com/weidai11/cryptopp/issues/64
  68. pass=TestPolynomialMod2() && pass;
  69. #endif
  70. pass=ValidateCRC32() && pass;
  71. pass=ValidateAdler32() && pass;
  72. pass=ValidateMD2() && pass;
  73. pass=ValidateMD5() && pass;
  74. pass=ValidateSHA() && pass;
  75. pass=RunTestDataFile("TestVectors/sha3.txt") && pass;
  76. pass=ValidateTiger() && pass;
  77. pass=ValidateRIPEMD() && pass;
  78. pass=ValidatePanama() && pass;
  79. pass=ValidateWhirlpool() && pass;
  80. pass=ValidateHMAC() && pass;
  81. pass=ValidateTTMAC() && pass;
  82. pass=ValidatePBKDF() && pass;
  83. pass=ValidateHKDF() && pass;
  84. pass=ValidateDES() && pass;
  85. pass=ValidateCipherModes() && pass;
  86. pass=ValidateIDEA() && pass;
  87. pass=ValidateSAFER() && pass;
  88. pass=ValidateRC2() && pass;
  89. pass=ValidateARC4() && pass;
  90. pass=ValidateRC5() && pass;
  91. pass=ValidateBlowfish() && pass;
  92. pass=ValidateThreeWay() && pass;
  93. pass=ValidateGOST() && pass;
  94. pass=ValidateSHARK() && pass;
  95. pass=ValidateCAST() && pass;
  96. pass=ValidateSquare() && pass;
  97. pass=ValidateSKIPJACK() && pass;
  98. pass=ValidateSEAL() && pass;
  99. pass=ValidateRC6() && pass;
  100. pass=ValidateMARS() && pass;
  101. pass=ValidateRijndael() && pass;
  102. pass=ValidateTwofish() && pass;
  103. pass=ValidateSerpent() && pass;
  104. pass=ValidateSHACAL2() && pass;
  105. pass=ValidateCamellia() && pass;
  106. pass=ValidateSalsa() && pass;
  107. pass=ValidateSosemanuk() && pass;
  108. pass=ValidateVMAC() && pass;
  109. pass=ValidateCCM() && pass;
  110. pass=ValidateGCM() && pass;
  111. pass=ValidateCMAC() && pass;
  112. pass=RunTestDataFile("TestVectors/eax.txt") && pass;
  113. pass=RunTestDataFile("TestVectors/seed.txt") && pass;
  114. pass=ValidateBBS() && pass;
  115. pass=ValidateDH() && pass;
  116. pass=ValidateMQV() && pass;
  117. pass=ValidateRSA() && pass;
  118. pass=ValidateElGamal() && pass;
  119. pass=ValidateDLIES() && pass;
  120. pass=ValidateNR() && pass;
  121. pass=ValidateDSA(thorough) && pass;
  122. pass=ValidateLUC() && pass;
  123. pass=ValidateLUC_DH() && pass;
  124. pass=ValidateLUC_DL() && pass;
  125. pass=ValidateXTR_DH() && pass;
  126. pass=ValidateRabin() && pass;
  127. pass=ValidateRW() && pass;
  128. // pass=ValidateBlumGoldwasser() && pass;
  129. pass=ValidateECP() && pass;
  130. pass=ValidateEC2N() && pass;
  131. pass=ValidateECDSA() && pass;
  132. pass=ValidateESIGN() && pass;
  133. if (pass)
  134. cout << "\nAll tests passed!\n";
  135. else
  136. cout << "\nOops! Not all tests passed.\n";
  137. return pass;
  138. }
  139. bool TestSettings()
  140. {
  141. // Thanks to IlyaBizyaev and Zireael-N, http://github.com/weidai11/cryptopp/issues/28
  142. #if defined(__MINGW32__)
  143. using CryptoPP::memcpy_s;
  144. #endif
  145. bool pass = true;
  146. cout << "\nTesting Settings...\n\n";
  147. word32 w;
  148. memcpy_s(&w, sizeof(w), "\x01\x02\x03\x04", 4);
  149. if (w == 0x04030201L)
  150. {
  151. #ifdef IS_LITTLE_ENDIAN
  152. cout << "passed: ";
  153. #else
  154. cout << "FAILED: ";
  155. pass = false;
  156. #endif
  157. cout << "Your machine is little endian.\n";
  158. }
  159. else if (w == 0x01020304L)
  160. {
  161. #ifndef IS_LITTLE_ENDIAN
  162. cout << "passed: ";
  163. #else
  164. cout << "FAILED: ";
  165. pass = false;
  166. #endif
  167. cout << "Your machine is big endian.\n";
  168. }
  169. else
  170. {
  171. cout << "FAILED: Your machine is neither big endian nor little endian.\n";
  172. pass = false;
  173. }
  174. #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
  175. byte testvals[10] = {1,2,2,3,3,3,3,2,2,1};
  176. if (*(word32 *)(testvals+3) == 0x03030303 && *(word64 *)(testvals+1) == W64LIT(0x0202030303030202))
  177. cout << "passed: Your machine allows unaligned data access.\n";
  178. else
  179. {
  180. cout << "FAILED: Unaligned data access gave incorrect results.\n";
  181. pass = false;
  182. }
  183. #else
  184. cout << "passed: CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is not defined. Will restrict to aligned data access.\n";
  185. #endif
  186. if (sizeof(byte) == 1)
  187. cout << "passed: ";
  188. else
  189. {
  190. cout << "FAILED: ";
  191. pass = false;
  192. }
  193. cout << "sizeof(byte) == " << sizeof(byte) << endl;
  194. if (sizeof(word16) == 2)
  195. cout << "passed: ";
  196. else
  197. {
  198. cout << "FAILED: ";
  199. pass = false;
  200. }
  201. cout << "sizeof(word16) == " << sizeof(word16) << endl;
  202. if (sizeof(word32) == 4)
  203. cout << "passed: ";
  204. else
  205. {
  206. cout << "FAILED: ";
  207. pass = false;
  208. }
  209. cout << "sizeof(word32) == " << sizeof(word32) << endl;
  210. if (sizeof(word64) == 8)
  211. cout << "passed: ";
  212. else
  213. {
  214. cout << "FAILED: ";
  215. pass = false;
  216. }
  217. cout << "sizeof(word64) == " << sizeof(word64) << endl;
  218. #ifdef CRYPTOPP_WORD128_AVAILABLE
  219. if (sizeof(word128) == 16)
  220. cout << "passed: ";
  221. else
  222. {
  223. cout << "FAILED: ";
  224. pass = false;
  225. }
  226. cout << "sizeof(word128) == " << sizeof(word128) << endl;
  227. #endif
  228. if (sizeof(word) == 2*sizeof(hword)
  229. #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE
  230. && sizeof(dword) == 2*sizeof(word)
  231. #endif
  232. )
  233. cout << "passed: ";
  234. else
  235. {
  236. cout << "FAILED: ";
  237. pass = false;
  238. }
  239. cout << "sizeof(hword) == " << sizeof(hword) << ", sizeof(word) == " << sizeof(word);
  240. #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE
  241. cout << ", sizeof(dword) == " << sizeof(dword);
  242. #endif
  243. cout << endl;
  244. #ifdef CRYPTOPP_CPUID_AVAILABLE
  245. bool hasMMX = HasMMX();
  246. bool hasISSE = HasISSE();
  247. bool hasSSE2 = HasSSE2();
  248. bool hasSSSE3 = HasSSSE3();
  249. bool isP4 = IsP4();
  250. int cacheLineSize = GetCacheLineSize();
  251. if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize)))
  252. {
  253. cout << "FAILED: ";
  254. pass = false;
  255. }
  256. else
  257. cout << "passed: ";
  258. cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasAESNI == " << HasAESNI() << ", hasRDRAND == " << HasRDRAND() << ", hasRDSEED == " << HasRDSEED() << ", hasCLMUL == " << HasCLMUL() << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize;
  259. cout << ", AESNI_INTRINSICS == " << CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE << endl;
  260. #endif
  261. if (!pass)
  262. {
  263. cout << "Some critical setting in config.h is in error. Please fix it and recompile." << endl;
  264. abort();
  265. }
  266. return pass;
  267. }
  268. bool TestOS_RNG()
  269. {
  270. bool pass = true;
  271. member_ptr<RandomNumberGenerator> rng;
  272. #ifdef BLOCKING_RNG_AVAILABLE
  273. try {rng.reset(new BlockingRng);}
  274. catch (OS_RNG_Err &) {}
  275. #endif
  276. if (rng.get())
  277. {
  278. cout << "\nTesting operating system provided blocking random number generator...\n\n";
  279. MeterFilter meter(new Redirector(TheBitBucket()));
  280. RandomNumberSource test(*rng, UINT_MAX, false, new Deflator(new Redirector(meter)));
  281. unsigned long total=0, length=0;
  282. time_t t = time(NULL), t1 = 0;
  283. CRYPTOPP_UNUSED(length);
  284. // check that it doesn't take too long to generate a reasonable amount of randomness
  285. while (total < 16 && (t1 < 10 || total*8 > (unsigned long)t1))
  286. {
  287. test.Pump(1);
  288. total += 1;
  289. t1 = time(NULL) - t;
  290. }
  291. if (total < 16)
  292. {
  293. cout << "FAILED:";
  294. pass = false;
  295. }
  296. else
  297. cout << "passed:";
  298. cout << " it took " << long(t1) << " seconds to generate " << total << " bytes" << endl;
  299. #if 0 // disable this part. it's causing an unpredictable pause during the validation testing
  300. if (t1 < 2)
  301. {
  302. // that was fast, are we really blocking?
  303. // first exhaust the extropy reserve
  304. t = time(NULL);
  305. while (time(NULL) - t < 2)
  306. {
  307. test.Pump(1);
  308. total += 1;
  309. }
  310. // if it generates too many bytes in a certain amount of time,
  311. // something's probably wrong
  312. t = time(NULL);
  313. while (time(NULL) - t < 2)
  314. {
  315. test.Pump(1);
  316. total += 1;
  317. length += 1;
  318. }
  319. if (length > 1024)
  320. {
  321. cout << "FAILED:";
  322. pass = false;
  323. }
  324. else
  325. cout << "passed:";
  326. cout << " it generated " << length << " bytes in " << long(time(NULL) - t) << " seconds" << endl;
  327. }
  328. #endif
  329. test.AttachedTransformation()->MessageEnd();
  330. if (meter.GetTotalBytes() < total)
  331. {
  332. cout << "FAILED:";
  333. pass = false;
  334. }
  335. else
  336. cout << "passed:";
  337. cout << " " << total << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << endl;
  338. }
  339. else
  340. cout << "\nNo operating system provided blocking random number generator, skipping test." << endl;
  341. rng.reset(NULL);
  342. #ifdef NONBLOCKING_RNG_AVAILABLE
  343. try {rng.reset(new NonblockingRng);}
  344. catch (OS_RNG_Err &) {}
  345. #endif
  346. if (rng.get())
  347. {
  348. cout << "\nTesting operating system provided nonblocking random number generator...\n\n";
  349. MeterFilter meter(new Redirector(TheBitBucket()));
  350. RandomNumberSource test(*rng, 100000, true, new Deflator(new Redirector(meter)));
  351. if (meter.GetTotalBytes() < 100000)
  352. {
  353. cout << "FAILED:";
  354. pass = false;
  355. }
  356. else
  357. cout << "passed:";
  358. cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << endl;
  359. }
  360. else
  361. cout << "\nNo operating system provided nonblocking random number generator, skipping test." << endl;
  362. return pass;
  363. }
  364. #if NO_OS_DEPENDENCE
  365. bool TestAutoSeeded()
  366. {
  367. return true;
  368. }
  369. #else
  370. bool TestAutoSeeded()
  371. {
  372. // This tests Auto-Seeding and GenerateIntoBufferedTransformation.
  373. cout << "\nTesting AutoSeeded generator...\n\n";
  374. AutoSeededRandomPool prng;
  375. bool generate = true, discard = true;
  376. MeterFilter meter(new Redirector(TheBitBucket()));
  377. RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter)));
  378. if (meter.GetTotalBytes() < 100000)
  379. {
  380. cout << "FAILED:";
  381. generate = false;
  382. }
  383. else
  384. cout << "passed:";
  385. cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << endl;
  386. try
  387. {
  388. prng.DiscardBytes(100000);
  389. }
  390. catch(const Exception&)
  391. {
  392. discard = false;
  393. }
  394. if (!discard)
  395. cout << "FAILED:";
  396. else
  397. cout << "passed:";
  398. cout << " discarded 10000 bytes" << endl;
  399. return generate && discard;
  400. }
  401. #endif // NO_OS_DEPENDENCE
  402. #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
  403. bool TestRDRAND()
  404. {
  405. RDRAND rdrand;
  406. bool entropy = true, compress = true, discard = true;
  407. static const unsigned int SIZE = 10000;
  408. if (HasRDRAND())
  409. {
  410. cout << "\nTesting RDRAND generator...\n\n";
  411. MeterFilter meter(new Redirector(TheBitBucket()));
  412. Deflator deflator(new Redirector(meter));
  413. MaurerRandomnessTest maurer;
  414. ChannelSwitch chsw;
  415. chsw.AddDefaultRoute(deflator);
  416. chsw.AddDefaultRoute(maurer);
  417. RandomNumberSource rns(rdrand, SIZE, true, new Redirector(chsw));
  418. deflator.Flush(true);
  419. assert(0 == maurer.BytesNeeded());
  420. const double mv = maurer.GetTestValue();
  421. if (mv < 0.98f)
  422. {
  423. cout << "FAILED:";
  424. entropy = false;
  425. }
  426. else
  427. cout << "passed:";
  428. const std::streamsize oldp = cout.precision(6);
  429. const std::ios::fmtflags oldf = cout.setf(std::ios::fixed, std::ios::floatfield);
  430. cout << " Maurer Randomness Test returned value " << mv << endl;
  431. cout.precision(oldp);
  432. cout.setf(oldf, std::ios::floatfield);
  433. if (meter.GetTotalBytes() < SIZE)
  434. {
  435. cout << "FAILED:";
  436. compress = false;
  437. }
  438. else
  439. cout << "passed:";
  440. cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
  441. try
  442. {
  443. rdrand.DiscardBytes(SIZE);
  444. }
  445. catch(const Exception&)
  446. {
  447. discard = false;
  448. }
  449. if (!discard)
  450. cout << "FAILED:";
  451. else
  452. cout << "passed:";
  453. cout << " discarded " << SIZE << " bytes\n";
  454. }
  455. else
  456. cout << "\nRDRAND generator not available, skipping test.\n";
  457. if (!(entropy && compress && discard))
  458. cout.flush();
  459. return entropy && compress && discard;
  460. }
  461. #endif
  462. #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
  463. bool TestRDSEED()
  464. {
  465. RDSEED rdseed;
  466. bool entropy = true, compress = true, discard = true;
  467. static const unsigned int SIZE = 10000;
  468. if (HasRDSEED())
  469. {
  470. cout << "\nTesting RDSEED generator...\n\n";
  471. MeterFilter meter(new Redirector(TheBitBucket()));
  472. Deflator deflator(new Redirector(meter));
  473. MaurerRandomnessTest maurer;
  474. ChannelSwitch chsw;
  475. chsw.AddDefaultRoute(deflator);
  476. chsw.AddDefaultRoute(maurer);
  477. RandomNumberSource rns(rdseed, SIZE, true, new Redirector(chsw));
  478. deflator.Flush(true);
  479. assert(0 == maurer.BytesNeeded());
  480. const double mv = maurer.GetTestValue();
  481. if (mv < 0.98f)
  482. {
  483. cout << "FAILED:";
  484. entropy = false;
  485. }
  486. else
  487. cout << "passed:";
  488. const std::streamsize oldp = cout.precision(6);
  489. const std::ios::fmtflags oldf = cout.setf(std::ios::fixed, std::ios::floatfield);
  490. cout << " Maurer Randomness Test returned value " << mv << endl;
  491. cout.precision(oldp);
  492. cout.setf(oldf, std::ios::floatfield);
  493. if (meter.GetTotalBytes() < SIZE)
  494. {
  495. cout << "FAILED:";
  496. compress = false;
  497. }
  498. else
  499. cout << "passed:";
  500. cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
  501. try
  502. {
  503. rdseed.DiscardBytes(SIZE);
  504. }
  505. catch(const Exception&)
  506. {
  507. discard = false;
  508. }
  509. if (!discard)
  510. cout << "FAILED:";
  511. else
  512. cout << "passed:";
  513. cout << " discarded " << SIZE << " bytes\n";
  514. }
  515. else
  516. cout << "\nRDSEED generator not available, skipping test.\n";
  517. if (!(entropy && compress && discard))
  518. cout.flush();
  519. return entropy && compress && discard;
  520. }
  521. #endif
  522. // VC50 workaround
  523. typedef auto_ptr<BlockTransformation> apbt;
  524. class CipherFactory
  525. {
  526. public:
  527. virtual unsigned int BlockSize() const =0;
  528. virtual unsigned int KeyLength() const =0;
  529. virtual apbt NewEncryption(const byte *key) const =0;
  530. virtual apbt NewDecryption(const byte *key) const =0;
  531. };
  532. template <class E, class D> class FixedRoundsCipherFactory : public CipherFactory
  533. {
  534. public:
  535. FixedRoundsCipherFactory(unsigned int keylen=0) : m_keylen(keylen?keylen:E::DEFAULT_KEYLENGTH) {}
  536. unsigned int BlockSize() const {return E::BLOCKSIZE;}
  537. unsigned int KeyLength() const {return m_keylen;}
  538. apbt NewEncryption(const byte *key) const
  539. {return apbt(new E(key, m_keylen));}
  540. apbt NewDecryption(const byte *key) const
  541. {return apbt(new D(key, m_keylen));}
  542. unsigned int m_keylen;
  543. };
  544. template <class E, class D> class VariableRoundsCipherFactory : public CipherFactory
  545. {
  546. public:
  547. VariableRoundsCipherFactory(unsigned int keylen=0, unsigned int rounds=0)
  548. : m_keylen(keylen ? keylen : E::DEFAULT_KEYLENGTH), m_rounds(rounds ? rounds : E::DEFAULT_ROUNDS) {}
  549. unsigned int BlockSize() const {return E::BLOCKSIZE;}
  550. unsigned int KeyLength() const {return m_keylen;}
  551. apbt NewEncryption(const byte *key) const
  552. {return apbt(new E(key, m_keylen, m_rounds));}
  553. apbt NewDecryption(const byte *key) const
  554. {return apbt(new D(key, m_keylen, m_rounds));}
  555. unsigned int m_keylen, m_rounds;
  556. };
  557. bool BlockTransformationTest(const CipherFactory &cg, BufferedTransformation &valdata, unsigned int tuples = 0xffff)
  558. {
  559. HexEncoder output(new FileSink(cout));
  560. SecByteBlock plain(cg.BlockSize()), cipher(cg.BlockSize()), out(cg.BlockSize()), outplain(cg.BlockSize());
  561. SecByteBlock key(cg.KeyLength());
  562. bool pass=true, fail;
  563. while (valdata.MaxRetrievable() && tuples--)
  564. {
  565. valdata.Get(key, cg.KeyLength());
  566. valdata.Get(plain, cg.BlockSize());
  567. valdata.Get(cipher, cg.BlockSize());
  568. apbt transE = cg.NewEncryption(key);
  569. transE->ProcessBlock(plain, out);
  570. fail = memcmp(out, cipher, cg.BlockSize()) != 0;
  571. apbt transD = cg.NewDecryption(key);
  572. transD->ProcessBlock(out, outplain);
  573. fail=fail || memcmp(outplain, plain, cg.BlockSize());
  574. pass = pass && !fail;
  575. cout << (fail ? "FAILED " : "passed ");
  576. output.Put(key, cg.KeyLength());
  577. cout << " ";
  578. output.Put(outplain, cg.BlockSize());
  579. cout << " ";
  580. output.Put(out, cg.BlockSize());
  581. cout << endl;
  582. }
  583. return pass;
  584. }
  585. class FilterTester : public Unflushable<Sink>
  586. {
  587. public:
  588. FilterTester(const byte *validOutput, size_t outputLen)
  589. : validOutput(validOutput), outputLen(outputLen), counter(0), fail(false) {}
  590. void PutByte(byte inByte)
  591. {
  592. if (counter >= outputLen || validOutput[counter] != inByte)
  593. {
  594. std::cerr << "incorrect output " << counter << ", " << (word16)validOutput[counter] << ", " << (word16)inByte << "\n";
  595. fail = true;
  596. assert(false);
  597. }
  598. counter++;
  599. }
  600. size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
  601. {
  602. CRYPTOPP_UNUSED(messageEnd), CRYPTOPP_UNUSED(blocking);
  603. while (length--)
  604. FilterTester::PutByte(*inString++);
  605. if (messageEnd)
  606. if (counter != outputLen)
  607. {
  608. fail = true;
  609. assert(false);
  610. }
  611. return 0;
  612. }
  613. bool GetResult()
  614. {
  615. return !fail;
  616. }
  617. const byte *validOutput;
  618. size_t outputLen, counter;
  619. bool fail;
  620. };
  621. bool TestFilter(BufferedTransformation &bt, const byte *in, size_t inLen, const byte *out, size_t outLen)
  622. {
  623. FilterTester *ft;
  624. bt.Attach(ft = new FilterTester(out, outLen));
  625. while (inLen)
  626. {
  627. size_t randomLen = GlobalRNG().GenerateWord32(0, (word32)inLen);
  628. bt.Put(in, randomLen);
  629. in += randomLen;
  630. inLen -= randomLen;
  631. }
  632. bt.MessageEnd();
  633. return ft->GetResult();
  634. }
  635. bool ValidateDES()
  636. {
  637. cout << "\nDES validation suite running...\n\n";
  638. FileSource valdata("TestData/descert.dat", true, new HexDecoder);
  639. bool pass = BlockTransformationTest(FixedRoundsCipherFactory<DESEncryption, DESDecryption>(), valdata);
  640. cout << "\nTesting EDE2, EDE3, and XEX3 variants...\n\n";
  641. FileSource valdata1("TestData/3desval.dat", true, new HexDecoder);
  642. pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE2_Encryption, DES_EDE2_Decryption>(), valdata1, 1) && pass;
  643. pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE3_Encryption, DES_EDE3_Decryption>(), valdata1, 1) && pass;
  644. pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_XEX3_Encryption, DES_XEX3_Decryption>(), valdata1, 1) && pass;
  645. return pass;
  646. }
  647. bool TestModeIV(SymmetricCipher &e, SymmetricCipher &d)
  648. {
  649. SecByteBlock lastIV, iv(e.IVSize());
  650. StreamTransformationFilter filter(e, new StreamTransformationFilter(d));
  651. // vector_ptr<byte> due to Enterprise Analysis finding on the stack based array.
  652. vector_ptr<byte> plaintext(20480);
  653. for (unsigned int i=1; i<20480; i*=2)
  654. {
  655. e.GetNextIV(GlobalRNG(), iv);
  656. if (iv == lastIV)
  657. return false;
  658. else
  659. lastIV = iv;
  660. e.Resynchronize(iv);
  661. d.Resynchronize(iv);
  662. unsigned int length = STDMAX(GlobalRNG().GenerateWord32(0, i), (word32)e.MinLastBlockSize());
  663. GlobalRNG().GenerateBlock(plaintext, length);
  664. if (!TestFilter(filter, plaintext, length, plaintext, length))
  665. return false;
  666. }
  667. return true;
  668. }
  669. bool ValidateCipherModes()
  670. {
  671. cout << "\nTesting DES modes...\n\n";
  672. const byte key[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
  673. const byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
  674. const byte plain[] = { // "Now is the time for all " without tailing 0
  675. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  676. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  677. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};
  678. DESEncryption desE(key);
  679. DESDecryption desD(key);
  680. bool pass=true, fail;
  681. {
  682. // from FIPS 81
  683. const byte encrypted[] = {
  684. 0x3f, 0xa4, 0x0e, 0x8a, 0x98, 0x4d, 0x48, 0x15,
  685. 0x6a, 0x27, 0x17, 0x87, 0xab, 0x88, 0x83, 0xf9,
  686. 0x89, 0x3d, 0x51, 0xec, 0x4b, 0x56, 0x3b, 0x53};
  687. ECB_Mode_ExternalCipher::Encryption modeE(desE);
  688. fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
  689. plain, sizeof(plain), encrypted, sizeof(encrypted));
  690. pass = pass && !fail;
  691. cout << (fail ? "FAILED " : "passed ") << "ECB encryption" << endl;
  692. ECB_Mode_ExternalCipher::Decryption modeD(desD);
  693. fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
  694. encrypted, sizeof(encrypted), plain, sizeof(plain));
  695. pass = pass && !fail;
  696. cout << (fail ? "FAILED " : "passed ") << "ECB decryption" << endl;
  697. }
  698. {
  699. // from FIPS 81
  700. const byte encrypted[] = {
  701. 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
  702. 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
  703. 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6};
  704. CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
  705. fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
  706. plain, sizeof(plain), encrypted, sizeof(encrypted));
  707. pass = pass && !fail;
  708. cout << (fail ? "FAILED " : "passed ") << "CBC encryption with no padding" << endl;
  709. CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
  710. fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
  711. encrypted, sizeof(encrypted), plain, sizeof(plain));
  712. pass = pass && !fail;
  713. cout << (fail ? "FAILED " : "passed ") << "CBC decryption with no padding" << endl;
  714. fail = !TestModeIV(modeE, modeD);
  715. pass = pass && !fail;
  716. cout << (fail ? "FAILED " : "passed ") << "CBC mode IV generation" << endl;
  717. }
  718. {
  719. // generated with Crypto++, matches FIPS 81
  720. // but has extra 8 bytes as result of padding
  721. const byte encrypted[] = {
  722. 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
  723. 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
  724. 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
  725. 0x62, 0xC1, 0x6A, 0x27, 0xE4, 0xFC, 0xF2, 0x77};
  726. CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
  727. fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
  728. plain, sizeof(plain), encrypted, sizeof(encrypted));
  729. pass = pass && !fail;
  730. cout << (fail ? "FAILED " : "passed ") << "CBC encryption with PKCS #7 padding" << endl;
  731. CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
  732. fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
  733. encrypted, sizeof(encrypted), plain, sizeof(plain));
  734. pass = pass && !fail;
  735. cout << (fail ? "FAILED " : "passed ") << "CBC decryption with PKCS #7 padding" << endl;
  736. }
  737. {
  738. // generated with Crypto++ 5.2, matches FIPS 81
  739. // but has extra 8 bytes as result of padding
  740. const byte encrypted[] = {
  741. 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
  742. 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
  743. 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
  744. 0xcf, 0xb7, 0xc7, 0x64, 0x0e, 0x7c, 0xd9, 0xa7};
  745. CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
  746. fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::ONE_AND_ZEROS_PADDING).Ref(),
  747. plain, sizeof(plain), encrypted, sizeof(encrypted));
  748. pass = pass && !fail;
  749. cout << (fail ? "FAILED " : "passed ") << "CBC encryption with one-and-zeros padding" << endl;
  750. CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
  751. fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::ONE_AND_ZEROS_PADDING).Ref(),
  752. encrypted, sizeof(encrypted), plain, sizeof(plain));
  753. pass = pass && !fail;
  754. cout << (fail ? "FAILED " : "passed ") << "CBC decryption with one-and-zeros padding" << endl;
  755. }
  756. {
  757. const byte plain_1[] = {'a', 0, 0, 0, 0, 0, 0, 0};
  758. // generated with Crypto++
  759. const byte encrypted[] = {
  760. 0x9B, 0x47, 0x57, 0x59, 0xD6, 0x9C, 0xF6, 0xD0};
  761. CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
  762. fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),
  763. plain_1, 1, encrypted, sizeof(encrypted));
  764. pass = pass && !fail;
  765. cout << (fail ? "FAILED " : "passed ") << "CBC encryption with zeros padding" << endl;
  766. CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
  767. fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),
  768. encrypted, sizeof(encrypted), plain_1, sizeof(plain_1));
  769. pass = pass && !fail;
  770. cout << (fail ? "FAILED " : "passed ") << "CBC decryption with zeros padding" << endl;
  771. }
  772. {
  773. // generated with Crypto++, matches FIPS 81
  774. // but with last two blocks swapped as result of CTS
  775. const byte encrypted[] = {
  776. 0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
  777. 0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
  778. 0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F};
  779. CBC_CTS_Mode_ExternalCipher::Encryption modeE(desE, iv);
  780. fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
  781. plain, sizeof(plain), encrypted, sizeof(encrypted));
  782. pass = pass && !fail;
  783. cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext stealing (CTS)" << endl;
  784. CBC_CTS_Mode_ExternalCipher::Decryption modeD(desD, iv);
  785. fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
  786. encrypted, sizeof(encrypted), plain, sizeof(plain));
  787. pass = pass && !fail;
  788. cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext stealing (CTS)" << endl;
  789. fail = !TestModeIV(modeE, modeD);
  790. pass = pass && !fail;
  791. cout << (fail ? "FAILED " : "passed ") << "CBC CTS IV generation" << endl;
  792. }
  793. {
  794. // generated with Crypto++
  795. const byte decryptionIV[] = {0x4D, 0xD0, 0xAC, 0x8F, 0x47, 0xCF, 0x79, 0xCE};
  796. const byte encrypted[] = {0x12, 0x34, 0x56};
  797. byte stolenIV[8];
  798. CBC_CTS_Mode_ExternalCipher::Encryption modeE(desE, iv);
  799. modeE.SetStolenIV(stolenIV);
  800. fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
  801. plain, 3, encrypted, sizeof(encrypted));
  802. fail = memcmp(stolenIV, decryptionIV, 8) != 0 || fail;
  803. pass = pass && !fail;
  804. cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext and IV stealing" << endl;
  805. CBC_CTS_Mode_ExternalCipher::Decryption modeD(desD, stolenIV);
  806. fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
  807. encrypted, sizeof(encrypted), plain, 3);
  808. pass = pass && !fail;
  809. cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext and IV stealing" << endl;
  810. }
  811. {
  812. const byte encrypted[] = { // from FIPS 81
  813. 0xF3,0x09,0x62,0x49,0xC7,0xF4,0x6E,0x51,
  814. 0xA6,0x9E,0x83,0x9B,0x1A,0x92,0xF7,0x84,
  815. 0x03,0x46,0x71,0x33,0x89,0x8E,0xA6,0x22};
  816. CFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
  817. fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
  818. plain, sizeof(plain), encrypted, sizeof(encrypted));
  819. pass = pass && !fail;
  820. cout << (fail ? "FAILED " : "passed ") << "CFB encryption" << endl;
  821. CFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
  822. fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
  823. encrypted, sizeof(encrypted), plain, sizeof(plain));
  824. pass = pass && !fail;
  825. cout << (fail ? "FAILED " : "passed ") << "CFB decryption" << endl;
  826. fail = !TestModeIV(modeE, modeD);
  827. pass = pass && !fail;
  828. cout << (fail ? "FAILED " : "passed ") << "CFB mode IV generation" << endl;
  829. }
  830. {
  831. const byte plain_2[] = { // "Now is the." without tailing 0
  832. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,0x68,0x65};
  833. const byte encrypted[] = { // from FIPS 81
  834. 0xf3,0x1f,0xda,0x07,0x01,0x14,0x62,0xee,0x18,0x7f};
  835. CFB_Mode_ExternalCipher::Encryption modeE(desE, iv, 1);
  836. fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
  837. plain_2, sizeof(plain_2), encrypted, sizeof(encrypted));
  838. pass = pass && !fail;
  839. cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) encryption" << endl;
  840. CFB_Mode_ExternalCipher::Decryption modeD(desE, iv, 1);
  841. fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
  842. encrypted, sizeof(encrypted), plain_2, sizeof(plain_2));
  843. pass = pass && !fail;
  844. cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) decryption" << endl;
  845. fail = !TestModeIV(modeE, modeD);
  846. pass = pass && !fail;
  847. cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) IV generation" << endl;
  848. }
  849. {
  850. const byte encrypted[] = { // from Eric Young's libdes
  851. 0xf3,0x09,0x62,0x49,0xc7,0xf4,0x6e,0x51,
  852. 0x35,0xf2,0x4a,0x24,0x2e,0xeb,0x3d,0x3f,
  853. 0x3d,0x6d,0x5b,0xe3,0x25,0x5a,0xf8,0xc3};
  854. OFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
  855. fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
  856. plain, sizeof(plain), encrypted, sizeof(encrypted));
  857. pass = pass && !fail;
  858. cout << (fail ? "FAILED " : "passed ") << "OFB encryption" << endl;
  859. OFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
  860. fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
  861. encrypted, sizeof(encrypted), plain, sizeof(plain));
  862. pass = pass && !fail;
  863. cout << (fail ? "FAILED " : "passed ") << "OFB decryption" << endl;
  864. fail = !TestModeIV(modeE, modeD);
  865. pass = pass && !fail;
  866. cout << (fail ? "FAILED " : "passed ") << "OFB IV generation" << endl;
  867. }
  868. {
  869. const byte encrypted[] = { // generated with Crypto++
  870. 0xF3, 0x09, 0x62, 0x49, 0xC7, 0xF4, 0x6E, 0x51,
  871. 0x16, 0x3A, 0x8C, 0xA0, 0xFF, 0xC9, 0x4C, 0x27,
  872. 0xFA, 0x2F, 0x80, 0xF4, 0x80, 0xB8, 0x6F, 0x75};
  873. CTR_Mode_ExternalCipher::Encryption modeE(desE, iv);
  874. fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
  875. plain, sizeof(plain), encrypted, sizeof(encrypted));
  876. pass = pass && !fail;
  877. cout << (fail ? "FAILED " : "passed ") << "Counter Mode encryption" << endl;
  878. CTR_Mode_ExternalCipher::Decryption modeD(desE, iv);
  879. fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
  880. encrypted, sizeof(encrypted), plain, sizeof(plain));
  881. pass = pass && !fail;
  882. cout << (fail ? "FAILED " : "passed ") << "Counter Mode decryption" << endl;
  883. fail = !TestModeIV(modeE, modeD);
  884. pass = pass && !fail;
  885. cout << (fail ? "FAILED " : "passed ") << "Counter Mode IV generation" << endl;
  886. }
  887. {
  888. const byte plain_3[] = { // "7654321 Now is the time for "
  889. 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,
  890. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  891. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  892. 0x66, 0x6f, 0x72, 0x20};
  893. const byte mac1[] = { // from FIPS 113
  894. 0xf1, 0xd3, 0x0f, 0x68, 0x49, 0x31, 0x2c, 0xa4};
  895. const byte mac2[] = { // generated with Crypto++
  896. 0x35, 0x80, 0xC5, 0xC4, 0x6B, 0x81, 0x24, 0xE2};
  897. CBC_MAC<DES> cbcmac(key);
  898. HashFilter cbcmacFilter(cbcmac);
  899. fail = !TestFilter(cbcmacFilter, plain_3, sizeof(plain_3), mac1, sizeof(mac1));
  900. pass = pass && !fail;
  901. cout << (fail ? "FAILED " : "passed ") << "CBC MAC" << endl;
  902. DMAC<DES> dmac(key);
  903. HashFilter dmacFilter(dmac);
  904. fail = !TestFilter(dmacFilter, plain_3, sizeof(plain_3), mac2, sizeof(mac2));
  905. pass = pass && !fail;
  906. cout << (fail ? "FAILED " : "passed ") << "DMAC" << endl;
  907. }
  908. {
  909. CTR_Mode<AES>::Encryption modeE(plain, 16, plain);
  910. CTR_Mode<AES>::Decryption modeD(plain, 16, plain);
  911. fail = !TestModeIV(modeE, modeD);
  912. pass = pass && !fail;
  913. cout << (fail ? "FAILED " : "passed ") << "AES CTR Mode" << endl;
  914. }
  915. {
  916. OFB_Mode<AES>::Encryption modeE(plain, 16, plain);
  917. OFB_Mode<AES>::Decryption modeD(plain, 16, plain);
  918. fail = !TestModeIV(modeE, modeD);
  919. pass = pass && !fail;
  920. cout << (fail ? "FAILED " : "passed ") << "AES OFB Mode" << endl;
  921. }
  922. {
  923. CFB_Mode<AES>::Encryption modeE(plain, 16, plain);
  924. CFB_Mode<AES>::Decryption modeD(plain, 16, plain);
  925. fail = !TestModeIV(modeE, modeD);
  926. pass = pass && !fail;
  927. cout << (fail ? "FAILED " : "passed ") << "AES CFB Mode" << endl;
  928. }
  929. {
  930. CBC_Mode<AES>::Encryption modeE(plain, 16, plain);
  931. CBC_Mode<AES>::Decryption modeD(plain, 16, plain);
  932. fail = !TestModeIV(modeE, modeD);
  933. pass = pass && !fail;
  934. cout << (fail ? "FAILED " : "passed ") << "AES CBC Mode" << endl;
  935. }
  936. return pass;
  937. }
  938. bool ValidateIDEA()
  939. {
  940. cout << "\nIDEA validation suite running...\n\n";
  941. FileSource valdata("TestData/ideaval.dat", true, new HexDecoder);
  942. return BlockTransformationTest(FixedRoundsCipherFactory<IDEAEncryption, IDEADecryption>(), valdata);
  943. }
  944. bool ValidateSAFER()
  945. {
  946. cout << "\nSAFER validation suite running...\n\n";
  947. FileSource valdata("TestData/saferval.dat", true, new HexDecoder);
  948. bool pass = true;
  949. pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(8,6), valdata, 4) && pass;
  950. pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(16,12), valdata, 4) && pass;
  951. pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(8,6), valdata, 4) && pass;
  952. pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(16,10), valdata, 4) && pass;
  953. return pass;
  954. }
  955. bool ValidateRC2()
  956. {
  957. cout << "\nRC2 validation suite running...\n\n";
  958. FileSource valdata("TestData/rc2val.dat", true, new HexDecoder);
  959. HexEncoder output(new FileSink(cout));
  960. SecByteBlock plain(RC2Encryption::BLOCKSIZE), cipher(RC2Encryption::BLOCKSIZE), out(RC2Encryption::BLOCKSIZE), outplain(RC2Encryption::BLOCKSIZE);
  961. SecByteBlock key(128);
  962. bool pass=true, fail;
  963. while (valdata.MaxRetrievable())
  964. {
  965. byte keyLen, effectiveLen;
  966. valdata.Get(keyLen);
  967. valdata.Get(effectiveLen);
  968. valdata.Get(key, keyLen);
  969. valdata.Get(plain, RC2Encryption::BLOCKSIZE);
  970. valdata.Get(cipher, RC2Encryption::BLOCKSIZE);
  971. apbt transE(new RC2Encryption(key, keyLen, effectiveLen));
  972. transE->ProcessBlock(plain, out);
  973. fail = memcmp(out, cipher, RC2Encryption::BLOCKSIZE) != 0;
  974. apbt transD(new RC2Decryption(key, keyLen, effectiveLen));
  975. transD->ProcessBlock(out, outplain);
  976. fail=fail || memcmp(outplain, plain, RC2Encryption::BLOCKSIZE);
  977. pass = pass && !fail;
  978. cout << (fail ? "FAILED " : "passed ");
  979. output.Put(key, keyLen);
  980. cout << " ";
  981. output.Put(outplain, RC2Encryption::BLOCKSIZE);
  982. cout << " ";
  983. output.Put(out, RC2Encryption::BLOCKSIZE);
  984. cout << endl;
  985. }
  986. return pass;
  987. }
  988. bool ValidateARC4()
  989. {
  990. unsigned char Key0[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
  991. unsigned char Input0[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
  992. unsigned char Output0[] = {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96};
  993. unsigned char Key1[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
  994. unsigned char Input1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  995. unsigned char Output1[]={0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79};
  996. unsigned char Key2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  997. unsigned char Input2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  998. unsigned char Output2[]={0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a};
  999. unsigned char Key3[]={0xef,0x01,0x23,0x45};
  1000. unsigned char Input3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  1001. unsigned char Output3[]={0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61};
  1002. unsigned char Key4[]={ 0x01,0x23,0x45,0x67,0x89,0xab, 0xcd,0xef };
  1003. unsigned char Input4[] =
  1004. {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1005. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1006. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1007. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1008. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1009. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1010. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1011. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1012. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1013. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1014. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1015. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1016. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1017. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1018. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1019. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1020. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1021. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1022. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1023. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1024. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1025. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1026. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1027. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1028. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1029. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1030. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1031. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1032. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1033. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1034. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1035. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1036. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1037. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1038. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1039. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1040. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1041. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1042. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1043. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1044. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1045. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1046. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1047. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1048. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1049. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1050. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1051. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1052. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1053. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1054. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  1055. 0x01};
  1056. unsigned char Output4[]= {
  1057. 0x75,0x95,0xc3,0xe6,0x11,0x4a,0x09,0x78,0x0c,0x4a,0xd4,
  1058. 0x52,0x33,0x8e,0x1f,0xfd,0x9a,0x1b,0xe9,0x49,0x8f,
  1059. 0x81,0x3d,0x76,0x53,0x34,0x49,0xb6,0x77,0x8d,0xca,
  1060. 0xd8,0xc7,0x8a,0x8d,0x2b,0xa9,0xac,0x66,0x08,0x5d,
  1061. 0x0e,0x53,0xd5,0x9c,0x26,0xc2,0xd1,0xc4,0x90,0xc1,
  1062. 0xeb,0xbe,0x0c,0xe6,0x6d,0x1b,0x6b,0x1b,0x13,0xb6,
  1063. 0xb9,0x19,0xb8,0x47,0xc2,0x5a,0x91,0x44,0x7a,0x95,
  1064. 0xe7,0x5e,0x4e,0xf1,0x67,0x79,0xcd,0xe8,0xbf,0x0a,
  1065. 0x95,0x85,0x0e,0x32,0xaf,0x96,0x89,0x44,0x4f,0xd3,
  1066. 0x77,0x10,0x8f,0x98,0xfd,0xcb,0xd4,0xe7,0x26,0x56,
  1067. 0x75,0x00,0x99,0x0b,0xcc,0x7e,0x0c,0xa3,0xc4,0xaa,
  1068. 0xa3,0x04,0xa3,0x87,0xd2,0x0f,0x3b,0x8f,0xbb,0xcd,
  1069. 0x42,0xa1,0xbd,0x31,0x1d,0x7a,0x43,0x03,0xdd,0xa5,
  1070. 0xab,0x07,0x88,0x96,0xae,0x80,0xc1,0x8b,0x0a,0xf6,
  1071. 0x6d,0xff,0x31,0x96,0x16,0xeb,0x78,0x4e,0x49,0x5a,
  1072. 0xd2,0xce,0x90,0xd7,0xf7,0x72,0xa8,0x17,0x47,0xb6,
  1073. 0x5f,0x62,0x09,0x3b,0x1e,0x0d,0xb9,0xe5,0xba,0x53,
  1074. 0x2f,0xaf,0xec,0x47,0x50,0x83,0x23,0xe6,0x71,0x32,
  1075. 0x7d,0xf9,0x44,0x44,0x32,0xcb,0x73,0x67,0xce,0xc8,
  1076. 0x2f,0x5d,0x44,0xc0,0xd0,0x0b,0x67,0xd6,0x50,0xa0,
  1077. 0x75,0xcd,0x4b,0x70,0xde,0xdd,0x77,0xeb,0x9b,0x10,
  1078. 0x23,0x1b,0x6b,0x5b,0x74,0x13,0x47,0x39,0x6d,0x62,
  1079. 0x89,0x74,0x21,0xd4,0x3d,0xf9,0xb4,0x2e,0x44,0x6e,
  1080. 0x35,0x8e,0x9c,0x11,0xa9,0xb2,0x18,0x4e,0xcb,0xef,
  1081. 0x0c,0xd8,0xe7,0xa8,0x77,0xef,0x96,0x8f,0x13,0x90,
  1082. 0xec,0x9b,0x3d,0x35,0xa5,0x58,0x5c,0xb0,0x09,0x29,
  1083. 0x0e,0x2f,0xcd,0xe7,0xb5,0xec,0x66,0xd9,0x08,0x4b,
  1084. 0xe4,0x40,0x55,0xa6,0x19,0xd9,0xdd,0x7f,0xc3,0x16,
  1085. 0x6f,0x94,0x87,0xf7,0xcb,0x27,0x29,0x12,0x42,0x64,
  1086. 0x45,0x99,0x85,0x14,0xc1,0x5d,0x53,0xa1,0x8c,0x86,
  1087. 0x4c,0xe3,0xa2,0xb7,0x55,0x57,0x93,0x98,0x81,0x26,
  1088. 0x52,0x0e,0xac,0xf2,0xe3,0x06,0x6e,0x23,0x0c,0x91,
  1089. 0xbe,0xe4,0xdd,0x53,0x04,0xf5,0xfd,0x04,0x05,0xb3,
  1090. 0x5b,0xd9,0x9c,0x73,0x13,0x5d,0x3d,0x9b,0xc3,0x35,
  1091. 0xee,0x04,0x9e,0xf6,0x9b,0x38,0x67,0xbf,0x2d,0x7b,
  1092. 0xd1,0xea,0xa5,0x95,0xd8,0xbf,0xc0,0x06,0x6f,0xf8,
  1093. 0xd3,0x15,0x09,0xeb,0x0c,0x6c,0xaa,0x00,0x6c,0x80,
  1094. 0x7a,0x62,0x3e,0xf8,0x4c,0x3d,0x33,0xc1,0x95,0xd2,
  1095. 0x3e,0xe3,0x20,0xc4,0x0d,0xe0,0x55,0x81,0x57,0xc8,
  1096. 0x22,0xd4,0xb8,0xc5,0x69,0xd8,0x49,0xae,0xd5,0x9d,
  1097. 0x4e,0x0f,0xd7,0xf3,0x79,0x58,0x6b,0x4b,0x7f,0xf6,
  1098. 0x84,0xed,0x6a,0x18,0x9f,0x74,0x86,0xd4,0x9b,0x9c,
  1099. 0x4b,0xad,0x9b,0xa2,0x4b,0x96,0xab,0xf9,0x24,0x37,
  1100. 0x2c,0x8a,0x8f,0xff,0xb1,0x0d,0x55,0x35,0x49,0x00,
  1101. 0xa7,0x7a,0x3d,0xb5,0xf2,0x05,0xe1,0xb9,0x9f,0xcd,
  1102. 0x86,0x60,0x86,0x3a,0x15,0x9a,0xd4,0xab,0xe4,0x0f,
  1103. 0xa4,0x89,0x34,0x16,0x3d,0xdd,0xe5,0x42,0xa6,0x58,
  1104. 0x55,0x40,0xfd,0x68,0x3c,0xbf,0xd8,0xc0,0x0f,0x12,
  1105. 0x12,0x9a,0x28,0x4d,0xea,0xcc,0x4c,0xde,0xfe,0x58,
  1106. 0xbe,0x71,0x37,0x54,0x1c,0x04,0x71,0x26,0xc8,0xd4,
  1107. 0x9e,0x27,0x55,0xab,0x18,0x1a,0xb7,0xe9,0x40,0xb0,
  1108. 0xc0};
  1109. // VC60 workaround: auto_ptr lacks reset()
  1110. member_ptr<Weak::ARC4> arc4;
  1111. bool pass=true, fail;
  1112. unsigned int i;
  1113. cout << "\nARC4 validation suite running...\n\n";
  1114. arc4.reset(new Weak::ARC4(Key0, sizeof(Key0)));
  1115. arc4->ProcessString(Input0, sizeof(Input0));
  1116. fail = memcmp(Input0, Output0, sizeof(Input0)) != 0;
  1117. cout << (fail ? "FAILED" : "passed") << " Test 0" << endl;
  1118. pass = pass && !fail;
  1119. arc4.reset(new Weak::ARC4(Key1, sizeof(Key1)));
  1120. arc4->ProcessString(Key1, Input1, sizeof(Key1));
  1121. fail = memcmp(Output1, Key1, sizeof(Key1)) != 0;
  1122. cout << (fail ? "FAILED" : "passed") << " Test 1" << endl;
  1123. pass = pass && !fail;
  1124. arc4.reset(new Weak::ARC4(Key2, sizeof(Key2)));
  1125. for (i=0, fail=false; i<sizeof(Input2); i++)
  1126. if (arc4->ProcessByte(Input2[i]) != Output2[i])
  1127. fail = true;
  1128. cout << (fail ? "FAILED" : "passed") << " Test 2" << endl;
  1129. pass = pass && !fail;
  1130. arc4.reset(new Weak::ARC4(Key3, sizeof(Key3)));
  1131. for (i=0, fail=false; i<sizeof(Input3); i++)
  1132. if (arc4->ProcessByte(Input3[i]) != Output3[i])
  1133. fail = true;
  1134. cout << (fail ? "FAILED" : "passed") << " Test 3" << endl;
  1135. pass = pass && !fail;
  1136. arc4.reset(new Weak::ARC4(Key4, sizeof(Key4)));
  1137. for (i=0, fail=false; i<sizeof(Input4); i++)
  1138. if (arc4->ProcessByte(Input4[i]) != Output4[i])
  1139. fail = true;
  1140. cout << (fail ? "FAILED" : "passed") << " Test 4" << endl;
  1141. pass = pass && !fail;
  1142. return pass;
  1143. }
  1144. bool ValidateRC5()
  1145. {
  1146. cout << "\nRC5 validation suite running...\n\n";
  1147. FileSource valdata("TestData/rc5val.dat", true, new HexDecoder);
  1148. return BlockTransformationTest(VariableRoundsCipherFactory<RC5Encryption, RC5Decryption>(16, 12), valdata);
  1149. }
  1150. bool ValidateRC6()
  1151. {
  1152. cout << "\nRC6 validation suite running...\n\n";
  1153. FileSource valdata("TestData/rc6val.dat", true, new HexDecoder);
  1154. bool pass = true;
  1155. pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(16), valdata, 2) && pass;
  1156. pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(24), valdata, 2) && pass;
  1157. pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(32), valdata, 2) && pass;
  1158. return pass;
  1159. }
  1160. bool ValidateMARS()
  1161. {
  1162. cout << "\nMARS validation suite running...\n\n";
  1163. FileSource valdata("TestData/marsval.dat", true, new HexDecoder);
  1164. bool pass = true;
  1165. pass = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(16), valdata, 4) && pass;
  1166. pass = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(24), valdata, 3) && pass;
  1167. pass = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(32), valdata, 2) && pass;
  1168. return pass;
  1169. }
  1170. bool ValidateRijndael()
  1171. {
  1172. cout << "\nRijndael (AES) validation suite running...\n\n";
  1173. FileSource valdata("TestData/rijndael.dat", true, new HexDecoder);
  1174. bool pass = true;
  1175. pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(16), valdata, 4) && pass;
  1176. pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(24), valdata, 3) && pass;
  1177. pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(32), valdata, 2) && pass;
  1178. pass = RunTestDataFile("TestVectors/aes.txt") && pass;
  1179. return pass;
  1180. }
  1181. bool ValidateTwofish()
  1182. {
  1183. cout << "\nTwofish validation suite running...\n\n";
  1184. FileSource valdata("TestData/twofishv.dat", true, new HexDecoder);
  1185. bool pass = true;
  1186. pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(16), valdata, 4) && pass;
  1187. pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(24), valdata, 3) && pass;
  1188. pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(32), valdata, 2) && pass;
  1189. return pass;
  1190. }
  1191. bool ValidateSerpent()
  1192. {
  1193. cout << "\nSerpent validation suite running...\n\n";
  1194. FileSource valdata("TestData/serpentv.dat", true, new HexDecoder);
  1195. bool pass = true;
  1196. pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(16), valdata, 5) && pass;
  1197. pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(24), valdata, 4) && pass;
  1198. pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(32), valdata, 3) && pass;
  1199. return pass;
  1200. }
  1201. bool ValidateBlowfish()
  1202. {
  1203. cout << "\nBlowfish validation suite running...\n\n";
  1204. HexEncoder output(new FileSink(cout));
  1205. const char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"};
  1206. byte *plain[]={(byte *)"BLOWFISH", (byte *)"\xfe\xdc\xba\x98\x76\x54\x32\x10"};
  1207. byte *cipher[]={(byte *)"\x32\x4e\xd0\xfe\xf4\x13\xa2\x03", (byte *)"\xcc\x91\x73\x2b\x80\x22\xf6\x84"};
  1208. byte out[8], outplain[8];
  1209. bool pass=true, fail;
  1210. for (int i=0; i<2; i++)
  1211. {
  1212. ECB_Mode<Blowfish>::Encryption enc((byte *)key[i], strlen(key[i]));
  1213. enc.ProcessData(out, plain[i], 8);
  1214. fail = memcmp(out, cipher[i], 8) != 0;
  1215. ECB_Mode<Blowfish>::Decryption dec((byte *)key[i], strlen(key[i]));
  1216. dec.ProcessData(outplain, cipher[i], 8);
  1217. fail = fail || memcmp(outplain, plain[i], 8);
  1218. pass = pass && !fail;
  1219. cout << (fail ? "FAILED " : "passed ");
  1220. cout << '\"' << key[i] << '\"';
  1221. for (int j=0; j<(signed int)(30-strlen(key[i])); j++)
  1222. cout << ' ';
  1223. output.Put(outplain, 8);
  1224. cout << " ";
  1225. output.Put(out, 8);
  1226. cout << endl;
  1227. }
  1228. return pass;
  1229. }
  1230. bool ValidateThreeWay()
  1231. {
  1232. cout << "\n3-WAY validation suite running...\n\n";
  1233. FileSource valdata("TestData/3wayval.dat", true, new HexDecoder);
  1234. return BlockTransformationTest(FixedRoundsCipherFactory<ThreeWayEncryption, ThreeWayDecryption>(), valdata);
  1235. }
  1236. bool ValidateGOST()
  1237. {
  1238. cout << "\nGOST validation suite running...\n\n";
  1239. FileSource valdata("TestData/gostval.dat", true, new HexDecoder);
  1240. return BlockTransformationTest(FixedRoundsCipherFactory<GOSTEncryption, GOSTDecryption>(), valdata);
  1241. }
  1242. bool ValidateSHARK()
  1243. {
  1244. cout << "\nSHARK validation suite running...\n\n";
  1245. FileSource valdata("TestData/sharkval.dat", true, new HexDecoder);
  1246. return BlockTransformationTest(FixedRoundsCipherFactory<SHARKEncryption, SHARKDecryption>(), valdata);
  1247. }
  1248. bool ValidateCAST()
  1249. {
  1250. bool pass = true;
  1251. cout << "\nCAST-128 validation suite running...\n\n";
  1252. FileSource val128("TestData/cast128v.dat", true, new HexDecoder);
  1253. pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(16), val128, 1) && pass;
  1254. pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(10), val128, 1) && pass;
  1255. pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(5), val128, 1) && pass;
  1256. cout << "\nCAST-256 validation suite running...\n\n";
  1257. FileSource val256("TestData/cast256v.dat", true, new HexDecoder);
  1258. pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(16), val256, 1) && pass;
  1259. pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(24), val256, 1) && pass;
  1260. pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(32), val256, 1) && pass;
  1261. return pass;
  1262. }
  1263. bool ValidateSquare()
  1264. {
  1265. cout << "\nSquare validation suite running...\n\n";
  1266. FileSource valdata("TestData/squareva.dat", true, new HexDecoder);
  1267. return BlockTransformationTest(FixedRoundsCipherFactory<SquareEncryption, SquareDecryption>(), valdata);
  1268. }
  1269. bool ValidateSKIPJACK()
  1270. {
  1271. cout << "\nSKIPJACK validation suite running...\n\n";
  1272. FileSource valdata("TestData/skipjack.dat", true, new HexDecoder);
  1273. return BlockTransformationTest(FixedRoundsCipherFactory<SKIPJACKEncryption, SKIPJACKDecryption>(), valdata);
  1274. }
  1275. bool ValidateSEAL()
  1276. {
  1277. 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};
  1278. byte output[32];
  1279. byte key[] = {0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x98, 0xba, 0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76, 0xc3, 0xd2, 0xe1, 0xf0};
  1280. byte iv[] = {0x01, 0x35, 0x77, 0xaf};
  1281. cout << "\nSEAL validation suite running...\n\n";
  1282. SEAL<>::Encryption seal(key, sizeof(key), iv);
  1283. unsigned int size = sizeof(input);
  1284. bool pass = true;
  1285. memset(output, 1, size);
  1286. seal.ProcessString(output, input, size);
  1287. for (unsigned int i=0; i<size; i++)
  1288. if (output[i] != 0)
  1289. pass = false;
  1290. seal.Seek(1);
  1291. output[1] = seal.ProcessByte(output[1]);
  1292. seal.ProcessString(output+2, size-2);
  1293. pass = pass && memcmp(output+1, input+1, size-1) == 0;
  1294. cout << (pass ? "passed" : "FAILED") << endl;
  1295. return pass;
  1296. }
  1297. bool ValidateBaseCode()
  1298. {
  1299. bool pass = true, fail;
  1300. byte data[255];
  1301. for (unsigned int i=0; i<255; i++)
  1302. data[i] = byte(i);
  1303. static const char hexEncoded[] =
  1304. "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"
  1305. "28292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F"
  1306. "505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F7071727374757677"
  1307. "78797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F"
  1308. "A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7"
  1309. "C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
  1310. "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFE";
  1311. static const char base32Encoded[] =
  1312. "AAASEA2EAWDAQCAJBIFS2DIQB6IBCESVCSKTNF22DEPBYHA7D2RUAIJCENUCKJTHFAWUWK3NFWZC8NBT"
  1313. "GI3VIPJYG66DUQT5HS8V6R4AIFBEGTCFI3DWSUKKJPGE4VURKBIXEW4WKXMFQYC3MJPX2ZK8M7SGC2VD"
  1314. "NTUYN35IPFXGY5DPP3ZZA6MUQP4HK7VZRB6ZW856RX9H9AEBSKB2JBNGS8EIVCWMTUG27D6SUGJJHFEX"
  1315. "U4M3TGN4VQQJ5HW9WCS4FI7EWYVKRKFJXKX43MPQX82MDNXVYU45PP72ZG7MZRF7Z496BSQC2RCNMTYH"
  1316. "3DE6XU8N3ZHN9WGT4MJ7JXQY49NPVYY55VQ77Z9A6HTQH3HF65V8T4RK7RYQ55ZR8D29F69W8Z5RR8H3"
  1317. "9M7939R8";
  1318. const char *base64AndHexEncoded =
  1319. "41414543417751464267634943516F4C4441304F4478415245684D554652595847426B6147787764"
  1320. "486838674953496A4A43556D4A7967704B6973734C5334764D4445794D7A51310A4E6A63344F546F"
  1321. "375044302B50304242516B4E4552555A4853456C4B5330784E546B395155564A5456465657563168"
  1322. "5A576C746358563566594746695932526C5A6D646F615770720A6247317562334278636E4E306458"
  1323. "5A3365486C3665337839666E2B4167594B44684957476834694A696F754D6A5936506B4A47536B35"
  1324. "53566C7065596D5A71626E4A32656E3643680A6F714F6B7061616E714B6D717136797472712B7773"
  1325. "624B7A744C573274376935757275387662362F774D484377385446787366497963724C7A4D334F7A"
  1326. "39445230745055316462580A324E6E6132397A6433742F6734654C6A354F586D352B6A7036757673"
  1327. "3765377638504879382F5431397666342B6672372F50332B0A";
  1328. cout << "\nBase64, base32 and hex coding validation suite running...\n\n";
  1329. fail = !TestFilter(HexEncoder().Ref(), data, 255, (const byte *)hexEncoded, strlen(hexEncoded));
  1330. cout << (fail ? "FAILED " : "passed ");
  1331. cout << "Hex Encoding\n";
  1332. pass = pass && !fail;
  1333. fail = !TestFilter(HexDecoder().Ref(), (const byte *)hexEncoded, strlen(hexEncoded), data, 255);
  1334. cout << (fail ? "FAILED " : "passed ");
  1335. cout << "Hex Decoding\n";
  1336. pass = pass && !fail;
  1337. fail = !TestFilter(Base32Encoder().Ref(), data, 255, (const byte *)base32Encoded, strlen(base32Encoded));
  1338. cout << (fail ? "FAILED " : "passed ");
  1339. cout << "Base32 Encoding\n";
  1340. pass = pass && !fail;
  1341. fail = !TestFilter(Base32Decoder().Ref(), (const byte *)base32Encoded, strlen(base32Encoded), data, 255);
  1342. cout << (fail ? "FAILED " : "passed ");
  1343. cout << "Base32 Decoding\n";
  1344. pass = pass && !fail;
  1345. fail = !TestFilter(Base64Encoder(new HexEncoder).Ref(), data, 255, (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded));
  1346. cout << (fail ? "FAILED " : "passed ");
  1347. cout << "Base64 Encoding\n";
  1348. pass = pass && !fail;
  1349. fail = !TestFilter(HexDecoder(new Base64Decoder).Ref(), (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded), data, 255);
  1350. cout << (fail ? "FAILED " : "passed ");
  1351. cout << "Base64 Decoding\n";
  1352. pass = pass && !fail;
  1353. return pass;
  1354. }
  1355. bool ValidateSHACAL2()
  1356. {
  1357. cout << "\nSHACAL-2 validation suite running...\n\n";
  1358. bool pass = true;
  1359. FileSource valdata("TestData/shacal2v.dat", true, new HexDecoder);
  1360. pass = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(16), valdata, 4) && pass;
  1361. pass = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(64), valdata, 10) && pass;
  1362. return pass;
  1363. }
  1364. bool ValidateCamellia()
  1365. {
  1366. cout << "\nCamellia validation suite running...\n\n";
  1367. bool pass = true;
  1368. FileSource valdata("TestData/camellia.dat", true, new HexDecoder);
  1369. pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(16), valdata, 15) && pass;
  1370. pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(24), valdata, 15) && pass;
  1371. pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(32), valdata, 15) && pass;
  1372. return pass;
  1373. }
  1374. bool ValidateSalsa()
  1375. {
  1376. cout << "\nSalsa validation suite running...\n";
  1377. return RunTestDataFile("TestVectors/salsa.txt");
  1378. }
  1379. bool ValidateSosemanuk()
  1380. {
  1381. cout << "\nSosemanuk validation suite running...\n";
  1382. return RunTestDataFile("TestVectors/sosemanuk.txt");
  1383. }
  1384. bool ValidateVMAC()
  1385. {
  1386. cout << "\nVMAC validation suite running...\n";
  1387. return RunTestDataFile("TestVectors/vmac.txt");
  1388. }
  1389. bool ValidateCCM()
  1390. {
  1391. cout << "\nAES/CCM validation suite running...\n";
  1392. return RunTestDataFile("TestVectors/ccm.txt");
  1393. }
  1394. bool ValidateGCM()
  1395. {
  1396. cout << "\nAES/GCM validation suite running...\n";
  1397. cout << "\n2K tables:";
  1398. bool pass = RunTestDataFile("TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)2048));
  1399. cout << "\n64K tables:";
  1400. return RunTestDataFile("TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)64*1024)) && pass;
  1401. }
  1402. bool ValidateCMAC()
  1403. {
  1404. cout << "\nCMAC validation suite running...\n";
  1405. return RunTestDataFile("TestVectors/cmac.txt");
  1406. }