15ca98fd9SDimitry Andric //===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
25ca98fd9SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65ca98fd9SDimitry Andric //
75ca98fd9SDimitry Andric //===----------------------------------------------------------------------===//
85ca98fd9SDimitry Andric //
967c32a98SDimitry Andric // This file implements deterministic random number generation (RNG).
105ca98fd9SDimitry Andric // The current implementation is NOT cryptographically secure as it uses
115ca98fd9SDimitry Andric // the C++11 <random> facilities.
125ca98fd9SDimitry Andric //
135ca98fd9SDimitry Andric //===----------------------------------------------------------------------===//
145ca98fd9SDimitry Andric
155a5ac124SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h"
16344a3780SDimitry Andric
17344a3780SDimitry Andric #include "DebugOptions.h"
18344a3780SDimitry Andric
195ca98fd9SDimitry Andric #include "llvm/Support/CommandLine.h"
205ca98fd9SDimitry Andric #include "llvm/Support/Debug.h"
21ac9a064cSDimitry Andric #include "llvm/Support/Error.h"
22ac9a064cSDimitry Andric #include "llvm/Support/ManagedStatic.h"
235a5ac124SDimitry Andric #include "llvm/Support/raw_ostream.h"
24eb11fae6SDimitry Andric #ifdef _WIN32
25cfca06d7SDimitry Andric #include "llvm/Support/Windows/WindowsSupport.h"
26b915e9e0SDimitry Andric #else
27b915e9e0SDimitry Andric #include "Unix/Unix.h"
28b915e9e0SDimitry Andric #endif
295ca98fd9SDimitry Andric
305ca98fd9SDimitry Andric using namespace llvm;
315ca98fd9SDimitry Andric
325a5ac124SDimitry Andric #define DEBUG_TYPE "rng"
33344a3780SDimitry Andric namespace {
34344a3780SDimitry Andric struct CreateSeed {
call__anon7bfe14020111::CreateSeed35344a3780SDimitry Andric static void *call() {
36344a3780SDimitry Andric return new cl::opt<uint64_t>(
37344a3780SDimitry Andric "rng-seed", cl::value_desc("seed"), cl::Hidden,
38344a3780SDimitry Andric cl::desc("Seed for the random number generator"), cl::init(0));
39344a3780SDimitry Andric }
40344a3780SDimitry Andric };
41344a3780SDimitry Andric } // namespace
42344a3780SDimitry Andric static ManagedStatic<cl::opt<uint64_t>, CreateSeed> Seed;
initRandomSeedOptions()43344a3780SDimitry Andric void llvm::initRandomSeedOptions() { *Seed; }
445ca98fd9SDimitry Andric
RandomNumberGenerator(StringRef Salt)455ca98fd9SDimitry Andric RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
46344a3780SDimitry Andric LLVM_DEBUG(if (*Seed == 0) dbgs()
47eb11fae6SDimitry Andric << "Warning! Using unseeded random number generator.\n");
485ca98fd9SDimitry Andric
4967c32a98SDimitry Andric // Combine seed and salts using std::seed_seq.
5067c32a98SDimitry Andric // Data: Seed-low, Seed-high, Salt
5167c32a98SDimitry Andric // Note: std::seed_seq can only store 32-bit values, even though we
5267c32a98SDimitry Andric // are using a 64-bit RNG. This isn't a problem since the Mersenne
5367c32a98SDimitry Andric // twister constructor copies these correctly into its initial state.
545ca98fd9SDimitry Andric std::vector<uint32_t> Data;
55b915e9e0SDimitry Andric Data.resize(2 + Salt.size());
56344a3780SDimitry Andric Data[0] = *Seed;
57344a3780SDimitry Andric Data[1] = *Seed >> 32;
585ca98fd9SDimitry Andric
59d8e91e46SDimitry Andric llvm::copy(Salt, Data.begin() + 2);
605ca98fd9SDimitry Andric
615ca98fd9SDimitry Andric std::seed_seq SeedSeq(Data.begin(), Data.end());
625ca98fd9SDimitry Andric Generator.seed(SeedSeq);
635ca98fd9SDimitry Andric }
645ca98fd9SDimitry Andric
operator ()()65b915e9e0SDimitry Andric RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
6667c32a98SDimitry Andric return Generator();
675ca98fd9SDimitry Andric }
68b915e9e0SDimitry Andric
69b915e9e0SDimitry Andric // Get random vector of specified size
getRandomBytes(void * Buffer,size_t Size)70b915e9e0SDimitry Andric std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
71eb11fae6SDimitry Andric #ifdef _WIN32
72b915e9e0SDimitry Andric HCRYPTPROV hProvider;
73b915e9e0SDimitry Andric if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
74b915e9e0SDimitry Andric CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
75b915e9e0SDimitry Andric ScopedCryptContext ScopedHandle(hProvider);
76b915e9e0SDimitry Andric if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
77b915e9e0SDimitry Andric return std::error_code();
78b915e9e0SDimitry Andric }
79b915e9e0SDimitry Andric return std::error_code(GetLastError(), std::system_category());
80b915e9e0SDimitry Andric #else
81b915e9e0SDimitry Andric int Fd = open("/dev/urandom", O_RDONLY);
82b915e9e0SDimitry Andric if (Fd != -1) {
83b915e9e0SDimitry Andric std::error_code Ret;
84b915e9e0SDimitry Andric ssize_t BytesRead = read(Fd, Buffer, Size);
85b915e9e0SDimitry Andric if (BytesRead == -1)
86ac9a064cSDimitry Andric Ret = errnoAsErrorCode();
87b915e9e0SDimitry Andric else if (BytesRead != static_cast<ssize_t>(Size))
88b915e9e0SDimitry Andric Ret = std::error_code(EIO, std::system_category());
89b915e9e0SDimitry Andric if (close(Fd) == -1)
90ac9a064cSDimitry Andric Ret = errnoAsErrorCode();
91b915e9e0SDimitry Andric
92b915e9e0SDimitry Andric return Ret;
93b915e9e0SDimitry Andric }
94ac9a064cSDimitry Andric return errnoAsErrorCode();
95b915e9e0SDimitry Andric #endif
96b915e9e0SDimitry Andric }
97