File indexing completed on 2025-01-18 09:53:31
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <cstddef>
0016 #include <boost/config.hpp>
0017 #include <boost/core/ignore_unused.hpp>
0018 #include <boost/move/core.hpp>
0019 #include <boost/numeric/conversion/cast.hpp>
0020 #include <boost/winapi/crypt.hpp>
0021 #include <boost/winapi/get_last_error.hpp>
0022 #include <boost/throw_exception.hpp>
0023
0024 #if defined(BOOST_UUID_FORCE_AUTO_LINK) || (!defined(BOOST_ALL_NO_LIB) && !defined(BOOST_UUID_RANDOM_PROVIDER_NO_LIB))
0025 # if defined(_WIN32_WCE)
0026 # define BOOST_LIB_NAME "coredll"
0027 # else
0028 # define BOOST_LIB_NAME "advapi32"
0029 # endif
0030 # if defined(BOOST_AUTO_LINK_NOMANGLE)
0031 # include <boost/config/auto_link.hpp>
0032 # else
0033 # define BOOST_AUTO_LINK_NOMANGLE
0034 # include <boost/config/auto_link.hpp>
0035 # undef BOOST_AUTO_LINK_NOMANGLE
0036 # endif
0037 #endif
0038
0039 namespace boost {
0040 namespace uuids {
0041 namespace detail {
0042
0043 class random_provider_base
0044 {
0045 BOOST_MOVABLE_BUT_NOT_COPYABLE(random_provider_base)
0046
0047 public:
0048 random_provider_base()
0049 : hProv_(0)
0050 {
0051 boost::winapi::BOOL_ res = boost::winapi::CryptAcquireContextW(
0052 &hProv_,
0053 NULL,
0054 NULL,
0055 boost::winapi::PROV_RSA_FULL_,
0056 boost::winapi::CRYPT_VERIFYCONTEXT_ | boost::winapi::CRYPT_SILENT_);
0057 if (BOOST_UNLIKELY(!res))
0058 {
0059 boost::winapi::DWORD_ err = boost::winapi::GetLastError();
0060 BOOST_THROW_EXCEPTION(entropy_error(err, "CryptAcquireContext"));
0061 }
0062 }
0063
0064 random_provider_base(BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT : hProv_(that.hProv_)
0065 {
0066 that.hProv_ = 0;
0067 }
0068
0069 random_provider_base& operator= (BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT
0070 {
0071 destroy();
0072 hProv_ = that.hProv_;
0073 that.hProv_ = 0;
0074 return *this;
0075 }
0076
0077 ~random_provider_base() BOOST_NOEXCEPT
0078 {
0079 destroy();
0080 }
0081
0082
0083
0084
0085 void get_random_bytes(void *buf, std::size_t siz)
0086 {
0087 boost::winapi::BOOL_ res = boost::winapi::CryptGenRandom(
0088 hProv_,
0089 boost::numeric_cast<boost::winapi::DWORD_>(siz),
0090 static_cast<boost::winapi::BYTE_ *>(buf));
0091 if (BOOST_UNLIKELY(!res))
0092 {
0093 boost::winapi::DWORD_ err = boost::winapi::GetLastError();
0094 BOOST_THROW_EXCEPTION(entropy_error(err, "CryptGenRandom"));
0095 }
0096 }
0097
0098 private:
0099 void destroy() BOOST_NOEXCEPT
0100 {
0101 if (hProv_)
0102 {
0103 boost::ignore_unused(boost::winapi::CryptReleaseContext(hProv_, 0));
0104 }
0105 }
0106
0107 private:
0108 boost::winapi::HCRYPTPROV_ hProv_;
0109 };
0110
0111 }
0112 }
0113 }