191bc56edSDimitry Andric //===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
291bc56edSDimitry Andric //
391bc56edSDimitry Andric // The LLVM Compiler Infrastructure
491bc56edSDimitry Andric //
591bc56edSDimitry Andric // This file is distributed under the University of Illinois Open Source
691bc56edSDimitry Andric // License. See LICENSE.TXT for details.
791bc56edSDimitry Andric //
891bc56edSDimitry Andric //===----------------------------------------------------------------------===//
991bc56edSDimitry Andric //
1039d628a0SDimitry Andric // This file implements deterministic random number generation (RNG).
1191bc56edSDimitry Andric // The current implementation is NOT cryptographically secure as it uses
1291bc56edSDimitry Andric // the C++11 <random> facilities.
1391bc56edSDimitry Andric //
1491bc56edSDimitry Andric //===----------------------------------------------------------------------===//
1591bc56edSDimitry Andric
16ff0cc061SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h"
1791bc56edSDimitry Andric #include "llvm/Support/CommandLine.h"
1891bc56edSDimitry Andric #include "llvm/Support/Debug.h"
19ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h"
204ba319b5SDimitry Andric #ifdef _WIN32
21d88c1a5aSDimitry Andric #include "Windows/WindowsSupport.h"
22d88c1a5aSDimitry Andric #else
23d88c1a5aSDimitry Andric #include "Unix/Unix.h"
24d88c1a5aSDimitry Andric #endif
2591bc56edSDimitry Andric
2691bc56edSDimitry Andric using namespace llvm;
2791bc56edSDimitry Andric
28ff0cc061SDimitry Andric #define DEBUG_TYPE "rng"
29ff0cc061SDimitry Andric
3091bc56edSDimitry Andric // Tracking BUG: 19665
3191bc56edSDimitry Andric // http://llvm.org/bugs/show_bug.cgi?id=19665
3291bc56edSDimitry Andric //
3391bc56edSDimitry Andric // Do not change to cl::opt<uint64_t> since this silently breaks argument parsing.
3491bc56edSDimitry Andric static cl::opt<unsigned long long>
352cab237bSDimitry Andric Seed("rng-seed", cl::value_desc("seed"), cl::Hidden,
3691bc56edSDimitry Andric cl::desc("Seed for the random number generator"), cl::init(0));
3791bc56edSDimitry Andric
RandomNumberGenerator(StringRef Salt)3891bc56edSDimitry Andric RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
394ba319b5SDimitry Andric LLVM_DEBUG(if (Seed == 0) dbgs()
404ba319b5SDimitry Andric << "Warning! Using unseeded random number generator.\n");
4191bc56edSDimitry Andric
4239d628a0SDimitry Andric // Combine seed and salts using std::seed_seq.
4339d628a0SDimitry Andric // Data: Seed-low, Seed-high, Salt
4439d628a0SDimitry Andric // Note: std::seed_seq can only store 32-bit values, even though we
4539d628a0SDimitry Andric // are using a 64-bit RNG. This isn't a problem since the Mersenne
4639d628a0SDimitry Andric // twister constructor copies these correctly into its initial state.
4791bc56edSDimitry Andric std::vector<uint32_t> Data;
48d88c1a5aSDimitry Andric Data.resize(2 + Salt.size());
49d88c1a5aSDimitry Andric Data[0] = Seed;
50d88c1a5aSDimitry Andric Data[1] = Seed >> 32;
5191bc56edSDimitry Andric
52*b5893f02SDimitry Andric llvm::copy(Salt, Data.begin() + 2);
5391bc56edSDimitry Andric
5491bc56edSDimitry Andric std::seed_seq SeedSeq(Data.begin(), Data.end());
5591bc56edSDimitry Andric Generator.seed(SeedSeq);
5691bc56edSDimitry Andric }
5791bc56edSDimitry Andric
operator ()()58d88c1a5aSDimitry Andric RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
5939d628a0SDimitry Andric return Generator();
6091bc56edSDimitry Andric }
61d88c1a5aSDimitry Andric
62d88c1a5aSDimitry Andric // Get random vector of specified size
getRandomBytes(void * Buffer,size_t Size)63d88c1a5aSDimitry Andric std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
644ba319b5SDimitry Andric #ifdef _WIN32
65d88c1a5aSDimitry Andric HCRYPTPROV hProvider;
66d88c1a5aSDimitry Andric if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
67d88c1a5aSDimitry Andric CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
68d88c1a5aSDimitry Andric ScopedCryptContext ScopedHandle(hProvider);
69d88c1a5aSDimitry Andric if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
70d88c1a5aSDimitry Andric return std::error_code();
71d88c1a5aSDimitry Andric }
72d88c1a5aSDimitry Andric return std::error_code(GetLastError(), std::system_category());
73d88c1a5aSDimitry Andric #else
74d88c1a5aSDimitry Andric int Fd = open("/dev/urandom", O_RDONLY);
75d88c1a5aSDimitry Andric if (Fd != -1) {
76d88c1a5aSDimitry Andric std::error_code Ret;
77d88c1a5aSDimitry Andric ssize_t BytesRead = read(Fd, Buffer, Size);
78d88c1a5aSDimitry Andric if (BytesRead == -1)
79d88c1a5aSDimitry Andric Ret = std::error_code(errno, std::system_category());
80d88c1a5aSDimitry Andric else if (BytesRead != static_cast<ssize_t>(Size))
81d88c1a5aSDimitry Andric Ret = std::error_code(EIO, std::system_category());
82d88c1a5aSDimitry Andric if (close(Fd) == -1)
83d88c1a5aSDimitry Andric Ret = std::error_code(errno, std::system_category());
84d88c1a5aSDimitry Andric
85d88c1a5aSDimitry Andric return Ret;
86d88c1a5aSDimitry Andric }
87d88c1a5aSDimitry Andric return std::error_code(errno, std::system_category());
88d88c1a5aSDimitry Andric #endif
89d88c1a5aSDimitry Andric }
90