diff --git a/validat3.cpp b/validat3.cpp index e4cbe10a..8463ae2c 100644 --- a/validat3.cpp +++ b/validat3.cpp @@ -403,6 +403,104 @@ bool TestSettings() return pass; } +bool Test_RandomNumberGenerator(RandomNumberGenerator& prng, bool drain=false) +{ + bool pass = true, result = true; + const size_t GENERATE_SIZE = 1024*10, ENTROPY_SIZE = 32; + + if(drain) + { + RandomNumberSource(prng, UINT_MAX, true, new Redirector(TheBitBucket())); + } + + MeterFilter meter(new Redirector(TheBitBucket())); + RandomNumberSource(prng, GENERATE_SIZE, true, new Deflator(new Redirector(meter))); + + if (meter.GetTotalBytes() < GENERATE_SIZE) + { + pass = false; + result = false; + } + + if (!pass) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " " << GENERATE_SIZE << " generated bytes compressed to "; + std::cout << meter.GetTotalBytes() << " bytes by DEFLATE\n"; + + try + { + pass = true; + 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*/) + { + pass = false; + result = false; + } + + if (!pass) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " IncorporateEntropy with " << 4*ENTROPY_SIZE << " bytes\n"; + + try + { + word32 result = prng.GenerateWord32(); + result = prng.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff)); + + prng.GenerateBlock(reinterpret_cast(&result), 4); + prng.GenerateBlock(reinterpret_cast(&result), 3); + prng.GenerateBlock(reinterpret_cast(&result), 2); + prng.GenerateBlock(reinterpret_cast(&result), 1); + } + catch (const Exception&) + { + pass = false; + result = false; + } + + if (!pass) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " GenerateWord32 and Crop\n"; + + + try + { + pass = true; + prng.DiscardBytes(GENERATE_SIZE); + } + catch (const Exception&) + { + pass = false; + result = false; + } + + if (!pass) + std::cout << "FAILED:"; + else + std::cout << "passed:"; + std::cout << " DiscardBytes with " << GENERATE_SIZE << " bytes\n"; + + // Miscellaneous for code coverage + (void)prng.AlgorithmName(); // "unknown" + + return result; +} + bool TestOS_RNG() { bool pass = true; @@ -412,7 +510,6 @@ bool TestOS_RNG() #ifdef BLOCKING_RNG_AVAILABLE try {rng.reset(new BlockingRng);} catch (const OS_RNG_Err &) {} -#endif if (rng.get()) { @@ -511,244 +608,59 @@ bool TestOS_RNG() } else std::cout << "\nNo operating system provided blocking random number generator, skipping test." << std::endl; +#endif - rng.reset(NULLPTR); #ifdef NONBLOCKING_RNG_AVAILABLE try {rng.reset(new NonblockingRng);} catch (OS_RNG_Err &) {} -#endif if (rng.get()) { std::cout << "\nTesting operating system provided nonblocking random number generator...\n\n"; - MeterFilter meter(new Redirector(TheBitBucket())); - RandomNumberSource test(*rng, 100000, true, new Deflator(new Redirector(meter))); - - if (meter.GetTotalBytes() < 100000) - { - std::cout << "FAILED:"; - pass = false; - } - else - std::cout << "passed:"; - std::cout << " 100000 generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n"; - - try - { - // Miscellaneous for code coverage - RandomNumberGenerator& prng = *rng.get(); - (void)prng.AlgorithmName(); - word32 result = prng.GenerateWord32(); - result = prng.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff)); - prng.GenerateBlock(reinterpret_cast(&result), 4); - prng.GenerateBlock(reinterpret_cast(&result), 3); - prng.GenerateBlock(reinterpret_cast(&result), 2); - prng.GenerateBlock(reinterpret_cast(&result), 1); - prng.GenerateBlock(reinterpret_cast(&result), 0); - pass = true; - } - catch (const Exception&) - { - pass = false; - } - - if (!pass) - std::cout << "FAILED:"; - else - std::cout << "passed:"; - std::cout << " GenerateWord32 and Crop\n"; + pass = Test_RandomNumberGenerator(*rng.get()) && pass; } else - std::cout << "\nNo operating system provided nonblocking random number generator, skipping test." << std::endl; + std::cout << "\nNo operating system provided non-blocking random number generator, skipping test." << std::endl; +#endif return pass; } bool TestRandomPool() { - std::cout << "\nTesting RandomPool generator...\n\n"; - bool pass=true, fail; + member_ptr prng; + bool pass=true; + + try {prng.reset(new RandomPool);} + catch (OS_RNG_Err &) {} + + if(prng.get()) { - RandomPool prng; - static const unsigned int ENTROPY_SIZE = 32; - - 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 - (void)prng.AlgorithmName(); // "unknown" - word32 result = prng.GenerateWord32(); - result = prng.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff)); - prng.GenerateBlock(reinterpret_cast(&result), 4); - prng.GenerateBlock(reinterpret_cast(&result), 3); - prng.GenerateBlock(reinterpret_cast(&result), 2); - prng.GenerateBlock(reinterpret_cast(&result), 1); - } - catch (const Exception&) - { - fail = true; - } - - pass &= !fail; - if (fail) - std::cout << "FAILED:"; - else - std::cout << "passed:"; - std::cout << " GenerateWord32 and Crop\n"; + std::cout << "\nTesting RandomPool generator...\n\n"; + pass = Test_RandomNumberGenerator(*prng.get()) && pass; } #if !defined(NO_OS_DEPENDENCE) && defined(OS_RNG_AVAILABLE) - std::cout << "\nTesting AutoSeeded RandomPool generator...\n\n"; + try {prng.reset(new AutoSeededRandomPool);} + catch (OS_RNG_Err &) {} + + if(prng.get()) { - AutoSeededRandomPool prng; - static const unsigned int ENTROPY_SIZE = 32; - - 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; - (void)prng.AlgorithmName(); // "unknown" - word32 result = prng.GenerateWord32(); - result = prng.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff)); - prng.GenerateBlock(reinterpret_cast(&result), 4); - prng.GenerateBlock(reinterpret_cast(&result), 3); - prng.GenerateBlock(reinterpret_cast(&result), 2); - prng.GenerateBlock(reinterpret_cast(&result), 1); - } - catch (const Exception&) - { - fail = true; - } - - pass &= !fail; - if (fail) - std::cout << "FAILED:"; - else - std::cout << "passed:"; - std::cout << " GenerateWord32 and Crop\n"; + std::cout << "\nTesting AutoSeeded RandomPool generator...\n\n"; + pass = Test_RandomNumberGenerator(*prng.get()) && pass; } #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"; + try {prng.reset(new OldRandomPool);} + catch (OS_RNG_Err &) {} + + if(prng.get()) { - OldRandomPool old; - static const unsigned int ENTROPY_SIZE = 32; + std::cout << "\nTesting OldRandomPool generator...\n\n"; + pass = Test_RandomNumberGenerator(*prng.get()) && pass; // https://github.com/weidai11/cryptopp/issues/452 byte actual[32], expected[32] = { @@ -758,101 +670,22 @@ bool TestRandomPool() 0xD1,0x13,0xE1,0xBA,0x07,0x49,0x8F,0x75 }; + prng.reset(new OldRandomPool); + RandomNumberGenerator& old = *prng.get(); + SecByteBlock seed(384); for (size_t i=0; i<384; ++i) seed[i] = static_cast(i); old.IncorporateEntropy(seed, seed.size()); old.GenerateBlock(actual, sizeof(actual)); - fail = (0 != std::memcmp(actual, expected, sizeof(expected))); + pass = (0 == std::memcmp(actual, expected, sizeof(expected))) && pass; - pass &= !fail; - if (fail) + if (!pass) std::cout << "FAILED:"; else std::cout << "passed:"; std::cout << " Expected sequence from PGP-style RandomPool (circa 2007)\n"; - - OldRandomPool prng; - 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(&result), 4); - prng.GenerateBlock(reinterpret_cast(&result), 3); - prng.GenerateBlock(reinterpret_cast(&result), 2); - prng.GenerateBlock(reinterpret_cast(&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; @@ -865,91 +698,7 @@ bool TestAutoSeededX917() std::cout << "\nTesting AutoSeeded X917 generator...\n\n"; AutoSeededX917RNG prng; - bool pass = true, fail; - static const unsigned int ENTROPY_SIZE = 32; - - 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; - (void)prng.AlgorithmName(); // "unknown" - word32 result = prng.GenerateWord32(); - result = prng.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff)); - prng.GenerateBlock(reinterpret_cast(&result), 4); - prng.GenerateBlock(reinterpret_cast(&result), 3); - prng.GenerateBlock(reinterpret_cast(&result), 2); - prng.GenerateBlock(reinterpret_cast(&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; + return Test_RandomNumberGenerator(prng); } #endif @@ -958,106 +707,18 @@ bool TestMersenne() { std::cout << "\nTesting Mersenne Twister...\n\n"; - static const unsigned int ENTROPY_SIZE = 32; - bool pass = true, fail = false; + MT19937ar prng; + bool pass = true; + + pass = Test_RandomNumberGenerator(prng); // First 10; http://create.stephan-brumme.com/mersenne-twister/ word32 result[10], expected[10] = {0xD091BB5C, 0x22AE9EF6, 0xE7E1FAEE, 0xD5C31F79, 0x2082352C, 0xF807B7DF, 0xE9D30005, 0x3895AFE1, 0xA1E24BBA, 0x4EE4092B}; - MT19937ar prng; prng.GenerateBlock(reinterpret_cast(result), sizeof(result)); - fail = (0 != std::memcmp(result, expected, sizeof(expected))); - - pass &= !fail; - if (fail) - std::cout << "FAILED:"; - else - std::cout << "passed:"; - std::cout << " Expected sequence from MT19937ar (2002 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\n"; - - 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 - (void)prng.AlgorithmName(); - word32 temp = prng.GenerateWord32(); - temp = prng.GenerateWord32((temp & 0xff), 0xffffffff - (temp & 0xff)); - prng.GenerateBlock(reinterpret_cast(&result[0]), 4); - prng.GenerateBlock(reinterpret_cast(&result[0]), 3); - prng.GenerateBlock(reinterpret_cast(&result[0]), 2); - prng.GenerateBlock(reinterpret_cast(&result[0]), 1); - prng.GenerateBlock(reinterpret_cast(&result[0]), 0); - fail = false; - } - catch (const Exception&) - { - fail = true; - } - - pass &= !fail; - if (fail) - std::cout << "FAILED:"; - else - std::cout << "passed:"; - std::cout << " GenerateWord32 and Crop\n"; + pass = (0 == std::memcmp(result, expected, sizeof(expected))) && pass; return pass; } @@ -1068,21 +729,19 @@ bool TestMersenne() { std::cout << "\nTesting Padlock RNG generator...\n\n"; - bool pass = true, fail = false; member_ptr rng; - - std::ostringstream oss; - oss << std::setiosflags(std::ios::fixed) << std::setprecision(6); + bool pass = true, fail; try {rng.reset(new PadlockRNG);} catch (const PadlockRNG_Err &) {} + if (rng.get()) { PadlockRNG& padlock = dynamic_cast(*rng.get()); + pass = Test_RandomNumberGenerator(padlock); + static const unsigned int SIZE = 10000; SecByteBlock zero(16), one(16), t(16); - std::memset(zero, 0x00, 16); - std::memset( one, 0xff, 16); // Cryptography Research, Inc tests word32 oldDivisor = padlock.SetDivisor(0); @@ -1094,37 +753,37 @@ bool TestMersenne() fail = !(msr & (1 << 6U)); pass &= !fail; if (fail) - oss << "FAILED:"; + std::cout << "FAILED:"; else - oss << "passed:"; - oss << " VIA RNG is activated\n"; + std::cout << "passed:"; + std::cout << " VIA RNG is activated\n"; // Bit 13 should be unset fail = !!(msr & (1 << 13U)); pass &= !fail; if (fail) - oss << "FAILED:"; + std::cout << "FAILED:"; else - oss << "passed:"; - oss << " von Neumann corrector is activated\n"; + std::cout << "passed:"; + std::cout << " von Neumann corrector is activated\n"; // Bit 14 should be unset fail = !!(msr & (1 << 14U)); pass &= !fail; if (fail) - oss << "FAILED:"; + std::cout << "FAILED:"; else - oss << "passed:"; - oss << " String filter is deactivated\n"; + std::cout << "passed:"; + std::cout << " String filter is deactivated\n"; // Bit 12:10 should be unset fail = !!(msr & (0x7 << 10U)); pass &= !fail; if (fail) - oss << "FAILED:"; + std::cout << "FAILED:"; else - oss << "passed:"; - oss << " Bias voltage is unmodified\n"; + std::cout << "passed:"; + std::cout << " Bias voltage is unmodified\n"; fail = false; if (t == zero || t == one) @@ -1132,94 +791,14 @@ bool TestMersenne() pass &= !fail; if (fail) - oss << "FAILED:"; + std::cout << "FAILED:"; else - oss << "passed:"; - oss << " All 0's or all 1's test\n"; - - MeterFilter meter(new Redirector(TheBitBucket())); - Deflator deflator(new Redirector(meter)); - MaurerRandomnessTest maurer; - - ChannelSwitch chsw; - chsw.AddDefaultRoute(deflator); - chsw.AddDefaultRoute(maurer); - - RandomNumberSource rns(padlock, SIZE, true, new Redirector(chsw)); - deflator.Flush(true); - - CRYPTOPP_ASSERT(0 == maurer.BytesNeeded()); - const double mv = maurer.GetTestValue(); - fail = false; - if (mv < 0.98f) - fail = true; - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " Maurer Randomness Test returned value " << mv << "\n"; - - fail = false; - if (meter.GetTotalBytes() < SIZE) - fail = true; - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n"; - - try - { - fail = false; - padlock.DiscardBytes(SIZE); - } - catch (const Exception&) - { - fail = true; - } - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " discarded " << SIZE << " bytes\n"; - - try - { - // Miscellaneous for code coverage - (void)padlock.AlgorithmName(); - (void)padlock.CanIncorporateEntropy(); - padlock.IncorporateEntropy(NULLPTR, 0); - - word32 result = padlock.GenerateWord32(); - result = padlock.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff)); - padlock.GenerateBlock(reinterpret_cast(&result), 4); - padlock.GenerateBlock(reinterpret_cast(&result), 3); - padlock.GenerateBlock(reinterpret_cast(&result), 2); - padlock.GenerateBlock(reinterpret_cast(&result), 1); - fail = false; - } - catch (const Exception&) - { - fail = true; - } - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " GenerateWord32 and Crop\n"; + std::cout << "passed:"; + std::cout << " All 0's or all 1's test\n"; } else - oss << "Padlock RNG generator not available, skipping test.\n"; + std::cout << "Padlock RNG generator not available, skipping test.\n"; - std::cout << oss.str(); return pass; } @@ -1227,101 +806,40 @@ bool TestRDRAND() { std::cout << "\nTesting RDRAND generator...\n\n"; - bool pass = true, fail = false; + bool pass = true; member_ptr rng; - std::ostringstream oss; - oss << std::setiosflags(std::ios::fixed) << std::setprecision(6); - try {rng.reset(new RDRAND);} catch (const RDRAND_Err &) {} + if (rng.get()) { RDRAND& rdrand = dynamic_cast(*rng.get()); - static const unsigned int SIZE = 10000; + pass = Test_RandomNumberGenerator(rdrand) && pass; - MeterFilter meter(new Redirector(TheBitBucket())); - Deflator deflator(new Redirector(meter)); MaurerRandomnessTest maurer; - - ChannelSwitch chsw; - chsw.AddDefaultRoute(deflator); - chsw.AddDefaultRoute(maurer); - - RandomNumberSource rns(rdrand, SIZE, true, new Redirector(chsw)); - deflator.Flush(true); + const unsigned int SIZE = 1024*10; + RandomNumberSource(rdrand, SIZE, true, new Redirector(maurer)); CRYPTOPP_ASSERT(0 == maurer.BytesNeeded()); const double mv = maurer.GetTestValue(); if (mv < 0.98f) - fail = true; + pass = false; - pass &= !fail; - if (fail) + std::ostringstream oss; + oss.flags(std::ios::fixed); + oss.precision(6); + + if (!pass) oss << "FAILED:"; else oss << "passed:"; oss << " Maurer Randomness Test returned value " << mv << "\n"; - - fail = false; - if (meter.GetTotalBytes() < SIZE) - fail = true; - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n"; - - try - { - fail = false; - rdrand.DiscardBytes(SIZE); - } - catch (const Exception&) - { - fail = true; - } - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " discarded " << SIZE << " bytes\n"; - - try - { - // Miscellaneous for code coverage - (void)rdrand.AlgorithmName(); - (void)rdrand.CanIncorporateEntropy(); - rdrand.IncorporateEntropy(NULLPTR, 0); - - word32 result = rdrand.GenerateWord32(); - result = rdrand.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff)); - rdrand.GenerateBlock(reinterpret_cast(&result), 4); - rdrand.GenerateBlock(reinterpret_cast(&result), 3); - rdrand.GenerateBlock(reinterpret_cast(&result), 2); - rdrand.GenerateBlock(reinterpret_cast(&result), 1); - fail = false; - } - catch (const Exception&) - { - fail = true; - } - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " GenerateWord32 and Crop\n"; + std::cout << oss.str(); } else - oss << "RDRAND generator not available, skipping test.\n"; + std::cout << "RDRAND generator not available, skipping test.\n"; - std::cout << oss.str(); return pass; } @@ -1329,101 +847,40 @@ bool TestRDSEED() { std::cout << "\nTesting RDSEED generator...\n\n"; - bool pass = true, fail = false; + bool pass = true; member_ptr rng; - std::ostringstream oss; - oss << std::setiosflags(std::ios::fixed) << std::setprecision(6); - try {rng.reset(new RDSEED);} catch (const RDSEED_Err &) {} + if (rng.get()) { RDSEED& rdseed = dynamic_cast(*rng.get()); - static const unsigned int SIZE = 10000; + pass = Test_RandomNumberGenerator(rdseed) && pass; - MeterFilter meter(new Redirector(TheBitBucket())); - Deflator deflator(new Redirector(meter)); MaurerRandomnessTest maurer; - - ChannelSwitch chsw; - chsw.AddDefaultRoute(deflator); - chsw.AddDefaultRoute(maurer); - - RandomNumberSource rns(rdseed, SIZE, true, new Redirector(chsw)); - deflator.Flush(true); + const unsigned int SIZE = 1024*10; + RandomNumberSource(rdseed, SIZE, true, new Redirector(maurer)); CRYPTOPP_ASSERT(0 == maurer.BytesNeeded()); const double mv = maurer.GetTestValue(); if (mv < 0.98f) - fail = true; + pass = false; - pass &= !fail; - if (fail) + std::ostringstream oss; + oss.flags(std::ios::fixed); + oss.precision(6); + + if (!pass) oss << "FAILED:"; else oss << "passed:"; oss << " Maurer Randomness Test returned value " << mv << "\n"; - - fail = false; - if (meter.GetTotalBytes() < SIZE) - fail = true; - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n"; - - try - { - fail = false; - rdseed.DiscardBytes(SIZE); - } - catch (const Exception&) - { - fail = true; - } - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " discarded " << SIZE << " bytes\n"; - - try - { - // Miscellaneous for code coverage - (void)rdseed.AlgorithmName(); - (void)rdseed.CanIncorporateEntropy(); - rdseed.IncorporateEntropy(NULLPTR, 0); - - word32 result = rdseed.GenerateWord32(); - result = rdseed.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff)); - rdseed.GenerateBlock(reinterpret_cast(&result), 4); - rdseed.GenerateBlock(reinterpret_cast(&result), 3); - rdseed.GenerateBlock(reinterpret_cast(&result), 2); - rdseed.GenerateBlock(reinterpret_cast(&result), 1); - fail = false; - } - catch (const Exception&) - { - fail = true; - } - - pass &= !fail; - if (fail) - oss << "FAILED:"; - else - oss << "passed:"; - oss << " GenerateWord32 and Crop\n"; + std::cout << oss.str(); } else - oss << "RDSEED generator not available, skipping test.\n"; + std::cout << "RDSEED generator not available, skipping test.\n"; - std::cout << oss.str(); return pass; } #endif