Botan: src/entropy/cryptoapi_rng/es_capi.cpp Source File

Botan 1.10.17

es_capi.cpp
Go to the documentation of this file.
1 /*
2 * Win32 CryptoAPI EntropySource
3 * (C) 1999-2009 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/internal/es_capi.h>
9 #include <botan/parsing.h>
10 #include <windows.h>
11 #include <wincrypt.h>
12
13 namespace Botan {
14
15 namespace {
16
17 class CSP_Handle
18 {
19 public:
20 CSP_Handle(u64bit capi_provider)
21 {
22 valid = false;
23 DWORD prov_type = (DWORD)capi_provider;
24
25 if(CryptAcquireContext(&handle, 0, 0,
26 prov_type, CRYPT_VERIFYCONTEXT))
27 valid = true;
28 }
29
30 ~CSP_Handle()
31 {
32 if(is_valid())
33 CryptReleaseContext(handle, 0);
34 }
35
36 size_t gen_random(byte out[], size_t n) const
37 {
38 if(is_valid() && CryptGenRandom(handle, static_cast<DWORD>(n), out))
39 return n;
40 return 0;
41 }
42
43 bool is_valid() const { return valid; }
44
45 HCRYPTPROV get_handle() const { return handle; }
46 private:
47 HCRYPTPROV handle;
48 bool valid;
49 };
50
51 }
52
53 /*
54 * Gather Entropy from Win32 CAPI
55 */
56 void Win32_CAPI_EntropySource::poll(Entropy_Accumulator& accum)
57 {
58 MemoryRegion<byte>& io_buffer = accum.get_io_buffer(32);
59
60 for(size_t i = 0; i != prov_types.size(); ++i)
61 {
62 CSP_Handle csp(prov_types[i]);
63
64 size_t got = csp.gen_random(&io_buffer[0], io_buffer.size());
65
66 if(got)
67 {
68 accum.add(&io_buffer[0], io_buffer.size(), 6);
69 break;
70 }
71 }
72 }
73
74 /*
75 * Win32_Capi_Entropysource Constructor
76 */
77 Win32_CAPI_EntropySource::Win32_CAPI_EntropySource(const std::string& provs)
78 {
79 std::vector<std::string> capi_provs = split_on(provs, ':');
80
81 for(size_t i = 0; i != capi_provs.size(); ++i)
82 {
83 if(capi_provs[i] == "RSA_FULL") prov_types.push_back(PROV_RSA_FULL);
84 if(capi_provs[i] == "INTEL_SEC") prov_types.push_back(PROV_INTEL_SEC);
85 if(capi_provs[i] == "FORTEZZA") prov_types.push_back(PROV_FORTEZZA);
86 if(capi_provs[i] == "RNG") prov_types.push_back(PROV_RNG);
87 }
88
89 if(prov_types.size() == 0)
90 prov_types.push_back(PROV_RSA_FULL);
91 }
92
93 }
Botan::Win32_CAPI_EntropySource::poll
void poll(Entropy_Accumulator &accum)
Definition: es_capi.cpp:56
Botan::Entropy_Accumulator::get_io_buffer
MemoryRegion< byte > & get_io_buffer(size_t size)
Definition: entropy_src.h:38
Botan::Entropy_Accumulator::add
void add(const void *bytes, size_t length, double entropy_bits_per_byte)
Definition: entropy_src.h:70
Botan::split_on
std::vector< std::string > split_on(const std::string &str, char delim)
Definition: parsing.cpp:152
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::u64bit
unsigned long long u64bit
Definition: types.h:49
Botan::Win32_CAPI_EntropySource::Win32_CAPI_EntropySource
Win32_CAPI_EntropySource(const std::string &provs="")
Definition: es_capi.cpp:77
Botan::MemoryRegion< byte >
Botan
Definition: algo_base.h:14
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::Entropy_Accumulator
Definition: entropy_src.h:19