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