From 0c189e08b5bd066146c07c918831f5004aab05f1 Mon Sep 17 00:00:00 2001 From: arvidn Date: Sat, 14 Jan 2017 18:13:09 -0500 Subject: [PATCH] use /dev/random as random number source on macOS --- include/libtorrent/Makefile.am | 1 + include/libtorrent/aux_/dev_random.hpp | 80 ++++++++++++++++++++++++++ include/libtorrent/config.hpp | 5 ++ src/random.cpp | 23 +++++--- 4 files changed, 100 insertions(+), 9 deletions(-) create mode 100644 include/libtorrent/aux_/dev_random.hpp diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index bed4b50ac..aad56caa4 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -163,6 +163,7 @@ nobase_include_HEADERS = \ aux_/cpuid.hpp \ aux_/disable_warnings_push.hpp \ aux_/disable_warnings_pop.hpp \ + aux_/dev_random.hpp \ aux_/escape_string.hpp \ aux_/io.hpp \ aux_/max_path.hpp \ diff --git a/include/libtorrent/aux_/dev_random.hpp b/include/libtorrent/aux_/dev_random.hpp new file mode 100644 index 000000000..b8ea3c9e8 --- /dev/null +++ b/include/libtorrent/aux_/dev_random.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 + +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 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 + diff --git a/include/libtorrent/config.hpp b/include/libtorrent/config.hpp index b81c3fa3e..ecac833a3 100644 --- a/include/libtorrent/config.hpp +++ b/include/libtorrent/config.hpp @@ -153,6 +153,7 @@ POSSIBILITY OF SUCH DAMAGE. #endif #endif // __APPLE__ +#define TORRENT_USE_DEV_RANDOM 1 #define TORRENT_HAVE_MMAP 1 #define TORRENT_HAS_FALLOCATE 0 @@ -413,6 +414,10 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_USE_CRYPTOAPI_SHA_512 0 #endif +#ifndef TORRENT_USE_DEV_RANDOM +#define TORRENT_USE_DEV_RANDOM 0 +#endif + #ifndef TORRENT_HAVE_MMAP #define TORRENT_HAVE_MMAP 0 #endif diff --git a/src/random.cpp b/src/random.cpp index 5a9d7a6be..55a4aed6d 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -46,10 +46,16 @@ extern "C" { #include } +#include + #endif #include "libtorrent/aux_/disable_warnings_pop.hpp" +#if TORRENT_USE_DEV_RANDOM +#include "libtorrent/aux_/dev_random.hpp" +#endif + namespace libtorrent { namespace aux @@ -78,27 +84,26 @@ namespace libtorrent aux::crypt_gen_random(buffer); +#elif TORRENT_USE_DEV_RANDOM + // /dev/random + + static dev_random dev; + dev.read(buffer); + #elif defined TORRENT_USE_LIBCRYPTO // openssl -#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wdeprecated-declarations" -#endif - int r = RAND_bytes(reinterpret_cast(buffer.data()) , int(buffer.size())); if (r != 1) { #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 std::terminate(); #endif } -#ifdef TORRENT_MACOS_DEPRECATED_LIBCRYPTO -#pragma clang diagnostic pop -#endif #else // fallback