Skip to content

Commit

Permalink
Inline jitter code
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkz committed Sep 11, 2024
1 parent b1bdf42 commit ddf8b02
Showing 1 changed file with 27 additions and 55 deletions.
82 changes: 27 additions & 55 deletions src/lib/rng/jitter_rng/jitter_rng.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,9 @@

#include <jitterentropy.h>

namespace {

rand_data* jitter_collector_create();
void jitter_collector_free(rand_data* collector);
void jitter_collect_into_buffer(rand_data* collector, std::span<uint8_t> 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<uint8_t> 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<uint8_t> out, std::span<const uint8_t> 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) {
Expand All @@ -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<uint8_t> 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<char*>(buf), len);
ssize_t num_bytes = jent_read_entropy(m_jitter, reinterpret_cast<char*>(buf.data()), buf.size());
if(num_bytes < 0) {
switch(num_bytes) {
case -1: // should never happen because of the check above
Expand All @@ -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<uint8_t> 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
void Jitter_RNG::clear() {}

void Jitter_RNG::fill_bytes_with_input(std::span<uint8_t> out, std::span<const uint8_t> in) {
m_jitter.collect_into_buffer(out);
}
}; // namespace Botan

0 comments on commit ddf8b02

Please sign in to comment.