diff options
author | lloyd <[email protected]> | 2008-10-26 03:07:18 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2008-10-26 03:07:18 +0000 |
commit | a8ee54d459a42d98fdfe1e9ff4f0c011c2f41e10 (patch) | |
tree | 576d871ed243508e5458456d12ea99d240e8339c /src | |
parent | b1344477a80c7410da9ce05dd3343c04d24f8095 (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')
-rw-r--r-- | src/core/botan.h | 4 | ||||
-rw-r--r-- | src/core/info.txt | 9 | ||||
-rw-r--r-- | src/rng/auto_rng/auto_rng.cpp (renamed from src/core/rng.cpp) | 69 | ||||
-rw-r--r-- | src/rng/auto_rng/auto_rng.h | 42 | ||||
-rw-r--r-- | src/rng/auto_rng/info.txt | 18 | ||||
-rw-r--r-- | src/rng/info.txt | 8 | ||||
-rw-r--r-- | src/rng/rng.cpp | 44 | ||||
-rw-r--r-- | src/s2k/info.txt | 12 | ||||
-rw-r--r-- | src/utils/timer/cpu_counter/info.txt | 2 | ||||
-rw-r--r-- | src/utils/timer/gettimeofday/info.txt | 2 | ||||
-rw-r--r-- | src/utils/timer/info.txt (renamed from src/utils/timer/timer_base/info.txt) | 0 | ||||
-rw-r--r-- | src/utils/timer/posix_rt/info.txt | 2 | ||||
-rw-r--r-- | src/utils/timer/timers.cpp (renamed from src/utils/timer/timer_base/timers.cpp) | 0 | ||||
-rw-r--r-- | src/utils/timer/timers.h (renamed from src/utils/timer/timer_base/timers.h) | 0 | ||||
-rw-r--r-- | src/utils/timer/win32_query_perf_ctr/info.txt | 2 |
15 files changed, 158 insertions, 56 deletions
diff --git a/src/core/botan.h b/src/core/botan.h index 70261398a..97b7dc1a3 100644 --- a/src/core/botan.h +++ b/src/core/botan.h @@ -9,3 +9,7 @@ #include <botan/lookup.h> #include <botan/version.h> #include <botan/parsing.h> + +#if defined(BOTAN_HAS_AUTO_SEEDING_RNG) + #include <botan/auto_rng.h> +#endif diff --git a/src/core/info.txt b/src/core/info.txt index 7528abba8..a4cdfbf2b 100644 --- a/src/core/info.txt +++ b/src/core/info.txt @@ -5,15 +5,12 @@ load_on auto define CORE_MODULE <requires> -aes -sha1 -sha2 -hmac +rng filters hex bigint libstate -timer_base +timer </requires> <add> @@ -31,8 +28,6 @@ exceptn.h mem_pool.cpp mem_pool.h mutex.h -rng.cpp -rng.h secmem.h symkey.cpp symkey.h diff --git a/src/core/rng.cpp b/src/rng/auto_rng/auto_rng.cpp index 05746f188..076630f6d 100644 --- a/src/core/rng.cpp +++ b/src/rng/auto_rng/auto_rng.cpp @@ -1,23 +1,20 @@ /************************************************* -* Random Number Generator Base Source File * -* (C) 1999-2008 Jack Lloyd * +* Auto Seeded RNG Source File * +* (C) 2008 Jack Lloyd * *************************************************/ -#include <botan/rng.h> -#include <botan/util.h> +#include <botan/auto_rng.h> +#include <botan/randpool.h> #include <botan/parsing.h> #include <botan/timers.h> - -#if defined(BOTAN_HAS_RANDPOOL) - #include <botan/lookup.h> - #include <botan/randpool.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 -#endif - #if defined(BOTAN_HAS_TIMER_HARDWARE) #include <botan/tm_hard.h> #elif defined(BOTAN_HAS_TIMER_POSIX) @@ -58,41 +55,13 @@ namespace Botan { -/************************************************* -* Default fast poll for EntropySources * -*************************************************/ -u32bit EntropySource::fast_poll(byte buf[], u32bit len) - { - return this->slow_poll(buf, len); - } +namespace { -/************************************************* -* Get a single random byte * -*************************************************/ -byte RandomNumberGenerator::next_byte() +/** +* Add any known entropy sources to this RNG +*/ +void add_entropy_sources(RandomNumberGenerator* rng) { - byte out; - this->randomize(&out, 1); - return out; - } - -/************************************************* -* Create and seed a new RNG object * -*************************************************/ -RandomNumberGenerator* RandomNumberGenerator::make_rng() - { -#if defined(BOTAN_HAS_RANDPOOL) - - /* Randpool is required for make_rng to work */ - RandomNumberGenerator* rng = new Randpool(get_block_cipher("AES-256"), - get_mac("HMAC(SHA-256)")); - - - /* If X9.31 is available, wrap the Randpool algorithm in it */ -#if defined(BOTAN_HAS_X931_RNG) - rng = new ANSI_X931_RNG(get_block_cipher("AES-256"), rng); -#endif - #if defined(BOTAN_HAS_TIMER_HARDWARE) rng->add_entropy_source(new Hardware_Timer); #elif defined(BOTAN_HAS_TIMER_POSIX) @@ -140,11 +109,21 @@ RandomNumberGenerator* RandomNumberGenerator::make_rng() #if defined(BOTAN_HAS_ENTROPY_SRC_FTW) rng->add_entropy_source(new FTW_EntropySource("/proc")); #endif + } - return rng; +} + +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 - throw Algorithm_Not_Found("RandomNumberGenerator::make_rng - no RNG found"); + 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"); + } + +} diff --git a/src/s2k/info.txt b/src/s2k/info.txt new file mode 100644 index 000000000..9c8bb0c45 --- /dev/null +++ b/src/s2k/info.txt @@ -0,0 +1,12 @@ +realname "String to Key Functions" + +load_on auto + +<requires> +utils +</requires> + +<add> +s2k.cpp +s2k.h +</add> diff --git a/src/utils/timer/cpu_counter/info.txt b/src/utils/timer/cpu_counter/info.txt index eef6be5a2..025663a84 100644 --- a/src/utils/timer/cpu_counter/info.txt +++ b/src/utils/timer/cpu_counter/info.txt @@ -32,5 +32,5 @@ hppa </arch> <requires> -timer_base +timer </requires> diff --git a/src/utils/timer/gettimeofday/info.txt b/src/utils/timer/gettimeofday/info.txt index c079dfd58..d3812eedf 100644 --- a/src/utils/timer/gettimeofday/info.txt +++ b/src/utils/timer/gettimeofday/info.txt @@ -27,6 +27,6 @@ tru64 </os> <requires> -timer_base +timer </requires> diff --git a/src/utils/timer/timer_base/info.txt b/src/utils/timer/info.txt index 3637d4c94..3637d4c94 100644 --- a/src/utils/timer/timer_base/info.txt +++ b/src/utils/timer/info.txt diff --git a/src/utils/timer/posix_rt/info.txt b/src/utils/timer/posix_rt/info.txt index fb1870988..7501373bb 100644 --- a/src/utils/timer/posix_rt/info.txt +++ b/src/utils/timer/posix_rt/info.txt @@ -23,6 +23,6 @@ linux </os> <requires> -timer_base +timer </requires> diff --git a/src/utils/timer/timer_base/timers.cpp b/src/utils/timer/timers.cpp index 4f482916f..4f482916f 100644 --- a/src/utils/timer/timer_base/timers.cpp +++ b/src/utils/timer/timers.cpp diff --git a/src/utils/timer/timer_base/timers.h b/src/utils/timer/timers.h index 253f71f6b..253f71f6b 100644 --- a/src/utils/timer/timer_base/timers.h +++ b/src/utils/timer/timers.h diff --git a/src/utils/timer/win32_query_perf_ctr/info.txt b/src/utils/timer/win32_query_perf_ctr/info.txt index 5ac05da95..e74259184 100644 --- a/src/utils/timer/win32_query_perf_ctr/info.txt +++ b/src/utils/timer/win32_query_perf_ctr/info.txt @@ -21,6 +21,6 @@ windows -> user32 </libs> <requires> -timer_base +timer </requires> |