From f6c35f47ff4b3bca03f2a6f057aa5a49712539e4 Mon Sep 17 00:00:00 2001 From: Jack Lloyd Date: Mon, 28 May 2018 17:46:14 -0400 Subject: Add back support for Windows Phone RNG, undeprecate UWP See #1586. Reverts part of #1494 --- src/lib/rng/system_rng/system_rng.cpp | 44 +++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'src/lib/rng/system_rng/system_rng.cpp') 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 +#elif defined(BOTAN_TARGET_OS_HAS_CRYPTO_NG) + #include + #elif defined(BOTAN_TARGET_OS_HAS_ARC4RANDOM) #include @@ -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(buf), static_cast(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 -- cgit v1.2.3