Botan 2.17.3
Crypto and TLS for C&
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_PROCESSOR_RNG)
14 #include <botan/processor_rng.h>
15#endif
16
17extern "C" {
18
19using namespace Botan_FFI;
20
21int 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" || rng_type_s == "user-threadsafe")
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_PROCESSOR_RNG)
44 else if((rng_type_s == "rdrand" || rng_type_s == "hwrng") && Botan::Processor_RNG::available())
45 {
46 rng.reset(new Botan::Processor_RNG);
47 }
48#endif
49
50 if(!rng)
51 {
53 }
54
55 *rng_out = new botan_rng_struct(rng.release());
56 return BOTAN_FFI_SUCCESS;
57 });
58 }
59
61 {
62 return BOTAN_FFI_CHECKED_DELETE(rng);
63 }
64
65int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len)
66 {
67 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
68 }
69
70int botan_rng_reseed(botan_rng_t rng, size_t bits)
71 {
73 }
74
75int botan_rng_add_entropy(botan_rng_t rng, const uint8_t* input, size_t len)
76 {
77 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.add_entropy(input, len); });
78 }
79
80int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
81 {
82 return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(safe_get(source_rng), bits); });
83 }
84
85}
static bool available()
virtual void reseed_from_rng(RandomNumberGenerator &rng, size_t poll_bits=BOTAN_RNG_RESEED_POLL_BITS)
Definition: rng.cpp:59
virtual void randomize(uint8_t output[], size_t length)=0
virtual void add_entropy(const uint8_t input[], size_t length)=0
struct botan_rng_struct * botan_rng_t
Definition: ffi.h:189
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition: ffi.h:82
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition: ffi.h:76
@ BOTAN_FFI_SUCCESS
Definition: ffi.h:62
int botan_rng_reseed_from_rng(botan_rng_t rng, botan_rng_t source_rng, size_t bits)
Definition: ffi_rng.cpp:80
int botan_rng_reseed(botan_rng_t rng, size_t bits)
Definition: ffi_rng.cpp:70
int botan_rng_add_entropy(botan_rng_t rng, const uint8_t *input, size_t len)
Definition: ffi_rng.cpp:75
int botan_rng_init(botan_rng_t *rng_out, const char *rng_type)
Definition: ffi_rng.cpp:21
int botan_rng_get(botan_rng_t rng, uint8_t *out, size_t out_len)
Definition: ffi_rng.cpp:65
int botan_rng_destroy(botan_rng_t rng)
Definition: ffi_rng.cpp:60
#define BOTAN_FFI_DO(T, obj, param, block)
Definition: ffi_util.h:92
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition: ffi_util.h:129
int ffi_guard_thunk(const char *func_name, std::function< int()> thunk)
Definition: ffi.cpp:89
T & safe_get(botan_struct< T, M > *p)
Definition: ffi_util.h:61
RandomNumberGenerator & system_rng()
Definition: system_rng.cpp:283