random_provider_wincrypt.ipp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. * https://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. # if defined(BOOST_AUTO_LINK_NOMANGLE)
  29. # include <boost/config/auto_link.hpp>
  30. # else
  31. # define BOOST_AUTO_LINK_NOMANGLE
  32. # include <boost/config/auto_link.hpp>
  33. # undef BOOST_AUTO_LINK_NOMANGLE
  34. # endif
  35. #endif
  36. namespace boost {
  37. namespace uuids {
  38. namespace detail {
  39. class random_provider_base
  40. {
  41. BOOST_MOVABLE_BUT_NOT_COPYABLE(random_provider_base)
  42. public:
  43. random_provider_base()
  44. : hProv_(0)
  45. {
  46. boost::winapi::BOOL_ res = boost::winapi::CryptAcquireContextW(
  47. &hProv_,
  48. NULL,
  49. NULL,
  50. boost::winapi::PROV_RSA_FULL_,
  51. boost::winapi::CRYPT_VERIFYCONTEXT_ | boost::winapi::CRYPT_SILENT_);
  52. if (BOOST_UNLIKELY(!res))
  53. {
  54. boost::winapi::DWORD_ err = boost::winapi::GetLastError();
  55. BOOST_THROW_EXCEPTION(entropy_error(err, "CryptAcquireContext"));
  56. }
  57. }
  58. random_provider_base(BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT : hProv_(that.hProv_)
  59. {
  60. that.hProv_ = 0;
  61. }
  62. random_provider_base& operator= (BOOST_RV_REF(random_provider_base) that) BOOST_NOEXCEPT
  63. {
  64. destroy();
  65. hProv_ = that.hProv_;
  66. that.hProv_ = 0;
  67. return *this;
  68. }
  69. ~random_provider_base() BOOST_NOEXCEPT
  70. {
  71. destroy();
  72. }
  73. //! Obtain entropy and place it into a memory location
  74. //! \param[in] buf the location to write entropy
  75. //! \param[in] siz the number of bytes to acquire
  76. void get_random_bytes(void *buf, std::size_t siz)
  77. {
  78. boost::winapi::BOOL_ res = boost::winapi::CryptGenRandom(
  79. hProv_,
  80. boost::numeric_cast<boost::winapi::DWORD_>(siz),
  81. static_cast<boost::winapi::BYTE_ *>(buf));
  82. if (BOOST_UNLIKELY(!res))
  83. {
  84. boost::winapi::DWORD_ err = boost::winapi::GetLastError();
  85. BOOST_THROW_EXCEPTION(entropy_error(err, "CryptGenRandom"));
  86. }
  87. }
  88. private:
  89. void destroy() BOOST_NOEXCEPT
  90. {
  91. if (hProv_)
  92. {
  93. boost::ignore_unused(boost::winapi::CryptReleaseContext(hProv_, 0));
  94. }
  95. }
  96. private:
  97. boost::winapi::HCRYPTPROV_ hProv_;
  98. };
  99. } // detail
  100. } // uuids
  101. } // boost