aboutsummaryrefslogtreecommitdiffstats
path: root/checks/bench.cpp
blob: 3be61462a58ecf1423d4391f9a195e385911bc53 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

#include <iostream>
#include <iomanip>

#include <botan/benchmark.h>
#include <botan/libstate.h>
#include <botan/pipe.h>
#include <botan/filters.h>
#include <botan/engine.h>
#include <botan/parsing.h>
#include <botan/symkey.h>
#include <botan/time.h>

#include "common.h"
#include "bench.h"

namespace {

const std::string algos[] = {

   /* Block ciphers */
   "AES-128",
   "AES-192",
   "AES-256",
   "Blowfish",
   "CAST-128",
   "CAST-256",
   "DES",
   "DESX",
   "GOST",
   "IDEA",
   "KASUMI",
   "Lion(SHA-256,Turing,8192)",
   "Luby-Rackoff(SHA-512)",
   "MARS",
   "MISTY1",
   "Noekeon",
   "RC2",
   "RC5(12)",
   "RC5(16)",
   "RC6",
   "SAFER-SK(10)",
   "SEED",
   "Serpent",
   "Skipjack",
   "Square",
   "TEA",
   "TripleDES",
   "Twofish",
   "XTEA",

   /* Cipher modes */
   "TripleDES/CBC/PKCS7",
   "TripleDES/CBC/CTS",
   "TripleDES/CTR-BE",
   "TripleDES/EAX",
   "TripleDES/OFB",
   "TripleDES/CFB(64)",
   "TripleDES/CFB(32)",
   "TripleDES/CFB(16)",
   "TripleDES/CFB(8)",

   "AES-128/CBC/PKCS7",
   "AES-128/CBC/CTS",
   "AES-128/CTR-BE",
   "AES-128/EAX",
   "AES-128/OFB",
   "AES-128/XTS",
   "AES-128/CFB(128)",
   "AES-128/CFB(64)",
   "AES-128/CFB(32)",
   "AES-128/CFB(16)",
   "AES-128/CFB(8)",

   "Serpent/CBC/PKCS7",
   "Serpent/CBC/CTS",
   "Serpent/CTR-BE",
   "Serpent/EAX",
   "Serpent/OFB",
   "Serpent/XTS",
   "Serpent/CFB(128)",
   "Serpent/CFB(64)",
   "Serpent/CFB(32)",
   "Serpent/CFB(16)",
   "Serpent/CFB(8)",

   /* Stream ciphers */
   "ARC4",
   "Salsa20",
   "Turing",
   "WiderWake4+1-BE",

   /* Checksums */
   "Adler32",
   "CRC24",
   "CRC32",

   /* Hashes */
   "BMW-512",
   "FORK-256",
   "GOST-34.11",
   "HAS-160",
   "MD2",
   "MD4",
   "MD5",
   "RIPEMD-128",
   "RIPEMD-160",
   "SHA-160",
   "SHA-256",
   "SHA-384",
   "SHA-512",
   "Skein-512",
   "Tiger",
   "Whirlpool",

   /* MACs */
   "CMAC(AES-128)",
   "HMAC(SHA-1)",
   "X9.19-MAC",
   "",
};

void report_results(const std::string& algo,
                    const std::map<std::string, double>& speeds)
   {
   // invert, showing fastest impl first
   std::map<double, std::string> results;

   for(std::map<std::string, double>::const_iterator 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;

   for(std::map<double, std::string>::const_reverse_iterator i = results.rbegin();
       i != results.rend(); ++i)
      {
      std::cout << " [" << i->second << "] "
                << std::fixed << std::setprecision(2) << i->first;
      }
   std::cout << std::endl;
   }

}

bool bench_algo(const std::string& algo,
                Botan::RandomNumberGenerator& rng,
                double seconds)
   {
   Botan::Algorithm_Factory& af = Botan::global_state().algorithm_factory();

   u32bit milliseconds = static_cast<u32bit>(seconds * 1000);

   std::map<std::string, double> speeds =
      algorithm_benchmark(algo, milliseconds, rng, af);

   if(speeds.empty()) // maybe a cipher mode, then?
      {
      Botan::Algorithm_Factory::Engine_Iterator i(af);

      std::vector<std::string> algo_parts = Botan::split_on(algo, '/');

      if(algo_parts.size() < 2) // not a cipher mode
         return false;

      std::string cipher = algo_parts[0];

      u32bit cipher_keylen =
         af.prototype_block_cipher(cipher)->MAXIMUM_KEYLENGTH;
      u32bit cipher_ivlen =
         af.prototype_block_cipher(cipher)->BLOCK_SIZE;

      if(algo_parts[1] == "XTS")
         cipher_keylen *= 2; // hack!

      std::vector<byte> buf(16 * 1024);
      rng.randomize(&buf[0], buf.size());

      while(Botan::Engine* engine = i.next())
         {
         u64bit nanoseconds_max = static_cast<u64bit>(seconds * 1000000000.0);

         Botan::Keyed_Filter* filt =
            engine->get_cipher(algo, Botan::ENCRYPTION, af);

         if(!filt)
            continue;

         filt->set_key(Botan::SymmetricKey(&buf[0], cipher_keylen));
         filt->set_iv(Botan::InitializationVector(&buf[0], cipher_ivlen));

         Botan::Pipe pipe(filt, new Botan::BitBucket);
         pipe.start_msg();

         const u64bit start = Botan::get_nanoseconds_clock();
         u64bit nanoseconds_used = 0;
         u64bit reps = 0;

         while(nanoseconds_used < nanoseconds_max)
            {
            pipe.write(&buf[0], buf.size());
            ++reps;
            nanoseconds_used = Botan::get_nanoseconds_clock() - start;
            }

         double mbytes_per_second =
            (953.67 * (buf.size() * reps)) / nanoseconds_used;

         speeds[engine->provider_name()] = mbytes_per_second;
         }
      }

   if(!speeds.empty())
      report_results(algo, speeds);

   return !speeds.empty();
   }

void benchmark(Botan::RandomNumberGenerator& rng,
               double seconds)
   {
   for(u32bit i = 0; algos[i] != ""; ++i)
      bench_algo(algos[i], rng, seconds);
   }