X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FRandom.cpp;h=9f85de19e81a49a537dfdab1bf89f041127cd788;hb=HEAD;hp=a189c3fc6789a2269f1a16fabd51a3995a129244;hpb=ce64f0f685111ac24c7a321ea56d0c3524621df1;p=folly.git diff --git a/folly/Random.cpp b/folly/Random.cpp index a189c3fc..9f85de19 100644 --- a/folly/Random.cpp +++ b/folly/Random.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2011-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,25 +16,58 @@ #include +#include #include -#include -#include +#include #include -#include -#include #include #include +#include +#include +#include +#include +#include +#include + +#ifdef _MSC_VER +#include // @manual +#endif namespace folly { namespace { -// Keep it open for the duration of the program -File randomDevice("/dev/urandom"); - void readRandomDevice(void* data, size_t size) { - PCHECK(readFull(randomDevice.fd(), data, size) == size); +#ifdef _MSC_VER + static folly::once_flag flag; + static HCRYPTPROV cryptoProv; + folly::call_once(flag, [&] { + if (!CryptAcquireContext( + &cryptoProv, + nullptr, + nullptr, + PROV_RSA_FULL, + CRYPT_VERIFYCONTEXT)) { + 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 { @@ -56,7 +89,7 @@ class BufferedRandomDevice { void getSlow(unsigned char* data, size_t size); inline size_t remaining() const { - return buffer_.get() + bufferSize_ - ptr_; + return size_t(buffer_.get() + bufferSize_ - ptr_); } const size_t bufferSize_; @@ -91,32 +124,30 @@ void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) { ptr_ += size; } -ThreadLocal bufferedRandomDevice; +struct RandomTag {}; -} // namespace +} // namespace void Random::secureRandom(void* data, size_t size) { - bufferedRandomDevice->get(data, size); + static SingletonThreadLocal + bufferedRandomDevice; + bufferedRandomDevice.get().get(data, size); } -folly::ThreadLocalPtr -ThreadLocalPRNG::localInstance; - class ThreadLocalPRNG::LocalInstancePRNG { public: - LocalInstancePRNG() : rng(Random::create()) { } + LocalInstancePRNG() : rng(Random::create()) {} Random::DefaultGenerator rng; }; -ThreadLocalPRNG::LocalInstancePRNG* ThreadLocalPRNG::initLocal() { - auto ret = new LocalInstancePRNG; - localInstance.reset(ret); - return ret; +ThreadLocalPRNG::ThreadLocalPRNG() { + static SingletonThreadLocal + localInstancePRNG; + local_ = &localInstancePRNG.get(); } uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) { return local->rng(); } - -} +} // namespace folly