aboutsummaryrefslogtreecommitdiffstats
path: root/src/cmd/speed.cpp
blob: e8d30c6f1666fdd5004532780e60c19651ad0e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* (C) 2009 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include "apps.h"

#if defined(BOTAN_HAS_RUNTIME_BENCHMARKING)

#include "implementation/speed.h"

#include <iostream>
#include <iomanip>

#include <botan/benchmark.h>
#include <botan/auto_rng.h>

using namespace Botan;

namespace {
const std::vector<std::string> default_benchmark_list = {
   /* Block ciphers */
   "AES-128",
   "AES-192",
   "AES-256",
   "Blowfish",
   "CAST-128",
   "CAST-256",
   "DES",
   "IDEA",
   "KASUMI",
   "MARS",
   "MISTY1",
   "Noekeon",
   "RC2",
   "RC5(16)",
   "RC6",
   "SAFER-SK(10)",
   "SEED",
   "Serpent",
   "Skipjack",
   "Square",
   "TEA",
   "TripleDES",
   "Threefish-512",
   "Twofish",
   "XTEA",

   /* Cipher modes */
   "AES-128/CBC",
   "AES-128/CTR-BE",
   "AES-128/EAX",
   "AES-128/OCB",
   "AES-128/GCM",
   "AES-128/XTS",

   "Serpent/CBC",
   "Serpent/CTR-BE",
   "Serpent/EAX",
   "Serpent/OCB",
   "Serpent/GCM",
   "Serpent/XTS",

   /* Stream ciphers */
   "RC4",
   "Salsa20",

   /* Hashes */
   "Keccak-1600(512)",
   "MD5",
   "RIPEMD-160",
   "SHA-160",
   "SHA-256",
   "SHA-384",
   "SHA-512",
   "Skein-512",
   "Tiger",
   "Whirlpool",

   /* MACs */
   "CMAC(AES-128)",
   "HMAC(SHA-1)",

   /* Misc */
   "is_prime",
   "random_prime"
};

void report_results(const std::string& algo,
                    const std::map<std::string, double>& speeds)
   {
   if(speeds.empty())
      return;

   // invert, showing fastest impl first
   std::map<double, std::string> results;

   for(auto i = speeds.begin(); i != speeds.end(); ++i)
      {
      // Speeds might collide, tweak slightly to handle this
      if(results[i->second] == "")
         results[i->second] = i->first;
      else
         results[i->second - .01] = i->first;
      }

   std::cout << algo;

   const std::ios::fmtflags flags = std::cout.flags();
   for(auto i = results.rbegin(); i != results.rend(); ++i)
      {
      std::cout << " [" << i->second << "] "
                << std::fixed << std::setprecision(2) << i->first;
      }
   std::cout << std::endl;
   std::cout.flags(flags);
   }

void bench_algo(const std::string& algo,
                const std::string& provider,
                RandomNumberGenerator& rng,
                double seconds,
                size_t buf_size)
   {
   std::chrono::milliseconds runtime(
        static_cast<std::chrono::milliseconds::rep>(seconds * 1000));

   if (algo == "random_prime")
   {
       auto speeds = benchmark_random_prime(rng, runtime);
       report_results(algo, speeds);
       return;
   }

   if (algo == "is_prime")
   {
       auto speeds = benchmark_is_prime(rng, runtime);
       report_results(algo, speeds);
       return;
   }

   // This does report itself
   if (benchmark_transform(rng, algo, runtime))
      return;

   try
      {
      auto speeds = algorithm_benchmark(algo, rng, runtime, buf_size);
      report_results(algo, speeds);
      }
   catch (No_Provider_Found)
      {
      #if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
      benchmark_public_key(rng, algo, provider, seconds);
      #endif
      }
   }

int speed(const std::vector<std::string> &args)
   {
   OptionParser opts("seconds=|buf-size=|provider=");
   opts.parse(args);

   double seconds = .5;
   u32bit buf_size = 16;

   if(opts.is_set("seconds"))
      {
      seconds = std::atof(opts.value("seconds").c_str());
      if(seconds < 0.1 || seconds > (5 * 60))
         {
         std::cout << "Invalid argument to --seconds" << std::endl;
         return 2;
         }
      }

   if(opts.is_set("buf-size"))
      {
      buf_size = std::atoi(opts.value("buf-size").c_str());
      if(buf_size == 0 || buf_size > 1024)
         {
         std::cout << "Invalid argument to --buf-size" << std::endl;
         return 2;
         }
      }

   const std::string provider = opts.value_if_set("provider");

   std::vector<std::string> opt_args = opts.arguments();

   if(opt_args.empty())
      opt_args = default_benchmark_list;

   if(opt_args[0] == "help" || opt_args[0] == "-h")
      {
      std::cout << "Usage: " << args[0] << " [algo name...]" << std::endl;
      return 1;
      }

   AutoSeeded_RNG rng;

   for(auto alg: opt_args)
      {
      bench_algo(alg, provider, rng, seconds, buf_size);
      }

   return 0;
   }

REGISTER_APP(speed);

}
#endif // BOTAN_HAS_RUNTIME_BENCHMARKING