monotone

monotone Mtn Source Tree

Root/botan/es_capi.cpp

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

Archive Download this file

Branches

Tags

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