diff options
-rw-r--r-- | checks/bigint.cpp | 24 | ||||
-rw-r--r-- | checks/check.cpp | 41 | ||||
-rw-r--r-- | checks/common.h | 2 | ||||
-rw-r--r-- | checks/rng.cpp | 14 | ||||
-rw-r--r-- | checks/validate.cpp | 23 | ||||
-rw-r--r-- | checks/validate.h | 9 |
6 files changed, 56 insertions, 57 deletions
diff --git a/checks/bigint.cpp b/checks/bigint.cpp index 518261580..a56fd9181 100644 --- a/checks/bigint.cpp +++ b/checks/bigint.cpp @@ -19,14 +19,17 @@ u32bit check_sub(const std::vector<std::string>&); u32bit check_mul(const std::vector<std::string>&); u32bit check_sqr(const std::vector<std::string>&); u32bit check_div(const std::vector<std::string>&); -u32bit check_mod(const std::vector<std::string>&); +u32bit check_mod(const std::vector<std::string>&, + Botan::RandomNumberGenerator& rng); u32bit check_shr(const std::vector<std::string>&); u32bit check_shl(const std::vector<std::string>&); u32bit check_powmod(const std::vector<std::string>&); -u32bit check_primetest(const std::vector<std::string>&); +u32bit check_primetest(const std::vector<std::string>&, + Botan::RandomNumberGenerator&); -u32bit do_bigint_tests(const std::string& filename) +u32bit do_bigint_tests(const std::string& filename, + Botan::RandomNumberGenerator& rng) { std::ifstream test_data(filename.c_str()); @@ -93,7 +96,7 @@ u32bit do_bigint_tests(const std::string& filename) else if(algorithm.find("Division") != std::string::npos) new_errors = check_div(substr); else if(algorithm.find("Modulo") != std::string::npos) - new_errors = check_mod(substr); + new_errors = check_mod(substr, rng); else if(algorithm.find("LeftShift") != std::string::npos) new_errors = check_shl(substr); else if(algorithm.find("RightShift") != std::string::npos) @@ -101,7 +104,7 @@ u32bit do_bigint_tests(const std::string& filename) else if(algorithm.find("ModExp") != std::string::npos) new_errors = check_powmod(substr); else if(algorithm.find("PrimeTest") != std::string::npos) - new_errors = check_primetest(substr); + new_errors = check_primetest(substr, rng); else std::cout << "Unknown MPI test " << algorithm << std::endl; @@ -249,7 +252,8 @@ u32bit check_div(const std::vector<std::string>& args) return results("/", a, b, c, d, e); } -u32bit check_mod(const std::vector<std::string>& args) +u32bit check_mod(const std::vector<std::string>& args, + Botan::RandomNumberGenerator& rng) { BigInt a(args[0]); BigInt b(args[1]); @@ -268,7 +272,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().next_byte(); + b_word = (b_word << 4) ^ rng.next_byte(); b = b_word; @@ -331,13 +335,13 @@ u32bit check_powmod(const std::vector<std::string>& args) } /* Make sure that n is prime or not prime, according to should_be_prime */ -u32bit check_primetest(const std::vector<std::string>& args) +u32bit check_primetest(const std::vector<std::string>& args, + Botan::RandomNumberGenerator& rng) { BigInt n(args[0]); bool should_be_prime = (args[1] == "1"); - bool is_prime = Botan::verify_prime(n, - global_rng()); + bool is_prime = Botan::verify_prime(n, rng); if(is_prime != should_be_prime) { diff --git a/checks/check.cpp b/checks/check.cpp index 9ef5ee1f9..4494921e3 100644 --- a/checks/check.cpp +++ b/checks/check.cpp @@ -10,6 +10,7 @@ #include <cstring> #include <exception> #include <limits> +#include <memory> #include <botan/botan.h> #include <botan/mp_types.h> @@ -26,7 +27,7 @@ const std::string BIGINT_VALIDATION_FILE = "checks/mp_valid.dat"; const std::string PK_VALIDATION_FILE = "checks/pk_valid.dat"; const std::string EXPECTED_FAIL_FILE = "checks/fail.dat"; -int validate(); +int validate(RandomNumberGenerator& rng); int main(int argc, char* argv[]) { @@ -39,7 +40,8 @@ int main(int argc, char* argv[]) Botan::InitializerOptions init_options(opts.value_if_set("init")); Botan::LibraryInitializer init(init_options); - RandomNumberGenerator& rng = global_rng(); + std::auto_ptr<RandomNumberGenerator> rng( + RandomNumberGenerator::make_rng()); if(opts.is_set("help") || argc <= 1) { @@ -58,7 +60,7 @@ int main(int argc, char* argv[]) } if(opts.is_set("validate")) - return validate(); + return validate(*rng); double seconds = 1.5; @@ -82,32 +84,32 @@ int main(int argc, char* argv[]) for(u32bit j = 0; j != algs.size(); j++) { const std::string alg = algs[j]; - u32bit found = bench_algo(alg, rng, seconds); + u32bit found = bench_algo(alg, *rng, seconds); if(!found) // maybe it's a PK algorithm - bench_pk(global_rng(), alg, html, seconds); + bench_pk(*rng, alg, html, seconds); } } if(opts.is_set("benchmark")) - benchmark("All", rng, html, seconds); + benchmark("All", *rng, html, seconds); else if(opts.is_set("bench-type")) { const std::string type = opts.value("bench-type"); if(type == "all") - benchmark("All", rng, html, seconds); + benchmark("All", *rng, html, seconds); else if(type == "block") - benchmark("Block Cipher", rng, html, seconds); + benchmark("Block Cipher", *rng, html, seconds); else if(type == "stream") - benchmark("Stream Cipher", rng, html, seconds); + benchmark("Stream Cipher", *rng, html, seconds); else if(type == "hash") - benchmark("Hash", rng, html, seconds); + benchmark("Hash", *rng, html, seconds); else if(type == "mac") - benchmark("MAC", rng, html, seconds); - else if(type == "rng") - benchmark("RNG", rng, html, seconds); + benchmark("MAC", *rng, html, seconds); + else if(type == "*rng") + benchmark("RNG", *rng, html, seconds); else if(type == "pk") - bench_pk(rng, "All", html, seconds); + bench_pk(*rng, "All", html, seconds); } } catch(Botan::Exception& e) @@ -186,18 +188,17 @@ void test_types() } -int validate() +int validate(RandomNumberGenerator& rng) { std::cout << "Beginning validation tests..." << std::endl; test_types(); u32bit errors = 0; try { - errors += do_validation_tests(VALIDATION_FILE); - errors += do_validation_tests(EXPECTED_FAIL_FILE, false); - errors += do_bigint_tests(BIGINT_VALIDATION_FILE); - errors += do_pk_validation_tests(PK_VALIDATION_FILE, - global_rng()); + errors += do_validation_tests(VALIDATION_FILE, rng); + errors += do_validation_tests(EXPECTED_FAIL_FILE, rng, false); + errors += do_bigint_tests(BIGINT_VALIDATION_FILE, rng); + errors += do_pk_validation_tests(PK_VALIDATION_FILE, rng); } catch(Botan::Exception& e) { diff --git a/checks/common.h b/checks/common.h index 5a75c9059..e42fa8e44 100644 --- a/checks/common.h +++ b/checks/common.h @@ -55,8 +55,6 @@ Botan::Filter* lookup_s2k(const std::string&, const std::vector<std::string>&); Botan::Filter* lookup_kdf(const std::string&, const std::string&, const std::string&); -Botan::RandomNumberGenerator& global_rng(); - class Fixed_Output_RNG : public Botan::RandomNumberGenerator { public: diff --git a/checks/rng.cpp b/checks/rng.cpp deleted file mode 100644 index a2e53ba63..000000000 --- a/checks/rng.cpp +++ /dev/null @@ -1,14 +0,0 @@ - -#include "common.h" - -using namespace Botan; - -RandomNumberGenerator& global_rng() - { - static RandomNumberGenerator* rng = 0; - - if(!rng) - rng = RandomNumberGenerator::make_rng(); - - return *rng; - } diff --git a/checks/validate.cpp b/checks/validate.cpp index 1871abedd..5c3aca6d5 100644 --- a/checks/validate.cpp +++ b/checks/validate.cpp @@ -20,7 +20,8 @@ using namespace Botan; namespace { -u32bit random_word(u32bit max) +u32bit random_word(Botan::RandomNumberGenerator& rng, + u32bit max) { #if DEBUG /* deterministic version for tracking down buffering bugs */ @@ -35,7 +36,7 @@ u32bit random_word(u32bit max) /* normal version */ u32bit r = 0; for(u32bit j = 0; j != 4; j++) - r = (r << 8) | global_rng().next_byte(); + r = (r << 8) | rng.next_byte(); return ((r % max) + 1); // return between 1 and max inclusive #endif } @@ -46,13 +47,16 @@ Botan::Filter* lookup(const std::string&, const std::vector<std::string>&, const std::string& = "All"); bool failed_test(const std::string&, std::vector<std::string>, bool, bool, - const std::string&, std::string&); + const std::string&, std::string&, + Botan::RandomNumberGenerator& rng); std::vector<std::string> parse(const std::string&); void strip(std::string&); Botan::SecureVector<byte> decode_hex(const std::string&); -u32bit do_validation_tests(const std::string& filename, bool should_pass) +u32bit do_validation_tests(const std::string& filename, + RandomNumberGenerator& rng, + bool should_pass) { std::ifstream test_data(filename.c_str()); bool first_mark = true; @@ -142,7 +146,7 @@ u32bit do_validation_tests(const std::string& filename, bool should_pass) bool failed = failed_test(algorithm, substr, is_extension, should_pass, - section, last_missing); + section, last_missing, rng); if(failed && should_pass) { @@ -168,7 +172,8 @@ bool failed_test(const std::string& algo, std::vector<std::string> params, bool is_extension, bool exp_pass, const std::string& section, - std::string& last_missing) + std::string& last_missing, + Botan::RandomNumberGenerator& rng) { #if DEBUG std::cout << "Testing: " << algo; @@ -221,7 +226,7 @@ bool failed_test(const std::string& algo, pipe.start_msg(); while(len) { - u32bit how_much = random_word(len); + u32bit how_much = random_word(rng, len); pipe.write(data_ptr, how_much); data_ptr += how_much; len -= how_much; @@ -258,8 +263,8 @@ bool failed_test(const std::string& algo, if(pipe.remaining()) { /* Test peeking at an offset in Pipe/SecureQueue */ - u32bit offset = random_word(pipe.remaining() - 1); - u32bit length = random_word(pipe.remaining() - offset); + u32bit offset = random_word(rng, pipe.remaining() - 1); + u32bit length = random_word(rng, pipe.remaining() - offset); Botan::SecureVector<byte> peekbuf(length); pipe.peek(peekbuf, length, offset); diff --git a/checks/validate.h b/checks/validate.h index a562cd4b4..54af39798 100644 --- a/checks/validate.h +++ b/checks/validate.h @@ -5,8 +5,13 @@ #include <botan/rng.h> #include <string> -u32bit do_validation_tests(const std::string&, bool = true); -u32bit do_bigint_tests(const std::string&); +u32bit do_validation_tests(const std::string&, + Botan::RandomNumberGenerator& rng, + bool = true); + +u32bit do_bigint_tests(const std::string&, + Botan::RandomNumberGenerator& rng); + u32bit do_pk_validation_tests(const std::string&, Botan::RandomNumberGenerator&); void do_x509_tests(Botan::RandomNumberGenerator&); |