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 (http://stackoverflow.com/a/30968371 does not squash the finding)
48  std::ostringstream out;
49  out.copyfmt(cout);
50 
51  // Coverity finding
52  if (length < 0.0000000001f) length = 0.000001f;
53  if (timeTaken < 0.0000000001f) timeTaken = 0.000001f;
54 
55  double mbs = length / timeTaken / (1024*1024);
56  out << "\n<TR><TH>" << name;
57 // out << "<TD>" << setprecision(3) << length / (1024*1024);
58  out << setiosflags(ios::fixed);
59 // out << "<TD>" << setprecision(3) << timeTaken;
60  out << "<TD>" << setprecision(0) << setiosflags(ios::fixed) << mbs;
61  if (g_hertz)
62  out << "<TD>" << setprecision(1) << setiosflags(ios::fixed) << timeTaken * g_hertz / length;
63  logtotal += log(mbs);
64  logcount++;
65 
66  cout << out.str();
67 }
68 
69 void OutputResultKeying(double iterations, double timeTaken)
70 {
71  // Coverity finding (http://stackoverflow.com/a/30968371 does not squash the finding)
72  std::ostringstream out;
73  out.copyfmt(cout);
74 
75  // Coverity finding
76  if (iterations < 0.0000000001f) iterations = 0.000001f;
77  if (timeTaken < 0.0000000001f) timeTaken = 0.000001f;
78 
79  out << "<TD>" << setprecision(3) << setiosflags(ios::fixed) << (1000*1000*timeTaken/iterations);
80  if (g_hertz)
81  out << "<TD>" << setprecision(0) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations;
82 
83  cout << out.str();
84 }
85 
86 void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken)
87 {
88  // Coverity finding (http://stackoverflow.com/a/30968371 does not squash the finding)
89  std::ostringstream out;
90  out.copyfmt(cout);
91 
92  // Coverity finding
93  if (!iterations) iterations++;
94  if (timeTaken < 0.0000000001f) timeTaken = 0.000001f;
95 
96  out << "\n<TR><TH>" << name << " " << operation << (pc ? " with precomputation" : "");
97  out << "<TD>" << setprecision(2) << setiosflags(ios::fixed) << (1000*timeTaken/iterations);
98  if (g_hertz)
99  out << "<TD>" << setprecision(2) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations / 1000000;
100 
101  logtotal += log(iterations/timeTaken);
102  logcount++;
103 
104  cout << out.str();
105 }
106 
107 /*
108 void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal)
109 {
110  const int BUF_SIZE = RoundUpToMultipleOf(2048U, cipher.OptimalNumberOfParallelBlocks() * cipher.BlockSize());
111  AlignedSecByteBlock buf(BUF_SIZE);
112  const int nBlocks = BUF_SIZE / cipher.BlockSize();
113  clock_t start = clock();
114 
115  unsigned long i=0, blocks=1;
116  double timeTaken;
117  do
118  {
119  blocks *= 2;
120  for (; i<blocks; i++)
121  cipher.ProcessAndXorMultipleBlocks(buf, NULL, buf, nBlocks);
122  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
123  }
124  while (timeTaken < 2.0/3*timeTotal);
125 
126  OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
127 }
128 */
129 
130 void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
131 {
132  const int BUF_SIZE=RoundUpToMultipleOf(2048U, cipher.OptimalBlockSize());
133  AlignedSecByteBlock buf(BUF_SIZE);
134  GlobalRNG().GenerateBlock(buf, BUF_SIZE);
135  clock_t start = clock();
136 
137  unsigned long i=0, blocks=1;
138  double timeTaken;
139  do
140  {
141  blocks *= 2;
142  for (; i<blocks; i++)
143  cipher.ProcessString(buf, BUF_SIZE);
144  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
145  }
146  while (timeTaken < 2.0/3*timeTotal);
147 
148  OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
149 }
150 
151 void BenchMark(const char *name, AuthenticatedSymmetricCipher &cipher, double timeTotal)
152 {
153  if (cipher.NeedsPrespecifiedDataLengths())
154  cipher.SpecifyDataLengths(0, cipher.MaxMessageLength(), 0);
155 
156  BenchMark(name, static_cast<StreamTransformation &>(cipher), timeTotal);
157 }
158 
159 void BenchMark(const char *name, HashTransformation &ht, double timeTotal)
160 {
161  const int BUF_SIZE=2048U;
162  AlignedSecByteBlock buf(BUF_SIZE);
163  GlobalRNG().GenerateBlock(buf, BUF_SIZE);
164  clock_t start = clock();
165 
166  unsigned long i=0, blocks=1;
167  double timeTaken;
168  do
169  {
170  blocks *= 2;
171  for (; i<blocks; i++)
172  ht.Update(buf, BUF_SIZE);
173  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
174  }
175  while (timeTaken < 2.0/3*timeTotal);
176 
177  OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
178 }
179 
180 void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
181 {
182  const int BUF_SIZE=2048U;
183  AlignedSecByteBlock buf(BUF_SIZE);
184  GlobalRNG().GenerateBlock(buf, BUF_SIZE);
185  clock_t start = clock();
186 
187  unsigned long i=0, blocks=1;
188  double timeTaken;
189  do
190  {
191  blocks *= 2;
192  for (; i<blocks; i++)
193  bt.Put(buf, BUF_SIZE);
194  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
195  }
196  while (timeTaken < 2.0/3*timeTotal);
197 
198  OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
199 }
200 
201 void BenchMarkKeying(SimpleKeyingInterface &c, size_t keyLength, const NameValuePairs &params)
202 {
203  unsigned long iterations = 0;
204  clock_t start = clock();
205  double timeTaken;
206  do
207  {
208  for (unsigned int i=0; i<1024; i++)
209  c.SetKey(defaultKey, keyLength, params);
210  timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
211  iterations += 1024;
212  }
213  while (timeTaken < g_allocatedTime);
214 
215  OutputResultKeying(iterations, timeTaken);
216 }
217 
218 //VC60 workaround: compiler bug triggered without the extra dummy parameters
219 // on VC60 also needs to be named differently from BenchMarkByName
220 template <class T_FactoryOutput, class T_Interface>
221 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)
222 {
223  CRYPTOPP_UNUSED(x), CRYPTOPP_UNUSED(y), CRYPTOPP_UNUSED(params);
224 
225  std::string name(factoryName ? factoryName : "");
227 
228  if (!keyLength)
229  keyLength = obj->DefaultKeyLength();
230 
231  if (displayName)
232  name = displayName;
233  else if (keyLength)
234  name += " (" + IntToString(keyLength * 8) + "-bit key)";
235 
236  obj->SetKey(defaultKey, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false)));
237  BenchMark(name.c_str(), *static_cast<T_Interface *>(obj.get()), g_allocatedTime);
238  BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false)));
239 }
240 
241 //VC60 workaround: compiler bug triggered without the extra dummy parameters
242 template <class T_FactoryOutput>
243 void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs, T_FactoryOutput *x=NULL)
244 {
245  CRYPTOPP_UNUSED(x), CRYPTOPP_UNUSED(params);
246 
247  BenchMarkByName2<T_FactoryOutput, T_FactoryOutput>(factoryName, keyLength, displayName, params, x, x);
248 }
249 
250 template <class T>
251 void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName=NULL, const NameValuePairs &params = g_nullNameValuePairs, T *x=NULL)
252 {
253  CRYPTOPP_UNUSED(x), CRYPTOPP_UNUSED(params);
254 
255  std::string name = factoryName;
256  if (displayName)
257  name = displayName;
258 
259  member_ptr<T> obj(ObjectFactoryRegistry<T>::Registry().CreateObject(factoryName));
260  BenchMark(name.c_str(), *obj, g_allocatedTime);
261 }
262 
263 void BenchmarkAll(double t, double hertz)
264 {
265 #if 1
266  logtotal = 0;
267  logcount = 0;
268  g_allocatedTime = t;
269  g_hertz = hertz;
270 
271  const char *cpb, *cpk;
272  if (g_hertz)
273  {
274  cpb = "<TH>Cycles Per Byte";
275  cpk = "<TH>Cycles to<br>Setup Key and IV";
276  cout << "CPU frequency of the test platform is " << g_hertz << " Hz.\n";
277  }
278  else
279  {
280  cpb = cpk = "";
281  cout << "CPU frequency of the test platform was not provided.\n";
282  }
283 
284  cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right><COL align=right><COL align=right>" << endl;
285  cout << "<THEAD><TR><TH>Algorithm<TH>MiB/Second" << cpb << "<TH>Microseconds to<br>Setup Key and IV" << cpk << endl;
286 
287  cout << "\n<TBODY style=\"background: yellow\">";
288 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
289  if (HasCLMUL())
290  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM");
291  else
292 #endif
293  {
294  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (2K tables)", MakeParameters(Name::TableSize(), 2048));
295  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
296  }
297  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/CCM");
298  BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/EAX");
299 
300  cout << "\n<TBODY style=\"background: white\">";
301 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
302  if (HasCLMUL())
303  BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES)");
304  else
305 #endif
306  {
307  BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (2K tables)", MakeParameters(Name::TableSize(), 2048));
308  BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
309  }
310  BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-64");
311  BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-128");
312  BenchMarkByName<MessageAuthenticationCode>("HMAC(SHA-1)");
313  BenchMarkByName<MessageAuthenticationCode>("Two-Track-MAC");
314  BenchMarkByName<MessageAuthenticationCode>("CMAC(AES)");
315  BenchMarkByName<MessageAuthenticationCode>("DMAC(AES)");
316 
317  cout << "\n<TBODY style=\"background: yellow\">";
318  BenchMarkByNameKeyLess<HashTransformation>("CRC32");
319  BenchMarkByNameKeyLess<HashTransformation>("Adler32");
320  BenchMarkByNameKeyLess<HashTransformation>("MD5");
321  BenchMarkByNameKeyLess<HashTransformation>("SHA-1");
322  BenchMarkByNameKeyLess<HashTransformation>("SHA-256");
323  BenchMarkByNameKeyLess<HashTransformation>("SHA-512");
324  BenchMarkByNameKeyLess<HashTransformation>("SHA-3-224");
325  BenchMarkByNameKeyLess<HashTransformation>("SHA-3-256");
326  BenchMarkByNameKeyLess<HashTransformation>("SHA-3-384");
327  BenchMarkByNameKeyLess<HashTransformation>("SHA-3-512");
328  BenchMarkByNameKeyLess<HashTransformation>("Tiger");
329  BenchMarkByNameKeyLess<HashTransformation>("Whirlpool");
330  BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-160");
331  BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-320");
332  BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-128");
333  BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-256");
334 
335  cout << "\n<TBODY style=\"background: white\">";
336  BenchMarkByName<SymmetricCipher>("Panama-LE");
337  BenchMarkByName<SymmetricCipher>("Panama-BE");
338  BenchMarkByName<SymmetricCipher>("Salsa20");
339  BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/12", MakeParameters(Name::Rounds(), 12));
340  BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/8", MakeParameters(Name::Rounds(), 8));
341  BenchMarkByName<SymmetricCipher>("Sosemanuk");
342  BenchMarkByName<SymmetricCipher>("MARC4");
343  BenchMarkByName<SymmetricCipher>("SEAL-3.0-LE");
344  BenchMarkByName<SymmetricCipher>("WAKE-OFB-LE");
345 
346  cout << "\n<TBODY style=\"background: yellow\">";
347  BenchMarkByName<SymmetricCipher>("AES/CTR", 16);
348  BenchMarkByName<SymmetricCipher>("AES/CTR", 24);
349  BenchMarkByName<SymmetricCipher>("AES/CTR", 32);
350  BenchMarkByName<SymmetricCipher>("AES/CBC", 16);
351  BenchMarkByName<SymmetricCipher>("AES/CBC", 24);
352  BenchMarkByName<SymmetricCipher>("AES/CBC", 32);
353  BenchMarkByName<SymmetricCipher>("AES/OFB", 16);
354  BenchMarkByName<SymmetricCipher>("AES/CFB", 16);
355  BenchMarkByName<SymmetricCipher>("AES/ECB", 16);
356  BenchMarkByName<SymmetricCipher>("Camellia/CTR", 16);
357  BenchMarkByName<SymmetricCipher>("Camellia/CTR", 32);
358  BenchMarkByName<SymmetricCipher>("Twofish/CTR");
359  BenchMarkByName<SymmetricCipher>("Serpent/CTR");
360  BenchMarkByName<SymmetricCipher>("CAST-256/CTR");
361  BenchMarkByName<SymmetricCipher>("RC6/CTR");
362  BenchMarkByName<SymmetricCipher>("MARS/CTR");
363  BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 16);
364  BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 64);
365  BenchMarkByName<SymmetricCipher>("DES/CTR");
366  BenchMarkByName<SymmetricCipher>("DES-XEX3/CTR");
367  BenchMarkByName<SymmetricCipher>("DES-EDE3/CTR");
368  BenchMarkByName<SymmetricCipher>("IDEA/CTR");
369  BenchMarkByName<SymmetricCipher>("RC5/CTR", 0, "RC5 (r=16)");
370  BenchMarkByName<SymmetricCipher>("Blowfish/CTR");
371  BenchMarkByName<SymmetricCipher>("TEA/CTR");
372  BenchMarkByName<SymmetricCipher>("XTEA/CTR");
373  BenchMarkByName<SymmetricCipher>("CAST-128/CTR");
374  BenchMarkByName<SymmetricCipher>("SKIPJACK/CTR");
375  BenchMarkByName<SymmetricCipher>("SEED/CTR", 0, "SEED/CTR (1/2 K table)");
376  cout << "</TABLE>" << endl;
377 
378  BenchmarkAll2(t, hertz);
379  cout << "Throughput Geometric Average: " << setiosflags(ios::fixed) << exp(logtotal/(logcount ? logcount : 1)) << endl;
380 
381 // Safer functions on Windows for C&A, https://github.com/weidai11/cryptopp/issues/55
382 #if defined(CRYPTOPP_MSC_VERSION)
383  tm localTime = {};
384  char timeBuf[64];
385  errno_t err;
386 
387  const time_t endTime = time(NULL);
388  err = localtime_s(&localTime, &endTime);
389  assert(err == 0);
390  err = asctime_s(timeBuf, sizeof(timeBuf), &localTime);
391  assert(err == 0);
392 
393  cout << "\nTest ended at " << timeBuf;
394 #else
395  const time_t endTime = time(NULL);
396  cout << "\nTest ended at " << asctime(localtime(&endTime));
397 #endif
398 #endif
399 }
used to pass byte array input as part of a NameValuePairs object
Definition: algparam.h:30
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:1036
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
if this function returns true, SpecifyDataLengths() must be called before attempting to input data ...
Definition: cryptlib.h:1055
virtual lword MaxMessageLength() const =0
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:801
Interface for buffered transformations.
Definition: cryptlib.h:1247
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:1268
virtual unsigned int OptimalBlockSize() const
Provides the input block size most efficient for this cipher.
Definition: cryptlib.h:769
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:487
Classes for HexEncoder and HexDecoder.
Class file for the AES cipher (Rijndael)
Interface for algorithms that take byte strings as keys.
Definition: cryptlib.h:497
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.cpp:79
AlignedSecByteBlock is a SecBlock<byte, AllocatorWithCleanup<byte, true> > typedef.
Interface for the data processing portion of stream ciphers.
Definition: cryptlib.h:752
Classes, functions, intrinsics and features for X86, X32 nd X64 assembly.
Implementation of BufferedTransformation&#39;s attachment interface in cryptlib.h.
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:858
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:449
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Definition: misc.h:759
void SpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength=0)
this function only needs to be called if NeedsPrespecifiedDataLengths() returns true ...
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:261