Botan  2.7.0
Crypto and TLS for C++11
ffi_rng.cpp
Go to the documentation of this file.
1 /*
2 * (C) 2015,2017 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #include <botan/ffi.h>
8 #include <botan/internal/ffi_util.h>
9 #include <botan/internal/ffi_rng.h>
10 #include <botan/system_rng.h>
11 #include <botan/auto_rng.h>
12 
13 extern "C" {
14 
15 using namespace Botan_FFI;
16 
17 int botan_rng_init(botan_rng_t* rng_out, const char* rng_type)
18  {
19  return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() -> int {
20  if(rng_out == nullptr)
22 
23  const std::string rng_type_s(rng_type ? rng_type : "system");
24 
25  std::unique_ptr<Botan::RandomNumberGenerator> rng;
26 
27  if(rng_type_s == "system")
28  rng.reset(new Botan::System_RNG);
29  else if(rng_type_s == "user")
30  rng.reset(new Botan::AutoSeeded_RNG);
31  else
33 
34  *rng_out = new botan_rng_struct(rng.release());
35  return BOTAN_FFI_SUCCESS;
36  });
37  }
38 
40  {
41  return BOTAN_FFI_CHECKED_DELETE(rng);
42  }
43 
44 int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len)
45  {
46  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
47  }
48 
49 int botan_rng_reseed(botan_rng_t rng, size_t bits)
50  {
52  }
53 
54 }
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:225
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
Definition: ffi_rng.cpp:44
virtual void randomize(uint8_t output[], size_t length)=0
int botan_rng_reseed(botan_rng_t rng, size_t bits)
Definition: ffi_rng.cpp:49
int ffi_guard_thunk(const char *func_name, Thunk thunk)
Definition: ffi_util.h:64
int botan_rng_destroy(botan_rng_t rng)
Definition: ffi_rng.cpp:39
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:126
int botan_rng_init(botan_rng_t *rng_out, const char *rng_type)
Definition: ffi_rng.cpp:17
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:45
#define BOTAN_CURRENT_FUNCTION
Definition: compiler.h:143
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:98
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:182