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