1*0b57cec5SDimitry Andric //===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric //
9*0b57cec5SDimitry Andric // This file implements deterministic random number generation (RNG).
10*0b57cec5SDimitry Andric // The current implementation is NOT cryptographically secure as it uses
11*0b57cec5SDimitry Andric // the C++11 <random> facilities.
12*0b57cec5SDimitry Andric //
13*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
14*0b57cec5SDimitry Andric
15*0b57cec5SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h"
16*0b57cec5SDimitry Andric
17*0b57cec5SDimitry Andric #include "DebugOptions.h"
18*0b57cec5SDimitry Andric
19*0b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
20*0b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
21*0b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
22*0b57cec5SDimitry Andric #ifdef _WIN32
23*0b57cec5SDimitry Andric #include "llvm/Support/Windows/WindowsSupport.h"
24*0b57cec5SDimitry Andric #else
25*0b57cec5SDimitry Andric #include "Unix/Unix.h"
26*0b57cec5SDimitry Andric #endif
27*0b57cec5SDimitry Andric
28*0b57cec5SDimitry Andric using namespace llvm;
29*0b57cec5SDimitry Andric
30*0b57cec5SDimitry Andric #define DEBUG_TYPE "rng"
31*0b57cec5SDimitry Andric namespace {
32*0b57cec5SDimitry Andric struct CreateSeed {
call__anon2f3f8dc30111::CreateSeed33*0b57cec5SDimitry Andric static void *call() {
34*0b57cec5SDimitry Andric return new cl::opt<uint64_t>(
35*0b57cec5SDimitry Andric "rng-seed", cl::value_desc("seed"), cl::Hidden,
36*0b57cec5SDimitry Andric cl::desc("Seed for the random number generator"), cl::init(0));
37*0b57cec5SDimitry Andric }
38*0b57cec5SDimitry Andric };
39*0b57cec5SDimitry Andric } // namespace
40*0b57cec5SDimitry Andric static ManagedStatic<cl::opt<uint64_t>, CreateSeed> Seed;
initRandomSeedOptions()41*0b57cec5SDimitry Andric void llvm::initRandomSeedOptions() { *Seed; }
42*0b57cec5SDimitry Andric
RandomNumberGenerator(StringRef Salt)43*0b57cec5SDimitry Andric RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
44*0b57cec5SDimitry Andric LLVM_DEBUG(if (*Seed == 0) dbgs()
45*0b57cec5SDimitry Andric << "Warning! Using unseeded random number generator.\n");
46*0b57cec5SDimitry Andric
47*0b57cec5SDimitry Andric // Combine seed and salts using std::seed_seq.
48*0b57cec5SDimitry Andric // Data: Seed-low, Seed-high, Salt
49*0b57cec5SDimitry Andric // Note: std::seed_seq can only store 32-bit values, even though we
50*0b57cec5SDimitry Andric // are using a 64-bit RNG. This isn't a problem since the Mersenne
51*0b57cec5SDimitry Andric // twister constructor copies these correctly into its initial state.
52*0b57cec5SDimitry Andric std::vector<uint32_t> Data;
53*0b57cec5SDimitry Andric Data.resize(2 + Salt.size());
54*0b57cec5SDimitry Andric Data[0] = *Seed;
55*0b57cec5SDimitry Andric Data[1] = *Seed >> 32;
56*0b57cec5SDimitry Andric
57*0b57cec5SDimitry Andric llvm::copy(Salt, Data.begin() + 2);
58*0b57cec5SDimitry Andric
59*0b57cec5SDimitry Andric std::seed_seq SeedSeq(Data.begin(), Data.end());
60*0b57cec5SDimitry Andric Generator.seed(SeedSeq);
61*0b57cec5SDimitry Andric }
62*0b57cec5SDimitry Andric
operator ()()63*0b57cec5SDimitry Andric RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
64*0b57cec5SDimitry Andric return Generator();
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric
67*0b57cec5SDimitry Andric // Get random vector of specified size
getRandomBytes(void * Buffer,size_t Size)68*0b57cec5SDimitry Andric std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
69*0b57cec5SDimitry Andric #ifdef _WIN32
70*0b57cec5SDimitry Andric HCRYPTPROV hProvider;
71*0b57cec5SDimitry Andric if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
72*0b57cec5SDimitry Andric CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
73*0b57cec5SDimitry Andric ScopedCryptContext ScopedHandle(hProvider);
74*0b57cec5SDimitry Andric if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
75*0b57cec5SDimitry Andric return std::error_code();
76*0b57cec5SDimitry Andric }
77*0b57cec5SDimitry Andric return std::error_code(GetLastError(), std::system_category());
78*0b57cec5SDimitry Andric #else
79*0b57cec5SDimitry Andric int Fd = open("/dev/urandom", O_RDONLY);
80*0b57cec5SDimitry Andric if (Fd != -1) {
81*0b57cec5SDimitry Andric std::error_code Ret;
82*0b57cec5SDimitry Andric ssize_t BytesRead = read(Fd, Buffer, Size);
83*0b57cec5SDimitry Andric if (BytesRead == -1)
84*0b57cec5SDimitry Andric Ret = std::error_code(errno, std::system_category());
85 else if (BytesRead != static_cast<ssize_t>(Size))
86 Ret = std::error_code(EIO, std::system_category());
87 if (close(Fd) == -1)
88 Ret = std::error_code(errno, std::system_category());
89
90 return Ret;
91 }
92 return std::error_code(errno, std::system_category());
93 #endif
94 }
95