random_provider_getrandom.ipp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // Copyright (c) 2018 Andrey Semashev
  3. //
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // https://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // getrandom() capable platforms
  9. //
  10. #include <boost/config.hpp>
  11. #include <boost/throw_exception.hpp>
  12. #include <boost/predef/library/c/gnu.h>
  13. #include <cerrno>
  14. #include <cstddef>
  15. #if !defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM)
  16. #if BOOST_LIB_C_GNU >= BOOST_VERSION_NUMBER(2, 25, 0)
  17. #define BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER
  18. #elif defined(__has_include)
  19. #if __has_include(<sys/random.h>)
  20. #define BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER
  21. #endif
  22. #endif
  23. #endif // !defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM)
  24. #if defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER)
  25. #include <sys/random.h>
  26. #else
  27. #include <stddef.h> // ssize_t
  28. #endif
  29. namespace boost {
  30. namespace uuids {
  31. namespace detail {
  32. class random_provider_base
  33. {
  34. public:
  35. //! Obtain entropy and place it into a memory location
  36. //! \param[in] buf the location to write entropy
  37. //! \param[in] siz the number of bytes to acquire
  38. void get_random_bytes(void *buf, std::size_t siz)
  39. {
  40. std::size_t offset = 0;
  41. while (offset < siz)
  42. {
  43. ssize_t sz = get_random(static_cast< char* >(buf) + offset, siz - offset, 0u);
  44. if (BOOST_UNLIKELY(sz < 0))
  45. {
  46. int err = errno;
  47. if (err == EINTR)
  48. continue;
  49. BOOST_THROW_EXCEPTION(entropy_error(err, "getrandom"));
  50. }
  51. offset += sz;
  52. }
  53. }
  54. private:
  55. static ssize_t get_random(void *buf, std::size_t size, unsigned int flags)
  56. {
  57. #if defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM)
  58. return BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_IMPL_GETRANDOM(buf, size, flags);
  59. #elif defined(BOOST_UUID_RANDOM_PROVIDER_GETRANDOM_HAS_LIBC_WRAPPER)
  60. return ::getrandom(buf, size, flags);
  61. #else
  62. return ::syscall(SYS_getrandom, buf, size, flags);
  63. #endif
  64. }
  65. };
  66. } // detail
  67. } // uuids
  68. } // boost