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