2014-06-25 23:21:42 +08:00
|
|
|
//===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-06-25 23:21:42 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-12-18 02:12:10 +08:00
|
|
|
// This file implements deterministic random number generation (RNG).
|
2014-06-25 23:21:42 +08:00
|
|
|
// The current implementation is NOT cryptographically secure as it uses
|
|
|
|
// the C++11 <random> facilities.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-24 02:19:41 +08:00
|
|
|
#include "llvm/Support/RandomNumberGenerator.h"
|
2014-06-25 23:21:42 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2015-03-24 02:19:41 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2018-04-29 08:45:03 +08:00
|
|
|
#ifdef _WIN32
|
2016-08-26 16:14:54 +08:00
|
|
|
#include "Windows/WindowsSupport.h"
|
|
|
|
#else
|
|
|
|
#include "Unix/Unix.h"
|
|
|
|
#endif
|
2014-06-25 23:21:42 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2015-03-24 02:19:41 +08:00
|
|
|
#define DEBUG_TYPE "rng"
|
|
|
|
|
2019-04-24 10:40:20 +08:00
|
|
|
static cl::opt<uint64_t> Seed("rng-seed", cl::value_desc("seed"), cl::Hidden,
|
|
|
|
cl::desc("Seed for the random number generator"),
|
|
|
|
cl::init(0));
|
2014-06-25 23:21:42 +08:00
|
|
|
|
|
|
|
RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
|
2018-05-14 20:53:11 +08:00
|
|
|
LLVM_DEBUG(if (Seed == 0) dbgs()
|
|
|
|
<< "Warning! Using unseeded random number generator.\n");
|
2014-06-25 23:21:42 +08:00
|
|
|
|
2014-12-18 02:12:10 +08: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-06-25 23:21:42 +08:00
|
|
|
std::vector<uint32_t> Data;
|
2016-10-12 02:17:26 +08:00
|
|
|
Data.resize(2 + Salt.size());
|
|
|
|
Data[0] = Seed;
|
|
|
|
Data[1] = Seed >> 32;
|
2014-06-25 23:21:42 +08:00
|
|
|
|
2018-11-17 09:44:25 +08:00
|
|
|
llvm::copy(Salt, Data.begin() + 2);
|
2014-06-25 23:21:42 +08:00
|
|
|
|
|
|
|
std::seed_seq SeedSeq(Data.begin(), Data.end());
|
|
|
|
Generator.seed(SeedSeq);
|
|
|
|
}
|
|
|
|
|
2016-10-11 15:13:01 +08:00
|
|
|
RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
|
2014-12-18 02:12:10 +08:00
|
|
|
return Generator();
|
2014-06-25 23:21:42 +08:00
|
|
|
}
|
2016-08-26 16:14:54 +08:00
|
|
|
|
|
|
|
// Get random vector of specified size
|
|
|
|
std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
|
2018-04-29 08:45:03 +08:00
|
|
|
#ifdef _WIN32
|
2016-08-26 16:14:54 +08:00
|
|
|
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
|
|
|
|
}
|