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