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