Skip to content

Commit

Permalink
CPU Jitter Random Number Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkz committed Sep 1, 2024
1 parent 3bee7a1 commit 331af13
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/lib/rng/jitter_rng/info.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
load_on dep

<module_info>
name -> "CPU Jitter Random Number Generator"
</module_info>

<defines>
JITTER_RNG -> 20240901
</defines>

<header:internal>
jitter.h
</header:internal>

<header:public>
jitter_rng.h
</header:public>

<libs>
all -> jitterentropy
</libs>
62 changes: 62 additions & 0 deletions src/lib/rng/jitter_rng/jitter.cpp
Original file line number Diff line number Diff line change
@@ -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 <span>

#include <botan/exceptn.h>
#include <botan/internal/jitter.h>

#include <memory>

extern "C" {
#include <jitterentropy.h>
}

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<uint8_t> 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<char*>(buf), len);
if(num_bytes < 0) {
throw Internal_Error("error reading jitter entropy");
}
}

} // namespace Botan
21 changes: 21 additions & 0 deletions src/lib/rng/jitter_rng/jitter.h
Original file line number Diff line number Diff line change
@@ -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 <span>

#include <botan/build.h>

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<uint8_t> buf);
void jitter_collect_into_buffer(rand_data* collector, uint8_t buf[], size_t len);

} // namespace Botan
36 changes: 36 additions & 0 deletions src/lib/rng/jitter_rng/jitter_rng.cpp
Original file line number Diff line number Diff line change
@@ -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 <botan/jitter_rng.h>
#include <botan/internal/jitter.h>

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<uint8_t> out, std::span<const uint8_t> in) {
jitter_collect_into_buffer(m_jitter, out);
}
}; // namespace Botan
41 changes: 41 additions & 0 deletions src/lib/rng/jitter_rng/jitter_rng.h
Original file line number Diff line number Diff line change
@@ -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 <botan/rng.h>

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<uint8_t> out, std::span<const uint8_t> in) override;

private:
rand_data* m_jitter;
};
} // namespace Botan

#endif

0 comments on commit 331af13

Please sign in to comment.