8 #include <botan/benchmark.h>     9 #include <botan/buf_comp.h>    10 #include <botan/block_cipher.h>    11 #include <botan/stream_cipher.h>    12 #include <botan/hash.h>    13 #include <botan/mac.h>    14 #include <botan/time.h>    24 std::pair<u64bit, u64bit> bench_buf_comp(Buffered_Computation* buf_comp,
    26                                          const byte buf[], 
size_t buf_len)
    29    u64bit nanoseconds_used = 0;
    31    while(nanoseconds_used < nanoseconds_max)
    34       buf_comp->update(buf, buf_len);
    40    return std::make_pair(reps * buf_len, nanoseconds_used);
    46 std::pair<u64bit, u64bit>
    47 bench_block_cipher(BlockCipher* block_cipher,
    49                    byte buf[], 
size_t buf_len)
    51    const size_t in_blocks = buf_len / block_cipher->block_size();
    54    u64bit nanoseconds_used = 0;
    56    block_cipher->set_key(buf, block_cipher->maximum_keylength());
    58    while(nanoseconds_used < nanoseconds_max)
    61       block_cipher->encrypt_n(buf, buf, in_blocks);
    67    return std::make_pair(reps * in_blocks * block_cipher->block_size(),
    74 std::pair<u64bit, u64bit>
    75 bench_stream_cipher(StreamCipher* stream_cipher,
    77                     byte buf[], 
size_t buf_len)
    80    u64bit nanoseconds_used = 0;
    82    stream_cipher->set_key(buf, stream_cipher->maximum_keylength());
    84    while(nanoseconds_used < nanoseconds_max)
    87       stream_cipher->cipher1(buf, buf_len);
    93    return std::make_pair(reps * buf_len, nanoseconds_used);
    99 std::pair<u64bit, u64bit>
   100 bench_hash(HashFunction* hash,
   102            const byte buf[], 
size_t buf_len)
   104    return bench_buf_comp(hash, nanoseconds_max, buf, buf_len);
   110 std::pair<u64bit, u64bit>
   111 bench_mac(MessageAuthenticationCode* mac,
   113           const byte buf[], 
size_t buf_len)
   115    mac->set_key(buf, mac->maximum_keylength());
   116    return bench_buf_comp(mac, nanoseconds_max, buf, buf_len);
   121 std::map<std::string, double>
   128    std::vector<std::string> providers = af.
providers_of(name);
   129    std::map<std::string, double> all_results;
   131    if(providers.empty()) 
   134    const u64bit ns_per_provider =
   135       (
static_cast<u64bit>(milliseconds) * 1000 * 1000) / providers.size();
   137    std::vector<byte> buf(buf_size * 1024);
   140    for(
size_t i = 0; i != providers.size(); ++i)
   142       const std::string provider = providers[i];
   144       std::pair<u64bit, u64bit> results(0, 0);
   149          std::auto_ptr<BlockCipher> block_cipher(proto->clone());
   150          results = bench_block_cipher(block_cipher.get(),
   152                                       &buf[0], buf.size());
   157          std::auto_ptr<StreamCipher> stream_cipher(proto->clone());
   158          results = bench_stream_cipher(stream_cipher.get(),
   160                                        &buf[0], buf.size());
   165          std::auto_ptr<HashFunction> hash(proto->clone());
   166          results = bench_hash(hash.get(), ns_per_provider,
   167                               &buf[0], buf.size());
   172          std::auto_ptr<MessageAuthenticationCode> mac(proto->clone());
   173          results = bench_mac(mac.get(), ns_per_provider,
   174                              &buf[0], buf.size());
   177       if(results.first && results.second)
   182          double speed = (953.67 * results.first) / results.second;
   183          all_results[provider] = speed;
 virtual void randomize(byte output[], size_t length)=0
u64bit get_nanoseconds_clock()
const BlockCipher * prototype_block_cipher(const std::string &algo_spec, const std::string &provider="")
const HashFunction * prototype_hash_function(const std::string &algo_spec, const std::string &provider="")
unsigned long long u64bit
const MessageAuthenticationCode * prototype_mac(const std::string &algo_spec, const std::string &provider="")
std::vector< std::string > providers_of(const std::string &algo_spec)
std::map< std::string, double > algorithm_benchmark(const std::string &name, Algorithm_Factory &af, RandomNumberGenerator &rng, u32bit milliseconds, size_t buf_size)
const StreamCipher * prototype_stream_cipher(const std::string &algo_spec, const std::string &provider="")