aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2013-12-25 20:30:16 +0000
committerlloyd <[email protected]>2013-12-25 20:30:16 +0000
commit94b7b07b34057474122a091b2b81b1cdcd9ee8db (patch)
treea4f11b0385fd8685d8ccb4563b72fe169a8923a4 /src
parenta4a59c29500bbae02273bfb75ddb8318a449e851 (diff)
Make Serialized_RNG public
Diffstat (limited to 'src')
-rw-r--r--src/libstate/global_rng.cpp63
-rw-r--r--src/libstate/libstate.cpp42
-rw-r--r--src/libstate/libstate.h11
-rw-r--r--src/rng/rng.h49
4 files changed, 60 insertions, 105 deletions
diff --git a/src/libstate/global_rng.cpp b/src/libstate/global_rng.cpp
index db857b41d..b6dc6b559 100644
--- a/src/libstate/global_rng.cpp
+++ b/src/libstate/global_rng.cpp
@@ -100,57 +100,6 @@ std::vector<std::unique_ptr<EntropySource>> Library_State::entropy_sources()
return sources;
}
-namespace {
-
-class Serialized_PRNG : public RandomNumberGenerator
- {
- public:
- void randomize(byte out[], size_t len)
- {
- std::lock_guard<std::mutex> lock(mutex);
- rng->randomize(out, len);
- }
-
- bool is_seeded() const
- {
- std::lock_guard<std::mutex> lock(mutex);
- return rng->is_seeded();
- }
-
- void clear()
- {
- std::lock_guard<std::mutex> lock(mutex);
- rng->clear();
- }
-
- std::string name() const
- {
- std::lock_guard<std::mutex> lock(mutex);
- return rng->name();
- }
-
- void reseed(size_t poll_bits)
- {
- std::lock_guard<std::mutex> lock(mutex);
- rng->reseed(poll_bits);
- }
-
- void add_entropy(const byte in[], size_t len)
- {
- std::lock_guard<std::mutex> lock(mutex);
- rng->add_entropy(in, len);
- }
-
- // We do not own the mutex; Library_State does
- Serialized_PRNG(RandomNumberGenerator* r, std::mutex& m) :
- mutex(m), rng(r) {}
- private:
- std::mutex& mutex;
- std::unique_ptr<RandomNumberGenerator> rng;
- };
-
-}
-
void Library_State::poll_available_sources(class Entropy_Accumulator& accum)
{
std::lock_guard<std::mutex> lock(m_entropy_src_mutex);
@@ -170,15 +119,5 @@ void Library_State::poll_available_sources(class Entropy_Accumulator& accum)
}
}
-RandomNumberGenerator* Library_State::make_global_rng(Algorithm_Factory& af,
- std::mutex& mutex)
- {
- auto rng = RandomNumberGenerator::make_rng(af);
-
- if(!rng)
- throw Internal_Error("No usable RNG found enabled in build");
-
- return new Serialized_PRNG(rng.release(), mutex);
- }
-
}
+
diff --git a/src/libstate/libstate.cpp b/src/libstate/libstate.cpp
index 6a0a28a45..358bba191 100644
--- a/src/libstate/libstate.cpp
+++ b/src/libstate/libstate.cpp
@@ -112,28 +112,19 @@ Algorithm_Factory& Library_State::algorithm_factory() const
*/
RandomNumberGenerator& Library_State::global_rng()
{
- std::lock_guard<std::mutex> lock(global_rng_lock);
-
- if(!global_rng_ptr)
- global_rng_ptr = make_global_rng(algorithm_factory(),
- global_rng_lock);
-
- return *global_rng_ptr;
+ return *m_global_prng;
}
-/*
-* Load a set of modules
-*/
void Library_State::initialize()
{
- CPUID::initialize();
-
- if(m_algorithm_factory)
+ if(m_algorithm_factory.get())
throw Invalid_State("Library_State has already been initialized");
+ CPUID::initialize();
+
load_default_config();
- m_algorithm_factory = new Algorithm_Factory();
+ m_algorithm_factory.reset(new Algorithm_Factory());
#if defined(BOTAN_HAS_ENGINE_GNU_MP)
algorithm_factory().add_engine(new GMP_Engine);
@@ -159,30 +150,11 @@ void Library_State::initialize()
m_sources = entropy_sources();
+ m_global_prng.reset(new Serialized_RNG());
+
#if defined(BOTAN_HAS_SELFTESTS)
confirm_startup_self_tests(algorithm_factory());
#endif
}
-/*
-* Library_State Constructor
-*/
-Library_State::Library_State()
- {
- m_algorithm_factory = nullptr;
- global_rng_ptr = nullptr;
- }
-
-/*
-* Library_State Destructor
-*/
-Library_State::~Library_State()
- {
- delete m_algorithm_factory;
- m_algorithm_factory = nullptr;
-
- delete global_rng_ptr;
- global_rng_ptr = nullptr;
- }
-
}
diff --git a/src/libstate/libstate.h b/src/libstate/libstate.h
index f3b085d38..af24000a5 100644
--- a/src/libstate/libstate.h
+++ b/src/libstate/libstate.h
@@ -25,8 +25,7 @@ namespace Botan {
class BOTAN_DLL Library_State
{
public:
- Library_State();
- ~Library_State();
+ Library_State() {}
Library_State(const Library_State&) = delete;
Library_State& operator=(const Library_State&) = delete;
@@ -93,15 +92,11 @@ class BOTAN_DLL Library_State
*/
std::string deref_alias(const std::string& alias);
private:
- static RandomNumberGenerator* make_global_rng(Algorithm_Factory& af,
- std::mutex& mutex);
-
static std::vector<std::unique_ptr<EntropySource>> entropy_sources();
void load_default_config();
- std::mutex global_rng_lock;
- RandomNumberGenerator* global_rng_ptr;
+ std::unique_ptr<Serialized_RNG> m_global_prng;
std::mutex m_entropy_src_mutex;
std::vector<std::unique_ptr<EntropySource>> m_sources;
@@ -109,7 +104,7 @@ class BOTAN_DLL Library_State
std::mutex config_lock;
std::map<std::string, std::string> config;
- Algorithm_Factory* m_algorithm_factory;
+ std::unique_ptr<Algorithm_Factory> m_algorithm_factory;
};
}
diff --git a/src/rng/rng.h b/src/rng/rng.h
index a8d8ebf28..8664ccbf7 100644
--- a/src/rng/rng.h
+++ b/src/rng/rng.h
@@ -12,6 +12,7 @@
#include <botan/exceptn.h>
#include <string>
#include <memory>
+#include <mutex>
namespace Botan {
@@ -120,6 +121,54 @@ class BOTAN_DLL Null_RNG : public RandomNumberGenerator
void add_entropy(const byte[], size_t) override {}
};
+/**
+* Wraps access to a RNG in a mutex
+*/
+class BOTAN_DLL Serialized_RNG : public RandomNumberGenerator
+ {
+ public:
+ void randomize(byte out[], size_t len)
+ {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ m_rng->randomize(out, len);
+ }
+
+ bool is_seeded() const
+ {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ return m_rng->is_seeded();
+ }
+
+ void clear()
+ {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ m_rng->clear();
+ }
+
+ std::string name() const
+ {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ return m_rng->name();
+ }
+
+ void reseed(size_t poll_bits)
+ {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ m_rng->reseed(poll_bits);
+ }
+
+ void add_entropy(const byte in[], size_t len)
+ {
+ std::lock_guard<std::mutex> lock(m_mutex);
+ m_rng->add_entropy(in, len);
+ }
+
+ Serialized_RNG() : m_rng(RandomNumberGenerator::make_rng()) {}
+ private:
+ mutable std::mutex m_mutex;
+ std::unique_ptr<RandomNumberGenerator> m_rng;
+ };
+
}
#endif