From 0c1f7d30ea71d3612138bccdad135b8fb5ac3c72 Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 3 Jan 2016 12:53:46 -0500 Subject: [PATCH] Added additional tests --- validat1.cpp | 105 ++++++++++++++++++++++++++++++++++++++++++++++++--- validate.h | 1 + 2 files changed, 101 insertions(+), 5 deletions(-) diff --git a/validat1.cpp b/validat1.cpp index c5613557..892dcc81 100644 --- a/validat1.cpp +++ b/validat1.cpp @@ -66,6 +66,7 @@ bool ValidateAll(bool thorough) bool pass=TestSettings(); pass=TestOS_RNG() && pass; pass=TestAutoSeeded() && pass; + pass=TestAutoSeededX917() && pass; #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) pass=TestRDRAND() && pass; @@ -887,7 +888,11 @@ bool TestOS_RNG() #if NO_OS_DEPENDENCE bool TestAutoSeeded() -{ +{ + return true; +} +bool TestAutoSeededX917() +{ return true; } #else @@ -897,7 +902,8 @@ bool TestAutoSeeded() cout << "\nTesting AutoSeeded generator...\n\n"; AutoSeededRandomPool prng; - bool generate = true, discard = true; + static const unsigned int ENTROPY_SIZE = 32; + bool generate = true, discard = true, incorporate = false; MeterFilter meter(new Redirector(TheBitBucket())); RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter))); @@ -926,7 +932,96 @@ bool TestAutoSeeded() cout << "passed:"; cout << " discarded 10000 bytes" << endl; - return generate && discard; + try + { + if(prng.CanIncorporateEntropy()) + { + SecByteBlock entropy(ENTROPY_SIZE); + OS_GenerateRandomBlock(false, entropy, entropy.SizeInBytes()); + + prng.IncorporateEntropy(entropy, entropy.SizeInBytes()); + prng.IncorporateEntropy(entropy, entropy.SizeInBytes()); + prng.IncorporateEntropy(entropy, entropy.SizeInBytes()); + prng.IncorporateEntropy(entropy, entropy.SizeInBytes()); + + incorporate = true; + } + } + catch(const Exception& /*ex*/) + { + } + + if (!incorporate) + cout << "FAILED:"; + else + cout << "passed:"; + cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << endl; + + return generate && discard && incorporate; +} + +bool TestAutoSeededX917() +{ + // This tests Auto-Seeding and GenerateIntoBufferedTransformation. + cout << "\nTesting AutoSeeded X917 generator...\n\n"; + + AutoSeededX917RNG prng; + static const unsigned int ENTROPY_SIZE = 32; + bool generate = true, discard = true, incorporate = false; + + MeterFilter meter(new Redirector(TheBitBucket())); + RandomNumberSource test(prng, 100000, true, new Deflator(new Redirector(meter))); + + if (meter.GetTotalBytes() < 100000) + { + cout << "FAILED:"; + generate = false; + } + else + cout << "passed:"; + cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE" << endl; + + try + { + prng.DiscardBytes(100000); + } + catch(const Exception&) + { + discard = false; + } + + if (!discard) + cout << "FAILED:"; + else + cout << "passed:"; + cout << " discarded 10000 bytes" << endl; + + try + { + if(prng.CanIncorporateEntropy()) + { + SecByteBlock entropy(ENTROPY_SIZE); + OS_GenerateRandomBlock(false, entropy, entropy.SizeInBytes()); + + prng.IncorporateEntropy(entropy, entropy.SizeInBytes()); + prng.IncorporateEntropy(entropy, entropy.SizeInBytes()); + prng.IncorporateEntropy(entropy, entropy.SizeInBytes()); + prng.IncorporateEntropy(entropy, entropy.SizeInBytes()); + + incorporate = true; + } + } + catch(const Exception& /*ex*/) + { + } + + if (!incorporate) + cout << "FAILED:"; + else + cout << "passed:"; + cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes" << endl; + + return generate && discard && incorporate; } #endif // NO_OS_DEPENDENCE @@ -951,7 +1046,7 @@ bool TestRDRAND() chsw.AddDefaultRoute(maurer); RandomNumberSource rns(rdrand, SIZE, true, new Redirector(chsw)); - deflator.Flush(false); + deflator.Flush(true); assert(0 == maurer.BytesNeeded()); const double mv = maurer.GetTestValue(); @@ -1028,7 +1123,7 @@ bool TestRDSEED() chsw.AddDefaultRoute(maurer); RandomNumberSource rns(rdseed, SIZE, true, new Redirector(chsw)); - deflator.Flush(false); + deflator.Flush(true); assert(0 == maurer.BytesNeeded()); const double mv = maurer.GetTestValue(); diff --git a/validate.h b/validate.h index 485ef16c..898df436 100644 --- a/validate.h +++ b/validate.h @@ -9,6 +9,7 @@ bool ValidateAll(bool thorough); bool TestSettings(); bool TestOS_RNG(); bool TestAutoSeeded(); +bool TestAutoSeededX917(); #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) bool TestRDRAND();