aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/ffi/ffi_rng.cpp
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-07-25 15:24:30 -0400
committerJack Lloyd <[email protected]>2017-07-31 10:31:53 -0400
commit5703b195f5dbf0a0df45bf0a7f39aa090666f877 (patch)
tree1f686ae1362aaef9eabe1db3c1a58ac0fd166a70 /src/lib/ffi/ffi_rng.cpp
parentbb30a1e1ffbe839478b4bf04624d841c6d9ecfc3 (diff)
Split up ffi.cpp into several files
It was getting pretty big and would get worse over time, eg whenver I get around to adding TLS support.
Diffstat (limited to 'src/lib/ffi/ffi_rng.cpp')
-rw-r--r--src/lib/ffi/ffi_rng.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/lib/ffi/ffi_rng.cpp b/src/lib/ffi/ffi_rng.cpp
new file mode 100644
index 000000000..68b4aaf64
--- /dev/null
+++ b/src/lib/ffi/ffi_rng.cpp
@@ -0,0 +1,53 @@
+/*
+* (C) 2015,2017 Jack Lloyd
+*
+* Botan is released under the Simplified BSD License (see license.txt)
+*/
+
+#include <botan/ffi.h>
+#include <botan/internal/ffi_util.h>
+#include <botan/internal/ffi_rng.h>
+#include <botan/system_rng.h>
+#include <botan/auto_rng.h>
+
+extern "C" {
+
+using namespace Botan_FFI;
+
+int botan_rng_init(botan_rng_t* rng_out, const char* rng_type)
+ {
+ return ffi_guard_thunk(BOTAN_CURRENT_FUNCTION, [=]() {
+ BOTAN_ASSERT_ARG_NON_NULL(rng_out);
+
+ const std::string rng_type_s(rng_type ? rng_type : "system");
+
+ std::unique_ptr<Botan::RandomNumberGenerator> rng;
+
+ if(rng_type_s == "system")
+ rng.reset(new Botan::System_RNG);
+ else if(rng_type_s == "user")
+ rng.reset(new Botan::AutoSeeded_RNG);
+ else
+ return BOTAN_FFI_ERROR_BAD_PARAMETER;
+
+ *rng_out = new botan_rng_struct(rng.release());
+ return BOTAN_FFI_SUCCESS;
+ });
+ }
+
+int botan_rng_destroy(botan_rng_t rng)
+ {
+ return BOTAN_FFI_CHECKED_DELETE(rng);
+ }
+
+int botan_rng_get(botan_rng_t rng, uint8_t* out, size_t out_len)
+ {
+ return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.randomize(out, out_len); });
+ }
+
+int botan_rng_reseed(botan_rng_t rng, size_t bits)
+ {
+ return BOTAN_FFI_DO(Botan::RandomNumberGenerator, rng, r, { r.reseed_from_rng(Botan::system_rng(), bits); });
+ }
+
+}