Crypto++  5.6.3
Free C++ class library of cryptographic schemes
bench.cpp
1 // bench.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "cryptlib.h"
4 #include "bench.h"
5 #include "validate.h"
6 
7 #include "aes.h"
8 #include "blumshub.h"
9 #include "files.h"
10 #include "filters.h"
11 #include "hex.h"
12 #include "modes.h"
13 #include "factory.h"
14 #include "smartptr.h"
15 #include "cpu.h"
16 
17 #include <time.h>
18 #include <math.h>
19 #include <iostream>
20 #include <sstream>
21 #include <iomanip>
22 
23 // These are noisy enoguh due to test.cpp. Turn them off here.
24 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
25 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
26 #endif
27 
28 USING_NAMESPACE(CryptoPP)
29 USING_NAMESPACE(std)
30 
31 #ifdef CLOCKS_PER_SEC
32 const double CLOCK_TICKS_PER_SECOND = (double)CLOCKS_PER_SEC;
33 #elif defined(CLK_TCK)
34 const double CLOCK_TICKS_PER_SECOND = (double)CLK_TCK;
35 #else
36 const double CLOCK_TICKS_PER_SECOND = 1000000.0;
37 #endif
38 
39 double logtotal = 0.0, g_allocatedTime = 0, g_hertz = 0;
40 unsigned int logcount = 0;
41 
42 static const byte defaultKey[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
43  "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
44 
45 void OutputResultBytes(const char *name, double length, double timeTaken)
46 {
47  // Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
48  StreamState ss(cout);
49 
50  // Coverity finding
51  if (length < 0.0000000001f) length = 0.000001f;
52  if (timeTaken < 0.0000000001f) timeTaken = 0.000001f;
53 
54  double mbs = length / timeTaken / (1024*1024);
55  cout << "\n<TR><TH>" << name;
56 // cout << "<TD>" << setprecision(3) << length / (1024*1024);
57  cout << setiosflags(ios::fixed);
58 // cout << "<TD>" << setprecision(3) << timeTaken;
59  cout << "<TD>" << setprecision(0) << setiosflags(ios::fixed) << mbs;
60  if (g_hertz)
61  cout << "<TD>" << setprecision(1) << setiosflags(ios::fixed) << timeTaken * g_hertz / length;
62  logtotal += log(mbs);
63  logcount++;
64 }
65 
66 void OutputResultKeying(double iterations, double timeTaken)
67 {
68  // Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
69  StreamState ss(cout);
70 
71  // Coverity finding
72  if (iterations < 0.0000000001f) iterations = 0.000001f;
73  if (timeTaken < 0.0000000001f) timeTaken = 0.000001f;
74 
75  cout << "<TD>" << setprecision(3) << setiosflags(ios::fixed) << (1000*1000*timeTaken/iterations);
76  if (g_hertz)
77  cout << "<TD>" << setprecision(0) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations;
78 }
79 
80 void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken)
81 {
82  // Coverity finding, also see http://stackoverflow.com/a/34509163/608639.
83  StreamState ss(cout);
84 
85  // Coverity finding
86  if (!iterations) iterations++;
87  if (timeTaken < 0.0000000001f) timeTaken = 0.000001f;
88 
89  cout << "\n<TR><TH>" << name << " " << operation << (pc ? " with precomputation" : "");
90  cout << "<TD>" << setprecision(2) << setiosflags(ios::fixed) << (1000*timeTaken/iterations);
91  if (g_hertz)
92  cout << "<TD>" << setprecision(2) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations / 1000000;
93 
94  logtotal += log(iterations/timeTaken);
95  logcount++;
96 }
97 
98 /*
99 void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal)
100 {
101  const int BUF_SIZE = RoundUpToMultipleOf(2048U, cipher.OptimalNumberOfParallelBlocks() * cipher.BlockSize());
102  AlignedSecByteBlock buf(BUF_SIZE);
103  const int nBlocks = BUF_SIZE / cipher.BlockSize();
104  clock_t start = clock();
105 
106  unsigned long i=0, blocks=1;
107  double timeTaken;
108  do
109  {
110  blocks *= 2;
111  for (; i<blocks; i++)
112  cipher.ProcessAndXorMultipleBlocks(buf, NULL, buf, nBlocks);
113  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
114  }
115  while (timeTaken < 2.0/3*timeTotal);
116 
117  OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
118 }
119 */
120 
121 void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
122 {
123  const int BUF_SIZE=RoundUpToMultipleOf(2048U, cipher.OptimalBlockSize());
124  AlignedSecByteBlock buf(BUF_SIZE);
125  GlobalRNG().GenerateBlock(buf, BUF_SIZE);
126  clock_t start = clock();
127 
128  unsigned long i=0, blocks=1;
129  double timeTaken;
130  do
131  {
132  blocks *= 2;
133  for (; i<blocks; i++)
134  cipher.ProcessString(buf, BUF_SIZE);
135  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
136  }
137  while (timeTaken < 2.0/3*timeTotal);
138 
139  OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
140 }
141 
142 void BenchMark(const char *name, AuthenticatedSymmetricCipher &cipher, double timeTotal)
143 {
144  if (cipher.NeedsPrespecifiedDataLengths())
145  cipher.SpecifyDataLengths(0, cipher.MaxMessageLength(), 0);
146 
147  BenchMark(name, static_cast<StreamTransformation &>(cipher), timeTotal);
148 }
149 
150 void BenchMark(const char *name, HashTransformation &ht, double timeTotal)
151 {
152  const int BUF_SIZE=2048U;
153  AlignedSecByteBlock buf(BUF_SIZE);
154  GlobalRNG().GenerateBlock(buf, BUF_SIZE);
155  clock_t start = clock();
156 
157  unsigned long i=0, blocks=1;
158  double timeTaken;
159  do
160  {
161  blocks *= 2;
162  for (; i<blocks; i++)
163  ht.Update(buf, BUF_SIZE);
164  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
165  }
166  while (timeTaken < 2.0/3*timeTotal);
167 
168  OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
169 }
170 
171 void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
172 {
173  const int BUF_SIZE=2048U;
174  AlignedSecByteBlock buf(BUF_SIZE);
175  GlobalRNG().GenerateBlock(buf, BUF_SIZE);
176  clock_t start = clock();
177 
178  unsigned long i=0, blocks=1;
179  double timeTaken;
180  do
181  {
182  blocks *= 2;
183  for (; i<blocks; i++)
184  bt.Put(buf, BUF_SIZE);
185  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
186  }
187  while (timeTaken < 2.0/3*timeTotal);
188 
189  OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
190 }
191 
192 void BenchMarkKeying(SimpleKeyingInterface &c, size_t keyLength, const NameValuePairs &params)
193 {
194  unsigned long iterations = 0;
195  clock_t start = clock();
196  double timeTaken;
197  do
198  {
199  for (unsigned int i=0; i<1024; i++)
200  c.SetKey(defaultKey, keyLength, params);
201  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
202  iterations += 1024;
203  }
204  while (timeTaken < g_allocatedTime);
205 
206  OutputResultKeying(iterations, timeTaken);
207 }
208 
209 //VC60 workaround: compiler bug triggered without the extra dummy parameters
210 // on VC60 also needs to be named differently from BenchMarkByName
211 template <class T_FactoryOutput, class T_Interface>
212 void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs, T_FactoryOutput *x=NULL, T_Interface *y=NULL)
213 {
214  CRYPTOPP_UNUSED(x), CRYPTOPP_UNUSED(y), CRYPTOPP_UNUSED(params);
215 
216  std::string name(factoryName ? factoryName : "");
218 
219  if (!keyLength)
220  keyLength = obj->DefaultKeyLength();
221 
222  if (displayName)
223  name = displayName;
224  else if (keyLength)
225  name += " (" + IntToString(keyLength * 8) + "-bit key)";
226 
227  obj->SetKey(defaultKey, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false)));
228  BenchMark(name.c_str(), *static_cast<T_Interface *>(obj.get()), g_allocatedTime);
229  BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false)));
230 }
231 
232 //VC60 workaround: compiler bug triggered without the extra dummy parameters
233 template <class T_FactoryOutput>
234 void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs, T_FactoryOutput *x=NULL)
235 {
236  CRYPTOPP_UNUSED(x), CRYPTOPP_UNUSED(params);
237 
238  BenchMarkByName2<T_FactoryOutput, T_FactoryOutput>(factoryName, keyLength, displayName, params, x, x);
239 }
240 
241 template <class T>
242 void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs, T *x=NULL)
243 {
244  CRYPTOPP_UNUSED(x), CRYPTOPP_UNUSED(params);
245 
246  std::string name = factoryName;
247  if (displayName)
248  name = displayName;
249 
250  member_ptr<T> obj(ObjectFactoryRegistry<T>::Registry().CreateObject(factoryName));
251  BenchMark(name.c_str(), *obj, g_allocatedTime);
252 }
253 
254 void BenchmarkAll(double t, double hertz)
255 {
256 #if 1
257  logtotal = 0;
258  logcount = 0;
259  g_allocatedTime = t;
260  g_hertz = hertz;
261 
262  const char *cpb, *cpk;
263  if (g_hertz)
264  {
265  cpb = "<TH>Cycles Per Byte";
266  cpk = "<TH>Cycles to<br>Setup Key and IV";
267  cout << "CPU frequency of the test platform is " << g_hertz << " Hz.\n";
268  }
269  else
270  {
271  cpb = cpk = "";
272  cout << "CPU frequency of the test platform was not provided.\n";
273  }
274 
275  cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right><COL align=right><COL align=right>" << endl;
276  cout << "<THEAD><TR><TH>Algorithm<TH>MiB/Second" << cpb << "<TH>Microseconds to<br>Setup Key and IV" << cpk << endl;
277 
278  cout << "\n<TBODY style=\"background: yellow\">";
279 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
280  if (HasCLMUL())
281  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM");
282  else
283 #endif
284  {
285  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (2K tables)", MakeParameters(Name::TableSize(), 2048));
286  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
287  }
288  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/CCM");
289  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/EAX");
290 
291  cout << "\n<TBODY style=\"background: white\">";
292 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
293  if (HasCLMUL())
294  BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES)");
295  else
296 #endif
297  {
298  BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (2K tables)", MakeParameters(Name::TableSize(), 2048));
299  BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
300  }
301  BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-64");
302  BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-128");
303  BenchMarkByName<MessageAuthenticationCode>("HMAC(SHA-1)");
304  BenchMarkByName<MessageAuthenticationCode>("Two-Track-MAC");
305  BenchMarkByName<MessageAuthenticationCode>("CMAC(AES)");
306  BenchMarkByName<MessageAuthenticationCode>("DMAC(AES)");
307 
308  cout << "\n<TBODY style=\"background: yellow\">";
309  BenchMarkByNameKeyLess<HashTransformation>("CRC32");
310  BenchMarkByNameKeyLess<HashTransformation>("Adler32");
311  BenchMarkByNameKeyLess<HashTransformation>("MD5");
312  BenchMarkByNameKeyLess<HashTransformation>("SHA-1");
313  BenchMarkByNameKeyLess<HashTransformation>("SHA-256");
314  BenchMarkByNameKeyLess<HashTransformation>("SHA-512");
315  BenchMarkByNameKeyLess<HashTransformation>("SHA-3-224");
316  BenchMarkByNameKeyLess<HashTransformation>("SHA-3-256");
317  BenchMarkByNameKeyLess<HashTransformation>("SHA-3-384");
318  BenchMarkByNameKeyLess<HashTransformation>("SHA-3-512");
319  BenchMarkByNameKeyLess<HashTransformation>("Tiger");
320  BenchMarkByNameKeyLess<HashTransformation>("Whirlpool");
321  BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-160");
322  BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-320");
323  BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-128");
324  BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-256");
325 
326  cout << "\n<TBODY style=\"background: white\">";
327  BenchMarkByName<SymmetricCipher>("Panama-LE");
328  BenchMarkByName<SymmetricCipher>("Panama-BE");
329  BenchMarkByName<SymmetricCipher>("Salsa20");
330  BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/12", MakeParameters(Name::Rounds(), 12));
331  BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/8", MakeParameters(Name::Rounds(), 8));
332  BenchMarkByName<SymmetricCipher>("Sosemanuk");
333  BenchMarkByName<SymmetricCipher>("MARC4");
334  BenchMarkByName<SymmetricCipher>("SEAL-3.0-LE");
335  BenchMarkByName<SymmetricCipher>("WAKE-OFB-LE");
336 
337  cout << "\n<TBODY style=\"background: yellow\">";
338  BenchMarkByName<SymmetricCipher>("AES/CTR", 16);
339  BenchMarkByName<SymmetricCipher>("AES/CTR", 24);
340  BenchMarkByName<SymmetricCipher>("AES/CTR", 32);
341  BenchMarkByName<SymmetricCipher>("AES/CBC", 16);
342  BenchMarkByName<SymmetricCipher>("AES/CBC", 24);
343  BenchMarkByName<SymmetricCipher>("AES/CBC", 32);
344  BenchMarkByName<SymmetricCipher>("AES/OFB", 16);
345  BenchMarkByName<SymmetricCipher>("AES/CFB", 16);
346  BenchMarkByName<SymmetricCipher>("AES/ECB", 16);
347  BenchMarkByName<SymmetricCipher>("Camellia/CTR", 16);
348  BenchMarkByName<SymmetricCipher>("Camellia/CTR", 32);
349  BenchMarkByName<SymmetricCipher>("Twofish/CTR");
350  BenchMarkByName<SymmetricCipher>("Serpent/CTR");
351  BenchMarkByName<SymmetricCipher>("CAST-256/CTR");
352  BenchMarkByName<SymmetricCipher>("RC6/CTR");
353  BenchMarkByName<SymmetricCipher>("MARS/CTR");
354  BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 16);
355  BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 64);
356  BenchMarkByName<SymmetricCipher>("DES/CTR");
357  BenchMarkByName<SymmetricCipher>("DES-XEX3/CTR");
358  BenchMarkByName<SymmetricCipher>("DES-EDE3/CTR");
359  BenchMarkByName<SymmetricCipher>("IDEA/CTR");
360  BenchMarkByName<SymmetricCipher>("RC5/CTR", 0, "RC5 (r=16)");
361  BenchMarkByName<SymmetricCipher>("Blowfish/CTR");
362  BenchMarkByName<SymmetricCipher>("TEA/CTR");
363  BenchMarkByName<SymmetricCipher>("XTEA/CTR");
364  BenchMarkByName<SymmetricCipher>("CAST-128/CTR");
365  BenchMarkByName<SymmetricCipher>("SKIPJACK/CTR");
366  BenchMarkByName<SymmetricCipher>("SEED/CTR", 0, "SEED/CTR (1/2 K table)");
367  cout << "</TABLE>" << endl;
368 
369  BenchmarkAll2(t, hertz);
370  cout << "Throughput Geometric Average: " << setiosflags(ios::fixed) << exp(logtotal/(logcount ? logcount : 1)) << endl;
371 
372 // Safer functions on Windows for C&A, https://github.com/weidai11/cryptopp/issues/55
373 #if (CRYPTOPP_MSC_VERSION >= 1400)
374  tm localTime = {};
375  char timeBuf[64];
376  errno_t err;
377 
378  const time_t endTime = time(NULL);
379  err = localtime_s(&localTime, &endTime);
380  assert(err == 0);
381  err = asctime_s(timeBuf, sizeof(timeBuf), &localTime);
382  assert(err == 0);
383 
384  cout << "\nTest ended at " << timeBuf;
385 #else
386  const time_t endTime = time(NULL);
387  cout << "\nTest ended at " << asctime(localtime(&endTime));
388 #endif
389 #endif
390 }
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:29
virtual void SetKey(const byte *key, size_t length, const NameValuePairs &params=g_nullNameValuePairs)
Sets or reset the key of this object.
Definition: cryptlib.cpp:100
const char * Rounds()
int
Definition: argnames.h:23
Interface for one direction (encryption or decryption) of a stream cipher or block cipher mode with a...
Definition: cryptlib.h:1117
Class file for modes of operation.
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: cryptlib.cpp:329
virtual bool NeedsPrespecifiedDataLengths() const
Determines if data lengths must be specified prior to inputting data.
Definition: cryptlib.h:1148
virtual lword MaxMessageLength() const =0
Provides the maximum length of encrypted data.
Abstract base classes that provide a uniform interface to this library.
Classes for automatic resource management.
STL namespace.
void ProcessString(byte *inoutString, size_t length)
Encrypt or decrypt a string of bytes.
Definition: cryptlib.h:869
Combines two sets of NameValuePairs.
Definition: algparam.h:135
Interface for buffered transformations.
Definition: cryptlib.h:1352
Pointer that overloads operator→
Definition: smartptr.h:39
const char * TableSize()
int, in bytes
Definition: argnames.h:80
size_t Put(byte inByte, bool blocking=true)
Input a byte for processing.
Definition: cryptlib.h:1378
virtual unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Definition: cryptlib.h:837
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:554
Classes for HexEncoder and HexDecoder.
Class file for the AES cipher (Rijndael)
Interface for algorithms that take byte strings as keys.
Definition: cryptlib.h:524
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.cpp:79
SecBlock using AllocatorWithCleanup typedef.
Definition: secblock.h:736
Interface for the data processing portion of stream ciphers.
Definition: cryptlib.h:816
Classes, functions, intrinsics and features for X86, X32 nd X64 assembly.
Implementation of BufferedTransformation's attachment interface.
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
Definition: argnames.h:21
Classes for Blum Blum Shub generator.
Interface for hash functions and data processing part of MACs.
Definition: cryptlib.h:922
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:460
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition: misc.h:769
void SpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength=0)
Prespecifies the data lengths.
Definition: cryptlib.cpp:257
Crypto++ library namespace.
virtual void Update(const byte *input, size_t length)=0
Updates a hash with additional input.
Interface for retrieving values given their names.
Definition: cryptlib.h:277