Crypto++  5.6.5
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 
72 bool ValidateAll(bool thorough)
73 {
74  bool pass=TestSettings();
75  pass=TestOS_RNG() && pass;
76  pass=TestAutoSeeded() && pass;
77  pass=TestAutoSeededX917() && pass;
78 
79 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
80  pass=TestRDRAND() && pass;
81  pass=TestRDSEED() && pass;
82 #endif
83 
84 #if CRYPTOPP_DEBUG && !defined(CRYPTOPP_IMPORTS)
85  // http://github.com/weidai11/cryptopp/issues/92
86  pass=TestSecBlock() && pass;
87  // http://github.com/weidai11/cryptopp/issues/64
88  pass=TestPolynomialMod2() && pass;
89  // http://github.com/weidai11/cryptopp/pull/242
90  pass=TestHuffmanCodes() && pass;
91 #endif
92 
93  pass=ValidateCRC32() && pass;
94  pass=ValidateCRC32C() && pass;
95  pass=ValidateAdler32() && pass;
96  pass=ValidateMD2() && pass;
97  pass=ValidateMD5() && pass;
98  pass=ValidateSHA() && pass;
99 
100  pass=RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/keccak.txt") && pass;
101  pass=RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/sha3_fips_202.txt") && pass;
102 
103  pass=ValidateTiger() && pass;
104  pass=ValidateRIPEMD() && pass;
105  pass=ValidatePanama() && pass;
106  pass=ValidateWhirlpool() && pass;
107  pass=ValidateBLAKE2s() && pass;
108  pass=ValidateBLAKE2b() && pass;
109 
110  pass=ValidateHMAC() && pass;
111  pass=ValidateTTMAC() && pass;
112 
113  pass=ValidatePBKDF() && pass;
114  pass=ValidateHKDF() && pass;
115 
116  pass=ValidateDES() && pass;
117  pass=ValidateCipherModes() && pass;
118  pass=ValidateIDEA() && pass;
119  pass=ValidateSAFER() && pass;
120  pass=ValidateRC2() && pass;
121  pass=ValidateARC4() && pass;
122  pass=ValidateRC5() && pass;
123  pass=ValidateBlowfish() && pass;
124  pass=ValidateThreeWay() && pass;
125  pass=ValidateGOST() && pass;
126  pass=ValidateSHARK() && pass;
127  pass=ValidateCAST() && pass;
128  pass=ValidateSquare() && pass;
129  pass=ValidateSKIPJACK() && pass;
130  pass=ValidateSEAL() && pass;
131  pass=ValidateRC6() && pass;
132  pass=ValidateMARS() && pass;
133  pass=ValidateRijndael() && pass;
134  pass=ValidateTwofish() && pass;
135  pass=ValidateSerpent() && pass;
136  pass=ValidateSHACAL2() && pass;
137  pass=ValidateCamellia() && pass;
138  pass=ValidateSalsa() && pass;
139  pass=ValidateSosemanuk() && pass;
140  pass=ValidateVMAC() && pass;
141  pass=ValidateCCM() && pass;
142  pass=ValidateGCM() && pass;
143  pass=ValidateCMAC() && pass;
144  pass=RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/eax.txt") && pass;
145  pass=RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/seed.txt") && pass;
146 
147  pass=ValidateBBS() && pass;
148  pass=ValidateDH() && pass;
149  pass=ValidateMQV() && pass;
150  pass=ValidateHMQV() && pass;
151  pass=ValidateFHMQV() && 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  pass=ValidateLegacyDLIES() && pass;
170  pass=ValidateLegacyECIES() && pass;
171  pass=ValidateInteropDLIES() && pass;
172  pass=ValidateInteropECIES() && pass;
173 
174  if (pass)
175  std::cout << "\nAll tests passed!\n";
176  else
177  std::cout << "\nOops! Not all tests passed.\n";
178 
179  return pass;
180 }
181 
182 bool TestSettings()
183 {
184  // Thanks to IlyaBizyaev and Zireael, http://github.com/weidai11/cryptopp/issues/28
185 #if defined(__MINGW32__)
186  using CryptoPP::memcpy_s;
187 #endif
188 
189  bool pass = true;
190 
191  std::cout << "\nTesting Settings...\n\n";
192 
193  word32 w;
194  memcpy_s(&w, sizeof(w), "\x01\x02\x03\x04", 4);
195 
196  if (w == 0x04030201L)
197  {
198 #ifdef IS_LITTLE_ENDIAN
199  std::cout << "passed: ";
200 #else
201  std::cout << "FAILED: ";
202  pass = false;
203 #endif
204  std::cout << "Your machine is little endian.\n";
205  }
206  else if (w == 0x01020304L)
207  {
208 #ifndef IS_LITTLE_ENDIAN
209  std::cout << "passed: ";
210 #else
211  std::cout << "FAILED: ";
212  pass = false;
213 #endif
214  std::cout << "Your machine is big endian.\n";
215  }
216  else
217  {
218  std::cout << "FAILED: Your machine is neither big endian nor little endian.\n";
219  pass = false;
220  }
221 
222 #ifdef CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS
223  // Don't CRYPTOPP_ASSERT the alignment of testvals. That's what this test is for.
224  byte testvals[10] = {1,2,2,3,3,3,3,2,2,1};
225  if (*(word32 *)(void *)(testvals+3) == 0x03030303 && *(word64 *)(void *)(testvals+1) == W64LIT(0x0202030303030202))
226  std::cout << "passed: Your machine allows unaligned data access.\n";
227  else
228  {
229  std::cout << "FAILED: Unaligned data access gave incorrect results.\n";
230  pass = false;
231  }
232 #else
233  std::cout << "passed: CRYPTOPP_ALLOW_UNALIGNED_DATA_ACCESS is not defined. Will restrict to aligned data access.\n";
234 #endif
235 
236  if (sizeof(byte) == 1)
237  std::cout << "passed: ";
238  else
239  {
240  std::cout << "FAILED: ";
241  pass = false;
242  }
243  std::cout << "sizeof(byte) == " << sizeof(byte) << std::endl;
244 
245  if (sizeof(word16) == 2)
246  std::cout << "passed: ";
247  else
248  {
249  std::cout << "FAILED: ";
250  pass = false;
251  }
252  std::cout << "sizeof(word16) == " << sizeof(word16) << std::endl;
253 
254  if (sizeof(word32) == 4)
255  std::cout << "passed: ";
256  else
257  {
258  std::cout << "FAILED: ";
259  pass = false;
260  }
261  std::cout << "sizeof(word32) == " << sizeof(word32) << std::endl;
262 
263  if (sizeof(word64) == 8)
264  std::cout << "passed: ";
265  else
266  {
267  std::cout << "FAILED: ";
268  pass = false;
269  }
270  std::cout << "sizeof(word64) == " << sizeof(word64) << std::endl;
271 
272 #ifdef CRYPTOPP_WORD128_AVAILABLE
273  if (sizeof(word128) == 16)
274  std::cout << "passed: ";
275  else
276  {
277  std::cout << "FAILED: ";
278  pass = false;
279  }
280  std::cout << "sizeof(word128) == " << sizeof(word128) << std::endl;
281 #endif
282 
283  if (sizeof(word) == 2*sizeof(hword)
284 #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE
285  && sizeof(dword) == 2*sizeof(word)
286 #endif
287  )
288  std::cout << "passed: ";
289  else
290  {
291  std::cout << "FAILED: ";
292  pass = false;
293  }
294  std::cout << "sizeof(hword) == " << sizeof(hword) << ", sizeof(word) == " << sizeof(word);
295 #ifdef CRYPTOPP_NATIVE_DWORD_AVAILABLE
296  std::cout << ", sizeof(dword) == " << sizeof(dword);
297 #endif
298  std::cout << std::endl;
299 
300 #ifdef CRYPTOPP_CPUID_AVAILABLE
301  bool hasMMX = HasMMX();
302  bool hasISSE = HasISSE();
303  bool hasSSE2 = HasSSE2();
304  bool hasSSSE3 = HasSSSE3();
305  bool hasSSE4 = HasSSE4();
306  bool isP4 = IsP4();
307  int cacheLineSize = GetCacheLineSize();
308 
309  if ((isP4 && (!hasMMX || !hasSSE2)) || (hasSSE2 && !hasMMX) || (cacheLineSize < 16 || cacheLineSize > 256 || !IsPowerOf2(cacheLineSize)))
310  {
311  std::cout << "FAILED: ";
312  pass = false;
313  }
314  else
315  std::cout << "passed: ";
316 
317  std::cout << "hasMMX == " << hasMMX << ", hasISSE == " << hasISSE << ", hasSSE2 == " << hasSSE2 << ", hasSSSE3 == " << hasSSSE3 << ", hasSSE4 == " << hasSSE4;
318  std::cout << ", hasAESNI == " << HasAESNI() << ", hasCLMUL == " << HasCLMUL() << ", hasRDRAND == " << HasRDRAND() << ", hasRDSEED == " << HasRDSEED();
319  std::cout << ", isP4 == " << isP4 << ", cacheLineSize == " << cacheLineSize << std::endl;
320 
321 #elif (CRYPTOPP_BOOL_ARM32 || CRYPTOPP_BOOL_ARM64)
322  bool hasNEON = HasNEON();
323  bool hasPMULL = HasPMULL();
324  bool hasCRC32 = HasCRC32();
325  bool hasAES = HasAES();
326  bool hasSHA1 = HasSHA1();
327  bool hasSHA2 = HasSHA2();
328 
329  std::cout << "passed: ";
330  std::cout << "hasNEON == " << hasNEON << ", hasPMULL == " << hasPMULL << ", hasCRC32 == " << hasCRC32 << ", hasAES == " << hasAES << ", hasSHA1 == " << hasSHA1 << ", hasSHA2 == " << hasSHA2 << std::endl;
331 #endif
332 
333  if (!pass)
334  {
335  std::cout << "Some critical setting in config.h is in error. Please fix it and recompile." << std::endl;
336  abort();
337  }
338  return pass;
339 }
340 
341 #if CRYPTOPP_DEBUG && !defined(CRYPTOPP_IMPORTS)
342 bool TestSecBlock()
343 {
344  std::cout << "\nTesting SecBlock...\n\n";
345 
346  bool pass1=true, pass2=true, pass3=true, pass4=true, pass5=true, pass6=true, pass7=true, temp=false;
347 
348  //********** Zeroized block **********//
349 
350  {
351  // NULL ptr with a size means to create a new SecBlock with all elements zero'd
352  SecByteBlock z1(NULL, 256);
353  temp = true;
354 
355  for (size_t i = 0; i < z1.size(); i++)
356  temp &= (z1[i] == 0);
357 
358  pass1 &= temp;
359  if (!temp)
360  std::cout << "FAILED:";
361  else
362  std::cout << "passed:";
363  std::cout << " Zeroized byte array" << std::endl;
364 
365  SecBlock<word32> z2(NULL, 256);
366  temp = true;
367 
368  for (size_t i = 0; i < z2.size(); i++)
369  temp &= (z2[i] == 0);
370 
371  pass1 &= temp;
372  if (!temp)
373  std::cout << "FAILED:";
374  else
375  std::cout << "passed:";
376  std::cout << " Zeroized word32 array" << std::endl;
377 
378  SecBlock<word64> z3(NULL, 256);
379  temp = true;
380 
381  for (size_t i = 0; i < z3.size(); i++)
382  temp &= (z3[i] == 0);
383 
384  pass1 &= temp;
385  if (!temp)
386  std::cout << "FAILED:";
387  else
388  std::cout << "passed:";
389  std::cout << " Zeroized word64 array" << std::endl;
390 
391 #if defined(CRYPTOPP_WORD128_AVAILABLE)
392  SecBlock<word128> z4(NULL, 256);
393  temp = true;
394 
395  for (size_t i = 0; i < z4.size(); i++)
396  temp &= (z4[i] == 0);
397 
398  pass1 &= temp;
399  if (!temp)
400  std::cout << "FAILED:";
401  else
402  std::cout << "passed:";
403  std::cout << " Zeroized word128 array" << std::endl;
404 #endif
405  }
406 
407  //********** Assign **********//
408 
409  try
410  {
411  SecByteBlock a, b;
412  temp = true;
413 
414  a.Assign((const byte*)"a", 1);
415  b.Assign((const byte*)"b", 1);
416 
417  temp &= (a.SizeInBytes() == 1);
418  temp &= (b.SizeInBytes() == 1);
419  temp &= (a[0] == 'a');
420  temp &= (b[0] == 'b');
421 
422  a.Assign((const byte*)"ab", 2);
423  b.Assign((const byte*)"cd", 2);
424 
425  temp &= (a.SizeInBytes() == 2);
426  temp &= (b.SizeInBytes() == 2);
427  temp &= (a[0] == 'a' && a[1] == 'b');
428  temp &= (b[0] == 'c' && b[1] == 'd');
429  }
430  catch(const Exception& /*ex*/)
431  {
432  temp = false;
433  }
434 
435  pass2 &= temp;
436  if (!temp)
437  std::cout << "FAILED:";
438  else
439  std::cout << "passed:";
440  std::cout << " Assign byte" << std::endl;
441 
442  try
443  {
444  SecBlock<word32> a, b;
445  temp = true;
446 
447  word32 one[1] = {1}, two[1] = {2};
448  a.Assign(one, 1);
449  b.Assign(two, 1);
450 
451  temp &= (a.SizeInBytes() == 4);
452  temp &= (b.SizeInBytes() == 4);
453  temp &= (a[0] == 1);
454  temp &= (b[0] == 2);
455 
456  word32 three[2] = {1,2}, four[2] = {3,4};
457  a.Assign(three, 2);
458  b.Assign(four, 2);
459 
460  temp &= (a.SizeInBytes() == 8);
461  temp &= (b.SizeInBytes() == 8);
462  temp &= (a[0] == 1 && a[1] == 2);
463  temp &= (b[0] == 3 && b[1] == 4);
464  }
465  catch(const Exception& /*ex*/)
466  {
467  temp = false;
468  }
469 
470  pass2 &= temp;
471  if (!temp)
472  std::cout << "FAILED:";
473  else
474  std::cout << "passed:";
475  std::cout << " Assign word32" << std::endl;
476 
477  try
478  {
479  SecBlock<word64> a, b;
480  temp = true;
481 
482  word64 one[1] = {1}, two[1] = {2};
483  a.Assign(one, 1);
484  b.Assign(two, 1);
485 
486  temp &= (a.SizeInBytes() == 8);
487  temp &= (b.SizeInBytes() == 8);
488  temp &= (a[0] == 1);
489  temp &= (b[0] == 2);
490 
491  word64 three[2] = {1,2}, four[2] = {3,4};
492  a.Assign(three, 2);
493  b.Assign(four, 2);
494 
495  temp &= (a.SizeInBytes() == 16);
496  temp &= (b.SizeInBytes() == 16);
497  temp &= (a[0] == 1 && a[1] == 2);
498  temp &= (b[0] == 3 && b[1] == 4);
499  }
500  catch(const Exception& /*ex*/)
501  {
502  temp = false;
503  }
504 
505  pass2 &= temp;
506  if (!temp)
507  std::cout << "FAILED:";
508  else
509  std::cout << "passed:";
510  std::cout << " Assign word64" << std::endl;
511 
512 #if defined(CRYPTOPP_WORD128_AVAILABLE)
513  try
514  {
515  SecBlock<word128> a, b;
516  temp = true;
517 
518  word128 one[1] = {1}, two[1] = {2};
519  a.Assign(one, 1);
520  b.Assign(two, 1);
521 
522  temp &= (a.SizeInBytes() == 16);
523  temp &= (b.SizeInBytes() == 16);
524  temp &= (a[0] == 1);
525  temp &= (b[0] == 2);
526 
527  word128 three[2] = {1,2}, four[2] = {3,4};
528  a.Assign(three, 2);
529  b.Assign(four, 2);
530 
531  temp &= (a.SizeInBytes() == 32);
532  temp &= (b.SizeInBytes() == 32);
533  temp &= (a[0] == 1 && a[1] == 2);
534  temp &= (b[0] == 3 && b[1] == 4);
535  }
536  catch(const Exception& /*ex*/)
537  {
538  temp = false;
539  }
540 
541  pass2 &= temp;
542  if (!temp)
543  std::cout << "FAILED:";
544  else
545  std::cout << "passed:";
546  std::cout << " Assign word128" << std::endl;
547 #endif
548 
549  //********** Append **********//
550 
551  try
552  {
553  SecByteBlock a, b;
554  temp = true;
555 
556  a.Assign((const byte*)"a", 1);
557  b.Assign((const byte*)"b", 1);
558 
559  a += b;
560  temp &= (a.SizeInBytes() == 2);
561  temp &= (a[0] == 'a' && a[1] == 'b');
562 
563  a.Assign((const byte*)"ab", 2);
564  b.Assign((const byte*)"cd", 2);
565 
566  a += b;
567  temp &= (a.SizeInBytes() == 4);
568  temp &= (a[0] == 'a' && a[1] == 'b' && a[2] == 'c' && a[3] == 'd');
569 
570  a.Assign((const byte*)"a", 1);
571 
572  a += a;
573  temp &= (a.SizeInBytes() == 2);
574  temp &= (a[0] == 'a' && a[1] == 'a');
575 
576  a.Assign((const byte*)"ab", 2);
577 
578  a += a;
579  temp &= (a.SizeInBytes() == 4);
580  temp &= (a[0] == 'a' && a[1] == 'b' && a[2] == 'a' && a[3] == 'b');
581  }
582  catch(const Exception& /*ex*/)
583  {
584  temp = false;
585  }
586 
587  pass3 &= temp;
588  if (!temp)
589  std::cout << "FAILED:";
590  else
591  std::cout << "passed:";
592  std::cout << " Append byte" << std::endl;
593 
594  try
595  {
596  SecBlock<word32> a, b;
597  temp = true;
598 
599  const word32 one[1] = {1}, two[1] = {2};
600  a.Assign(one, 1);
601  b.Assign(two, 1);
602 
603  a += b;
604  temp &= (a.SizeInBytes() == 8);
605  temp &= (a[0] == 1 && a[1] == 2);
606 
607  const word32 three[2] = {1,2}, four[2] = {3,4};
608  a.Assign(three, 2);
609  b.Assign(four, 2);
610 
611  a += b;
612  temp &= (a.SizeInBytes() == 16);
613  temp &= (a[0] == 1 && a[1] == 2 && a[2] == 3 && a[3] == 4);
614 
615  a.Assign(one, 1);
616 
617  a += a;
618  temp &= (a.SizeInBytes() == 8);
619  temp &= (a[0] == 1 && a[1] == 1);
620 
621  a.Assign(three, 2);
622 
623  a += a;
624  temp &= (a.SizeInBytes() == 16);
625  temp &= (a[0] == 1 && a[1] == 2 && a[2] == 1 && a[3] == 2);
626  }
627  catch(const Exception& /*ex*/)
628  {
629  temp = false;
630  }
631 
632  pass3 &= temp;
633  if (!temp)
634  std::cout << "FAILED:";
635  else
636  std::cout << "passed:";
637  std::cout << " Append word32" << std::endl;
638 
639  try
640  {
641  SecBlock<word64> a, b;
642  temp = true;
643 
644  const word64 one[1] = {1}, two[1] = {2};
645  a.Assign(one, 1);
646  b.Assign(two, 1);
647 
648  a += b;
649  temp &= (a.SizeInBytes() == 16);
650  temp &= (a[0] == 1 && a[1] == 2);
651 
652  const word64 three[2] = {1,2}, four[2] = {3,4};
653  a.Assign(three, 2);
654  b.Assign(four, 2);
655 
656  a += b;
657  temp &= (a.SizeInBytes() == 32);
658  temp &= (a[0] == 1 && a[1] == 2 && a[2] == 3 && a[3] == 4);
659 
660  a.Assign(one, 1);
661 
662  a += a;
663  temp &= (a.SizeInBytes() == 16);
664  temp &= (a[0] == 1 && a[1] == 1);
665 
666  a.Assign(three, 2);
667 
668  a += a;
669  temp &= (a.SizeInBytes() == 32);
670  temp &= (a[0] == 1 && a[1] == 2 && a[2] == 1 && a[3] == 2);
671  }
672  catch(const Exception& /*ex*/)
673  {
674  temp = false;
675  }
676 
677  pass3 &= temp;
678  if (!temp)
679  std::cout << "FAILED:";
680  else
681  std::cout << "passed:";
682  std::cout << " Append word64" << std::endl;
683 
684 #if defined(CRYPTOPP_WORD128_AVAILABLE)
685  try
686  {
687  SecBlock<word128> a, b;
688  temp = true;
689 
690  const word128 one[1] = {1}, two[1] = {2};
691  a.Assign(one, 1);
692  b.Assign(two, 1);
693 
694  a += b;
695  temp &= (a.SizeInBytes() == 32);
696  temp &= (a[0] == 1 && a[1] == 2);
697 
698  const word128 three[2] = {1,2}, four[2] = {3,4};
699  a.Assign(three, 2);
700  b.Assign(four, 2);
701 
702  a += b;
703  temp &= (a.SizeInBytes() == 64);
704  temp &= (a[0] == 1 && a[1] == 2 && a[2] == 3 && a[3] == 4);
705 
706  a.Assign(one, 1);
707 
708  a += a;
709  temp &= (a.SizeInBytes() == 32);
710  temp &= (a[0] == 1 && a[1] == 1);
711 
712  a.Assign(three, 2);
713 
714  a += a;
715  temp &= (a.SizeInBytes() == 64);
716  temp &= (a[0] == 1 && a[1] == 2 && a[2] == 1 && a[3] == 2);
717  }
718  catch(const Exception& /*ex*/)
719  {
720  temp = false;
721  }
722 
723  pass3 &= temp;
724  if (!temp)
725  std::cout << "FAILED:";
726  else
727  std::cout << "passed:";
728  std::cout << " Append word128" << std::endl;
729 #endif
730 
731  //********** Concatenate **********//
732 
733  // byte
734  try
735  {
736  SecByteBlock a, b, c;
737  temp = true;
738 
739  a.Assign((const byte*)"a", 1);
740  b.Assign((const byte*)"b", 1);
741 
742  c = a + b;
743  temp &= (a[0] == 'a');
744  temp &= (b[0] == 'b');
745  temp &= (c.SizeInBytes() == 2);
746  temp &= (c[0] == 'a' && c[1] == 'b');
747 
748  a.Assign((const byte*)"ab", 2);
749  b.Assign((const byte*)"cd", 2);
750 
751  c = a + b;
752  temp &= (a[0] == 'a' && a[1] == 'b');
753  temp &= (b[0] == 'c' && b[1] == 'd');
754  temp &= (c.SizeInBytes() == 4);
755  temp &= (c[0] == 'a' && c[1] == 'b' && c[2] == 'c' && c[3] == 'd');
756  }
757  catch(const Exception& /*ex*/)
758  {
759  temp = false;
760  }
761 
762  pass4 &= temp;
763  if (!temp)
764  std::cout << "FAILED:";
765  else
766  std::cout << "passed:";
767  std::cout << " Concatenate byte" << std::endl;
768 
769  // word32
770  try
771  {
772  SecBlock<word32> a, b, c;
773  temp = true;
774 
775  const word32 one[1] = {1}, two[1] = {2};
776  a.Assign(one, 1);
777  b.Assign(two, 1);
778 
779  c = a + b;
780  temp &= (a[0] == 1);
781  temp &= (b[0] == 2);
782  temp &= (c.SizeInBytes() == 8);
783  temp &= (c[0] == 1 && c[1] == 2);
784 
785  const word32 three[2] = {1,2}, four[2] = {3,4};
786  a.Assign(three, 2);
787  b.Assign(four, 2);
788 
789  c = a + b;
790  temp &= (a[0] == 1 && a[1] == 2);
791  temp &= (b[0] == 3 && b[1] == 4);
792  temp &= (c.SizeInBytes() == 16);
793  temp &= (c[0] == 1 && c[1] == 2 && c[2] == 3 && c[3] == 4);
794  }
795  catch(const Exception& /*ex*/)
796  {
797  temp = false;
798  }
799 
800  pass4 &= temp;
801  if (!temp)
802  std::cout << "FAILED:";
803  else
804  std::cout << "passed:";
805  std::cout << " Concatenate word32" << std::endl;
806 
807  // word64
808  try
809  {
810  SecBlock<word64> a, b, c;
811  temp = true;
812 
813  const word64 one[1] = {1}, two[1] = {2};
814  a.Assign(one, 1);
815  b.Assign(two, 1);
816 
817  c = a + b;
818  temp &= (a[0] == 1);
819  temp &= (b[0] == 2);
820  temp &= (c.SizeInBytes() == 16);
821  temp &= (c[0] == 1 && c[1] == 2);
822 
823  const word64 three[2] = {1,2}, four[2] = {3,4};
824  a.Assign(three, 2);
825  b.Assign(four, 2);
826 
827  c = a + b;
828  temp &= (a[0] == 1 && a[1] == 2);
829  temp &= (b[0] == 3 && b[1] == 4);
830  temp &= (c.SizeInBytes() == 32);
831  temp &= (c[0] == 1 && c[1] == 2 && c[2] == 3 && c[3] == 4);
832  }
833  catch(const Exception& /*ex*/)
834  {
835  temp = false;
836  }
837 
838  pass4 &= temp;
839  if (!temp)
840  std::cout << "FAILED:";
841  else
842  std::cout << "passed:";
843  std::cout << " Concatenate word64" << std::endl;
844 
845 #if defined(CRYPTOPP_WORD128_AVAILABLE)
846  try
847  {
848  SecBlock<word128> a, b, c;
849  temp = true;
850 
851  const word128 one[1] = {1}, two[1] = {2};
852  a.Assign(one, 1);
853  b.Assign(two, 1);
854 
855  c = a + b;
856  temp &= (a[0] == 1);
857  temp &= (b[0] == 2);
858  temp &= (c.SizeInBytes() == 32);
859  temp &= (c[0] == 1 && c[1] == 2);
860 
861  const word128 three[2] = {1,2}, four[2] = {3,4};
862  a.Assign(three, 2);
863  b.Assign(four, 2);
864 
865  c = a + b;
866  temp &= (a[0] == 1 && a[1] == 2);
867  temp &= (b[0] == 3 && b[1] == 4);
868  temp &= (c.SizeInBytes() == 64);
869  temp &= (c[0] == 1 && c[1] == 2 && c[2] == 3 && c[3] == 4);
870  }
871  catch(const Exception& /*ex*/)
872  {
873  temp = false;
874  }
875 
876  pass4 &= temp;
877  if (!temp)
878  std::cout << "FAILED:";
879  else
880  std::cout << "passed:";
881  std::cout << " Concatenate word128" << std::endl;
882 #endif
883 
884  //********** Equality **********//
885 
886  // byte
887  try
888  {
889  static const byte str1[] = "abcdefghijklmnopqrstuvwxyz";
890  static const byte str2[] = "zyxwvutsrqponmlkjihgfedcba";
891  static const byte str3[] = "0123456789";
892 
893  temp = true;
894  SecByteBlock a,b;
895 
896  a.Assign(str1, COUNTOF(str1));
897  b.Assign(str1, COUNTOF(str1));
898  temp &= (a.operator==(b));
899 
900  a.Assign(str3, COUNTOF(str3));
901  b.Assign(str3, COUNTOF(str3));
902  temp &= (a == b);
903 
904  a.Assign(str1, COUNTOF(str1));
905  b.Assign(str2, COUNTOF(str2));
906  temp &= (a.operator!=(b));
907 
908  a.Assign(str1, COUNTOF(str1));
909  b.Assign(str3, COUNTOF(str3));
910  temp &= (a != b);
911  }
912  catch(const Exception& /*ex*/)
913  {
914  temp = false;
915  }
916 
917  pass5 &= temp;
918  if (!temp)
919  std::cout << "FAILED:";
920  else
921  std::cout << "passed:";
922  std::cout << " Equality byte" << std::endl;
923 
924  // word32
925  try
926  {
927  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};
928  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};
929  static const word32 str3[] = {0,1,2,3,4,5,6,7,8,9};
930 
931  temp = true;
932  SecBlock<word32> a,b;
933 
934  a.Assign(str1, COUNTOF(str1));
935  b.Assign(str1, COUNTOF(str1));
936  temp &= (a.operator==(b));
937 
938  a.Assign(str3, COUNTOF(str3));
939  b.Assign(str3, COUNTOF(str3));
940  temp &= (a == b);
941 
942  a.Assign(str1, COUNTOF(str1));
943  b.Assign(str2, COUNTOF(str2));
944  temp &= (a.operator!=(b));
945 
946  a.Assign(str1, COUNTOF(str1));
947  b.Assign(str3, COUNTOF(str3));
948  temp &= (a != b);
949  }
950  catch(const Exception& /*ex*/)
951  {
952  temp = false;
953  }
954 
955  pass5 &= temp;
956  if (!temp)
957  std::cout << "FAILED:";
958  else
959  std::cout << "passed:";
960  std::cout << " Equality word32" << std::endl;
961 
962  // word64
963  try
964  {
965  static const word64 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};
966  static const word64 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};
967  static const word64 str3[] = {0,1,2,3,4,5,6,7,8,9};
968 
969  temp = true;
970  SecBlock<word64> a,b;
971 
972  a.Assign(str1, COUNTOF(str1));
973  b.Assign(str1, COUNTOF(str1));
974  temp &= (a.operator==(b));
975 
976  a.Assign(str3, COUNTOF(str3));
977  b.Assign(str3, COUNTOF(str3));
978  temp &= (a == b);
979 
980  a.Assign(str1, COUNTOF(str1));
981  b.Assign(str2, COUNTOF(str2));
982  temp &= (a.operator!=(b));
983 
984  a.Assign(str1, COUNTOF(str1));
985  b.Assign(str3, COUNTOF(str3));
986  temp &= (a != b);
987  }
988  catch(const Exception& /*ex*/)
989  {
990  temp = false;
991  }
992 
993  pass5 &= temp;
994  if (!temp)
995  std::cout << "FAILED:";
996  else
997  std::cout << "passed:";
998  std::cout << " Equality word64" << std::endl;
999 
1000 #if defined(CRYPTOPP_WORD128_AVAILABLE)
1001  // word128
1002  try
1003  {
1004  static const word128 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};
1005  static const word128 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};
1006  static const word128 str3[] = {0,1,2,3,4,5,6,7,8,9};
1007 
1008  temp = true;
1009  SecBlock<word128> a,b;
1010 
1011  a.Assign(str1, COUNTOF(str1));
1012  b.Assign(str1, COUNTOF(str1));
1013  temp &= (a.operator==(b));
1014 
1015  a.Assign(str3, COUNTOF(str3));
1016  b.Assign(str3, COUNTOF(str3));
1017  temp &= (a == b);
1018 
1019  a.Assign(str1, COUNTOF(str1));
1020  b.Assign(str2, COUNTOF(str2));
1021  temp &= (a.operator!=(b));
1022 
1023  a.Assign(str1, COUNTOF(str1));
1024  b.Assign(str3, COUNTOF(str3));
1025  temp &= (a != b);
1026  }
1027  catch(const Exception& /*ex*/)
1028  {
1029  temp = false;
1030  }
1031 
1032  pass5 &= temp;
1033  if (!temp)
1034  std::cout << "FAILED:";
1035  else
1036  std::cout << "passed:";
1037  std::cout << " Equality word128" << std::endl;
1038 #endif
1039 
1040  //********** Allocator Size/Overflow **********//
1041 
1042  try
1043  {
1044  temp = false;
1045 
1047  const size_t max = A.max_size();
1048  SecBlock<word32> t(max+1);
1049  }
1050  catch(const Exception& /*ex*/)
1051  {
1052  temp = true;
1053  }
1054  catch(const std::exception& /*ex*/)
1055  {
1056  temp = true;
1057  }
1058 
1059  pass6 &= temp;
1060  if (!temp)
1061  std::cout << "FAILED:";
1062  else
1063  std::cout << "passed:";
1064  std::cout << " Overflow word32" << std::endl;
1065 
1066  try
1067  {
1068  temp = false;
1069 
1071  const size_t max = A.max_size();
1072  SecBlock<word64> t(max+1);
1073  }
1074  catch(const Exception& /*ex*/)
1075  {
1076  temp = true;
1077  }
1078  catch(const std::exception& /*ex*/)
1079  {
1080  temp = true;
1081  }
1082 
1083  pass6 &= temp;
1084  if (!temp)
1085  std::cout << "FAILED:";
1086  else
1087  std::cout << "passed:";
1088  std::cout << " Overflow word64" << std::endl;
1089 
1090 #if defined(CRYPTOPP_WORD128_AVAILABLE)
1091  try
1092  {
1093  temp = false;
1094 
1096  const size_t max = A.max_size();
1097  SecBlock<word128> t(max+1);
1098  }
1099  catch(const Exception& /*ex*/)
1100  {
1101  temp = true;
1102  }
1103  catch(const std::exception& /*ex*/)
1104  {
1105  temp = true;
1106  }
1107 
1108  pass6 &= temp;
1109  if (!temp)
1110  std::cout << "FAILED:";
1111  else
1112  std::cout << "passed:";
1113  std::cout << " Overflow word128" << std::endl;
1114 #endif
1115 
1116  //********** FixedSizeAllocatorWithCleanup and Grow **********//
1117 
1118  // byte
1119  try
1120  {
1121  static const unsigned int SIZE = 8;
1122  SecBlockWithHint<byte, SIZE> block(SIZE);
1123  memset(block, 0xaa, block.SizeInBytes());
1124 
1125  temp = true;
1126  block.CleanGrow(SIZE*2);
1127  temp &= (block.size() == SIZE*2);
1128 
1129  for (size_t i = 0; i < block.size()/2; i++)
1130  temp &= (block[i] == 0xaa);
1131  for (size_t i = block.size()/2; i < block.size(); i++)
1132  temp &= (block[i] == 0);
1133 
1134  block.CleanNew(SIZE*4);
1135  temp &= (block.size() == SIZE*4);
1136  for (size_t i = 0; i < block.size(); i++)
1137  temp &= (block[i] == 0);
1138  }
1139  catch(const Exception& /*ex*/)
1140  {
1141  temp = false;
1142  }
1143  catch(const std::exception& /*ex*/)
1144  {
1145  temp = false;
1146  }
1147 
1148  pass7 &= temp;
1149  if (!temp)
1150  std::cout << "FAILED:";
1151  else
1152  std::cout << "passed:";
1153  std::cout << " FixedSizeAllocator Grow with byte" << std::endl;
1154 
1155  // word32
1156  try
1157  {
1158  static const unsigned int SIZE = 8;
1159  SecBlockWithHint<word32, SIZE> block(SIZE);
1160  memset(block, 0xaa, block.SizeInBytes());
1161 
1162  temp = true;
1163  block.CleanGrow(SIZE*2);
1164  temp &= (block.size() == SIZE*2);
1165 
1166  for (size_t i = 0; i < block.size()/2; i++)
1167  temp &= (block[i] == 0xaaaaaaaa);
1168 
1169  for (size_t i = block.size()/2; i < block.size(); i++)
1170  temp &= (block[i] == 0);
1171 
1172  block.CleanNew(SIZE*4);
1173  temp &= (block.size() == SIZE*4);
1174  for (size_t i = 0; i < block.size(); i++)
1175  temp &= (block[i] == 0);
1176  }
1177  catch(const Exception& /*ex*/)
1178  {
1179  temp = false;
1180  }
1181  catch(const std::exception& /*ex*/)
1182  {
1183  temp = false;
1184  }
1185 
1186  pass7 &= temp;
1187  if (!temp)
1188  std::cout << "FAILED:";
1189  else
1190  std::cout << "passed:";
1191  std::cout << " FixedSizeAllocator Grow with word32" << std::endl;
1192 
1193  // word64
1194  try
1195  {
1196  static const unsigned int SIZE = 8;
1197  SecBlockWithHint<word64, SIZE> block(SIZE);
1198  memset(block, 0xaa, block.SizeInBytes());
1199 
1200  temp = true;
1201  block.CleanGrow(SIZE*2);
1202  temp &= (block.size() == SIZE*2);
1203 
1204  for (size_t i = 0; i < block.size()/2; i++)
1205  temp &= (block[i] == W64LIT(0xaaaaaaaaaaaaaaaa));
1206 
1207  for (size_t i = block.size()/2; i < block.size(); i++)
1208  temp &= (block[i] == 0);
1209 
1210  block.CleanNew(SIZE*4);
1211  temp &= (block.size() == SIZE*4);
1212  for (size_t i = 0; i < block.size(); i++)
1213  temp &= (block[i] == 0);
1214  }
1215  catch(const Exception& /*ex*/)
1216  {
1217  temp = false;
1218  }
1219  catch(const std::exception& /*ex*/)
1220  {
1221  temp = false;
1222  }
1223 
1224  pass7 &= temp;
1225  if (!temp)
1226  std::cout << "FAILED:";
1227  else
1228  std::cout << "passed:";
1229  std::cout << " FixedSizeAllocator Grow with word64" << std::endl;
1230 
1231 #if defined(CRYPTOPP_WORD128_AVAILABLE)
1232  // word128
1233  try
1234  {
1235  static const unsigned int SIZE = 8;
1237  memset(block, 0xaa, block.SizeInBytes());
1238 
1239  temp = true;
1240  block.CleanGrow(SIZE*2);
1241  temp &= (block.size() == SIZE*2);
1242 
1243  for (size_t i = 0; i < block.size()/2; i++)
1244  temp &= (block[i] == (((word128)W64LIT(0xaaaaaaaaaaaaaaaa) << 64U) | W64LIT(0xaaaaaaaaaaaaaaaa)));
1245 
1246  for (size_t i = block.size()/2; i < block.size(); i++)
1247  temp &= (block[i] == 0);
1248 
1249  block.CleanNew(SIZE*4);
1250  temp &= (block.size() == SIZE*4);
1251  for (size_t i = 0; i < block.size(); i++)
1252  temp &= (block[i] == 0);
1253  }
1254  catch(const Exception& /*ex*/)
1255  {
1256  temp = false;
1257  }
1258  catch(const std::exception& /*ex*/)
1259  {
1260  temp = false;
1261  }
1262 
1263  pass7 &= temp;
1264  if (!temp)
1265  std::cout << "FAILED:";
1266  else
1267  std::cout << "passed:";
1268  std::cout << " FixedSizeAllocator Grow with word128" << std::endl;
1269 #endif
1270 
1271  return pass1 && pass2 && pass3 && pass4 && pass5 && pass6 && pass7;
1272 }
1273 #endif
1274 
1275 #if CRYPTOPP_DEBUG && !defined(CRYPTOPP_IMPORTS)
1276 bool TestHuffmanCodes()
1277 {
1278  std::cout << "\nTesting Huffman codes...\n\n";
1279  bool pass=true;
1280 
1281  static const size_t nCodes = 30;
1282  const unsigned int codeCounts[nCodes] = {
1283  1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1284  0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1285  0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
1286 
1287  static const unsigned int maxCodeBits = nCodes >> 1;
1288  unsigned int codeBits[nCodes] = {
1289  ~0u, ~0u, ~0u, ~0u, ~0u,
1290  ~0u, ~0u, ~0u, ~0u, ~0u,
1291  ~0u, ~0u, ~0u, ~0u, ~0u,
1292  };
1293 
1294  try
1295  {
1296  HuffmanEncoder::GenerateCodeLengths(codeBits, maxCodeBits, codeCounts, nCodes);
1297  }
1298  catch(const Exception& /*ex*/)
1299  {
1300  pass=false;
1301  }
1302 
1303  if (!pass)
1304  std::cout << "FAILED:";
1305  else
1306  std::cout << "passed:";
1307  std::cout << " GenerateCodeLengths" << std::endl;
1308 
1309  return pass;
1310 }
1311 #endif
1312 
1313 bool TestOS_RNG()
1314 {
1315  bool pass = true;
1316 
1318 
1319 #ifdef BLOCKING_RNG_AVAILABLE
1320  try {rng.reset(new BlockingRng);}
1321  catch (OS_RNG_Err &) {}
1322 #endif
1323 
1324  if (rng.get())
1325  {
1326  std::cout << "\nTesting operating system provided blocking random number generator...\n\n";
1327 
1328  MeterFilter meter(new Redirector(TheBitBucket()));
1329  RandomNumberSource test(*rng, UINT_MAX, false, new Deflator(new Redirector(meter)));
1330  unsigned long total=0, length=0;
1331  time_t t = time(NULL), t1 = 0;
1332  CRYPTOPP_UNUSED(length);
1333 
1334  // check that it doesn't take too long to generate a reasonable amount of randomness
1335  while (total < 16 && (t1 < 10 || total*8 > (unsigned long)t1))
1336  {
1337  test.Pump(1);
1338  total += 1;
1339  t1 = time(NULL) - t;
1340  }
1341 
1342  if (total < 16)
1343  {
1344  std::cout << "FAILED:";
1345  pass = false;
1346  }
1347  else
1348  std::cout << "passed:";
1349  std::cout << " it took " << long(t1) << " seconds to generate " << total << " bytes" << std::endl;
1350 
1351 #if 0 // disable this part. it's causing an unpredictable pause during the validation testing
1352  if (t1 < 2)
1353  {
1354  // that was fast, are we really blocking?
1355  // first exhaust the extropy reserve
1356  t = time(NULL);
1357  while (time(NULL) - t < 2)
1358  {
1359  test.Pump(1);
1360  total += 1;
1361  }
1362 
1363  // if it generates too many bytes in a certain amount of time,
1364  // something's probably wrong
1365  t = time(NULL);
1366  while (time(NULL) - t < 2)
1367  {
1368  test.Pump(1);
1369  total += 1;
1370  length += 1;
1371  }
1372  if (length > 1024)
1373  {
1374  std::cout << "FAILED:";
1375  pass = false;
1376  }
1377  else
1378  std::cout << "passed:";
1379  std::cout << " it generated " << length << " bytes in " << long(time(NULL) - t) << " seconds" << std::endl;
1380  }
1381 #endif
1382 
1384 
1385  if (meter.GetTotalBytes() < total)
1386  {
1387  std::cout << "FAILED:";
1388  pass = false;
1389  }
1390  else
1391  std::cout << "passed:";
1392  std::cout << " " << total << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << std::endl;
1393  }
1394  else
1395  std::cout << "\nNo operating system provided blocking random number generator, skipping test." << std::endl;
1396 
1397  rng.reset(NULL);
1398 #ifdef NONBLOCKING_RNG_AVAILABLE
1399  try {rng.reset(new NonblockingRng);}
1400  catch (OS_RNG_Err &) {}
1401 #endif
1402 
1403  if (rng.get())
1404  {
1405  std::cout << "\nTesting operating system provided nonblocking random number generator...\n\n";
1406 
1407  MeterFilter meter(new Redirector(TheBitBucket()));
1408  RandomNumberSource test(*rng, 100000, true, new Deflator(new Redirector(meter)));
1409 
1410  if (meter.GetTotalBytes() < 100000)
1411  {
1412  std::cout << "FAILED:";
1413  pass = false;
1414  }
1415  else
1416  std::cout << "passed:";
1417  std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << std::endl;
1418  }
1419  else
1420  std::cout << "\nNo operating system provided nonblocking random number generator, skipping test." << std::endl;
1421 
1422  return pass;
1423 }
1424 
1425 #if defined(NO_OS_DEPENDENCE) || !defined(OS_RNG_AVAILABLE)
1426 bool TestAutoSeeded()
1427 {
1428  return true;
1429 }
1430 bool TestAutoSeededX917()
1431 {
1432  return true;
1433 }
1434 #else
1435 bool TestAutoSeeded()
1436 {
1437  // This tests Auto-Seeding and GenerateIntoBufferedTransformation.
1438  std::cout << "\nTesting AutoSeeded generator...\n\n";
1439 
1440  AutoSeededRandomPool prng;
1441  static const unsigned int ENTROPY_SIZE = 32;
1442  bool generate = true, discard = true, incorporate = false;
1443 
1444  MeterFilter meter(new Redirector(TheBitBucket()));
1445  RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter)));
1446 
1447  if (meter.GetTotalBytes() < 100000)
1448  {
1449  std::cout << "FAILED:";
1450  generate = false;
1451  }
1452  else
1453  std::cout << "passed:";
1454  std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << std::endl;
1455 
1456  try
1457  {
1458  prng.DiscardBytes(100000);
1459  }
1460  catch(const Exception&)
1461  {
1462  discard = false;
1463  }
1464 
1465  if (!discard)
1466  std::cout << "FAILED:";
1467  else
1468  std::cout << "passed:";
1469  std::cout << " discarded 10000 bytes" << std::endl;
1470 
1471  try
1472  {
1473  if(prng.CanIncorporateEntropy())
1474  {
1475  SecByteBlock entropy(ENTROPY_SIZE);
1476  OS_GenerateRandomBlock(false, entropy, entropy.SizeInBytes());
1477 
1478  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1479  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1480  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1481  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1482 
1483  incorporate = true;
1484  }
1485  }
1486  catch(const Exception& /*ex*/)
1487  {
1488  }
1489 
1490  if (!incorporate)
1491  std::cout << "FAILED:";
1492  else
1493  std::cout << "passed:";
1494  std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << std::endl;
1495 
1496  return generate && discard && incorporate;
1497 }
1498 
1499 bool TestAutoSeededX917()
1500 {
1501  // This tests Auto-Seeding and GenerateIntoBufferedTransformation.
1502  std::cout << "\nTesting AutoSeeded X917 generator...\n\n";
1503 
1505  static const unsigned int ENTROPY_SIZE = 32;
1506  bool generate = true, discard = true, incorporate = false;
1507 
1508  MeterFilter meter(new Redirector(TheBitBucket()));
1509  RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter)));
1510 
1511  if (meter.GetTotalBytes() < 100000)
1512  {
1513  std::cout << "FAILED:";
1514  generate = false;
1515  }
1516  else
1517  std::cout << "passed:";
1518  std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << std::endl;
1519 
1520  try
1521  {
1522  prng.DiscardBytes(100000);
1523  }
1524  catch(const Exception&)
1525  {
1526  discard = false;
1527  }
1528 
1529  if (!discard)
1530  std::cout << "FAILED:";
1531  else
1532  std::cout << "passed:";
1533  std::cout << " discarded 10000 bytes" << std::endl;
1534 
1535  try
1536  {
1537  if(prng.CanIncorporateEntropy())
1538  {
1539  SecByteBlock entropy(ENTROPY_SIZE);
1540  OS_GenerateRandomBlock(false, entropy, entropy.SizeInBytes());
1541 
1542  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1543  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1544  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1545  prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
1546 
1547  incorporate = true;
1548  }
1549  }
1550  catch(const Exception& /*ex*/)
1551  {
1552  }
1553 
1554  if (!incorporate)
1555  std::cout << "FAILED:";
1556  else
1557  std::cout << "passed:";
1558  std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << std::endl;
1559 
1560  return generate && discard && incorporate;
1561 }
1562 #endif // NO_OS_DEPENDENCE
1563 
1564 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
1565 bool TestRDRAND()
1566 {
1567  // Testing on 6th generation i7 shows RDRAND needs less than 8 retries for 10K bytes.
1568  RDRAND rdrand;
1569  bool entropy = true, compress = true, discard = true;
1570  static const unsigned int SIZE = 10000;
1571 
1572  if (HasRDRAND())
1573  {
1574  std::cout << "\nTesting RDRAND generator...\n\n";
1575 
1576  MeterFilter meter(new Redirector(TheBitBucket()));
1577  Deflator deflator(new Redirector(meter));
1578  MaurerRandomnessTest maurer;
1579 
1580  ChannelSwitch chsw;
1581  chsw.AddDefaultRoute(deflator);
1582  chsw.AddDefaultRoute(maurer);
1583 
1584  RandomNumberSource rns(rdrand, SIZE, true, new Redirector(chsw));
1585  deflator.Flush(true);
1586 
1587  CRYPTOPP_ASSERT(0 == maurer.BytesNeeded());
1588  const double mv = maurer.GetTestValue();
1589  if (mv < 0.98f)
1590  {
1591  std::cout << "FAILED:";
1592  entropy = false;
1593  }
1594  else
1595  std::cout << "passed:";
1596 
1597  // Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
1598  StreamState ss(std::cout);
1599  std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
1600  std::cout << " Maurer Randomness Test returned value " << mv << std::endl;
1601 
1602  if (meter.GetTotalBytes() < SIZE)
1603  {
1604  std::cout << "FAILED:";
1605  compress = false;
1606  }
1607  else
1608  std::cout << "passed:";
1609  std::cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
1610 
1611  try
1612  {
1613  rdrand.DiscardBytes(SIZE);
1614  }
1615  catch(const Exception&)
1616  {
1617  discard = false;
1618  }
1619 
1620  if (!discard)
1621  std::cout << "FAILED:";
1622  else
1623  std::cout << "passed:";
1624  std::cout << " discarded " << SIZE << " bytes\n";
1625  }
1626  else
1627  std::cout << "\nRDRAND generator not available, skipping test.\n";
1628 
1629  // Squash code coverage warnings on unused functions
1630  (void)rdrand.AlgorithmName();
1631  (void)rdrand.CanIncorporateEntropy();
1632  rdrand.SetRetries(rdrand.GetRetries());
1633  rdrand.IncorporateEntropy(NULL, 0);
1634 
1635  if (!(entropy && compress && discard))
1636  std::cout.flush();
1637 
1638  return entropy && compress && discard;
1639 }
1640 #endif
1641 
1642 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
1643 bool TestRDSEED()
1644 {
1645  // Testing on 5th generation i5 shows RDSEED needs about 128 retries for 10K bytes
1646  // on 64-bit/amd64 VM, and it needs more for an 32-bit/i686 VM.
1647  RDSEED rdseed(256);
1648  bool entropy = true, compress = true, discard = true;
1649  static const unsigned int SIZE = 10000;
1650 
1651  if (HasRDSEED())
1652  {
1653  std::cout << "\nTesting RDSEED generator...\n\n";
1654 
1655  MeterFilter meter(new Redirector(TheBitBucket()));
1656  Deflator deflator(new Redirector(meter));
1657  MaurerRandomnessTest maurer;
1658 
1659  ChannelSwitch chsw;
1660  chsw.AddDefaultRoute(deflator);
1661  chsw.AddDefaultRoute(maurer);
1662 
1663  RandomNumberSource rns(rdseed, SIZE, true, new Redirector(chsw));
1664  deflator.Flush(true);
1665 
1666  CRYPTOPP_ASSERT(0 == maurer.BytesNeeded());
1667  const double mv = maurer.GetTestValue();
1668  if (mv < 0.98f)
1669  {
1670  std::cout << "FAILED:";
1671  entropy = false;
1672  }
1673  else
1674  std::cout << "passed:";
1675 
1676  // Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
1677  StreamState ss(std::cout);
1678  std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
1679  std::cout << " Maurer Randomness Test returned value " << mv << std::endl;
1680 
1681  if (meter.GetTotalBytes() < SIZE)
1682  {
1683  std::cout << "FAILED:";
1684  compress = false;
1685  }
1686  else
1687  std::cout << "passed:";
1688  std::cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
1689 
1690  try
1691  {
1692  rdseed.DiscardBytes(SIZE);
1693  }
1694  catch(const Exception&)
1695  {
1696  discard = false;
1697  }
1698 
1699  if (!discard)
1700  std::cout << "FAILED:";
1701  else
1702  std::cout << "passed:";
1703  std::cout << " discarded " << SIZE << " bytes\n";
1704  }
1705  else
1706  std::cout << "\nRDSEED generator not available, skipping test.\n";
1707 
1708  // Squash code coverage warnings on unused functions
1709  (void)rdseed.AlgorithmName();
1710  (void)rdseed.CanIncorporateEntropy();
1711  rdseed.SetRetries(rdseed.GetRetries());
1712  rdseed.IncorporateEntropy(NULL, 0);
1713 
1714  if (!(entropy && compress && discard))
1715  std::cout.flush();
1716 
1717  return entropy && compress && discard;
1718 }
1719 #endif
1720 
1721 // VC50 workaround
1722 typedef std::auto_ptr<BlockTransformation> apbt;
1723 
1725 {
1726 public:
1727  virtual unsigned int BlockSize() const =0;
1728  virtual unsigned int KeyLength() const =0;
1729 
1730  virtual apbt NewEncryption(const byte *keyStr) const =0;
1731  virtual apbt NewDecryption(const byte *keyStr) const =0;
1732 };
1733 
1734 template <class E, class D> class FixedRoundsCipherFactory : public CipherFactory
1735 {
1736 public:
1737  FixedRoundsCipherFactory(unsigned int keylen=0) : m_keylen(keylen?keylen:E::DEFAULT_KEYLENGTH) {}
1738  unsigned int BlockSize() const {return E::BLOCKSIZE;}
1739  unsigned int KeyLength() const {return m_keylen;}
1740 
1741  apbt NewEncryption(const byte *keyStr) const
1742  {return apbt(new E(keyStr, m_keylen));}
1743  apbt NewDecryption(const byte *keyStr) const
1744  {return apbt(new D(keyStr, m_keylen));}
1745 
1746  unsigned int m_keylen;
1747 };
1748 
1749 template <class E, class D> class VariableRoundsCipherFactory : public CipherFactory
1750 {
1751 public:
1752  VariableRoundsCipherFactory(unsigned int keylen=0, unsigned int rounds=0)
1753  : m_keylen(keylen ? keylen : E::DEFAULT_KEYLENGTH), m_rounds(rounds ? rounds : E::DEFAULT_ROUNDS) {}
1754  unsigned int BlockSize() const {return E::BLOCKSIZE;}
1755  unsigned int KeyLength() const {return m_keylen;}
1756 
1757  apbt NewEncryption(const byte *keyStr) const
1758  {return apbt(new E(keyStr, m_keylen, m_rounds));}
1759  apbt NewDecryption(const byte *keyStr) const
1760  {return apbt(new D(keyStr, m_keylen, m_rounds));}
1761 
1762  unsigned int m_keylen, m_rounds;
1763 };
1764 
1765 bool BlockTransformationTest(const CipherFactory &cg, BufferedTransformation &valdata, unsigned int tuples = 0xffff)
1766 {
1767  HexEncoder output(new FileSink(std::cout));
1768  SecByteBlock plain(cg.BlockSize()), cipher(cg.BlockSize()), out(cg.BlockSize()), outplain(cg.BlockSize());
1769  SecByteBlock key(cg.KeyLength());
1770  bool pass=true, fail;
1771 
1772  while (valdata.MaxRetrievable() && tuples--)
1773  {
1774  valdata.Get(key, cg.KeyLength());
1775  valdata.Get(plain, cg.BlockSize());
1776  valdata.Get(cipher, cg.BlockSize());
1777 
1778  apbt transE = cg.NewEncryption(key);
1779  transE->ProcessBlock(plain, out);
1780  fail = memcmp(out, cipher, cg.BlockSize()) != 0;
1781 
1782  apbt transD = cg.NewDecryption(key);
1783  transD->ProcessBlock(out, outplain);
1784  fail=fail || memcmp(outplain, plain, cg.BlockSize());
1785 
1786  pass = pass && !fail;
1787 
1788  std::cout << (fail ? "FAILED " : "passed ");
1789  output.Put(key, cg.KeyLength());
1790  std::cout << " ";
1791  output.Put(outplain, cg.BlockSize());
1792  std::cout << " ";
1793  output.Put(out, cg.BlockSize());
1794  std::cout << std::endl;
1795  }
1796  return pass;
1797 }
1798 
1799 class FilterTester : public Unflushable<Sink>
1800 {
1801 public:
1802  FilterTester(const byte *validOutput, size_t outputLen)
1803  : validOutput(validOutput), outputLen(outputLen), counter(0), fail(false) {}
1804  void PutByte(byte inByte)
1805  {
1806  if (counter >= outputLen || validOutput[counter] != inByte)
1807  {
1808  std::cerr << "incorrect output " << counter << ", " << (word16)validOutput[counter] << ", " << (word16)inByte << "\n";
1809  fail = true;
1810  CRYPTOPP_ASSERT(false);
1811  }
1812  counter++;
1813  }
1814  size_t Put2(const byte *inString, size_t length, int messageEnd, bool blocking)
1815  {
1816  CRYPTOPP_UNUSED(messageEnd), CRYPTOPP_UNUSED(blocking);
1817 
1818  while (length--)
1819  FilterTester::PutByte(*inString++);
1820 
1821  if (messageEnd)
1822  if (counter != outputLen)
1823  {
1824  fail = true;
1825  CRYPTOPP_ASSERT(false);
1826  }
1827 
1828  return 0;
1829  }
1830  bool GetResult()
1831  {
1832  return !fail;
1833  }
1834 
1835  const byte *validOutput;
1836  size_t outputLen, counter;
1837  bool fail;
1838 };
1839 
1840 bool TestFilter(BufferedTransformation &bt, const byte *in, size_t inLen, const byte *out, size_t outLen)
1841 {
1842  FilterTester *ft;
1843  bt.Attach(ft = new FilterTester(out, outLen));
1844 
1845  while (inLen)
1846  {
1847  size_t randomLen = GlobalRNG().GenerateWord32(0, (word32)inLen);
1848  bt.Put(in, randomLen);
1849  in += randomLen;
1850  inLen -= randomLen;
1851  }
1852  bt.MessageEnd();
1853  return ft->GetResult();
1854 }
1855 
1856 bool ValidateDES()
1857 {
1858  std::cout << "\nDES validation suite running...\n\n";
1859 
1860  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/descert.dat", true, new HexDecoder);
1861  bool pass = BlockTransformationTest(FixedRoundsCipherFactory<DESEncryption, DESDecryption>(), valdata);
1862 
1863  std::cout << "\nTesting EDE2, EDE3, and XEX3 variants...\n\n";
1864 
1865  FileSource valdata1(CRYPTOPP_DATA_DIR "TestData/3desval.dat", true, new HexDecoder);
1866  pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE2_Encryption, DES_EDE2_Decryption>(), valdata1, 1) && pass;
1867  pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_EDE3_Encryption, DES_EDE3_Decryption>(), valdata1, 1) && pass;
1868  pass = BlockTransformationTest(FixedRoundsCipherFactory<DES_XEX3_Encryption, DES_XEX3_Decryption>(), valdata1, 1) && pass;
1869 
1870  return pass;
1871 }
1872 
1873 bool TestModeIV(SymmetricCipher &e, SymmetricCipher &d)
1874 {
1875  SecByteBlock lastIV, iv(e.IVSize());
1877 
1878  // vector_ptr<byte> due to Enterprise Analysis finding on the stack based array.
1879  vector_ptr<byte> plaintext(20480);
1880 
1881  for (unsigned int i=1; i<20480; i*=2)
1882  {
1883  e.GetNextIV(GlobalRNG(), iv);
1884  if (iv == lastIV)
1885  return false;
1886  else
1887  lastIV = iv;
1888 
1889  e.Resynchronize(iv);
1890  d.Resynchronize(iv);
1891 
1892  unsigned int length = STDMAX(GlobalRNG().GenerateWord32(0, i), (word32)e.MinLastBlockSize());
1893  GlobalRNG().GenerateBlock(plaintext, length);
1894 
1895  if (!TestFilter(filter, plaintext, length, plaintext, length))
1896  return false;
1897  }
1898 
1899  return true;
1900 }
1901 
1902 bool ValidateCipherModes()
1903 {
1904  std::cout << "\nTesting DES modes...\n\n";
1905  const byte key[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
1906  const byte iv[] = {0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef};
1907  const byte plain[] = { // "Now is the time for all " without tailing 0
1908  0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
1909  0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
1910  0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20};
1911  DESEncryption desE(key);
1912  DESDecryption desD(key);
1913  bool pass=true, fail;
1914 
1915  {
1916  // from FIPS 81
1917  const byte encrypted[] = {
1918  0x3f, 0xa4, 0x0e, 0x8a, 0x98, 0x4d, 0x48, 0x15,
1919  0x6a, 0x27, 0x17, 0x87, 0xab, 0x88, 0x83, 0xf9,
1920  0x89, 0x3d, 0x51, 0xec, 0x4b, 0x56, 0x3b, 0x53};
1921 
1923  fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
1924  plain, sizeof(plain), encrypted, sizeof(encrypted));
1925  pass = pass && !fail;
1926  std::cout << (fail ? "FAILED " : "passed ") << "ECB encryption" << std::endl;
1927 
1929  fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
1930  encrypted, sizeof(encrypted), plain, sizeof(plain));
1931  pass = pass && !fail;
1932  std::cout << (fail ? "FAILED " : "passed ") << "ECB decryption" << std::endl;
1933  }
1934  {
1935  // from FIPS 81
1936  const byte encrypted[] = {
1937  0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
1938  0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
1939  0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6};
1940 
1941  CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
1942  fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
1943  plain, sizeof(plain), encrypted, sizeof(encrypted));
1944  pass = pass && !fail;
1945  std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with no padding" << std::endl;
1946 
1947  CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
1948  fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::NO_PADDING).Ref(),
1949  encrypted, sizeof(encrypted), plain, sizeof(plain));
1950  pass = pass && !fail;
1951  std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with no padding" << std::endl;
1952 
1953  fail = !TestModeIV(modeE, modeD);
1954  pass = pass && !fail;
1955  std::cout << (fail ? "FAILED " : "passed ") << "CBC mode IV generation" << std::endl;
1956  }
1957  {
1958  // generated with Crypto++, matches FIPS 81
1959  // but has extra 8 bytes as result of padding
1960  const byte encrypted[] = {
1961  0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
1962  0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
1963  0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
1964  0x62, 0xC1, 0x6A, 0x27, 0xE4, 0xFC, 0xF2, 0x77};
1965 
1966  CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
1967  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
1968  plain, sizeof(plain), encrypted, sizeof(encrypted));
1969  pass = pass && !fail;
1970  std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with PKCS #7 padding" << std::endl;
1971 
1972  CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
1973  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
1974  encrypted, sizeof(encrypted), plain, sizeof(plain));
1975  pass = pass && !fail;
1976  std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with PKCS #7 padding" << std::endl;
1977  }
1978  {
1979  // generated with Crypto++ 5.2, matches FIPS 81
1980  // but has extra 8 bytes as result of padding
1981  const byte encrypted[] = {
1982  0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
1983  0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F,
1984  0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
1985  0xcf, 0xb7, 0xc7, 0x64, 0x0e, 0x7c, 0xd9, 0xa7};
1986 
1987  CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
1989  plain, sizeof(plain), encrypted, sizeof(encrypted));
1990  pass = pass && !fail;
1991  std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with one-and-zeros padding" << std::endl;
1992 
1993  CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
1995  encrypted, sizeof(encrypted), plain, sizeof(plain));
1996  pass = pass && !fail;
1997  std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with one-and-zeros padding" << std::endl;
1998  }
1999  {
2000  const byte plain_1[] = {'a', 0, 0, 0, 0, 0, 0, 0};
2001  // generated with Crypto++
2002  const byte encrypted[] = {
2003  0x9B, 0x47, 0x57, 0x59, 0xD6, 0x9C, 0xF6, 0xD0};
2004 
2005  CBC_Mode_ExternalCipher::Encryption modeE(desE, iv);
2006  fail = !TestFilter(StreamTransformationFilter(modeE, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),
2007  plain_1, 1, encrypted, sizeof(encrypted));
2008  pass = pass && !fail;
2009  std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with zeros padding" << std::endl;
2010 
2011  CBC_Mode_ExternalCipher::Decryption modeD(desD, iv);
2012  fail = !TestFilter(StreamTransformationFilter(modeD, NULL, StreamTransformationFilter::ZEROS_PADDING).Ref(),
2013  encrypted, sizeof(encrypted), plain_1, sizeof(plain_1));
2014  pass = pass && !fail;
2015  std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with zeros padding" << std::endl;
2016  }
2017  {
2018  // generated with Crypto++, matches FIPS 81
2019  // but with last two blocks swapped as result of CTS
2020  const byte encrypted[] = {
2021  0xE5, 0xC7, 0xCD, 0xDE, 0x87, 0x2B, 0xF2, 0x7C,
2022  0x68, 0x37, 0x88, 0x49, 0x9A, 0x7C, 0x05, 0xF6,
2023  0x43, 0xE9, 0x34, 0x00, 0x8C, 0x38, 0x9C, 0x0F};
2024 
2026  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
2027  plain, sizeof(plain), encrypted, sizeof(encrypted));
2028  pass = pass && !fail;
2029  std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext stealing (CTS)" << std::endl;
2030 
2032  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
2033  encrypted, sizeof(encrypted), plain, sizeof(plain));
2034  pass = pass && !fail;
2035  std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext stealing (CTS)" << std::endl;
2036 
2037  fail = !TestModeIV(modeE, modeD);
2038  pass = pass && !fail;
2039  std::cout << (fail ? "FAILED " : "passed ") << "CBC CTS IV generation" << std::endl;
2040  }
2041  {
2042  // generated with Crypto++
2043  const byte decryptionIV[] = {0x4D, 0xD0, 0xAC, 0x8F, 0x47, 0xCF, 0x79, 0xCE};
2044  const byte encrypted[] = {0x12, 0x34, 0x56};
2045 
2046  byte stolenIV[8];
2047 
2049  modeE.SetStolenIV(stolenIV);
2050  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
2051  plain, 3, encrypted, sizeof(encrypted));
2052  fail = memcmp(stolenIV, decryptionIV, 8) != 0 || fail;
2053  pass = pass && !fail;
2054  std::cout << (fail ? "FAILED " : "passed ") << "CBC encryption with ciphertext and IV stealing" << std::endl;
2055 
2056  CBC_CTS_Mode_ExternalCipher::Decryption modeD(desD, stolenIV);
2057  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
2058  encrypted, sizeof(encrypted), plain, 3);
2059  pass = pass && !fail;
2060  std::cout << (fail ? "FAILED " : "passed ") << "CBC decryption with ciphertext and IV stealing" << std::endl;
2061  }
2062  {
2063  const byte encrypted[] = { // from FIPS 81
2064  0xF3,0x09,0x62,0x49,0xC7,0xF4,0x6E,0x51,
2065  0xA6,0x9E,0x83,0x9B,0x1A,0x92,0xF7,0x84,
2066  0x03,0x46,0x71,0x33,0x89,0x8E,0xA6,0x22};
2067 
2068  CFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
2069  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
2070  plain, sizeof(plain), encrypted, sizeof(encrypted));
2071  pass = pass && !fail;
2072  std::cout << (fail ? "FAILED " : "passed ") << "CFB encryption" << std::endl;
2073 
2074  CFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
2075  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
2076  encrypted, sizeof(encrypted), plain, sizeof(plain));
2077  pass = pass && !fail;
2078  std::cout << (fail ? "FAILED " : "passed ") << "CFB decryption" << std::endl;
2079 
2080  fail = !TestModeIV(modeE, modeD);
2081  pass = pass && !fail;
2082  std::cout << (fail ? "FAILED " : "passed ") << "CFB mode IV generation" << std::endl;
2083  }
2084  {
2085  const byte plain_2[] = { // "Now is the." without tailing 0
2086  0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,0x68,0x65};
2087  const byte encrypted[] = { // from FIPS 81
2088  0xf3,0x1f,0xda,0x07,0x01,0x14,0x62,0xee,0x18,0x7f};
2089 
2090  CFB_Mode_ExternalCipher::Encryption modeE(desE, iv, 1);
2091  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
2092  plain_2, sizeof(plain_2), encrypted, sizeof(encrypted));
2093  pass = pass && !fail;
2094  std::cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) encryption" << std::endl;
2095 
2096  CFB_Mode_ExternalCipher::Decryption modeD(desE, iv, 1);
2097  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
2098  encrypted, sizeof(encrypted), plain_2, sizeof(plain_2));
2099  pass = pass && !fail;
2100  std::cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) decryption" << std::endl;
2101 
2102  fail = !TestModeIV(modeE, modeD);
2103  pass = pass && !fail;
2104  std::cout << (fail ? "FAILED " : "passed ") << "CFB (8-bit feedback) IV generation" << std::endl;
2105  }
2106  {
2107  const byte encrypted[] = { // from Eric Young's libdes
2108  0xf3,0x09,0x62,0x49,0xc7,0xf4,0x6e,0x51,
2109  0x35,0xf2,0x4a,0x24,0x2e,0xeb,0x3d,0x3f,
2110  0x3d,0x6d,0x5b,0xe3,0x25,0x5a,0xf8,0xc3};
2111 
2112  OFB_Mode_ExternalCipher::Encryption modeE(desE, iv);
2113  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
2114  plain, sizeof(plain), encrypted, sizeof(encrypted));
2115  pass = pass && !fail;
2116  std::cout << (fail ? "FAILED " : "passed ") << "OFB encryption" << std::endl;
2117 
2118  OFB_Mode_ExternalCipher::Decryption modeD(desE, iv);
2119  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
2120  encrypted, sizeof(encrypted), plain, sizeof(plain));
2121  pass = pass && !fail;
2122  std::cout << (fail ? "FAILED " : "passed ") << "OFB decryption" << std::endl;
2123 
2124  fail = !TestModeIV(modeE, modeD);
2125  pass = pass && !fail;
2126  std::cout << (fail ? "FAILED " : "passed ") << "OFB IV generation" << std::endl;
2127  }
2128  {
2129  const byte encrypted[] = { // generated with Crypto++
2130  0xF3, 0x09, 0x62, 0x49, 0xC7, 0xF4, 0x6E, 0x51,
2131  0x16, 0x3A, 0x8C, 0xA0, 0xFF, 0xC9, 0x4C, 0x27,
2132  0xFA, 0x2F, 0x80, 0xF4, 0x80, 0xB8, 0x6F, 0x75};
2133 
2134  CTR_Mode_ExternalCipher::Encryption modeE(desE, iv);
2135  fail = !TestFilter(StreamTransformationFilter(modeE).Ref(),
2136  plain, sizeof(plain), encrypted, sizeof(encrypted));
2137  pass = pass && !fail;
2138  std::cout << (fail ? "FAILED " : "passed ") << "Counter Mode encryption" << std::endl;
2139 
2140  CTR_Mode_ExternalCipher::Decryption modeD(desE, iv);
2141  fail = !TestFilter(StreamTransformationFilter(modeD).Ref(),
2142  encrypted, sizeof(encrypted), plain, sizeof(plain));
2143  pass = pass && !fail;
2144  std::cout << (fail ? "FAILED " : "passed ") << "Counter Mode decryption" << std::endl;
2145 
2146  fail = !TestModeIV(modeE, modeD);
2147  pass = pass && !fail;
2148  std::cout << (fail ? "FAILED " : "passed ") << "Counter Mode IV generation" << std::endl;
2149  }
2150  {
2151  const byte plain_3[] = { // "7654321 Now is the time for "
2152  0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x20,
2153  0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
2154  0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
2155  0x66, 0x6f, 0x72, 0x20};
2156  const byte mac1[] = { // from FIPS 113
2157  0xf1, 0xd3, 0x0f, 0x68, 0x49, 0x31, 0x2c, 0xa4};
2158  const byte mac2[] = { // generated with Crypto++
2159  0x35, 0x80, 0xC5, 0xC4, 0x6B, 0x81, 0x24, 0xE2};
2160 
2161  CBC_MAC<DES> cbcmac(key);
2162  HashFilter cbcmacFilter(cbcmac);
2163  fail = !TestFilter(cbcmacFilter, plain_3, sizeof(plain_3), mac1, sizeof(mac1));
2164  pass = pass && !fail;
2165  std::cout << (fail ? "FAILED " : "passed ") << "CBC MAC" << std::endl;
2166 
2167  DMAC<DES> dmac(key);
2168  HashFilter dmacFilter(dmac);
2169  fail = !TestFilter(dmacFilter, plain_3, sizeof(plain_3), mac2, sizeof(mac2));
2170  pass = pass && !fail;
2171  std::cout << (fail ? "FAILED " : "passed ") << "DMAC" << std::endl;
2172  }
2173  {
2174  CTR_Mode<AES>::Encryption modeE(plain, 16, plain);
2175  CTR_Mode<AES>::Decryption modeD(plain, 16, plain);
2176  fail = !TestModeIV(modeE, modeD);
2177  pass = pass && !fail;
2178  std::cout << (fail ? "FAILED " : "passed ") << "AES CTR Mode" << std::endl;
2179  }
2180  {
2181  OFB_Mode<AES>::Encryption modeE(plain, 16, plain);
2182  OFB_Mode<AES>::Decryption modeD(plain, 16, plain);
2183  fail = !TestModeIV(modeE, modeD);
2184  pass = pass && !fail;
2185  std::cout << (fail ? "FAILED " : "passed ") << "AES OFB Mode" << std::endl;
2186  }
2187  {
2188  CFB_Mode<AES>::Encryption modeE(plain, 16, plain);
2189  CFB_Mode<AES>::Decryption modeD(plain, 16, plain);
2190  fail = !TestModeIV(modeE, modeD);
2191  pass = pass && !fail;
2192  std::cout << (fail ? "FAILED " : "passed ") << "AES CFB Mode" << std::endl;
2193  }
2194  {
2195  CBC_Mode<AES>::Encryption modeE(plain, 16, plain);
2196  CBC_Mode<AES>::Decryption modeD(plain, 16, plain);
2197  fail = !TestModeIV(modeE, modeD);
2198  pass = pass && !fail;
2199  std::cout << (fail ? "FAILED " : "passed ") << "AES CBC Mode" << std::endl;
2200  }
2201 
2202  return pass;
2203 }
2204 
2205 bool ValidateIDEA()
2206 {
2207  std::cout << "\nIDEA validation suite running...\n\n";
2208 
2209  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/ideaval.dat", true, new HexDecoder);
2210  return BlockTransformationTest(FixedRoundsCipherFactory<IDEAEncryption, IDEADecryption>(), valdata);
2211 }
2212 
2213 bool ValidateSAFER()
2214 {
2215  std::cout << "\nSAFER validation suite running...\n\n";
2216 
2217  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/saferval.dat", true, new HexDecoder);
2218  bool pass = true;
2219  pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(8,6), valdata, 4) && pass;
2220  pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_K_Encryption, SAFER_K_Decryption>(16,12), valdata, 4) && pass;
2221  pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(8,6), valdata, 4) && pass;
2222  pass = BlockTransformationTest(VariableRoundsCipherFactory<SAFER_SK_Encryption, SAFER_SK_Decryption>(16,10), valdata, 4) && pass;
2223  return pass;
2224 }
2225 
2226 bool ValidateRC2()
2227 {
2228  std::cout << "\nRC2 validation suite running...\n\n";
2229 
2230  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rc2val.dat", true, new HexDecoder);
2231  HexEncoder output(new FileSink(std::cout));
2232  SecByteBlock plain(RC2Encryption::BLOCKSIZE), cipher(RC2Encryption::BLOCKSIZE), out(RC2Encryption::BLOCKSIZE), outplain(RC2Encryption::BLOCKSIZE);
2233  SecByteBlock key(128);
2234  bool pass=true, fail;
2235 
2236  while (valdata.MaxRetrievable())
2237  {
2238  byte keyLen, effectiveLen;
2239 
2240  valdata.Get(keyLen);
2241  valdata.Get(effectiveLen);
2242  valdata.Get(key, keyLen);
2243  valdata.Get(plain, RC2Encryption::BLOCKSIZE);
2244  valdata.Get(cipher, RC2Encryption::BLOCKSIZE);
2245 
2246  apbt transE(new RC2Encryption(key, keyLen, effectiveLen));
2247  transE->ProcessBlock(plain, out);
2248  fail = memcmp(out, cipher, RC2Encryption::BLOCKSIZE) != 0;
2249 
2250  apbt transD(new RC2Decryption(key, keyLen, effectiveLen));
2251  transD->ProcessBlock(out, outplain);
2252  fail=fail || memcmp(outplain, plain, RC2Encryption::BLOCKSIZE);
2253 
2254  pass = pass && !fail;
2255 
2256  std::cout << (fail ? "FAILED " : "passed ");
2257  output.Put(key, keyLen);
2258  std::cout << " ";
2259  output.Put(outplain, RC2Encryption::BLOCKSIZE);
2260  std::cout << " ";
2261  output.Put(out, RC2Encryption::BLOCKSIZE);
2262  std::cout << std::endl;
2263  }
2264  return pass;
2265 }
2266 
2267 bool ValidateARC4()
2268 {
2269  unsigned char Key0[] = {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef };
2270  unsigned char Input0[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
2271  unsigned char Output0[] = {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96};
2272 
2273  unsigned char Key1[]={0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef};
2274  unsigned char Input1[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
2275  unsigned char Output1[]={0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79};
2276 
2277  unsigned char Key2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
2278  unsigned char Input2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
2279  unsigned char Output2[]={0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a};
2280 
2281  unsigned char Key3[]={0xef,0x01,0x23,0x45};
2282  unsigned char Input3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
2283  unsigned char Output3[]={0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61};
2284 
2285  unsigned char Key4[]={ 0x01,0x23,0x45,0x67,0x89,0xab, 0xcd,0xef };
2286  unsigned char Input4[] =
2287  {0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2288  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2289  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2290  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2291  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2292  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2293  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2294  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2295  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2296  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2297  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2298  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2299  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2300  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2301  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2302  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2303  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2304  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2305  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2306  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2307  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2308  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2309  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2310  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2311  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2312  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2313  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2314  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2315  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2316  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2317  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2318  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2319  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2320  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2321  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2322  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2323  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2324  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2325  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2326  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2327  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2328  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2329  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2330  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2331  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2332  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2333  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2334  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2335  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2336  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2337  0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
2338  0x01};
2339  unsigned char Output4[]= {
2340  0x75,0x95,0xc3,0xe6,0x11,0x4a,0x09,0x78,0x0c,0x4a,0xd4,
2341  0x52,0x33,0x8e,0x1f,0xfd,0x9a,0x1b,0xe9,0x49,0x8f,
2342  0x81,0x3d,0x76,0x53,0x34,0x49,0xb6,0x77,0x8d,0xca,
2343  0xd8,0xc7,0x8a,0x8d,0x2b,0xa9,0xac,0x66,0x08,0x5d,
2344  0x0e,0x53,0xd5,0x9c,0x26,0xc2,0xd1,0xc4,0x90,0xc1,
2345  0xeb,0xbe,0x0c,0xe6,0x6d,0x1b,0x6b,0x1b,0x13,0xb6,
2346  0xb9,0x19,0xb8,0x47,0xc2,0x5a,0x91,0x44,0x7a,0x95,
2347  0xe7,0x5e,0x4e,0xf1,0x67,0x79,0xcd,0xe8,0xbf,0x0a,
2348  0x95,0x85,0x0e,0x32,0xaf,0x96,0x89,0x44,0x4f,0xd3,
2349  0x77,0x10,0x8f,0x98,0xfd,0xcb,0xd4,0xe7,0x26,0x56,
2350  0x75,0x00,0x99,0x0b,0xcc,0x7e,0x0c,0xa3,0xc4,0xaa,
2351  0xa3,0x04,0xa3,0x87,0xd2,0x0f,0x3b,0x8f,0xbb,0xcd,
2352  0x42,0xa1,0xbd,0x31,0x1d,0x7a,0x43,0x03,0xdd,0xa5,
2353  0xab,0x07,0x88,0x96,0xae,0x80,0xc1,0x8b,0x0a,0xf6,
2354  0x6d,0xff,0x31,0x96,0x16,0xeb,0x78,0x4e,0x49,0x5a,
2355  0xd2,0xce,0x90,0xd7,0xf7,0x72,0xa8,0x17,0x47,0xb6,
2356  0x5f,0x62,0x09,0x3b,0x1e,0x0d,0xb9,0xe5,0xba,0x53,
2357  0x2f,0xaf,0xec,0x47,0x50,0x83,0x23,0xe6,0x71,0x32,
2358  0x7d,0xf9,0x44,0x44,0x32,0xcb,0x73,0x67,0xce,0xc8,
2359  0x2f,0x5d,0x44,0xc0,0xd0,0x0b,0x67,0xd6,0x50,0xa0,
2360  0x75,0xcd,0x4b,0x70,0xde,0xdd,0x77,0xeb,0x9b,0x10,
2361  0x23,0x1b,0x6b,0x5b,0x74,0x13,0x47,0x39,0x6d,0x62,
2362  0x89,0x74,0x21,0xd4,0x3d,0xf9,0xb4,0x2e,0x44,0x6e,
2363  0x35,0x8e,0x9c,0x11,0xa9,0xb2,0x18,0x4e,0xcb,0xef,
2364  0x0c,0xd8,0xe7,0xa8,0x77,0xef,0x96,0x8f,0x13,0x90,
2365  0xec,0x9b,0x3d,0x35,0xa5,0x58,0x5c,0xb0,0x09,0x29,
2366  0x0e,0x2f,0xcd,0xe7,0xb5,0xec,0x66,0xd9,0x08,0x4b,
2367  0xe4,0x40,0x55,0xa6,0x19,0xd9,0xdd,0x7f,0xc3,0x16,
2368  0x6f,0x94,0x87,0xf7,0xcb,0x27,0x29,0x12,0x42,0x64,
2369  0x45,0x99,0x85,0x14,0xc1,0x5d,0x53,0xa1,0x8c,0x86,
2370  0x4c,0xe3,0xa2,0xb7,0x55,0x57,0x93,0x98,0x81,0x26,
2371  0x52,0x0e,0xac,0xf2,0xe3,0x06,0x6e,0x23,0x0c,0x91,
2372  0xbe,0xe4,0xdd,0x53,0x04,0xf5,0xfd,0x04,0x05,0xb3,
2373  0x5b,0xd9,0x9c,0x73,0x13,0x5d,0x3d,0x9b,0xc3,0x35,
2374  0xee,0x04,0x9e,0xf6,0x9b,0x38,0x67,0xbf,0x2d,0x7b,
2375  0xd1,0xea,0xa5,0x95,0xd8,0xbf,0xc0,0x06,0x6f,0xf8,
2376  0xd3,0x15,0x09,0xeb,0x0c,0x6c,0xaa,0x00,0x6c,0x80,
2377  0x7a,0x62,0x3e,0xf8,0x4c,0x3d,0x33,0xc1,0x95,0xd2,
2378  0x3e,0xe3,0x20,0xc4,0x0d,0xe0,0x55,0x81,0x57,0xc8,
2379  0x22,0xd4,0xb8,0xc5,0x69,0xd8,0x49,0xae,0xd5,0x9d,
2380  0x4e,0x0f,0xd7,0xf3,0x79,0x58,0x6b,0x4b,0x7f,0xf6,
2381  0x84,0xed,0x6a,0x18,0x9f,0x74,0x86,0xd4,0x9b,0x9c,
2382  0x4b,0xad,0x9b,0xa2,0x4b,0x96,0xab,0xf9,0x24,0x37,
2383  0x2c,0x8a,0x8f,0xff,0xb1,0x0d,0x55,0x35,0x49,0x00,
2384  0xa7,0x7a,0x3d,0xb5,0xf2,0x05,0xe1,0xb9,0x9f,0xcd,
2385  0x86,0x60,0x86,0x3a,0x15,0x9a,0xd4,0xab,0xe4,0x0f,
2386  0xa4,0x89,0x34,0x16,0x3d,0xdd,0xe5,0x42,0xa6,0x58,
2387  0x55,0x40,0xfd,0x68,0x3c,0xbf,0xd8,0xc0,0x0f,0x12,
2388  0x12,0x9a,0x28,0x4d,0xea,0xcc,0x4c,0xde,0xfe,0x58,
2389  0xbe,0x71,0x37,0x54,0x1c,0x04,0x71,0x26,0xc8,0xd4,
2390  0x9e,0x27,0x55,0xab,0x18,0x1a,0xb7,0xe9,0x40,0xb0,
2391  0xc0};
2392 
2393  // VC60 workaround: std::auto_ptr lacks reset()
2395  bool pass=true, fail;
2396  unsigned int i;
2397 
2398  std::cout << "\nARC4 validation suite running...\n\n";
2399 
2400  arc4.reset(new Weak::ARC4(Key0, sizeof(Key0)));
2401  arc4->ProcessString(Input0, sizeof(Input0));
2402  fail = memcmp(Input0, Output0, sizeof(Input0)) != 0;
2403  std::cout << (fail ? "FAILED" : "passed") << " Test 0" << std::endl;
2404  pass = pass && !fail;
2405 
2406  arc4.reset(new Weak::ARC4(Key1, sizeof(Key1)));
2407  arc4->ProcessString(Key1, Input1, sizeof(Key1));
2408  fail = memcmp(Output1, Key1, sizeof(Key1)) != 0;
2409  std::cout << (fail ? "FAILED" : "passed") << " Test 1" << std::endl;
2410  pass = pass && !fail;
2411 
2412  arc4.reset(new Weak::ARC4(Key2, sizeof(Key2)));
2413  for (i=0, fail=false; i<sizeof(Input2); i++)
2414  if (arc4->ProcessByte(Input2[i]) != Output2[i])
2415  fail = true;
2416  std::cout << (fail ? "FAILED" : "passed") << " Test 2" << std::endl;
2417  pass = pass && !fail;
2418 
2419  arc4.reset(new Weak::ARC4(Key3, sizeof(Key3)));
2420  for (i=0, fail=false; i<sizeof(Input3); i++)
2421  if (arc4->ProcessByte(Input3[i]) != Output3[i])
2422  fail = true;
2423  std::cout << (fail ? "FAILED" : "passed") << " Test 3" << std::endl;
2424  pass = pass && !fail;
2425 
2426  arc4.reset(new Weak::ARC4(Key4, sizeof(Key4)));
2427  for (i=0, fail=false; i<sizeof(Input4); i++)
2428  if (arc4->ProcessByte(Input4[i]) != Output4[i])
2429  fail = true;
2430  std::cout << (fail ? "FAILED" : "passed") << " Test 4" << std::endl;
2431  pass = pass && !fail;
2432 
2433  return pass;
2434 }
2435 
2436 bool ValidateRC5()
2437 {
2438  std::cout << "\nRC5 validation suite running...\n\n";
2439  bool pass1 = true, pass2 = true;
2440 
2441  RC5Encryption enc; // 0 to 2040-bits (255-bytes)
2442  pass1 = RC5Encryption::DEFAULT_KEYLENGTH == 16 && pass1;
2443  pass1 = enc.StaticGetValidKeyLength(0) == 0 && pass1;
2444  pass1 = enc.StaticGetValidKeyLength(254) == 254 && pass1;
2445  pass1 = enc.StaticGetValidKeyLength(255) == 255 && pass1;
2446  pass1 = enc.StaticGetValidKeyLength(256) == 255 && pass1;
2447  pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
2448  pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
2449 
2450  RC5Decryption dec;
2451  pass2 = RC5Decryption::DEFAULT_KEYLENGTH == 16 && pass2;
2452  pass2 = dec.StaticGetValidKeyLength(0) == 0 && pass2;
2453  pass2 = dec.StaticGetValidKeyLength(254) == 254 && pass2;
2454  pass2 = dec.StaticGetValidKeyLength(255) == 255 && pass2;
2455  pass2 = dec.StaticGetValidKeyLength(256) == 255 && pass2;
2456  pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
2457  pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
2458  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2459 
2460  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rc5val.dat", true, new HexDecoder);
2461  return BlockTransformationTest(VariableRoundsCipherFactory<RC5Encryption, RC5Decryption>(16, 12), valdata) && pass1 && pass2;
2462 }
2463 
2464 bool ValidateRC6()
2465 {
2466  std::cout << "\nRC6 validation suite running...\n\n";
2467  bool pass1 = true, pass2 = true, pass3 = true;
2468 
2469  RC6Encryption enc;
2470  pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
2471  pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
2472  pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
2473  pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
2474  pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
2475  pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
2476  pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
2477  pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
2478 
2479  RC6Decryption dec;
2480  pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
2481  pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
2482  pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
2483  pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
2484  pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
2485  pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
2486  pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
2487  pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
2488  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2489 
2490  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rc6val.dat", true, new HexDecoder);
2491  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(16), valdata, 2) && pass3;
2492  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(24), valdata, 2) && pass3;
2493  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RC6Encryption, RC6Decryption>(32), valdata, 2) && pass3;
2494  return pass1 && pass2 && pass3;
2495 }
2496 
2497 bool ValidateMARS()
2498 {
2499  std::cout << "\nMARS validation suite running...\n\n";
2500  bool pass1 = true, pass2 = true, pass3 = true;
2501 
2502  MARSEncryption enc;
2503  pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
2504  pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
2505  pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
2506  pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
2507  pass1 = enc.StaticGetValidKeyLength(64) == 56 && pass1;
2508  pass1 = enc.StaticGetValidKeyLength(128) == 56 && pass1;
2509  pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
2510  pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
2511 
2512  MARSDecryption dec;
2513  pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
2514  pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
2515  pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
2516  pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
2517  pass2 = dec.StaticGetValidKeyLength(64) == 56 && pass2;
2518  pass2 = dec.StaticGetValidKeyLength(128) == 56 && pass2;
2519  pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
2520  pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
2521  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2522 
2523  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/marsval.dat", true, new HexDecoder);
2524  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(16), valdata, 4) && pass3;
2525  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(24), valdata, 3) && pass3;
2526  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<MARSEncryption, MARSDecryption>(32), valdata, 2) && pass3;
2527  return pass1 && pass2 && pass3;
2528 }
2529 
2530 bool ValidateRijndael()
2531 {
2532  std::cout << "\nRijndael (AES) validation suite running...\n\n";
2533  bool pass1 = true, pass2 = true, pass3 = true;
2534 
2535  RijndaelEncryption enc;
2536  pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
2537  pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
2538  pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
2539  pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
2540  pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
2541  pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
2542  pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
2543  pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
2544 
2545  RijndaelDecryption dec;
2546  pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
2547  pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
2548  pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
2549  pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
2550  pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
2551  pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
2552  pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
2553  pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
2554  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2555 
2556  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/rijndael.dat", true, new HexDecoder);
2557  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(16), valdata, 4) && pass3;
2558  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(24), valdata, 3) && pass3;
2559  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<RijndaelEncryption, RijndaelDecryption>(32), valdata, 2) && pass3;
2560  pass3 = RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/aes.txt") && pass3;
2561  return pass1 && pass2 && pass3;
2562 }
2563 
2564 bool ValidateTwofish()
2565 {
2566  std::cout << "\nTwofish validation suite running...\n\n";
2567  bool pass1 = true, pass2 = true, pass3 = true;
2568 
2569  TwofishEncryption enc;
2570  pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
2571  pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
2572  pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
2573  pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
2574  pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
2575  pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
2576 
2577  TwofishDecryption dec;
2578  pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
2579  pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
2580  pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
2581  pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
2582  pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
2583  pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
2584  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2585 
2586  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/twofishv.dat", true, new HexDecoder);
2587  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(16), valdata, 4) && pass3;
2588  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(24), valdata, 3) && pass3;
2589  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<TwofishEncryption, TwofishDecryption>(32), valdata, 2) && pass3;
2590  return pass1 && pass2 && pass3;
2591 }
2592 
2593 bool ValidateSerpent()
2594 {
2595  std::cout << "\nSerpent validation suite running...\n\n";
2596  bool pass1 = true, pass2 = true, pass3 = true;
2597 
2598  SerpentEncryption enc;
2599  pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
2600  pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
2601  pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
2602  pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
2603  pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
2604  pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
2605 
2606  SerpentDecryption dec;
2607  pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
2608  pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
2609  pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
2610  pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
2611  pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
2612  pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
2613  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2614 
2615  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/serpentv.dat", true, new HexDecoder);
2616  bool pass = true;
2617  pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(16), valdata, 5) && pass;
2618  pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(24), valdata, 4) && pass;
2619  pass = BlockTransformationTest(FixedRoundsCipherFactory<SerpentEncryption, SerpentDecryption>(32), valdata, 3) && pass;
2620  return pass1 && pass2 && pass3;
2621 }
2622 
2623 bool ValidateBlowfish()
2624 {
2625  std::cout << "\nBlowfish validation suite running...\n\n";
2626  bool pass1 = true, pass2 = true, pass3 = true, fail;
2627 
2628  BlowfishEncryption enc1; // 32 to 448-bits (4 to 56-bytes)
2629  pass1 = enc1.StaticGetValidKeyLength(3) == 4 && pass1;
2630  pass1 = enc1.StaticGetValidKeyLength(4) == 4 && pass1;
2631  pass1 = enc1.StaticGetValidKeyLength(5) == 5 && pass1;
2632  pass1 = enc1.StaticGetValidKeyLength(8) == 8 && pass1;
2633  pass1 = enc1.StaticGetValidKeyLength(16) == 16 && pass1;
2634  pass1 = enc1.StaticGetValidKeyLength(24) == 24 && pass1;
2635  pass1 = enc1.StaticGetValidKeyLength(32) == 32 && pass1;
2636  pass1 = enc1.StaticGetValidKeyLength(56) == 56 && pass1;
2637  pass1 = enc1.StaticGetValidKeyLength(57) == 56 && pass1;
2638  pass1 = enc1.StaticGetValidKeyLength(60) == 56 && pass1;
2639  pass1 = enc1.StaticGetValidKeyLength(64) == 56 && pass1;
2640  pass1 = enc1.StaticGetValidKeyLength(128) == 56 && pass1;
2641 
2642  BlowfishDecryption dec1; // 32 to 448-bits (4 to 56-bytes)
2643  pass2 = dec1.StaticGetValidKeyLength(3) == 4 && pass2;
2644  pass2 = dec1.StaticGetValidKeyLength(4) == 4 && pass2;
2645  pass2 = dec1.StaticGetValidKeyLength(5) == 5 && pass2;
2646  pass2 = dec1.StaticGetValidKeyLength(8) == 8 && pass2;
2647  pass2 = dec1.StaticGetValidKeyLength(16) == 16 && pass2;
2648  pass2 = dec1.StaticGetValidKeyLength(24) == 24 && pass2;
2649  pass2 = dec1.StaticGetValidKeyLength(32) == 32 && pass2;
2650  pass2 = dec1.StaticGetValidKeyLength(56) == 56 && pass2;
2651  pass2 = dec1.StaticGetValidKeyLength(57) == 56 && pass2;
2652  pass2 = dec1.StaticGetValidKeyLength(60) == 56 && pass2;
2653  pass2 = dec1.StaticGetValidKeyLength(64) == 56 && pass2;
2654  pass2 = dec1.StaticGetValidKeyLength(128) == 56 && pass2;
2655  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2656 
2657  HexEncoder output(new FileSink(std::cout));
2658  const char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"};
2659  byte *plain[]={(byte *)"BLOWFISH", (byte *)"\xfe\xdc\xba\x98\x76\x54\x32\x10"};
2660  byte *cipher[]={(byte *)"\x32\x4e\xd0\xfe\xf4\x13\xa2\x03", (byte *)"\xcc\x91\x73\x2b\x80\x22\xf6\x84"};
2661  byte out[8], outplain[8];
2662 
2663  for (int i=0; i<2; i++)
2664  {
2665  ECB_Mode<Blowfish>::Encryption enc2((byte *)key[i], strlen(key[i]));
2666  enc2.ProcessData(out, plain[i], 8);
2667  fail = memcmp(out, cipher[i], 8) != 0;
2668 
2669  ECB_Mode<Blowfish>::Decryption dec2((byte *)key[i], strlen(key[i]));
2670  dec2.ProcessData(outplain, cipher[i], 8);
2671  fail = fail || memcmp(outplain, plain[i], 8);
2672  pass3 = pass3 && !fail;
2673 
2674  std::cout << (fail ? "FAILED " : "passed ");
2675  std::cout << '\"' << key[i] << '\"';
2676  for (int j=0; j<(signed int)(30-strlen(key[i])); j++)
2677  std::cout << ' ';
2678  output.Put(outplain, 8);
2679  std::cout << " ";
2680  output.Put(out, 8);
2681  std::cout << std::endl;
2682  }
2683  return pass1 && pass2 && pass3;
2684 }
2685 
2686 bool ValidateThreeWay()
2687 {
2688  std::cout << "\n3-WAY validation suite running...\n\n";
2689  bool pass1 = true, pass2 = true;
2690 
2691  ThreeWayEncryption enc; // 96-bit only
2692  pass1 = ThreeWayEncryption::KEYLENGTH == 12 && pass1;
2693  pass1 = enc.StaticGetValidKeyLength(8) == 12 && pass1;
2694  pass1 = enc.StaticGetValidKeyLength(12) == 12 && pass1;
2695  pass1 = enc.StaticGetValidKeyLength(16) == 12 && pass1;
2696 
2697  ThreeWayDecryption dec; // 96-bit only
2698  pass2 = ThreeWayDecryption::KEYLENGTH == 12 && pass2;
2699  pass2 = dec.StaticGetValidKeyLength(8) == 12 && pass2;
2700  pass2 = dec.StaticGetValidKeyLength(12) == 12 && pass2;
2701  pass2 = dec.StaticGetValidKeyLength(16) == 12 && pass2;
2702  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2703 
2704  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/3wayval.dat", true, new HexDecoder);
2705  return BlockTransformationTest(FixedRoundsCipherFactory<ThreeWayEncryption, ThreeWayDecryption>(), valdata) && pass1 && pass2;
2706 }
2707 
2708 bool ValidateGOST()
2709 {
2710  std::cout << "\nGOST validation suite running...\n\n";
2711  bool pass1 = true, pass2 = true;
2712 
2713  GOSTEncryption enc; // 256-bit only
2714  pass1 = GOSTEncryption::KEYLENGTH == 32 && pass1;
2715  pass1 = enc.StaticGetValidKeyLength(16) == 32 && pass1;
2716  pass1 = enc.StaticGetValidKeyLength(24) == 32 && pass1;
2717  pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
2718  pass1 = enc.StaticGetValidKeyLength(40) == 32 && pass1;
2719  pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
2720 
2721  GOSTDecryption dec; // 256-bit only
2722  pass2 = GOSTDecryption::KEYLENGTH == 32 && pass2;
2723  pass2 = dec.StaticGetValidKeyLength(16) == 32 && pass2;
2724  pass2 = dec.StaticGetValidKeyLength(24) == 32 && pass2;
2725  pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
2726  pass2 = dec.StaticGetValidKeyLength(40) == 32 && pass2;
2727  pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
2728  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2729 
2730  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/gostval.dat", true, new HexDecoder);
2731  return BlockTransformationTest(FixedRoundsCipherFactory<GOSTEncryption, GOSTDecryption>(), valdata) && pass1 && pass2;
2732 }
2733 
2734 bool ValidateSHARK()
2735 {
2736  std::cout << "\nSHARK validation suite running...\n\n";
2737  bool pass1 = true, pass2 = true;
2738 
2739  SHARKEncryption enc; // 128-bit only
2740  pass1 = SHARKEncryption::KEYLENGTH == 16 && pass1;
2741  pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
2742  pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1;
2743  pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
2744  pass1 = enc.StaticGetValidKeyLength(17) == 16 && pass1;
2745  pass1 = enc.StaticGetValidKeyLength(32) == 16 && pass1;
2746 
2747  SHARKDecryption dec; // 128-bit only
2748  pass2 = SHARKDecryption::KEYLENGTH == 16 && pass2;
2749  pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
2750  pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2;
2751  pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
2752  pass2 = dec.StaticGetValidKeyLength(17) == 16 && pass2;
2753  pass2 = dec.StaticGetValidKeyLength(32) == 16 && pass2;
2754  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2755 
2756  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/sharkval.dat", true, new HexDecoder);
2757  return BlockTransformationTest(FixedRoundsCipherFactory<SHARKEncryption, SHARKDecryption>(), valdata) && pass1 && pass2;
2758 }
2759 
2760 bool ValidateCAST()
2761 {
2762  std::cout << "\nCAST-128 validation suite running...\n\n";
2763  bool pass1 = true, pass2 = true, pass3 = true;
2764 
2765  CAST128Encryption enc1; // 40 to 128-bits (5 to 16-bytes)
2766  pass1 = CAST128Encryption::DEFAULT_KEYLENGTH == 16 && pass1;
2767  pass1 = enc1.StaticGetValidKeyLength(4) == 5 && pass1;
2768  pass1 = enc1.StaticGetValidKeyLength(5) == 5 && pass1;
2769  pass1 = enc1.StaticGetValidKeyLength(15) == 15 && pass1;
2770  pass1 = enc1.StaticGetValidKeyLength(16) == 16 && pass1;
2771  pass1 = enc1.StaticGetValidKeyLength(17) == 16 && pass1;
2772 
2773  CAST128Decryption dec1; // 40 to 128-bits (5 to 16-bytes)
2774  pass2 = CAST128Decryption::DEFAULT_KEYLENGTH == 16 && pass2;
2775  pass2 = dec1.StaticGetValidKeyLength(4) == 5 && pass2;
2776  pass2 = dec1.StaticGetValidKeyLength(5) == 5 && pass2;
2777  pass2 = dec1.StaticGetValidKeyLength(15) == 15 && pass2;
2778  pass2 = dec1.StaticGetValidKeyLength(16) == 16 && pass2;
2779  pass2 = dec1.StaticGetValidKeyLength(17) == 16 && pass2;
2780  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2781 
2782  FileSource val128(CRYPTOPP_DATA_DIR "TestData/cast128v.dat", true, new HexDecoder);
2783  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(16), val128, 1) && pass3;
2784  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(10), val128, 1) && pass3;
2785  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CAST128Encryption, CAST128Decryption>(5), val128, 1) && pass3;
2786 
2787  std::cout << "\nCAST-256 validation suite running...\n\n";
2788  bool pass4 = true, pass5 = true, pass6 = true;
2789 
2790  CAST256Encryption enc2; // 128, 160, 192, 224, or 256-bits (16 to 32-bytes, step 4)
2791  pass1 = CAST128Encryption::DEFAULT_KEYLENGTH == 16 && pass1;
2792  pass4 = enc2.StaticGetValidKeyLength(15) == 16 && pass4;
2793  pass4 = enc2.StaticGetValidKeyLength(16) == 16 && pass4;
2794  pass4 = enc2.StaticGetValidKeyLength(17) == 20 && pass4;
2795  pass4 = enc2.StaticGetValidKeyLength(20) == 20 && pass4;
2796  pass4 = enc2.StaticGetValidKeyLength(24) == 24 && pass4;
2797  pass4 = enc2.StaticGetValidKeyLength(28) == 28 && pass4;
2798  pass4 = enc2.StaticGetValidKeyLength(31) == 32 && pass4;
2799  pass4 = enc2.StaticGetValidKeyLength(32) == 32 && pass4;
2800  pass4 = enc2.StaticGetValidKeyLength(33) == 32 && pass4;
2801 
2802  CAST256Decryption dec2; // 128, 160, 192, 224, or 256-bits (16 to 32-bytes, step 4)
2803  pass2 = CAST256Decryption::DEFAULT_KEYLENGTH == 16 && pass2;
2804  pass5 = dec2.StaticGetValidKeyLength(15) == 16 && pass5;
2805  pass5 = dec2.StaticGetValidKeyLength(16) == 16 && pass5;
2806  pass5 = dec2.StaticGetValidKeyLength(17) == 20 && pass5;
2807  pass5 = dec2.StaticGetValidKeyLength(20) == 20 && pass5;
2808  pass5 = dec2.StaticGetValidKeyLength(24) == 24 && pass5;
2809  pass5 = dec2.StaticGetValidKeyLength(28) == 28 && pass5;
2810  pass5 = dec2.StaticGetValidKeyLength(31) == 32 && pass5;
2811  pass5 = dec2.StaticGetValidKeyLength(32) == 32 && pass5;
2812  pass5 = dec2.StaticGetValidKeyLength(33) == 32 && pass5;
2813  std::cout << (pass4 && pass5 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2814 
2815  FileSource val256(CRYPTOPP_DATA_DIR "TestData/cast256v.dat", true, new HexDecoder);
2816  pass6 = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(16), val256, 1) && pass6;
2817  pass6 = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(24), val256, 1) && pass6;
2818  pass6 = BlockTransformationTest(FixedRoundsCipherFactory<CAST256Encryption, CAST256Decryption>(32), val256, 1) && pass6;
2819 
2820  return pass1 && pass2 && pass3 && pass4 && pass5 && pass6;
2821 }
2822 
2823 bool ValidateSquare()
2824 {
2825  std::cout << "\nSquare validation suite running...\n\n";
2826  bool pass1 = true, pass2 = true;
2827 
2828  SquareEncryption enc; // 128-bits only
2829  pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
2830  pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1;
2831  pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
2832  pass1 = enc.StaticGetValidKeyLength(17) == 16 && pass1;
2833 
2834  SquareDecryption dec; // 128-bits only
2835  pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
2836  pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2;
2837  pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
2838  pass2 = dec.StaticGetValidKeyLength(17) == 16 && pass2;
2839  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2840 
2841  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/squareva.dat", true, new HexDecoder);
2842  return BlockTransformationTest(FixedRoundsCipherFactory<SquareEncryption, SquareDecryption>(), valdata) && pass1 && pass2;
2843 }
2844 
2845 bool ValidateSKIPJACK()
2846 {
2847  std::cout << "\nSKIPJACK validation suite running...\n\n";
2848  bool pass1 = true, pass2 = true;
2849 
2850  SKIPJACKEncryption enc; // 80-bits only
2851  pass1 = enc.StaticGetValidKeyLength(8) == 10 && pass1;
2852  pass1 = enc.StaticGetValidKeyLength(9) == 10 && pass1;
2853  pass1 = enc.StaticGetValidKeyLength(10) == 10 && pass1;
2854  pass1 = enc.StaticGetValidKeyLength(16) == 10 && pass1;
2855 
2856  SKIPJACKDecryption dec; // 80-bits only
2857  pass2 = dec.StaticGetValidKeyLength(8) == 10 && pass2;
2858  pass2 = dec.StaticGetValidKeyLength(9) == 10 && pass2;
2859  pass2 = dec.StaticGetValidKeyLength(10) == 10 && pass2;
2860  pass2 = dec.StaticGetValidKeyLength(16) == 10 && pass2;
2861  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2862 
2863  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/skipjack.dat", true, new HexDecoder);
2864  return BlockTransformationTest(FixedRoundsCipherFactory<SKIPJACKEncryption, SKIPJACKDecryption>(), valdata) && pass1 && pass2;
2865 }
2866 
2867 bool ValidateSEAL()
2868 {
2869  static const 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};
2870  static const byte key[] = {0x67, 0x45, 0x23, 0x01, 0xef, 0xcd, 0xab, 0x89, 0x98, 0xba, 0xdc, 0xfe, 0x10, 0x32, 0x54, 0x76, 0xc3, 0xd2, 0xe1, 0xf0};
2871  static const byte iv[] = {0x01, 0x35, 0x77, 0xaf};
2872  byte output[32];
2873 
2874  std::cout << "\nSEAL validation suite running...\n\n";
2875 
2876  SEAL<>::Encryption seal(key, sizeof(key), iv);
2877  unsigned int size = sizeof(input);
2878  bool pass = true;
2879 
2880  memset(output, 1, size);
2881  seal.ProcessString(output, input, size);
2882  for (unsigned int i=0; i<size; i++)
2883  if (output[i] != 0)
2884  pass = false;
2885 
2886  seal.Seek(1);
2887  output[1] = seal.ProcessByte(output[1]);
2888  seal.ProcessString(output+2, size-2);
2889  pass = pass && memcmp(output+1, input+1, size-1) == 0;
2890 
2891  std::cout << (pass ? "passed" : "FAILED") << std::endl;
2892  return pass;
2893 }
2894 
2895 bool ValidateBaseCode()
2896 {
2897  bool pass = true, fail;
2898  byte data[255];
2899  for (unsigned int i=0; i<255; i++)
2900  data[i] = byte(i);
2901  static const char hexEncoded[] =
2902  "000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F2021222324252627"
2903  "28292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F"
2904  "505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F7071727374757677"
2905  "78797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9F"
2906  "A0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7"
2907  "C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF"
2908  "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFE";
2909  static const char base32Encoded[] =
2910  "AAASEA2EAWDAQCAJBIFS2DIQB6IBCESVCSKTNF22DEPBYHA7D2RUAIJCENUCKJTHFAWUWK3NFWZC8NBT"
2911  "GI3VIPJYG66DUQT5HS8V6R4AIFBEGTCFI3DWSUKKJPGE4VURKBIXEW4WKXMFQYC3MJPX2ZK8M7SGC2VD"
2912  "NTUYN35IPFXGY5DPP3ZZA6MUQP4HK7VZRB6ZW856RX9H9AEBSKB2JBNGS8EIVCWMTUG27D6SUGJJHFEX"
2913  "U4M3TGN4VQQJ5HW9WCS4FI7EWYVKRKFJXKX43MPQX82MDNXVYU45PP72ZG7MZRF7Z496BSQC2RCNMTYH"
2914  "3DE6XU8N3ZHN9WGT4MJ7JXQY49NPVYY55VQ77Z9A6HTQH3HF65V8T4RK7RYQ55ZR8D29F69W8Z5RR8H3"
2915  "9M7939R8";
2916  const char *base64AndHexEncoded =
2917  "41414543417751464267634943516F4C4441304F4478415245684D554652595847426B6147787764"
2918  "486838674953496A4A43556D4A7967704B6973734C5334764D4445794D7A51310A4E6A63344F546F"
2919  "375044302B50304242516B4E4552555A4853456C4B5330784E546B395155564A5456465657563168"
2920  "5A576C746358563566594746695932526C5A6D646F615770720A6247317562334278636E4E306458"
2921  "5A3365486C3665337839666E2B4167594B44684957476834694A696F754D6A5936506B4A47536B35"
2922  "53566C7065596D5A71626E4A32656E3643680A6F714F6B7061616E714B6D717136797472712B7773"
2923  "624B7A744C573274376935757275387662362F774D484377385446787366497963724C7A4D334F7A"
2924  "39445230745055316462580A324E6E6132397A6433742F6734654C6A354F586D352B6A7036757673"
2925  "3765377638504879382F5431397666342B6672372F50332B0A";
2926 
2927  std::cout << "\nBase64, base32 and hex coding validation suite running...\n\n";
2928 
2929  fail = !TestFilter(HexEncoder().Ref(), data, 255, (const byte *)hexEncoded, strlen(hexEncoded));
2930  std::cout << (fail ? "FAILED " : "passed ");
2931  std::cout << "Hex Encoding\n";
2932  pass = pass && !fail;
2933 
2934  fail = !TestFilter(HexDecoder().Ref(), (const byte *)hexEncoded, strlen(hexEncoded), data, 255);
2935  std::cout << (fail ? "FAILED " : "passed ");
2936  std::cout << "Hex Decoding\n";
2937  pass = pass && !fail;
2938 
2939  fail = !TestFilter(Base32Encoder().Ref(), data, 255, (const byte *)base32Encoded, strlen(base32Encoded));
2940  std::cout << (fail ? "FAILED " : "passed ");
2941  std::cout << "Base32 Encoding\n";
2942  pass = pass && !fail;
2943 
2944  fail = !TestFilter(Base32Decoder().Ref(), (const byte *)base32Encoded, strlen(base32Encoded), data, 255);
2945  std::cout << (fail ? "FAILED " : "passed ");
2946  std::cout << "Base32 Decoding\n";
2947  pass = pass && !fail;
2948 
2949  fail = !TestFilter(Base64Encoder(new HexEncoder).Ref(), data, 255, (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded));
2950  std::cout << (fail ? "FAILED " : "passed ");
2951  std::cout << "Base64 Encoding\n";
2952  pass = pass && !fail;
2953 
2954  fail = !TestFilter(HexDecoder(new Base64Decoder).Ref(), (const byte *)base64AndHexEncoded, strlen(base64AndHexEncoded), data, 255);
2955  std::cout << (fail ? "FAILED " : "passed ");
2956  std::cout << "Base64 Decoding\n";
2957  pass = pass && !fail;
2958 
2959  return pass;
2960 }
2961 
2962 bool ValidateSHACAL2()
2963 {
2964  std::cout << "\nSHACAL-2 validation suite running...\n\n";
2965  bool pass1 = true, pass2 = true, pass3 = true;
2966 
2967  SHACAL2Encryption enc; // 128 to 512-bits (16 to 64-bytes)
2968  pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
2969  pass1 = enc.StaticGetValidKeyLength(15) == 16 && pass1;
2970  pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
2971  pass1 = enc.StaticGetValidKeyLength(64) == 64 && pass1;
2972  pass1 = enc.StaticGetValidKeyLength(65) == 64 && pass1;
2973  pass1 = enc.StaticGetValidKeyLength(128) == 64 && pass1;
2974  pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
2975  pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
2976 
2977  SHACAL2Decryption dec; // 128 to 512-bits (16 to 64-bytes)
2978  pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
2979  pass2 = dec.StaticGetValidKeyLength(15) == 16 && pass2;
2980  pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
2981  pass2 = dec.StaticGetValidKeyLength(64) == 64 && pass2;
2982  pass2 = dec.StaticGetValidKeyLength(65) == 64 && pass2;
2983  pass2 = dec.StaticGetValidKeyLength(128) == 64 && pass2;
2984  pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
2985  pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
2986  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
2987 
2988  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/shacal2v.dat", true, new HexDecoder);
2989  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(16), valdata, 4) && pass3;
2990  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<SHACAL2Encryption, SHACAL2Decryption>(64), valdata, 10) && pass3;
2991  return pass1 && pass2 && pass3;
2992 }
2993 
2994 bool ValidateCamellia()
2995 {
2996  std::cout << "\nCamellia validation suite running...\n\n";
2997  bool pass1 = true, pass2 = true, pass3 = true;
2998 
2999  CamelliaEncryption enc;
3000  pass1 = enc.StaticGetValidKeyLength(8) == 16 && pass1;
3001  pass1 = enc.StaticGetValidKeyLength(16) == 16 && pass1;
3002  pass1 = enc.StaticGetValidKeyLength(24) == 24 && pass1;
3003  pass1 = enc.StaticGetValidKeyLength(32) == 32 && pass1;
3004  pass1 = enc.StaticGetValidKeyLength(64) == 32 && pass1;
3005  pass1 = enc.StaticGetValidKeyLength(128) == 32 && pass1;
3006  pass1 = enc.StaticGetValidKeyLength(0) == enc.MinKeyLength() && pass1;
3007  pass1 = enc.StaticGetValidKeyLength(SIZE_MAX) == enc.MaxKeyLength() && pass1;
3008 
3009  CamelliaDecryption dec;
3010  pass2 = dec.StaticGetValidKeyLength(8) == 16 && pass2;
3011  pass2 = dec.StaticGetValidKeyLength(16) == 16 && pass2;
3012  pass2 = dec.StaticGetValidKeyLength(24) == 24 && pass2;
3013  pass2 = dec.StaticGetValidKeyLength(32) == 32 && pass2;
3014  pass2 = dec.StaticGetValidKeyLength(64) == 32 && pass2;
3015  pass2 = dec.StaticGetValidKeyLength(128) == 32 && pass2;
3016  pass2 = dec.StaticGetValidKeyLength(0) == dec.MinKeyLength() && pass2;
3017  pass2 = dec.StaticGetValidKeyLength(SIZE_MAX) == dec.MaxKeyLength() && pass2;
3018  std::cout << (pass1 && pass2 ? "passed:" : "FAILED:") << " Algorithm key lengths\n";
3019 
3020  FileSource valdata(CRYPTOPP_DATA_DIR "TestData/camellia.dat", true, new HexDecoder);
3021  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(16), valdata, 15) && pass3;
3022  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(24), valdata, 15) && pass3;
3023  pass3 = BlockTransformationTest(FixedRoundsCipherFactory<CamelliaEncryption, CamelliaDecryption>(32), valdata, 15) && pass3;
3024  return pass1 && pass2 && pass3;
3025 }
3026 
3027 bool ValidateSalsa()
3028 {
3029  std::cout << "\nSalsa validation suite running...\n";
3030 
3031  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/salsa.txt");
3032 }
3033 
3034 bool ValidateSosemanuk()
3035 {
3036  std::cout << "\nSosemanuk validation suite running...\n";
3037  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/sosemanuk.txt");
3038 }
3039 
3040 bool ValidateVMAC()
3041 {
3042  std::cout << "\nVMAC validation suite running...\n";
3043  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/vmac.txt");
3044 }
3045 
3046 bool ValidateCCM()
3047 {
3048  std::cout << "\nAES/CCM validation suite running...\n";
3049  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/ccm.txt");
3050 }
3051 
3052 bool ValidateGCM()
3053 {
3054  std::cout << "\nAES/GCM validation suite running...\n";
3055  std::cout << "\n2K tables:";
3056  bool pass = RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)2048));
3057  std::cout << "\n64K tables:";
3058  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/gcm.txt", MakeParameters(Name::TableSize(), (int)64*1024)) && pass;
3059 }
3060 
3061 bool ValidateCMAC()
3062 {
3063  std::cout << "\nCMAC validation suite running...\n";
3064  return RunTestDataFile(CRYPTOPP_DATA_DIR "TestVectors/cmac.txt");
3065 }
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:144
Base32 encodes data.
Definition: base32.h:17
bool HasISSE()
Determines SSE availability.
Definition: cpu.h:145
Stack-based SecBlock that grows into the heap.
Definition: secblock.h:776
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:185
Class file for modes of operation.
bool HasSSSE3()
Determines SSSE3 availability.
Definition: cpu.h:175
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: cryptlib.cpp:326
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:55
DMAC.
Definition: dmac.h:45
Hardware generated random numbers using RDRAND instruction.
Definition: rdrand.h:39
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:298
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:79
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:1814
virtual void DiscardBytes(size_t n)
Generate and discard n bytes.
Definition: cryptlib.cpp:339
Classes for automatic resource management.
bool IsP4()
Determines if the CPU is an Intel P4.
Definition: cpu.h:215
DEFLATE compressor (RFC 1951)
Definition: zdeflate.h:73
virtual bool CanIncorporateEntropy() const
Determines if a generator can accept additional entropy.
Definition: cryptlib.h:1216
Classes for the GIST block cipher.
Classes for the Cameliia block cipher.
Classes for RDRAND and RDSEED.
int GetCacheLineSize()
Provides the cache line size.
Definition: cpu.h:299
virtual void IncorporateEntropy(const byte *input, size_t length)
Update RNG state with additional unpredictable values.
Definition: rdrand.h:102
Classes for Base32Encoder and Base32Decoder.
SecBlock typedef.
Definition: secblock.h:731
Interface for buffered transformations.
Definition: cryptlib.h:1359
Classes for CBC MAC.
bool HasRDRAND()
Determines RDRAND availability.
Definition: cpu.h:225
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:476
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:1443
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:760
Filter wrapper for HashTransformation.
Definition: filters.h:517
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1385
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
Block cipher mode of operation aggregate.
Definition: modes.h:296
virtual void Resynchronize(const byte *iv, int ivLength=-1)
Resynchronize with an IV.
Definition: cryptlib.h:659
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:92
bool HasRDSEED()
Determines RDSEED availability.
Definition: cpu.h:235
bool HasCLMUL()
Determines Carryless Multiply availability.
Definition: cpu.h:205
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
Definition: filters.cpp:36
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:544
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:1106
Provides class member functions to key a block cipher.
Definition: seckey.h:339
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:809
#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
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Definition: trap.h:62
Classes for SEAL stream cipher.
Redirect input to another BufferedTransformation without owning it.
Definition: filters.h:764
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:42
Implementation of BufferedTransformation's attachment interface.
lword Pump(lword pumpMax=(size_t)...)
Pump data to attached transformation.
Definition: filters.h:1178
Hardware generated random numbers using RDSEED instruction.
Definition: rdrand.h:126
Classes for the Base64Encoder, Base64Decoder, Base64URLEncoder and Base64URLDecoder.
Filter wrapper for StreamTransformation.
Definition: filters.h:485
bool HasAESNI()
Determines AES-NI availability.
Definition: cpu.h:195
Classes for the SAFER and SAFER-K block ciphers.
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:160
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:472
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:496
bool HasMMX()
Determines MMX availability.
Definition: cpu.h:130
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:67
No padding added to a block.
Definition: filters.h:470
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:515
Classes for the Serpent block cipher.
Crypto++ library namespace.
SymmetricCipher implementation.
Definition: strciphr.h:584
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:1287
unsigned int GetRetries() const
Retrieve the number of retries used by the generator.
Definition: rdrand.h:60
size_type SizeInBytes() const
Provides the number of bytes in the SecBlock.
Definition: secblock.h:538
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:173
Classes for ARC4 cipher.
virtual unsigned int IVSize() const
Returns length of the IV accepted by this object.
Definition: cryptlib.h:637
virtual unsigned int MinLastBlockSize() const
Provides the size of the last block.
Definition: cryptlib.h:872
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
#define SIZE_MAX
The maximum value of a machine word.
Definition: misc.h:95
Classes for the SHACAL-2 block cipher.