// bench1.cpp - originally written and placed in the public domain by Wei Dai // CryptoPP::Test namespace added by JW in February 2017 #include "cryptlib.h" #include "bench.h" #include "validate.h" #include "aes.h" #include "blumshub.h" #include "files.h" #include "filters.h" #include "hex.h" #include "modes.h" #include "factory.h" #include "smartptr.h" #include "cpu.h" #include #include #include #include #include NAMESPACE_BEGIN(CryptoPP) NAMESPACE_BEGIN(Test) double logTotal = 0.0, g_allocatedTime = 0, g_hertz = 0; unsigned int logCount = 0; void OutputResultBytes(const char *name, double length, double timeTaken) { // Coverity finding, also see http://stackoverflow.com/a/34509163/608639. StreamState ss(std::cout); // Coverity finding if (length < 0.0000000001f) length = 0.000001f; if (timeTaken < 0.0000000001f) timeTaken = 0.000001f; double mbs = length / timeTaken / (1024*1024); std::cout << "\n" << name; // std::cout << "" << std::setprecision(3) << length / (1024*1024); std::cout << std::setiosflags(std::ios::fixed); // std::cout << "" << std::setprecision(3) << timeTaken; std::cout << "" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << mbs; if (g_hertz) std::cout << "" << std::setprecision(1) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / length; logTotal += log(mbs); logCount++; } void OutputResultKeying(double iterations, double timeTaken) { // Coverity finding, also see http://stackoverflow.com/a/34509163/608639. StreamState ss(std::cout); // Coverity finding if (iterations < 0.0000000001f) iterations = 0.000001f; if (timeTaken < 0.0000000001f) timeTaken = 0.000001f; std::cout << "" << std::setprecision(3) << std::setiosflags(std::ios::fixed) << (1000*1000*timeTaken/iterations); if (g_hertz) std::cout << "" << std::setprecision(0) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations; } void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken) { // Coverity finding, also see http://stackoverflow.com/a/34509163/608639. StreamState ss(std::cout); // Coverity finding if (!iterations) iterations++; if (timeTaken < 0.0000000001f) timeTaken = 0.000001f; std::cout << "\n" << name << " " << operation << (pc ? " with precomputation" : ""); std::cout << "" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << (1000*timeTaken/iterations); if (g_hertz) std::cout << "" << std::setprecision(2) << std::setiosflags(std::ios::fixed) << timeTaken * g_hertz / iterations / 1000000; logTotal += log(iterations/timeTaken); logCount++; } /* void BenchMark(const char *name, BlockTransformation &cipher, double timeTotal) { const int BUF_SIZE = RoundUpToMultipleOf(2048U, cipher.OptimalNumberOfParallelBlocks() * cipher.BlockSize()); AlignedSecByteBlock buf(BUF_SIZE); const int nBlocks = BUF_SIZE / cipher.BlockSize(); clock_t start = clock(); unsigned long i=0, blocks=1; double timeTaken; do { blocks *= 2; for (; i(cipher), timeTotal); } void BenchMark(const char *name, HashTransformation &ht, double timeTotal) { const int BUF_SIZE=2048U; AlignedSecByteBlock buf(BUF_SIZE); Test::GlobalRNG().GenerateBlock(buf, BUF_SIZE); clock_t start = clock(); unsigned long i=0, blocks=1; double timeTaken; do { blocks *= 2; for (; i void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char *displayName=NULLPTR, const NameValuePairs ¶ms = g_nullNameValuePairs) { CRYPTOPP_UNUSED(params); std::string name(factoryName ? factoryName : ""); member_ptr obj(ObjectFactoryRegistry::Registry().CreateObject(name.c_str())); if (!keyLength) keyLength = obj->DefaultKeyLength(); if (displayName) name = displayName; else if (keyLength) name += " (" + IntToString(keyLength * 8) + "-bit key)"; obj->SetKey(defaultKey, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false))); BenchMark(name.c_str(), *static_cast(obj.get()), g_allocatedTime); BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false))); } template void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName=NULLPTR, const NameValuePairs ¶ms = g_nullNameValuePairs) { CRYPTOPP_UNUSED(params); BenchMarkByName2(factoryName, keyLength, displayName, params); } template void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName=NULLPTR, const NameValuePairs ¶ms = g_nullNameValuePairs) { CRYPTOPP_UNUSED(params); std::string name = factoryName; if (displayName) name = displayName; member_ptr obj(ObjectFactoryRegistry::Registry().CreateObject(factoryName)); BenchMark(name.c_str(), *obj, g_allocatedTime); } void BenchmarkAll(double t, double hertz) { #if 1 logTotal = 0; logCount = 0; g_allocatedTime = t; g_hertz = hertz; const char *cpb, *cpk; if (g_hertz) { cpb = "Cycles Per Byte"; cpk = "Cycles to
Setup Key and IV"; std::cout << "CPU frequency of the test platform is " << g_hertz << " Hz.\n"; } else { cpb = cpk = ""; std::cout << "CPU frequency of the test platform was not provided.\n"; } std::cout << "" << std::endl; std::cout << ""; #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE if (HasCLMUL()) BenchMarkByName2("AES/GCM", 0, "AES/GCM"); else #elif CRYPTOPP_BOOL_ARM_PMULL_AVAILABLE if (HasPMULL()) BenchMarkByName2("AES/GCM", 0, "AES/GCM"); else #endif { BenchMarkByName2("AES/GCM", 0, "AES/GCM (2K tables)", MakeParameters(Name::TableSize(), 2048)); BenchMarkByName2("AES/GCM", 0, "AES/GCM (64K tables)", MakeParameters(Name::TableSize(), 64*1024)); } BenchMarkByName2("AES/CCM"); BenchMarkByName2("AES/EAX"); std::cout << "\n"; #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE if (HasCLMUL()) BenchMarkByName2("AES/GCM", 0, "GMAC(AES)"); else #endif { BenchMarkByName2("AES/GCM", 0, "GMAC(AES) (2K tables)", MakeParameters(Name::TableSize(), 2048)); BenchMarkByName2("AES/GCM", 0, "GMAC(AES) (64K tables)", MakeParameters(Name::TableSize(), 64*1024)); } BenchMarkByName("VMAC(AES)-64"); BenchMarkByName("VMAC(AES)-128"); BenchMarkByName("HMAC(SHA-1)"); BenchMarkByName("Two-Track-MAC"); BenchMarkByName("CMAC(AES)"); BenchMarkByName("DMAC(AES)"); BenchMarkByName("Poly1305(AES)"); BenchMarkByName("BLAKE2s"); BenchMarkByName("BLAKE2b"); BenchMarkByName("SipHash-2-4"); BenchMarkByName("SipHash-4-8"); std::cout << "\n"; BenchMarkByNameKeyLess("CRC32"); BenchMarkByNameKeyLess("CRC32C"); BenchMarkByNameKeyLess("Adler32"); BenchMarkByNameKeyLess("MD5"); BenchMarkByNameKeyLess("SHA-1"); BenchMarkByNameKeyLess("SHA-256"); BenchMarkByNameKeyLess("SHA-512"); BenchMarkByNameKeyLess("Keccak-224"); BenchMarkByNameKeyLess("Keccak-256"); BenchMarkByNameKeyLess("Keccak-384"); BenchMarkByNameKeyLess("Keccak-512"); BenchMarkByNameKeyLess("SHA3-224"); BenchMarkByNameKeyLess("SHA3-256"); BenchMarkByNameKeyLess("SHA3-384"); BenchMarkByNameKeyLess("SHA3-512"); BenchMarkByNameKeyLess("Tiger"); BenchMarkByNameKeyLess("Whirlpool"); BenchMarkByNameKeyLess("RIPEMD-160"); BenchMarkByNameKeyLess("RIPEMD-320"); BenchMarkByNameKeyLess("RIPEMD-128"); BenchMarkByNameKeyLess("RIPEMD-256"); BenchMarkByNameKeyLess("BLAKE2s"); BenchMarkByNameKeyLess("BLAKE2b"); std::cout << "\n"; BenchMarkByName("Panama-LE"); BenchMarkByName("Panama-BE"); BenchMarkByName("Salsa20"); BenchMarkByName("Salsa20", 0, "Salsa20/12", MakeParameters(Name::Rounds(), 12)); BenchMarkByName("Salsa20", 0, "Salsa20/8", MakeParameters(Name::Rounds(), 8)); BenchMarkByName("ChaCha20"); BenchMarkByName("ChaCha12"); BenchMarkByName("ChaCha8"); BenchMarkByName("Sosemanuk"); BenchMarkByName("MARC4"); BenchMarkByName("SEAL-3.0-LE"); BenchMarkByName("WAKE-OFB-LE"); std::cout << "\n"; BenchMarkByName("AES/CTR", 16); BenchMarkByName("AES/CTR", 24); BenchMarkByName("AES/CTR", 32); BenchMarkByName("AES/CBC", 16); BenchMarkByName("AES/CBC", 24); BenchMarkByName("AES/CBC", 32); BenchMarkByName("AES/OFB", 16); BenchMarkByName("AES/CFB", 16); BenchMarkByName("AES/ECB", 16); BenchMarkByName("Camellia/CTR", 16); BenchMarkByName("Camellia/CTR", 32); BenchMarkByName("Twofish/CTR"); BenchMarkByName("Serpent/CTR"); BenchMarkByName("CAST-256/CTR"); BenchMarkByName("RC6/CTR"); BenchMarkByName("MARS/CTR"); BenchMarkByName("SHACAL-2/CTR", 16); BenchMarkByName("SHACAL-2/CTR", 64); BenchMarkByName("DES/CTR"); BenchMarkByName("DES-XEX3/CTR"); BenchMarkByName("DES-EDE3/CTR"); BenchMarkByName("IDEA/CTR"); BenchMarkByName("RC5/CTR", 0, "RC5 (r=16)"); BenchMarkByName("Blowfish/CTR"); BenchMarkByName("TEA/CTR"); BenchMarkByName("XTEA/CTR"); BenchMarkByName("CAST-128/CTR"); BenchMarkByName("SKIPJACK/CTR"); BenchMarkByName("SEED/CTR", 0, "SEED/CTR (1/2 K table)"); std::cout << "
AlgorithmMiB/Second" << cpb << "Microseconds to
Setup Key and IV" << cpk << std::endl; std::cout << "\n
" << std::endl; BenchmarkAll2(t, hertz); std::cout << "Throughput Geometric Average: " << std::setiosflags(std::ios::fixed) << exp(logTotal/(logCount ? logCount : 1)) << std::endl; // Safer functions on Windows for C&A, https://github.com/weidai11/cryptopp/issues/55 #if (CRYPTOPP_MSC_VERSION >= 1400) tm localTime = {}; char timeBuf[64]; errno_t err; const time_t endTime = time(NULLPTR); err = localtime_s(&localTime, &endTime); CRYPTOPP_ASSERT(err == 0); err = asctime_s(timeBuf, sizeof(timeBuf), &localTime); CRYPTOPP_ASSERT(err == 0); std::cout << "\nTest ended at " << timeBuf; #else const time_t endTime = time(NULLPTR); std::cout << "\nTest ended at " << asctime(localtime(&endTime)); #endif #endif } NAMESPACE_END // Test NAMESPACE_END // CryptoPP