Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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)
25 return BOTAN_FFI_ERROR_NULL_POINTER;
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 {
58 return BOTAN_FFI_ERROR_NOT_IMPLEMENTED;
59 }
60
61 *rng_out = new botan_rng_struct(rng.release());
62 return BOTAN_FFI_SUCCESS;
63 });
64 }
65
66 int botan_rng_destroy(botan_rng_t rng)
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 {
78 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(Botan::system_rng(), bits); });
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 }
Botan_FFI::ffi_guard_thunk
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
Botan::system_rng
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:279
botan_rng_add_entropy
int botan_rng_add_entropy(botan_rng_t rng, const uint8_t *input, size_t len)
Definition: ffi_rng.cpp:81
botan_rng_get
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
Definition: ffi_rng.cpp:71
Botan::RandomNumberGenerator::add_entropy
virtual void add_entropy(const uint8_t input[], size_t length)=0
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::RDRAND_RNG::available
static bool available()
Definition: rdrand_rng.cpp:85
Botan::RandomNumberGenerator::randomize
virtual void randomize(uint8_t output[], size_t length)=0
botan_rng_reseed_from_rng
int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
Definition: ffi_rng.cpp:86
botan_rng_reseed
int botan_rng_reseed(botan_rng_t rng, size_t bits)
Definition: ffi_rng.cpp:76
Botan::RDRAND_RNG
Definition: rdrand_rng.h:15
BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
Botan::AutoSeeded_RNG
Definition: auto_rng.h:20
botan_rng_destroy
int botan_rng_destroy(botan_rng_t rng)
Definition: ffi_rng.cpp:66
Botan::System_RNG
Definition: system_rng.h:25
BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
Botan_FFI
Definition: ffi.cpp:17
Botan::Null_RNG
Definition: rng.h:204
BOTAN_FFI_CHECKED_DELETE
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:129
botan_rng_init
int botan_rng_init(botan_rng_t *rng_out, const char *rng_type)
Definition: ffi_rng.cpp:21
Botan_FFI::safe_get
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:61
Botan::RandomNumberGenerator::reseed_from_rng
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
BOTAN_FFI_SUCCESS
Definition: ffi.h:62
BOTAN_FFI_DO
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92
botan_rng_t
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:189