X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FRandom.cpp;h=8f5e3128d2036952f31ba8efcc73791a139e333b;hb=3c2fad7c4e858b8bfa4018c4524a2af1a2c8156f;hp=981096214504417e9ec4710e306aeef7eff28386;hpb=68a546c7cdd7df99b436a0d26c2ef7bb766fe435;p=folly.git diff --git a/folly/Random.cpp b/folly/Random.cpp index 98109621..8f5e3128 100644 --- a/folly/Random.cpp +++ b/folly/Random.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2012 Facebook, Inc. + * Copyright 2016 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,29 +14,132 @@ * limitations under the License. */ -#include "folly/Random.h" +#include #include -#include -#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#ifdef _MSC_VER +# include +#endif namespace folly { namespace { -std::atomic seedInput(0); + +void readRandomDevice(void* data, size_t size) { +#ifdef _MSC_VER + static folly::once_flag flag; + static HCRYPTPROV cryptoProv; + folly::call_once(flag, [&] { + if (!CryptAcquireContext(&cryptoProv, nullptr, nullptr, PROV_RSA_FULL, 0)) { + if (GetLastError() == NTE_BAD_KEYSET) { + // Mostly likely cause of this is that no key container + // exists yet, so try to create one. + PCHECK(CryptAcquireContext( + &cryptoProv, nullptr, nullptr, PROV_RSA_FULL, CRYPT_NEWKEYSET)); + } else { + LOG(FATAL) << "Failed to acquire the default crypto context."; + } + } + }); + CHECK(size <= std::numeric_limits::max()); + PCHECK(CryptGenRandom(cryptoProv, (DWORD)size, (BYTE*)data)); +#else + // Keep the random device open for the duration of the program. + static int randomFd = ::open("/dev/urandom", O_RDONLY); + PCHECK(randomFd >= 0); + auto bytesRead = readFull(randomFd, data, size); + PCHECK(bytesRead >= 0 && size_t(bytesRead) == size); +#endif +} + +class BufferedRandomDevice { + public: + static constexpr size_t kDefaultBufferSize = 128; + + explicit BufferedRandomDevice(size_t bufferSize = kDefaultBufferSize); + + void get(void* data, size_t size) { + if (LIKELY(size <= remaining())) { + memcpy(data, ptr_, size); + ptr_ += size; + } else { + getSlow(static_cast(data), size); + } + } + + private: + void getSlow(unsigned char* data, size_t size); + + inline size_t remaining() const { + return buffer_.get() + bufferSize_ - ptr_; + } + + const size_t bufferSize_; + std::unique_ptr buffer_; + unsigned char* ptr_; +}; + +BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize) + : bufferSize_(bufferSize), + buffer_(new unsigned char[bufferSize]), + ptr_(buffer_.get() + bufferSize) { // refill on first use +} + +void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) { + DCHECK_GT(size, remaining()); + if (size >= bufferSize_) { + // Just read directly. + readRandomDevice(data, size); + return; + } + + size_t copied = remaining(); + memcpy(data, ptr_, copied); + data += copied; + size -= copied; + + // refill + readRandomDevice(buffer_.get(), bufferSize_); + ptr_ = buffer_.get(); + + memcpy(data, ptr_, size); + ptr_ += size; +} + + +} // namespace + +void Random::secureRandom(void* data, size_t size) { + static ThreadLocal bufferedRandomDevice; + bufferedRandomDevice->get(data, size); +} + +class ThreadLocalPRNG::LocalInstancePRNG { + public: + LocalInstancePRNG() : rng(Random::create()) { } + + Random::DefaultGenerator rng; +}; + +ThreadLocalPRNG::ThreadLocalPRNG() { + static folly::ThreadLocal localInstance; + local_ = localInstance.get(); } -uint32_t randomNumberSeed() { - struct timeval tv; - gettimeofday(&tv, NULL); - const uint32_t kPrime0 = 51551; - const uint32_t kPrime1 = 61631; - const uint32_t kPrime2 = 64997; - const uint32_t kPrime3 = 111857; - return kPrime0 * (seedInput++) - + kPrime1 * static_cast(getpid()) - + kPrime2 * static_cast(tv.tv_sec) - + kPrime3 * static_cast(tv.tv_usec); +uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) { + return local->rng(); } }