Skip to content
Permalink
Browse files

Add self tests for OldRandomPool

We still need to get the test result cross-validated
  • Loading branch information...
noloader committed Aug 1, 2017
1 parent 02e3a79 commit 5fbbc5311ceafeba77f6feac4cc41191676b89e9
Showing with 109 additions and 0 deletions.
  1. +109 −0 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;
}

1 comment on commit 5fbbc53

@noloader

This comment has been minimized.

Copy link
Collaborator Author

noloader commented on 5fbbc53 Aug 1, 2017

Cleared at Commit 02e3a794443ae6d9 (addition of OldRandomPool) and Commit 5fbbc5311ceafeba (self tests). Two commits were used in case the self tests need to be updated.

Also see Issue 452.

Please sign in to comment.
You can’t perform that action at this time.