Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Jamfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Boost.Random Library Jamfile
#
# Copyright (c) 2010
# Steven Watanabe
#
# 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 ;

# please order by name to ease maintenance
build-project test ;
18 changes: 0 additions & 18 deletions build/Jamfile.v2

This file was deleted.

110 changes: 110 additions & 0 deletions include/boost/random/detail/random_device_bcrypt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* boost random/detail/random_device_bcrypt header file
*
* Copyright 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/LICENSE_1_0.txt)
*
* $Id$
*
* Revision history
* 2017-09-14 initial bcrypt implementation
*/

#ifndef BOOST_RANDOM_DETAIL_RANDOM_DEVICE_BCRYPT
#define BOOST_RANDOM_DETAIL_RANDOM_DEVICE_BCRYPT

#include <boost/config.hpp>

#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

#if defined(BOOST_WINDOWS)
#include <boost/winapi/config.hpp>

#if !(BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM) || defined(BOOST_RANDOM_DEVICE_FORCE_BCRYPT)

#include <boost/core/ignore_unused.hpp>
#include <boost/winapi/bcrypt.hpp>
#include <boost/winapi/detail/cast_ptr.hpp>
#include <boost/winapi/get_last_error.hpp>
#include <boost/noncopyable.hpp>
#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>
#include <string>

#if !defined(BOOST_RANDOM_DEVICE_NO_LIB)
# define BOOST_LIB_NAME "bcrypt"
# define BOOST_AUTO_LINK_NOMANGLE
# include <boost/config/auto_link.hpp>
#endif

namespace boost {
namespace random {
namespace detail {

template<class Entropy>
class random_device_bcrypt : private noncopyable
{
public:
typedef Entropy result_type;

random_device_bcrypt(const std::string& token = std::string())
: hProv_(NULL)
{
boost::ignore_unused(token);
boost::winapi::NTSTATUS_ status =
boost::winapi::BCryptOpenAlgorithmProvider(
&hProv_,
boost::winapi::BCRYPT_RNG_ALGORITHM_,
NULL,
0);

if (status)
{
BOOST_THROW_EXCEPTION(system::system_error(
status, system::system_category(), "BCryptOpenAlgorithmProvider"));
}
}

~random_device_bcrypt() BOOST_NOEXCEPT
{
if (hProv_)
{
ignore_unused(boost::winapi::BCryptCloseAlgorithmProvider(hProv_, 0));
}
}

result_type operator()()
{
result_type result;

boost::winapi::NTSTATUS_ status =
boost::winapi::BCryptGenRandom(
hProv_,
reinterpret_cast<boost::winapi::PUCHAR_>(&result),
sizeof(result),
0);

if (status)
{
BOOST_THROW_EXCEPTION(system::system_error(
status, system::system_category(), "BCryptGenRandom"));
}

return result;
}

private:
boost::winapi::BCRYPT_ALG_HANDLE_ hProv_;
};

} // detail
} // random
} // boost

#endif // !(BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM) || defined(BOOST_RANDOM_DEVICE_FORCE_BCRYPT)
#endif // BOOST_WINDOWS
#endif // BOOST_RANDOM_DETAIL_RANDOM_DEVICE_BCRYPT
100 changes: 100 additions & 0 deletions include/boost/random/detail/random_device_file.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/* boost random/detail/random_device_file header file
*
* Copyright Jens Maurer 2000
* Copyright 2007 Andy Tompkins.
* Copyright Steven Watanabe 2010-2011
* Copyright 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/LICENSE_1_0.txt)
*
* $Id$
*
* Revision history
* 2017-09-14 urandom implementation moved here
*/

#ifndef BOOST_RANDOM_DETAIL_RANDOM_DEVICE_FILE
#define BOOST_RANDOM_DETAIL_RANDOM_DEVICE_FILE

#include <boost/config.hpp>

#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

#if !defined(BOOST_WINDOWS)

#include <boost/core/ignore_unused.hpp>
#include <boost/noncopyable.hpp>
#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>
#include <cerrno>
#include <fcntl.h> // open
#include <string>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(BOOST_HAS_UNISTD_H)
#include <unistd.h>
#endif

namespace boost {
namespace random {
namespace detail {

template<class Entropy>
class random_device_file : private noncopyable
{
public:
typedef Entropy result_type;

random_device_file(const std::string& token = std::string())
: fd_(0)
{
fd_ = open(token.empty() ? "/dev/urandom" : token.c_str(), O_RDONLY);

if (-1 == fd_)
{
BOOST_THROW_EXCEPTION(system::system_error(
errno, system::system_category(), "open " + std::string(token.empty() ? "/dev/urandom" : token.c_str())));
}
}

~random_device_file() BOOST_NOEXCEPT
{
if (fd_)
{
ignore_unused(close(fd_));
}
}

result_type operator()()
{
result_type result;
size_t offset = 0;
do
{
ssize_t sz = read(fd_, reinterpret_cast<char *>(&result) + offset, sizeof(result) - offset);

if (sz < 1)
{
BOOST_THROW_EXCEPTION(system::system_error(
errno, system::system_category(), "read"));
}
offset += sz;
} while (offset < sizeof(result));

return result;
}

private:
int fd_;
};

} // detail
} // random
} // boost

