1 // bench1.cpp - originally written and placed in the public domain by Wei Dai
2 //              CryptoPP::Test namespace added by JW in February 2017
3 
4 #include "cryptlib.h"
5 #include "bench.h"
6 #include "validate.h"
7 
8 #include "cpu.h"
9 #include "factory.h"
10 #include "algparam.h"
11 #include "argnames.h"
12 #include "smartptr.h"
13 #include "stdcpp.h"
14 
15 #include "osrng.h"
16 #include "drbg.h"
17 #include "darn.h"
18 #include "mersenne.h"
19 #include "rdrand.h"
20 #include "padlkrng.h"
21 
22 #include <iostream>
23 #include <iomanip>
24 #include <sstream>
25 
26 #if CRYPTOPP_MSC_VERSION
27 # pragma warning(disable: 4355)
28 #endif
29 
30 #if CRYPTOPP_MSC_VERSION
31 # pragma warning(disable: 4505 4355)
32 #endif
33 
34 NAMESPACE_BEGIN(CryptoPP)
35 NAMESPACE_BEGIN(Test)
36 
37 #ifdef CLOCKS_PER_SEC
38 const double CLOCK_TICKS_PER_SECOND = (double)CLOCKS_PER_SEC;
39 #elif defined(CLK_TCK)
40 const double CLOCK_TICKS_PER_SECOND = (double)CLK_TCK;
41 #else
42 const double CLOCK_TICKS_PER_SECOND = 1000000.0;
43 #endif
44 
45 extern const byte defaultKey[] = "0123456789" // 168 + NULL
46 	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 	"00000000000000000000000000000000000000000000000000000"
48 	"00000000000000000000000000000000000000000000000000000";
49 
50 double g_allocatedTime = 0.0, g_hertz = 0.0, g_logTotal = 0.0;
51 unsigned int g_logCount = 0;
52 time_t g_testBegin, g_testEnd;
53 
HertzToString(double hertz)54 inline std::string HertzToString(double hertz)
55 {
56 	std::ostringstream oss;
57 	oss.precision(3);
58 
59 	if (hertz >= 0.999e+9)
60 		oss << hertz / 1e+9 << " GHz";
61 	else if (hertz >= 0.999e+6)
62 		oss << hertz / 1e+6 << " MHz";
63 	else if (hertz >= 0.999e+3)
64 		oss << hertz / 1e+3 << " KHz";
65 	else
66 		oss << hertz << " Hz";
67 
68 	return oss.str();
69 }
70 
OutputResultBytes(const char * name,const char * provider,double length,double timeTaken)71 void OutputResultBytes(const char *name, const char *provider, double length, double timeTaken)
72 {
73 	std::ostringstream oss;
74 
75 	// Coverity finding
76 	if (length < 0.000001f) length = 0.000001f;
77 	if (timeTaken < 0.000001f) timeTaken = 0.000001f;
78 
79 	double mbs = length / timeTaken / (1024*1024);
80 	oss << "\n<TR><TD>" << name << "<TD>" << provider;
81 	oss << std::setiosflags(std::ios::fixed);
82 	oss << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << mbs;
83 	if (g_hertz > 1.0f)
84 	{
85 		const double cpb = timeTaken * g_hertz / length;
86 		if (cpb < 24.0f)
87 			oss << "<TD>" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << cpb;
88 		else
89 			oss << "<TD>" << std::setprecision(1) << std::setiosflags(std::ios::fixed) << cpb;
90 	}
91 	g_logTotal += log(mbs);
92 	g_logCount++;
93 
94 	std::cout << oss.str();
95 }
96 
OutputResultKeying(double iterations,double timeTaken)97 void OutputResultKeying(double iterations, double timeTaken)
98 {
99 	std::ostringstream oss;
100 
101 	// Coverity finding
102 	if (iterations < 0.000001f) iterations = 0.000001f;
103 	if (timeTaken < 0.000001f) timeTaken = 0.000001f;
104 
105 	oss << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*1000*timeTaken/iterations);
106 
107 	// Coverity finding
108 	if (g_hertz > 1.0f)
109 		oss << "<TD>" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations;
110 
111 	std::cout << oss.str();
112 }
113 
OutputResultOperations(const char * name,const char * provider,const char * operation,bool pc,unsigned long iterations,double timeTaken)114 void OutputResultOperations(const char *name, const char *provider, const char *operation, bool pc, unsigned long iterations, double timeTaken)
115 {
116 	CRYPTOPP_UNUSED(provider);
117 	std::ostringstream oss;
118 
119 	// Coverity finding
120 	if (!iterations) iterations++;
121 	if (timeTaken < 0.000001f) timeTaken = 0.000001f;
122 
123 	oss << "\n<TR><TD>" << name << " " << operation << (pc ? " with precomputation" : "");
124 	//oss << "<TD>" << provider;
125 	oss << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations);
126 
127 	// Coverity finding
128 	if (g_hertz > 1.0f)
129 	{
130 		const double t = timeTaken * g_hertz / iterations / 1000000;
131 		oss << "<TD>" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << t;
132 	}
133 
134 	g_logTotal += log(iterations/timeTaken);
135 	g_logCount++;
136 
137 	std::cout << oss.str();
138 }
139 
140 /*
141 void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal)
142 {
143 	const int BUF_SIZE = RoundUpToMultipleOf(2048U, cipher.OptimalNumberOfParallelBlocks() * cipher.BlockSize());
144 	AlignedSecByteBlock buf(BUF_SIZE);
145 	buf.SetMark(16);
146 
147 	const int nBlocks = BUF_SIZE / cipher.BlockSize();
148 	unsigned long i=0, blocks=1;
149 	double timeTaken;
150 
151 	clock_t start = ::clock();
152 	do
153 	{
154 		blocks *= 2;
155 		for (; i<blocks; i++)
156 			cipher.ProcessAndXorMultipleBlocks(buf, NULLPTR, buf, nBlocks);
157 		timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
158 	}
159 	while (timeTaken < 2.0/3*timeTotal);
160 
161 	OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
162 }
163 */
164 
BenchMark(const char * name,StreamTransformation & cipher,double timeTotal)165 void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
166 {
167 	const int BUF_SIZE=RoundUpToMultipleOf(2048U, cipher.OptimalBlockSize());
168 	AlignedSecByteBlock buf(BUF_SIZE);
169 	Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
170 	buf.SetMark(16);
171 
172 	unsigned long i=0, blocks=1;
173 	double timeTaken;
174 
175 	clock_t start = ::clock();
176 	do
177 	{
178 		blocks *= 2;
179 		for (; i<blocks; i++)
180 			cipher.ProcessString(buf, BUF_SIZE);
181 		timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
182 	}
183 	while (timeTaken < 2.0/3*timeTotal);
184 
185 	std::string provider = cipher.AlgorithmProvider();
186 	OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
187 }
188 
BenchMark(const char * name,HashTransformation & ht,double timeTotal)189 void BenchMark(const char *name, HashTransformation &ht, double timeTotal)
190 {
191 	const int BUF_SIZE=2048U;
192 	AlignedSecByteBlock buf(BUF_SIZE);
193 	Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
194 	buf.SetMark(16);
195 
196 	unsigned long i=0, blocks=1;
197 	double timeTaken;
198 
199 	clock_t start = ::clock();
200 	do
201 	{
202 		blocks *= 2;
203 		for (; i<blocks; i++)
204 			ht.Update(buf, BUF_SIZE);
205 		timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
206 	}
207 	while (timeTaken < 2.0/3*timeTotal);
208 
209 	std::string provider = ht.AlgorithmProvider();
210 	OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
211 }
212 
BenchMark(const char * name,BufferedTransformation & bt,double timeTotal)213 void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
214 {
215 	const int BUF_SIZE=2048U;
216 	AlignedSecByteBlock buf(BUF_SIZE);
217 	Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
218 	buf.SetMark(16);
219 
220 	unsigned long i=0, blocks=1;
221 	double timeTaken;
222 
223 	clock_t start = ::clock();
224 	do
225 	{
226 		blocks *= 2;
227 		for (; i<blocks; i++)
228 			bt.Put(buf, BUF_SIZE);
229 		timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
230 	}
231 	while (timeTaken < 2.0/3*timeTotal);
232 
233 	std::string provider = bt.AlgorithmProvider();
234 	OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
235 }
236 
BenchMark(const char * name,RandomNumberGenerator & rng,double timeTotal)237 void BenchMark(const char *name, RandomNumberGenerator &rng, double timeTotal)
238 {
239 	const int BUF_SIZE = 2048U;
240 	AlignedSecByteBlock buf(BUF_SIZE);
241 	Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
242 	buf.SetMark(16);
243 
244 	SymmetricCipher * cipher = dynamic_cast<SymmetricCipher*>(&rng);
245 	if (cipher != NULLPTR)
246 	{
247 		const size_t size = cipher->DefaultKeyLength();
248 		if (cipher->IsResynchronizable())
249 			cipher->SetKeyWithIV(buf, size, buf+size);
250 		else
251 			cipher->SetKey(buf, size);
252 	}
253 
254 	unsigned long long blocks = 1;
255 	double timeTaken;
256 
257 	clock_t start = ::clock();
258 	do
259 	{
260 		rng.GenerateBlock(buf, buf.size());
261 		blocks++;
262 		timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
263 	} while (timeTaken < timeTotal);
264 
265 	std::string provider = rng.AlgorithmProvider();
266 	OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
267 }
268 
269 // Hack, but we probably need a KeyedRandomNumberGenerator interface
270 //  and a few methods to generalize keying a RNG. X917RNG, Hash_DRBG,
271 //  HMAC_DRBG, AES/CFB RNG and a few others could use it. "A few others"
272 //  includes BLAKE2, ChaCha and Poly1305 when used as a RNG.
BenchMark(const char * name,NIST_DRBG & rng,double timeTotal)273 void BenchMark(const char *name, NIST_DRBG &rng, double timeTotal)
274 {
275 	const int BUF_SIZE = 2048U;
276 	AlignedSecByteBlock buf(BUF_SIZE);
277 	Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE);
278 	buf.SetMark(16);
279 
280 	rng.IncorporateEntropy(buf, rng.MinEntropyLength());
281 	unsigned long long blocks = 1;
282 	double timeTaken;
283 
284 	clock_t start = ::clock();
285 	do
286 	{
287 		rng.GenerateBlock(buf, buf.size());
288 		blocks++;
289 		timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
290 	} while (timeTaken < timeTotal);
291 
292 	std::string provider = rng.AlgorithmProvider();
293 	OutputResultBytes(name, provider.c_str(), double(blocks) * BUF_SIZE, timeTaken);
294 }
295 
296 template <class T>
BenchMarkByNameKeyLess(const char * factoryName,const char * displayName=NULLPTR,const NameValuePairs & params=g_nullNameValuePairs)297 void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName = NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
298 {
299 	CRYPTOPP_UNUSED(params);
300 	std::string name = factoryName;
301 	if (displayName)
302 		name = displayName;
303 
304 	member_ptr<T> obj(ObjectFactoryRegistry<T>::Registry().CreateObject(factoryName));
305 	BenchMark(name.c_str(), *obj, g_allocatedTime);
306 }
307 
AddHtmlHeader()308 void AddHtmlHeader()
309 {
310 	std::ostringstream oss;
311 
312 	// HTML5
313 	oss << "<!DOCTYPE HTML>";
314 	oss << "\n<HTML lang=\"en\">";
315 
316 	oss << "\n<HEAD>";
317 	oss << "\n<META charset=\"UTF-8\">";
318 	oss << "\n<TITLE>Speed Comparison of Popular Crypto Algorithms</TITLE>";
319 	oss << "\n<STYLE>\n  table {border-collapse: collapse;}";
320 	oss << "\n  table, th, td, tr {border: 1px solid black;}\n</STYLE>";
321 	oss << "\n</HEAD>";
322 
323 	oss << "\n<BODY>";
324 
325 	oss << "\n<H1><A href=\"http://www.cryptopp.com\">Crypto++</A> " << CRYPTOPP_VERSION / 100;
326 	oss << '.' << (CRYPTOPP_VERSION % 100) / 10 << '.' << CRYPTOPP_VERSION % 10 << " Benchmarks</H1>";
327 
328 	oss << "\n<P>Here are speed benchmarks for some commonly used cryptographic algorithms.</P>";
329 
330 	if (g_hertz > 1.0f)
331 		oss << "\n<P>CPU frequency of the test platform is " << HertzToString(g_hertz) << ".</P>";
332 	else
333 		oss << "\n<P>CPU frequency of the test platform was not provided.</P>" << std::endl;
334 
335 	std::cout << oss.str();
336 }
337 
AddHtmlFooter()338 void AddHtmlFooter()
339 {
340 	std::ostringstream oss;
341 	oss << "\n</BODY>\n</HTML>\n";
342 	std::cout << oss.str();
343 }
344 
BenchmarkWithCommand(int argc,const char * const argv[])345 void BenchmarkWithCommand(int argc, const char* const argv[])
346 {
347 	std::string command(argv[1]);
348 	float runningTime(argc >= 3 ? Test::StringToValue<float, true>(argv[2]) : 1.0f);
349 	float cpuFreq(argc >= 4 ? Test::StringToValue<float, true>(argv[3])*float(1e9) : 0.0f);
350 	std::string algoName(argc >= 5 ? argv[4] : "");
351 
352 	// https://github.com/weidai11/cryptopp/issues/983
353 	if (runningTime > 10.0f)
354 		runningTime = 10.0f;
355 
356 	if (command == "b")  // All benchmarks
357 		Benchmark(Test::All, runningTime, cpuFreq);
358 	else if (command == "b4")  // Public key algorithms over EC
359 		Test::Benchmark(Test::PublicKeyEC, runningTime, cpuFreq);
360 	else if (command == "b3")  // Public key algorithms
361 		Test::Benchmark(Test::PublicKey, runningTime, cpuFreq);
362 	else if (command == "b2")  // Shared key algorithms
363 		Test::Benchmark(Test::SharedKey, runningTime, cpuFreq);
364 	else if (command == "b1")  // Unkeyed algorithms
365 		Test::Benchmark(Test::Unkeyed, runningTime, cpuFreq);
366 }
367 
Benchmark(Test::TestClass suites,double t,double hertz)368 void Benchmark(Test::TestClass suites, double t, double hertz)
369 {
370 	g_allocatedTime = t;
371 	g_hertz = hertz;
372 
373 	// Add <br> in between tables
374 	size_t count_breaks = 0;
375 
376 	AddHtmlHeader();
377 
378 	g_testBegin = ::time(NULLPTR);
379 
380 	if (static_cast<int>(suites) == 0 || static_cast<int>(suites) > TestLast)
381 		suites = Test::All;
382 
383 	// Unkeyed algorithms
384 	if (suites & Test::Unkeyed)
385 	{
386 		if (count_breaks)
387 			std::cout << "\n<BR>";
388 		count_breaks++;
389 
390 		BenchmarkUnkeyedAlgorithms(t, hertz);
391 	}
392 
393 	// Shared key algorithms
394 	if (suites & Test::SharedKey)
395 	{
396 		if (count_breaks)
397 			std::cout << "\n<BR>";
398 		count_breaks++;
399 
400 		BenchmarkSharedKeyedAlgorithms(t, hertz);
401 	}
402 
403 	// Public key algorithms
404 	if (suites & Test::PublicKey)
405 	{
406 		if (count_breaks)
407 			std::cout << "\n<BR>";
408 		count_breaks++;
409 
410 		BenchmarkPublicKeyAlgorithms(t, hertz);
411 	}
412 
413 	// Public key algorithms over EC
414 	if (suites & Test::PublicKeyEC)
415 	{
416 		if (count_breaks)
417 			std::cout << "\n<BR>";
418 		count_breaks++;
419 
420 		BenchmarkEllipticCurveAlgorithms(t, hertz);
421 	}
422 
423 	g_testEnd = ::time(NULLPTR);
424 
425 	std::ostringstream oss;
426 	oss << "\n<P>Throughput Geometric Average: " << std::setiosflags(std::ios::fixed);
427 	oss << std::exp(g_logTotal/(g_logCount > 0.0f ? g_logCount : 1.0f)) << std::endl;
428 
429 	oss << "\n<P>Test started at " << TimeToString(g_testBegin);
430 	oss << "\n<BR>Test ended at " << TimeToString(g_testEnd);
431 	oss << "\n";
432 	std::cout << oss.str();
433 
434 	AddHtmlFooter();
435 }
436 
BenchmarkUnkeyedAlgorithms(double t,double hertz)437 void BenchmarkUnkeyedAlgorithms(double t, double hertz)
438 {
439 	g_allocatedTime = t;
440 	g_hertz = hertz;
441 
442 	const char *cpb;
443 	if (g_hertz > 1.0f)
444 		cpb = "<TH>Cycles/Byte";
445 	else
446 		cpb = "";
447 
448 	std::cout << "\n<TABLE>";
449 
450 	std::cout << "\n<COLGROUP><COL style=\"text-align: left;\"><COL style=\"text-align: right;\">";
451 	std::cout << "<COL style=\"text-align: right;\">";
452 	std::cout << "\n<THEAD style=\"background: #F0F0F0\">";
453 	std::cout << "\n<TR><TH>Algorithm<TH>Provider<TH>MiB/Second" << cpb;
454 
455 	std::cout << "\n<TBODY style=\"background: white;\">";
456 	{
457 #ifdef NONBLOCKING_RNG_AVAILABLE
458 		BenchMarkByNameKeyLess<RandomNumberGenerator>("NonblockingRng");
459 #endif
460 #ifdef OS_RNG_AVAILABLE
461 		BenchMarkByNameKeyLess<RandomNumberGenerator>("AutoSeededRandomPool");
462 		BenchMarkByNameKeyLess<RandomNumberGenerator>("AutoSeededX917RNG(AES)");
463 #endif
464 		BenchMarkByNameKeyLess<RandomNumberGenerator>("MT19937");
465 #if (CRYPTOPP_BOOL_X86) && !defined(CRYPTOPP_DISABLE_ASM)
466 		if (HasPadlockRNG())
467 			BenchMarkByNameKeyLess<RandomNumberGenerator>("PadlockRNG");
468 #endif
469 #if (CRYPTOPP_BOOL_X86 || CRYPTOPP_BOOL_X32 || CRYPTOPP_BOOL_X64) && !defined(CRYPTOPP_DISABLE_ASM)
470 		if (HasRDRAND())
471 			BenchMarkByNameKeyLess<RandomNumberGenerator>("RDRAND");
472 		if (HasRDSEED())
473 			BenchMarkByNameKeyLess<RandomNumberGenerator>("RDSEED");
474 #endif
475 #if (CRYPTOPP_BOOL_PPC32 || CRYPTOPP_BOOL_PPC64) && !defined(CRYPTOPP_DISABLE_ASM)
476 		if (HasDARN())
477 			BenchMarkByNameKeyLess<RandomNumberGenerator>("DARN");
478 #endif
479 		BenchMarkByNameKeyLess<RandomNumberGenerator>("AES/OFB RNG");
480 		BenchMarkByNameKeyLess<NIST_DRBG>("Hash_DRBG(SHA1)");
481 		BenchMarkByNameKeyLess<NIST_DRBG>("Hash_DRBG(SHA256)");
482 		BenchMarkByNameKeyLess<NIST_DRBG>("HMAC_DRBG(SHA1)");
483 		BenchMarkByNameKeyLess<NIST_DRBG>("HMAC_DRBG(SHA256)");
484 	}
485 
486 	std::cout << "\n<TBODY style=\"background: yellow;\">";
487 	{
488 		BenchMarkByNameKeyLess<HashTransformation>("CRC32");
489 		BenchMarkByNameKeyLess<HashTransformation>("CRC32C");
490 		BenchMarkByNameKeyLess<HashTransformation>("Adler32");
491 		BenchMarkByNameKeyLess<HashTransformation>("MD5");
492 		BenchMarkByNameKeyLess<HashTransformation>("SHA-1");
493 		BenchMarkByNameKeyLess<HashTransformation>("SHA-256");
494 		BenchMarkByNameKeyLess<HashTransformation>("SHA-512");
495 		BenchMarkByNameKeyLess<HashTransformation>("SHA3-224");
496 		BenchMarkByNameKeyLess<HashTransformation>("SHA3-256");
497 		BenchMarkByNameKeyLess<HashTransformation>("SHA3-384");
498 		BenchMarkByNameKeyLess<HashTransformation>("SHA3-512");
499 		BenchMarkByNameKeyLess<HashTransformation>("Keccak-224");
500 		BenchMarkByNameKeyLess<HashTransformation>("Keccak-256");
501 		BenchMarkByNameKeyLess<HashTransformation>("Keccak-384");
502 		BenchMarkByNameKeyLess<HashTransformation>("Keccak-512");
503 		BenchMarkByNameKeyLess<HashTransformation>("Tiger");
504 		BenchMarkByNameKeyLess<HashTransformation>("Whirlpool");
505 		BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-160");
506 		BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-320");
507 		BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-128");
508 		BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-256");
509 		BenchMarkByNameKeyLess<HashTransformation>("SM3");
510 		BenchMarkByNameKeyLess<HashTransformation>("BLAKE2s");
511 		BenchMarkByNameKeyLess<HashTransformation>("BLAKE2b");
512 	}
513 
514 	std::cout << "\n</TABLE>" << std::endl;
515 }
516 
517 NAMESPACE_END  // Test
518 NAMESPACE_END  // CryptoPP
519