diff options
author | Jack Lloyd <[email protected]> | 2016-01-17 19:11:28 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-01-17 19:11:28 -0500 |
commit | 6adce3dcdd8572b4e7091feedcfcf435ca553ad3 (patch) | |
tree | 0e9119d00fdfc16f655371890b7b6c12abcdcd57 /src/cli | |
parent | 6fcbd57915d7870c2782bd6e098903353e8756b0 (diff) |
Add speed test for entropy sources
Diffstat (limited to 'src/cli')
-rw-r--r-- | src/cli/speed.cpp | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/src/cli/speed.cpp b/src/cli/speed.cpp index e044003d9..79f657f98 100644 --- a/src/cli/speed.cpp +++ b/src/cli/speed.cpp @@ -18,6 +18,7 @@ #include <botan/mac.h> #include <botan/cipher_mode.h> #include <botan/auto_rng.h> +#include <botan/entropy_src.h> #if defined(BOTAN_HAS_SYSTEM_RNG) #include <botan/system_rng.h> @@ -353,6 +354,10 @@ class Speed final : public Command Botan::AutoSeeded_RNG auto_rng; bench_rng(auto_rng, "AutoSeeded_RNG", msec, buf_size); } + else if(algo == "entropy") + { + bench_entropy_sources(msec); + } else { if(verbose() || !using_defaults) @@ -506,17 +511,37 @@ class Speed final : public Command size_t buf_size) { Botan::secure_vector<uint8_t> buffer(buf_size); - Timer timer(rng_name, "", "bytes", buffer.size()); + timer.run_until_elapsed(runtime, [&] { rng.randomize(buffer.data(), buffer.size()); }); + output() << Timer::result_string_bps(timer); + } - while(timer.under(runtime)) + void bench_entropy_sources(const std::chrono::milliseconds runtime) + { + Botan::Entropy_Sources& srcs = Botan::Entropy_Sources::global_sources(); + + typedef std::chrono::system_clock clock; + + auto deadline = clock::now() + runtime; + + for(auto src : srcs.enabled_sources()) { - timer.run([&] { rng.randomize(buffer.data(), buffer.size()); }); + size_t bytes = 0, entropy_bits = 0, samples = 0; + Botan::Entropy_Accumulator accum( + [&](const uint8_t [], size_t buf_len, size_t buf_entropy) -> bool { + bytes += buf_len; + entropy_bits += buf_entropy; + samples += 1; + return (samples > 100 || entropy_bits > 512 || clock::now() > deadline); + }); - //timer.run([&] { for(size_t i = 0; i != buffer.size(); i++) buffer[i] = rng.next_byte(); }); - } + Timer timer(src, "", "bytes"); + timer.run([&] { srcs.poll_just(accum, src); }); - output() << Timer::result_string_bps(timer); + output() << "Entropy source " << src << " produced " << bytes << " bytes with " + << entropy_bits << " estimated bits of entropy in " + << samples << " samples in " << timer.milliseconds() << " ms\n"; + } } #if defined(BOTAN_HAS_NUMBERTHEORY) |