xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/RandomNumberGenerator.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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"
16fe6060f1SDimitry Andric 
17fe6060f1SDimitry Andric #include "DebugOptions.h"
18fe6060f1SDimitry Andric 
190b57cec5SDimitry Andric #include "llvm/Support/CommandLine.h"
200b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
21*0fca6ea1SDimitry Andric #include "llvm/Support/Error.h"
22*0fca6ea1SDimitry Andric #include "llvm/Support/ManagedStatic.h"
230b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
240b57cec5SDimitry Andric #ifdef _WIN32
258c27c554SDimitry Andric #include "llvm/Support/Windows/WindowsSupport.h"
260b57cec5SDimitry Andric #else
270b57cec5SDimitry Andric #include "Unix/Unix.h"
280b57cec5SDimitry Andric #endif
290b57cec5SDimitry Andric 
300b57cec5SDimitry Andric using namespace llvm;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric #define DEBUG_TYPE "rng"
33fe6060f1SDimitry Andric namespace {
34fe6060f1SDimitry Andric struct CreateSeed {
35fe6060f1SDimitry Andric   static void *call() {
36fe6060f1SDimitry Andric     return new cl::opt<uint64_t>(
37fe6060f1SDimitry Andric         "rng-seed", cl::value_desc("seed"), cl::Hidden,
38fe6060f1SDimitry Andric         cl::desc("Seed for the random number generator"), cl::init(0));
39fe6060f1SDimitry Andric   }
40fe6060f1SDimitry Andric };
41fe6060f1SDimitry Andric } // namespace
42fe6060f1SDimitry Andric static ManagedStatic<cl::opt<uint64_t>, CreateSeed> Seed;
43fe6060f1SDimitry Andric void llvm::initRandomSeedOptions() { *Seed; }
440b57cec5SDimitry Andric 
450b57cec5SDimitry Andric RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
46fe6060f1SDimitry Andric   LLVM_DEBUG(if (*Seed == 0) dbgs()
470b57cec5SDimitry Andric              << "Warning! Using unseeded random number generator.\n");
480b57cec5SDimitry Andric 
490b57cec5SDimitry Andric   // Combine seed and salts using std::seed_seq.
500b57cec5SDimitry Andric   // Data: Seed-low, Seed-high, Salt
510b57cec5SDimitry Andric   // Note: std::seed_seq can only store 32-bit values, even though we
520b57cec5SDimitry Andric   // are using a 64-bit RNG. This isn't a problem since the Mersenne
530b57cec5SDimitry Andric   // twister constructor copies these correctly into its initial state.
540b57cec5SDimitry Andric   std::vector<uint32_t> Data;
550b57cec5SDimitry Andric   Data.resize(2 + Salt.size());
56fe6060f1SDimitry Andric   Data[0] = *Seed;
57fe6060f1SDimitry Andric   Data[1] = *Seed >> 32;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   llvm::copy(Salt, Data.begin() + 2);
600b57cec5SDimitry Andric 
610b57cec5SDimitry Andric   std::seed_seq SeedSeq(Data.begin(), Data.end());
620b57cec5SDimitry Andric   Generator.seed(SeedSeq);
630b57cec5SDimitry Andric }
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric RandomNumberGenerator::result_type RandomNumberGenerator::operator()() {
660b57cec5SDimitry Andric   return Generator();
670b57cec5SDimitry Andric }
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric // Get random vector of specified size
700b57cec5SDimitry Andric std::error_code llvm::getRandomBytes(void *Buffer, size_t Size) {
710b57cec5SDimitry Andric #ifdef _WIN32
720b57cec5SDimitry Andric   HCRYPTPROV hProvider;
730b57cec5SDimitry Andric   if (CryptAcquireContext(&hProvider, 0, 0, PROV_RSA_FULL,
740b57cec5SDimitry Andric                            CRYPT_VERIFYCONTEXT | CRYPT_SILENT)) {
750b57cec5SDimitry Andric     ScopedCryptContext ScopedHandle(hProvider);
760b57cec5SDimitry Andric     if (CryptGenRandom(hProvider, Size, static_cast<BYTE *>(Buffer)))
770b57cec5SDimitry Andric       return std::error_code();
780b57cec5SDimitry Andric   }
790b57cec5SDimitry Andric   return std::error_code(GetLastError(), std::system_category());
800b57cec5SDimitry Andric #else
810b57cec5SDimitry Andric   int Fd = open("/dev/urandom", O_RDONLY);
820b57cec5SDimitry Andric   if (Fd != -1) {
830b57cec5SDimitry Andric     std::error_code Ret;
840b57cec5SDimitry Andric     ssize_t BytesRead = read(Fd, Buffer, Size);
850b57cec5SDimitry Andric     if (BytesRead == -1)
86*0fca6ea1SDimitry Andric       Ret = errnoAsErrorCode();
870b57cec5SDimitry Andric     else if (BytesRead != static_cast<ssize_t>(Size))
880b57cec5SDimitry Andric       Ret = std::error_code(EIO, std::system_category());
890b57cec5SDimitry Andric     if (close(Fd) == -1)
90*0fca6ea1SDimitry Andric       Ret = errnoAsErrorCode();
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric     return Ret;
930b57cec5SDimitry Andric   }
94*0fca6ea1SDimitry Andric   return errnoAsErrorCode();
950b57cec5SDimitry Andric #endif
960b57cec5SDimitry Andric }
97