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