Botan  2.9.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 #if defined(BOTAN_HAS_RDRAND_RNG)
14  #include <botan/rdrand_rng.h>
15 #endif
16 
17 extern "C" {
18 
19 using namespace Botan_FFI;
20 
21 int botan_rng_init(botan_rng_t* rng_out, const char* rng_type)
22  {
23  return ffi_guard_thunk(__func__, [=]() -> int {
24  if(rng_out == nullptr)
26 
27  const std::string rng_type_s(rng_type ? rng_type : "system");
28 
29  std::unique_ptr<Botan::RandomNumberGenerator> rng;
30 
31  if(rng_type_s == "system")
32  {
33  rng.reset(new Botan::System_RNG);
34  }
35  else if(rng_type_s == "user")
36  {
37  rng.reset(new Botan::AutoSeeded_RNG);
38  }
39  else if(rng_type_s == "null")
40  {
41  rng.reset(new Botan::Null_RNG);
42  }
43 #if defined(BOTAN_HAS_RDRAND_RNG)
44  else if(rng_type_s == "rdrand" && Botan::RDRAND_RNG::available())
45  {
46  rng.reset(new Botan::RDRAND_RNG);
47  }
48 #endif
49 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
50  else if(rng_type_s == "user-threadsafe")
51  {
52  rng.reset(new Botan::Serialized_RNG(new Botan::AutoSeeded_RNG));
53  }
54 #endif
55 
56  if(!rng)
57  {
59  }
60 
61  *rng_out = new botan_rng_struct(rng.release());
62  return BOTAN_FFI_SUCCESS;
63  });
64  }
65 
67  {
68  return BOTAN_FFI_CHECKED_DELETE(rng);
69  }
70 
71 int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len)
72  {
73  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
74  }
75 
76 int botan_rng_reseed(botan_rng_t rng, size_t bits)
77  {
79  }
80 
81 int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len)
82  {
83  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.add_entropy(input, len); });
84  }
85 
86 int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
87  {
88  return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(safe_get(source_rng), bits); });
89  }
90 
91 }
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:81
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:279
int botan_rng_add_entropy(botan_rng_t rng, const uint8_t *input, size_t len)
Definition: ffi_rng.cpp:81
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
Definition: ffi_rng.cpp:71
virtual void add_entropy(const uint8_t input[], size_t length)=0
static bool available()
Definition: rdrand_rng.cpp:25
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:86
int botan_rng_reseed(botan_rng_t rng, size_t bits)
Definition: ffi_rng.cpp:76
int botan_rng_destroy(botan_rng_t rng)
Definition: ffi_rng.cpp:66
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:116
int botan_rng_init(botan_rng_t *rng_out, const char *rng_type)
Definition: ffi_rng.cpp:21
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
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:88
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:188