Botan  2.19.1
Crypto and TLS for C++11
ffi_rng.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2015,2017 Jack Lloyd
3 * (C) 2021 RenĂ© Fischer
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7 
8 #include <botan/ffi.h>
9 #include <botan/internal/ffi_util.h>
10 #include <botan/internal/ffi_rng.h>
11 #include <botan/system_rng.h>
12 #include <botan/auto_rng.h>
13 
14 #include <functional>
15 
16 #if defined(BOTAN_HAS_PROCESSOR_RNG)
17  #include <botan/processor_rng.h>
18 #endif
19 
20 extern "C" {
21 
22 using namespace Botan_FFI;
23 
24 int botan_rng_init(botan_rng_t* rng_out, const char* rng_type)
25  {
26  return ffi_guard_thunk(__func__, [=]() -> int {
27  if(rng_out == nullptr)
29 
30  const std::string rng_type_s(rng_type ? rng_type : "system");
31 
32  std::unique_ptr<Botan::RandomNumberGenerator> rng;
33 
34  if(rng_type_s == "system")
35  {
36  rng.reset(new Botan::System_RNG);
37  }
38  else if(rng_type_s == "user" || rng_type_s == "user-threadsafe")
39  {
40  rng.reset(new Botan::AutoSeeded_RNG);
41  }
42  else if(rng_type_s == "null")
43  {
44  rng.reset(new Botan::Null_RNG);
45  }
46 #if defined(BOTAN_HAS_PROCESSOR_RNG)
47  else if((rng_type_s == "rdrand" || rng_type_s == "hwrng") && Botan::Processor_RNG::available())
48  {
49  rng.reset(new Botan::Processor_RNG);
50  }
51 #endif
52 
53  if(!rng)
54  {
56  }
57 
58  *rng_out = new botan_rng_struct(rng.release());
59  return BOTAN_FFI_SUCCESS;
60  });
61  }
62 
63 int botan_rng_init_custom(botan_rng_t* rng_out, const char* rng_name, void* context,
64  int(* get_cb)(void* context, uint8_t* out, size_t out_len),
65  int(* add_entropy_cb)(void* context, const uint8_t input[], size_t length),
66  void(* destroy_cb)(void* context))
67 {
68 return ffi_guard_thunk(__func__,[=]() -> int {
69  if(rng_out == nullptr)
71 
72  if(rng_name == nullptr)
74 
75  if(get_cb == nullptr)
77 
78  class Custom_RNG : public Botan::RandomNumberGenerator
79  {
80  public:
81  Custom_RNG(const std::string& name, void* context,
82  int(* get_cb)(void* context, uint8_t* out, size_t out_len),
83  int(* add_entropy_cb)(void* context, const uint8_t input[], size_t length),
84  void(* destroy_cb)(void* context)) :
85  m_name(name)
86  {
87  m_context = context;
88  m_get_cb = get_cb;
89  m_add_entropy_cb = add_entropy_cb;
90  m_destroy_cb = destroy_cb;
91  }
92 
93  ~Custom_RNG()
94  {
95  if(m_destroy_cb)
96  {
97  m_destroy_cb(m_context);
98  }
99  }
100 
101  void randomize(uint8_t output[], size_t length) override
102  {
103  int rc = m_get_cb(m_context, output, length);
104  if(rc)
105  {
106  throw Botan::Invalid_State("Failed to get random from C callback, rc=" + std::to_string(rc));
107  }
108  }
109 
110  bool accepts_input() const override
111  {
112  return m_add_entropy_cb != nullptr;
113  }
114 
115  void add_entropy(const uint8_t input[], size_t length) override
116  {
117  if(m_add_entropy_cb == nullptr)
118  {
119  return;
120  }
121 
122  int rc = m_add_entropy_cb(m_context, input, length);
123  if(rc)
124  {
125  throw Botan::Invalid_State("Failed to add entropy via C callback, rc=" + std::to_string(rc));
126  }
127  }
128 
129  std::string name() const override
130  {
131  return m_name;
132  }
133 
134  void clear() override
135  {
136  }
137 
138  bool is_seeded() const override
139  {
140  return true;
141  }
142 
143  private:
144  std::string m_name;
145  void* m_context;
146  std::function<int(void* context, uint8_t* out, size_t out_len)> m_get_cb;
147  std::function<int(void* context, const uint8_t input[], size_t length)> m_add_entropy_cb;
148  std::function<void(void* context)> m_destroy_cb;
149  };
150 
151  std::unique_ptr<Botan::RandomNumberGenerator> rng(new Custom_RNG(rng_name, context, get_cb, add_entropy_cb, destroy_cb));
152 
153  *rng_out = new botan_rng_struct(rng.release());
154  return BOTAN_FFI_SUCCESS;
155  });
156 }
157 
159  {
160  return BOTAN_FFI_CHECKED_DELETE(rng);
161  }
162 
163 int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len)
164  {
165  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
166  }
167 
168 int botan_rng_reseed(botan_rng_t rng, size_t bits)
169  {
171  }
172 
173 int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len)
174  {
175  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.add_entropy(input, len); });
176  }
177 
178 int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
179  {
180  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(safe_get(source_rng), bits); });
181  }
182 
183 }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:283
int botan_rng_add_entropy(botan_rng_t rng, const uint8_t *input, size_t len)
Definition: ffi_rng.cpp:173
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
Definition: ffi_rng.cpp:163
virtual void add_entropy(const uint8_t input[], size_t length)=0
std::string m_name
virtual void randomize(uint8_t output[], size_t length)=0
int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
Definition: ffi_rng.cpp:178
int botan_rng_reseed(botan_rng_t rng, size_t bits)
Definition: ffi_rng.cpp:168
T m_context
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
std::string name
int botan_rng_destroy(botan_rng_t rng)
Definition: ffi_rng.cpp:158
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))
Definition: ffi_rng.cpp:63
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:129
int botan_rng_init(botan_rng_t *rng_out, const char *rng_type)
Definition: ffi_rng.cpp:24
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:61
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
static bool available()
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:190