// bench2.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 "cpu.h" #include "factory.h" #include "algparam.h" #include "argnames.h" #include "smartptr.h" #include "stdcpp.h" #if CRYPTOPP_MSC_VERSION # pragma warning(disable: 4355) #endif #if CRYPTOPP_MSC_VERSION # pragma warning(disable: 4505 4355) #endif NAMESPACE_BEGIN(CryptoPP) NAMESPACE_BEGIN(Test) void BenchMarkKeying(SimpleKeyingInterface &c, size_t keyLength, const NameValuePairs ¶ms) { unsigned long iterations = 0; double timeTaken; clock_t start = ::clock(); do { for (unsigned int i=0; i<1024; i++) c.SetKey(defaultKey, keyLength, params); timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND; iterations += 1024; } while (timeTaken < g_allocatedTime); OutputResultKeying(iterations, timeTaken); } void BenchMark(const char *name, AuthenticatedSymmetricCipher &cipher, double timeTotal) { if (cipher.NeedsPrespecifiedDataLengths()) cipher.SpecifyDataLengths(0, cipher.MaxMessageLength(), 0); BenchMark(name, static_cast(cipher), timeTotal); } template void BenchMarkByName2(const char *factoryName, size_t keyLength=0, const char *displayName=NULLPTR, const NameValuePairs ¶ms = g_nullNameValuePairs) { std::string name(factoryName ? factoryName : ""); member_ptr obj(ObjectFactoryRegistry::Registry().CreateObject(name.c_str())); if (keyLength == 0) keyLength = obj->DefaultKeyLength(); if (displayName != NULLPTR) name = displayName; else if (keyLength != 0) 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) { BenchMarkByName2(factoryName, keyLength, displayName, params); } void Benchmark2(double t, double hertz) { g_allocatedTime = t; g_hertz = hertz; const char *cpb, *cpk; if (g_hertz > 1.0f) { cpb = "Cycles Per Byte"; cpk = "Cycles to
Setup Key and IV"; } else { cpb = cpk = ""; } std::cout << "\n"; std::cout << "\n"; std::cout << "\n"; std::cout << "\n"; { #if CRYPTOPP_AESNI_AVAILABLE if (HasCLMUL()) BenchMarkByName2("AES/GCM", 0, "GMAC(AES)"); else #elif CRYPTOPP_ARM_PMULL_AVAILABLE if (HasPMULL()) BenchMarkByName2("AES/GCM", 0, "GMAC(AES)"); else #elif CRYPTOPP_POWER8_VMULL_AVAILABLE if (HasPMULL()) 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("HMAC(SHA-256)"); 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"; { 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("ChaCha"); BenchMarkByName("ChaCha", 0, "ChaCha/12", MakeParameters(Name::Rounds(), 12)); BenchMarkByName("ChaCha", 0, "ChaCha/8", MakeParameters(Name::Rounds(), 8)); BenchMarkByName("Sosemanuk"); BenchMarkByName("Rabbit"); BenchMarkByName("RabbitWithIV"); BenchMarkByName("HC-128"); BenchMarkByName("HC-256"); 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("ARIA/CTR", 16); BenchMarkByName("ARIA/CTR", 32); BenchMarkByName("HIGHT/CTR"); BenchMarkByName("Camellia/CTR", 16); BenchMarkByName("Camellia/CTR", 32); BenchMarkByName("Twofish/CTR"); BenchMarkByName("Threefish-256(256)/CTR", 32); BenchMarkByName("Threefish-512(512)/CTR", 64); BenchMarkByName("Threefish-1024(1024)/CTR", 128); BenchMarkByName("Serpent/CTR"); BenchMarkByName("CAST-128/CTR"); BenchMarkByName("CAST-256/CTR", 32); 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("SKIPJACK/CTR"); BenchMarkByName("SEED/CTR", 0, "SEED/CTR (1/2 K table)"); BenchMarkByName("SM4/CTR"); BenchMarkByName("Kalyna-128/CTR", 16, "Kalyna-128(128)/CTR (128-bit key)"); BenchMarkByName("Kalyna-128/CTR", 32, "Kalyna-128(256)/CTR (256-bit key)"); BenchMarkByName("Kalyna-256/CTR", 32, "Kalyna-256(256)/CTR (256-bit key)"); BenchMarkByName("Kalyna-256/CTR", 64, "Kalyna-256(512)/CTR (512-bit key)"); BenchMarkByName("Kalyna-512/CTR", 64, "Kalyna-512(512)/CTR (512-bit key)"); } std::cout << "\n"; { BenchMarkByName("CHAM-64/CTR", 16, "CHAM-64(128)/CTR (128-bit key)"); BenchMarkByName("CHAM-128/CTR", 16, "CHAM-128(128)/CTR (128-bit key)"); BenchMarkByName("CHAM-128/CTR", 32, "CHAM-128(256)/CTR (256-bit key)"); BenchMarkByName("LEA-128/CTR", 16, "LEA-128(128)/CTR (128-bit key)"); BenchMarkByName("LEA-128/CTR", 24, "LEA-128(192)/CTR (192-bit key)"); BenchMarkByName("LEA-128/CTR", 32, "LEA-128(256)/CTR (256-bit key)"); BenchMarkByName("SIMECK-32/CTR", 8, "SIMECK-32(64)/CTR (64-bit key)"); BenchMarkByName("SIMECK-64/CTR", 16, "SIMECK-64(128)/CTR (128-bit key)"); BenchMarkByName("SIMON-64/CTR", 12, "SIMON-64(96)/CTR (96-bit key)"); BenchMarkByName("SIMON-64/CTR", 16, "SIMON-64(128)/CTR (128-bit key)"); BenchMarkByName("SIMON-128/CTR", 16, "SIMON-128(128)/CTR (128-bit key)"); BenchMarkByName("SIMON-128/CTR", 24, "SIMON-128(192)/CTR (192-bit key)"); BenchMarkByName("SIMON-128/CTR", 32, "SIMON-128(256)/CTR (256-bit key)"); BenchMarkByName("SPECK-64/CTR", 12, "SPECK-64(96)/CTR (96-bit key)"); BenchMarkByName("SPECK-64/CTR", 16, "SPECK-64(128)/CTR (128-bit key)"); BenchMarkByName("SPECK-128/CTR", 16, "SPECK-128(128)/CTR (128-bit key)"); BenchMarkByName("SPECK-128/CTR", 24, "SPECK-128(192)/CTR (192-bit key)"); BenchMarkByName("SPECK-128/CTR", 32, "SPECK-128(256)/CTR (256-bit key)"); BenchMarkByName("TEA/CTR"); BenchMarkByName("XTEA/CTR"); } std::cout << "\n"; { #if CRYPTOPP_AESNI_AVAILABLE if (HasCLMUL()) BenchMarkByName2("AES/GCM", 0, "AES/GCM"); else #elif CRYPTOPP_ARM_PMULL_AVAILABLE if (HasPMULL()) BenchMarkByName2("AES/GCM", 0, "AES/GCM"); else #elif CRYPTOPP_POWER8_VMULL_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
AlgorithmProviderMiB/Second" << cpb; std::cout << "Microseconds to
Setup Key and IV" << cpk; std::cout << "\n
" << std::endl; } NAMESPACE_END // Test NAMESPACE_END // CryptoPP