From d4bcc4c70792950e13fb4320bcb3b443ebb506ea Mon Sep 17 00:00:00 2001 From: Jeffrey Walton Date: Sun, 4 Aug 2019 02:20:15 -0400 Subject: [PATCH] Make global seed deteminsitic from the command line --- test.cpp | 66 +++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 42 insertions(+), 24 deletions(-) diff --git a/test.cpp b/test.cpp index f5139727..ec0816e9 100644 --- a/test.cpp +++ b/test.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #ifdef CRYPTOPP_WIN32_AVAILABLE @@ -89,7 +90,8 @@ int (*AdhocTest)(int argc, char *argv[]) = NULLPTR; NAMESPACE_BEGIN(CryptoPP) NAMESPACE_BEGIN(Test) -const int MAX_PHRASE_LENGTH=250; +const int MAX_PHRASE_LENGTH = 250; +const int GLOBAL_SEED_LENGTH = 16; std::string g_argvPathHint=""; void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed); @@ -125,7 +127,8 @@ void HexDecode(const char *infile, const char *outfile); void FIPS140_GenerateRandomFiles(); -bool Validate(int, bool, const char *); +bool Validate(int, bool); +bool GetGlobalSeed(int argc, char* argv[], std::string& seed); void SetArgvPathHint(const char* argv0, std::string& pathHint); ANONYMOUS_NAMESPACE_BEGIN @@ -173,16 +176,17 @@ int scoped_main(int argc, char *argv[]) { RegisterFactories(All); - // Some editors have problems with the '\0' character when redirecting output. - s_globalSeed = IntToString(time(NULLPTR)); - s_globalSeed.resize(16, ' '); + // Set a seed for reproducible results. It can be set on the command line or + // in the environment. The command line takes precedent. For example: + // ./cryptest.exe v seed=abcdefg + GetGlobalSeed(argc, argv, s_globalSeed); #if (CRYPTOPP_USE_AES_GENERATOR) // Fetch the SymmetricCipher interface, not the RandomNumberGenerator // interface, to key the underlying cipher. If CRYPTOPP_USE_AES_GENERATOR is 1 // then AES/OFB based is used. Otherwise the OS random number generator is used. SymmetricCipher& cipher = dynamic_cast(GlobalRNG()); - cipher.SetKeyWithIV((byte *)s_globalSeed.data(), 16, (byte *)s_globalSeed.data()); + cipher.SetKeyWithIV((byte *)s_globalSeed.data(), s_globalSeed.size(), (byte *)s_globalSeed.data()); #endif std::string command, executableName, macFilename; @@ -393,7 +397,7 @@ int scoped_main(int argc, char *argv[]) else if (command == "ir") InformationRecoverFile(argc-3, argv[2], argv+3); else if (command == "v" || command == "vv") - return !Validate(argc>2 ? StringToValue(argv[2]) : 0, argv[1][1] == 'v', argc>3 ? argv[3] : NULLPTR); + return !Validate(argc>2 ? StringToValue(argv[2]) : 0, argv[1][1] == 'v'); else if (command.substr(0,1) == "b") // "b", "b1", "b2", ... BenchmarkWithCommand(argc, argv); else if (command == "z") @@ -446,6 +450,36 @@ int scoped_main(int argc, char *argv[]) } } // main() +bool GetGlobalSeed(int argc, char* argv[], std::string& seed) +{ + bool ret = false; + + for (int i=0; i