diff options
author | Jack Lloyd <[email protected]> | 2015-12-19 15:36:40 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2015-12-19 15:36:40 -0500 |
commit | b48e6fb097c62bb246629ee7a182c57e497e4130 (patch) | |
tree | 0cb8ea2d05a89f5e90467f323ae56268d4d3480e /src/lib | |
parent | d774a9edc46ffcebb28205a678058f083ea75c28 (diff) |
CLI rewrite
The command line tools' origin as a collection of examples and test
programs glued together led to some unfortunate problems; lots of
hardcoded values, missing parameters, and obsolete crypto.
Adds a small library for writing command line programs of the sort
needed here (cli.h), which cuts the length of many of the commands in
half and makes commands more pleasant to write and extend.
Generalizes a lot of the commands also, eg previously only
signing/verification with DSA/SHA-1 was included!
Removes the fuzzer entry point since that's fairly useless outside of
an instrumented build.
Removes the in-library API for benchmarking.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/misc/benchmark/benchmark.cpp | 165 | ||||
-rw-r--r-- | src/lib/misc/benchmark/benchmark.h | 35 | ||||
-rw-r--r-- | src/lib/misc/benchmark/info.txt | 1 | ||||
-rw-r--r-- | src/lib/tls/tls_handshake_hash.h | 2 | ||||
-rw-r--r-- | src/lib/utils/stl_util.h | 15 | ||||
-rw-r--r-- | src/lib/utils/version.cpp | 24 | ||||
-rw-r--r-- | src/lib/utils/version.h | 13 |
7 files changed, 50 insertions, 205 deletions
diff --git a/src/lib/misc/benchmark/benchmark.cpp b/src/lib/misc/benchmark/benchmark.cpp deleted file mode 100644 index 4ecca1566..000000000 --- a/src/lib/misc/benchmark/benchmark.cpp +++ /dev/null @@ -1,165 +0,0 @@ -/* -* Runtime benchmarking -* (C) 2008-2009,2013 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#include <botan/benchmark.h> -#include <botan/exceptn.h> -#include <botan/buf_comp.h> -#include <botan/cipher_mode.h> -#include <botan/block_cipher.h> -#include <botan/stream_cipher.h> -#include <botan/hash.h> -#include <botan/mac.h> -#include <vector> -#include <chrono> - -namespace Botan { - -namespace { - -double time_op(std::chrono::nanoseconds runtime, std::function<void ()> op) - { - std::chrono::nanoseconds time_used(0); - size_t reps = 0; - - auto start = std::chrono::high_resolution_clock::now(); - - while(time_used < runtime) - { - op(); - ++reps; - time_used = std::chrono::high_resolution_clock::now() - start; - } - - const u64bit nsec_used = std::chrono::duration_cast<std::chrono::nanoseconds>(time_used).count(); - - const double seconds_used = static_cast<double>(nsec_used) / 1000000000; - - return reps / seconds_used; // ie, return ops per second - } - -std::map<std::string, double> -time_algorithm_ops(const std::string& name, - const std::string& provider, - RandomNumberGenerator& rng, - std::chrono::nanoseconds runtime, - size_t buf_size) - { - const size_t Mebibyte = 1024*1024; - - secure_vector<byte> buffer(buf_size * 1024); - rng.randomize(buffer.data(), buffer.size()); - - const double mb_mult = buffer.size() / static_cast<double>(Mebibyte); - - if(auto bc = BlockCipher::create(name, provider)) - { - const SymmetricKey key(rng, bc->maximum_keylength()); - - return std::map<std::string, double>({ - { "key schedule", time_op(runtime / 8, [&]() { bc->set_key(key); }) }, - { "encrypt", mb_mult * time_op(runtime / 2, [&]() { bc->encrypt(buffer); }) }, - { "decrypt", mb_mult * time_op(runtime / 2, [&]() { bc->decrypt(buffer); }) }, - }); - } - else if(auto sc = StreamCipher::create(name, provider)) - { - const SymmetricKey key(rng, sc->maximum_keylength()); - - return std::map<std::string, double>({ - { "key schedule", time_op(runtime / 8, [&]() { sc->set_key(key); }) }, - { "", mb_mult * time_op(runtime, [&]() { sc->encipher(buffer); }) }, - }); - } - else if(auto h = HashFunction::create(name, provider)) - { - return std::map<std::string, double>({ - { "", mb_mult * time_op(runtime, [&]() { h->update(buffer); }) }, - }); - } - else if(auto mac = MessageAuthenticationCode::create(name, provider)) - { - const SymmetricKey key(rng, mac->maximum_keylength()); - - return std::map<std::string, double>({ - { "key schedule", time_op(runtime / 8, [&]() { mac->set_key(key); }) }, - { "", mb_mult * time_op(runtime, [&]() { mac->update(buffer); }) }, - }); - } - else - { - std::unique_ptr<Cipher_Mode> enc(get_cipher_mode(name, ENCRYPTION)); - std::unique_ptr<Cipher_Mode> dec(get_cipher_mode(name, DECRYPTION)); - - if(enc && dec) - { - const SymmetricKey key(rng, enc->key_spec().maximum_keylength()); - - return std::map<std::string, double>({ - { "key schedule", time_op(runtime / 4, [&]() { enc->set_key(key); dec->set_key(key); }) / 2 }, - { "encrypt", mb_mult * time_op(runtime / 2, [&]() { enc->update(buffer, 0); buffer.resize(buf_size*1024); }) }, - { "decrypt", mb_mult * time_op(runtime / 2, [&]() { dec->update(buffer, 0); buffer.resize(buf_size*1024); }) }, - }); - } - } - - return std::map<std::string, double>(); - } - -double find_first_in(const std::map<std::string, double>& m, - const std::vector<std::string>& keys) - { - for(auto key : keys) - { - auto i = m.find(key); - if(i != m.end()) - return i->second; - } - - throw Exception("In algo benchmark no usable keys found in result"); - } - -std::set<std::string> get_all_providers_of(const std::string& algo) - { - std::set<std::string> provs; - - auto add_to_set = [&provs](const std::vector<std::string>& str) { for(auto&& s : str) { provs.insert(s); } }; - - add_to_set(BlockCipher::providers(algo)); - add_to_set(StreamCipher::providers(algo)); - add_to_set(HashFunction::providers(algo)); - add_to_set(MessageAuthenticationCode::providers(algo)); - - return provs; - } - -} - -std::map<std::string, double> -algorithm_benchmark(const std::string& name, - RandomNumberGenerator& rng, - std::chrono::milliseconds milliseconds, - size_t buf_size) - { - //Algorithm_Factory& af = global_state().algorithm_factory(); - const auto provider_names = get_all_providers_of(name); - if (provider_names.empty()) - throw No_Provider_Found(name); - - std::map<std::string, double> all_results; // provider -> ops/sec - - const std::chrono::nanoseconds ns_per_provider = milliseconds / provider_names.size(); - - for(auto provider : provider_names) - { - auto results = time_algorithm_ops(name, provider, rng, ns_per_provider, buf_size); - all_results[provider] = find_first_in(results, { "", "update", "encrypt" }); - } - - return all_results; - } - -} diff --git a/src/lib/misc/benchmark/benchmark.h b/src/lib/misc/benchmark/benchmark.h deleted file mode 100644 index 3fa020e1b..000000000 --- a/src/lib/misc/benchmark/benchmark.h +++ /dev/null @@ -1,35 +0,0 @@ -/* -* Runtime benchmarking -* (C) 2008-2009 Jack Lloyd -* -* Botan is released under the Simplified BSD License (see license.txt) -*/ - -#ifndef BOTAN_RUNTIME_BENCHMARK_H__ -#define BOTAN_RUNTIME_BENCHMARK_H__ - -#include <botan/rng.h> -#include <map> -#include <string> -#include <chrono> - -namespace Botan { - -/** -* Algorithm benchmark -* @param name the name of the algorithm to test (cipher, hash, or MAC) -* @param af the algorithm factory used to create objects -* @param rng the rng to use to generate random inputs -* @param milliseconds total time for the benchmark to run -* @param buf_size size of buffer to benchmark against, in KiB -* @return results a map from provider to speed in mebibytes per second -*/ -std::map<std::string, double> -BOTAN_DLL algorithm_benchmark(const std::string& name, - RandomNumberGenerator& rng, - std::chrono::milliseconds milliseconds, - size_t buf_size); - -} - -#endif diff --git a/src/lib/misc/benchmark/info.txt b/src/lib/misc/benchmark/info.txt deleted file mode 100644 index 6a26f0059..000000000 --- a/src/lib/misc/benchmark/info.txt +++ /dev/null @@ -1 +0,0 @@ -define RUNTIME_BENCHMARKING 20131128 diff --git a/src/lib/tls/tls_handshake_hash.h b/src/lib/tls/tls_handshake_hash.h index c6b412473..050f3a454 100644 --- a/src/lib/tls/tls_handshake_hash.h +++ b/src/lib/tls/tls_handshake_hash.h @@ -16,8 +16,6 @@ namespace Botan { namespace TLS { -using namespace Botan; - /** * TLS Handshake Hash */ diff --git a/src/lib/utils/stl_util.h b/src/lib/utils/stl_util.h index d74cbe713..12b749c3c 100644 --- a/src/lib/utils/stl_util.h +++ b/src/lib/utils/stl_util.h @@ -12,6 +12,7 @@ #include <vector> #include <string> #include <map> +#include <set> #include <botan/secmem.h> namespace Botan { @@ -26,6 +27,20 @@ inline std::string to_string(const secure_vector<byte> &bytes) return std::string(bytes.cbegin(), bytes.cend()); } +/** +* Return the keys of a map as a std::set +*/ +template<typename K, typename V> +std::set<K> map_keys_as_set(const std::map<K, V>& kv) + { + std::set<K> s; + for(auto&& i : kv) + { + s.insert(i.first); + } + return s; + } + /* * Searching through a std::map * @param mapping the map to search diff --git a/src/lib/utils/version.cpp b/src/lib/utils/version.cpp index f3e01e290..8e14cc62f 100644 --- a/src/lib/utils/version.cpp +++ b/src/lib/utils/version.cpp @@ -1,12 +1,13 @@ /* * Version Information -* (C) 1999-2013 Jack Lloyd +* (C) 1999-2013,2015 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ #include <botan/version.h> #include <botan/parsing.h> +#include <sstream> namespace Botan { @@ -57,4 +58,25 @@ u32bit version_major() { return BOTAN_VERSION_MAJOR; } u32bit version_minor() { return BOTAN_VERSION_MINOR; } u32bit version_patch() { return BOTAN_VERSION_PATCH; } +std::string runtime_version_check(u32bit major, + u32bit minor, + u32bit patch) + { + std::ostringstream oss; + + if(major != version_major() || + minor != version_minor() || + patch != version_patch()) + { + oss << "Warning: linked version (" + << Botan::version_major() << '.' + << Botan::version_minor() << '.' + << Botan::version_patch() + << ") does not match version built against (" + << major << '.' << minor << '.' << patch << ")\n"; + } + + return oss.str(); + } + } diff --git a/src/lib/utils/version.h b/src/lib/utils/version.h index 3fc6f5fe7..406deae66 100644 --- a/src/lib/utils/version.h +++ b/src/lib/utils/version.h @@ -1,6 +1,6 @@ /* * Version Information -* (C) 1999-2011 Jack Lloyd +* (C) 1999-2011,2015 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -54,6 +54,17 @@ BOTAN_DLL u32bit version_minor(); */ BOTAN_DLL u32bit version_patch(); +/** +* Usable for checking that the DLL version loaded at runtime exactly +* matches the compile-time version. Call using BOTAN_VERSION_* macro +* values. Returns the empty string if an exact match, otherwise an +* appropriate message. @added 1.11.26 +*/ +BOTAN_DLL std::string +runtime_version_check(u32bit major, + u32bit minor, + u32bit patch); + /* * Macros for compile-time version checks */ |