diff options
author | Jack Lloyd <[email protected]> | 2018-09-20 14:03:55 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-09-20 14:03:55 -0400 |
commit | 9a41b24157260eb9830e6a441c1131d6be09b3df (patch) | |
tree | a6a828c24801b1c6d966fc69cfb83cc5162379dc /src/lib/rng | |
parent | aabb8e40b3b5907977a817107d1d580af364a126 (diff) |
Add support for using Linux getrandom syscall
Disabled by default as it requires a relatively recent kernel and glibc.
Diffstat (limited to 'src/lib/rng')
-rw-r--r-- | src/lib/rng/system_rng/system_rng.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/lib/rng/system_rng/system_rng.cpp b/src/lib/rng/system_rng/system_rng.cpp index 5a55e0a83..aa7858d4a 100644 --- a/src/lib/rng/system_rng/system_rng.cpp +++ b/src/lib/rng/system_rng/system_rng.cpp @@ -19,6 +19,10 @@ #elif defined(BOTAN_TARGET_OS_HAS_ARC4RANDOM) #include <stdlib.h> +#elif defined(BOTAN_TARGET_OS_HAS_GETRANDOM) + #include <sys/random.h> + #include <errno.h> + #elif defined(BOTAN_TARGET_OS_HAS_DEV_RANDOM) #include <sys/types.h> #include <sys/stat.h> @@ -125,6 +129,41 @@ class System_RNG_Impl final : public RandomNumberGenerator std::string name() const override { return "arc4random"; } }; +#elif defined(BOTAN_TARGET_OS_HAS_GETRANDOM) + +class System_RNG_Impl final : public RandomNumberGenerator + { + public: + // No constructor or destructor needed as no userland state maintained + + void randomize(uint8_t buf[], size_t len) override + { + const unsigned int flags = 0; + + while(len > 0) + { + const ssize_t got = ::getrandom(buf, len, flags); + + if(got < 0) + { + if(errno == EINTR) + continue; + throw Exception("System_RNG getrandom failed error " + std::to_string(errno)); + } + + buf += got; + len -= got; + } + } + + bool accepts_input() const override { return false; } + void add_entropy(const uint8_t[], size_t) override { /* ignored */ } + bool is_seeded() const override { return true; } + void clear() override { /* not possible */ } + std::string name() const override { return "getrandom"; } + }; + + #elif defined(BOTAN_TARGET_OS_HAS_DEV_RANDOM) // Read a random device |