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. --- src/dsa_gen.cpp | 8 -------- 1 file changed, 8 deletions(-) (limited to 'src/dsa_gen.cpp') 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; -- 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 (limited to 'src/dsa_gen.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