-
Notifications
You must be signed in to change notification settings - Fork 568
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |