random_provider_wincrypt.ipp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* boost uuid/detail/random_provider_wincrypt implementation
  2. *
  3. * Copyright Jens Maurer 2000
  4. * Copyright 2007 Andy Tompkins.
  5. * Copyright Steven Watanabe 2010-2011
  6. * Copyright 2017 James E. King III
  7. *
  8. * Distributed under the Boost Software License, Version 1.0. (See
  9. * accompanying file LICENSE_1_0.txt or copy at
  10. * http://www.boost.org/LICENSE_1_0.txt)
  11. *
  12. * $Id$
  13. */
  14. #include <cstddef>
  15. #include <boost/config.hpp>
  16. #include <boost/core/ignore_unused.hpp>
  17. #include <boost/move/core.hpp>
  18. #include <boost/numeric/conversion/cast.hpp>
  19. #include <boost/winapi/crypt.hpp>
  20. #include <boost/winapi/get_last_error.hpp>
  21. #include <boost/throw_exception.hpp>
  22. #if defined(BOOST_UUID_FORCE_AUTO_LINK) || (!defined(BOOST_ALL_NO_LIB) && !defined(BOOST_UUID_RANDOM_PROVIDER_NO_LIB))
  23. # if defined(_WIN32_WCE)
  24. # define BOOST_LIB_NAME "coredll"
  25. # else
  26. # define BOOST_LIB_NAME "advapi32"
  27. # endif
  28. # define BOOST_AUTO_LINK_NOMANGLE
  29. # include <boost/config/auto_link.hpp>
  30. # undef BOOST_AUTO_LINK_NOMANGLE
  31. #endif
  32. namespace boost {
  33. namespace uuids {
  34. namespace detail {
  35. class random_provider_base
  36. {
  37. BOOST_MOVABLE_BUT_NOT_COPYABLE(random_provider_base)
  38. public:
  39. random_provider_base()
  40. : hProv_(0)
  41. {
  42. boost::winapi::BOOL_ res = boost::winapi::CryptAcquireContextW(
  43. &hProv_,
  44. NULL,
  45. NULL,
  46. boost::winapi::PROV_RSA_FULL_,
  47. boost::winapi::CRYPT_VERIFYCONTEXT_ | boost::winapi::CRYPT_SILENT_);
  48. if (BOOST_UNLIKELY(!res))
  49. {
  50. boost::winapi::DWORD_ err = boost::winapi::GetLastError();
  51. BOOST_THROW_EXCEPTION(entropy_error(err, "CryptAcquireContext"));
  52. }
  53. }
  54. random_provider_base(BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT : hProv_(that.hProv_)
  55. {
  56. that.hProv_ = 0;
  57. }
  58. random_provider_base& operator= (BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT
  59. {
  60. destroy();
  61. hProv_ = that.hProv_;
  62. that.hProv_ = 0;
  63. return *this;
  64. }
  65. ~random_provider_base() BOOST_NOEXCEPT
  66. {
  67. destroy();
  68. }
  69. //! Obtain entropy and place it into a memory location
  70. //! \param[in] buf the location to write entropy
  71. //! \param[in] siz the number of bytes to acquire
  72. void get_random_bytes(void *buf, std::size_t siz)
  73. {
  74. boost::winapi::BOOL_ res = boost::winapi::CryptGenRandom(
  75. hProv_,
  76. boost::numeric_cast<boost::winapi::DWORD_>(siz),
  77. static_cast<boost::winapi::BYTE_ *>(buf));
  78. if (BOOST_UNLIKELY(!res))
  79. {
  80. boost::winapi::DWORD_ err = boost::winapi::GetLastError();
  81. BOOST_THROW_EXCEPTION(entropy_error(err, "CryptGenRandom"));
  82. }
  83. }
  84. private:
  85. void destroy() BOOST_NOEXCEPT
  86. {
  87. if (hProv_)
  88. {
  89. boost::ignore_unused(boost::winapi::CryptReleaseContext(hProv_, 0));
  90. }
  91. }
  92. private:
  93. boost::winapi::HCRYPTPROV_ hProv_;
  94. };
  95. } // detail
  96. } // uuids
  97. } // boost