From ac81da89ac0dca6a4d4cc6cf12de29bf66709e57 Mon Sep 17 00:00:00 2001 From: lloyd Date: Mon, 7 Apr 2008 01:00:05 +0000 Subject: The intent of this branch is to remove the global library state object (Library_State, in libstate.{h,cpp}). It causes numerous 'interesting' problems with threads, etc, and the best solution here is to move to more or less an object-capability model, where the only objects that a piece of code can access are those which can be referenced through its arguments. First things first, remove the UI 'pulse' code. It is neither necessary nor sufficient for writing proper GUI/event driven code using Botan, has likely never been used in real code, and, given that, causes a distressing amount of overhead in terms of function calls made. --- include/libstate.h | 11 ----------- src/dsa_gen.cpp | 8 -------- src/filter.cpp | 2 -- src/libstate.cpp | 22 ---------------------- src/make_prm.cpp | 12 ------------ src/numthry.cpp | 3 --- 6 files changed, 58 deletions(-) diff --git a/include/libstate.h b/include/libstate.h index 297e39479..49f1c1b0f 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -42,13 +42,6 @@ class Library_State }; friend class Engine_Iterator; - class UI - { - public: - virtual void pulse(Pulse_Type) {} - virtual ~UI() {} - }; - Allocator* get_allocator(const std::string& = "") const; void add_allocator(Allocator*); void set_default_allocator(const std::string&) const; @@ -72,9 +65,6 @@ class Library_State void set_x509_state(class X509_GlobalState*); class X509_GlobalState& x509_state(); - void pulse(Pulse_Type) const; - void set_ui(UI*); - void set_transcoder(class Charset_Transcoder*); std::string transcode(const std::string, Character_Set, Character_Set) const; @@ -96,7 +86,6 @@ class Library_State std::map alloc_factory; mutable Allocator* cached_default_allocator; - UI* ui; class Charset_Transcoder* transcoder; RandomNumberGenerator* rng; std::vector allocators; diff --git a/src/dsa_gen.cpp b/src/dsa_gen.cpp index c4e475a49..15a0b5ff8 100644 --- a/src/dsa_gen.cpp +++ b/src/dsa_gen.cpp @@ -84,8 +84,6 @@ bool DL_Group::generate_dsa_primes(BigInt& p, BigInt& q, if(!is_prime(q)) return false; - global_state().pulse(PRIME_FOUND); - const u32bit n = (pbits-1) / (HASH_SIZE * 8), b = (pbits-1) % (HASH_SIZE * 8); @@ -94,8 +92,6 @@ bool DL_Group::generate_dsa_primes(BigInt& p, BigInt& q, for(u32bit j = 0; j != 4096; ++j) { - global_state().pulse(PRIME_SEARCHING); - for(u32bit k = 0; k <= n; ++k) { ++seed; @@ -110,10 +106,7 @@ bool DL_Group::generate_dsa_primes(BigInt& p, BigInt& q, p = X - (X % (2*q) - 1); if(p.bits() == pbits && is_prime(p)) - { - global_state().pulse(PRIME_FOUND); return true; - } } return false; } @@ -129,7 +122,6 @@ SecureVector DL_Group::generate_dsa_primes(BigInt& p, BigInt& q, while(true) { Global_RNG::randomize(seed, seed.size()); - global_state().pulse(PRIME_SEARCHING); if(generate_dsa_primes(p, q, pbits, qbits, seed)) return seed; diff --git a/src/filter.cpp b/src/filter.cpp index 385ab28a8..dcd3c3b5d 100644 --- a/src/filter.cpp +++ b/src/filter.cpp @@ -25,8 +25,6 @@ Filter::Filter() *************************************************/ void Filter::send(const byte input[], u32bit length) { - global_state().pulse(PIPE_WRITE); - bool nothing_attached = true; for(u32bit j = 0; j != total_ports(); ++j) if(next[j]) diff --git a/src/libstate.cpp b/src/libstate.cpp index f6926cdb4..7a742b468 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -276,24 +276,6 @@ X509_GlobalState& Library_State::x509_state() return (*x509_state_obj); } -/************************************************* -* Set the UI object state * -*************************************************/ -void Library_State::set_ui(UI* new_ui) - { - delete ui; - ui = new_ui; - } - -/************************************************* -* Send a pulse to the UI object * -*************************************************/ -void Library_State::pulse(Pulse_Type pulse_type) const - { - if(ui) - ui->pulse(pulse_type); - } - /************************************************* * Set the configuration object * *************************************************/ @@ -328,7 +310,6 @@ void Library_State::initialize(const InitializerOptions& args, cached_default_allocator = 0; x509_state_obj = 0; - ui = 0; timer = modules.timer(); transcoder = modules.transcoder(); @@ -382,11 +363,9 @@ Library_State::Library_State() config_obj = 0; x509_state_obj = 0; - ui = 0; transcoder = 0; rng = 0; cached_default_allocator = 0; - ui = 0; } /************************************************* @@ -399,7 +378,6 @@ Library_State::~Library_State() delete rng; delete timer; delete config_obj; - delete ui; std::for_each(entropy_sources.begin(), entropy_sources.end(), del_fun()); diff --git a/src/make_prm.cpp b/src/make_prm.cpp index a3258c131..715192c8b 100644 --- a/src/make_prm.cpp +++ b/src/make_prm.cpp @@ -29,8 +29,6 @@ BigInt random_prime(u32bit bits, const BigInt& coprime, while(true) { - global_state().pulse(PRIME_SEARCHING); - BigInt p = random_integer(bits); p.set_bit(bits - 2); p.set_bit(0); @@ -42,10 +40,7 @@ BigInt random_prime(u32bit bits, const BigInt& coprime, SecureVector sieve(sieve_size); for(u32bit j = 0; j != sieve.size(); ++j) - { sieve[j] = p % PRIMES[j]; - global_state().pulse(PRIME_SIEVING); - } u32bit counter = 0; while(true) @@ -53,8 +48,6 @@ BigInt random_prime(u32bit bits, const BigInt& coprime, if(counter == 4096 || p.bits() > bits) break; - global_state().pulse(PRIME_SEARCHING); - bool passes_sieve = true; ++counter; p += modulo; @@ -62,19 +55,14 @@ BigInt random_prime(u32bit bits, const BigInt& coprime, for(u32bit j = 0; j != sieve.size(); ++j) { sieve[j] = (sieve[j] + modulo) % PRIMES[j]; - global_state().pulse(PRIME_SIEVING); if(sieve[j] == 0) passes_sieve = false; } if(!passes_sieve || gcd(p - 1, coprime) != 1) continue; - global_state().pulse(PRIME_PASSED_SIEVE); if(passes_mr_tests(p)) - { - global_state().pulse(PRIME_FOUND); return p; - } } } } diff --git a/src/numthry.cpp b/src/numthry.cpp index 19fae1770..d5a992381 100644 --- a/src/numthry.cpp +++ b/src/numthry.cpp @@ -284,15 +284,12 @@ bool MillerRabin_Test::passes_test(const BigInt& a) if(a < 2 || a >= n_minus_1) throw Invalid_Argument("Bad size for nonce in Miller-Rabin test"); - global_state().pulse(PRIME_TESTING); - BigInt y = pow_mod(a); if(y == 1 || y == n_minus_1) return true; for(u32bit j = 1; j != s; ++j) { - global_state().pulse(PRIME_TESTING); y = reducer.square(y); if(y == 1) -- cgit v1.2.3 From d4121aee5eeab8328bb6a59ba0c2f16e2d3a72fa Mon Sep 17 00:00:00 2001 From: lloyd Date: Mon, 7 Apr 2008 01:14:53 +0000 Subject: Library_State::set_timer is never called from within Botan, low hanging fruit for removal. --- include/libstate.h | 1 - src/libstate.cpp | 9 --------- 2 files changed, 10 deletions(-) diff --git a/include/libstate.h b/include/libstate.h index 49f1c1b0f..312d68873 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -55,7 +55,6 @@ class Library_State void add_entropy(EntropySource&, bool); u32bit seed_prng(bool, u32bit); - void set_timer(class Timer*); u64bit system_clock() const; class Config& config() const; diff --git a/src/libstate.cpp b/src/libstate.cpp index 7a742b468..12f017ed4 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -122,15 +122,6 @@ void Library_State::set_default_allocator(const std::string& type) const cached_default_allocator = 0; } -/************************************************* -* Set the high resolution clock implementation * -*************************************************/ -void Library_State::set_timer(Timer* new_timer) - { - delete timer; - timer = new_timer; - } - /************************************************* * Read a high resolution clock * *************************************************/ -- cgit v1.2.3 From a48d895cf5e6874f4ae60c803bda17d62edb7a7b Mon Sep 17 00:00:00 2001 From: lloyd Date: Mon, 7 Apr 2008 01:41:17 +0000 Subject: In Botan, the Timer base class provides access to a high-resolution timer with an unspecified update rate and epoch. It is only used inside the entropy sources to provide some timing-dependent randomness. However, it is easier and basically 'as good' to treat the timers as entropy sources in their own right and feed their output directly into an entropy pool. This commit removes Library_State::system_clock and all calls to that function. --- include/buf_es.h | 1 - include/libstate.h | 3 --- include/modules.h | 2 -- include/timers.h | 6 ++++-- include/util.h | 3 +-- modules/es_unix/es_unix.cpp | 2 -- src/buf_es.cpp | 8 -------- src/libstate.cpp | 12 ------------ src/modules.cpp | 30 ++++++++++++------------------ src/randpool.cpp | 2 +- src/timers.cpp | 24 ++++++++++++++++-------- 11 files changed, 34 insertions(+), 59 deletions(-) diff --git a/include/buf_es.h b/include/buf_es.h index 53749d25a..8c0312c77 100644 --- a/include/buf_es.h +++ b/include/buf_es.h @@ -24,7 +24,6 @@ class Buffered_EntropySource : public EntropySource void add_bytes(const void*, u32bit); void add_bytes(u64bit); - void add_timestamp(); virtual void do_slow_poll() = 0; virtual void do_fast_poll(); diff --git a/include/libstate.h b/include/libstate.h index 312d68873..dc9835799 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -55,8 +55,6 @@ class Library_State void add_entropy(EntropySource&, bool); u32bit seed_prng(bool, u32bit); - u64bit system_clock() const; - class Config& config() const; class Mutex* get_mutex() const; @@ -78,7 +76,6 @@ class Library_State class Mutex* engine_lock; class Mutex* rng_lock; - class Timer* timer; mutable class Config* config_obj; class X509_GlobalState* x509_state_obj; diff --git a/include/modules.h b/include/modules.h index 1ec76478c..a5036ded3 100644 --- a/include/modules.h +++ b/include/modules.h @@ -19,7 +19,6 @@ class Modules { public: virtual class Mutex_Factory* mutex_factory() const = 0; - virtual class Timer* timer() const = 0; virtual class Charset_Transcoder* transcoder() const = 0; virtual std::string default_allocator() const = 0; @@ -38,7 +37,6 @@ class Builtin_Modules : public Modules { public: class Mutex_Factory* mutex_factory() const; - class Timer* timer() const; class Charset_Transcoder* transcoder() const; std::string default_allocator() const; diff --git a/include/timers.h b/include/timers.h index 9c836f064..676d7bbd2 100644 --- a/include/timers.h +++ b/include/timers.h @@ -6,17 +6,19 @@ #ifndef BOTAN_TIMERS_H__ #define BOTAN_TIMERS_H__ -#include +#include namespace Botan { /************************************************* * Timer Interface * *************************************************/ -class Timer +class Timer : public EntropySource { public: virtual u64bit clock() const; + u32bit slow_poll(byte[], u32bit); + virtual ~Timer() {} protected: static u64bit combine_timers(u32bit, u32bit, u32bit); diff --git a/include/util.h b/include/util.h index 2d9a70433..8cede5de9 100644 --- a/include/util.h +++ b/include/util.h @@ -11,10 +11,9 @@ namespace Botan { /************************************************* -* Timer Access Functions * +* Time Access Functions * *************************************************/ u64bit system_time(); -u64bit system_clock(); /************************************************* * Memory Locking Functions * diff --git a/modules/es_unix/es_unix.cpp b/modules/es_unix/es_unix.cpp index 3aaf6369a..baa413d55 100644 --- a/modules/es_unix/es_unix.cpp +++ b/modules/es_unix/es_unix.cpp @@ -91,8 +91,6 @@ void Unix_EntropySource::do_slow_poll() u32bit got = 0; for(u32bit j = 0; j != sources.size(); j++) { - add_timestamp(); - DataSource_Command pipe(sources[j].name_and_args, PATH); SecureVector buffer(DEFAULT_BUFFERSIZE); diff --git a/src/buf_es.cpp b/src/buf_es.cpp index b0ae98865..95b07232b 100644 --- a/src/buf_es.cpp +++ b/src/buf_es.cpp @@ -73,14 +73,6 @@ void Buffered_EntropySource::add_bytes(u64bit entropy) add_bytes(&entropy, 8); } -/************************************************* -* Add entropy to the internal buffer * -*************************************************/ -void Buffered_EntropySource::add_timestamp() - { - add_bytes(system_clock()); - } - /************************************************* * Take entropy from the internal buffer * *************************************************/ diff --git a/src/libstate.cpp b/src/libstate.cpp index 12f017ed4..21701cfa8 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -122,14 +121,6 @@ void Library_State::set_default_allocator(const std::string& type) const cached_default_allocator = 0; } -/************************************************* -* Read a high resolution clock * -*************************************************/ -u64bit Library_State::system_clock() const - { - return (timer) ? timer->clock() : 0; - } - /************************************************* * Set the global PRNG * *************************************************/ @@ -302,7 +293,6 @@ void Library_State::initialize(const InitializerOptions& args, cached_default_allocator = 0; x509_state_obj = 0; - timer = modules.timer(); transcoder = modules.transcoder(); std::vector mod_allocs = modules.allocators(); @@ -350,7 +340,6 @@ Library_State::Library_State() allocator_lock = engine_lock = rng_lock = 0; - timer = 0; config_obj = 0; x509_state_obj = 0; @@ -367,7 +356,6 @@ Library_State::~Library_State() delete x509_state_obj; delete transcoder; delete rng; - delete timer; delete config_obj; std::for_each(entropy_sources.begin(), entropy_sources.end(), diff --git a/src/modules.cpp b/src/modules.cpp index 70ce21a01..0076bbef8 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -93,24 +93,6 @@ Mutex_Factory* Builtin_Modules::mutex_factory() const #endif } -/************************************************* -* Find a high resolution timer, if possible * -*************************************************/ -Timer* Builtin_Modules::timer() const - { -#if defined(BOTAN_EXT_TIMER_HARDWARE) - return new Hardware_Timer; -#elif defined(BOTAN_EXT_TIMER_POSIX) - return new POSIX_Timer; -#elif defined(BOTAN_EXT_TIMER_UNIX) - return new Unix_Timer; -#elif defined(BOTAN_EXT_TIMER_WIN32) - return new Win32_Timer; -#else - return new Timer; -#endif - } - /************************************************* * Find any usable allocators * *************************************************/ @@ -152,6 +134,18 @@ std::vector Builtin_Modules::entropy_sources() const { std::vector sources; +#if defined(BOTAN_EXT_TIMER_HARDWARE) + sources.push_back(new Hardware_Timer); +#elif defined(BOTAN_EXT_TIMER_POSIX) + sources.push_back(new POSIX_Timer); +#elif defined(BOTAN_EXT_TIMER_UNIX) + sources.push_back(new Unix_Timer); +#elif defined(BOTAN_EXT_TIMER_WIN32) + sources.push_back(new Win32_Timer); +#else + sources.push_back(new Timer); +#endif + #if defined(BOTAN_EXT_ENTROPY_SRC_AEP) sources.push_back(new AEP_EntropySource); #endif diff --git a/src/randpool.cpp b/src/randpool.cpp index 2fdddf53e..7c3007008 100644 --- a/src/randpool.cpp +++ b/src/randpool.cpp @@ -59,7 +59,7 @@ void Randpool::randomize(byte out[], u32bit length) throw(PRNG_Unseeded) *************************************************/ void Randpool::update_buffer() { - const u64bit timestamp = system_clock(); + const u64bit timestamp = system_time(); for(u32bit j = 0; j != counter.size(); ++j) if(++counter[j]) diff --git a/src/timers.cpp b/src/timers.cpp index da02e46c6..559d77843 100644 --- a/src/timers.cpp +++ b/src/timers.cpp @@ -4,24 +4,19 @@ *************************************************/ #include -#include +#include #include namespace Botan { /************************************************* -* Timer Access Functions * +* Get the system clock * *************************************************/ u64bit system_time() { return static_cast(std::time(0)); } -u64bit system_clock() - { - return global_state().system_clock(); - } - /************************************************* * Default Timer clock reading * *************************************************/ @@ -30,12 +25,25 @@ u64bit Timer::clock() const return combine_timers(std::time(0), std::clock(), CLOCKS_PER_SEC); } +/************************************************* +* Read the clock and return the output * +*************************************************/ +u32bit Timer::slow_poll(byte out[], u32bit length) + { + const u64bit clock_value = this->clock(); + + for(u32bit j = 0; j != sizeof(clock_value); ++j) + out[j % length] ^= get_byte(j, clock_value); + + return (length < 8) ? length : 8; + } + /************************************************* * Combine a two time values into a single one * *************************************************/ u64bit Timer::combine_timers(u32bit seconds, u32bit parts, u32bit parts_hz) { - const u64bit NANOSECONDS_UNITS = 1000000000; + static const u64bit NANOSECONDS_UNITS = 1000000000; parts *= (NANOSECONDS_UNITS / parts_hz); return ((seconds * NANOSECONDS_UNITS) + parts); } -- cgit v1.2.3 From a2e45efa784ec1a75e7a6ec06d9a4f02461216aa Mon Sep 17 00:00:00 2001 From: lloyd Date: Mon, 7 Apr 2008 01:51:28 +0000 Subject: Remove Charset_Transcoder; hardwire UTF-8/Latin-1 conversions into charset.cpp --- include/charset.h | 12 ------ include/def_char.h | 25 ----------- include/libstate.h | 4 -- include/modules.h | 2 - src/charset.cpp | 103 ++++++++++++++++++++++++++++++++++++++++++++- src/def_char.cpp | 121 ----------------------------------------------------- src/libstate.cpp | 27 ------------ src/modules.cpp | 9 ---- 8 files changed, 101 insertions(+), 202 deletions(-) delete mode 100644 include/def_char.h delete mode 100644 src/def_char.cpp diff --git a/include/charset.h b/include/charset.h index 81d8f5bca..2cd0b6e3f 100644 --- a/include/charset.h +++ b/include/charset.h @@ -12,18 +12,6 @@ namespace Botan { -/************************************************* -* Character Set Transcoder Interface * -*************************************************/ -class Charset_Transcoder - { - public: - virtual std::string transcode(const std::string&, - Character_Set, Character_Set) const = 0; - - virtual ~Charset_Transcoder() {} - }; - namespace Charset { /************************************************* diff --git a/include/def_char.h b/include/def_char.h deleted file mode 100644 index 407cedfb0..000000000 --- a/include/def_char.h +++ /dev/null @@ -1,25 +0,0 @@ -/************************************************* -* Default Character Set Handling Header File * -* (C) 1999-2007 The Botan Project * -*************************************************/ - -#ifndef BOTAN_DEFAULT_CHARSET_H__ -#define BOTAN_DEFAULT_CHARSET_H__ - -#include - -namespace Botan { - -/************************************************* -* Default Character Set Transcoder Object * -*************************************************/ -class Default_Charset_Transcoder : public Charset_Transcoder - { - public: - std::string transcode(const std::string&, - Character_Set, Character_Set) const; - }; - -} - -#endif diff --git a/include/libstate.h b/include/libstate.h index dc9835799..f8a1c6939 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -62,9 +62,6 @@ class Library_State void set_x509_state(class X509_GlobalState*); class X509_GlobalState& x509_state(); - void set_transcoder(class Charset_Transcoder*); - std::string transcode(const std::string, - Character_Set, Character_Set) const; private: Library_State(const Library_State&) {} Library_State& operator=(const Library_State&) { return (*this); } @@ -82,7 +79,6 @@ class Library_State std::map alloc_factory; mutable Allocator* cached_default_allocator; - class Charset_Transcoder* transcoder; RandomNumberGenerator* rng; std::vector allocators; std::vector entropy_sources; diff --git a/include/modules.h b/include/modules.h index a5036ded3..0ef6cb36c 100644 --- a/include/modules.h +++ b/include/modules.h @@ -19,7 +19,6 @@ class Modules { public: virtual class Mutex_Factory* mutex_factory() const = 0; - virtual class Charset_Transcoder* transcoder() const = 0; virtual std::string default_allocator() const = 0; @@ -37,7 +36,6 @@ class Builtin_Modules : public Modules { public: class Mutex_Factory* mutex_factory() const; - class Charset_Transcoder* transcoder() const; std::string default_allocator() const; diff --git a/src/charset.cpp b/src/charset.cpp index 7cda83f0d..e19687163 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -6,20 +6,119 @@ #include #include #include -#include +#include #include namespace Botan { namespace Charset { +namespace { + +/************************************************* +* Convert from UCS-2 to ISO 8859-1 * +*************************************************/ +std::string ucs2_to_latin1(const std::string& ucs2) + { + if(ucs2.size() % 2 == 1) + throw Decoding_Error("UCS-2 string has an odd number of bytes"); + + std::string latin1; + + for(u32bit j = 0; j != ucs2.size(); j += 2) + { + const byte c1 = ucs2[j]; + const byte c2 = ucs2[j+1]; + + if(c1 != 0) + throw Decoding_Error("UCS-2 has non-Latin1 characters"); + + latin1 += static_cast(c2); + } + + return latin1; + } + +/************************************************* +* Convert from UTF-8 to ISO 8859-1 * +*************************************************/ +std::string utf8_to_latin1(const std::string& utf8) + { + std::string iso8859; + + u32bit position = 0; + while(position != utf8.size()) + { + const byte c1 = static_cast(utf8[position++]); + + if(c1 <= 0x7F) + iso8859 += static_cast(c1); + else if(c1 >= 0xC0 && c1 <= 0xC7) + { + if(position == utf8.size()) + throw Decoding_Error("UTF-8: sequence truncated"); + + const byte c2 = static_cast(utf8[position++]); + const byte iso_char = ((c1 & 0x07) << 6) | (c2 & 0x3F); + + if(iso_char <= 0x7F) + throw Decoding_Error("UTF-8: sequence longer than needed"); + + iso8859 += static_cast(iso_char); + } + else + throw Decoding_Error("UTF-8: Unicode chars not in Latin1 used"); + } + + return iso8859; + } + +/************************************************* +* Convert from ISO 8859-1 to UTF-8 * +*************************************************/ +std::string latin1_to_utf8(const std::string& iso8859) + { + std::string utf8; + for(u32bit j = 0; j != iso8859.size(); ++j) + { + const byte c = static_cast(iso8859[j]); + + if(c <= 0x7F) + utf8 += static_cast(c); + else + { + utf8 += static_cast((0xC0 | (c >> 6))); + utf8 += static_cast((0x80 | (c & 0x3F))); + } + } + return utf8; + } + +} + /************************************************* * Perform character set transcoding * *************************************************/ std::string transcode(const std::string& str, Character_Set to, Character_Set from) { - return global_state().transcode(str, to, from); + if(to == LOCAL_CHARSET) + to = LATIN1_CHARSET; + if(from == LOCAL_CHARSET) + from = LATIN1_CHARSET; + + if(to == from) + return str; + + if(from == LATIN1_CHARSET && to == UTF8_CHARSET) + return latin1_to_utf8(str); + if(from == UTF8_CHARSET && to == LATIN1_CHARSET) + return utf8_to_latin1(str); + if(from == UCS2_CHARSET && to == LATIN1_CHARSET) + return ucs2_to_latin1(str); + + throw Invalid_Argument("Unknown transcoding operation from " + + to_string(from) + " to " + to_string(to)); } /************************************************* diff --git a/src/def_char.cpp b/src/def_char.cpp deleted file mode 100644 index 0bbd719f6..000000000 --- a/src/def_char.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/************************************************* -* Default Character Set Handling Source File * -* (C) 1999-2007 The Botan Project * -*************************************************/ - -#include -#include -#include - -namespace Botan { - -namespace { - -/************************************************* -* Convert from UCS-2 to ISO 8859-1 * -*************************************************/ -std::string ucs2_to_latin1(const std::string& ucs2) - { - if(ucs2.size() % 2 == 1) - throw Decoding_Error("UCS-2 string has an odd number of bytes"); - - std::string latin1; - - for(u32bit j = 0; j != ucs2.size(); j += 2) - { - const byte c1 = ucs2[j]; - const byte c2 = ucs2[j+1]; - - if(c1 != 0) - throw Decoding_Error("UCS-2 has non-Latin1 characters"); - - latin1 += static_cast(c2); - } - - return latin1; - } - -/************************************************* -* Convert from UTF-8 to ISO 8859-1 * -*************************************************/ -std::string utf8_to_latin1(const std::string& utf8) - { - std::string iso8859; - - u32bit position = 0; - while(position != utf8.size()) - { - const byte c1 = static_cast(utf8[position++]); - - if(c1 <= 0x7F) - iso8859 += static_cast(c1); - else if(c1 >= 0xC0 && c1 <= 0xC7) - { - if(position == utf8.size()) - throw Decoding_Error("UTF-8: sequence truncated"); - - const byte c2 = static_cast(utf8[position++]); - const byte iso_char = ((c1 & 0x07) << 6) | (c2 & 0x3F); - - if(iso_char <= 0x7F) - throw Decoding_Error("UTF-8: sequence longer than needed"); - - iso8859 += static_cast(iso_char); - } - else - throw Decoding_Error("UTF-8: Unicode chars not in Latin1 used"); - } - - return iso8859; - } - -/************************************************* -* Convert from ISO 8859-1 to UTF-8 * -*************************************************/ -std::string latin1_to_utf8(const std::string& iso8859) - { - std::string utf8; - for(u32bit j = 0; j != iso8859.size(); ++j) - { - const byte c = static_cast(iso8859[j]); - - if(c <= 0x7F) - utf8 += static_cast(c); - else - { - utf8 += static_cast((0xC0 | (c >> 6))); - utf8 += static_cast((0x80 | (c & 0x3F))); - } - } - return utf8; - } - -} - -/************************************************* -* Transcode between character sets * -*************************************************/ -std::string Default_Charset_Transcoder::transcode(const std::string& str, - Character_Set to, - Character_Set from) const - { - if(to == LOCAL_CHARSET) - to = LATIN1_CHARSET; - if(from == LOCAL_CHARSET) - from = LATIN1_CHARSET; - - if(to == from) - return str; - - if(from == LATIN1_CHARSET && to == UTF8_CHARSET) - return latin1_to_utf8(str); - if(from == UTF8_CHARSET && to == LATIN1_CHARSET) - return utf8_to_latin1(str); - if(from == UCS2_CHARSET && to == LATIN1_CHARSET) - return ucs2_to_latin1(str); - - throw Invalid_Argument("Unknown transcoding operation from " + - to_string(from) + " to " + to_string(to)); - } - -} diff --git a/src/libstate.cpp b/src/libstate.cpp index 21701cfa8..7d3b633cd 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -215,29 +215,6 @@ void Library_State::add_engine(Engine* engine) engines.insert(engines.begin(), engine); } -/************************************************* -* Set the character set transcoder object * -*************************************************/ -void Library_State::set_transcoder(class Charset_Transcoder* transcoder) - { - if(this->transcoder) - delete this->transcoder; - this->transcoder = transcoder; - } - -/************************************************* -* Transcode a string from one charset to another * -*************************************************/ -std::string Library_State::transcode(const std::string str, - Character_Set to, - Character_Set from) const - { - if(!transcoder) - throw Invalid_State("Library_State::transcode: No transcoder set"); - - return transcoder->transcode(str, to, from); - } - /************************************************* * Set the X509 global state class * *************************************************/ @@ -293,8 +270,6 @@ void Library_State::initialize(const InitializerOptions& args, cached_default_allocator = 0; x509_state_obj = 0; - transcoder = modules.transcoder(); - std::vector mod_allocs = modules.allocators(); for(u32bit j = 0; j != mod_allocs.size(); ++j) add_allocator(mod_allocs[j]); @@ -343,7 +318,6 @@ Library_State::Library_State() config_obj = 0; x509_state_obj = 0; - transcoder = 0; rng = 0; cached_default_allocator = 0; } @@ -354,7 +328,6 @@ Library_State::Library_State() Library_State::~Library_State() { delete x509_state_obj; - delete transcoder; delete rng; delete config_obj; diff --git a/src/modules.cpp b/src/modules.cpp index 0076bbef8..e043ec970 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include @@ -208,14 +207,6 @@ std::vector Builtin_Modules::engines() const return engines; } -/************************************************* -* Find the best transcoder option * -*************************************************/ -Charset_Transcoder* Builtin_Modules::transcoder() const - { - return new Default_Charset_Transcoder; - } - /************************************************* * Builtin_Modules Constructor * *************************************************/ -- cgit v1.2.3 From ab95931a8161005c8fd8aecc6f2f59d182b86de8 Mon Sep 17 00:00:00 2001 From: lloyd Date: Mon, 7 Apr 2008 05:35:44 +0000 Subject: Remove X509_GlobalState in favor of static function Extensions::get_extension --- include/libstate.h | 5 ---- include/x509_ext.h | 2 ++ include/x509stat.h | 35 ------------------------- src/libstate.cpp | 24 ----------------- src/x509_ext.cpp | 27 ++++++++++++++++--- src/x509stat.cpp | 76 ------------------------------------------------------ 6 files changed, 25 insertions(+), 144 deletions(-) delete mode 100644 include/x509stat.h delete mode 100644 src/x509stat.cpp diff --git a/include/libstate.h b/include/libstate.h index f8a1c6939..0b2a2959f 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -58,10 +58,6 @@ class Library_State class Config& config() const; class Mutex* get_mutex() const; - - void set_x509_state(class X509_GlobalState*); - class X509_GlobalState& x509_state(); - private: Library_State(const Library_State&) {} Library_State& operator=(const Library_State&) { return (*this); } @@ -74,7 +70,6 @@ class Library_State class Mutex* rng_lock; mutable class Config* config_obj; - class X509_GlobalState* x509_state_obj; std::map alloc_factory; mutable Allocator* cached_default_allocator; diff --git a/include/x509_ext.h b/include/x509_ext.h index 13a58d7d5..63052f3c1 100644 --- a/include/x509_ext.h +++ b/include/x509_ext.h @@ -56,6 +56,8 @@ class Extensions : public ASN1_Object Extensions(bool st = true) : should_throw(st) {} ~Extensions(); private: + static Certificate_Extension* get_extension(const OID&); + std::vector extensions; bool should_throw; }; diff --git a/include/x509stat.h b/include/x509stat.h deleted file mode 100644 index c13cc31fa..000000000 --- a/include/x509stat.h +++ /dev/null @@ -1,35 +0,0 @@ -/************************************************* -* Globally Saved X.509 State Header * -* (C) 1999-2007 The Botan Project * -*************************************************/ - -#include - -namespace Botan { - -/************************************************* -* Prototype for a Certificate Extension * -*************************************************/ -class Extension_Prototype - { - public: - virtual class Certificate_Extension* make(const OID&) = 0; - virtual ~Extension_Prototype() {} - }; - -/************************************************* -* X.509 Global State * -*************************************************/ -class X509_GlobalState - { - public: - void add(Extension_Prototype*); - class Certificate_Extension* get_extension(const OID&) const; - - X509_GlobalState(); - ~X509_GlobalState(); - private: - std::vector prototypes; - }; - -} diff --git a/src/libstate.cpp b/src/libstate.cpp index 7d3b633cd..7f0c41b83 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -215,26 +214,6 @@ void Library_State::add_engine(Engine* engine) engines.insert(engines.begin(), engine); } -/************************************************* -* Set the X509 global state class * -*************************************************/ -void Library_State::set_x509_state(X509_GlobalState* new_x509_state_obj) - { - delete x509_state_obj; - x509_state_obj = new_x509_state_obj; - } - -/************************************************* -* Get the X509 global state class * -*************************************************/ -X509_GlobalState& Library_State::x509_state() - { - if(!x509_state_obj) - x509_state_obj = new X509_GlobalState(); - - return (*x509_state_obj); - } - /************************************************* * Set the configuration object * *************************************************/ @@ -268,7 +247,6 @@ void Library_State::initialize(const InitializerOptions& args, rng_lock = get_mutex(); cached_default_allocator = 0; - x509_state_obj = 0; std::vector mod_allocs = modules.allocators(); for(u32bit j = 0; j != mod_allocs.size(); ++j) @@ -316,7 +294,6 @@ Library_State::Library_State() allocator_lock = engine_lock = rng_lock = 0; config_obj = 0; - x509_state_obj = 0; rng = 0; cached_default_allocator = 0; @@ -327,7 +304,6 @@ Library_State::Library_State() *************************************************/ Library_State::~Library_State() { - delete x509_state_obj; delete rng; delete config_obj; diff --git a/src/x509_ext.cpp b/src/x509_ext.cpp index 197a86004..1c655d057 100644 --- a/src/x509_ext.cpp +++ b/src/x509_ext.cpp @@ -4,8 +4,6 @@ *************************************************/ #include -#include -#include #include #include #include @@ -17,6 +15,28 @@ namespace Botan { +/************************************************* +* List of X.509 Certificate Extensions * +*************************************************/ +Certificate_Extension* Extensions::get_extension(const OID& oid) + { +#define X509_EXTENSION(NAME, TYPE) \ + if(OIDS::name_of(oid, NAME)) \ + return new Cert_Extension::TYPE(); + + X509_EXTENSION("X509v3.KeyUsage", Key_Usage); + X509_EXTENSION("X509v3.BasicConstraints", Basic_Constraints); + X509_EXTENSION("X509v3.SubjectKeyIdentifier", Subject_Key_ID); + X509_EXTENSION("X509v3.AuthorityKeyIdentifier", Authority_Key_ID); + X509_EXTENSION("X509v3.ExtendedKeyUsage", Extended_Key_Usage); + X509_EXTENSION("X509v3.IssuerAlternativeName", Issuer_Alternative_Name); + X509_EXTENSION("X509v3.SubjectAlternativeName", Subject_Alternative_Name); + X509_EXTENSION("X509v3.CRLNumber", CRL_Number); + X509_EXTENSION("X509v3.CertificatePolicies", Certificate_Policies); + + return 0; + } + /************************************************* * Extensions Copy Constructor * *************************************************/ @@ -107,8 +127,7 @@ void Extensions::decode_from(BER_Decoder& from_source) .verify_end() .end_cons(); - Certificate_Extension* ext = - global_state().x509_state().get_extension(oid); + Certificate_Extension* ext = get_extension(oid); if(!ext) { diff --git a/src/x509stat.cpp b/src/x509stat.cpp deleted file mode 100644 index 45b858c02..000000000 --- a/src/x509stat.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/************************************************* -* Globally Saved X.509 State Source * -* (C) 1999-2007 The Botan Project * -*************************************************/ - -#include -#include -#include - -namespace Botan { - -/************************************************* -* Add a new prototype * -*************************************************/ -void X509_GlobalState::add(Extension_Prototype* proto) - { - if(proto) - prototypes.push_back(proto); - } - -/************************************************* -* Get an extension object * -*************************************************/ -Certificate_Extension* X509_GlobalState::get_extension(const OID& oid) const - { - Certificate_Extension* extension = 0; - for(u32bit j = 0; j != prototypes.size() && !extension; ++j) - extension = prototypes[j]->make(oid); - return extension; - } - -/************************************************* -* Set up a new global state for X.509 * -*************************************************/ -X509_GlobalState::X509_GlobalState() - { - -#define CREATE_PROTOTYPE(NAME, TYPE) \ - do { \ - struct TYPE ## _Prototype : public Extension_Prototype \ - { \ - Certificate_Extension* make(const OID& oid) \ - { \ - if(Botan::OIDS::name_of(oid, NAME)) \ - return new Botan::Cert_Extension::TYPE(); \ - return 0; \ - } \ - }; \ - \ - add(new TYPE ## _Prototype); \ - } while(0); - - CREATE_PROTOTYPE("X509v3.KeyUsage", Key_Usage); - CREATE_PROTOTYPE("X509v3.BasicConstraints", Basic_Constraints); - CREATE_PROTOTYPE("X509v3.SubjectKeyIdentifier", Subject_Key_ID); - CREATE_PROTOTYPE("X509v3.AuthorityKeyIdentifier", Authority_Key_ID); - CREATE_PROTOTYPE("X509v3.ExtendedKeyUsage", Extended_Key_Usage); - CREATE_PROTOTYPE("X509v3.IssuerAlternativeName", Issuer_Alternative_Name); - CREATE_PROTOTYPE("X509v3.SubjectAlternativeName", Subject_Alternative_Name); - CREATE_PROTOTYPE("X509v3.CRLNumber", CRL_Number); - CREATE_PROTOTYPE("X509v3.CertificatePolicies", Certificate_Policies); - -#undef CREATE_PROTOTYPE - } - -/************************************************* -* Destroy this global state object * -*************************************************/ -X509_GlobalState::~X509_GlobalState() - { - for(u32bit j = 0; j != prototypes.size(); ++j) - delete prototypes[j]; - prototypes.clear(); - } - -} -- cgit v1.2.3 From 9ec64ce60cad6c825b7cf40306a359d019e2c13c Mon Sep 17 00:00:00 2001 From: lloyd Date: Mon, 7 Apr 2008 14:08:27 +0000 Subject: Remove the Global_RNG namespace, along with rng.h and rng.cpp. This was essentially a facade for the RNG object living in the global library state. Rewrite all callers to directly invoke the global state object: this makes it more clear what functions are actually accessing mutable state outside of the normal reference graph (and thus, which functions will have to be altered in order to remove this dependency). Other facades remain in place for the configuration object and the memory allocator factory. --- checks/bench.cpp | 4 ++-- checks/bigint.cpp | 4 ++-- checks/dolook2.cpp | 4 ++-- checks/pk.cpp | 8 +++---- checks/pk_bench.cpp | 16 ++++++------- checks/validate.cpp | 4 ++-- include/botan.h | 1 - include/libstate.h | 1 + include/rng.h | 32 -------------------------- src/big_rand.cpp | 4 ++-- src/dsa_gen.cpp | 5 ++--- src/eme1.cpp | 4 ++-- src/eme_pkcs.cpp | 4 ++-- src/emsa4.cpp | 4 ++-- src/keypair.cpp | 6 ++--- src/libstate.cpp | 10 +++++++++ src/pbes1.cpp | 4 ++-- src/pbes2.cpp | 6 ++--- src/rng.cpp | 65 ----------------------------------------------------- src/s2k.cpp | 4 ++-- src/symkey.cpp | 4 ++-- 21 files changed, 53 insertions(+), 141 deletions(-) delete mode 100644 include/rng.h delete mode 100644 src/rng.cpp diff --git a/checks/bench.cpp b/checks/bench.cpp index 089b229f2..48db9d8cf 100644 --- a/checks/bench.cpp +++ b/checks/bench.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include using namespace Botan_types; using Botan::u64bit; @@ -31,7 +31,7 @@ double bench_filter(std::string name, Botan::Filter* filter, static const u32bit BUFFERSIZE = 32*1024; byte buf[BUFFERSIZE]; - Botan::Global_RNG::randomize(buf, BUFFERSIZE); + Botan::global_state().randomize(buf, BUFFERSIZE); u32bit iterations = 0; u64bit start = get_clock(), clocks_used = 0; diff --git a/checks/bigint.cpp b/checks/bigint.cpp index e4fec12b4..6a4d5ac94 100644 --- a/checks/bigint.cpp +++ b/checks/bigint.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include using namespace Botan; #include "common.h" @@ -264,7 +264,7 @@ u32bit check_mod(const std::vector& args) /* Won't work for us, just pick one at random */ while(b_word == 0) for(u32bit j = 0; j != 2*sizeof(word); j++) - b_word = (b_word << 4) ^ Global_RNG::random(); + b_word = (b_word << 4) ^ global_state().random(); b = b_word; diff --git a/checks/dolook2.cpp b/checks/dolook2.cpp index b6c9ba339..b49d48a5e 100644 --- a/checks/dolook2.cpp +++ b/checks/dolook2.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include using namespace Botan; /* A weird little hack to fit S2K algorithms into the validation suite @@ -49,7 +49,7 @@ class RNG_Filter : public Filter void write(const byte[], u32bit); RNG_Filter(RandomNumberGenerator* r) : rng(r), buffer(1024) { - Global_RNG::randomize(buffer, buffer.size()); + global_state().randomize(buffer, buffer.size()); rng->add_entropy(buffer, buffer.size()); } ~RNG_Filter() { delete rng; } diff --git a/checks/pk.cpp b/checks/pk.cpp index f2c2401fd..afd8d61e4 100644 --- a/checks/pk.cpp +++ b/checks/pk.cpp @@ -21,7 +21,7 @@ #include #include -#include +#include using namespace Botan; #include "common.h" @@ -194,7 +194,7 @@ u32bit do_pk_validation_tests(const std::string& filename) global_state().set_prng(new ANSI_X931_RNG); for(u32bit j = 0; j != 2; j++) - Global_RNG::seed(true, 384); + global_state().seed_prng(true, 384); do_pk_keygen_tests(); do_x509_tests(); @@ -249,7 +249,7 @@ void validate_encryption(PK_Encryptor* e, PK_Decryptor* d, global_state().set_prng(new ANSI_X931_RNG); for(u32bit j = 0; j != 2; j++) - Global_RNG::seed(true, 384); + global_state().seed_prng(true, 384); validate_decryption(d, algo, out, message, failure); delete e; @@ -290,7 +290,7 @@ void validate_signature(PK_Verifier* v, PK_Signer* s, const std::string& algo, global_state().set_prng(new ANSI_X931_RNG); for(u32bit j = 0; j != 2; j++) - Global_RNG::seed(true, 384); + global_state().seed_prng(true, 384); delete v; delete s; diff --git a/checks/pk_bench.cpp b/checks/pk_bench.cpp index c06f12abf..51a454f4a 100644 --- a/checks/pk_bench.cpp +++ b/checks/pk_bench.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include using namespace Botan; @@ -219,7 +219,7 @@ void bench_enc(PK_Encryptor* enc, const std::string& algo_name, while(clocks_used < seconds * ticks) { runs++; - Global_RNG::randomize(msg, MSG_SIZE); + global_state().randomize(msg, MSG_SIZE); u64bit start = get_clock(); enc->encrypt(msg, MSG_SIZE); @@ -237,7 +237,7 @@ void bench_dec(PK_Encryptor* enc, PK_Decryptor* dec, { static const u32bit MSG_SIZE = 16; byte msg[MSG_SIZE]; - Global_RNG::randomize(msg, MSG_SIZE); + global_state().randomize(msg, MSG_SIZE); SecureVector output; u32bit runs = 0; @@ -250,7 +250,7 @@ void bench_dec(PK_Encryptor* enc, PK_Decryptor* dec, { runs++; - Global_RNG::randomize(msg, MSG_SIZE); + global_state().randomize(msg, MSG_SIZE); msg[0] |= 0x80; // make sure it works with "Raw" padding encrypted_msg = enc->encrypt(msg, MSG_SIZE); @@ -286,7 +286,7 @@ void bench_sig(PK_Signer* sig, const std::string& algo_name, while(clocks_used < seconds * ticks) { runs++; - Global_RNG::randomize(msg, MSG_SIZE); + global_state().randomize(msg, MSG_SIZE); u64bit start = get_clock(); sig->update(msg, MSG_SIZE); sig->signature(); @@ -304,7 +304,7 @@ void bench_ver(PK_Signer* sig, PK_Verifier* ver, { static const u32bit MSG_SIZE = 16; byte msg[MSG_SIZE]; - Global_RNG::randomize(msg, MSG_SIZE); + global_state().randomize(msg, MSG_SIZE); sig->update(msg, MSG_SIZE); SecureVector signature = sig->signature(); @@ -317,7 +317,7 @@ void bench_ver(PK_Signer* sig, PK_Verifier* ver, // feel free to tweak, but make sure this always runs when runs == 0 if(runs % 100 == 0) { - Global_RNG::randomize(msg, MSG_SIZE); + global_state().randomize(msg, MSG_SIZE); sig->update(msg, MSG_SIZE); signature = sig->signature(); } @@ -352,7 +352,7 @@ void bench_kas(PK_Key_Agreement* kas, const std::string& algo_name, while(clocks_used < seconds * ticks) { runs++; - Global_RNG::randomize(key, REMOTE_KEY_SIZE); + global_state().randomize(key, REMOTE_KEY_SIZE); u64bit start = get_clock(); kas->derive_key(0, key, REMOTE_KEY_SIZE); diff --git a/checks/validate.cpp b/checks/validate.cpp index d634d3bb3..269b353a4 100644 --- a/checks/validate.cpp +++ b/checks/validate.cpp @@ -10,7 +10,7 @@ #include #include -#include +#include using namespace Botan_types; #define EXTRA_TESTS 0 @@ -31,7 +31,7 @@ u32bit random_word(u32bit max) /* normal version */ u32bit r = 0; for(u32bit j = 0; j != 4; j++) - r = (r << 8) | Botan::Global_RNG::random(); + r = (r << 8) | Botan::global_state().random(); return ((r % max) + 1); // return between 1 and max inclusive #endif } diff --git a/include/botan.h b/include/botan.h index 007bf411e..bdd591569 100644 --- a/include/botan.h +++ b/include/botan.h @@ -7,6 +7,5 @@ #include #include #include -#include #include #include diff --git a/include/libstate.h b/include/libstate.h index 0b2a2959f..5534d4ac1 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -48,6 +48,7 @@ class Library_State bool rng_is_seeded() const { return rng->is_seeded(); } void randomize(byte[], u32bit); + byte random(); void set_prng(RandomNumberGenerator*); void add_entropy_source(EntropySource*, bool = true); diff --git a/include/rng.h b/include/rng.h deleted file mode 100644 index 207da51b5..000000000 --- a/include/rng.h +++ /dev/null @@ -1,32 +0,0 @@ -/************************************************* -* Global RNG Header File * -* (C) 1999-2007 The Botan Project * -*************************************************/ - -#ifndef BOTAN_GLOBAL_RNG_H__ -#define BOTAN_GLOBAL_RNG_H__ - -#include - -namespace Botan { - -/************************************************* -* RNG Access and Seeding Functions * -*************************************************/ -namespace Global_RNG { - -void randomize(byte[], u32bit); -byte random(); - -void add_entropy(const byte[], u32bit); -void add_entropy(EntropySource&, bool = true); - -u32bit seed(bool = true, u32bit = 256); - -void add_es(EntropySource*, bool = true); - -} - -} - -#endif diff --git a/src/big_rand.cpp b/src/big_rand.cpp index 7dac05e67..4d3abe1a2 100644 --- a/src/big_rand.cpp +++ b/src/big_rand.cpp @@ -6,7 +6,7 @@ #include #include #include -#include +#include namespace Botan { @@ -36,7 +36,7 @@ void BigInt::randomize(u32bit bitsize) else { SecureVector array((bitsize + 7) / 8); - Global_RNG::randomize(array, array.size()); + global_state().randomize(array, array.size()); if(bitsize % 8) array[0] &= 0xFF >> (8 - (bitsize % 8)); array[0] |= 0x80 >> ((bitsize % 8) ? (8 - bitsize % 8) : 0); diff --git a/src/dsa_gen.cpp b/src/dsa_gen.cpp index 15a0b5ff8..3b1e52ad5 100644 --- a/src/dsa_gen.cpp +++ b/src/dsa_gen.cpp @@ -5,10 +5,9 @@ #include #include -#include #include #include -#include +#include #include #include @@ -121,7 +120,7 @@ SecureVector DL_Group::generate_dsa_primes(BigInt& p, BigInt& q, while(true) { - Global_RNG::randomize(seed, seed.size()); + global_state().randomize(seed, seed.size()); if(generate_dsa_primes(p, q, pbits, qbits, seed)) return seed; diff --git a/src/eme1.cpp b/src/eme1.cpp index bf914d248..ed8a8e4fd 100644 --- a/src/eme1.cpp +++ b/src/eme1.cpp @@ -4,7 +4,7 @@ *************************************************/ #include -#include +#include #include #include #include @@ -26,7 +26,7 @@ SecureVector EME1::pad(const byte in[], u32bit in_length, out.clear(); - Global_RNG::randomize(out, HASH_LENGTH); + global_state().randomize(out, HASH_LENGTH); out.copy(HASH_LENGTH, Phash, Phash.size()); out[out.size() - in_length - 1] = 0x01; diff --git a/src/eme_pkcs.cpp b/src/eme_pkcs.cpp index 043d955c2..55ceeea99 100644 --- a/src/eme_pkcs.cpp +++ b/src/eme_pkcs.cpp @@ -4,7 +4,7 @@ *************************************************/ #include -#include +#include namespace Botan { @@ -26,7 +26,7 @@ SecureVector EME_PKCS1v15::pad(const byte in[], u32bit inlen, out[0] = 0x02; for(u32bit j = 1; j != olen - inlen - 1; ++j) while(out[j] == 0) - out[j] = Global_RNG::random(); + out[j] = global_state().random(); out.copy(olen - inlen, in, inlen); return out; diff --git a/src/emsa4.cpp b/src/emsa4.cpp index 5b2719f58..071439007 100644 --- a/src/emsa4.cpp +++ b/src/emsa4.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include namespace Botan { @@ -43,7 +43,7 @@ SecureVector EMSA4::encoding_of(const MemoryRegion& msg, const u32bit output_length = (output_bits + 7) / 8; SecureVector salt(SALT_SIZE); - Global_RNG::randomize(salt, SALT_SIZE); + global_state().randomize(salt, SALT_SIZE); for(u32bit j = 0; j != 8; ++j) hash->update(0); diff --git a/src/keypair.cpp b/src/keypair.cpp index b40b8c395..6401131ea 100644 --- a/src/keypair.cpp +++ b/src/keypair.cpp @@ -5,7 +5,7 @@ #include #include -#include +#include #include namespace Botan { @@ -24,7 +24,7 @@ void check_key(PK_Encryptor* encryptor, PK_Decryptor* decryptor) std::auto_ptr dec(decryptor); SecureVector message(enc->maximum_input_size() - 1); - Global_RNG::randomize(message, message.size()); + global_state().randomize(message, message.size()); SecureVector ciphertext = enc->encrypt(message); if(ciphertext == message) @@ -44,7 +44,7 @@ void check_key(PK_Signer* signer, PK_Verifier* verifier) std::auto_ptr ver(verifier); SecureVector message(16); - Global_RNG::randomize(message, message.size()); + global_state().randomize(message, message.size()); SecureVector signature; diff --git a/src/libstate.cpp b/src/libstate.cpp index 7f0c41b83..5e440c103 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -141,6 +141,16 @@ void Library_State::randomize(byte out[], u32bit length) rng->randomize(out, length); } +/************************************************* +* Get a byte from the global PRNG * +*************************************************/ +byte Library_State::random() + { + byte out; + rng->randomize(&out, 1); + return out; + } + /************************************************* * Add a new entropy source to use * *************************************************/ diff --git a/src/pbes1.cpp b/src/pbes1.cpp index 69f2ebc10..cdd2a87fd 100644 --- a/src/pbes1.cpp +++ b/src/pbes1.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include @@ -86,7 +86,7 @@ void PBE_PKCS5v15::new_params() { iterations = 2048; salt.create(8); - Global_RNG::randomize(salt, salt.size()); + global_state().randomize(salt, salt.size()); } /************************************************* diff --git a/src/pbes2.cpp b/src/pbes2.cpp index 400e66572..029310fff 100644 --- a/src/pbes2.cpp +++ b/src/pbes2.cpp @@ -8,7 +8,7 @@ #include #include #include -#include +#include #include #include #include @@ -87,8 +87,8 @@ void PBE_PKCS5v20::new_params() key_length = max_keylength_of(cipher_algo); salt.create(8); iv.create(block_size_of(cipher_algo)); - Global_RNG::randomize(salt, salt.size()); - Global_RNG::randomize(iv, iv.size()); + global_state().randomize(salt, salt.size()); + global_state().randomize(iv, iv.size()); } /************************************************* diff --git a/src/rng.cpp b/src/rng.cpp deleted file mode 100644 index be8891921..000000000 --- a/src/rng.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/************************************************* -* Global RNG Source File * -* (C) 1999-2007 The Botan Project * -*************************************************/ - -#include -#include - -namespace Botan { - -namespace Global_RNG { - -/************************************************* -* Get random bits from the global RNG * -*************************************************/ -void randomize(byte output[], u32bit size) - { - global_state().randomize(output, size); - } - -/************************************************* -* Get random bits from the global RNG * -*************************************************/ -byte random() - { - byte ret = 0; - randomize(&ret, 1); - return ret; - } - -/************************************************* -* Add entropy to the global RNG * -*************************************************/ -void add_entropy(const byte entropy[], u32bit size) - { - global_state().add_entropy(entropy, size); - } - -/************************************************* -* Add entropy to the global RNG * -*************************************************/ -void add_entropy(EntropySource& src, bool slow_poll) - { - global_state().add_entropy(src, slow_poll); - } - -/************************************************* -* Add an EntropySource to the RNG seed list * -*************************************************/ -void add_es(EntropySource* src, bool last) - { - global_state().add_entropy_source(src, last); - } - -/************************************************* -* Seed the global RNG * -*************************************************/ -u32bit seed(bool slow_poll, u32bit bits_to_get) - { - return global_state().seed_prng(slow_poll, bits_to_get); - } - -} - -} diff --git a/src/s2k.cpp b/src/s2k.cpp index 94022b0d5..8860f5d4c 100644 --- a/src/s2k.cpp +++ b/src/s2k.cpp @@ -4,7 +4,7 @@ *************************************************/ #include -#include +#include namespace Botan { @@ -47,7 +47,7 @@ void S2K::change_salt(const MemoryRegion& new_salt) void S2K::new_random_salt(u32bit length) { salt.create(length); - Global_RNG::randomize(salt, length); + global_state().randomize(salt, length); } } diff --git a/src/symkey.cpp b/src/symkey.cpp index b1f0786b0..d6302afbc 100644 --- a/src/symkey.cpp +++ b/src/symkey.cpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include namespace Botan { @@ -18,7 +18,7 @@ namespace Botan { void OctetString::change(u32bit length) { bits.create(length); - Global_RNG::randomize(bits, length); + global_state().randomize(bits, length); } /************************************************* -- cgit v1.2.3