Add additional tests

This commit is contained in:
Jeffrey Walton 2017-05-10 01:28:52 -04:00
parent 4904a951ce
commit 151ec518e8
No known key found for this signature in database
GPG Key ID: B36AB348921B1838
3 changed files with 285 additions and 178 deletions

View File

@ -233,14 +233,15 @@ bool TestCompressors()
bool TestEncryptors()
{
std::cout << "\nTesting Default Encryptors and Decryptors...\n\n";
static const unsigned int ENCRYPT_COUNT = 128, ENCRYPT_MAC_COUNT = 64;
bool fail1 = false, fail2 = false, fail3 = false, fail4 = false;
try
{
for (unsigned int i=0; i<128; ++i)
for (unsigned int i=0; i<ENCRYPT_COUNT; ++i)
{
std::string pwd, src, dest, rec;
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8;
unsigned int len = GlobalRNG().GenerateWord32(16, 0xffff);
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
@ -264,16 +265,16 @@ bool TestEncryptors()
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " 128 default encryptions and decryptions" << std::endl;
std::cout << " " << ENCRYPT_COUNT << " default encryptions and decryptions" << std::endl;
// **************************************************************
try
{
for (unsigned int i=0; i<128; ++i)
for (unsigned int i=0; i<ENCRYPT_MAC_COUNT; ++i)
{
std::string pwd, src, dest, rec;
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8;
unsigned int len = GlobalRNG().GenerateWord32(16, 0xffff);
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
@ -329,16 +330,16 @@ bool TestEncryptors()
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " 128 default encryptions and decryptions with MAC" << std::endl;
std::cout << " " << ENCRYPT_MAC_COUNT << " default encryptions and decryptions with MAC" << std::endl;
// **************************************************************
try
{
for (unsigned int i=0; i<128; ++i)
for (unsigned int i=0; i<ENCRYPT_COUNT; ++i)
{
std::string pwd, src, dest, rec;
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8;
unsigned int len = GlobalRNG().GenerateWord32(16, 0xffff);
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
@ -362,16 +363,16 @@ bool TestEncryptors()
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " 128 legacy encryptions and decryptions" << std::endl;
std::cout << " " << ENCRYPT_COUNT << " legacy encryptions and decryptions" << std::endl;
// **************************************************************
try
{
for (unsigned int i=0; i<128; ++i)
for (unsigned int i=0; i<ENCRYPT_MAC_COUNT; ++i)
{
std::string pwd, src, dest, rec;
unsigned int len = GlobalRNG().GenerateWord32(0, 0xffff) + 8;
unsigned int len = GlobalRNG().GenerateWord32(16, 0xffff);
src.resize(len);
GlobalRNG().GenerateBlock(reinterpret_cast<byte*>(&src[0]), src.size());
@ -427,7 +428,7 @@ bool TestEncryptors()
std::cout << "passed:";
else
std::cout << "FAILED:";
std::cout << " 128 legacy encryptions and decryptions with MAC" << std::endl;
std::cout << " " << ENCRYPT_MAC_COUNT << " legacy encryptions and decryptions with MAC" << std::endl;
return !fail1 && !fail2 && !fail3 && !fail4;
}
@ -508,7 +509,7 @@ bool TestSharing()
pass &= !fail;
}
std::cout << (fail ? "FAILED:" : "passed:") << " 64 information dispersals\n";
std::cout << (fail ? "FAILED:" : "passed:") << " " << INFORMATION_SHARES << " information dispersals\n";
// ********** Secret Sharing **********//
@ -577,7 +578,7 @@ bool TestSharing()
pass &= !fail;
}
std::cout << (fail ? "FAILED:" : "passed:") << " 64 secret sharings\n";
std::cout << (fail ? "FAILED:" : "passed:") << " " << SECRET_SHARES << " secret sharings\n";
return pass;
}

View File

@ -70,7 +70,7 @@ bool ValidateAll(bool thorough)
{
bool pass=TestSettings();
pass=TestOS_RNG() && pass;
pass=TestAutoSeeded() && pass;
pass=TestRandomPool() && pass;
pass=TestAutoSeededX917() && pass;
// pass=TestSecRandom() && pass;
#if defined(CRYPTOPP_EXTENDED_VALIDATION)
@ -459,7 +459,7 @@ bool TestOS_RNG()
RandomNumberGenerator& prng = *rng.get();
(void)prng.AlgorithmName();
word32 result = prng.GenerateWord32();
result = prng.GenerateWord32(21, 0xffffffff - 21);
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);
@ -509,7 +509,7 @@ bool TestOS_RNG()
RandomNumberGenerator& prng = *rng.get();
(void)prng.AlgorithmName();
word32 result = prng.GenerateWord32();
result = prng.GenerateWord32(21, 0xffffffff - 21);
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);
@ -534,47 +534,41 @@ bool TestOS_RNG()
return pass;
}
#if defined(NO_OS_DEPENDENCE) || !defined(OS_RNG_AVAILABLE)
bool TestAutoSeeded()
#if !defined(NO_OS_DEPENDENCE)
bool TestRandomPool()
{
return true;
}
bool TestAutoSeededX917()
std::cout << "\nTesting RandomPool generator...\n\n";
bool pass=true, fail;
{
return true;
}
#else
bool TestAutoSeeded()
{
// This tests Auto-Seeding and GenerateIntoBufferedTransformation.
std::cout << "\nTesting AutoSeeded generator...\n\n";
AutoSeededRandomPool prng;
RandomPool prng;
static const unsigned int ENTROPY_SIZE = 32;
bool generate = true, discard = true, incorporate = false, crop = false;
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:";
generate = false;
}
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&)
{
discard = false;
fail = true;
}
if (!discard)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -582,6 +576,7 @@ bool TestAutoSeeded()
try
{
fail = false;
if(prng.CanIncorporateEntropy())
{
SecByteBlock entropy(ENTROPY_SIZE);
@ -591,15 +586,15 @@ bool TestAutoSeeded()
prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
incorporate = true;
}
}
catch (const Exception& /*ex*/)
{
fail = true;
}
if (!incorporate)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -608,28 +603,116 @@ bool TestAutoSeeded()
try
{
// Miscellaneous for code coverage
(void)prng.AlgorithmName();
(void)prng.AlgorithmName(); // "unknown"
word32 result = prng.GenerateWord32();
result = prng.GenerateWord32(21, 0xffffffff - 21);
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);
prng.GenerateBlock(reinterpret_cast<byte*>(&result), 0);
crop = true;
}
catch (const Exception&)
{
crop = false;
fail = true;
}
if (!crop)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
std::cout << " GenerateWord32 and Crop\n";
}
return generate && discard && incorporate && crop;
std::cout << "\nTesting AutoSeeded RandomPool generator...\n\n";
{
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<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";
}
std::cout.flush();
return pass;
}
bool TestAutoSeededX917()
@ -638,31 +721,35 @@ bool TestAutoSeededX917()
std::cout << "\nTesting AutoSeeded X917 generator...\n\n";
AutoSeededX917RNG<AES> prng;
bool pass = true, fail;
static const unsigned int ENTROPY_SIZE = 32;
bool generate = true, discard = true, incorporate = false, crop = false;
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:";
generate = false;
}
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&)
{
discard = false;
fail = true;
}
if (!discard)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -670,6 +757,7 @@ bool TestAutoSeededX917()
try
{
fail = false;
if(prng.CanIncorporateEntropy())
{
SecByteBlock entropy(ENTROPY_SIZE);
@ -679,15 +767,15 @@ bool TestAutoSeededX917()
prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
incorporate = true;
}
}
catch (const Exception& /*ex*/)
{
fail = true;
}
if (!incorporate)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -696,28 +784,29 @@ bool TestAutoSeededX917()
try
{
// Miscellaneous for code coverage
(void)prng.AlgorithmName();
fail = false;
(void)prng.AlgorithmName(); // "unknown"
word32 result = prng.GenerateWord32();
result = prng.GenerateWord32(21, 0xffffffff - 21);
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);
prng.GenerateBlock(reinterpret_cast<byte*>(&result), 0);
crop = true;
}
catch (const Exception&)
{
crop = false;
fail = true;
}
if (!crop)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
std::cout << " GenerateWord32 and Crop\n";
return generate && discard && incorporate && crop;
std::cout.flush();
return pass;
}
#endif // NO_OS_DEPENDENCE
@ -727,7 +816,7 @@ bool TestMersenne()
std::cout << "\nTesting Mersenne Twister...\n\n";
static const unsigned int ENTROPY_SIZE = 32;
bool equal = true, generate = true, discard = true, incorporate = false, crop = false;
bool pass = true, fail = false;
// First 10; http://create.stephan-brumme.com/mersenne-twister/
word32 result[10], expected[10] = {0xD091BB5C, 0x22AE9EF6,
@ -736,41 +825,41 @@ bool TestMersenne()
MT19937ar prng;
prng.GenerateBlock(reinterpret_cast<byte*>(result), sizeof(result));
equal = (0 == ::memcmp(result, expected, sizeof(expected)));
fail = (0 != ::memcmp(result, expected, sizeof(expected)));
if (equal)
{
std::cout << "passed:";
}
else
{
pass &= !fail;
if (fail)
std::cout << "FAILED:";
equal = false;
}
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:";
generate = false;
}
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&)
{
discard = false;
fail = true;
}
if (!discard)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -778,6 +867,7 @@ bool TestMersenne()
try
{
fail = false;
if(prng.CanIncorporateEntropy())
{
SecByteBlock entropy(ENTROPY_SIZE);
@ -787,15 +877,15 @@ bool TestMersenne()
prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
prng.IncorporateEntropy(entropy, entropy.SizeInBytes());
incorporate = true;
}
}
catch (const Exception& /*ex*/)
{
fail = true;
}
if (!incorporate)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -805,27 +895,29 @@ bool TestMersenne()
{
// Miscellaneous for code coverage
(void)prng.AlgorithmName();
result[0] = prng.GenerateWord32();
result[0] = prng.GenerateWord32(21, 0xffffffff - 21);
word32 temp = prng.GenerateWord32();
temp = prng.GenerateWord32((temp & 0xff), 0xffffffff - (temp & 0xff));
prng.GenerateBlock(reinterpret_cast<byte*>(&result[0]), 4);
prng.GenerateBlock(reinterpret_cast<byte*>(&result[0]), 3);
prng.GenerateBlock(reinterpret_cast<byte*>(&result[0]), 2);
prng.GenerateBlock(reinterpret_cast<byte*>(&result[0]), 1);
prng.GenerateBlock(reinterpret_cast<byte*>(&result[0]), 0);
crop = true;
fail = false;
}
catch (const Exception&)
{
crop = false;
fail = true;
}
if (!crop)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
std::cout << " GenerateWord32 and Crop\n";
return equal && generate && discard && incorporate && crop;
std::cout.flush();
return pass;
}
#endif
@ -834,7 +926,7 @@ bool TestRDRAND()
{
std::cout << "\nTesting RDRAND generator...\n\n";
bool entropy = true, compress = true, discard = true, crop = true;
bool pass = true, fail = false;
member_ptr<RandomNumberGenerator> rng;
try {rng.reset(new RDRAND);}
@ -858,37 +950,42 @@ bool TestRDRAND()
CRYPTOPP_ASSERT(0 == maurer.BytesNeeded());
const double mv = maurer.GetTestValue();
if (mv < 0.98f)
{
std::cout << "FAILED:";
entropy = false;
}
else
std::cout << "passed:";
fail = true;
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
std::cout << " Maurer Randomness Test returned value " << mv << "\n";
fail = false;
if (meter.GetTotalBytes() < SIZE)
{
fail = true;
pass &= !fail;
if (fail)
std::cout << "FAILED:";
compress = false;
}
else
std::cout << "passed:";
std::cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
try
{
fail = false;
rdrand.DiscardBytes(SIZE);
}
catch (const Exception&)
{
discard = false;
fail = true;
}
if (!discard)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -902,19 +999,20 @@ bool TestRDRAND()
rdrand.IncorporateEntropy(NULLPTR, 0);
word32 result = rdrand.GenerateWord32();
result = rdrand.GenerateWord32(21, 0xffffffff - 21);
result = rdrand.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff));
rdrand.GenerateBlock(reinterpret_cast<byte*>(&result), 4);
rdrand.GenerateBlock(reinterpret_cast<byte*>(&result), 3);
rdrand.GenerateBlock(reinterpret_cast<byte*>(&result), 2);
rdrand.GenerateBlock(reinterpret_cast<byte*>(&result), 1);
crop = true;
fail = false;
}
catch (const Exception&)
{
crop = false;
fail = true;
}
if (!crop)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -923,7 +1021,8 @@ bool TestRDRAND()
else
std::cout << "RDRAND generator not available, skipping test.\n";
return entropy && compress && discard && crop;
std::cout.flush();
return pass;
}
#endif
@ -932,7 +1031,7 @@ bool TestRDSEED()
{
std::cout << "\nTesting RDSEED generator...\n\n";
bool entropy = true, compress = true, discard = true, crop = true;
bool pass = true, fail = false;
member_ptr<RandomNumberGenerator> rng;
try {rng.reset(new RDSEED);}
@ -956,37 +1055,42 @@ bool TestRDSEED()
CRYPTOPP_ASSERT(0 == maurer.BytesNeeded());
const double mv = maurer.GetTestValue();
if (mv < 0.98f)
{
std::cout << "FAILED:";
entropy = false;
}
else
std::cout << "passed:";
fail = true;
// Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
StreamState ss(std::cout);
std::cout << std::setiosflags(std::ios::fixed) << std::setprecision(6);
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
std::cout << " Maurer Randomness Test returned value " << mv << "\n";
fail = false;
if (meter.GetTotalBytes() < SIZE)
{
fail = true;
pass &= !fail;
if (fail)
std::cout << "FAILED:";
compress = false;
}
else
std::cout << "passed:";
std::cout << " " << SIZE << " generated bytes compressed to " << meter.GetTotalBytes() << " bytes by DEFLATE\n";
try
{
fail = false;
rdseed.DiscardBytes(SIZE);
}
catch (const Exception&)
{
discard = false;
fail = true;
}
if (!discard)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -1000,19 +1104,20 @@ bool TestRDSEED()
rdseed.IncorporateEntropy(NULLPTR, 0);
word32 result = rdseed.GenerateWord32();
result = rdseed.GenerateWord32(21, 0xffffffff - 21);
result = rdseed.GenerateWord32((result & 0xff), 0xffffffff - (result & 0xff));
rdseed.GenerateBlock(reinterpret_cast<byte*>(&result), 4);
rdseed.GenerateBlock(reinterpret_cast<byte*>(&result), 3);
rdseed.GenerateBlock(reinterpret_cast<byte*>(&result), 2);
rdseed.GenerateBlock(reinterpret_cast<byte*>(&result), 1);
crop = true;
fail = false;
}
catch (const Exception&)
{
crop = false;
fail = true;
}
if (!crop)
pass &= !fail;
if (fail)
std::cout << "FAILED:";
else
std::cout << "passed:";
@ -1021,7 +1126,8 @@ bool TestRDSEED()
else
std::cout << "RDSEED generator not available, skipping test.\n";
return entropy && compress && discard && crop;
std::cout.flush();
return pass;
}
#endif

View File

@ -18,7 +18,7 @@ bool ValidateAll(bool thorough);
bool TestSettings();
bool TestOS_RNG();
// bool TestSecRandom();
bool TestAutoSeeded();
bool TestRandomPool();
bool TestAutoSeededX917();
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)