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