aboutsummaryrefslogtreecommitdiffstats
path: root/src/rng
diff options
context:
space:
mode:
authorlloyd <[email protected]>2008-10-26 03:07:18 +0000
committerlloyd <[email protected]>2008-10-26 03:07:18 +0000
commita8ee54d459a42d98fdfe1e9ff4f0c011c2f41e10 (patch)
tree576d871ed243508e5458456d12ea99d240e8339c /src/rng
parentb1344477a80c7410da9ce05dd3343c04d24f8095 (diff)
Move rng.{cpp,h} from core to rng/ topdir
Add a new class AutoSeeded_RNG that is a RandomNumberGenerator that wraps up the logic formerly in RandomNumberGenerator::make_rng. make_rng in fact now just returns a new AutoSeeded_RNG object. AutoSeeded_RNG is a bit more convenient because - No need to use auto_ptr - No need to dereference (same syntax everywhere - it's an underestimated advantage imo) Also move the code from timer/timer_base to timer/
Diffstat (limited to 'src/rng')
-rw-r--r--src/rng/auto_rng/auto_rng.cpp129
-rw-r--r--src/rng/auto_rng/auto_rng.h42
-rw-r--r--src/rng/auto_rng/info.txt18
-rw-r--r--src/rng/info.txt8
-rw-r--r--src/rng/rng.cpp44
5 files changed, 241 insertions, 0 deletions
diff --git a/src/rng/auto_rng/auto_rng.cpp b/src/rng/auto_rng/auto_rng.cpp
new file mode 100644
index 000000000..076630f6d
--- /dev/null
+++ b/src/rng/auto_rng/auto_rng.cpp
@@ -0,0 +1,129 @@
+/*************************************************
+* Auto Seeded RNG Source File *
+* (C) 2008 Jack Lloyd *
+*************************************************/
+
+#include <botan/auto_rng.h>
+#include <botan/randpool.h>
+#include <botan/parsing.h>
+#include <botan/timers.h>
+#include <botan/aes.h>
+#include <botan/hmac.h>
+#include <botan/sha2_32.h>
+
+#if defined(BOTAN_HAS_X931_RNG)
+ #include <botan/x931_rng.h>
+#endif
+
+#if defined(BOTAN_HAS_TIMER_HARDWARE)
+ #include <botan/tm_hard.h>
+#elif defined(BOTAN_HAS_TIMER_POSIX)
+ #include <botan/tm_posix.h>
+#elif defined(BOTAN_HAS_TIMER_UNIX)
+ #include <botan/tm_unix.h>
+#elif defined(BOTAN_HAS_TIMER_WIN32)
+ #include <botan/tm_win32.h>
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE)
+ #include <botan/es_dev.h>
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_EGD)
+ #include <botan/es_egd.h>
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_UNIX)
+ #include <botan/es_unix.h>
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_BEOS)
+ #include <botan/es_beos.h>
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_CAPI)
+ #include <botan/es_capi.h>
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
+ #include <botan/es_win32.h>
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_FTW)
+ #include <botan/es_ftw.h>
+#endif
+
+namespace Botan {
+
+namespace {
+
+/**
+* Add any known entropy sources to this RNG
+*/
+void add_entropy_sources(RandomNumberGenerator* rng)
+ {
+#if defined(BOTAN_HAS_TIMER_HARDWARE)
+ rng->add_entropy_source(new Hardware_Timer);
+#elif defined(BOTAN_HAS_TIMER_POSIX)
+ rng->add_entropy_source(new POSIX_Timer);
+#elif defined(BOTAN_HAS_TIMER_UNIX)
+ rng->add_entropy_source(new Unix_Timer);
+#elif defined(BOTAN_HAS_TIMER_WIN32)
+ rng->add_entropy_source(new Win32_Timer);
+#else
+ rng->add_entropy_source(new Timer);
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_DEVICE)
+ rng->add_entropy_source(
+ new Device_EntropySource(
+ split_on("/dev/random:/dev/srandom:/dev/urandom", ':')
+ )
+ );
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_EGD)
+ rng->add_entropy_source(
+ new EGD_EntropySource(split_on("/var/run/egd-pool:/dev/egd-pool", ':'))
+ );
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_CAPI)
+ rng->add_entropy_source(new Win32_CAPI_EntropySource);
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_WIN32)
+ rng->add_entropy_source(new Win32_EntropySource);
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_UNIX)
+ rng->add_entropy_source(
+ new Unix_EntropySource(split_on("/bin:/sbin:/usr/bin:/usr/sbin", ':'))
+ );
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_BEOS)
+ rng->add_entropy_source(new BeOS_EntropySource);
+#endif
+
+#if defined(BOTAN_HAS_ENTROPY_SRC_FTW)
+ rng->add_entropy_source(new FTW_EntropySource("/proc"));
+#endif
+ }
+
+}
+
+AutoSeeded_RNG::AutoSeeded_RNG()
+ {
+ /* Randpool is required for make_rng to work */
+ rng = new Randpool(new AES_256, new HMAC(new SHA_256));
+
+ /* If X9.31 is available, wrap the Randpool algorithm in it */
+#if defined(BOTAN_HAS_X931_RNG)
+ rng = new ANSI_X931_RNG(new AES_256, rng);
+#endif
+
+ add_entropy_sources(rng);
+ }
+
+}
diff --git a/src/rng/auto_rng/auto_rng.h b/src/rng/auto_rng/auto_rng.h
new file mode 100644
index 000000000..bbc3703c1
--- /dev/null
+++ b/src/rng/auto_rng/auto_rng.h
@@ -0,0 +1,42 @@
+/*************************************************
+* Auto Seeded RNG Header File *
+* (C) 2008 Jack Lloyd *
+*************************************************/
+
+#ifndef BOTAN_AUTO_SEEDING_RNG_H__
+#define BOTAN_AUTO_SEEDING_RNG_H__
+
+#include <botan/rng.h>
+#include <botan/base.h>
+
+namespace Botan {
+
+/**
+* RNG that attempts to seed itself
+*/
+class BOTAN_DLL AutoSeeded_RNG : public RandomNumberGenerator
+ {
+ public:
+ void randomize(byte out[], u32bit len)
+ { rng->randomize(out, len); }
+ bool is_seeded() const
+ { return rng->is_seeded(); }
+ void clear() throw() { rng->clear(); }
+ std::string name() const
+ { return "AutoSeeded(" + rng->name() + ")"; }
+
+ void reseed() { rng->reseed(); }
+ void add_entropy_source(EntropySource* es)
+ { rng->add_entropy_source(es); }
+ void add_entropy(const byte in[], u32bit len)
+ { rng->add_entropy(in, len); }
+
+ AutoSeeded_RNG();
+ ~AutoSeeded_RNG() { delete rng; }
+ private:
+ RandomNumberGenerator* rng;
+ };
+
+}
+
+#endif
diff --git a/src/rng/auto_rng/info.txt b/src/rng/auto_rng/info.txt
new file mode 100644
index 000000000..c2b653220
--- /dev/null
+++ b/src/rng/auto_rng/info.txt
@@ -0,0 +1,18 @@
+realname "Auto-seeded Random Number Generator"
+
+define AUTO_SEEDING_RNG
+
+load_on auto
+
+<requires>
+randpool
+aes
+sha2
+hmac
+</requires>
+
+<add>
+auto_rng.h
+auto_rng.cpp
+</add>
+
diff --git a/src/rng/info.txt b/src/rng/info.txt
new file mode 100644
index 000000000..8b542b68f
--- /dev/null
+++ b/src/rng/info.txt
@@ -0,0 +1,8 @@
+realname "Random Number Generators"
+
+load_on auto
+
+<add>
+rng.cpp
+rng.h
+</add>
diff --git a/src/rng/rng.cpp b/src/rng/rng.cpp
new file mode 100644
index 000000000..01e909610
--- /dev/null
+++ b/src/rng/rng.cpp
@@ -0,0 +1,44 @@
+/*************************************************
+* Random Number Generator Base Source File *
+* (C) 1999-2008 Jack Lloyd *
+*************************************************/
+
+#include <botan/rng.h>
+
+#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
+ #include <botan/auto_rng.h>
+#endif
+
+namespace Botan {
+
+/*************************************************
+* Default fast poll for EntropySources *
+*************************************************/
+u32bit EntropySource::fast_poll(byte buf[], u32bit len)
+ {
+ return this->slow_poll(buf, len);
+ }
+
+/*************************************************
+* Get a single random byte *
+*************************************************/
+byte RandomNumberGenerator::next_byte()
+ {
+ byte out;
+ this->randomize(&out, 1);
+ return out;
+ }
+
+/*************************************************
+* Create and seed a new RNG object *
+*************************************************/
+RandomNumberGenerator* RandomNumberGenerator::make_rng()
+ {
+#if defined(BOTAN_HAS_AUTO_SEEDING_RNG)
+ return new AutoSeeded_RNG;
+#endif
+
+ throw Algorithm_Not_Found("RandomNumberGenerator::make_rng - no RNG found");
+ }
+
+}