Crypto++  5.6.3
Free C++ class library of cryptographic schemes
bench2.cpp
1 // bench2.cpp - written and placed in the public domain by Wei Dai
2 
3 #include "cryptlib.h"
4 #include "pubkey.h"
5 #include "gfpcrypt.h"
6 #include "eccrypto.h"
7 #include "bench.h"
8 #include "validate.h"
9 
10 #include "files.h"
11 #include "filters.h"
12 #include "hex.h"
13 #include "rsa.h"
14 #include "nr.h"
15 #include "dsa.h"
16 #include "luc.h"
17 #include "rw.h"
18 #include "eccrypto.h"
19 #include "ecp.h"
20 #include "ec2n.h"
21 #include "asn.h"
22 #include "dh.h"
23 #include "mqv.h"
24 #include "xtrcrypt.h"
25 #include "esign.h"
26 #include "pssr.h"
27 #include "oids.h"
28 #include "randpool.h"
29 
30 #include <time.h>
31 #include <math.h>
32 #include <iostream>
33 #include <iomanip>
34 
35 // These are noisy enoguh due to test.cpp. Turn them off here.
36 #if CRYPTOPP_GCC_DIAGNOSTIC_AVAILABLE
37 # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
38 #endif
39 
40 USING_NAMESPACE(CryptoPP)
41 USING_NAMESPACE(std)
42 
43 void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken);
44 
45 void BenchMarkEncryption(const char *name, PK_Encryptor &key, double timeTotal, bool pc=false)
46 {
47  unsigned int len = 16;
48  SecByteBlock plaintext(len), ciphertext(key.CiphertextLength(len));
49  GlobalRNG().GenerateBlock(plaintext, len);
50 
51  const clock_t start = clock();
52  unsigned int i;
53  double timeTaken;
54  for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
55  key.Encrypt(GlobalRNG(), plaintext, len, ciphertext);
56 
57  OutputResultOperations(name, "Encryption", pc, i, timeTaken);
58 
59  if (!pc && key.GetMaterial().SupportsPrecomputation())
60  {
61  key.AccessMaterial().Precompute(16);
62  BenchMarkEncryption(name, key, timeTotal, true);
63  }
64 }
65 
66 void BenchMarkDecryption(const char *name, PK_Decryptor &priv, PK_Encryptor &pub, double timeTotal)
67 {
68  unsigned int len = 16;
69  SecByteBlock ciphertext(pub.CiphertextLength(len));
70  SecByteBlock plaintext(pub.MaxPlaintextLength(ciphertext.size()));
71  GlobalRNG().GenerateBlock(plaintext, len);
72  pub.Encrypt(GlobalRNG(), plaintext, len, ciphertext);
73 
74  const clock_t start = clock();
75  unsigned int i;
76  double timeTaken;
77  for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
78  priv.Decrypt(GlobalRNG(), ciphertext, ciphertext.size(), plaintext);
79 
80  OutputResultOperations(name, "Decryption", false, i, timeTaken);
81 }
82 
83 void BenchMarkSigning(const char *name, PK_Signer &key, double timeTotal, bool pc=false)
84 {
85  unsigned int len = 16;
86  AlignedSecByteBlock message(len), signature(key.SignatureLength());
87  GlobalRNG().GenerateBlock(message, len);
88 
89  const clock_t start = clock();
90  unsigned int i;
91  double timeTaken;
92  for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
93  key.SignMessage(GlobalRNG(), message, len, signature);
94 
95  OutputResultOperations(name, "Signature", pc, i, timeTaken);
96 
97  if (!pc && key.GetMaterial().SupportsPrecomputation())
98  {
99  key.AccessMaterial().Precompute(16);
100  BenchMarkSigning(name, key, timeTotal, true);
101  }
102 }
103 
104 void BenchMarkVerification(const char *name, const PK_Signer &priv, PK_Verifier &pub, double timeTotal, bool pc=false)
105 {
106  unsigned int len = 16;
107  AlignedSecByteBlock message(len), signature(pub.SignatureLength());
108  GlobalRNG().GenerateBlock(message, len);
109  priv.SignMessage(GlobalRNG(), message, len, signature);
110 
111  const clock_t start = clock();
112  unsigned int i;
113  double timeTaken;
114  for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
115  {
116  // The return value is ignored because we are interested in throughput
117  bool unused = pub.VerifyMessage(message, len, signature, signature.size());
118  CRYPTOPP_UNUSED(unused);
119  }
120 
121  OutputResultOperations(name, "Verification", pc, i, timeTaken);
122 
123  if (!pc && pub.GetMaterial().SupportsPrecomputation())
124  {
125  pub.AccessMaterial().Precompute(16);
126  BenchMarkVerification(name, priv, pub, timeTotal, true);
127  }
128 }
129 
130 void BenchMarkKeyGen(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
131 {
132  SecByteBlock priv(d.PrivateKeyLength()), pub(d.PublicKeyLength());
133 
134  const clock_t start = clock();
135  unsigned int i;
136  double timeTaken;
137  for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
138  d.GenerateKeyPair(GlobalRNG(), priv, pub);
139 
140  OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
141 
142  if (!pc && d.GetMaterial().SupportsPrecomputation())
143  {
144  d.AccessMaterial().Precompute(16);
145  BenchMarkKeyGen(name, d, timeTotal, true);
146  }
147 }
148 
149 void BenchMarkKeyGen(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
150 {
152 
153  const clock_t start = clock();
154  unsigned int i;
155  double timeTaken;
156  for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
157  d.GenerateEphemeralKeyPair(GlobalRNG(), priv, pub);
158 
159  OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
160 
161  if (!pc && d.GetMaterial().SupportsPrecomputation())
162  {
163  d.AccessMaterial().Precompute(16);
164  BenchMarkKeyGen(name, d, timeTotal, true);
165  }
166 }
167 
168 void BenchMarkAgreement(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
169 {
170  SecByteBlock priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());
171  SecByteBlock pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());
172  d.GenerateKeyPair(GlobalRNG(), priv1, pub1);
173  d.GenerateKeyPair(GlobalRNG(), priv2, pub2);
175 
176  const clock_t start = clock();
177  unsigned int i;
178  double timeTaken;
179  for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i+=2)
180  {
181  d.Agree(val, priv1, pub2);
182  d.Agree(val, priv2, pub1);
183  }
184 
185  OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
186 }
187 
188 void BenchMarkAgreement(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
189 {
194  d.GenerateStaticKeyPair(GlobalRNG(), spriv1, spub1);
195  d.GenerateStaticKeyPair(GlobalRNG(), spriv2, spub2);
196  d.GenerateEphemeralKeyPair(GlobalRNG(), epriv1, epub1);
197  d.GenerateEphemeralKeyPair(GlobalRNG(), epriv2, epub2);
199 
200  const clock_t start = clock();
201  unsigned int i;
202  double timeTaken;
203  for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i+=2)
204  {
205  d.Agree(val, spriv1, epriv1, spub2, epub2);
206  d.Agree(val, spriv2, epriv2, spub1, epub1);
207  }
208 
209  OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
210 }
211 
212 //VC60 workaround: compiler bug triggered without the extra dummy parameters
213 template <class SCHEME>
214 void BenchMarkCrypto(const char *filename, const char *name, double timeTotal, SCHEME *x=NULL)
215 {
216  CRYPTOPP_UNUSED(x);
217 
218  FileSource f(filename, true, new HexDecoder());
219  typename SCHEME::Decryptor priv(f);
220  typename SCHEME::Encryptor pub(priv);
221  BenchMarkEncryption(name, pub, timeTotal);
222  BenchMarkDecryption(name, priv, pub, timeTotal);
223 }
224 
225 //VC60 workaround: compiler bug triggered without the extra dummy parameters
226 template <class SCHEME>
227 void BenchMarkSignature(const char *filename, const char *name, double timeTotal, SCHEME *x=NULL)
228 {
229  CRYPTOPP_UNUSED(x);
230 
231  FileSource f(filename, true, new HexDecoder());
232  typename SCHEME::Signer priv(f);
233  typename SCHEME::Verifier pub(priv);
234  BenchMarkSigning(name, priv, timeTotal);
235  BenchMarkVerification(name, priv, pub, timeTotal);
236 }
237 
238 //VC60 workaround: compiler bug triggered without the extra dummy parameters
239 template <class D>
240 void BenchMarkKeyAgreement(const char *filename, const char *name, double timeTotal, D *x=NULL)
241 {
242  CRYPTOPP_UNUSED(x);
243 
244  FileSource f(filename, true, new HexDecoder());
245  D d(f);
246  BenchMarkKeyGen(name, d, timeTotal);
247  BenchMarkAgreement(name, d, timeTotal);
248 }
249 
250 extern double g_hertz;
251 
252 void BenchmarkAll2(double t, double hertz)
253 {
254  g_hertz = hertz;
255 
256  cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right>" << endl;
257  cout << "<THEAD><TR><TH>Operation<TH>Milliseconds/Operation" << (g_hertz ? "<TH>Megacycles/Operation" : "") << endl;
258 
259  cout << "\n<TBODY style=\"background: yellow\">";
260  BenchMarkCrypto<RSAES<OAEP<SHA> > >(CRYPTOPP_DATA_DIR "TestData/rsa1024.dat", "RSA 1024", t);
261  BenchMarkCrypto<LUCES<OAEP<SHA> > >(CRYPTOPP_DATA_DIR "TestData/luc1024.dat", "LUC 1024", t);
262  BenchMarkCrypto<DLIES<> >(CRYPTOPP_DATA_DIR "TestData/dlie1024.dat", "DLIES 1024", t);
263  BenchMarkCrypto<LUC_IES<> >(CRYPTOPP_DATA_DIR "TestData/lucc512.dat", "LUCELG 512", t);
264 
265  cout << "\n<TBODY style=\"background: white\">";
266  BenchMarkCrypto<RSAES<OAEP<SHA> > >(CRYPTOPP_DATA_DIR "TestData/rsa2048.dat", "RSA 2048", t);
267  BenchMarkCrypto<LUCES<OAEP<SHA> > >(CRYPTOPP_DATA_DIR "TestData/luc2048.dat", "LUC 2048", t);
268  BenchMarkCrypto<DLIES<> >(CRYPTOPP_DATA_DIR "TestData/dlie2048.dat", "DLIES 2048", t);
269  BenchMarkCrypto<LUC_IES<> >(CRYPTOPP_DATA_DIR "TestData/lucc1024.dat", "LUCELG 1024", t);
270 
271  cout << "\n<TBODY style=\"background: yellow\">";
272  BenchMarkSignature<RSASS<PSSR, SHA> >(CRYPTOPP_DATA_DIR "TestData/rsa1024.dat", "RSA 1024", t);
273  BenchMarkSignature<RWSS<PSSR, SHA> >(CRYPTOPP_DATA_DIR "TestData/rw1024.dat", "RW 1024", t);
274  BenchMarkSignature<LUCSS<PSSR, SHA> >(CRYPTOPP_DATA_DIR "TestData/luc1024.dat", "LUC 1024", t);
275  BenchMarkSignature<NR<SHA> >(CRYPTOPP_DATA_DIR "TestData/nr1024.dat", "NR 1024", t);
276  BenchMarkSignature<DSA>(CRYPTOPP_DATA_DIR "TestData/dsa1024.dat", "DSA 1024", t);
277  BenchMarkSignature<LUC_HMP<SHA> >(CRYPTOPP_DATA_DIR "TestData/lucs512.dat", "LUC-HMP 512", t);
278  BenchMarkSignature<ESIGN<SHA> >(CRYPTOPP_DATA_DIR "TestData/esig1023.dat", "ESIGN 1023", t);
279  BenchMarkSignature<ESIGN<SHA> >(CRYPTOPP_DATA_DIR "TestData/esig1536.dat", "ESIGN 1536", t);
280 
281  cout << "\n<TBODY style=\"background: white\">";
282  BenchMarkSignature<RSASS<PSSR, SHA> >(CRYPTOPP_DATA_DIR "TestData/rsa2048.dat", "RSA 2048", t);
283  BenchMarkSignature<RWSS<PSSR, SHA> >(CRYPTOPP_DATA_DIR "TestData/rw2048.dat", "RW 2048", t);
284  BenchMarkSignature<LUCSS<PSSR, SHA> >(CRYPTOPP_DATA_DIR "TestData/luc2048.dat", "LUC 2048", t);
285  BenchMarkSignature<NR<SHA> >(CRYPTOPP_DATA_DIR "TestData/nr2048.dat", "NR 2048", t);
286  BenchMarkSignature<LUC_HMP<SHA> >(CRYPTOPP_DATA_DIR "TestData/lucs1024.dat", "LUC-HMP 1024", t);
287  BenchMarkSignature<ESIGN<SHA> >(CRYPTOPP_DATA_DIR "TestData/esig2046.dat", "ESIGN 2046", t);
288 
289  cout << "\n<TBODY style=\"background: yellow\">";
290  BenchMarkKeyAgreement<XTR_DH>(CRYPTOPP_DATA_DIR "TestData/xtrdh171.dat", "XTR-DH 171", t);
291  BenchMarkKeyAgreement<XTR_DH>(CRYPTOPP_DATA_DIR "TestData/xtrdh342.dat", "XTR-DH 342", t);
292  BenchMarkKeyAgreement<DH>(CRYPTOPP_DATA_DIR "TestData/dh1024.dat", "DH 1024", t);
293  BenchMarkKeyAgreement<DH>(CRYPTOPP_DATA_DIR "TestData/dh2048.dat", "DH 2048", t);
294  BenchMarkKeyAgreement<LUC_DH>(CRYPTOPP_DATA_DIR "TestData/lucd512.dat", "LUCDIF 512", t);
295  BenchMarkKeyAgreement<LUC_DH>(CRYPTOPP_DATA_DIR "TestData/lucd1024.dat", "LUCDIF 1024", t);
296  BenchMarkKeyAgreement<MQV>(CRYPTOPP_DATA_DIR "TestData/mqv1024.dat", "MQV 1024", t);
297  BenchMarkKeyAgreement<MQV>(CRYPTOPP_DATA_DIR "TestData/mqv2048.dat", "MQV 2048", t);
298 
299  cout << "\n<TBODY style=\"background: white\">";
300  {
301  ECIES<ECP>::Decryptor cpriv(GlobalRNG(), ASN1::secp256k1());
302  ECIES<ECP>::Encryptor cpub(cpriv);
303  ECDSA<ECP, SHA>::Signer spriv(cpriv);
304  ECDSA<ECP, SHA>::Verifier spub(spriv);
305  ECDH<ECP>::Domain ecdhc(ASN1::secp256k1());
306  ECMQV<ECP>::Domain ecmqvc(ASN1::secp256k1());
307 
308  BenchMarkEncryption("ECIES over GF(p) 256", cpub, t);
309  BenchMarkDecryption("ECIES over GF(p) 256", cpriv, cpub, t);
310  BenchMarkSigning("ECDSA over GF(p) 256", spriv, t);
311  BenchMarkVerification("ECDSA over GF(p) 256", spriv, spub, t);
312  BenchMarkKeyGen("ECDHC over GF(p) 256", ecdhc, t);
313  BenchMarkAgreement("ECDHC over GF(p) 256", ecdhc, t);
314  BenchMarkKeyGen("ECMQVC over GF(p) 256", ecmqvc, t);
315  BenchMarkAgreement("ECMQVC over GF(p) 256", ecmqvc, t);
316  }
317 
318  cout << "<TBODY style=\"background: yellow\">" << endl;
319  {
320  ECIES<EC2N>::Decryptor cpriv(GlobalRNG(), ASN1::sect233r1());
321  ECIES<EC2N>::Encryptor cpub(cpriv);
322  ECDSA<EC2N, SHA>::Signer spriv(cpriv);
323  ECDSA<EC2N, SHA>::Verifier spub(spriv);
324  ECDH<EC2N>::Domain ecdhc(ASN1::sect233r1());
325  ECMQV<EC2N>::Domain ecmqvc(ASN1::sect233r1());
326 
327  BenchMarkEncryption("ECIES over GF(2^n) 233", cpub, t);
328  BenchMarkDecryption("ECIES over GF(2^n) 233", cpriv, cpub, t);
329  BenchMarkSigning("ECDSA over GF(2^n) 233", spriv, t);
330  BenchMarkVerification("ECDSA over GF(2^n) 233", spriv, spub, t);
331  BenchMarkKeyGen("ECDHC over GF(2^n) 233", ecdhc, t);
332  BenchMarkAgreement("ECDHC over GF(2^n) 233", ecdhc, t);
333  BenchMarkKeyGen("ECMQVC over GF(2^n) 233", ecmqvc, t);
334  BenchMarkAgreement("ECMQVC over GF(2^n) 233", ecmqvc, t);
335  }
336  cout << "</TABLE>" << endl;
337 }
virtual void Precompute(unsigned int precomputationStorage)
Perform precomputation.
Definition: cryptlib.h:2106
virtual void GenerateEphemeralKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
Generate private/public key pair.
Definition: cryptlib.cpp:933
virtual unsigned int StaticPublicKeyLength() const =0
Provides the size of the static public key.
virtual bool VerifyMessage(const byte *message, size_t messageLen, const byte *signature, size_t signatureLen) const
Check whether input signature is a valid signature for input message.
Definition: cryptlib.cpp:897
Class file for Randomness Pool.
virtual DecodingResult Decrypt(RandomNumberGenerator &rng, const byte *ciphertext, size_t ciphertextLength, byte *plaintext, const NameValuePairs &parameters=g_nullNameValuePairs) const =0
Decrypt a byte string.
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
Definition: cryptlib.cpp:329
This file contains helper classes/functions for implementing public key algorithms.
virtual unsigned int AgreedValueLength() const =0
Provides the size of the agreed value.
file-based implementation of Source interface
Definition: files.h:55
Classes for Elliptic Curves over prime fields.
Interface for public-key signers.
Definition: cryptlib.h:2524
Interface for public-key encryptors.
Definition: cryptlib.h:2332
Decode base 16 data back to bytes.
Definition: hex.h:28
Abstract base classes that provide a uniform interface to this library.
CryptoMaterial & AccessMaterial()
Retrieves a reference to a Private Key.
Definition: cryptlib.h:2246
virtual bool Agree(byte *agreedValue, const byte *staticPrivateKey, const byte *ephemeralPrivateKey, const byte *staticOtherPublicKey, const byte *ephemeralOtherPublicKey, bool validateStaticOtherPublicKey=true) const =0
Derive agreed value.
virtual bool SupportsPrecomputation() const
Determines whether the object supports precomputation.
Definition: cryptlib.h:2096
Object identifiers for algorthms and schemes.
STL namespace.
SecBlock typedef.
Definition: secblock.h:723
const CryptoMaterial & GetMaterial() const
Retrieves a reference to a Private Key.
Definition: cryptlib.h:2249
This file contains classes that implement the ESIGN signature schemes as defined in IEEE P1363a...
virtual unsigned int EphemeralPublicKeyLength() const =0
Provides the size of ephemeral public key.
Classes for Elliptic Curves over binary fields.
Interface for domains of simple key agreement protocols.
Definition: cryptlib.h:2665
Classes for Rabin-Williams signature schemes.
Interface for public-key decryptors.
Definition: cryptlib.h:2368
MQV domain for performing authenticated key agreement.
Definition: mqv.h:26
virtual void GenerateKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
Generate a private/public key pair.
Definition: cryptlib.cpp:921
virtual unsigned int AgreedValueLength() const =0
Provides the size of the agreed value.
const CryptoMaterial & GetMaterial() const
Retrieves a reference to Crypto Parameters.
Definition: cryptlib.h:2272
Classes for Diffie-Hellman key exchange.
Classes for HexEncoder and HexDecoder.
virtual void GenerateStaticKeyPair(RandomNumberGenerator &rng, byte *privateKey, byte *publicKey) const
Generate a static private/public key pair.
Definition: cryptlib.cpp:927
virtual size_t MaxPlaintextLength(size_t ciphertextLength) const =0
Provides the maximum length of plaintext for a given ciphertext length.
virtual size_t CiphertextLength(size_t plaintextLength) const =0
Calculate the length of ciphertext given length of plaintext.
const CryptoMaterial & GetMaterial() const
Retrieves a reference to a Public Key.
Definition: cryptlib.h:2224
Implementation of schemes based on DL over GF(p)
SecBlock using AllocatorWithCleanup typedef.
Definition: secblock.h:729
Classes for the DSA signature algorithm.
virtual size_t SignMessage(RandomNumberGenerator &rng, const byte *message, size_t messageLen, byte *signature) const
Sign a message.
Definition: cryptlib.cpp:875
Diffie-Hellman domain.
Definition: dh.h:23
virtual unsigned int PrivateKeyLength() const =0
Provides the size of the private key.
Classes and functions for working with ANS.1 objects.
virtual bool Agree(byte *agreedValue, const byte *privateKey, const byte *otherPublicKey, bool validateOtherPublicKey=true) const =0
Derive agreed value.
Implementation of BufferedTransformation's attachment interface.
"The XTR public key system" by Arjen K.
Classes for the RSA cryptosystem.
CryptoMaterial & AccessMaterial()
Retrieves a reference to a Public Key.
Definition: cryptlib.h:2220
Interface for public-key signature verifiers.
Definition: cryptlib.h:2591
virtual unsigned int StaticPrivateKeyLength() const =0
Provides the size of the static private key.
CryptoMaterial & AccessMaterial()
Retrieves a reference to Crypto Parameters.
Definition: cryptlib.h:2269
virtual unsigned int PublicKeyLength() const =0
Provides the size of the public key.
virtual unsigned int EphemeralPrivateKeyLength() const =0
Provides the size of ephemeral private key.
Classes and functions for Elliptic Curves over prime and binary fields.
Crypto++ library namespace.
Interface for domains of authenticated key agreement protocols.
Definition: cryptlib.h:2730
virtual void Encrypt(RandomNumberGenerator &rng, const byte *plaintext, size_t plaintextLength, byte *ciphertext, const NameValuePairs &parameters=g_nullNameValuePairs) const =0
Encrypt a byte string.
Classes for Menezes–Qu–Vanstone (MQV) key agreement.
Classes for probablistic signature schemes.
virtual size_t SignatureLength() const =0
Provides the signature length if it only depends on the key.