#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/rand_util.h"
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/syscall.h>
#include <sys/utsname.h>
#include <unistd.h>
#include "base/check.h"
#include "base/compiler_specific.h"
#include "base/containers/span.h"
#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/metrics/histogram_macros.h"
#include "base/no_destructor.h"
#include "base/posix/eintr_wrapper.h"
#include "base/time/time.h"
#include "build/build_config.h"
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && !BUILDFLAG(IS_NACL)
#include "third_party/lss/linux_syscall_support.h"
#elif BUILDFLAG(IS_MAC)
#include <sys/random.h>
#endif
#if !BUILDFLAG(IS_NACL)
#include "third_party/boringssl/src/include/openssl/rand.h"
#endif
namespace base {
namespace {
#if BUILDFLAG(IS_AIX)
static constexpr int kOpenFlags = O_RDONLY;
#else
static constexpr int kOpenFlags = …;
#endif
class URandomFd { … };
#if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_ANDROID)) && \
!BUILDFLAG(IS_NACL)
void KernelVersionNumbers(int32_t* major_version,
int32_t* minor_version,
int32_t* bugfix_version) { … }
bool KernelSupportsGetRandom() { … }
bool GetRandomSyscall(void* output, size_t output_length) { … }
#endif
#if BUILDFLAG(IS_ANDROID)
std::atomic<bool> g_use_getrandom;
BASE_FEATURE(kUseGetrandomForRandBytes,
"UseGetrandomForRandBytes",
FEATURE_ENABLED_BY_DEFAULT);
bool UseGetrandom() {
return g_use_getrandom.load(std::memory_order_relaxed);
}
#elif (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && !BUILDFLAG(IS_NACL)
bool UseGetrandom() { … }
#endif
}
namespace internal {
#if BUILDFLAG(IS_ANDROID)
void ConfigureRandBytesFieldTrial() {
g_use_getrandom.store(FeatureList::IsEnabled(kUseGetrandomForRandBytes),
std::memory_order_relaxed);
}
#endif
namespace {
#if !BUILDFLAG(IS_NACL)
std::atomic<bool> g_use_boringssl;
BASE_FEATURE(…);
}
void ConfigureBoringSSLBackedRandBytesFieldTrial() { … }
bool UseBoringSSLForRandBytes() { … }
#endif
}
namespace {
void RandBytesInternal(span<uint8_t> output, bool avoid_allocation) { … }
}
namespace internal {
double RandDoubleAvoidAllocation() { … }
}
void RandBytes(span<uint8_t> output) { … }
int GetUrandomFD() { … }
}