diff options
41 files changed, 213 insertions, 614 deletions
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 <string> #include <exception> -#include <botan/rng.h> +#include <botan/libstate.h> #include <botan/filters.h> 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 <botan/bigint.h> #include <botan/exceptn.h> #include <botan/numthry.h> -#include <botan/rng.h> +#include <botan/libstate.h> using namespace Botan; #include "common.h" @@ -264,7 +264,7 @@ u32bit check_mod(const std::vector<std::string>& 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 <botan/filters.h> #include <botan/randpool.h> #include <botan/x931_rng.h> -#include <botan/rng.h> +#include <botan/libstate.h> 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 <botan/numthry.h> #include <botan/x931_rng.h> -#include <botan/rng.h> +#include <botan/libstate.h> 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 <botan/pkcs8.h> #include <botan/look_pk.h> -#include <botan/rng.h> +#include <botan/libstate.h> 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<byte> 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<byte> 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 <botan/filters.h> #include <botan/exceptn.h> -#include <botan/rng.h> +#include <botan/libstate.h> 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 99c3864f3..a7b124da4 100644 --- a/include/botan.h +++ b/include/botan.h @@ -7,6 +7,5 @@ #include <botan/config.h> #include <botan/init.h> #include <botan/lookup.h> -#include <botan/rng.h> #include <botan/version.h> #include <botan/parsing.h> diff --git a/include/buf_es.h b/include/buf_es.h index 516866c79..4bf178ae7 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/charset.h b/include/charset.h index 9c2be4c07..ac9f501a0 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 57080bd7f..000000000 --- a/include/def_char.h +++ /dev/null @@ -1,25 +0,0 @@ -/************************************************* -* Default Character Set Handling Header File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#ifndef BOTAN_DEFAULT_CHARSET_H__ -#define BOTAN_DEFAULT_CHARSET_H__ - -#include <botan/charset.h> - -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 40c4a94b0..7e83f57c6 100644 --- a/include/libstate.h +++ b/include/libstate.h @@ -42,19 +42,13 @@ 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; 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); @@ -62,22 +56,9 @@ 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; class Mutex* get_mutex() const; - - 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; private: Library_State(const Library_State&) {} Library_State& operator=(const Library_State&) { return (*this); } @@ -89,15 +70,11 @@ 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; std::map<std::string, Allocator*> alloc_factory; mutable Allocator* cached_default_allocator; - UI* ui; - class Charset_Transcoder* transcoder; RandomNumberGenerator* rng; std::vector<Allocator*> allocators; std::vector<EntropySource*> entropy_sources; diff --git a/include/modules.h b/include/modules.h index 5b84e6ca1..76c1337ab 100644 --- a/include/modules.h +++ b/include/modules.h @@ -19,8 +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,8 +36,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/rng.h b/include/rng.h deleted file mode 100644 index fee9c088f..000000000 --- a/include/rng.h +++ /dev/null @@ -1,32 +0,0 @@ -/************************************************* -* Global RNG Header File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#ifndef BOTAN_GLOBAL_RNG_H__ -#define BOTAN_GLOBAL_RNG_H__ - -#include <botan/base.h> - -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/include/timers.h b/include/timers.h index c4213de5b..abceda449 100644 --- a/include/timers.h +++ b/include/timers.h @@ -6,17 +6,19 @@ #ifndef BOTAN_TIMERS_H__ #define BOTAN_TIMERS_H__ -#include <botan/types.h> +#include <botan/base.h> 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 f3817cd7e..022026bc4 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/include/x509_ext.h b/include/x509_ext.h index 85aa47c92..b715de2e9 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<Certificate_Extension*> extensions; bool should_throw; }; diff --git a/include/x509stat.h b/include/x509stat.h deleted file mode 100644 index 4f734941f..000000000 --- a/include/x509stat.h +++ /dev/null @@ -1,35 +0,0 @@ -/************************************************* -* Globally Saved X.509 State Header * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/asn1_oid.h> - -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<Extension_Prototype*> prototypes; - }; - -} diff --git a/modules/es_unix/es_unix.cpp b/modules/es_unix/es_unix.cpp index 466c0accb..b22d9dd5c 100644 --- a/modules/es_unix/es_unix.cpp +++ b/modules/es_unix/es_unix.cpp @@ -90,8 +90,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<byte> buffer(DEFAULT_BUFFERSIZE); diff --git a/src/big_rand.cpp b/src/big_rand.cpp index c18aadfeb..264f5dcb3 100644 --- a/src/big_rand.cpp +++ b/src/big_rand.cpp @@ -6,7 +6,7 @@ #include <botan/bigint.h> #include <botan/parsing.h> #include <botan/numthry.h> -#include <botan/rng.h> +#include <botan/libstate.h> namespace Botan { @@ -36,7 +36,7 @@ void BigInt::randomize(u32bit bitsize) else { SecureVector<byte> 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/buf_es.cpp b/src/buf_es.cpp index bf5a9732e..9f7be3e9b 100644 --- a/src/buf_es.cpp +++ b/src/buf_es.cpp @@ -74,14 +74,6 @@ void Buffered_EntropySource::add_bytes(u64bit entropy) } /************************************************* -* Add entropy to the internal buffer * -*************************************************/ -void Buffered_EntropySource::add_timestamp() - { - add_bytes(system_clock()); - } - -/************************************************* * Take entropy from the internal buffer * *************************************************/ u32bit Buffered_EntropySource::copy_out(byte out[], u32bit length, diff --git a/src/charset.cpp b/src/charset.cpp index 3025794ff..4ebf37abf 100644 --- a/src/charset.cpp +++ b/src/charset.cpp @@ -6,20 +6,119 @@ #include <botan/charset.h> #include <botan/hex.h> #include <botan/base64.h> -#include <botan/libstate.h> +#include <botan/parsing.h> #include <cctype> 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<char>(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<byte>(utf8[position++]); + + if(c1 <= 0x7F) + iso8859 += static_cast<char>(c1); + else if(c1 >= 0xC0 && c1 <= 0xC7) + { + if(position == utf8.size()) + throw Decoding_Error("UTF-8: sequence truncated"); + + const byte c2 = static_cast<byte>(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<char>(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<byte>(iso8859[j]); + + if(c <= 0x7F) + utf8 += static_cast<char>(c); + else + { + utf8 += static_cast<char>((0xC0 | (c >> 6))); + utf8 += static_cast<char>((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 512647eb2..000000000 --- a/src/def_char.cpp +++ /dev/null @@ -1,121 +0,0 @@ -/************************************************* -* Default Character Set Handling Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/def_char.h> -#include <botan/exceptn.h> -#include <botan/parsing.h> - -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<char>(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<byte>(utf8[position++]); - - if(c1 <= 0x7F) - iso8859 += static_cast<char>(c1); - else if(c1 >= 0xC0 && c1 <= 0xC7) - { - if(position == utf8.size()) - throw Decoding_Error("UTF-8: sequence truncated"); - - const byte c2 = static_cast<byte>(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<char>(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<byte>(iso8859[j]); - - if(c <= 0x7F) - utf8 += static_cast<char>(c); - else - { - utf8 += static_cast<char>((0xC0 | (c >> 6))); - utf8 += static_cast<char>((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/dsa_gen.cpp b/src/dsa_gen.cpp index 576c9a938..002af7d96 100644 --- a/src/dsa_gen.cpp +++ b/src/dsa_gen.cpp @@ -5,10 +5,9 @@ #include <botan/dl_group.h> #include <botan/numthry.h> -#include <botan/libstate.h> #include <botan/lookup.h> #include <botan/parsing.h> -#include <botan/rng.h> +#include <botan/libstate.h> #include <algorithm> #include <memory> @@ -84,8 +83,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 +91,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 +105,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; } @@ -128,8 +120,7 @@ SecureVector<byte> DL_Group::generate_dsa_primes(BigInt& p, BigInt& q, while(true) { - Global_RNG::randomize(seed, seed.size()); - global_state().pulse(PRIME_SEARCHING); + 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 62c5e81bd..43b5a0027 100644 --- a/src/eme1.cpp +++ b/src/eme1.cpp @@ -4,7 +4,7 @@ *************************************************/ #include <botan/eme.h> -#include <botan/rng.h> +#include <botan/libstate.h> #include <botan/lookup.h> #include <botan/look_pk.h> #include <memory> @@ -26,7 +26,7 @@ SecureVector<byte> 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 a9bf30625..8296681d8 100644 --- a/src/eme_pkcs.cpp +++ b/src/eme_pkcs.cpp @@ -4,7 +4,7 @@ *************************************************/ #include <botan/eme.h> -#include <botan/rng.h> +#include <botan/libstate.h> namespace Botan { @@ -26,7 +26,7 @@ SecureVector<byte> 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 3465d63d7..831afd590 100644 --- a/src/emsa4.cpp +++ b/src/emsa4.cpp @@ -7,7 +7,7 @@ #include <botan/lookup.h> #include <botan/look_pk.h> #include <botan/bit_ops.h> -#include <botan/rng.h> +#include <botan/libstate.h> namespace Botan { @@ -43,7 +43,7 @@ SecureVector<byte> EMSA4::encoding_of(const MemoryRegion<byte>& msg, const u32bit output_length = (output_bits + 7) / 8; SecureVector<byte> 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/filter.cpp b/src/filter.cpp index b58b60332..a5bc2dea1 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/keypair.cpp b/src/keypair.cpp index 54d117293..edec0b572 100644 --- a/src/keypair.cpp +++ b/src/keypair.cpp @@ -5,7 +5,7 @@ #include <botan/keypair.h> #include <botan/look_pk.h> -#include <botan/rng.h> +#include <botan/libstate.h> #include <memory> namespace Botan { @@ -24,7 +24,7 @@ void check_key(PK_Encryptor* encryptor, PK_Decryptor* decryptor) std::auto_ptr<PK_Decryptor> dec(decryptor); SecureVector<byte> message(enc->maximum_input_size() - 1); - Global_RNG::randomize(message, message.size()); + global_state().randomize(message, message.size()); SecureVector<byte> ciphertext = enc->encrypt(message); if(ciphertext == message) @@ -44,7 +44,7 @@ void check_key(PK_Signer* signer, PK_Verifier* verifier) std::auto_ptr<PK_Verifier> ver(verifier); SecureVector<byte> message(16); - Global_RNG::randomize(message, message.size()); + global_state().randomize(message, message.size()); SecureVector<byte> signature; diff --git a/src/libstate.cpp b/src/libstate.cpp index 154ec928c..db2d124a8 100644 --- a/src/libstate.cpp +++ b/src/libstate.cpp @@ -7,10 +7,8 @@ #include <botan/config.h> #include <botan/modules.h> #include <botan/engine.h> -#include <botan/x509stat.h> #include <botan/stl_util.h> #include <botan/mutex.h> -#include <botan/timers.h> #include <botan/charset.h> #include <botan/x931_rng.h> #include <botan/selftest.h> @@ -123,23 +121,6 @@ void Library_State::set_default_allocator(const std::string& type) const } /************************************************* -* Set the high resolution clock implementation * -*************************************************/ -void Library_State::set_timer(Timer* new_timer) - { - delete timer; - timer = new_timer; - } - -/************************************************* -* Read a high resolution clock * -*************************************************/ -u64bit Library_State::system_clock() const - { - return (timer) ? timer->clock() : 0; - } - -/************************************************* * Set the global PRNG * *************************************************/ void Library_State::set_prng(RandomNumberGenerator* new_rng) @@ -161,6 +142,16 @@ void Library_State::randomize(byte out[], u32bit 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 * *************************************************/ void Library_State::add_entropy_source(EntropySource* src, bool last_in_list) @@ -234,67 +225,6 @@ void Library_State::add_engine(Engine* 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 * -*************************************************/ -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 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 * *************************************************/ Config& Library_State::config() const @@ -327,11 +257,6 @@ void Library_State::initialize(const InitializerOptions& args, rng_lock = get_mutex(); cached_default_allocator = 0; - x509_state_obj = 0; - ui = 0; - - timer = modules.timer(); - transcoder = modules.transcoder(); std::vector<Allocator*> mod_allocs = modules.allocators(); for(u32bit j = 0; j != mod_allocs.size(); ++j) @@ -378,15 +303,10 @@ Library_State::Library_State() allocator_lock = engine_lock = rng_lock = 0; - timer = 0; config_obj = 0; - x509_state_obj = 0; - ui = 0; - transcoder = 0; rng = 0; cached_default_allocator = 0; - ui = 0; } /************************************************* @@ -394,12 +314,8 @@ Library_State::Library_State() *************************************************/ Library_State::~Library_State() { - delete x509_state_obj; - delete transcoder; delete rng; - delete timer; delete config_obj; - delete ui; std::for_each(entropy_sources.begin(), entropy_sources.end(), del_fun<EntropySource>()); diff --git a/src/make_prm.cpp b/src/make_prm.cpp index 01f524865..d5f9961af 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<u32bit> 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/modules.cpp b/src/modules.cpp index 51dc362fd..b69e6c5f7 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -5,7 +5,6 @@ #include <botan/modules.h> #include <botan/defalloc.h> -#include <botan/def_char.h> #include <botan/eng_def.h> #include <botan/timers.h> #include <botan/parsing.h> @@ -95,24 +94,6 @@ Mutex_Factory* Builtin_Modules::mutex_factory() const } /************************************************* -* 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 * *************************************************/ std::vector<Allocator*> Builtin_Modules::allocators() const @@ -153,6 +134,18 @@ std::vector<EntropySource*> Builtin_Modules::entropy_sources() const { std::vector<EntropySource*> 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 @@ -224,14 +217,6 @@ std::vector<Engine*> Builtin_Modules::engines() const } /************************************************* -* Find the best transcoder option * -*************************************************/ -Charset_Transcoder* Builtin_Modules::transcoder() const - { - return new Default_Charset_Transcoder; - } - -/************************************************* * Builtin_Modules Constructor * *************************************************/ Builtin_Modules::Builtin_Modules(const InitializerOptions& args) : diff --git a/src/numthry.cpp b/src/numthry.cpp index 233702138..01b46c982 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) diff --git a/src/pbes1.cpp b/src/pbes1.cpp index fb8889fdc..8e548f6b4 100644 --- a/src/pbes1.cpp +++ b/src/pbes1.cpp @@ -8,7 +8,7 @@ #include <botan/ber_dec.h> #include <botan/parsing.h> #include <botan/lookup.h> -#include <botan/rng.h> +#include <botan/libstate.h> #include <algorithm> #include <memory> @@ -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 f2c46b556..ea51597a3 100644 --- a/src/pbes2.cpp +++ b/src/pbes2.cpp @@ -8,7 +8,7 @@ #include <botan/ber_dec.h> #include <botan/parsing.h> #include <botan/lookup.h> -#include <botan/rng.h> +#include <botan/libstate.h> #include <botan/asn1_obj.h> #include <botan/oids.h> #include <algorithm> @@ -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/randpool.cpp b/src/randpool.cpp index e935efe35..46968eee1 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/rng.cpp b/src/rng.cpp deleted file mode 100644 index dc93b56f6..000000000 --- a/src/rng.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/************************************************* -* Global RNG Source File * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/rng.h> -#include <botan/libstate.h> - -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 d20b6738c..e5668f32f 100644 --- a/src/s2k.cpp +++ b/src/s2k.cpp @@ -4,7 +4,7 @@ *************************************************/ #include <botan/s2k.h> -#include <botan/rng.h> +#include <botan/libstate.h> namespace Botan { @@ -47,7 +47,7 @@ void S2K::change_salt(const MemoryRegion<byte>& 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 58c050aac..db70810fb 100644 --- a/src/symkey.cpp +++ b/src/symkey.cpp @@ -7,7 +7,7 @@ #include <botan/bit_ops.h> #include <botan/pipe.h> #include <botan/hex.h> -#include <botan/rng.h> +#include <botan/libstate.h> #include <algorithm> 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); } /************************************************* diff --git a/src/timers.cpp b/src/timers.cpp index 86839ad85..55ee12236 100644 --- a/src/timers.cpp +++ b/src/timers.cpp @@ -4,24 +4,19 @@ *************************************************/ #include <botan/timers.h> -#include <botan/libstate.h> +#include <botan/loadstor.h> #include <ctime> namespace Botan { /************************************************* -* Timer Access Functions * +* Get the system clock * *************************************************/ u64bit system_time() { return static_cast<u64bit>(std::time(0)); } -u64bit system_clock() - { - return global_state().system_clock(); - } - /************************************************* * Default Timer clock reading * *************************************************/ @@ -31,11 +26,24 @@ u64bit Timer::clock() const } /************************************************* +* 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); } diff --git a/src/x509_ext.cpp b/src/x509_ext.cpp index f692c5ac2..42a5bad01 100644 --- a/src/x509_ext.cpp +++ b/src/x509_ext.cpp @@ -4,8 +4,6 @@ *************************************************/ #include <botan/x509_ext.h> -#include <botan/x509stat.h> -#include <botan/libstate.h> #include <botan/der_enc.h> #include <botan/ber_dec.h> #include <botan/lookup.h> @@ -18,6 +16,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 * *************************************************/ Extensions::Extensions(const Extensions& extensions) : ASN1_Object() @@ -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 fd45c00b3..000000000 --- a/src/x509stat.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/************************************************* -* Globally Saved X.509 State Source * -* (C) 1999-2007 Jack Lloyd * -*************************************************/ - -#include <botan/x509stat.h> -#include <botan/x509_ext.h> -#include <botan/oids.h> - -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(); - } - -} |