summaryrefslogtreecommitdiff
path: root/validat1.cpp
diff options
context:
space:
mode:
authorJeffrey Walton <noloader@gmail.com>2017-08-19 15:41:45 -0400
committerJeffrey Walton <noloader@gmail.com>2017-08-19 15:41:45 -0400
commit7fb5953055d14307a9d4ae95fd6499f3a48f8b95 (patch)
tree8373b41085ae414dd5c06f410f8a7a430d366ba5 /validat1.cpp
parent65a96fe983d28e6e51612e3d2361716d7cdf9453 (diff)
downloadcryptopp-git-7fb5953055d14307a9d4ae95fd6499f3a48f8b95.tar.gz
Add VIA Padlock RNG
Diffstat (limited to 'validat1.cpp')
-rw-r--r--validat1.cpp104
1 files changed, 104 insertions, 0 deletions
diff --git a/validat1.cpp b/validat1.cpp
index 10e6932d..650e5f20 100644
--- a/validat1.cpp
+++ b/validat1.cpp
@@ -45,6 +45,7 @@
#include "osrng.h"
#include "drbg.h"
#include "rdrand.h"
+#include "padlkrng.h"
#include "mersenne.h"
#include "randpool.h"
#include "zdeflate.h"
@@ -84,6 +85,7 @@ bool ValidateAll(bool thorough)
pass=TestMersenne() && pass;
#endif
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
+ pass=TestPadlockRNG() && pass;
pass=TestRDRAND() && pass;
pass=TestRDSEED() && pass;
#endif
@@ -1043,6 +1045,108 @@ bool TestMersenne()
#endif
#if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64)
+ bool TestPadlockRNG()
+{
+ std::cout << "\nTesting Padlock RNG generator...\n\n";
+
+ bool pass = true, fail = false;
+ member_ptr<RandomNumberGenerator> rng;
+
+ try {rng.reset(new PadlockRNG);}
+ catch (const PadlockRNG_Err &) {}
+ if (rng.get())
+ {
+ PadlockRNG& padlock = dynamic_cast<PadlockRNG&>(*rng.get());
+ static const unsigned int SIZE = 10000;
+
+ 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();
+ if (mv < 0.98f)
+ 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:";
+ else
+ std::cout << "passed:";
+ std::cout << " " << 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)
+ std::cout << "FAILED:";
+ else
+ std::cout << "passed:";
+ std::cout << " 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<byte*>(&result), 4);
+ padlock.GenerateBlock(reinterpret_cast<byte*>(&result), 3);
+ padlock.GenerateBlock(reinterpret_cast<byte*>(&result), 2);
+ padlock.GenerateBlock(reinterpret_cast<byte*>(&result), 1);
+ fail = false;
+ }
+ catch (const Exception&)
+ {
+ fail = true;
+ }
+
+ pass &= !fail;
+ if (fail)
+ std::cout << "FAILED:";
+ else
+ std::cout << "passed:";
+ std::cout << " GenerateWord32 and Crop\n";
+ }
+ else
+ std::cout << "Padlock RNG generator not available, skipping test.\n";
+
+ return pass;
+}
+
bool TestRDRAND()
{
std::cout << "\nTesting RDRAND generator...\n\n";