monotone

monotone Mtn Source Tree

Root/botan/es_capi.cpp

1/*************************************************
2* Win32 CryptoAPI EntropySource Source File *
3* (C) 1999-2006 The Botan Project *
4*************************************************/
5
6#include <botan/es_capi.h>
7#include <botan/parsing.h>
8#include <botan/config.h>
9#include <windows.h>
10#include <wincrypt.h>
11
12namespace Botan {
13
14/*************************************************
15* Gather Entropy from Win32 CAPI *
16*************************************************/
17u32bit Win32_CAPI_EntropySource::slow_poll(byte output[], u32bit length)
18 {
19
20 class CSP_Handle
21 {
22 public:
23 CSP_Handle(u64bit capi_provider)
24 {
25 valid = false;
26 DWORD prov_type = (DWORD)capi_provider;
27
28 if(CryptAcquireContext(&handle, 0, 0,
29 prov_type, CRYPT_VERIFYCONTEXT))
30 valid = true;
31 }
32
33 ~CSP_Handle()
34 {
35 if(is_valid())
36 CryptReleaseContext(handle, 0);
37 }
38
39 void gen_random(byte out[], u32bit n) const
40 {
41 if(is_valid())
42 CryptGenRandom(handle, n, out);
43 }
44
45 bool is_valid() const { return valid; }
46
47 HCRYPTPROV get_handle() const { return handle; }
48 private:
49 HCRYPTPROV handle;
50 bool valid;
51 };
52
53
54 if(length > 64)
55 length = 64;
56
57 for(u32bit j = 0; j != prov_types.size(); j++)
58 {
59 CSP_Handle csp(prov_types[j]);
60 if(!csp.is_valid())
61 continue;
62
63 csp.gen_random(output, length);
64 break;
65 }
66 return length;
67 }
68
69/*************************************************
70* Gather Entropy from Win32 CAPI *
71*************************************************/
72Win32_CAPI_EntropySource::Win32_CAPI_EntropySource(const std::string& provs)
73 {
74 std::vector<std::string> capi_provs;
75
76 if(provs == "")
77 capi_provs = global_config().option_as_list("rng/ms_capi_prov_type");
78 else
79 capi_provs = split_on(provs, ':');
80
81 for(u32bit j = 0; j != capi_provs.size(); j++)
82 {
83 if(capi_provs[j] == "RSA_FULL") prov_types.push_back(PROV_RSA_FULL);
84 if(capi_provs[j] == "INTEL_SEC") prov_types.push_back(PROV_INTEL_SEC);
85 if(capi_provs[j] == "FORTEZZA") prov_types.push_back(PROV_FORTEZZA);
86 if(capi_provs[j] == "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}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status