157 lines · cpp
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include <__config>10 11#if defined(_LIBCPP_USING_WIN32_RANDOM)12// Must be defined before including stdlib.h to enable rand_s().13# define _CRT_RAND_S14#endif // defined(_LIBCPP_USING_WIN32_RANDOM)15 16#include <__system_error/throw_system_error.h>17#include <limits>18#include <random>19#include <string>20 21#include <errno.h>22#include <stdio.h>23#include <stdlib.h>24 25#if defined(_LIBCPP_USING_GETENTROPY)26# include <sys/random.h>27#elif defined(_LIBCPP_USING_DEV_RANDOM)28# include <fcntl.h>29# include <unistd.h>30# if __has_include(<sys/ioctl.h>) && __has_include(<linux/random.h>)31# include <linux/random.h>32# include <sys/ioctl.h>33# endif34#elif defined(_LIBCPP_USING_FUCHSIA_CPRNG)35# include <zircon/syscalls.h>36#endif37 38_LIBCPP_BEGIN_NAMESPACE_STD39 40#if defined(_LIBCPP_USING_GETENTROPY)41 42random_device::random_device(const string& __token) {43 if (__token != "/dev/urandom")44 std::__throw_system_error(ENOENT, ("random device not supported " + __token).c_str());45}46 47random_device::~random_device() {}48 49unsigned random_device::operator()() {50 unsigned r;51 size_t n = sizeof(r);52 int err = getentropy(&r, n);53 if (err)54 std::__throw_system_error(errno, "random_device getentropy failed");55 return r;56}57 58#elif defined(_LIBCPP_USING_ARC4_RANDOM)59 60random_device::random_device(const string&) {}61 62random_device::~random_device() {}63 64unsigned random_device::operator()() { return arc4random(); }65 66#elif defined(_LIBCPP_USING_DEV_RANDOM)67 68random_device::random_device(const string& __token) : __f_(open(__token.c_str(), O_RDONLY)) {69 if (__f_ < 0)70 std::__throw_system_error(errno, ("random_device failed to open " + __token).c_str());71}72 73random_device::~random_device() { close(__f_); }74 75unsigned random_device::operator()() {76 unsigned r;77 size_t n = sizeof(r);78 char* p = reinterpret_cast<char*>(&r);79 while (n > 0) {80 ssize_t s = read(__f_, p, n);81 if (s == 0)82 std::__throw_system_error(ENOMSG, "random_device got EOF");83 if (s == -1) {84 if (errno != EINTR)85 std::__throw_system_error(errno, "random_device got an unexpected error");86 continue;87 }88 n -= static_cast<size_t>(s);89 p += static_cast<size_t>(s);90 }91 return r;92}93 94#elif defined(_LIBCPP_USING_WIN32_RANDOM)95 96random_device::random_device(const string& __token) {97 if (__token != "/dev/urandom")98 std::__throw_system_error(ENOENT, ("random device not supported " + __token).c_str());99}100 101random_device::~random_device() {}102 103unsigned random_device::operator()() {104 unsigned r;105 errno_t err = rand_s(&r);106 if (err)107 std::__throw_system_error(err, "random_device rand_s failed.");108 return r;109}110 111#elif defined(_LIBCPP_USING_FUCHSIA_CPRNG)112 113random_device::random_device(const string& __token) {114 if (__token != "/dev/urandom")115 std::__throw_system_error(ENOENT, ("random device not supported " + __token).c_str());116}117 118random_device::~random_device() {}119 120unsigned random_device::operator()() {121 // Implicitly link against the vDSO system call ABI without122 // requiring the final link to specify -lzircon explicitly when123 // statically linking libc++.124# pragma comment(lib, "zircon")125 126 // The system call cannot fail. It returns only when the bits are ready.127 unsigned r;128 _zx_cprng_draw(&r, sizeof(r));129 return r;130}131 132#else133# error "Random device not implemented for this architecture"134#endif135 136double random_device::entropy() const noexcept {137#if defined(_LIBCPP_USING_DEV_RANDOM) && defined(RNDGETENTCNT)138 int ent;139 if (::ioctl(__f_, RNDGETENTCNT, &ent) < 0)140 return 0;141 142 if (ent < 0)143 return 0;144 145 if (ent > std::numeric_limits<result_type>::digits)146 return std::numeric_limits<result_type>::digits;147 148 return ent;149#elif defined(_LIBCPP_USING_ARC4_RANDOM) || defined(_LIBCPP_USING_FUCHSIA_CPRNG)150 return std::numeric_limits<result_type>::digits;151#else152 return 0;153#endif154}155 156_LIBCPP_END_NAMESPACE_STD157