diff --git a/src/lib/rng/jitter_rng/jitter_rng.cpp b/src/lib/rng/jitter_rng/jitter_rng.cpp index 98ca9db18b..7d2518ff0c 100644 --- a/src/lib/rng/jitter_rng/jitter_rng.cpp +++ b/src/lib/rng/jitter_rng/jitter_rng.cpp @@ -9,48 +9,9 @@ #include -namespace { - -rand_data* jitter_collector_create(); -void jitter_collector_free(rand_data* collector); -void jitter_collect_into_buffer(rand_data* collector, std::span buf); - -} // namespace - namespace Botan { -Jitter_RNG_Data::Jitter_RNG_Data() : m_jitter{jitter_collector_create()} {} - -Jitter_RNG_Data::~Jitter_RNG_Data() { - jitter_collector_free(m_jitter); -} - -void Jitter_RNG_Data::collect_into_buffer(std::span buf) { - jitter_collect_into_buffer(m_jitter, buf); -} - -std::string Jitter_RNG::name() const { - return "JitterRNG"; -} - -bool Jitter_RNG::is_seeded() const { - return true; -} - -bool Jitter_RNG::accepts_input() const { - return false; -} - -void Jitter_RNG::clear() {} - -void Jitter_RNG::fill_bytes_with_input(std::span out, std::span in) { - m_jitter.collect_into_buffer(out); -} -}; // namespace Botan - -namespace { - -rand_data* jitter_collector_create() { +Jitter_RNG_Data::Jitter_RNG_Data() { static int result = jent_entropy_init(); if(result != 0) { @@ -61,30 +22,28 @@ rand_data* jitter_collector_create() { const unsigned int oversampling_rate = 0; // use default oversampling const unsigned int flags = 0; - rand_data* jitter = jent_entropy_collector_alloc(oversampling_rate, flags); - if(!jitter) { + m_jitter = jent_entropy_collector_alloc(oversampling_rate, flags); + if(!m_jitter) { throw Botan::Internal_Error("JitterRNG: Jitter entropy collector could not be allocated"); } - - return jitter; } -void jitter_collector_free(rand_data* collector) { - if(collector) { - jent_entropy_collector_free(collector); +Jitter_RNG_Data::~Jitter_RNG_Data() { + if(m_jitter) { + jent_entropy_collector_free(m_jitter); } } -void jitter_collect_into_buffer(rand_data* collector, uint8_t buf[], size_t len) { - if(len == 0) { +void Jitter_RNG_Data::collect_into_buffer(std::span buf) { + if(buf.size() == 0) { return; } - if(!collector) { + if(!m_jitter) { throw Botan::Invalid_State("JitterRNG: No jitter entropy collector has been allocated"); } - ssize_t num_bytes = jent_read_entropy(collector, reinterpret_cast(buf), len); + ssize_t num_bytes = jent_read_entropy(m_jitter, reinterpret_cast(buf.data()), buf.size()); if(num_bytes < 0) { switch(num_bytes) { case -1: // should never happen because of the check above @@ -107,13 +66,26 @@ void jitter_collect_into_buffer(rand_data* collector, uint8_t buf[], size_t len) throw Botan::Internal_Error("JitterRNG: Error reading entropy"); } } - if(num_bytes < len) { + if(num_bytes < buf.size()) { throw Botan::Internal_Error("JitterRNG: Not enough bytes have been produced"); } } -void jitter_collect_into_buffer(rand_data* collector, std::span buf) { - jitter_collect_into_buffer(collector, buf.data(), buf.size()); +std::string Jitter_RNG::name() const { + return "JitterRNG"; +} + +bool Jitter_RNG::is_seeded() const { + return true; +} + +bool Jitter_RNG::accepts_input() const { + return false; } -} // namespace \ No newline at end of file +void Jitter_RNG::clear() {} + +void Jitter_RNG::fill_bytes_with_input(std::span out, std::span in) { + m_jitter.collect_into_buffer(out); +} +}; // namespace Botan