10#include <botan/auto_rng.h>
11#include <botan/system_rng.h>
12#include <botan/internal/ffi_rng.h>
13#include <botan/internal/ffi_util.h>
18#if defined(BOTAN_HAS_PROCESSOR_RNG)
19 #include <botan/processor_rng.h>
28 if(rng_out ==
nullptr) {
32 const std::string rng_type_s(rng_type ? rng_type :
"system");
34 std::unique_ptr<Botan::RandomNumberGenerator> rng;
36 if(rng_type_s ==
"system") {
37 rng = std::make_unique<Botan::System_RNG>();
38 }
else if(rng_type_s ==
"user" || rng_type_s ==
"user-threadsafe") {
39 rng = std::make_unique<Botan::AutoSeeded_RNG>();
40 }
else if(rng_type_s ==
"null") {
41 rng = std::make_unique<Botan::Null_RNG>();
43#if defined(BOTAN_HAS_PROCESSOR_RNG)
45 rng = std::make_unique<Botan::Processor_RNG>();
53 *rng_out =
new botan_rng_struct(std::move(rng));
61 int (*get_cb)(
void* context, uint8_t* out,
size_t out_len),
62 int (*add_entropy_cb)(
void* context,
const uint8_t input[],
size_t length),
63 void (*destroy_cb)(
void* context)) {
65 if(rng_out ==
nullptr) {
69 if(rng_name ==
nullptr) {
73 if(get_cb ==
nullptr) {
79 Custom_RNG(std::string_view
name,
81 int (*get_cb)(
void* context, uint8_t* out,
size_t out_len),
82 int (*add_entropy_cb)(
void* context,
const uint8_t input[],
size_t length),
83 void (*destroy_cb)(
void* context)) :
87 m_add_entropy_cb = add_entropy_cb;
88 m_destroy_cb = destroy_cb;
91 ~Custom_RNG()
override {
93 m_destroy_cb(m_context);
97 Custom_RNG(
const Custom_RNG& other) =
delete;
98 Custom_RNG(Custom_RNG&& other) =
delete;
99 Custom_RNG& operator=(
const Custom_RNG& other) =
delete;
100 Custom_RNG& operator=(Custom_RNG&& other) =
delete;
103 void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input)
override {
104 if(accepts_input() && !input.empty()) {
105 int rc = m_add_entropy_cb(m_context, input.data(), input.size());
111 if(!output.empty()) {
112 int rc = m_get_cb(m_context, output.data(), output.size());
120 bool accepts_input()
const override {
return m_add_entropy_cb !=
nullptr; }
122 std::string
name()
const override {
return m_name; }
124 void clear()
override {}
126 bool is_seeded()
const override {
return true; }
131 std::function<int(
void* context, uint8_t* out,
size_t out_len)> m_get_cb;
132 std::function<int(
void* context,
const uint8_t input[],
size_t length)> m_add_entropy_cb;
133 std::function<void(
void* context)> m_destroy_cb;
136 auto rng = std::make_unique<Custom_RNG>(rng_name, context, get_cb, add_entropy_cb, destroy_cb);
138 *rng_out =
new botan_rng_struct(std::move(rng));
148 return BOTAN_FFI_VISIT(rng, [=](
auto& r) { r.randomize(out, out_len); });
163 return BOTAN_FFI_VISIT(rng, [=](
auto& r) { r.add_entropy(input, len); });
void randomize(std::span< uint8_t > output)
struct botan_rng_struct * botan_rng_t
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
@ BOTAN_FFI_ERROR_NULL_POINTER
int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
int botan_rng_init_custom(botan_rng_t *rng_out, const char *rng_name, void *context, int(*get_cb)(void *context, uint8_t *out, size_t out_len), int(*add_entropy_cb)(void *context, const uint8_t input[], size_t length), void(*destroy_cb)(void *context))
int botan_rng_reseed(botan_rng_t rng, size_t bits)
int botan_rng_add_entropy(botan_rng_t rng, const uint8_t *input, size_t len)
int botan_rng_init(botan_rng_t *rng_out, const char *rng_type)
int botan_system_rng_get(uint8_t *out, size_t out_len)
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
int botan_rng_destroy(botan_rng_t rng)
#define BOTAN_FFI_VISIT(obj, lambda)
#define BOTAN_FFI_CHECKED_DELETE(o)
T & safe_get(botan_struct< T, M > *p)
int ffi_guard_thunk(const char *func_name, const std::function< int()> &thunk)
RandomNumberGenerator & system_rng()