diff --git a/src/lib/rng/jitter_rng/info.txt b/src/lib/rng/jitter_rng/info.txt new file mode 100644 index 0000000000..2fc50e7599 --- /dev/null +++ b/src/lib/rng/jitter_rng/info.txt @@ -0,0 +1,21 @@ +load_on dep + + +name -> "CPU Jitter Random Number Generator" + + + +JITTER_RNG -> 20240901 + + + +jitter.h + + + +jitter_rng.h + + + +all -> jitterentropy + diff --git a/src/lib/rng/jitter_rng/jitter.cpp b/src/lib/rng/jitter_rng/jitter.cpp new file mode 100644 index 0000000000..4cbf1b22a7 --- /dev/null +++ b/src/lib/rng/jitter_rng/jitter.cpp @@ -0,0 +1,62 @@ +/* +* Jitter utility functions +* (C) 2024 Planck Security S.A. +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include + +#include +#include + +#include + +extern "C" { +#include +} + +namespace Botan { + +rand_data* jitter_collector_create() { + static int result = jent_entropy_init(); + + if(result == 0) { + unsigned int oversampling_rate = 0; // use default oversampling + unsigned int flags = 0; + rand_data* jitter = jent_entropy_collector_alloc(oversampling_rate, flags); + if(!jitter) { + throw Internal_Error("jitter entropy could not be allocated"); + } + return jitter; + } else { + throw Internal_Error("jitter entropy can not be used"); + } +} + +void jitter_collector_free(rand_data* collector) { + if(collector) { + jent_entropy_collector_free(collector); + } +} + +void jitter_collect_into_buffer(rand_data* collector, std::span buf) { + jitter_collect_into_buffer(collector, buf.data(), buf.size()); +} + +void jitter_collect_into_buffer(rand_data* collector, uint8_t buf[], size_t len) { + if(!collector) { + throw Invalid_State("no jitter entropy collector has been allocated"); + } + + if(len <= 0) { + throw Invalid_Argument("number of jitter entropy bytes to return must be positive"); + } + + ssize_t num_bytes = jent_read_entropy(collector, reinterpret_cast(buf), len); + if(num_bytes < 0) { + throw Internal_Error("error reading jitter entropy"); + } +} + +} // namespace Botan diff --git a/src/lib/rng/jitter_rng/jitter.h b/src/lib/rng/jitter_rng/jitter.h new file mode 100644 index 0000000000..e0e75d4c2c --- /dev/null +++ b/src/lib/rng/jitter_rng/jitter.h @@ -0,0 +1,21 @@ +/* +* Jitter utility functions interface +* (C) 2024 Planck Security S.A. +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include + +#include + +struct rand_data; + +namespace Botan { + +rand_data* jitter_collector_create(); +void jitter_collector_free(rand_data* collector); +void jitter_collect_into_buffer(rand_data* collector, std::span buf); +void jitter_collect_into_buffer(rand_data* collector, uint8_t buf[], size_t len); + +} // namespace Botan diff --git a/src/lib/rng/jitter_rng/jitter_rng.cpp b/src/lib/rng/jitter_rng/jitter_rng.cpp new file mode 100644 index 0000000000..18bf9c3a97 --- /dev/null +++ b/src/lib/rng/jitter_rng/jitter_rng.cpp @@ -0,0 +1,36 @@ +/* +* CPU Jitter Random Number Generator +* (C) 2024 Planck Security S.A. +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include +#include + +namespace Botan { + +Jitter_RNG::Jitter_RNG() : m_jitter{jitter_collector_create()} {} + +Jitter_RNG::~Jitter_RNG() { + jitter_collector_free(m_jitter); +} + +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) { + jitter_collect_into_buffer(m_jitter, out); +} +}; // namespace Botan diff --git a/src/lib/rng/jitter_rng/jitter_rng.h b/src/lib/rng/jitter_rng/jitter_rng.h new file mode 100644 index 0000000000..518f7c28c0 --- /dev/null +++ b/src/lib/rng/jitter_rng/jitter_rng.h @@ -0,0 +1,41 @@ +/* +* CPU Jitter Random Number Generator +* (C) 2024 Planck Security S.A. +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_JITTER_RNG_H_ +#define BOTAN_JITTER_RNG_H_ + +#include + +struct rand_data; + +namespace Botan { + +/* +* RNG using libjitterentropy. +*/ +class BOTAN_PUBLIC_API(3, 7) Jitter_RNG final : public RandomNumberGenerator { + public: + Jitter_RNG(); + ~Jitter_RNG(); + + std::string name() const override; + + bool is_seeded() const override; + + bool accepts_input() const override; + + void clear() override; + + protected: + void fill_bytes_with_input(std::span out, std::span in) override; + + private: + rand_data* m_jitter; +}; +} // namespace Botan + +#endif