aboutsummaryrefslogtreecommitdiffstats
path: root/src/tests/main.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-09-24 11:10:58 -0400
committerJack Lloyd <[email protected]>2017-09-24 11:10:58 -0400
commit92104e5e770da875565ce1b671fef5613cefe2c5 (patch)
treee7e6dee0dc0c3415d561bb16745bab634d438e6f /src/tests/main.cpp
parentef9d827383aef0cdd3162c42b9b87b2663911705 (diff)
Refactor how test RNG is created
Diffstat (limited to 'src/tests/main.cpp')
-rw-r--r--src/tests/main.cpp163
1 files changed, 82 insertions, 81 deletions
diff --git a/src/tests/main.cpp b/src/tests/main.cpp
index 891f71304..0ddd28dc2 100644
--- a/src/tests/main.cpp
+++ b/src/tests/main.cpp
@@ -47,6 +47,87 @@ class Test_Runner final : public Botan_CLI::Command
: Command("test --threads=0 --run-long-tests --run-online-tests --test-runs=1 --drbg-seed= --data-dir="
" --pkcs11-lib= --provider= --log-success *suites") {}
+ std::unique_ptr<Botan::RandomNumberGenerator>
+ create_test_rng(const std::string& drbg_seed)
+ {
+ std::unique_ptr<Botan::RandomNumberGenerator> rng;
+
+#if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_64)
+
+ std::vector<uint8_t> seed = Botan::hex_decode(drbg_seed);
+ if(seed.empty())
+ {
+ const uint64_t ts = Botan_Tests::Test::timestamp();
+ seed.resize(8);
+ Botan::store_be(ts, seed.data());
+ }
+
+ output() << " rng:HMAC_DRBG with seed '" << Botan::hex_encode(seed) << "'\n";
+
+ // Expand out the seed to 512 bits to make the DRBG happy
+ std::unique_ptr<Botan::HashFunction> sha512(Botan::HashFunction::create("SHA-512"));
+ sha512->update(seed);
+ seed.resize(sha512->output_length());
+ sha512->final(seed.data());
+
+ std::unique_ptr<Botan::HMAC_DRBG> drbg(new Botan::HMAC_DRBG("SHA-384"));
+ drbg->initialize_with(seed.data(), seed.size());
+
+#if defined(BOTAN_TARGET_OS_HAS_THREADS)
+ rng.reset(new Botan::Serialized_RNG(drbg.release()));
+#else
+ rng = std::move(drbg);
+#endif
+
+#endif
+
+ if(!rng && drbg_seed != "")
+ throw Botan_Tests::Test_Error("HMAC_DRBG disabled in build, cannot specify DRBG seed");
+
+#if defined(BOTAN_HAS_SYSTEM_RNG)
+ if(!rng)
+ {
+ output() << " rng:system\n";
+ rng.reset(new Botan::System_RNG);
+ }
+#endif
+
+#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
+ if(!rng)
+ {
+ output() << " rng:autoseeded\n";
+ rng.reset(new Botan::Serialized_RNG(new Botan::AutoSeeded_RNG));
+ }
+#endif
+
+ if(!rng)
+ {
+ // last ditch fallback for RNG-less build
+ class Bogus_Fallback_RNG final : public Botan::RandomNumberGenerator
+ {
+ public:
+ std::string name() const override { return "Bogus_Fallback_RNG"; }
+
+ void clear() override { /* ignored */ }
+ void add_entropy(const uint8_t[], size_t) override { /* ignored */ }
+ bool is_seeded() const override { return true; }
+
+ void randomize(uint8_t out[], size_t len) override
+ {
+ for(size_t i = 0; i != len; ++i)
+ {
+ out[i] = std::rand();
+ }
+ }
+ };
+
+ output() << " rng:bogus\n";
+ rng.reset(new Bogus_Fallback_RNG);
+ }
+
+ return rng;
+ }
+
std::string help_text() const override
{
std::ostringstream err;
@@ -174,87 +255,7 @@ class Test_Runner final : public Botan_CLI::Command
}
#endif
- std::unique_ptr<Botan::RandomNumberGenerator> rng;
-
-#if defined(BOTAN_HAS_HMAC_DRBG) && defined(BOTAN_HAS_SHA2_64)
- std::vector<uint8_t> seed = Botan::hex_decode(drbg_seed);
- if(seed.empty())
- {
- const uint64_t ts = Botan_Tests::Test::timestamp();
- seed.resize(8);
- Botan::store_be(ts, seed.data());
- }
-
- output() << " rng:HMAC_DRBG with seed '" << Botan::hex_encode(seed) << "'";
-
- // Expand out the seed to 512 bits to make the DRBG happy
- std::unique_ptr<Botan::HashFunction> sha512(Botan::HashFunction::create("SHA-512"));
- sha512->update(seed);
- seed.resize(sha512->output_length());
- sha512->final(seed.data());
-
- std::unique_ptr<Botan::HMAC_DRBG> drbg(new Botan::HMAC_DRBG("SHA-384"));
- drbg->initialize_with(seed.data(), seed.size());
-
-#if defined(BOTAN_TARGET_OS_HAS_THREADS)
- rng.reset(new Botan::Serialized_RNG(drbg.release()));
-#else
- rng = std::move(drbg);
-#endif
-
-#else
-
- if(drbg_seed != "")
- {
- throw Botan_Tests::Test_Error("HMAC_DRBG disabled in build, cannot specify DRBG seed");
- }
-
-#if defined(BOTAN_HAS_SYSTEM_RNG)
- output() << " rng:system";
- rng.reset(new Botan::System_RNG);
-#elif defined(BOTAN_HAS_AUTO_SEEDING_RNG)
- output() << " rng:autoseeded";
- rng.reset(new Botan::Serialized_RNG(new Botan::AutoSeeded_RNG));
-#else
- // last ditch fallback for RNG-less build
- class Bogus_Fallback_RNG final : public Botan::RandomNumberGenerator
- {
- public:
- std::string name() const override
- {
- return "Bogus_Fallback_RNG";
- }
-
- void clear() override
- {
- /* ignored */
- }
-
- void randomize(uint8_t out[], size_t len) override
- {
- for(size_t i = 0; i != len; ++i)
- {
- out[i] = std::rand();
- }
- }
-
- bool is_seeded() const override
- {
- return true;
- }
-
- void add_entropy(const uint8_t[], size_t) override
- {
- /* ignored */
- }
- };
-
- rng.reset(new Bogus_Fallback_RNG);
-#endif
-
-#endif
-
- output() << "\n";
+ std::unique_ptr<Botan::RandomNumberGenerator> rng = create_test_rng(drbg_seed);
Botan_Tests::Test::setup_tests(log_success, run_online_tests, run_long_tests,
data_dir, pkcs11_lib, pf, rng.get());