2014-11-24 09:08:18 +00:00
|
|
|
//===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2015-01-18 16:17:27 +00:00
|
|
|
// This file implements deterministic random number generation (RNG).
|
2014-11-24 09:08:18 +00:00
|
|
|
// The current implementation is NOT cryptographically secure as it uses
|
|
|
|
// the C++11 <random> facilities.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
#include "llvm/Support/RandomNumberGenerator.h"
|
2014-11-24 09:08:18 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-05-27 18:44:32 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-01-02 19:17:04 +00:00
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
#include "Windows/WindowsSupport.h"
|
|
|
|
#else
|
|
|
|
#include "Unix/Unix.h"
|
|
|
|
#endif
|
2014-11-24 09:08:18 +00:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-05-27 18:44:32 +00:00
|
|
|
#define DEBUG_TYPE "rng"
|
|
|
|
|
2014-11-24 09:08:18 +00:00
|
|
|
// Tracking BUG: 19665
|
|
|
|
// http://llvm.org/bugs/show_bug.cgi?id=19665
|
|
|
|
//
|
|
|
|
// Do not change to cl::opt<uint64_t> since this silently breaks argument parsing.
|
|
|
|
static cl::opt<unsigned long long>
|
|
|
|
Seed("rng-seed", cl::value_desc("seed"),
|
|
|
|
cl::desc("Seed for the random number generator"), cl::init(0));
|
|
|
|
|
|
|
|
RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
|
|
|
|
DEBUG(
|
|
|
|
if (Seed == 0)
|
2015-01-18 16:17:27 +00:00
|
|
|
dbgs() << "Warning! Using unseeded random number generator.\n"
|
2014-11-24 09:08:18 +00:00
|
|
|
);
|
|
|
|
|
2015-01-18 16:17:27 +00:00
|
|
|
// Combine seed and salts using std::seed_seq.
|
|
|
|
// Data: Seed-low, Seed-high, Salt
|
|
|
|
// Note: std::seed_seq can only store 32-bit values, even though we
|
|
|
|
// are using a 64-bit RNG. This isn't a problem since the Mersenne
|
|
|
|
// twister constructor copies these correctly into its initial state.
|
2014-11-24 09:08:18 +00:00
|
|
|
std::vector<uint32_t> Data;
|
2017-01-02 19:17:04 +00:00
|
|
|
Data.resize(2 + Salt.size());
|
|
|
|
Data[0] = Seed;
|
|
|
|
Data[1] = Seed >> 32;
|
2014-11-24 09:08:18 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
std::copy(Salt.begin(), Salt.end(), Data.begin() + 2);
|
2014-11-24 09:08:18 +00:00
|
|
|
|
|
|
|
std::seed_seq SeedSeq(Data.begin(), Data.end());
|
|
|
|
Generator.seed(SeedSeq);
|
|
|
|
}
|
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
|
2015-01-18 16:17:27 +00:00
|
|
|
return Generator();
|
2014-11-24 09:08:18 +00:00
|
|
|
}
|
2017-01-02 19:17:04 +00:00
|
|
|
|
|
|
|
// Get random vector of specified size
|
|
|
|
std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
|
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
HCRYPTPROV hProvider;
|
|
|
|
if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
|
|
|
|
CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
|
|
|
|
ScopedCryptContext ScopedHandle(hProvider);
|
|
|
|
if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
|
|
|
|
return std::error_code();
|
|
|
|
}
|
|
|
|
return std::error_code(GetLastError(), std::system_category());
|
|
|
|
#else
|
|
|
|
int Fd = open("/dev/urandom", O_RDONLY);
|
|
|
|
if (Fd != -1) {
|
|
|
|
std::error_code Ret;
|
|
|
|
ssize_t BytesRead = read(Fd, Buffer, Size);
|
|
|
|
if (BytesRead == -1)
|
|
|
|
Ret = std::error_code(errno, std::system_category());
|
|
|
|
else if (BytesRead != static_cast<ssize_t>(Size))
|
|
|
|
Ret = std::error_code(EIO, std::system_category());
|
|
|
|
if (close(Fd) == -1)
|
|
|
|
Ret = std::error_code(errno, std::system_category());
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
return std::error_code(errno, std::system_category());
|
|
|
|
#endif
|
|
|
|
}
|