X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FRandom.cpp;h=8f5e3128d2036952f31ba8efcc73791a139e333b;hb=3c2fad7c4e858b8bfa4018c4524a2af1a2c8156f;hp=c6b611be181346d5b489851b710f5af22a24a5f2;hpb=35fcff936a0ba58986269fb05689843f99e89eb5;p=folly.git diff --git a/folly/Random.cpp b/folly/Random.cpp index c6b611be..8f5e3128 100644 --- a/folly/Random.cpp +++ b/folly/Random.cpp @@ -42,8 +42,16 @@ void readRandomDevice(void* data, size_t size) { static folly::once_flag flag; static HCRYPTPROV cryptoProv; folly::call_once(flag, [&] { - PCHECK(CryptAcquireContext(&cryptoProv, nullptr, nullptr, - PROV_RSA_FULL, 0)); + 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));