| 1 |
/* |
| 2 |
* CPU Jitter Random Number Generator |
| 3 |
* (C) 2024 Planck Security S.A. |
| 4 |
* |
| 5 |
* Botan is released under the Simplified BSD License (see license.txt) |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <botan/jitter_rng.h> |
| 9 |
|
| 10 |
#include <botan/assert.h> |
| 11 |
#include <botan/exceptn.h> |
| 12 |
|
| 13 |
#include <jitterentropy.h> |
| 14 |
|
| 15 |
namespace Botan { |
| 16 |
|
| 17 |
struct Jitter_RNG_Internal { |
| 18 |
Jitter_RNG_Internal(); |
| 19 |
~Jitter_RNG_Internal(); |
| 20 |
void collect_into_buffer(std::span<uint8_t> buf); |
| 21 |
|
| 22 |
private: |
| 23 |
rand_data* m_rand_data; |
| 24 |
}; |
| 25 |
|
| 26 |
Jitter_RNG_Internal::Jitter_RNG_Internal() { |
| 27 |
constexpr unsigned int oversampling_rate = 0; // use default oversampling |
| 28 |
constexpr unsigned int flags = JENT_FORCE_FIPS; // enable health tests |
| 29 |
|
| 30 |
// if flags and osr are used, use the same values for init and alloc |
| 31 |
static int result = jent_entropy_init_ex(oversampling_rate, flags); |
| 32 |
|
| 33 |
// no further details documented regarding the return value |
| 34 |
if(result != 0) { |
| 35 |
throw Internal_Error("Jitter_RNG_Internal initialization failed"); |
| 36 |
} |
| 37 |
|
| 38 |
m_rand_data = jent_entropy_collector_alloc(oversampling_rate, flags); |
| 39 |
if(m_rand_data == nullptr) { |
| 40 |
throw Internal_Error("Jitter_RNG_Internal collector allocation failed"); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
Jitter_RNG_Internal::~Jitter_RNG_Internal() { |
| 45 |
if(m_rand_data) { |
| 46 |
jent_entropy_collector_free(m_rand_data); |
| 47 |
m_rand_data = nullptr; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
void Jitter_RNG_Internal::collect_into_buffer(std::span<uint8_t> buf) { |
| 52 |
if(buf.empty()) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
BOTAN_STATE_CHECK(m_rand_data != nullptr); |
| 57 |
|
| 58 |
ssize_t num_bytes = jent_read_entropy_safe(&m_rand_data, reinterpret_cast<char*>(buf.data()), buf.size()); |
| 59 |
if(num_bytes < 0) { |
| 60 |
const auto error_msg = [&]() -> std::string_view { |
| 61 |
switch(num_bytes) { |
| 62 |
case -1: // should never happen because of the check above |
| 63 |
return "JitterRNG: Uninitilialized"; |
| 64 |
case -2: |
| 65 |
return "JitterRNG: SP800-90B repetition count online health test failed"; |
| 66 |
case -3: |
| 67 |
return "JitterRNG: SP800-90B adaptive proportion online health test failed"; |
| 68 |
case -4: |
| 69 |
return "JitterRNG: Internal timer generator could not be initialized"; |
| 70 |
case -5: |
| 71 |
return "JitterRNG: LAG predictor health test failed"; |
| 72 |
case -6: |
| 73 |
return "JitterRNG: Repetitive count test (RCT) failed permanently"; |
| 74 |
case -7: |
| 75 |
return "JitterRNG: Adaptive proportion test (APT) failed permanently"; |
| 76 |
case -8: |
| 77 |
return "JitterRNG: LAG prediction test failed permanently"; |
| 78 |
default: |
| 79 |
return "JitterRNG: Error reading entropy"; |
| 80 |
} |
| 81 |
}(); |
| 82 |
throw Internal_Error(error_msg); |
| 83 |
} |
| 84 |
|
| 85 |
// According to the docs, `jent_read_entropy` itself runs its logic as often |
| 86 |
// as necessary to gather the requested number of bytes, |
| 87 |
// so this should actually never happen. |
| 88 |
BOTAN_ASSERT(static_cast<size_t>(num_bytes) == buf.size(), "JitterRNG produced the expected number of bytes"); |
| 89 |
} |
| 90 |
|
| 91 |
Jitter_RNG::Jitter_RNG() : m_jitter{std::make_unique<Jitter_RNG_Internal>()} {} |
| 92 |
|
| 93 |
Jitter_RNG::~Jitter_RNG() = default; |
| 94 |
|
| 95 |
void Jitter_RNG::clear() { |
| 96 |
m_jitter = std::make_unique<Jitter_RNG_Internal>(); |
| 97 |
} |
| 98 |
|
| 99 |
void Jitter_RNG::fill_bytes_with_input(std::span<uint8_t> out, std::span<const uint8_t> in) { |
| 100 |
BOTAN_UNUSED(in); |
| 101 |
|
| 102 |
m_jitter->collect_into_buffer(out); |
| 103 |
} |
| 104 |
|
| 105 |
} // namespace Botan |