Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 5fbbc53
Showing 1 changed file with 109 additions and 0 deletions.
109 changes: 109 additions & 0 deletions validat1.cpp
Expand Up @@ -719,6 +719,115 @@ bool TestRandomPool()
} }
#endif #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; return pass;
} }


Expand Down

1 comment on commit 5fbbc53

@noloader
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.