Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:31

0001 /* boost uuid/detail/random_provider_wincrypt implementation
0002 *
0003 * Copyright Jens Maurer 2000
0004 * Copyright 2007 Andy Tompkins.
0005 * Copyright Steven Watanabe 2010-2011
0006 * Copyright 2017 James E. King III
0007 *
0008 * Distributed under the Boost Software License, Version 1.0. (See
0009 * accompanying file LICENSE_1_0.txt or copy at
0010 * https://www.boost.org/LICENSE_1_0.txt)
0011 *
0012 * $Id$
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     //! Obtain entropy and place it into a memory location
0083     //! \param[in]  buf  the location to write entropy
0084     //! \param[in]  siz  the number of bytes to acquire
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 } // detail
0112 } // uuids
0113 } // boost