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  pass=TestAutoSeededX917() && pass;
70 
71 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
72  pass=TestRDRAND() && pass;
73  pass=TestRDSEED() && pass;
74 #endif
75 
76 #if !defined(NDEBUG) && !defined(CRYPTOPP_IMPORTS)
77  // http://github.com/weidai11/cryptopp/issues/92
78  pass=TestSecBlock() && pass;
79  // http://github.com/weidai11/cryptopp/issues/64
80  pass=TestPolynomialMod2() && pass;
81 #endif
82 
83  pass=ValidateCRC32() && pass;
84  pass=ValidateAdler32() && pass;
85  pass=ValidateMD2() && pass;
86  pass=ValidateMD5() && pass;
87  pass=ValidateSHA() && pass;
88  pass=RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/sha3.txt") && pass;
89  pass=ValidateTiger() && pass;
90  pass=ValidateRIPEMD() && pass;
91  pass=ValidatePanama() && pass;
92  pass=ValidateWhirlpool() && pass;
93 
94  pass=ValidateHMAC() && pass;
95  pass=ValidateTTMAC() && pass;
96 
97  pass=ValidatePBKDF() && pass;
98  pass=ValidateHKDF() && pass;
99 
100  pass=ValidateDES() && pass;
101  pass=ValidateCipherModes() && pass;
102  pass=ValidateIDEA() && pass;
103  pass=ValidateSAFER() && pass;
104  pass=ValidateRC2() && pass;
105  pass=ValidateARC4() && pass;
106  pass=ValidateRC5() && pass;
107  pass=ValidateBlowfish() && pass;
108  pass=ValidateThreeWay() && pass;
109  pass=ValidateGOST() && pass;
110  pass=ValidateSHARK() && pass;
111  pass=ValidateCAST() && pass;
112  pass=ValidateSquare() && pass;
113  pass=ValidateSKIPJACK() && pass;
114  pass=ValidateSEAL() && pass;
115  pass=ValidateRC6() && pass;
116  pass=ValidateMARS() && pass;
117  pass=ValidateRijndael() && pass;
118  pass=ValidateTwofish() && pass;
119  pass=ValidateSerpent() && pass;
120  pass=ValidateSHACAL2() && pass;
121  pass=ValidateCamellia() && pass;
122  pass=ValidateSalsa() && pass;
123  pass=ValidateSosemanuk() && pass;
124  pass=ValidateVMAC() && pass;
125  pass=ValidateCCM() && pass;
126  pass=ValidateGCM() && pass;
127  pass=ValidateCMAC() && pass;
128  pass=RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/eax.txt") && pass;
129  pass=RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/seed.txt") && pass;
130 
131  pass=ValidateBBS() && pass;
132  pass=ValidateDH() && pass;
133  pass=ValidateMQV() && pass;
134  pass=ValidateRSA() && pass;
135  pass=ValidateElGamal() && pass;
136  pass=ValidateDLIES() && pass;
137  pass=ValidateNR() && pass;
138  pass=ValidateDSA(thorough) && pass;
139  pass=ValidateLUC() && pass;
140  pass=ValidateLUC_DH() && pass;
141  pass=ValidateLUC_DL() && pass;
142  pass=ValidateXTR_DH() && pass;
143  pass=ValidateRabin() && pass;
144  pass=ValidateRW() && pass;
145 // pass=ValidateBlumGoldwasser() && pass;
146  pass=ValidateECP() && pass;
147  pass=ValidateEC2N() && pass;
148  pass=ValidateECDSA() && pass;
149  pass=ValidateESIGN() && pass;
150 
151  if (pass)
152  cout << "\nAll tests passed!\n";
153  else
154  cout << "\nOops! Not all tests passed.\n";
155 
156  return pass;
157 }
158 
159 bool TestSettings()
160 {
161  // Thanks to IlyaBizyaev and Zireael, http://github.com/weidai11/cryptopp/issues/28
162 #if defined(__MINGW32__)
163  using CryptoPP::memcpy_s;
164 #endif
165 
166  bool pass = true;
167 
168  cout << "\nTesting Settings...\n\n";
169 
170  word32 w;
171  memcpy_s(&w, sizeof(w), "\x01\x02\x03\x04", 4);
172 
173  if (w == 0x04030201L)
174  {
175 #ifdef IS_LITTLE_ENDIAN
176  cout << "passed: ";
177 #else
178  cout << "FAILED: ";
179  pass = false;
180 #endif
181  cout << "Your machine is little endian.\n";
182  }
183  else if (w == 0x01020304L)
184  {
185 #ifndef IS_LITTLE_ENDIAN
186  cout << "passed: ";
187 #else
188  cout << "FAILED: ";
189  pass = false;
190 #endif
191  cout << "Your machine is big endian.\n";
192  }
193  else
194  {
195  cout << "FAILED: Your machine is neither big endian nor little endian.\n";
196  pass = false;
197  }
198 
199 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
200  // Don't assert the alignment of testvals. That's what this test is for.
201  byte testvals[10] = {1,2,2,3,3,3,3,2,2,1};
202  if (*(word32 *)(void *)(testvals+3) == 0x03030303 && *(word64 *)(void *)(testvals+1) == W64LIT(0x0202030303030202))
203  cout << "passed: Your machine allows unaligned data access.\n";
204  else
205  {
206  cout << "FAILED: Unaligned data access gave incorrect results.\n";
207  pass = false;
208  }
209 #else
210  cout << "passed: CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is not defined. Will restrict to aligned data access.\n";
211 #endif
212 
213  if (sizeof(byte) == 1)
214  cout << "passed: ";
215  else
216  {
217  cout << "FAILED: ";
218  pass = false;
219  }
220  cout << "sizeof(byte) == " << sizeof(byte) << endl;
221 
222  if (sizeof(word16) == 2)
223  cout << "passed: ";
224  else
225  {
226  cout << "FAILED: ";
227  pass = false;
228  }
229  cout << "sizeof(word16) == " << sizeof(word16) << endl;
230 
231  if (sizeof(word32) == 4)
232  cout << "passed: ";
233  else
234  {
235  cout << "FAILED: ";
236  pass = false;
237  }
238  cout << "sizeof(word32) == " << sizeof(word32) << endl;
239 
240  if (sizeof(word64) == 8)
241  cout << "passed: ";
242  else
243  {
244  cout << "FAILED: ";
245  pass = false;
246  }
247  cout << "sizeof(word64) == " << sizeof(word64) << endl;
248 
249 #ifdef CRYPTOPP_WORD128_AVAILABLE
250  if (sizeof(word128) == 16)
251  cout << "passed: ";
252  else
253  {
254  cout << "FAILED: ";
255  pass = false;
256  }
257  cout << "sizeof(word128) == " << sizeof(word128) << endl;
258 #endif
259 
260  if (sizeof(word) == 2*sizeof(hword)
261 #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE
262  && sizeof(dword) == 2*sizeof(word)
263 #endif
264  )
265  cout << "passed: ";
266  else
267  {
268  cout << "FAILED: ";
269  pass = false;
270  }
271  cout << "sizeof(hword) == " << sizeof(hword) << ", sizeof(word) == " << sizeof(word);
272 #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE
273  cout << ", sizeof(dword) == " << sizeof(dword);
274 #endif
275  cout << endl;
276 
277 #ifdef CRYPTOPP_CPUID_AVAILABLE
278  bool hasMMX = HasMMX();
279  bool hasISSE = HasISSE();
280  bool hasSSE2 = HasSSE2();
281  bool hasSSSE3 = HasSSSE3();
282  bool isP4 = IsP4();
283  int cacheLineSize = GetCacheLineSize();
284 
285  if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize)))
286  {
287  cout << "FAILED: ";
288  pass = false;
289  }
290  else
291  cout << "passed: ";
292 
293  cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasAESNI == " << HasAESNI() << ", hasRDRAND == " << HasRDRAND() << ", hasRDSEED == " << HasRDSEED() << ", hasCLMUL == " << HasCLMUL() << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize;
294  cout << ", AESNI_INTRINSICS == " << CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE << endl;
295 #endif
296 
297  if (!pass)
298  {
299  cout << "Some critical setting in config.h is in error. Please fix it and recompile." << endl;
300  abort();
301  }
302  return pass;
303 }
304 
305 #if !defined(NDEBUG) && !defined(CRYPTOPP_IMPORTS)
306 bool TestSecBlock()
307 {
308  cout << "\nTesting SecBlock...\n\n";
309 
310  bool result = true, temp = true;
311 
312  //********** Zeroized block **********//
313 
314  // NULL ptr with a size means to create a new SecBloc with all elements zero'd
315  SecByteBlock z1(NULL, 256);
316  temp = true;
317 
318  for (size_t i = 0; i < z1.size(); i++)
319  temp &= (z1[i] == 0);
320 
321  result &= temp;
322  if (!temp)
323  cout << "FAILED:";
324  else
325  cout << "passed:";
326  cout << " Zeroized byte array" << endl;
327 
328  SecBlock<word32> z2(NULL, 256);
329  temp = true;
330 
331  for (size_t i = 0; i < z2.size(); i++)
332  temp &= (z2[i] == 0);
333 
334  result &= temp;
335  if (!temp)
336  cout << "FAILED:";
337  else
338  cout << "passed:";
339  cout << " Zeroized word32 array" << endl;
340 
341  //********** Assign **********//
342 
343  try
344  {
345  temp = true;
346  SecByteBlock a, b;
347  a.Assign((const byte*)"a", 1);
348  b.Assign((const byte*)"b", 1);
349 
350  temp &= (a.SizeInBytes() == 1);
351  temp &= (b.SizeInBytes() == 1);
352  temp &= (a[0] == 'a');
353  temp &= (b[0] == 'b');
354 
355  a.Assign((const byte*)"ab", 2);
356  b.Assign((const byte*)"cd", 2);
357 
358  temp &= (a.SizeInBytes() == 2);
359  temp &= (b.SizeInBytes() == 2);
360  temp &= (a[0] == 'a' && a[1] == 'b');
361  temp &= (b[0] == 'c' && b[1] == 'd');
362  }
363  catch(const Exception& /*ex*/)
364  {
365  temp = false;
366  }
367 
368  result &= temp;
369  if (!temp)
370  cout << "FAILED:";
371  else
372  cout << "passed:";
373  cout << " Assign byte" << endl;
374 
375  try
376  {
377  temp = true;
378  SecBlock<word32> a, b;
379  word32 one[1] = {1}, two[1] = {2};
380 
381  a.Assign(one, 1);
382  b.Assign(two, 1);
383 
384  temp &= (a.SizeInBytes() == 4);
385  temp &= (b.SizeInBytes() == 4);
386  temp &= (a[0] == 1);
387  temp &= (b[0] == 2);
388 
389  word32 three[2] = {1,2}, four[2] = {3,4};
390 
391  a.Assign(three, 2);
392  b.Assign(four, 2);
393 
394  temp &= (a.SizeInBytes() == 8);
395  temp &= (b.SizeInBytes() == 8);
396  temp &= (a[0] == 1 && a[1] == 2);
397  temp &= (b[0] == 3 && b[1] == 4);
398  }
399  catch(const Exception& /*ex*/)
400  {
401  temp = false;
402  }
403 
404  result &= temp;
405  if (!temp)
406  cout << "FAILED:";
407  else
408  cout << "passed:";
409  cout << " Assign word32" << endl;
410 
411  //********** Append **********//
412 
413  try
414  {
415  temp = true;
416  SecByteBlock a, b;
417  a.Assign((const byte*)"a", 1);
418  b.Assign((const byte*)"b", 1);
419 
420  a += b;
421  temp &= (a.SizeInBytes() == 2);
422  temp &= (a[0] == 'a' && a[1] == 'b');
423 
424  a.Assign((const byte*)"ab", 2);
425  b.Assign((const byte*)"cd", 2);
426 
427  a += b;
428  temp &= (a.SizeInBytes() == 4);
429  temp &= (a[0] == 'a' && a[1] == 'b' && a[2] == 'c' && a[3] == 'd');
430 
431  a.Assign((const byte*)"a", 1);
432 
433  a += a;
434  temp &= (a.SizeInBytes() == 2);
435  temp &= (a[0] == 'a' && a[1] == 'a');
436 
437  a.Assign((const byte*)"ab", 2);
438 
439  a += a;
440  temp &= (a.SizeInBytes() == 4);
441  temp &= (a[0] == 'a' && a[1] == 'b' && a[2] == 'a' && a[3] == 'b');
442  }
443  catch(const Exception& /*ex*/)
444  {
445  temp = false;
446  }
447 
448  result &= temp;
449  if (!temp)
450  cout << "FAILED:";
451  else
452  cout << "passed:";
453  cout << " Append byte" << endl;
454 
455  try
456  {
457  temp = true;
458  SecBlock<word32> a, b;
459  word32 one[1] = {1}, two[1] = {2};
460 
461  a.Assign(one, 1);
462  b.Assign(two, 1);
463 
464  a += b;
465  temp &= (a.SizeInBytes() == 8);
466  temp &= (a[0] == 1 && a[1] == 2);
467 
468  word32 three[2] = {1,2}, four[2] = {3,4};
469 
470  a.Assign(three, 2);
471  b.Assign(four, 2);
472 
473  a += b;
474  temp &= (a.SizeInBytes() == 16);
475  temp &= (a[0] == 1 && a[1] == 2 && a[2] == 3 && a[3] == 4);
476 
477  a.Assign(one, 1);
478 
479  a += a;
480  temp &= (a.SizeInBytes() == 8);
481  temp &= (a[0] == 1 && a[1] == 1);
482 
483  a.Assign(three, 2);
484 
485  a += a;
486  temp &= (a.SizeInBytes() == 16);
487  temp &= (a[0] == 1 && a[1] == 2 && a[2] == 1 && a[3] == 2);
488  }
489  catch(const Exception& /*ex*/)
490  {
491  temp = false;
492  }
493 
494  result &= temp;
495  if (!temp)
496  cout << "FAILED:";
497  else
498  cout << "passed:";
499  cout << " Append word32" << endl;
500 
501  //********** Concatenate **********//
502 
503  try
504  {
505  temp = true;
506  SecByteBlock a, b, c;
507  a.Assign((const byte*)"a", 1);
508  b.Assign((const byte*)"b", 1);
509 
510  c = a + b;
511  temp &= (a[0] == 'a');
512  temp &= (b[0] == 'b');
513  temp &= (c.SizeInBytes() == 2);
514  temp &= (c[0] == 'a' && c[1] == 'b');
515 
516  a.Assign((const byte*)"ab", 2);
517  b.Assign((const byte*)"cd", 2);
518 
519  c = a + b;
520  temp &= (a[0] == 'a' && a[1] == 'b');
521  temp &= (b[0] == 'c' && b[1] == 'd');
522  temp &= (c.SizeInBytes() == 4);
523  temp &= (c[0] == 'a' && c[1] == 'b' && c[2] == 'c' && c[3] == 'd');
524  }
525  catch(const Exception& /*ex*/)
526  {
527  temp = false;
528  }
529 
530  result &= temp;
531  if (!temp)
532  cout << "FAILED:";
533  else
534  cout << "passed:";
535  cout << " Concatenate byte" << endl;
536 
537  try
538  {
539  temp = true;
540  SecBlock<word32> a, b, c;
541  word32 one[1] = {1}, two[1] = {2};
542 
543  a.Assign(one, 1);
544  b.Assign(two, 1);
545 
546  c = a + b;
547  temp &= (a[0] == 1);
548  temp &= (b[0] == 2);
549  temp &= (c.SizeInBytes() == 8);
550  temp &= (c[0] == 1 && c[1] == 2);
551 
552  word32 three[2] = {1,2}, four[2] = {3,4};
553 
554  a.Assign(three, 2);
555  b.Assign(four, 2);
556 
557  c = a + b;
558  temp &= (a[0] == 1 && a[1] == 2);
559  temp &= (b[0] == 3 && b[1] == 4);
560  temp &= (c.SizeInBytes() == 16);
561  temp &= (c[0] == 1 && c[1] == 2 && c[2] == 3 && c[3] == 4);
562  }
563  catch(const Exception& /*ex*/)
564  {
565  temp = false;
566  }
567 
568  result &= temp;
569  if (!temp)
570  cout << "FAILED:";
571  else
572  cout << "passed:";
573  cout << " Concatenate word32" << endl;
574 
575  //********** Equality **********//
576 
577  try
578  {
579  static const byte str1[] = "abcdefghijklmnopqrstuvwxyz";
580  static const byte str2[] = "zyxwvutsrqponmlkjihgfedcba";
581  static const byte str3[] = "0123456789";
582 
583  temp = true;
584  SecByteBlock a,b;
585 
586  a.Assign(str1, COUNTOF(str1));
587  b.Assign(str1, COUNTOF(str1));
588  temp &= (a.operator==(b));
589 
590  a.Assign(str3, COUNTOF(str3));
591  b.Assign(str3, COUNTOF(str3));
592  temp &= (a == b);
593 
594  a.Assign(str1, COUNTOF(str1));
595  b.Assign(str2, COUNTOF(str2));
596  temp &= (a.operator!=(b));
597 
598  a.Assign(str1, COUNTOF(str1));
599  b.Assign(str3, COUNTOF(str3));
600  temp &= (a != b);
601  }
602  catch(const Exception& /*ex*/)
603  {
604  temp = false;
605  }
606 
607  result &= temp;
608  if (!temp)
609  cout << "FAILED:";
610  else
611  cout << "passed:";
612  cout << " Equality byte" << endl;
613 
614  try
615  {
616  static const word32 str1[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
617  static const word32 str2[] = {97,89,83,79,73,71,67,61,59,53,47,43,41,37,31,29,23,19,17,13,11,7,5,3,2};
618  static const word32 str3[] = {0,1,2,3,4,5,6,7,8,9};
619 
620  temp = true;
621  SecBlock<word32> a,b;
622 
623  a.Assign(str1, COUNTOF(str1));
624  b.Assign(str1, COUNTOF(str1));
625  temp &= (a.operator==(b));
626 
627  a.Assign(str3, COUNTOF(str3));
628  b.Assign(str3, COUNTOF(str3));
629  temp &= (a == b);
630 
631  a.Assign(str1, COUNTOF(str1));
632  b.Assign(str2, COUNTOF(str2));
633  temp &= (a.operator!=(b));
634 
635  a.Assign(str1, COUNTOF(str1));
636  b.Assign(str3, COUNTOF(str3));
637  temp &= (a != b);
638  }
639  catch(const Exception& /*ex*/)
640  {
641  temp = false;
642  }
643 
644  result &= temp;
645  if (!temp)
646  cout << "FAILED:";
647  else
648  cout << "passed:";
649  cout << " Equality word32" << endl;
650 
651  //********** Size/Overflow **********//
652 
653  try
654  {
655  temp = false;
657  const size_t max = A.max_size();
658  SecBlock<word32> t(max+1);
659  }
660  catch(const Exception& /*ex*/)
661  {
662  temp = true;
663  }
664  catch(const std::exception& /*ex*/)
665  {
666  temp = true;
667  }
668 
669  result &= temp;
670  if (!temp)
671  cout << "FAILED:";
672  else
673  cout << "passed:";
674  cout << " Overflow word32" << endl;
675 
676  try
677  {
678  temp = false;
680  const size_t max = A.max_size();
681  SecBlock<word64> t(max+1);
682  }
683  catch(const Exception& /*ex*/)
684  {
685  temp = true;
686  }
687  catch(const std::exception& /*ex*/)
688  {
689  temp = true;
690  }
691 
692  result &= temp;
693  if (!temp)
694  cout << "FAILED:";
695  else
696  cout << "passed:";
697  cout << " Overflow word64" << endl;
698 
699  //********** FixedSizeAllocatorWithCleanup and Grow **********//
700 
701  try
702  {
703  static const unsigned int SIZE = 8;
704  SecBlockWithHint<byte, SIZE> block(SIZE);
705  memset(block, 0xaa, block.SizeInBytes());
706 
707  temp = true;
708  block.CleanGrow(SIZE*2);
709  temp &= (block.size() == SIZE*2);
710 
711  for (size_t i = 0; i < block.size()/2; i++)
712  temp &= (block[i] == 0xaa);
713  for (size_t i = block.size()/2; i < block.size(); i++)
714  temp &= (block[i] == 0);
715 
716  block.CleanNew(SIZE*4);
717  temp &= (block.size() == SIZE*4);
718  for (size_t i = 0; i < block.size(); i++)
719  temp &= (block[i] == 0);
720 
721  result &= temp;
722  if (!temp)
723  cout << "FAILED:";
724  else
725  cout << "passed:";
726  cout << " FixedSizeAllocator and Grow with byte" << endl;
727  }
728  catch(const Exception& /*ex*/)
729  {
730  temp = false;
731  }
732  catch(const std::exception& /*ex*/)
733  {
734  temp = false;
735  }
736 
737  try
738  {
739  static const unsigned int SIZE = 8;
740  SecBlockWithHint<word32, SIZE> block(SIZE);
741  memset(block, 0xaa, block.SizeInBytes());
742 
743  temp = true;
744  block.CleanGrow(SIZE*2);
745  temp &= (block.size() == SIZE*2);
746 
747  for (size_t i = 0; i < block.size()/2; i++)
748  temp &= (block[i] == 0xaaaaaaaa);
749 
750  for (size_t i = block.size()/2; i < block.size(); i++)
751  temp &= (block[i] == 0);
752 
753  block.CleanNew(SIZE*4);
754  temp &= (block.size() == SIZE*4);
755  for (size_t i = 0; i < block.size(); i++)
756  temp &= (block[i] == 0);
757 
758  result &= temp;
759  if (!temp)
760  cout << "FAILED:";
761  else
762  cout << "passed:";
763  cout << " FixedSizeAllocator and Grow with word32" << endl;
764  }
765  catch(const Exception& /*ex*/)
766  {
767  temp = false;
768  }
769  catch(const std::exception& /*ex*/)
770  {
771  temp = false;
772  }
773 
774  return result;
775 }
776 #endif
777 
778 bool TestOS_RNG()
779 {
780  bool pass = true;
781 
783 
784 #ifdef BLOCKING_RNG_AVAILABLE
785  try {rng.reset(new BlockingRng);}
786  catch (OS_RNG_Err &) {}
787 #endif
788 
789  if (rng.get())
790  {
791  cout << "\nTesting operating system provided blocking random number generator...\n\n";
792 
793  MeterFilter meter(new Redirector(TheBitBucket()));
794  RandomNumberSource test(*rng, UINT_MAX, false, new Deflator(new Redirector(meter)));
795  unsigned long total=0, length=0;
796  time_t t = time(NULL), t1 = 0;
797  CRYPTOPP_UNUSED(length);
798 
799  // check that it doesn't take too long to generate a reasonable amount of randomness
800  while (total < 16 && (t1 < 10 || total*8 > (unsigned long)t1))
801  {
802  test.Pump(1);
803  total += 1;
804  t1 = time(NULL) - t;
805  }
806 
807  if (total < 16)
808  {
809  cout << "FAILED:";
810  pass = false;
811  }
812  else
813  cout << "passed:";
814  cout << " it took " << long(t1) << " seconds to generate " << total << " bytes" << endl;
815 
816 #if 0 // disable this part. it's causing an unpredictable pause during the validation testing
817  if (t1 < 2)
818  {
819  // that was fast, are we really blocking?
820  // first exhaust the extropy reserve
821  t = time(NULL);
822  while (time(NULL) - t < 2)
823  {
824  test.Pump(1);
825  total += 1;
826  }
827 
828  // if it generates too many bytes in a certain amount of time,
829  // something's probably wrong
830  t = time(NULL);
831  while (time(NULL) - t < 2)
832  {
833  test.Pump(1);
834  total += 1;
835  length += 1;
836  }
837  if (length > 1024)
838  {
839  cout << "FAILED:";
840  pass = false;
841  }
842  else
843  cout << "passed:";
844  cout << " it generated " << length << " bytes in " << long(time(NULL) - t) << " seconds" << endl;
845  }
846 #endif
847 
848  test.AttachedTransformation()->MessageEnd();
849 
850  if (meter.GetTotalBytes() < total)
851  {
852  cout << "FAILED:";
853  pass = false;
854  }
855  else
856  cout << "passed:";
857  cout << " " << total << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << endl;
858  }
859  else
860  cout << "\nNo operating system provided blocking random number generator, skipping test." << endl;
861 
862  rng.reset(NULL);
863 #ifdef NONBLOCKING_RNG_AVAILABLE
864  try {rng.reset(new NonblockingRng);}
865  catch (OS_RNG_Err &) {}
866 #endif
867 
868  if (rng.get())
869  {
870  cout << "\nTesting operating system provided nonblocking random number generator...\n\n";
871 
872  MeterFilter meter(new Redirector(TheBitBucket()));
873  RandomNumberSource test(*rng, 100000, true, new Deflator(new Redirector(meter)));
874 
875  if (meter.GetTotalBytes() < 100000)
876  {
877  cout << "FAILED:";
878  pass = false;
879  }
880  else
881  cout << "passed:";
882  cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << endl;
883  }
884  else
885  cout << "\nNo operating system provided nonblocking random number generator, skipping test." << endl;
886 
887  return pass;
888 }
889 
890 #if NO_OS_DEPENDENCE
891 bool TestAutoSeeded()
892 {
893  return true;
894 }
895 bool TestAutoSeededX917()
896 {
897  return true;
898 }
899 #else
900 bool TestAutoSeeded()
901 {
902  // This tests Auto-Seeding and GenerateIntoBufferedTransformation.
903  cout << "\nTesting AutoSeeded generator...\n\n";
904 
906  static const unsigned int ENTROPY_SIZE = 32;
907  bool generate = true, discard = true, incorporate = false;
908 
909  MeterFilter meter(new Redirector(TheBitBucket()));
910  RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter)));
911 
912  if (meter.GetTotalBytes() < 100000)
913  {
914  cout << "FAILED:";
915  generate = false;
916  }
917  else
918  cout << "passed:";
919  cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << endl;
920 
921  try
922  {
923  prng.DiscardBytes(100000);
924  }
925  catch(const Exception&)
926  {
927  discard = false;
928  }
929 
930  if (!discard)
931  cout << "FAILED:";
932  else
933  cout << "passed:";
934  cout << " discarded 10000 bytes" << endl;
935 
936  try
937  {
938  if(prng.CanIncorporateEntropy())
939  {
940  SecByteBlock entropy(ENTROPY_SIZE);
941  OS_GenerateRandomBlock(false, entropy, entropy.SizeInBytes());
942 
943  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
944  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
945  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
946  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
947 
948  incorporate = true;
949  }
950  }
951  catch(const Exception& /*ex*/)
952  {
953  }
954 
955  if (!incorporate)
956  cout << "FAILED:";
957  else
958  cout << "passed:";
959  cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << endl;
960 
961  return generate && discard && incorporate;
962 }
963 
964 bool TestAutoSeededX917()
965 {
966  // This tests Auto-Seeding and GenerateIntoBufferedTransformation.
967  cout << "\nTesting AutoSeeded X917 generator...\n\n";
968 
970  static const unsigned int ENTROPY_SIZE = 32;
971  bool generate = true, discard = true, incorporate = false;
972 
973  MeterFilter meter(new Redirector(TheBitBucket()));
974  RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter)));
975 
976  if (meter.GetTotalBytes() < 100000)
977  {
978  cout << "FAILED:";
979  generate = false;
980  }
981  else
982  cout << "passed:";
983  cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << endl;
984 
985  try
986  {
987  prng.DiscardBytes(100000);
988  }
989  catch(const Exception&)
990  {
991  discard = false;
992  }
993 
994  if (!discard)
995  cout << "FAILED:";
996  else
997  cout << "passed:";
998  cout << " discarded 10000 bytes" << endl;
999 
1000  try
1001  {
1002  if(prng.CanIncorporateEntropy())
1003  {
1004  SecByteBlock entropy(ENTROPY_SIZE);
1005  OS_GenerateRandomBlock(false, entropy, entropy.SizeInBytes());
1006 
1007  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1008  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1009  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1010  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1011 
1012  incorporate = true;
1013  }
1014  }
1015  catch(const Exception& /*ex*/)
1016  {
1017  }
1018 
1019  if (!incorporate)
1020  cout << "FAILED:";
1021  else
1022  cout << "passed:";
1023  cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << endl;
1024 
1025  return generate && discard && incorporate;
1026 }
1027 #endif // NO_OS_DEPENDENCE
1028 
1029 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
1030 bool TestRDRAND()
1031 {
1032  // Testing on 6th generation i7 shows RDRAND needs less than 8 retries for 10K bytes.
1033  RDRAND rdrand;
1034  bool entropy = true, compress = true, discard = true;
1035  static const unsigned int SIZE = 10000;
1036 
1037  if (HasRDRAND())
1038  {
1039  cout << "\nTesting RDRAND generator...\n\n";
1040 
1041  MeterFilter meter(new Redirector(TheBitBucket()));
1042  Deflator deflator(new Redirector(meter));
1043  MaurerRandomnessTest maurer;
1044 
1045  ChannelSwitch chsw;
1046  chsw.AddDefaultRoute(deflator);
1047  chsw.AddDefaultRoute(maurer);
1048 
1049  RandomNumberSource rns(rdrand, SIZE, true, new Redirector(chsw));
1050  deflator.Flush(true);
1051 
1052  assert(0 == maurer.BytesNeeded());
1053  const double mv = maurer.GetTestValue();
1054  if (mv < 0.98f)
1055  {
1056  cout << "FAILED:";
1057  entropy = false;
1058  }
1059  else
1060  cout << "passed:";
1061 
1062  // Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
1063  StreamState ss(cout);
1064  cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
1065  cout << " Maurer Randomness Test returned value " << mv << endl;
1066 
1067  if (meter.GetTotalBytes() < SIZE)
1068  {
1069  cout << "FAILED:";
1070  compress = false;
1071  }
1072  else
1073  cout << "passed:";
1074  cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
1075 
1076  try
1077  {
1078  rdrand.DiscardBytes(SIZE);
1079  }
1080  catch(const Exception&)
1081  {
1082  discard = false;
1083  }
1084 
1085  if (!discard)
1086  cout << "FAILED:";
1087  else
1088  cout << "passed:";
1089  cout << " discarded " << SIZE << " bytes\n";
1090  }
1091  else
1092  cout << "\nRDRAND generator not available, skipping test.\n";
1093 
1094  // Squash code coverage warnings on unused functions
1095  (void)rdrand.AlgorithmName();
1096  (void)rdrand.CanIncorporateEntropy();
1097  rdrand.SetRetries(rdrand.GetRetries());
1098  rdrand.IncorporateEntropy(NULL, 0);
1099 
1100  if (!(entropy && compress && discard))
1101  cout.flush();
1102 
1103  return entropy && compress && discard;
1104 }
1105 #endif
1106 
1107 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
1108 bool TestRDSEED()
1109 {
1110  // Testing on 5th generation i5 shows RDSEED needs about 128 retries for 10K bytes
1111  // on 64-bit/amd64 VM, and it needs more for an 32-bit/i686 VM.
1112  RDSEED rdseed(256);
1113  bool entropy = true, compress = true, discard = true;
1114  static const unsigned int SIZE = 10000;
1115 
1116  if (HasRDSEED())
1117  {
1118  cout << "\nTesting RDSEED generator...\n\n";
1119 
1120  MeterFilter meter(new Redirector(TheBitBucket()));
1121  Deflator deflator(new Redirector(meter));
1122  MaurerRandomnessTest maurer;
1123 
1124  ChannelSwitch chsw;
1125  chsw.AddDefaultRoute(deflator);
1126  chsw.AddDefaultRoute(maurer);
1127 
1128  RandomNumberSource rns(rdseed, SIZE, true, new Redirector(chsw));
1129  deflator.Flush(true);
1130 
1131  assert(0 == maurer.BytesNeeded());
1132  const double mv = maurer.GetTestValue();
1133  if (mv < 0.98f)
1134  {
1135  cout << "FAILED:";
1136  entropy = false;
1137  }
1138  else
1139  cout << "passed:";
1140 
1141  // Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
1142  StreamState ss(cout);
1143  cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
1144  cout << " Maurer Randomness Test returned value " << mv << endl;
1145 
1146  if (meter.GetTotalBytes() < SIZE)
1147  {
1148  cout << "FAILED:";
1149  compress = false;
1150  }
1151  else
1152  cout << "passed:";
1153  cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
1154 
1155  try
1156  {
1157  rdseed.DiscardBytes(SIZE);
1158  }
1159  catch(const Exception&)
1160  {
1161  discard = false;
1162  }
1163 
1164  if (!discard)
1165  cout << "FAILED:";
1166  else
1167  cout << "passed:";
1168  cout << " discarded " << SIZE << " bytes\n";
1169  }
1170  else
1171  cout << "\nRDSEED generator not available, skipping test.\n";
1172 
1173  // Squash code coverage warnings on unused functions
1174  (void)rdseed.AlgorithmName();
1175  (void)rdseed.CanIncorporateEntropy();
1176  rdseed.SetRetries(rdseed.GetRetries());
1177  rdseed.IncorporateEntropy(NULL, 0);
1178 
1179  if (!(entropy && compress && discard))
1180  cout.flush();
1181 
1182  return entropy && compress && discard;
1183 }
1184 #endif
1185 
1186 // VC50 workaround
1187 typedef auto_ptr<BlockTransformation> apbt;
1188 
1190 {
1191 public:
1192  virtual unsigned int BlockSize() const =0;
1193  virtual unsigned int KeyLength() const =0;
1194 
1195  virtual apbt NewEncryption(const byte *key) const =0;
1196  virtual apbt NewDecryption(const byte *key) const =0;
1197 };
1198 
1199 template <class E, class D> class FixedRoundsCipherFactory : public CipherFactory
1200 {
1201 public:
1202  FixedRoundsCipherFactory(unsigned int keylen=0) : m_keylen(keylen?keylen:E::DEFAULT_KEYLENGTH) {}
1203  unsigned int BlockSize() const {return E::BLOCKSIZE;}
1204  unsigned int KeyLength() const {return m_keylen;}
1205 
1206  apbt NewEncryption(const byte *key) const
1207  {return apbt(new E(key, m_keylen));}
1208  apbt NewDecryption(const byte *key) const
1209  {return apbt(new D(key, m_keylen));}
1210 
1211  unsigned int m_keylen;
1212 };
1213 
1214 template <class E, class D> class VariableRoundsCipherFactory : public CipherFactory
1215 {
1216 public:
1217  VariableRoundsCipherFactory(unsigned int keylen=0, unsigned int rounds=0)
1218  : m_keylen(keylen ? keylen : E::DEFAULT_KEYLENGTH), m_rounds(rounds ? rounds : E::DEFAULT_ROUNDS) {}
1219  unsigned int BlockSize() const {return E::BLOCKSIZE;}
1220  unsigned int KeyLength() const {return m_keylen;}
1221 
1222  apbt NewEncryption(const byte *key) const
1223  {return apbt(new E(key, m_keylen, m_rounds));}
1224  apbt NewDecryption(const byte *key) const
1225  {return apbt(new D(key, m_keylen, m_rounds));}
1226 
1227  unsigned int m_keylen, m_rounds;
1228 };
1229 
1230 bool BlockTransformationTest(const CipherFactory &cg, BufferedTransformation &valdata, unsigned int tuples = 0xffff)
1231 {
1232  HexEncoder output(new FileSink(cout));
1233  SecByteBlock plain(cg.BlockSize()), cipher(cg.BlockSize()), out(cg.BlockSize()), outplain(cg.BlockSize());
1234  SecByteBlock key(cg.KeyLength());
1235  bool pass=true, fail;
1236 
1237  while (valdata.MaxRetrievable() && tuples--)
1238  {
1239  valdata.Get(key, cg.KeyLength());
1240  valdata.Get(plain, cg.BlockSize());
1241  valdata.Get(cipher, cg.BlockSize());
1242 
1243  apbt transE = cg.NewEncryption(key);
1244  transE->ProcessBlock(plain, out);
1245  fail = memcmp(out, cipher, cg.BlockSize()) != 0;
1246 
1247  apbt transD = cg.NewDecryption(key);
1248  transD->ProcessBlock(out, outplain);
1249  fail=fail || memcmp(outplain, plain, cg.BlockSize());
1250 
1251  pass = pass && !fail;
1252 
1253  cout << (fail ? "FAILED " : "passed ");
1254  output.Put(key, cg.KeyLength());
1255  cout << " ";
1256  output.Put(outplain, cg.BlockSize());
1257  cout << " ";
1258  output.Put(out, cg.BlockSize());
1259  cout << endl;
1260  }
1261  return pass;
1262 }
1263 
1264 class FilterTester : public Unflushable<Sink>
1265 {
1266 public:
1267  FilterTester(const byte *validOutput, size_t outputLen)
1268  : validOutput(validOutput), outputLen(outputLen), counter(0), fail(false) {}
1269  void PutByte(byte inByte)
1270  {
1271  if (counter >= outputLen || validOutput[counter] != inByte)
1272  {
1273  std::cerr << "incorrect output " << counter << ", " << (word16)validOutput[counter] << ", " << (word16)inByte << "\n";
1274  fail = true;
1275  assert(false);
1276  }
1277  counter++;
1278  }
1279  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
1280  {
1281  CRYPTOPP_UNUSED(messageEnd), CRYPTOPP_UNUSED(blocking);
1282 
1283  while (length--)
1284  FilterTester::PutByte(*inString++);
1285 
1286  if (messageEnd)
1287  if (counter != outputLen)
1288  {
1289  fail = true;
1290  assert(false);
1291  }
1292 
1293  return 0;
1294  }
1295  bool GetResult()
1296  {
1297  return !fail;
1298  }
1299 
1300  const byte *validOutput;
1301  size_t outputLen, counter;
1302  bool fail;
1303 };
1304 
1305 bool TestFilter(BufferedTransformation &bt, const byte *in, size_t inLen, const byte *out, size_t outLen)
1306 {
1307  FilterTester *ft;
1308  bt.Attach(ft = new FilterTester(out, outLen));
1309 
1310  while (inLen)
1311  {
1312  size_t randomLen = GlobalRNG().GenerateWord32(0, (word32)inLen);
1313  bt.Put(in, randomLen);
1314  in += randomLen;
1315  inLen -= randomLen;
1316  }
1317  bt.MessageEnd();
1318  return ft->GetResult();
1319 }
1320 
1321 bool ValidateDES()
1322 {
1323  cout << "\nDES validation suite running...\n\n";
1324 
1325  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/descert.dat", true, new HexDecoder);
1326  bool pass = BlockTransformationTest(FixedRoundsCipherFactory<DESEncryption, DESDecryption>(), valdata);
1327 
1328  cout << "\nTesting EDE2, EDE3, and XEX3 variants...\n\n";
1329 
1330  FileSource valdata1(CRYPTOPP_DATA_DIR "TestData/3desval.dat", true, new HexDecoder);
1331  pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE2_Encryption, DES_EDE2_Decryption>(), valdata1, 1) && pass;
1332  pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE3_Encryption, DES_EDE3_Decryption>(), valdata1, 1) && pass;
1333  pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_XEX3_Encryption, DES_XEX3_Decryption>(), valdata1, 1) && pass;
1334 
1335  return pass;
1336 }
1337 
1338 bool TestModeIV(SymmetricCipher &e, SymmetricCipher &d)
1339 {
1340  SecByteBlock lastIV, iv(e.IVSize());
1342 
1343  // vector_ptr<byte> due to Enterprise Analysis finding on the stack based array.
1344  vector_ptr<byte> plaintext(20480);
1345 
1346  for (unsigned int i=1; i<20480; i*=2)
1347  {
1348  e.GetNextIV(GlobalRNG(), iv);
1349  if (iv == lastIV)
1350  return false;
1351  else
1352  lastIV = iv;
1353 
1354  e.Resynchronize(iv);
1355  d.Resynchronize(iv);
1356 
1357  unsigned int length = STDMAX(GlobalRNG().GenerateWord32(0, i), (word32)e.MinLastBlockSize());
1358  GlobalRNG().GenerateBlock(plaintext, length);
1359 
1360  if (!TestFilter(filter, plaintext, length, plaintext, length))
1361  return false;
1362  }
1363 
1364  return true;
1365 }
1366 
1367 bool ValidateCipherModes()
1368 {
1369  cout << "\nTesting DES modes...\n\n";
1370  const byte key[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
1371  const byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
1372  const byte plain[] = { // "Now is the time for all " without tailing 0
1373  0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
1374  0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
1375  0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};
1376  DESEncryption desE(key);
1377  DESDecryption desD(key);
1378  bool pass=true, fail;
1379 
1380  {
1381  // from FIPS 81
1382  const byte encrypted[] = {
1383  0x3f, 0xa4, 0x0e, 0x8a, 0x98, 0x4d, 0x48, 0x15,
1384  0x6a, 0x27, 0x17, 0x87, 0xab, 0x88, 0x83, 0xf9,
1385  0x89, 0x3d, 0x51, 0xec, 0x4b, 0x56, 0x3b, 0x53};
1386 
1388  fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
1389  plain, sizeof(plain), encrypted, sizeof(encrypted));
1390  pass = pass && !fail;
1391  cout << (fail ? "FAILED " : "passed ") << "ECB encryption" << endl;
1392 
1394  fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
1395  encrypted, sizeof(encrypted), plain, sizeof(plain));
1396  pass = pass && !fail;
1397  cout << (fail ? "FAILED " : "passed ") << "ECB decryption" << endl;
1398  }
1399  {
1400  // from FIPS 81
1401  const byte encrypted[] = {
1402  0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
1403  0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
1404  0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6};
1405 
1406  CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
1407  fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
1408  plain, sizeof(plain), encrypted, sizeof(encrypted));
1409  pass = pass && !fail;
1410  cout << (fail ? "FAILED " : "passed ") << "CBC encryption with no padding" << endl;
1411 
1412  CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
1413  fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
1414  encrypted, sizeof(encrypted), plain, sizeof(plain));
1415  pass = pass && !fail;
1416  cout << (fail ? "FAILED " : "passed ") << "CBC decryption with no padding" << endl;
1417 
1418  fail = !TestModeIV(modeE, modeD);
1419  pass = pass && !fail;
1420  cout << (fail ? "FAILED " : "passed ") << "CBC mode IV generation" << endl;
1421  }
1422  {
1423  // generated with Crypto++, matches FIPS 81
1424  // but has extra 8 bytes as result of padding
1425  const byte encrypted[] = {
1426  0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
1427  0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
1428  0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
1429  0x62, 0xC1, 0x6A, 0x27, 0xE4, 0xFC, 0xF2, 0x77};
1430 
1431  CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
1432  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
1433  plain, sizeof(plain), encrypted, sizeof(encrypted));
1434  pass = pass && !fail;
1435  cout << (fail ? "FAILED " : "passed ") << "CBC encryption with PKCS #7 padding" << endl;
1436 
1437  CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
1438  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
1439  encrypted, sizeof(encrypted), plain, sizeof(plain));
1440  pass = pass && !fail;
1441  cout << (fail ? "FAILED " : "passed ") << "CBC decryption with PKCS #7 padding" << endl;
1442  }
1443  {
1444  // generated with Crypto++ 5.2, matches FIPS 81
1445  // but has extra 8 bytes as result of padding
1446  const byte encrypted[] = {
1447  0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
1448  0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
1449  0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
1450  0xcf, 0xb7, 0xc7, 0x64, 0x0e, 0x7c, 0xd9, 0xa7};
1451 
1452  CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
1454  plain, sizeof(plain), encrypted, sizeof(encrypted));
1455  pass = pass && !fail;
1456  cout << (fail ? "FAILED " : "passed ") << "CBC encryption with one-and-zeros padding" << endl;
1457 
1458  CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
1460  encrypted, sizeof(encrypted), plain, sizeof(plain));
1461  pass = pass && !fail;
1462  cout << (fail ? "FAILED " : "passed ") << "CBC decryption with one-and-zeros padding" << endl;
1463  }
1464  {
1465  const byte plain_1[] = {'a', 0, 0, 0, 0, 0, 0, 0};
1466  // generated with Crypto++
1467  const byte encrypted[] = {
1468  0x9B, 0x47, 0x57, 0x59, 0xD6, 0x9C, 0xF6, 0xD0};
1469 
1470  CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
1471  fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),
1472  plain_1, 1, encrypted, sizeof(encrypted));
1473  pass = pass && !fail;
1474  cout << (fail ? "FAILED " : "passed ") << "CBC encryption with zeros padding" << endl;
1475 
1476  CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
1477  fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),
1478  encrypted, sizeof(encrypted), plain_1, sizeof(plain_1));
1479  pass = pass && !fail;
1480  cout << (fail ? "FAILED " : "passed ") << "CBC decryption with zeros padding" << endl;
1481  }
1482  {
1483  // generated with Crypto++, matches FIPS 81
1484  // but with last two blocks swapped as result of CTS
1485  const byte encrypted[] = {
1486  0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
1487  0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
1488  0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F};
1489 
1491  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
1492  plain, sizeof(plain), encrypted, sizeof(encrypted));
1493  pass = pass && !fail;
1494  cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext stealing (CTS)" << endl;
1495 
1497  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
1498  encrypted, sizeof(encrypted), plain, sizeof(plain));
1499  pass = pass && !fail;
1500  cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext stealing (CTS)" << endl;
1501 
1502  fail = !TestModeIV(modeE, modeD);
1503  pass = pass && !fail;
1504  cout << (fail ? "FAILED " : "passed ") << "CBC CTS IV generation" << endl;
1505  }
1506  {
1507  // generated with Crypto++
1508  const byte decryptionIV[] = {0x4D, 0xD0, 0xAC, 0x8F, 0x47, 0xCF, 0x79, 0xCE};
1509  const byte encrypted[] = {0x12, 0x34, 0x56};
1510 
1511  byte stolenIV[8];
1512 
1514  modeE.SetStolenIV(stolenIV);
1515  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
1516  plain, 3, encrypted, sizeof(encrypted));
1517  fail = memcmp(stolenIV, decryptionIV, 8) != 0 || fail;
1518  pass = pass && !fail;
1519  cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext and IV stealing" << endl;
1520 
1521  CBC_CTS_Mode_ExternalCipher::Decryption modeD(desD, stolenIV);
1522  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
1523  encrypted, sizeof(encrypted), plain, 3);
1524  pass = pass && !fail;
1525  cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext and IV stealing" << endl;
1526  }
1527  {
1528  const byte encrypted[] = { // from FIPS 81
1529  0xF3,0x09,0x62,0x49,0xC7,0xF4,0x6E,0x51,
1530  0xA6,0x9E,0x83,0x9B,0x1A,0x92,0xF7,0x84,
1531  0x03,0x46,0x71,0x33,0x89,0x8E,0xA6,0x22};
1532 
1533  CFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
1534  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
1535  plain, sizeof(plain), encrypted, sizeof(encrypted));
1536  pass = pass && !fail;
1537  cout << (fail ? "FAILED " : "passed ") << "CFB encryption" << endl;
1538 
1539  CFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
1540  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
1541  encrypted, sizeof(encrypted), plain, sizeof(plain));
1542  pass = pass && !fail;
1543  cout << (fail ? "FAILED " : "passed ") << "CFB decryption" << endl;
1544 
1545  fail = !TestModeIV(modeE, modeD);
1546  pass = pass && !fail;
1547  cout << (fail ? "FAILED " : "passed ") << "CFB mode IV generation" << endl;
1548  }
1549  {
1550  const byte plain_2[] = { // "Now is the." without tailing 0
1551  0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,0x68,0x65};
1552  const byte encrypted[] = { // from FIPS 81
1553  0xf3,0x1f,0xda,0x07,0x01,0x14,0x62,0xee,0x18,0x7f};
1554 
1555  CFB_Mode_ExternalCipher::Encryption modeE(desE, iv, 1);
1556  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
1557  plain_2, sizeof(plain_2), encrypted, sizeof(encrypted));
1558  pass = pass && !fail;
1559  cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) encryption" << endl;
1560 
1561  CFB_Mode_ExternalCipher::Decryption modeD(desE, iv, 1);
1562  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
1563  encrypted, sizeof(encrypted), plain_2, sizeof(plain_2));
1564  pass = pass && !fail;
1565  cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) decryption" << endl;
1566 
1567  fail = !TestModeIV(modeE, modeD);
1568  pass = pass && !fail;
1569  cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) IV generation" << endl;
1570  }
1571  {
1572  const byte encrypted[] = { // from Eric Young's libdes
1573  0xf3,0x09,0x62,0x49,0xc7,0xf4,0x6e,0x51,
1574  0x35,0xf2,0x4a,0x24,0x2e,0xeb,0x3d,0x3f,
1575  0x3d,0x6d,0x5b,0xe3,0x25,0x5a,0xf8,0xc3};
1576 
1577  OFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
1578  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
1579  plain, sizeof(plain), encrypted, sizeof(encrypted));
1580  pass = pass && !fail;
1581  cout << (fail ? "FAILED " : "passed ") << "OFB encryption" << endl;
1582 
1583  OFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
1584  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
1585  encrypted, sizeof(encrypted), plain, sizeof(plain));
1586  pass = pass && !fail;
1587  cout << (fail ? "FAILED " : "passed ") << "OFB decryption" << endl;
1588 
1589  fail = !TestModeIV(modeE, modeD);
1590  pass = pass && !fail;
1591  cout << (fail ? "FAILED " : "passed ") << "OFB IV generation" << endl;
1592  }
1593  {
1594  const byte encrypted[] = { // generated with Crypto++
1595  0xF3, 0x09, 0x62, 0x49, 0xC7, 0xF4, 0x6E, 0x51,
1596  0x16, 0x3A, 0x8C, 0xA0, 0xFF, 0xC9, 0x4C, 0x27,
1597  0xFA, 0x2F, 0x80, 0xF4, 0x80, 0xB8, 0x6F, 0x75};
1598 
1599  CTR_Mode_ExternalCipher::Encryption modeE(desE, iv);
1600  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
1601  plain, sizeof(plain), encrypted, sizeof(encrypted));
1602  pass = pass && !fail;
1603  cout << (fail ? "FAILED " : "passed ") << "Counter Mode encryption" << endl;
1604 
1605  CTR_Mode_ExternalCipher::Decryption modeD(desE, iv);
1606  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
1607  encrypted, sizeof(encrypted), plain, sizeof(plain));
1608  pass = pass && !fail;
1609  cout << (fail ? "FAILED " : "passed ") << "Counter Mode decryption" << endl;
1610 
1611  fail = !TestModeIV(modeE, modeD);
1612  pass = pass && !fail;
1613  cout << (fail ? "FAILED " : "passed ") << "Counter Mode IV generation" << endl;
1614  }
1615  {
1616  const byte plain_3[] = { // "7654321 Now is the time for "
1617  0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,
1618  0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
1619  0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
1620  0x66, 0x6f, 0x72, 0x20};
1621  const byte mac1[] = { // from FIPS 113
1622  0xf1, 0xd3, 0x0f, 0x68, 0x49, 0x31, 0x2c, 0xa4};
1623  const byte mac2[] = { // generated with Crypto++
1624  0x35, 0x80, 0xC5, 0xC4, 0x6B, 0x81, 0x24, 0xE2};
1625 
1626  CBC_MAC<DES> cbcmac(key);
1627  HashFilter cbcmacFilter(cbcmac);
1628  fail = !TestFilter(cbcmacFilter, plain_3, sizeof(plain_3), mac1, sizeof(mac1));
1629  pass = pass && !fail;
1630  cout << (fail ? "FAILED " : "passed ") << "CBC MAC" << endl;
1631 
1632  DMAC<DES> dmac(key);
1633  HashFilter dmacFilter(dmac);
1634  fail = !TestFilter(dmacFilter, plain_3, sizeof(plain_3), mac2, sizeof(mac2));
1635  pass = pass && !fail;
1636  cout << (fail ? "FAILED " : "passed ") << "DMAC" << endl;
1637  }
1638  {
1639  CTR_Mode<AES>::Encryption modeE(plain, 16, plain);
1640  CTR_Mode<AES>::Decryption modeD(plain, 16, plain);
1641  fail = !TestModeIV(modeE, modeD);
1642  pass = pass && !fail;
1643  cout << (fail ? "FAILED " : "passed ") << "AES CTR Mode" << endl;
1644  }
1645  {
1646  OFB_Mode<AES>::Encryption modeE(plain, 16, plain);
1647  OFB_Mode<AES>::Decryption modeD(plain, 16, plain);
1648  fail = !TestModeIV(modeE, modeD);
1649  pass = pass && !fail;
1650  cout << (fail ? "FAILED " : "passed ") << "AES OFB Mode" << endl;
1651  }
1652  {
1653  CFB_Mode<AES>::Encryption modeE(plain, 16, plain);
1654  CFB_Mode<AES>::Decryption modeD(plain, 16, plain);
1655  fail = !TestModeIV(modeE, modeD);
1656  pass = pass && !fail;
1657  cout << (fail ? "FAILED " : "passed ") << "AES CFB Mode" << endl;
1658  }
1659  {
1660  CBC_Mode<AES>::Encryption modeE(plain, 16, plain);
1661  CBC_Mode<AES>::Decryption modeD(plain, 16, plain);
1662  fail = !TestModeIV(modeE, modeD);
1663  pass = pass && !fail;
1664  cout << (fail ? "FAILED " : "passed ") << "AES CBC Mode" << endl;
1665  }
1666 
1667  return pass;
1668 }
1669 
1670 bool ValidateIDEA()
1671 {
1672  cout << "\nIDEA validation suite running...\n\n";
1673 
1674  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/ideaval.dat", true, new HexDecoder);
1675  return BlockTransformationTest(FixedRoundsCipherFactory<IDEAEncryption, IDEADecryption>(), valdata);
1676 }
1677 
1678 bool ValidateSAFER()
1679 {
1680  cout << "\nSAFER validation suite running...\n\n";
1681 
1682  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/saferval.dat", true, new HexDecoder);
1683  bool pass = true;
1684  pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(8,6), valdata, 4) && pass;
1685  pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(16,12), valdata, 4) && pass;
1686  pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(8,6), valdata, 4) && pass;
1687  pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(16,10), valdata, 4) && pass;
1688  return pass;
1689 }
1690 
1691 bool ValidateRC2()
1692 {
1693  cout << "\nRC2 validation suite running...\n\n";
1694 
1695  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rc2val.dat", true, new HexDecoder);
1696  HexEncoder output(new FileSink(cout));
1697  SecByteBlock plain(RC2Encryption::BLOCKSIZE), cipher(RC2Encryption::BLOCKSIZE), out(RC2Encryption::BLOCKSIZE), outplain(RC2Encryption::BLOCKSIZE);
1698  SecByteBlock key(128);
1699  bool pass=true, fail;
1700 
1701  while (valdata.MaxRetrievable())
1702  {
1703  byte keyLen, effectiveLen;
1704 
1705  valdata.Get(keyLen);
1706  valdata.Get(effectiveLen);
1707  valdata.Get(key, keyLen);
1708  valdata.Get(plain, RC2Encryption::BLOCKSIZE);
1709  valdata.Get(cipher, RC2Encryption::BLOCKSIZE);
1710 
1711  apbt transE(new RC2Encryption(key, keyLen, effectiveLen));
1712  transE->ProcessBlock(plain, out);
1713  fail = memcmp(out, cipher, RC2Encryption::BLOCKSIZE) != 0;
1714 
1715  apbt transD(new RC2Decryption(key, keyLen, effectiveLen));
1716  transD->ProcessBlock(out, outplain);
1717  fail=fail || memcmp(outplain, plain, RC2Encryption::BLOCKSIZE);
1718 
1719  pass = pass && !fail;
1720 
1721  cout << (fail ? "FAILED " : "passed ");
1722  output.Put(key, keyLen);
1723  cout << " ";
1724  output.Put(outplain, RC2Encryption::BLOCKSIZE);
1725  cout << " ";
1726  output.Put(out, RC2Encryption::BLOCKSIZE);
1727  cout << endl;
1728  }
1729  return pass;
1730 }
1731 
1732 bool ValidateARC4()
1733 {
1734  unsigned char Key0[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
1735  unsigned char Input0[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
1736  unsigned char Output0[] = {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96};
1737 
1738  unsigned char Key1[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
1739  unsigned char Input1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
1740  unsigned char Output1[]={0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79};
1741 
1742  unsigned char Key2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
1743  unsigned char Input2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
1744  unsigned char Output2[]={0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a};
1745 
1746  unsigned char Key3[]={0xef,0x01,0x23,0x45};
1747  unsigned char Input3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
1748  unsigned char Output3[]={0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61};
1749 
1750  unsigned char Key4[]={ 0x01,0x23,0x45,0x67,0x89,0xab, 0xcd,0xef };
1751  unsigned char Input4[] =
1752  {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1753  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1754  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1755  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1756  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1757  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1758  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1759  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1760  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1761  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1762  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1763  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1764  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1765  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1766  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1767  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1768  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1769  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1770  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1771  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1772  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1773  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1774  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1775  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1776  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1777  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1778  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1779  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1780  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1781  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1782  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1783  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1784  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1785  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1786  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1787  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1788  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1789  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1790  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1791  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1792  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1793  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1794  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1795  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1796  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1797  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1798  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1799  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1800  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1801  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1802  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
1803  0x01};
1804  unsigned char Output4[]= {
1805  0x75,0x95,0xc3,0xe6,0x11,0x4a,0x09,0x78,0x0c,0x4a,0xd4,
1806  0x52,0x33,0x8e,0x1f,0xfd,0x9a,0x1b,0xe9,0x49,0x8f,
1807  0x81,0x3d,0x76,0x53,0x34,0x49,0xb6,0x77,0x8d,0xca,
1808  0xd8,0xc7,0x8a,0x8d,0x2b,0xa9,0xac,0x66,0x08,0x5d,
1809  0x0e,0x53,0xd5,0x9c,0x26,0xc2,0xd1,0xc4,0x90,0xc1,
1810  0xeb,0xbe,0x0c,0xe6,0x6d,0x1b,0x6b,0x1b,0x13,0xb6,
1811  0xb9,0x19,0xb8,0x47,0xc2,0x5a,0x91,0x44,0x7a,0x95,
1812  0xe7,0x5e,0x4e,0xf1,0x67,0x79,0xcd,0xe8,0xbf,0x0a,
1813  0x95,0x85,0x0e,0x32,0xaf,0x96,0x89,0x44,0x4f,0xd3,
1814  0x77,0x10,0x8f,0x98,0xfd,0xcb,0xd4,0xe7,0x26,0x56,
1815  0x75,0x00,0x99,0x0b,0xcc,0x7e,0x0c,0xa3,0xc4,0xaa,
1816  0xa3,0x04,0xa3,0x87,0xd2,0x0f,0x3b,0x8f,0xbb,0xcd,
1817  0x42,0xa1,0xbd,0x31,0x1d,0x7a,0x43,0x03,0xdd,0xa5,
1818  0xab,0x07,0x88,0x96,0xae,0x80,0xc1,0x8b,0x0a,0xf6,
1819  0x6d,0xff,0x31,0x96,0x16,0xeb,0x78,0x4e,0x49,0x5a,
1820  0xd2,0xce,0x90,0xd7,0xf7,0x72,0xa8,0x17,0x47,0xb6,
1821  0x5f,0x62,0x09,0x3b,0x1e,0x0d,0xb9,0xe5,0xba,0x53,
1822  0x2f,0xaf,0xec,0x47,0x50,0x83,0x23,0xe6,0x71,0x32,
1823  0x7d,0xf9,0x44,0x44,0x32,0xcb,0x73,0x67,0xce,0xc8,
1824  0x2f,0x5d,0x44,0xc0,0xd0,0x0b,0x67,0xd6,0x50,0xa0,
1825  0x75,0xcd,0x4b,0x70,0xde,0xdd,0x77,0xeb,0x9b,0x10,
1826  0x23,0x1b,0x6b,0x5b,0x74,0x13,0x47,0x39,0x6d,0x62,
1827  0x89,0x74,0x21,0xd4,0x3d,0xf9,0xb4,0x2e,0x44,0x6e,
1828  0x35,0x8e,0x9c,0x11,0xa9,0xb2,0x18,0x4e,0xcb,0xef,
1829  0x0c,0xd8,0xe7,0xa8,0x77,0xef,0x96,0x8f,0x13,0x90,
1830  0xec,0x9b,0x3d,0x35,0xa5,0x58,0x5c,0xb0,0x09,0x29,
1831  0x0e,0x2f,0xcd,0xe7,0xb5,0xec,0x66,0xd9,0x08,0x4b,
1832  0xe4,0x40,0x55,0xa6,0x19,0xd9,0xdd,0x7f,0xc3,0x16,
1833  0x6f,0x94,0x87,0xf7,0xcb,0x27,0x29,0x12,0x42,0x64,
1834  0x45,0x99,0x85,0x14,0xc1,0x5d,0x53,0xa1,0x8c,0x86,
1835  0x4c,0xe3,0xa2,0xb7,0x55,0x57,0x93,0x98,0x81,0x26,
1836  0x52,0x0e,0xac,0xf2,0xe3,0x06,0x6e,0x23,0x0c,0x91,
1837  0xbe,0xe4,0xdd,0x53,0x04,0xf5,0xfd,0x04,0x05,0xb3,
1838  0x5b,0xd9,0x9c,0x73,0x13,0x5d,0x3d,0x9b,0xc3,0x35,
1839  0xee,0x04,0x9e,0xf6,0x9b,0x38,0x67,0xbf,0x2d,0x7b,
1840  0xd1,0xea,0xa5,0x95,0xd8,0xbf,0xc0,0x06,0x6f,0xf8,
1841  0xd3,0x15,0x09,0xeb,0x0c,0x6c,0xaa,0x00,0x6c,0x80,
1842  0x7a,0x62,0x3e,0xf8,0x4c,0x3d,0x33,0xc1,0x95,0xd2,
1843  0x3e,0xe3,0x20,0xc4,0x0d,0xe0,0x55,0x81,0x57,0xc8,
1844  0x22,0xd4,0xb8,0xc5,0x69,0xd8,0x49,0xae,0xd5,0x9d,
1845  0x4e,0x0f,0xd7,0xf3,0x79,0x58,0x6b,0x4b,0x7f,0xf6,
1846  0x84,0xed,0x6a,0x18,0x9f,0x74,0x86,0xd4,0x9b,0x9c,
1847  0x4b,0xad,0x9b,0xa2,0x4b,0x96,0xab,0xf9,0x24,0x37,
1848  0x2c,0x8a,0x8f,0xff,0xb1,0x0d,0x55,0x35,0x49,0x00,
1849  0xa7,0x7a,0x3d,0xb5,0xf2,0x05,0xe1,0xb9,0x9f,0xcd,
1850  0x86,0x60,0x86,0x3a,0x15,0x9a,0xd4,0xab,0xe4,0x0f,
1851  0xa4,0x89,0x34,0x16,0x3d,0xdd,0xe5,0x42,0xa6,0x58,
1852  0x55,0x40,0xfd,0x68,0x3c,0xbf,0xd8,0xc0,0x0f,0x12,
1853  0x12,0x9a,0x28,0x4d,0xea,0xcc,0x4c,0xde,0xfe,0x58,
1854  0xbe,0x71,0x37,0x54,0x1c,0x04,0x71,0x26,0xc8,0xd4,
1855  0x9e,0x27,0x55,0xab,0x18,0x1a,0xb7,0xe9,0x40,0xb0,
1856  0xc0};
1857 
1858  // VC60 workaround: auto_ptr lacks reset()
1860  bool pass=true, fail;
1861  unsigned int i;
1862 
1863  cout << "\nARC4 validation suite running...\n\n";
1864 
1865  arc4.reset(new Weak::ARC4(Key0, sizeof(Key0)));
1866  arc4->ProcessString(Input0, sizeof(Input0));
1867  fail = memcmp(Input0, Output0, sizeof(Input0)) != 0;
1868  cout << (fail ? "FAILED" : "passed") << " Test 0" << endl;
1869  pass = pass && !fail;
1870 
1871  arc4.reset(new Weak::ARC4(Key1, sizeof(Key1)));
1872  arc4->ProcessString(Key1, Input1, sizeof(Key1));
1873  fail = memcmp(Output1, Key1, sizeof(Key1)) != 0;
1874  cout << (fail ? "FAILED" : "passed") << " Test 1" << endl;
1875  pass = pass && !fail;
1876 
1877  arc4.reset(new Weak::ARC4(Key2, sizeof(Key2)));
1878  for (i=0, fail=false; i<sizeof(Input2); i++)
1879  if (arc4->ProcessByte(Input2[i]) != Output2[i])
1880  fail = true;
1881  cout << (fail ? "FAILED" : "passed") << " Test 2" << endl;
1882  pass = pass && !fail;
1883 
1884  arc4.reset(new Weak::ARC4(Key3, sizeof(Key3)));
1885  for (i=0, fail=false; i<sizeof(Input3); i++)
1886  if (arc4->ProcessByte(Input3[i]) != Output3[i])
1887  fail = true;
1888  cout << (fail ? "FAILED" : "passed") << " Test 3" << endl;
1889  pass = pass && !fail;
1890 
1891  arc4.reset(new Weak::ARC4(Key4, sizeof(Key4)));
1892  for (i=0, fail=false; i<sizeof(Input4); i++)
1893  if (arc4->ProcessByte(Input4[i]) != Output4[i])
1894  fail = true;
1895  cout << (fail ? "FAILED" : "passed") << " Test 4" << endl;
1896  pass = pass && !fail;
1897 
1898  return pass;
1899 }
1900 
1901 bool ValidateRC5()
1902 {
1903  cout << "\nRC5 validation suite running...\n\n";
1904 
1905  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rc5val.dat", true, new HexDecoder);
1906  return BlockTransformationTest(VariableRoundsCipherFactory<RC5Encryption, RC5Decryption>(16, 12), valdata);
1907 }
1908 
1909 bool ValidateRC6()
1910 {
1911  cout << "\nRC6 validation suite running...\n\n";
1912 
1913  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rc6val.dat", true, new HexDecoder);
1914  bool pass = true;
1915  pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(16), valdata, 2) && pass;
1916  pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(24), valdata, 2) && pass;
1917  pass = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(32), valdata, 2) && pass;
1918  return pass;
1919 }
1920 
1921 bool ValidateMARS()
1922 {
1923  cout << "\nMARS validation suite running...\n\n";
1924 
1925  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/marsval.dat", true, new HexDecoder);
1926  bool pass = true;
1927  pass = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(16), valdata, 4) && pass;
1928  pass = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(24), valdata, 3) && pass;
1929  pass = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(32), valdata, 2) && pass;
1930  return pass;
1931 }
1932 
1933 bool ValidateRijndael()
1934 {
1935  cout << "\nRijndael (AES) validation suite running...\n\n";
1936 
1937  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rijndael.dat", true, new HexDecoder);
1938  bool pass = true;
1939  pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(16), valdata, 4) && pass;
1940  pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(24), valdata, 3) && pass;
1941  pass = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(32), valdata, 2) && pass;
1942  pass = RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/aes.txt") && pass;
1943  return pass;
1944 }
1945 
1946 bool ValidateTwofish()
1947 {
1948  cout << "\nTwofish validation suite running...\n\n";
1949 
1950  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/twofishv.dat", true, new HexDecoder);
1951  bool pass = true;
1952  pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(16), valdata, 4) && pass;
1953  pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(24), valdata, 3) && pass;
1954  pass = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(32), valdata, 2) && pass;
1955  return pass;
1956 }
1957 
1958 bool ValidateSerpent()
1959 {
1960  cout << "\nSerpent validation suite running...\n\n";
1961 
1962  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/serpentv.dat", true, new HexDecoder);
1963  bool pass = true;
1964  pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(16), valdata, 5) && pass;
1965  pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(24), valdata, 4) && pass;
1966  pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(32), valdata, 3) && pass;
1967  return pass;
1968 }
1969 
1970 bool ValidateBlowfish()
1971 {
1972  cout << "\nBlowfish validation suite running...\n\n";
1973 
1974  HexEncoder output(new FileSink(cout));
1975  const char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"};
1976  byte *plain[]={(byte *)"BLOWFISH", (byte *)"\xfe\xdc\xba\x98\x76\x54\x32\x10"};
1977  byte *cipher[]={(byte *)"\x32\x4e\xd0\xfe\xf4\x13\xa2\x03", (byte *)"\xcc\x91\x73\x2b\x80\x22\xf6\x84"};
1978  byte out[8], outplain[8];
1979  bool pass=true, fail;
1980 
1981  for (int i=0; i<2; i++)
1982  {
1983  ECB_Mode<Blowfish>::Encryption enc((byte *)key[i], strlen(key[i]));
1984  enc.ProcessData(out, plain[i], 8);
1985  fail = memcmp(out, cipher[i], 8) != 0;
1986 
1987  ECB_Mode<Blowfish>::Decryption dec((byte *)key[i], strlen(key[i]));
1988  dec.ProcessData(outplain, cipher[i], 8);
1989  fail = fail || memcmp(outplain, plain[i], 8);
1990  pass = pass && !fail;
1991 
1992  cout << (fail ? "FAILED " : "passed ");
1993  cout << '\"' << key[i] << '\"';
1994  for (int j=0; j<(signed int)(30-strlen(key[i])); j++)
1995  cout << ' ';
1996  output.Put(outplain, 8);
1997  cout << " ";
1998  output.Put(out, 8);
1999  cout << endl;
2000  }
2001  return pass;
2002 }
2003 
2004 bool ValidateThreeWay()
2005 {
2006  cout << "\n3-WAY validation suite running...\n\n";
2007 
2008  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/3wayval.dat", true, new HexDecoder);
2009  return BlockTransformationTest(FixedRoundsCipherFactory<ThreeWayEncryption, ThreeWayDecryption>(), valdata);
2010 }
2011 
2012 bool ValidateGOST()
2013 {
2014  cout << "\nGOST validation suite running...\n\n";
2015 
2016  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/gostval.dat", true, new HexDecoder);
2017  return BlockTransformationTest(FixedRoundsCipherFactory<GOSTEncryption, GOSTDecryption>(), valdata);
2018 }
2019 
2020 bool ValidateSHARK()
2021 {
2022  cout << "\nSHARK validation suite running...\n\n";
2023 
2024  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/sharkval.dat", true, new HexDecoder);
2025  return BlockTransformationTest(FixedRoundsCipherFactory<SHARKEncryption, SHARKDecryption>(), valdata);
2026 }
2027 
2028 bool ValidateCAST()
2029 {
2030  bool pass = true;
2031 
2032  cout << "\nCAST-128 validation suite running...\n\n";
2033 
2034  FileSource val128(CRYPTOPP_DATA_DIR "TestData/cast128v.dat", true, new HexDecoder);
2035  pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(16), val128, 1) && pass;
2036  pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(10), val128, 1) && pass;
2037  pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(5), val128, 1) && pass;
2038 
2039  cout << "\nCAST-256 validation suite running...\n\n";
2040 
2041  FileSource val256(CRYPTOPP_DATA_DIR "TestData/cast256v.dat", true, new HexDecoder);
2042  pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(16), val256, 1) && pass;
2043  pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(24), val256, 1) && pass;
2044  pass = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(32), val256, 1) && pass;
2045 
2046  return pass;
2047 }
2048 
2049 bool ValidateSquare()
2050 {
2051  cout << "\nSquare validation suite running...\n\n";
2052 
2053  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/squareva.dat", true, new HexDecoder);
2054  return BlockTransformationTest(FixedRoundsCipherFactory<SquareEncryption, SquareDecryption>(), valdata);
2055 }
2056 
2057 bool ValidateSKIPJACK()
2058 {
2059  cout << "\nSKIPJACK validation suite running...\n\n";
2060 
2061  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/skipjack.dat", true, new HexDecoder);
2062  return BlockTransformationTest(FixedRoundsCipherFactory<SKIPJACKEncryption, SKIPJACKDecryption>(), valdata);
2063 }
2064 
2065 bool ValidateSEAL()
2066 {
2067  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};
2068  byte output[32];
2069  byte key[] = {0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x98, 0xba, 0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76, 0xc3, 0xd2, 0xe1, 0xf0};
2070  byte iv[] = {0x01, 0x35, 0x77, 0xaf};
2071 
2072  cout << "\nSEAL validation suite running...\n\n";
2073 
2074  SEAL<>::Encryption seal(key, sizeof(key), iv);
2075  unsigned int size = sizeof(input);
2076  bool pass = true;
2077 
2078  memset(output, 1, size);
2079  seal.ProcessString(output, input, size);
2080  for (unsigned int i=0; i<size; i++)
2081  if (output[i] != 0)
2082  pass = false;
2083 
2084  seal.Seek(1);
2085  output[1] = seal.ProcessByte(output[1]);
2086  seal.ProcessString(output+2, size-2);
2087  pass = pass && memcmp(output+1, input+1, size-1) == 0;
2088 
2089  cout << (pass ? "passed" : "FAILED") << endl;
2090  return pass;
2091 }
2092 
2093 bool ValidateBaseCode()
2094 {
2095  bool pass = true, fail;
2096  byte data[255];
2097  for (unsigned int i=0; i<255; i++)
2098  data[i] = byte(i);
2099  static const char hexEncoded[] =
2100 "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"
2101 "28292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F"
2102 "505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F7071727374757677"
2103 "78797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F"
2104 "A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7"
2105 "C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
2106 "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFE";
2107  static const char base32Encoded[] =
2108 "AAASEA2EAWDAQCAJBIFS2DIQB6IBCESVCSKTNF22DEPBYHA7D2RUAIJCENUCKJTHFAWUWK3NFWZC8NBT"
2109 "GI3VIPJYG66DUQT5HS8V6R4AIFBEGTCFI3DWSUKKJPGE4VURKBIXEW4WKXMFQYC3MJPX2ZK8M7SGC2VD"
2110 "NTUYN35IPFXGY5DPP3ZZA6MUQP4HK7VZRB6ZW856RX9H9AEBSKB2JBNGS8EIVCWMTUG27D6SUGJJHFEX"
2111 "U4M3TGN4VQQJ5HW9WCS4FI7EWYVKRKFJXKX43MPQX82MDNXVYU45PP72ZG7MZRF7Z496BSQC2RCNMTYH"
2112 "3DE6XU8N3ZHN9WGT4MJ7JXQY49NPVYY55VQ77Z9A6HTQH3HF65V8T4RK7RYQ55ZR8D29F69W8Z5RR8H3"
2113 "9M7939R8";
2114  const char *base64AndHexEncoded =
2115 "41414543417751464267634943516F4C4441304F4478415245684D554652595847426B6147787764"
2116 "486838674953496A4A43556D4A7967704B6973734C5334764D4445794D7A51310A4E6A63344F546F"
2117 "375044302B50304242516B4E4552555A4853456C4B5330784E546B395155564A5456465657563168"
2118 "5A576C746358563566594746695932526C5A6D646F615770720A6247317562334278636E4E306458"
2119 "5A3365486C3665337839666E2B4167594B44684957476834694A696F754D6A5936506B4A47536B35"
2120 "53566C7065596D5A71626E4A32656E3643680A6F714F6B7061616E714B6D717136797472712B7773"
2121 "624B7A744C573274376935757275387662362F774D484377385446787366497963724C7A4D334F7A"
2122 "39445230745055316462580A324E6E6132397A6433742F6734654C6A354F586D352B6A7036757673"
2123 "3765377638504879382F5431397666342B6672372F50332B0A";
2124 
2125  cout << "\nBase64, base32 and hex coding validation suite running...\n\n";
2126 
2127  fail = !TestFilter(HexEncoder().Ref(), data, 255, (const byte *)hexEncoded, strlen(hexEncoded));
2128  cout << (fail ? "FAILED " : "passed ");
2129  cout << "Hex Encoding\n";
2130  pass = pass && !fail;
2131 
2132  fail = !TestFilter(HexDecoder().Ref(), (const byte *)hexEncoded, strlen(hexEncoded), data, 255);
2133  cout << (fail ? "FAILED " : "passed ");
2134  cout << "Hex Decoding\n";
2135  pass = pass && !fail;
2136 
2137  fail = !TestFilter(Base32Encoder().Ref(), data, 255, (const byte *)base32Encoded, strlen(base32Encoded));
2138  cout << (fail ? "FAILED " : "passed ");
2139  cout << "Base32 Encoding\n";
2140  pass = pass && !fail;
2141 
2142  fail = !TestFilter(Base32Decoder().Ref(), (const byte *)base32Encoded, strlen(base32Encoded), data, 255);
2143  cout << (fail ? "FAILED " : "passed ");
2144  cout << "Base32 Decoding\n";
2145  pass = pass && !fail;
2146 
2147  fail = !TestFilter(Base64Encoder(new HexEncoder).Ref(), data, 255, (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded));
2148  cout << (fail ? "FAILED " : "passed ");
2149  cout << "Base64 Encoding\n";
2150  pass = pass && !fail;
2151 
2152  fail = !TestFilter(HexDecoder(new Base64Decoder).Ref(), (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded), data, 255);
2153  cout << (fail ? "FAILED " : "passed ");
2154  cout << "Base64 Decoding\n";
2155  pass = pass && !fail;
2156 
2157  return pass;
2158 }
2159 
2160 bool ValidateSHACAL2()
2161 {
2162  cout << "\nSHACAL-2 validation suite running...\n\n";
2163 
2164  bool pass = true;
2165  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/shacal2v.dat", true, new HexDecoder);
2166  pass = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(16), valdata, 4) && pass;
2167  pass = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(64), valdata, 10) && pass;
2168  return pass;
2169 }
2170 
2171 bool ValidateCamellia()
2172 {
2173  cout << "\nCamellia validation suite running...\n\n";
2174 
2175  bool pass = true;
2176  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/camellia.dat", true, new HexDecoder);
2177  pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(16), valdata, 15) && pass;
2178  pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(24), valdata, 15) && pass;
2179  pass = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(32), valdata, 15) && pass;
2180  return pass;
2181 }
2182 
2183 bool ValidateSalsa()
2184 {
2185  cout << "\nSalsa validation suite running...\n";
2186 
2187  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/salsa.txt");
2188 }
2189 
2190 bool ValidateSosemanuk()
2191 {
2192  cout << "\nSosemanuk validation suite running...\n";
2193  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/sosemanuk.txt");
2194 }
2195 
2196 bool ValidateVMAC()
2197 {
2198  cout << "\nVMAC validation suite running...\n";
2199  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/vmac.txt");
2200 }
2201 
2202 bool ValidateCCM()
2203 {
2204  cout << "\nAES/CCM validation suite running...\n";
2205  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/ccm.txt");
2206 }
2207 
2208 bool ValidateGCM()
2209 {
2210  cout << "\nAES/GCM validation suite running...\n";
2211  cout << "\n2K tables:";
2212  bool pass = RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)2048));
2213  cout << "\n64K tables:";
2214  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)64*1024)) && pass;
2215 }
2216 
2217 bool ValidateCMAC()
2218 {
2219  cout << "\nCMAC validation suite running...\n";
2220  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/cmac.txt");
2221 }
void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition: randpool.cpp:26
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:139
Base32 encodes data.
Definition: base32.h:17
OFB block cipher mode of operation.
Definition: modes.h:302
Stack-based SecBlock that grows into the heap.
Definition: secblock.h:768
unsigned int BytesNeeded() const
Provides the number of bytes of input is needed by the test.
Definition: rng.h:96
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.
bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
Definition: randpool.h:31
DMAC.
Definition: dmac.h:45
Hardware generated random numbers using RDRAND instruction.
Definition: rdrand.h:36
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:59
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:301
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:1279
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
virtual bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
Definition: cryptlib.h:1195
Classes for the GIST block cipher.
STL namespace.
Classes for the Cameliia block cipher.
Classes for RDRAND and RDSEED.
virtual void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition: rdrand.h:96
Classes for Base32Encoder and Base32Decoder.
SecBlock typedef.
Definition: secblock.h:723
Interface for buffered transformations.
Definition: cryptlib.h:1342
Classes for CBC MAC.
Classes for the RC6 block cipher.
size_type max_size() const
Returns the maximum number of elements the allocator can provide.
Definition: secblock.h:55
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's padding added to a block
Definition: filters.h:468
Base64 encodes data.
Definition: base64.h:17
bool MessageEnd(int propagation=-1, bool blocking=true)
Signals the end of messages to the object.
Definition: cryptlib.h:1421
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
Filter wrapper for HashTransformation.
Definition: filters.h:509
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1363
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:554
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.
Definition: cryptlib.h:643
Classes for HexEncoder and HexDecoder.
Maurer'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:86
Automatically Seeded Randomness Pool.
Definition: osrng.h:140
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
Definition: secblock.h:536
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:1089
Provides class member functions to key a block cipher.
Definition: seckey.h:316
Classes for DMAC message authentication code.
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:728
#define COUNTOF(arr)
Counts elements in an array.
Definition: misc.h:140
Miscellaneous classes for RNGs.
Automatically Seeded X9.17 RNG.
Definition: osrng.h:167
Classes for SEAL stream cipher.
Redirect input to another BufferedTransformation without owning it.
Definition: filters.h:756
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.
std::string AlgorithmName() const
Provides the name of this algorithm.
Definition: rdrand.h:39
Implementation of BufferedTransformation's attachment interface.
Hardware generated random numbers using RDSEED instruction.
Definition: rdrand.h:118
Classes for the Base64Encoder, Base64Decoder, Base64URLEncoder and Base64URLDecoder.
Filter wrapper for StreamTransformation.
Definition: filters.h:477
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
bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
Definition: osrng.h:197
void OS_GenerateRandomBlock(bool blocking, byte *output, size_t size)
OS_GenerateRandomBlock.
Definition: osrng.cpp:158
0's padding added to a block
Definition: filters.h:464
Measure how many bytes and messages pass through the filter.
Definition: filters.h:221
virtual lword MaxRetrievable() const
Provides the number of bytes ready for retrieval.
Definition: cryptlib.cpp:499
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Definition: misc.h:407
Base32 decodes data.
Definition: base32.h:61
Classes for the 3-Way block cipher.
void SetRetries(unsigned int retries)
Set the number of retries used by the generator.
Definition: rdrand.h:61
No padding added to a block.
Definition: filters.h:462
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:1258
unsigned int GetRetries() const
Retrieve the number of retries used by the generator.
Definition: rdrand.h:54
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
Definition: secblock.h:530
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)
Retrieves a secure IV for the next message.
Definition: cryptlib.cpp:176
Classes for ARC4 cipher.
virtual unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: cryptlib.h:621
virtual unsigned int MinLastBlockSize() const
returns the minimum size of the last block, 0 indicating the last block is not special ...
Definition: cryptlib.h:849
Classes for access to the operating system's random number generators.
Classes for the IDEA block cipher.
void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition: osrng.h:198
Classes for the SHACAL-2 block cipher.