From 0741627756b201ca696289fda3745dfcc672a052 Mon Sep 17 00:00:00 2001 From: MITSUNARI Shigeo Date: Sun, 21 Jun 2020 20:04:53 +0900 Subject: [PATCH] add CRYPT_VERIFYCONTEXT to CryptoAcquireContext --- include/cybozu/random_generator.hpp | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/include/cybozu/random_generator.hpp b/include/cybozu/random_generator.hpp index d0627f8..66900f9 100644 --- a/include/cybozu/random_generator.hpp +++ b/include/cybozu/random_generator.hpp @@ -17,7 +17,6 @@ #ifdef _MSC_VER #pragma comment (lib, "advapi32.lib") #endif -#include #else #include #include @@ -32,10 +31,9 @@ public: #ifdef _WIN32 RandomGenerator() : prov_(0) - , pos_(bufSize) { - DWORD flagTbl[] = { 0, CRYPT_NEWKEYSET, CRYPT_MACHINE_KEYSET }; - for (int i = 0; i < 3; i++) { + DWORD flagTbl[] = { CRYPT_VERIFYCONTEXT | CRYPT_SILENT, 0, CRYPT_MACHINE_KEYSET }; + for (int i = 0; i < CYBOZU_NUM_OF_ARRAY(flagTbl); i++) { if (CryptAcquireContext(&prov_, NULL, NULL, PROV_RSA_FULL, flagTbl[i]) != 0) return; } #ifdef CYBOZU_DONT_USE_EXCEPTION @@ -62,29 +60,11 @@ public: template void read(bool *pb, T *buf, size_t bufNum) { - cybozu::AutoLockCs al(cs_); const size_t byteSize = sizeof(T) * bufNum; - if (byteSize > bufSize) { - if (!read_inner(buf, byteSize)) { - *pb = false; - return; - } - } else { - if (pos_ + byteSize > bufSize) { - read_inner(buf_, bufSize); - pos_ = 0; - } - memcpy(buf, buf_ + pos_, byteSize); - pos_ += byteSize; - } - *pb = true; + *pb = read_inner(buf, byteSize); } private: HCRYPTPROV prov_; - static const size_t bufSize = 1024; - char buf_[bufSize]; - size_t pos_; - cybozu::CriticalSection cs_; #else RandomGenerator() : fp_(::fopen("/dev/urandom", "rb"))