aboutsummaryrefslogtreecommitdiffstats
path: root/modules/es_capi
diff options
context:
space:
mode:
authorlloyd <[email protected]>2006-05-18 18:33:19 +0000
committerlloyd <[email protected]>2006-05-18 18:33:19 +0000
commita2c99d3270eb73ef2db5704fc54356c6b75096f8 (patch)
treead3d6c4fcc8dd0f403f8105598943616246fe172 /modules/es_capi
Initial checkin1.5.6
Diffstat (limited to 'modules/es_capi')
-rw-r--r--modules/es_capi/es_capi.cpp109
-rw-r--r--modules/es_capi/es_capi.h28
-rw-r--r--modules/es_capi/modinfo.txt17
3 files changed, 154 insertions, 0 deletions
diff --git a/modules/es_capi/es_capi.cpp b/modules/es_capi/es_capi.cpp
new file mode 100644
index 000000000..95bc3d969
--- /dev/null
+++ b/modules/es_capi/es_capi.cpp
@@ -0,0 +1,109 @@
+/*************************************************
+* Win32 CryptoAPI EntropySource Source File *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#include <botan/es_capi.h>
+#include <botan/parsing.h>
+#include <botan/conf.h>
+#include <windows.h>
+#include <wincrypt.h>
+
+namespace Botan {
+
+namespace {
+
+/*************************************************
+* CSP Handle *
+*************************************************/
+class CSP_Handle
+ {
+ public:
+ CSP_Handle(u64bit);
+ ~CSP_Handle();
+
+ void gen_random(byte[], u32bit) const;
+ bool is_valid() const { return valid; }
+
+ HCRYPTPROV get_handle() const { return handle; }
+ private:
+ HCRYPTPROV handle;
+ bool valid;
+ };
+
+/*************************************************
+* Call CryptGenRandom *
+*************************************************/
+void CSP_Handle::gen_random(byte out[], u32bit n) const
+ {
+ CryptGenRandom(handle, n, out);
+ }
+
+/*************************************************
+* Initialize a CSP Handle *
+*************************************************/
+CSP_Handle::CSP_Handle(u64bit capi_provider)
+ {
+ valid = false;
+ DWORD prov_type = (DWORD)capi_provider;
+
+ if(CryptAcquireContext(&handle, 0, 0, prov_type, CRYPT_VERIFYCONTEXT))
+ valid = true;
+ }
+
+/*************************************************
+* Destroy a CSP Handle *
+*************************************************/
+CSP_Handle::~CSP_Handle()
+ {
+ if(valid)
+ CryptReleaseContext(handle, 0);
+ }
+
+}
+
+/*************************************************
+* Gather Entropy from Win32 CAPI *
+*************************************************/
+u32bit Win32_CAPI_EntropySource::slow_poll(byte output[], u32bit length)
+ {
+ if(length > 64)
+ length = 64;
+
+ for(u32bit j = 0; j != prov_types.size(); j++)
+ {
+ CSP_Handle csp(prov_types[j]);
+ if(!csp.is_valid())
+ continue;
+
+ csp.gen_random(output, length);
+ break;
+ }
+ return length;
+ }
+
+/*************************************************
+* Gather Entropy from Win32 CAPI *
+*************************************************/
+Win32_CAPI_EntropySource::Win32_CAPI_EntropySource(const std::string& provs)
+ {
+ std::vector<std::string> capi_provs;
+
+ if(provs == "")
+ capi_provs = Config::get_list("rng/ms_capi_prov_type");
+ else
+ capi_provs = split_on(provs, ':');
+
+ for(u32bit j = 0; j != capi_provs.size(); j++)
+ {
+ if(capi_provs[j] == "RSA_FULL") prov_types.push_back(PROV_RSA_FULL);
+ if(capi_provs[j] == "INTEL_SEC") prov_types.push_back(PROV_INTEL_SEC);
+ if(capi_provs[j] == "FORTEZZA") prov_types.push_back(PROV_FORTEZZA);
+ if(capi_provs[j] == "RNG") prov_types.push_back(PROV_RNG);
+ }
+
+ if(prov_types.size() == 0)
+ prov_types.push_back(PROV_RSA_FULL);
+ }
+
+}
diff --git a/modules/es_capi/es_capi.h b/modules/es_capi/es_capi.h
new file mode 100644
index 000000000..8025e9ce7
--- /dev/null
+++ b/modules/es_capi/es_capi.h
@@ -0,0 +1,28 @@
+/*************************************************
+* Win32 CAPI EntropySource Header File *
+* (C) 1999-2006 The Botan Project *
+*************************************************/
+
+#ifndef BOTAN_EXT_ENTROPY_SRC_WIN32_CAPI_H__
+#define BOTAN_EXT_ENTROPY_SRC_WIN32_CAPI_H__
+
+#include <botan/base.h>
+#include <vector>
+
+namespace Botan {
+
+/*************************************************
+* Win32 CAPI Entropy Source *
+*************************************************/
+class Win32_CAPI_EntropySource : public EntropySource
+ {
+ public:
+ u32bit slow_poll(byte[], u32bit);
+ Win32_CAPI_EntropySource(const std::string& = "");
+ private:
+ std::vector<u64bit> prov_types;
+ };
+
+}
+
+#endif
diff --git a/modules/es_capi/modinfo.txt b/modules/es_capi/modinfo.txt
new file mode 100644
index 000000000..a61e33834
--- /dev/null
+++ b/modules/es_capi/modinfo.txt
@@ -0,0 +1,17 @@
+realname "Win32 CryptoAPI Entropy Source"
+
+define ENTROPY_SRC_CAPI
+
+add_file es_capi.h
+add_file es_capi.cpp
+
+# We'll just assume CAPI is there; this is OK except for 3.x, early versions
+# of 95, and maybe NT 3.5
+<os>
+windows
+cygwin
+</os>
+
+<libs>
+windows -> advapi32
+</libs>