Crypto++ 8.1 Free C++ class library of cryptographic schemes

bench2.cpp
1 // bench2.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 #if CRYPTOPP_MSC_VERSION
16 # pragma warning(disable: 4355)
17 #endif
18
19 #if CRYPTOPP_MSC_VERSION
20 # pragma warning(disable: 4505 4355)
21 #endif
22
23 NAMESPACE_BEGIN(CryptoPP)
24 NAMESPACE_BEGIN(Test)
25
26 void BenchMarkKeying(SimpleKeyingInterface &c, size_t keyLength, const NameValuePairs &params)
27 {
28 unsigned long iterations = 0;
29 double timeTaken;
30
31 clock_t start = ::clock();
32 do
33 {
34 for (unsigned int i=0; i<1024; i++)
35 c.SetKey(defaultKey, keyLength, params);
36 timeTaken = double(::clock() - start) / CLOCK_TICKS_PER_SECOND;
37 iterations += 1024;
38 }
39 while (timeTaken < g_allocatedTime);
40
41 OutputResultKeying(iterations, timeTaken);
42 }
43
44 void BenchMark(const char *name, AuthenticatedSymmetricCipher &cipher, double timeTotal)
45 {
46 if (cipher.NeedsPrespecifiedDataLengths())
47 cipher.SpecifyDataLengths(0, cipher.MaxMessageLength(), 0);
48
49 BenchMark(name, static_cast<StreamTransformation &>(cipher), timeTotal);
50 }
51
52 template <class T_FactoryOutput, class T_Interface>
53 void BenchMarkByName2(const char *factoryName, size_t keyLength=0, const char *displayName=NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
54 {
55 std::string name(factoryName ? factoryName : "");
56 member_ptr<T_FactoryOutput> obj(ObjectFactoryRegistry<T_FactoryOutput>::Registry().CreateObject(name.c_str()));
57
58 if (keyLength == 0)
59 keyLength = obj->DefaultKeyLength();
60
61 if (displayName != NULLPTR)
62 name = displayName;
63 else if (keyLength != 0)
64 name += " (" + IntToString(keyLength * 8) + "-bit key)";
65
66 obj->SetKey(defaultKey, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false)));
67 BenchMark(name.c_str(), *static_cast<T_Interface *>(obj.get()), g_allocatedTime);
68 BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(defaultKey, obj->IVSize()), false)));
69 }
70
71 template <class T_FactoryOutput>
72 void BenchMarkByName(const char *factoryName, size_t keyLength=0, const char *displayName=NULLPTR, const NameValuePairs &params = g_nullNameValuePairs)
73 {
74 BenchMarkByName2<T_FactoryOutput,T_FactoryOutput>(factoryName, keyLength, displayName, params);
75 }
76
77 void Benchmark2(double t, double hertz)
78 {
79 g_allocatedTime = t;
80 g_hertz = hertz;
81
82 const char *cpb, *cpk;
83 if (g_hertz > 1.0f)
84 {
85 cpb = "<TH>Cycles/Byte";
86 cpk = "<TH>Cycles to<BR>Setup Key and IV";
87 }
88 else
89 {
90 cpb = cpk = "";
91 }
92
93 std::cout << "\n<TABLE>";
94 std::cout << "\n<COLGROUP><COL style=\"text-align: left;\"><COL style=\"text-align: right;\"><COL style=";
95 std::cout << "\"text-align: right;\"><COL style=\"text-align: right;\"><COL style=\"text-align: right;\">";
96 std::cout << "\n<THEAD style=\"background: #F0F0F0\">";
97 std::cout << "\n<TR><TH>Algorithm<TH>Provider<TH>MiB/Second" << cpb;
98 std::cout << "<TH>Microseconds to<BR>Setup Key and IV" << cpk;
99
100 std::cout << "\n<TBODY style=\"background: white;\">";
101 {
102 #if CRYPTOPP_AESNI_AVAILABLE
103 if (HasCLMUL())
104 BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES)");
105 else
106 #elif CRYPTOPP_ARM_PMULL_AVAILABLE
107 if (HasPMULL())
108 BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES)");
109 else
110 #elif CRYPTOPP_POWER8_VMULL_AVAILABLE
111 if (HasPMULL())
112 BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES)");
113 else
114 #endif
115 {
116 BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (2K tables)", MakeParameters(Name::TableSize(), 2048));
117 BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (64K tables)", MakeParameters(Name::TableSize(), 64 * 1024));
118 }
119
120 BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-64");
121 BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-128");
122 BenchMarkByName<MessageAuthenticationCode>("HMAC(SHA-1)");
123 BenchMarkByName<MessageAuthenticationCode>("HMAC(SHA-256)");
124 BenchMarkByName<MessageAuthenticationCode>("Two-Track-MAC");
125 BenchMarkByName<MessageAuthenticationCode>("CMAC(AES)");
126 BenchMarkByName<MessageAuthenticationCode>("DMAC(AES)");
127 BenchMarkByName<MessageAuthenticationCode>("Poly1305(AES)");
128 BenchMarkByName<MessageAuthenticationCode>("Poly1305TLS");
129 BenchMarkByName<MessageAuthenticationCode>("BLAKE2s");
130 BenchMarkByName<MessageAuthenticationCode>("BLAKE2b");
131 BenchMarkByName<MessageAuthenticationCode>("SipHash-2-4");
132 BenchMarkByName<MessageAuthenticationCode>("SipHash-4-8");
133 }
134
135 std::cout << "\n<TBODY style=\"background: yellow;\">";
136 {
137 BenchMarkByName<SymmetricCipher>("Panama-LE");
138 BenchMarkByName<SymmetricCipher>("Panama-BE");
139 BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20");
140 BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/12", MakeParameters(Name::Rounds(), 12));
141 BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/8", MakeParameters(Name::Rounds(), 8));
142 BenchMarkByName<SymmetricCipher>("ChaCha", 0, "ChaCha20");
143 BenchMarkByName<SymmetricCipher>("ChaCha", 0, "ChaCha12", MakeParameters(Name::Rounds(), 12));
144 BenchMarkByName<SymmetricCipher>("ChaCha", 0, "ChaCha8", MakeParameters(Name::Rounds(), 8));
145 BenchMarkByName<SymmetricCipher>("ChaChaTLS");
146 BenchMarkByName<SymmetricCipher>("Sosemanuk");
147 BenchMarkByName<SymmetricCipher>("Rabbit");
148 BenchMarkByName<SymmetricCipher>("RabbitWithIV");
149 BenchMarkByName<SymmetricCipher>("HC-128");
150 BenchMarkByName<SymmetricCipher>("HC-256");
151 BenchMarkByName<SymmetricCipher>("MARC4");
152 BenchMarkByName<SymmetricCipher>("SEAL-3.0-LE");
153 BenchMarkByName<SymmetricCipher>("WAKE-OFB-LE");
154 }
155
156 std::cout << "\n<TBODY style=\"background: white;\">";
157 {
158 BenchMarkByName<SymmetricCipher>("AES/CTR", 16);
159 BenchMarkByName<SymmetricCipher>("AES/CTR", 24);
160 BenchMarkByName<SymmetricCipher>("AES/CTR", 32);
161 BenchMarkByName<SymmetricCipher>("AES/CBC", 16);
162 BenchMarkByName<SymmetricCipher>("AES/CBC", 24);
163 BenchMarkByName<SymmetricCipher>("AES/CBC", 32);
164 BenchMarkByName<SymmetricCipher>("AES/OFB", 16);
165 BenchMarkByName<SymmetricCipher>("AES/CFB", 16);
166 BenchMarkByName<SymmetricCipher>("AES/ECB", 16);
167 BenchMarkByName<SymmetricCipher>("ARIA/CTR", 16);
168 BenchMarkByName<SymmetricCipher>("ARIA/CTR", 32);
169 BenchMarkByName<SymmetricCipher>("HIGHT/CTR");
170 BenchMarkByName<SymmetricCipher>("Camellia/CTR", 16);
171 BenchMarkByName<SymmetricCipher>("Camellia/CTR", 32);
172 BenchMarkByName<SymmetricCipher>("Twofish/CTR");
173 BenchMarkByName<SymmetricCipher>("Threefish-256(256)/CTR", 32);
174 BenchMarkByName<SymmetricCipher>("Threefish-512(512)/CTR", 64);
175 BenchMarkByName<SymmetricCipher>("Threefish-1024(1024)/CTR", 128);
176 BenchMarkByName<SymmetricCipher>("Serpent/CTR");
177 BenchMarkByName<SymmetricCipher>("CAST-128/CTR");
178 BenchMarkByName<SymmetricCipher>("CAST-256/CTR", 32);
179 BenchMarkByName<SymmetricCipher>("RC6/CTR");
180 BenchMarkByName<SymmetricCipher>("MARS/CTR");
181 BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 16);
182 BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 64);
183 BenchMarkByName<SymmetricCipher>("DES/CTR");
184 BenchMarkByName<SymmetricCipher>("DES-XEX3/CTR");
185 BenchMarkByName<SymmetricCipher>("DES-EDE3/CTR");
186 BenchMarkByName<SymmetricCipher>("IDEA/CTR");
187 BenchMarkByName<SymmetricCipher>("RC5/CTR", 0, "RC5 (r=16)");
188 BenchMarkByName<SymmetricCipher>("Blowfish/CTR");
189 BenchMarkByName<SymmetricCipher>("SKIPJACK/CTR");
190 BenchMarkByName<SymmetricCipher>("SEED/CTR", 0, "SEED/CTR (1/2 K table)");
191 BenchMarkByName<SymmetricCipher>("SM4/CTR");
192
193 BenchMarkByName<SymmetricCipher>("Kalyna-128/CTR", 16, "Kalyna-128(128)/CTR (128-bit key)");
194 BenchMarkByName<SymmetricCipher>("Kalyna-128/CTR", 32, "Kalyna-128(256)/CTR (256-bit key)");
195 BenchMarkByName<SymmetricCipher>("Kalyna-256/CTR", 32, "Kalyna-256(256)/CTR (256-bit key)");
196 BenchMarkByName<SymmetricCipher>("Kalyna-256/CTR", 64, "Kalyna-256(512)/CTR (512-bit key)");
197 BenchMarkByName<SymmetricCipher>("Kalyna-512/CTR", 64, "Kalyna-512(512)/CTR (512-bit key)");
198 }
199
200 std::cout << "\n<TBODY style=\"background: yellow;\">";
201 {
202 BenchMarkByName<SymmetricCipher>("CHAM-64/CTR", 16, "CHAM-64(128)/CTR (128-bit key)");
203 BenchMarkByName<SymmetricCipher>("CHAM-128/CTR", 16, "CHAM-128(128)/CTR (128-bit key)");
204 BenchMarkByName<SymmetricCipher>("CHAM-128/CTR", 32, "CHAM-128(256)/CTR (256-bit key)");
205
206 BenchMarkByName<SymmetricCipher>("LEA-128/CTR", 16, "LEA-128(128)/CTR (128-bit key)");
207 BenchMarkByName<SymmetricCipher>("LEA-128/CTR", 24, "LEA-128(192)/CTR (192-bit key)");
208 BenchMarkByName<SymmetricCipher>("LEA-128/CTR", 32, "LEA-128(256)/CTR (256-bit key)");
209
210 BenchMarkByName<SymmetricCipher>("SIMECK-32/CTR", 8, "SIMECK-32(64)/CTR (64-bit key)");
211 BenchMarkByName<SymmetricCipher>("SIMECK-64/CTR", 16, "SIMECK-64(128)/CTR (128-bit key)");
212
213 BenchMarkByName<SymmetricCipher>("SIMON-64/CTR", 12, "SIMON-64(96)/CTR (96-bit key)");
214 BenchMarkByName<SymmetricCipher>("SIMON-64/CTR", 16, "SIMON-64(128)/CTR (128-bit key)");
215 BenchMarkByName<SymmetricCipher>("SIMON-128/CTR", 16, "SIMON-128(128)/CTR (128-bit key)");
216 BenchMarkByName<SymmetricCipher>("SIMON-128/CTR", 24, "SIMON-128(192)/CTR (192-bit key)");
217 BenchMarkByName<SymmetricCipher>("SIMON-128/CTR", 32, "SIMON-128(256)/CTR (256-bit key)");
218
219 BenchMarkByName<SymmetricCipher>("SPECK-64/CTR", 12, "SPECK-64(96)/CTR (96-bit key)");
220 BenchMarkByName<SymmetricCipher>("SPECK-64/CTR", 16, "SPECK-64(128)/CTR (128-bit key)");
221 BenchMarkByName<SymmetricCipher>("SPECK-128/CTR", 16, "SPECK-128(128)/CTR (128-bit key)");
222 BenchMarkByName<SymmetricCipher>("SPECK-128/CTR", 24, "SPECK-128(192)/CTR (192-bit key)");
223 BenchMarkByName<SymmetricCipher>("SPECK-128/CTR", 32, "SPECK-128(256)/CTR (256-bit key)");
224
225 BenchMarkByName<SymmetricCipher>("TEA/CTR");
226 BenchMarkByName<SymmetricCipher>("XTEA/CTR");
227 }
228
229 std::cout << "\n<TBODY style=\"background: white;\">";
230 {
231 #if CRYPTOPP_AESNI_AVAILABLE
232 if (HasCLMUL())
233 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM");
234 else
235 #elif CRYPTOPP_ARM_PMULL_AVAILABLE
236 if (HasPMULL())
237 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM");
238 else
239 #elif CRYPTOPP_POWER8_VMULL_AVAILABLE
240 if (HasPMULL())
241 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM");
242 else
243 #endif
244 {
245 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (2K tables)", MakeParameters(Name::TableSize(), 2048));
246 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (64K tables)", MakeParameters(Name::TableSize(), 64 * 1024));
247 }
248 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/CCM");
249 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/EAX");
250 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("ChaCha20/Poly1305");
251 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("XChaCha20/Poly1305");
252 }
253
254 std::cout << "\n</TABLE>" << std::endl;
255 }
256
257 NAMESPACE_END // Test
258 NAMESPACE_END // CryptoPP
ConstByteArrayParameter
Used to pass byte array input as part of a NameValuePairs object.
Definition: algparam.h:20
argnames.h
Standard names for retrieving values by name when working with NameValuePairs.
algparam.h
Classes for working with NameValuePairs.
AuthenticatedSymmetricCipher::NeedsPrespecifiedDataLengths
virtual bool NeedsPrespecifiedDataLengths() const
Determines if data lengths must be specified prior to inputting data.
Definition: cryptlib.h:1320
Name::Rounds
const char * Rounds()
int
Definition: argnames.h:24
AuthenticatedSymmetricCipher
Interface for authenticated encryption modes of operation.
Definition: cryptlib.h:1288
cryptlib.h
Abstract base classes that provide a uniform interface to this library.
smartptr.h
Classes for automatic resource management.
stdcpp.h
Common C++ header files.
CombinedNameValuePairs
Combines two sets of NameValuePairs.
Definition: algparam.h:124
member_ptr
Pointer that overloads operator ->
Definition: smartptr.h:36
Name::TableSize
const char * TableSize()
int, in bytes
Definition: argnames.h:81
MakeParameters
AlgorithmParameters MakeParameters(const char *name, const T &value, bool throwIfNotUsed=true)
Create an object that implements NameValuePairs.
Definition: algparam.h:502
HasCLMUL
bool HasCLMUL()
Determines Carryless Multiply availability.
Definition: cpu.h:177
SimpleKeyingInterface
Interface for algorithms that take byte strings as keys.
Definition: cryptlib.h:613
g_nullNameValuePairs
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Definition: cryptlib.h:500
Test
Namespace containing testing and benchmark classes.
Definition: cryptlib.h:547
factory.h
Classes and functions for registering and locating library objects.
cpu.h
Functions for CPU features and intrinsics.
Name::IV
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
Definition: argnames.h:21
AuthenticatedSymmetricCipher::MaxMessageLength
virtual lword MaxMessageLength() const =0
Provides the maximum length of encrypted data.
IntToString
std::string IntToString(T value, unsigned int base=10)
Converts a value to a string.
Definition: misc.h:604
AuthenticatedSymmetricCipher::SpecifyDataLengths
void SpecifyDataLengths(lword headerLength, lword messageLength, lword footerLength=0)
Prescribes the data lengths.
Definition: cryptlib.cpp:233
CryptoPP
Crypto++ library namespace.
ObjectFactoryRegistry
Object factory registry.
Definition: factory.h:42
HasPMULL
bool HasPMULL()
Determine if an ARM processor provides Polynomial Multiplication.
Definition: cpu.h:408
NameValuePairs
Interface for retrieving values given their names.
Definition: cryptlib.h:293