1 /* boost uuid/detail/random_provider_wincrypt implementation
3 * Copyright Jens Maurer 2000
4 * Copyright 2007 Andy Tompkins.
5 * Copyright Steven Watanabe 2010-2011
6 * Copyright 2017 James E. King III
8 * Distributed under the Boost Software License, Version 1.0. (See
9 * accompanying file LICENSE_1_0.txt or copy at
10 * https://www.boost.org/LICENSE_1_0.txt)
16 #include <boost/config.hpp>
17 #include <boost/core/ignore_unused.hpp>
18 #include <boost/move/core.hpp>
19 #include <boost/numeric/conversion/cast.hpp>
20 #include <boost/winapi/crypt.hpp>
21 #include <boost/winapi/get_last_error.hpp>
22 #include <boost/throw_exception.hpp>
24 #if defined(BOOST_UUID_FORCE_AUTO_LINK) || (!defined(BOOST_ALL_NO_LIB) && !defined(BOOST_UUID_RANDOM_PROVIDER_NO_LIB))
25 # if defined(_WIN32_WCE)
26 # define BOOST_LIB_NAME "coredll"
28 # define BOOST_LIB_NAME "advapi32"
30 # if defined(BOOST_AUTO_LINK_NOMANGLE)
31 # include <boost/config/auto_link.hpp>
33 # define BOOST_AUTO_LINK_NOMANGLE
34 # include <boost/config/auto_link.hpp>
35 # undef BOOST_AUTO_LINK_NOMANGLE
43 class random_provider_base
45 BOOST_MOVABLE_BUT_NOT_COPYABLE(random_provider_base)
48 random_provider_base()
51 boost::winapi::BOOL_ res = boost::winapi::CryptAcquireContextW(
55 boost::winapi::PROV_RSA_FULL_,
56 boost::winapi::CRYPT_VERIFYCONTEXT_ | boost::winapi::CRYPT_SILENT_);
57 if (BOOST_UNLIKELY(!res))
59 boost::winapi::DWORD_ err = boost::winapi::GetLastError();
60 BOOST_THROW_EXCEPTION(entropy_error(err, "CryptAcquireContext"));
64 random_provider_base(BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT : hProv_(that.hProv_)
69 random_provider_base& operator= (BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT
77 ~random_provider_base() BOOST_NOEXCEPT
82 //! Obtain entropy and place it into a memory location
83 //! \param[in] buf the location to write entropy
84 //! \param[in] siz the number of bytes to acquire
85 void get_random_bytes(void *buf, std::size_t siz)
87 boost::winapi::BOOL_ res = boost::winapi::CryptGenRandom(
89 boost::numeric_cast<boost::winapi::DWORD_>(siz),
90 static_cast<boost::winapi::BYTE_ *>(buf));
91 if (BOOST_UNLIKELY(!res))
93 boost::winapi::DWORD_ err = boost::winapi::GetLastError();
94 BOOST_THROW_EXCEPTION(entropy_error(err, "CryptGenRandom"));
99 void destroy() BOOST_NOEXCEPT
103 boost::ignore_unused(boost::winapi::CryptReleaseContext(hProv_, 0));
108 boost::winapi::HCRYPTPROV_ hProv_;