aboutsummaryrefslogtreecommitdiffstats
path: root/src/entropy/cryptoapi_rng
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-09-28 19:29:24 +0000
committerlloyd <[email protected]>2008-09-28 19:29:24 +0000
commit9bcfe627321ddc81691b835dffaa6324ac4684a4 (patch)
treefe5e8ae9813b853549558b59833022e87e83981b /src/entropy/cryptoapi_rng
parent9822a701516396b7de4e41339faecd48ff8dc8ff (diff)
Move all modules into src/ directory
Diffstat (limited to 'src/entropy/cryptoapi_rng')
-rw-r--r--src/entropy/cryptoapi_rng/es_capi.cpp87
-rw-r--r--src/entropy/cryptoapi_rng/es_capi.h28
-rw-r--r--src/entropy/cryptoapi_rng/modinfo.txt21
3 files changed, 136 insertions, 0 deletions
diff --git a/src/entropy/cryptoapi_rng/es_capi.cpp b/src/entropy/cryptoapi_rng/es_capi.cpp
new file mode 100644
index 000000000..7d1e0e753
--- /dev/null
+++ b/src/entropy/cryptoapi_rng/es_capi.cpp
@@ -0,0 +1,87 @@
+/*************************************************
+* Win32 CryptoAPI EntropySource Source File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#include <botan/es_capi.h>
+#include <botan/parsing.h>
+#include <windows.h>
+#include <wincrypt.h>
+
+namespace Botan {
+
+/*************************************************
+* Gather Entropy from Win32 CAPI *
+*************************************************/
+u32bit Win32_CAPI_EntropySource::slow_poll(byte output[], u32bit length)
+ {
+
+ class CSP_Handle
+ {
+ public:
+ CSP_Handle(u64bit capi_provider)
+ {
+ valid = false;
+ DWORD prov_type = (DWORD)capi_provider;
+
+ if(CryptAcquireContext(&handle, 0, 0,
+ prov_type, CRYPT_VERIFYCONTEXT))
+ valid = true;
+ }
+
+ ~CSP_Handle()
+ {
+ if(is_valid())
+ CryptReleaseContext(handle, 0);
+ }
+
+ void gen_random(byte out[], u32bit n) const
+ {
+ if(is_valid())
+ CryptGenRandom(handle, n, out);
+ }
+
+ bool is_valid() const { return valid; }
+
+ HCRYPTPROV get_handle() const { return handle; }
+ private:
+ HCRYPTPROV handle;
+ bool valid;
+ };
+
+
+ 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 = 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/src/entropy/cryptoapi_rng/es_capi.h b/src/entropy/cryptoapi_rng/es_capi.h
new file mode 100644
index 000000000..68e42cf9c
--- /dev/null
+++ b/src/entropy/cryptoapi_rng/es_capi.h
@@ -0,0 +1,28 @@
+/*************************************************
+* Win32 CAPI EntropySource Header File *
+* (C) 1999-2007 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_ENTROPY_SRC_WIN32_CAPI_H__
+#define BOTAN_ENTROPY_SRC_WIN32_CAPI_H__
+
+#include <botan/rng.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/src/entropy/cryptoapi_rng/modinfo.txt b/src/entropy/cryptoapi_rng/modinfo.txt
new file mode 100644
index 000000000..40104664b
--- /dev/null
+++ b/src/entropy/cryptoapi_rng/modinfo.txt
@@ -0,0 +1,21 @@
+realname "Win32 CryptoAPI Entropy Source"
+
+define ENTROPY_SRC_CAPI
+load_on auto
+modset win32
+
+<add>
+es_capi.h
+es_capi.cpp
+</add>
+
+# 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>