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