From 83fe87cc13b4dd6285fbc15465c7bd39fdadb53d Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Thu, 15 Oct 2015 09:28:25 -0400 Subject: Add System_RNG which is an instantiatable RNG that uses the system RNG Previously you couldn't have an unique_ptr that might point to either a system rng or an autoseed rng depending on availability. That was already needed in ffi and is useful elsewhere. --- src/lib/ffi/ffi.cpp | 17 +---------------- src/lib/rng/system_rng/system_rng.cpp | 16 ++++++++-------- src/lib/rng/system_rng/system_rng.h | 25 ++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 25 deletions(-) (limited to 'src/lib') diff --git a/src/lib/ffi/ffi.cpp b/src/lib/ffi/ffi.cpp index 27dcc6015..978a76c0f 100644 --- a/src/lib/ffi/ffi.cpp +++ b/src/lib/ffi/ffi.cpp @@ -228,21 +228,6 @@ int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags) int botan_rng_init(botan_rng_t* rng_out, const char* rng_type) { - // Just gives unique_ptr something to delete, really - class RNG_Wrapper : public Botan::RandomNumberGenerator - { - public: - RNG_Wrapper(Botan::RandomNumberGenerator& rng) : m_rng(rng) {} - void randomize(Botan::byte out[], size_t len) override { m_rng.randomize(out, len); } - bool is_seeded() const override { return m_rng.is_seeded(); } - void clear() override { m_rng.clear(); } - std::string name() const override { return m_rng.name(); } - void reseed(size_t poll_bits = 256) override { m_rng.reseed(poll_bits); } - void add_entropy(const Botan::byte in[], size_t len) override { m_rng.add_entropy(in, len); } - private: - Botan::RandomNumberGenerator& m_rng; - }; - try { BOTAN_ASSERT_ARG_NON_NULL(rng_out); @@ -255,7 +240,7 @@ int botan_rng_init(botan_rng_t* rng_out, const char* rng_type) std::unique_ptr rng; if(rng_type_s == "system") - rng.reset(new RNG_Wrapper(Botan::system_rng())); + rng.reset(new Botan::System_RNG); else if(rng_type_s == "user") rng.reset(new Botan::AutoSeeded_RNG); diff --git a/src/lib/rng/system_rng/system_rng.cpp b/src/lib/rng/system_rng/system_rng.cpp index 1ab80669b..8b949d071 100644 --- a/src/lib/rng/system_rng/system_rng.cpp +++ b/src/lib/rng/system_rng/system_rng.cpp @@ -1,6 +1,6 @@ /* * System RNG -* (C) 2014 Jack Lloyd +* (C) 2014,2015 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -28,11 +28,11 @@ namespace Botan { namespace { -class System_RNG : public RandomNumberGenerator +class System_RNG_Impl : public RandomNumberGenerator { public: - System_RNG(); - ~System_RNG(); + System_RNG_Impl(); + ~System_RNG_Impl(); void randomize(byte buf[], size_t len) override; @@ -51,7 +51,7 @@ class System_RNG : public RandomNumberGenerator #endif }; -System_RNG::System_RNG() +System_RNG_Impl::System_RNG_Impl() { #if defined(BOTAN_TARGET_OS_HAS_CRYPTGENRANDOM) @@ -66,7 +66,7 @@ System_RNG::System_RNG() #endif } -System_RNG::~System_RNG() +System_RNG_Impl::~System_RNG_Impl() { #if defined(BOTAN_TARGET_OS_HAS_CRYPTGENRANDOM) ::CryptReleaseContext(m_prov, 0); @@ -76,7 +76,7 @@ System_RNG::~System_RNG() #endif } -void System_RNG::randomize(byte buf[], size_t len) +void System_RNG_Impl::randomize(byte buf[], size_t len) { #if defined(BOTAN_TARGET_OS_HAS_CRYPTGENRANDOM) ::CryptGenRandom(m_prov, static_cast(len), buf); @@ -104,7 +104,7 @@ void System_RNG::randomize(byte buf[], size_t len) RandomNumberGenerator& system_rng() { - static System_RNG g_system_rng; + static System_RNG_Impl g_system_rng; return g_system_rng; } diff --git a/src/lib/rng/system_rng/system_rng.h b/src/lib/rng/system_rng/system_rng.h index cac861618..0f4b94725 100644 --- a/src/lib/rng/system_rng/system_rng.h +++ b/src/lib/rng/system_rng/system_rng.h @@ -1,6 +1,6 @@ /* * System RNG interface -* (C) 2014 Jack Lloyd +* (C) 2014,2015 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -19,6 +19,29 @@ namespace Botan { */ BOTAN_DLL RandomNumberGenerator& system_rng(); +/* +* Instantiatable reference to the system RNG. +*/ +class BOTAN_DLL System_RNG : public RandomNumberGenerator + { + public: + System_RNG() : m_rng(system_rng()) {} + + void randomize(Botan::byte out[], size_t len) override { m_rng.randomize(out, len); } + + bool is_seeded() const override { return m_rng.is_seeded(); } + + void clear() override { m_rng.clear(); } + + std::string name() const override { return m_rng.name(); } + + void reseed(size_t poll_bits = 256) override { m_rng.reseed(poll_bits); } + + void add_entropy(const byte in[], size_t len) override { m_rng.add_entropy(in, len); } + private: + Botan::RandomNumberGenerator& m_rng; + }; + } #endif -- cgit v1.2.3