10b57cec5SDimitry Andric //===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file implements deterministic random number generation (RNG).
100b57cec5SDimitry Andric // The current implementation is NOT cryptographically secure as it uses
110b57cec5SDimitry Andric // the C++11 <random> facilities.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
140b57cec5SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/Support/RandomNumberGenerator.h"
160b57cec5SDimitry Andric 
170b57cec5SDimitry Andric #include "DebugOptions.h"
180b57cec5SDimitry Andric 
190b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
200b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
210b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
220b57cec5SDimitry Andric #ifdef _WIN32
230b57cec5SDimitry Andric #include "llvm/Support/Windows/WindowsSupport.h"
240b57cec5SDimitry Andric #else
250b57cec5SDimitry Andric #include "Unix/Unix.h"
260b57cec5SDimitry Andric #endif
270b57cec5SDimitry Andric 
280b57cec5SDimitry Andric using namespace llvm;
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric #define DEBUG_TYPE "rng"
310b57cec5SDimitry Andric namespace {
320b57cec5SDimitry Andric struct CreateSeed {
call__anon8ff815150111::CreateSeed330b57cec5SDimitry Andric   static void *call() {
340b57cec5SDimitry Andric     return new cl::opt<uint64_t>(
350b57cec5SDimitry Andric         "rng-seed", cl::value_desc("seed"), cl::Hidden,
360b57cec5SDimitry Andric         cl::desc("Seed for the random number generator"), cl::init(0));
370b57cec5SDimitry Andric   }
380b57cec5SDimitry Andric };
390b57cec5SDimitry Andric } // namespace
400b57cec5SDimitry Andric static ManagedStatic<cl::opt<uint64_t>, CreateSeed> Seed;
initRandomSeedOptions()410b57cec5SDimitry Andric void llvm::initRandomSeedOptions() { *Seed; }
420b57cec5SDimitry Andric 
RandomNumberGenerator(StringRef Salt)430b57cec5SDimitry Andric RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
440b57cec5SDimitry Andric   LLVM_DEBUG(if (*Seed == 0) dbgs()
450b57cec5SDimitry Andric              << "Warning! Using unseeded random number generator.\n");
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric   // Combine seed and salts using std::seed_seq.
480b57cec5SDimitry Andric   // Data: Seed-low, Seed-high, Salt
490b57cec5SDimitry Andric   // Note: std::seed_seq can only store 32-bit values, even though we
500b57cec5SDimitry Andric   // are using a 64-bit RNG. This isn't a problem since the Mersenne
510b57cec5SDimitry Andric   // twister constructor copies these correctly into its initial state.
520b57cec5SDimitry Andric   std::vector<uint32_t> Data;
530b57cec5SDimitry Andric   Data.resize(2 + Salt.size());
540b57cec5SDimitry Andric   Data[0] = *Seed;
550b57cec5SDimitry Andric   Data[1] = *Seed >> 32;
560b57cec5SDimitry Andric 
570b57cec5SDimitry Andric   llvm::copy(Salt, Data.begin() + 2);
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   std::seed_seq SeedSeq(Data.begin(), Data.end());
600b57cec5SDimitry Andric   Generator.seed(SeedSeq);
610b57cec5SDimitry Andric }
620b57cec5SDimitry Andric 
operator ()()630b57cec5SDimitry Andric RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
640b57cec5SDimitry Andric   return Generator();
650b57cec5SDimitry Andric }
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric // Get random vector of specified size
getRandomBytes(void * Buffer,size_t Size)680b57cec5SDimitry Andric std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
690b57cec5SDimitry Andric #ifdef _WIN32
700b57cec5SDimitry Andric   HCRYPTPROV hProvider;
710b57cec5SDimitry Andric   if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
720b57cec5SDimitry Andric                            CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
730b57cec5SDimitry Andric     ScopedCryptContext ScopedHandle(hProvider);
740b57cec5SDimitry Andric     if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
750b57cec5SDimitry Andric       return std::error_code();
760b57cec5SDimitry Andric   }
770b57cec5SDimitry Andric   return std::error_code(GetLastError(), std::system_category());
780b57cec5SDimitry Andric #else
790b57cec5SDimitry Andric   int Fd = open("/dev/urandom", O_RDONLY);
800b57cec5SDimitry Andric   if (Fd != -1) {
810b57cec5SDimitry Andric     std::error_code Ret;
820b57cec5SDimitry Andric     ssize_t BytesRead = read(Fd, Buffer, Size);
830b57cec5SDimitry Andric     if (BytesRead == -1)
840b57cec5SDimitry 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