X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;ds=sidebyside;f=folly%2FRandom.cpp;h=1f64b1345e6c5e36300160e23a163159ca7783c7;hb=ad1bc24381d332f39052c6bc00192fc5c14db11d;hp=533ac407a801dc06ea493b1bdfb64b8377d8c7bb;hpb=7ffe7766f032914384aa6e0b9598bab7d9a90602;p=folly.git diff --git a/folly/Random.cpp b/folly/Random.cpp index 533ac407..1f64b134 100644 --- a/folly/Random.cpp +++ b/folly/Random.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,24 +17,57 @@ #include #include -#include -#include +#include #include #include -#include +#include #include #include +#include +#include +#include +#include +#include + +#ifdef _MSC_VER +# include +#endif namespace folly { namespace { void readRandomDevice(void* data, size_t size) { - // Keep it open for the duration of the program - static File randomDevice("/dev/urandom"); - auto bytesRead = readFull(randomDevice.fd(), data, 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,28 +124,30 @@ void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) { ptr_ += size; } +struct RandomTag {}; -} // namespace +} // namespace void Random::secureRandom(void* data, size_t size) { - static ThreadLocal bufferedRandomDevice; - bufferedRandomDevice->get(data, size); -} - -ThreadLocalPRNG::ThreadLocalPRNG() { - static folly::ThreadLocal localInstance; - local_ = localInstance.get(); + static SingletonThreadLocal + bufferedRandomDevice; + bufferedRandomDevice.get().get(data, size); } class ThreadLocalPRNG::LocalInstancePRNG { public: - LocalInstancePRNG() : rng(Random::create()) { } + LocalInstancePRNG() : rng(Random::create()) {} Random::DefaultGenerator rng; }; +ThreadLocalPRNG::ThreadLocalPRNG() { + static SingletonThreadLocal + localInstancePRNG; + local_ = &localInstancePRNG.get(); +} + uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) { return local->rng(); } - }