summaryrefslogtreecommitdiff
path: root/validat1.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2017-08-01 18:57:23 -0400
committerJeffrey Walton <noloader@gmail.com>2017-08-01 18:57:23 -0400
commit5fbbc5311ceafeba77f6feac4cc41191676b89e9 (patch)
treee16d24ed69f3eac2dd9c46a616153851457cc12f /validat1.cpp
parent02e3a794443ae6d985ddf881d52a87bae1e7cd6f (diff)
downloadcryptopp-git-5fbbc5311ceafeba77f6feac4cc41191676b89e9.tar.gz
Add self tests for OldRandomPool
We still need to get the test result cross-validated
Diffstat (limited to 'validat1.cpp')
-rw-r--r--validat1.cpp109
1 files changed, 109 insertions, 0 deletions
diff --git a/validat1.cpp b/validat1.cpp
index c10efdf4..2cf5bfa0 100644
--- a/validat1.cpp
+++ b/validat1.cpp
@@ -719,6 +719,115 @@ bool TestRandomPool()
}
#endif
+ // Old, PGP 2.6 style RandomPool. Added because users were still having problems
+ // with it in 2017. The missing functionality was a barrier to upgrades.
+ std::cout << "\nTesting OldRandomPool generator...\n\n";
+ {
+ OldRandomPool prng;
+ static const unsigned int ENTROPY_SIZE = 32;
+
+ // https://github.com/weidai11/cryptopp/issues/452
+ byte result[32], expected[32] = {
+ 0x58,0x3E,0x0A,0xAC,0x79,0x71,0x19,0x18,
+ 0x51,0x97,0xC6,0x9B,0xEF,0x82,0x18,0x1E,
+ 0x9C,0x0F,0x5C,0xEF,0xC7,0x89,0xB2,0x94,
+ 0x04,0x96,0xD6,0xD9,0xA4,0x3B,0xB7,0xEC
+ };
+
+ SecByteBlock seed(0x00, 384);
+ prng.Put(seed, seed.size());
+
+ prng.GenerateBlock(result, sizeof(result));
+ fail = (0 != ::memcmp(result, expected, sizeof(expected)));
+
+ pass &= !fail;
+ if (fail)
+ std::cout << "FAILED:";
+ else
+ std::cout << "passed:";
+ std::cout << " Expected sequence from PGP-style RandomPool (2007 version)\n";
+
+ MeterFilter meter(new Redirector(TheBitBucket()));
+ RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter)));
+
+ fail = false;
+ if (meter.GetTotalBytes() < 100000)
+ fail = true;
+
+ pass &= !fail;
+ if (fail)
+ std::cout << "FAILED:";
+ else
+ std::cout << "passed:";
+ std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
+
+ try
+ {
+ fail = false;
+ prng.DiscardBytes(100000);
+ }
+ catch (const Exception&)
+ {
+ fail = true;
+ }
+
+ pass &= !fail;
+ if (fail)
+ std::cout << "FAILED:";
+ else
+ std::cout << "passed:";
+ std::cout << " discarded 10000 bytes" << std::endl;
+
+ try
+ {
+ fail = false;
+ if(prng.CanIncorporateEntropy())
+ {
+ SecByteBlock entropy(ENTROPY_SIZE);
+ GlobalRNG().GenerateBlock(entropy, entropy.SizeInBytes());
+
+ prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
+ prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
+ prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
+ prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
+ }
+ }
+ catch (const Exception& /*ex*/)
+ {
+ fail = true;
+ }
+
+ pass &= !fail;
+ if (fail)
+ std::cout << "FAILED:";
+ else
+ std::cout << "passed:";
+ std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes\n";
+
+ try
+ {
+ // Miscellaneous for code coverage
+ fail = false;
+ word32 result = prng.GenerateWord32();
+ result = prng.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff));
+ prng.GenerateBlock(reinterpret_cast<byte*>(&result), 4);
+ prng.GenerateBlock(reinterpret_cast<byte*>(&result), 3);
+ prng.GenerateBlock(reinterpret_cast<byte*>(&result), 2);
+ prng.GenerateBlock(reinterpret_cast<byte*>(&result), 1);
+ }
+ catch (const Exception&)
+ {
+ fail = true;
+ }
+
+ pass &= !fail;
+ if (fail)
+ std::cout << "FAILED:";
+ else
+ std::cout << "passed:";
+ std::cout << " GenerateWord32 and Crop\n";
+ }
+
return pass;
}