diff options
author | Jack Lloyd <[email protected]> | 2018-05-28 17:46:14 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-05-28 17:48:06 -0400 |
commit | f6c35f47ff4b3bca03f2a6f057aa5a49712539e4 (patch) | |
tree | 08d045a697f7d8438f97229baadf3d2daa3e5206 /src/lib/rng/system_rng/system_rng.cpp | |
parent | c6a95c62236cdbc0feaae21715900fe9ee45039a (diff) |
Add back support for Windows Phone RNG, undeprecate UWP
See #1586. Reverts part of #1494
Diffstat (limited to 'src/lib/rng/system_rng/system_rng.cpp')
-rw-r--r-- | src/lib/rng/system_rng/system_rng.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
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 |