diff options
-rw-r--r-- | src/cmd/apps.h | 1 | ||||
-rw-r--r-- | src/cmd/main.cpp | 1 | ||||
-rw-r--r-- | src/cmd/rng.cpp | 51 | ||||
-rw-r--r-- | src/lib/entropy/entropy_src.h | 4 |
4 files changed, 55 insertions, 2 deletions
diff --git a/src/cmd/apps.h b/src/cmd/apps.h index 280ee46a0..d72220322 100644 --- a/src/cmd/apps.h +++ b/src/cmd/apps.h @@ -29,6 +29,7 @@ DEFINE_APP(cert_verify); DEFINE_APP(ocsp_check); DEFINE_APP(pkcs10); DEFINE_APP(read_ssh); +DEFINE_APP(rng); DEFINE_APP(self_sig); DEFINE_APP(tls_client); DEFINE_APP(tls_server); diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp index cd42658b9..92ecc051e 100644 --- a/src/cmd/main.cpp +++ b/src/cmd/main.cpp @@ -161,6 +161,7 @@ int main(int argc, char* argv[]) CALL_APP(fpe); CALL_APP(hash); CALL_APP(keygen); + CALL_APP(rng); CALL_APP(read_ssh); CALL_APP(speed); diff --git a/src/cmd/rng.cpp b/src/cmd/rng.cpp new file mode 100644 index 000000000..e2a7a8270 --- /dev/null +++ b/src/cmd/rng.cpp @@ -0,0 +1,51 @@ +/* +* (C) 2014 Jack Lloyd +* +* Distributed under the terms in doc/license.rst +*/ + +#include "apps.h" +#include <botan/libstate.h> + +int rng_main(int argc, char* argv[]) + { + if(argc == 1) + { + std::cout << "Usage: " << argv[0] << " [--raw-entropy] [n]\n"; + return 1; + } + + try + { + const size_t amt = to_u32bit(argv[argc-1]); + const bool raw = (argc == 3 && std::string(argv[1]) == "--raw-entropy"); + + if(!raw) + { + AutoSeeded_RNG rng; + std::cout << hex_encode(rng.random_vec(amt)) << "\n"; + } + else + { + double total_collected = 0; + + Entropy_Accumulator accum( + [amt,&total_collected](const byte in[], size_t in_len, double entropy_estimate) + { + std::cout << "Collected estimated "<< entropy_estimate << " bits in " + << hex_encode(in, in_len) << "\n"; + total_collected += entropy_estimate; + return total_collected >= amt; + }); + + global_state().poll_available_sources(accum); + } + } + catch(std::exception& e) + { + std::cout << "Error: " << e.what() << "\n"; + return 1; + } + + return 0; + } diff --git a/src/lib/entropy/entropy_src.h b/src/lib/entropy/entropy_src.h index d42f6eca2..1635a936c 100644 --- a/src/lib/entropy/entropy_src.h +++ b/src/lib/entropy/entropy_src.h @@ -24,7 +24,7 @@ class BOTAN_DLL Entropy_Accumulator * Initialize an Entropy_Accumulator * @param goal is how many bits we would like to collect */ - Entropy_Accumulator(std::function<bool (const byte[], size_t, size_t)> accum) : + Entropy_Accumulator(std::function<bool (const byte[], size_t, double)> accum) : m_accum_fn(accum), m_done(false) {} virtual ~Entropy_Accumulator() {} @@ -73,7 +73,7 @@ class BOTAN_DLL Entropy_Accumulator add(&v, sizeof(T), entropy_bits_per_byte); } private: - std::function<bool (const byte[], size_t, size_t)> m_accum_fn; + std::function<bool (const byte[], size_t, double)> m_accum_fn; bool m_done; secure_vector<byte> m_io_buffer; }; |