forked from boostorg/random
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update the random_device implementation so that it:
* Supports more platforms optimally, for example CloudABI, OpenBSD, and Windows UWP * Is easier to maintain as each platform's implementation is in a separate file * Removes the library dependency on Boost.System * Is header-only, and thus makes Boost.Random header-only * Is well-tested for happy and sad paths Adds a new exception "entropy_error" to handle errors getting entropy. Removed the detail auto_link implementation inside Boost.Random as it is no longer necessary - the one in Boost.Config is sufficient. Also added a top-level Jamfile that builds the example subdirectory with each build, as one of the examples needed to be updated in order to build. This will prevent rot in the example directory. Note: Other libraries that link against Boost.Random (like Boost.Uuid) will fail to build until they stop trying to link against Boost.Random. This fixes boostorg#20 This fixes boostorg#22
- Loading branch information
Showing
25 changed files
with
1,240 additions
and
397 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/doc/html | ||
/doc/reference.xml | ||
/test/rng.saved | ||
/example/rng.saved | ||
**/rng.saved |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Boost.Random Library Jamfile | ||
# | ||
# Copyright (c) 2017 James E. King, III | ||
# | ||
# Use, modification, and distribution are subject to the | ||
# Boost Software License, Version 1.0. (See accompanying file | ||
# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
|
||
project libs/random | ||
: requirements | ||
|
||
<warnings>all | ||
|
||
<toolset>clang:<cxxflags>-Wextra | ||
<toolset>clang:<cxxflags>-ansi | ||
# <toolset>clang:<cxxflags>-pedantic | ||
<toolset>clang:<cxxflags>-Wno-c++11-long-long | ||
|
||
<toolset>gcc:<cxxflags>-Wextra | ||
<toolset>gcc:<cxxflags>-ansi | ||
# <toolset>gcc:<cxxflags>-pedantic | ||
<toolset>gcc:<cxxflags>-Wno-long-long | ||
; | ||
|
||
# pedantic mode disabled due to issue in multiprecision | ||
# https://github.com/boostorg/multiprecision/issues/34 | ||
|
||
# please order by name to ease maintenance | ||
build-project example ; | ||
build-project test ; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// Copyright (c) 2017 James E. King III | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENCE_1_0.txt) | ||
// | ||
// Platform-specific random entropy provider | ||
// | ||
|
||
#ifndef BOOST_RANDOM_DETAIL_RANDOM_PROVIDER_HPP | ||
#define BOOST_RANDOM_DETAIL_RANDOM_PROVIDER_HPP | ||
|
||
#include <boost/core/noncopyable.hpp> | ||
#include <boost/cstdint.hpp> | ||
#include <boost/limits.hpp> | ||
#include <boost/static_assert.hpp> | ||
#include <boost/type_traits/is_integral.hpp> | ||
#include <boost/type_traits/is_unsigned.hpp> | ||
#include <boost/random/entropy_error.hpp> | ||
#include <iterator> | ||
|
||
// Detection of the platform is separated from inclusion of the correct | ||
// header to facilitate mock testing of the provider implementations. | ||
|
||
#include <boost/random/detail/random_provider_detect_platform.hpp> | ||
#include <boost/random/detail/random_provider_include_platform.hpp> | ||
|
||
|
||
namespace boost { | ||
namespace random { | ||
namespace detail { | ||
|
||
//! \brief Contains code common to all random_provider implementations. | ||
//! \note random_provider_base is required to provide this method: | ||
//! void get_random_bytes(void *buf, size_t siz); | ||
//! \note noncopyable because of some base implementations so | ||
//! this makes it uniform across platforms to avoid any | ||
//! porting surprises | ||
class random_provider | ||
: public detail::random_provider_base, | ||
public noncopyable | ||
{ | ||
public: | ||
//! Leverage the provider as a SeedSeq for | ||
//! PseudoRandomNumberGeneration seeding | ||
//! \note: See Boost.Random documentation for more details | ||
template<class Iter> | ||
void generate(Iter first, Iter last) | ||
{ | ||
typedef typename std::iterator_traits<Iter>::value_type value_type; | ||
BOOST_STATIC_ASSERT(is_integral<value_type>::value); | ||
BOOST_STATIC_ASSERT(is_unsigned<value_type>::value); | ||
BOOST_STATIC_ASSERT(sizeof(value_type) * CHAR_BIT >= 32); | ||
|
||
for (; first != last; ++first) | ||
{ | ||
get_random_bytes(&*first, sizeof(*first)); | ||
*first &= (std::numeric_limits<boost::uint32_t>::max)(); | ||
} | ||
} | ||
|
||
//! Return the name of the selected provider | ||
const char * name() const | ||
{ | ||
return BOOST_RANDOM_PROVIDER_STRINGIFY(BOOST_RANDOM_PROVIDER_NAME); | ||
} | ||
}; | ||
|
||
} // detail | ||
} // random | ||
} // boost | ||
|
||
#endif // BOOST_RANDOM_DETAIL_RANDOM_PROVIDER_HPP | ||
|
32 changes: 32 additions & 0 deletions
32
include/boost/random/detail/random_provider_arc4random.ipp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// | ||
// Copyright (c) 2017 James E. King III | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENCE_1_0.txt) | ||
// | ||
// "A Replacement Call for Random" | ||
// https://man.openbsd.org/arc4random.3 | ||
// | ||
|
||
#include <stdlib.h> | ||
|
||
namespace boost { | ||
namespace random { | ||
namespace detail { | ||
|
||
class random_provider_base | ||
{ | ||
public: | ||
//! Obtain entropy and place it into a memory location | ||
//! \param[in] buf the location to write entropy | ||
//! \param[in] siz the number of bytes to acquire | ||
void get_random_bytes(void *buf, size_t siz) | ||
{ | ||
arc4random_buf(buf, siz); | ||
} | ||
}; | ||
|
||
} // detail | ||
} // random | ||
} // boost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// | ||
// Copyright (c) 2017 James E. King III | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENCE_1_0.txt) | ||
// | ||
// BCrypt provider for entropy | ||
// | ||
|
||
#include <boost/core/ignore_unused.hpp> | ||
#include <boost/throw_exception.hpp> | ||
#include <boost/winapi/bcrypt.hpp> | ||
#include <boost/winapi/get_last_error.hpp> | ||
|
||
#if defined(BOOST_RANDOM_FORCE_AUTO_LINK) || (!defined(BOOST_ALL_NO_LIB) && !defined(BOOST_RANDOM_NO_LIB)) | ||
# define BOOST_LIB_NAME "bcrypt" | ||
# define BOOST_AUTO_LINK_NOMANGLE | ||
# include <boost/config/auto_link.hpp> | ||
# undef BOOST_AUTO_LINK_NOMANGLE | ||
#endif | ||
|
||
namespace boost { | ||
namespace random { | ||
namespace detail { | ||
|
||
class random_provider_base | ||
{ | ||
public: | ||
random_provider_base() | ||
: hProv_(NULL) | ||
{ | ||
boost::winapi::NTSTATUS_ status = | ||
boost::winapi::BCryptOpenAlgorithmProvider( | ||
&hProv_, | ||
boost::winapi::BCRYPT_RNG_ALGORITHM_, | ||
NULL, | ||
0); | ||
|
||
if (status) | ||
{ | ||
BOOST_THROW_EXCEPTION(entropy_error(status, "BCryptOpenAlgorithmProvider")); | ||
} | ||
} | ||
|
||
~random_provider_base() BOOST_NOEXCEPT | ||
{ | ||
if (hProv_) | ||
{ | ||
ignore_unused(boost::winapi::BCryptCloseAlgorithmProvider(hProv_, 0)); | ||
} | ||
} | ||
|
||
//! Obtain entropy and place it into a memory location | ||
//! \param[in] buf the location to write entropy | ||
//! \param[in] siz the number of bytes to acquire | ||
void get_random_bytes(void *buf, size_t siz) | ||
{ | ||
boost::winapi::NTSTATUS_ status = | ||
boost::winapi::BCryptGenRandom( | ||
hProv_, | ||
static_cast<boost::winapi::PUCHAR_>(buf), | ||
siz, | ||
0); | ||
|
||
if (status) | ||
{ | ||
BOOST_THROW_EXCEPTION(entropy_error(status, "BCryptGenRandom")); | ||
} | ||
} | ||
|
||
private: | ||
boost::winapi::BCRYPT_ALG_HANDLE_ hProv_; | ||
}; | ||
|
||
} // detail | ||
} // random | ||
} // boost |
62 changes: 62 additions & 0 deletions
62
include/boost/random/detail/random_provider_detect_platform.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// Copyright (c) 2017 James E. King III | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENCE_1_0.txt) | ||
// | ||
// Platform-specific random entropy provider platform detection | ||
// | ||
|
||
#ifndef BOOST_RANDOM_DETAIL_RANDOM_PROVIDER_PLATFORM_DETECTION_HPP | ||
#define BOOST_RANDOM_DETAIL_RANDOM_PROVIDER_PLATFORM_DETECTION_HPP | ||
|
||
#include <boost/predef/library/c/cloudabi.h> | ||
#include <boost/predef/library/c/gnu.h> | ||
#include <boost/predef/os/bsd/open.h> | ||
#include <boost/predef/os/windows.h> | ||
|
||
// | ||
// Platform Detection - will load in the correct header and | ||
// will define the class <tt>random_provider_base</tt>. | ||
// | ||
|
||
#if BOOST_OS_BSD_OPEN >= BOOST_VERSION_NUMBER(2, 1, 0) || BOOST_LIB_C_CLOUDABI | ||
# define BOOST_RANDOM_PROVIDER_ARC4RANDOM | ||
# define BOOST_RANDOM_PROVIDER_NAME arc4random | ||
|
||
#elif BOOST_OS_WINDOWS | ||
# include <boost/winapi/config.hpp> | ||
# if BOOST_WINAPI_PARTITION_APP_SYSTEM && \ | ||
!defined(BOOST_RANDOM_PROVIDER_FORCE_WINCRYPT) && \ | ||
!defined(_WIN32_WCE) && \ | ||
(defined(BOOST_WINAPI_IS_MINGW_W64) || \ | ||
(BOOST_USE_WINAPI_VERSION >= BOOST_WINAPI_VERSION_WIN6)) | ||
# define BOOST_RANDOM_PROVIDER_BCRYPT | ||
# define BOOST_RANDOM_PROVIDER_NAME bcrypt | ||
|
||
# elif BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM | ||
# define BOOST_RANDOM_PROVIDER_WINCRYPT | ||
# define BOOST_RANDOM_PROVIDER_NAME wincrypt | ||
# else | ||
# error Unable to find a suitable windows entropy provider | ||
# endif | ||
|
||
#elif BOOST_LIB_C_GNU >= BOOST_VERSION_NUMBER(2, 25, 0) && !defined(BOOST_RANDOM_PROVIDER_FORCE_POSIX) | ||
# define BOOST_RANDOM_PROVIDER_GETENTROPY | ||
# define BOOST_RANDOM_PROVIDER_NAME getentropy | ||
|
||
#else | ||
# define BOOST_RANDOM_PROVIDER_POSIX | ||
# define BOOST_RANDOM_PROVIDER_NAME posix | ||
|
||
#endif | ||
|
||
#define BOOST_RANDOM_PROVIDER_STRINGIFY2(X) #X | ||
#define BOOST_RANDOM_PROVIDER_STRINGIFY(X) BOOST_RANDOM_PROVIDER_STRINGIFY2(X) | ||
|
||
#if defined(BOOST_RANDOM_PROVIDER_SHOW) | ||
#pragma message("BOOST_RANDOM_PROVIDER_NAME " BOOST_RANDOM_PROVIDER_STRINGIFY(BOOST_RANDOM_PROVIDER_NAME)) | ||
#endif | ||
|
||
#endif // BOOST_RANDOM_DETAIL_RANDOM_PROVIDER_PLATFORM_DETECTION_HPP |
Oops, something went wrong.