#endif // !defined(BOOST_WINDOWS)
#endif // BOOST_RANDOM_DETAIL_RANDOM_DEVICE_FILE
140 changes: 140 additions & 0 deletions include/boost/random/detail/random_device_wincrypt.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/* boost random/detail/random_device_wincrypt header file
*
* Copyright Jens Maurer 2000
* Copyright 2007 Andy Tompkins.
* Copyright Steven Watanabe 2010-2011
* Copyright 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/LICENSE_1_0.txt)
*
* $Id$
*
* Revision history
* 2017-09-14 wincrypt implementation moved here
*/

#ifndef BOOST_RANDOM_DETAIL_RANDOM_DEVICE_WINCRYPT
#define BOOST_RANDOM_DETAIL_RANDOM_DEVICE_WINCRYPT

#include <boost/config.hpp>

#ifdef BOOST_HAS_PRAGMA_ONCE
#pragma once
#endif

#if defined(BOOST_WINDOWS)
#include <boost/detail/winapi/config.hpp>

#if BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM

#include <boost/core/ignore_unused.hpp>
#include <boost/winapi/crypt.hpp>
#include <boost/winapi/detail/cast_ptr.hpp>
#include <boost/winapi/get_last_error.hpp>
#include <boost/noncopyable.hpp>
#include <boost/system/system_error.hpp>
#include <boost/throw_exception.hpp>
#include <string>

#if !defined(BOOST_RANDOM_DEVICE_NO_LIB)
# if defined(_WIN32_WCE)
# define BOOST_LIB_NAME "coredll"
# else
# define BOOST_LIB_NAME "advapi32"
# endif
# define BOOST_AUTO_LINK_NOMANGLE
# include <boost/config/auto_link.hpp>
#endif

namespace boost {
namespace random {
namespace detail {

template<class Entropy>
class random_device_wincrypt : private noncopyable
{
public:
typedef Entropy result_type;

random_device_wincrypt(const std::string& token = std::string())
: hProv_(NULL)
{
#if defined(BOOST_NO_ANSI_APIS)
// Without ANSI APIs, the token is ignored
ignore_unused(token);
if (!boost::winapi::CryptAcquireContextW(
&hProv_,
NULL,
NULL,
boost::winapi::PROV_RNG_,
boost::winapi::CRYPT_VERIFYCONTEXT_ | boost::winapi::CRYPT_SILENT_))
{
BOOST_THROW_EXCEPTION(system::system_error(
boost::winapi::GetLastError(), system::system_category(), "CryptAcquireContextW"));
}
#else
// With ANSI APIs, the behavior is backwards compatible to previous releases
boost::winapi::CHAR_ buffer[256];
boost::winapi::DWORD_ type;
boost::winapi::DWORD_ len;
std::string provider = token.empty() ? "Microsoft Base Cryptographic Provider v1.0" : token;

// Find the type of a specific provider
for (boost::winapi::DWORD_ i = 0; ; ++i)
{
len = sizeof(buffer);
if (!boost::winapi::CryptEnumProvidersA(i, NULL, 0, &type, buffer, &len))
{
BOOST_THROW_EXCEPTION(system::system_error(
boost::winapi::GetLastError(), system::system_category(), "CryptEnumProvidersA"));
}
if (buffer == provider) {
break;
}
}

if (!boost::winapi::CryptAcquireContextA(
&hProv_,
NULL,
provider.c_str(),
type,
boost::winapi::CRYPT_VERIFYCONTEXT_ | boost::winapi::CRYPT_SILENT_))
{
BOOST_THROW_EXCEPTION(system::system_error(
boost::winapi::GetLastError(), system::system_category(), "CryptAcquireContextA"));
}
#endif
}

~random_device_wincrypt() BOOST_NOEXCEPT
{
if (hProv_)
{
ignore_unused(boost::winapi::CryptReleaseContext(hProv_, 0));
}
}

result_type operator()()
{
result_type result;
if (!boost::winapi::CryptGenRandom(hProv_, sizeof(result), boost::winapi::detail::cast_ptr(&result)))
{
BOOST_THROW_EXCEPTION(system::system_error(
boost::winapi::GetLastError(), system::system_category(), "CryptGenRandom"));
}
return result;
}

private:
boost::winapi::HCRYPTPROV_ hProv_;
};

} // detail
} // random
} // boost

#endif // BOOST_WINAPI_PARTITION_DESKTOP || BOOST_WINAPI_PARTITION_SYSTEM
#endif // BOOST_WINDOWS
#endif // BOOST_RANDOM_DETAIL_RANDOM_DEVICE_WINCRYPT
Loading