diff options
Diffstat (limited to 'src/lib/rng/system_rng')
-rw-r--r-- | src/lib/rng/system_rng/info.txt | 5 | ||||
-rw-r--r-- | src/lib/rng/system_rng/system_rng.cpp | 44 |
2 files changed, 49 insertions, 0 deletions
diff --git a/src/lib/rng/system_rng/info.txt b/src/lib/rng/system_rng/info.txt index 4dc5be758..da4fce4e3 100644 --- a/src/lib/rng/system_rng/info.txt +++ b/src/lib/rng/system_rng/info.txt @@ -6,8 +6,13 @@ SYSTEM_RNG -> 20141202 dev_random,posix1 arc4random rtlgenrandom +crypto_ng </os_features> +<libs> +uwp -> bcrypt.lib +</libs> + <requires> rtlgenrandom?dyn_load </requires> diff --git a/src/lib/rng/system_rng/system_rng.cpp b/src/lib/rng/system_rng/system_rng.cpp index 3c2e98661..c3b37ea9c 100644 --- a/src/lib/rng/system_rng/system_rng.cpp +++ b/src/lib/rng/system_rng/system_rng.cpp @@ -13,6 +13,9 @@ #define _WINSOCKAPI_ // stop windows.h including winsock.h #include <windows.h> +#elif defined(BOTAN_TARGET_OS_HAS_CRYPTO_NG) + #include <bcrypt.h> + #elif defined(BOTAN_TARGET_OS_HAS_ARC4RANDOM) #include <stdlib.h> @@ -60,6 +63,47 @@ class System_RNG_Impl final : public RandomNumberGenerator RtlGenRandom_fptr m_rtlgenrandom; }; +#elif defined(BOTAN_TARGET_OS_HAS_CRYPTO_NG) + +class System_RNG_Impl final : public RandomNumberGenerator + { + public: + System_RNG_Impl() + { + NTSTATUS ret = ::BCryptOpenAlgorithmProvider(&m_prov, + BCRYPT_RNG_ALGORITHM, + MS_PRIMITIVE_PROVIDER, 0); + if(ret != STATUS_SUCCESS) + throw Exception("System_RNG failed to acquire crypto provider"); + } + + ~System_RNG_Impl() + { + ::BCryptCloseAlgorithmProvider(m_prov, 0); + } + + void randomize(uint8_t buf[], size_t len) override + { + NTSTATUS ret = ::BCryptGenRandom(m_prov, static_cast<PUCHAR>(buf), static_cast<ULONG>(len), 0); + if(ret != STATUS_SUCCESS) + throw Exception("System_RNG call to BCryptGenRandom failed"); + } + + void add_entropy(const uint8_t in[], size_t length) override + { + /* + There is a flag BCRYPT_RNG_USE_ENTROPY_IN_BUFFER to provide + entropy inputs, but it is ignored in Windows 8 and later. + */ + } + + bool is_seeded() const override { return true; } + void clear() override { /* not possible */ } + std::string name() const override { return "crypto_ng"; } + private: + BCRYPT_ALG_HANDLE m_handle; + }; + #elif defined(BOTAN_TARGET_OS_HAS_ARC4RANDOM) class System_RNG_Impl final : public RandomNumberGenerator |