use /dev/random as random number source on macOS
This commit is contained in:
parent
8a35e42012
commit
0c189e08b5
|
@ -163,6 +163,7 @@ nobase_include_HEADERS = \
|
||||||
aux_/cpuid.hpp \
|
aux_/cpuid.hpp \
|
||||||
aux_/disable_warnings_push.hpp \
|
aux_/disable_warnings_push.hpp \
|
||||||
aux_/disable_warnings_pop.hpp \
|
aux_/disable_warnings_pop.hpp \
|
||||||
|
aux_/dev_random.hpp \
|
||||||
aux_/escape_string.hpp \
|
aux_/escape_string.hpp \
|
||||||
aux_/io.hpp \
|
aux_/io.hpp \
|
||||||
aux_/max_path.hpp \
|
aux_/max_path.hpp \
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
/*
|
||||||
|
|
||||||
|
Copyright (c) 2017, Arvid Norberg
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions
|
||||||
|
are met:
|
||||||
|
|
||||||
|
* Redistributions of source code must retain the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer.
|
||||||
|
* Redistributions in binary form must reproduce the above copyright
|
||||||
|
notice, this list of conditions and the following disclaimer in
|
||||||
|
the documentation and/or other materials provided with the distribution.
|
||||||
|
* Neither the name of the author nor the names of its
|
||||||
|
contributors may be used to endorse or promote products derived
|
||||||
|
from this software without specific prior written permission.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TORRENT_DEV_RANDOM_HPP_INCLUDED
|
||||||
|
#define TORRENT_DEV_RANDOM_HPP_INCLUDED
|
||||||
|
|
||||||
|
#include "libtorrent/config.hpp"
|
||||||
|
#include "libtorrent/span.hpp"
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
|
namespace libtorrent { namespace aux {
|
||||||
|
|
||||||
|
struct dev_random
|
||||||
|
{
|
||||||
|
dev_random()
|
||||||
|
: m_fd(open("/dev/random", O_RDONLY))
|
||||||
|
{
|
||||||
|
if (m_fd < 0)
|
||||||
|
{
|
||||||
|
#ifndef BOOST_NO_EXCEPTIONS
|
||||||
|
throw system_error(error_code(errno, system_category()));
|
||||||
|
#else
|
||||||
|
std::terminate();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dev_random(dev_random const&) = delete;
|
||||||
|
dev_random& operator=(dev_random const&) = delete;
|
||||||
|
|
||||||
|
void read(span<char> buffer)
|
||||||
|
{
|
||||||
|
std::size_t const ret = ::read(m_fd, buffer.data(), buffer.size());
|
||||||
|
if (ret != buffer.size())
|
||||||
|
{
|
||||||
|
#ifndef BOOST_NO_EXCEPTIONS
|
||||||
|
throw system_error(error_code(EIO, system_category()));
|
||||||
|
#else
|
||||||
|
std::terminate();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
~dev_random() { close(m_fd); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
int m_fd;
|
||||||
|
};
|
||||||
|
}}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
@ -153,6 +153,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#endif
|
#endif
|
||||||
#endif // __APPLE__
|
#endif // __APPLE__
|
||||||
|
|
||||||
|
#define TORRENT_USE_DEV_RANDOM 1
|
||||||
#define TORRENT_HAVE_MMAP 1
|
#define TORRENT_HAVE_MMAP 1
|
||||||
|
|
||||||
#define TORRENT_HAS_FALLOCATE 0
|
#define TORRENT_HAS_FALLOCATE 0
|
||||||
|
@ -413,6 +414,10 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
#define TORRENT_USE_CRYPTOAPI_SHA_512 0
|
#define TORRENT_USE_CRYPTOAPI_SHA_512 0
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef TORRENT_USE_DEV_RANDOM
|
||||||
|
#define TORRENT_USE_DEV_RANDOM 0
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef TORRENT_HAVE_MMAP
|
#ifndef TORRENT_HAVE_MMAP
|
||||||
#define TORRENT_HAVE_MMAP 0
|
#define TORRENT_HAVE_MMAP 0
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -46,10 +46,16 @@ extern "C" {
|
||||||
#include <openssl/err.h>
|
#include <openssl/err.h>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include <boost/asio/ssl/error.hpp>
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
#include "libtorrent/aux_/disable_warnings_pop.hpp"
|
||||||
|
|
||||||
|
#if TORRENT_USE_DEV_RANDOM
|
||||||
|
#include "libtorrent/aux_/dev_random.hpp"
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace libtorrent
|
namespace libtorrent
|
||||||
{
|
{
|
||||||
namespace aux
|
namespace aux
|
||||||
|
@ -78,27 +84,26 @@ namespace libtorrent
|
||||||
|
|
||||||
aux::crypt_gen_random(buffer);
|
aux::crypt_gen_random(buffer);
|
||||||
|
|
||||||
|
#elif TORRENT_USE_DEV_RANDOM
|
||||||
|
// /dev/random
|
||||||
|
|
||||||
|
static dev_random dev;
|
||||||
|
dev.read(buffer);
|
||||||
|
|
||||||
#elif defined TORRENT_USE_LIBCRYPTO
|
#elif defined TORRENT_USE_LIBCRYPTO
|
||||||
// openssl
|
// openssl
|
||||||
|
|
||||||
#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO
|
|
||||||
#pragma clang diagnostic push
|
|
||||||
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int r = RAND_bytes(reinterpret_cast<unsigned char*>(buffer.data())
|
int r = RAND_bytes(reinterpret_cast<unsigned char*>(buffer.data())
|
||||||
, int(buffer.size()));
|
, int(buffer.size()));
|
||||||
if (r != 1)
|
if (r != 1)
|
||||||
{
|
{
|
||||||
#ifndef BOOST_NO_EXCEPTIONS
|
#ifndef BOOST_NO_EXCEPTIONS
|
||||||
throw system_error(error_code(int(::ERR_get_error()), system_category()));
|
throw system_error(error_code(int(::ERR_get_error())
|
||||||
|
, boost::asio::error::get_ssl_category()));
|
||||||
#else
|
#else
|
||||||
std::terminate();
|
std::terminate();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO
|
|
||||||
#pragma clang diagnostic pop
|
|
||||||
#endif
|
|
||||||
#else
|
#else
|
||||||
// fallback
|
// fallback
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue