bench2.cpp

00001 // bench2.cpp - written and placed in the public domain by Wei Dai
00002 
00003 #include "bench.h"
00004 #include "rng.h"
00005 #include "files.h"
00006 #include "hex.h"
00007 
00008 #include "rsa.h"
00009 #include "nr.h"
00010 #include "dsa.h"
00011 #include "luc.h"
00012 #include "rw.h"
00013 #include "eccrypto.h"
00014 #include "ecp.h"
00015 #include "ec2n.h"
00016 #include "asn.h"
00017 #include "dh.h"
00018 #include "mqv.h"
00019 #include "xtrcrypt.h"
00020 #include "esign.h"
00021 #include "pssr.h"
00022 #include "oids.h"
00023 #include "randpool.h"
00024 
00025 #include <time.h>
00026 #include <math.h>
00027 #include <iostream>
00028 #include <iomanip>
00029 
00030 USING_NAMESPACE(CryptoPP)
00031 USING_NAMESPACE(std)
00032 
00033 void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken);
00034 
00035 void BenchMarkEncryption(const char *name, PK_Encryptor &key, double timeTotal, bool pc=false)
00036 {
00037         unsigned int len = 16;
00038         LC_RNG rng((word32)time(NULL));
00039         SecByteBlock plaintext(len), ciphertext(key.CiphertextLength(len));
00040         rng.GenerateBlock(plaintext, len);
00041 
00042         clock_t start = clock();
00043         unsigned int i;
00044         double timeTaken;
00045         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
00046                 key.Encrypt(rng, plaintext, len, ciphertext);
00047 
00048         OutputResultOperations(name, "Encryption", pc, i, timeTaken);
00049 
00050         if (!pc && key.GetMaterial().SupportsPrecomputation())
00051         {
00052                 key.AccessMaterial().Precompute(16);
00053                 BenchMarkEncryption(name, key, timeTotal, true);
00054         }
00055 }
00056 
00057 void BenchMarkDecryption(const char *name, PK_Decryptor &priv, PK_Encryptor &pub, double timeTotal)
00058 {
00059         unsigned int len = 16;
00060         LC_RNG rng((word32)time(NULL));
00061         SecByteBlock ciphertext(pub.CiphertextLength(len));
00062         SecByteBlock plaintext(pub.MaxPlaintextLength(ciphertext.size()));
00063         rng.GenerateBlock(plaintext, len);
00064         pub.Encrypt(rng, plaintext, len, ciphertext);
00065 
00066         clock_t start = clock();
00067         unsigned int i;
00068         double timeTaken;
00069         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
00070                 priv.Decrypt(rng, ciphertext, ciphertext.size(), plaintext);
00071 
00072         OutputResultOperations(name, "Decryption", false, i, timeTaken);
00073 }
00074 
00075 void BenchMarkSigning(const char *name, PK_Signer &key, double timeTotal, bool pc=false)
00076 {
00077         unsigned int len = 16;
00078         LC_RNG rng((word32)time(NULL));
00079         AlignedSecByteBlock message(len), signature(key.SignatureLength());
00080         rng.GenerateBlock(message, len);
00081 
00082         clock_t start = clock();
00083         unsigned int i;
00084         double timeTaken;
00085         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
00086                 key.SignMessage(rng, message, len, signature);
00087 
00088         OutputResultOperations(name, "Signature", pc, i, timeTaken);
00089 
00090         if (!pc && key.GetMaterial().SupportsPrecomputation())
00091         {
00092                 key.AccessMaterial().Precompute(16);
00093                 BenchMarkSigning(name, key, timeTotal, true);
00094         }
00095 }
00096 
00097 void BenchMarkVerification(const char *name, const PK_Signer &priv, PK_Verifier &pub, double timeTotal, bool pc=false)
00098 {
00099         unsigned int len = 16;
00100         LC_RNG rng((word32)time(NULL));
00101         AlignedSecByteBlock message(len), signature(pub.SignatureLength());
00102         rng.GenerateBlock(message, len);
00103         priv.SignMessage(rng, message, len, signature);
00104 
00105         clock_t start = clock();
00106         unsigned int i;
00107         double timeTaken;
00108         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
00109                 pub.VerifyMessage(message, len, signature, signature.size());
00110 
00111         OutputResultOperations(name, "Verification", pc, i, timeTaken);
00112 
00113         if (!pc && pub.GetMaterial().SupportsPrecomputation())
00114         {
00115                 pub.AccessMaterial().Precompute(16);
00116                 BenchMarkVerification(name, priv, pub, timeTotal, true);
00117         }
00118 }
00119 
00120 void BenchMarkKeyGen(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
00121 {
00122         LC_RNG rng((word32)time(NULL));
00123         SecByteBlock priv(d.PrivateKeyLength()), pub(d.PublicKeyLength());
00124 
00125         clock_t start = clock();
00126         unsigned int i;
00127         double timeTaken;
00128         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
00129                 d.GenerateKeyPair(rng, priv, pub);
00130 
00131         OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
00132 
00133         if (!pc && d.GetMaterial().SupportsPrecomputation())
00134         {
00135                 d.AccessMaterial().Precompute(16);
00136                 BenchMarkKeyGen(name, d, timeTotal, true);
00137         }
00138 }
00139 
00140 void BenchMarkKeyGen(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
00141 {
00142         LC_RNG rng((word32)time(NULL));
00143         SecByteBlock priv(d.EphemeralPrivateKeyLength()), pub(d.EphemeralPublicKeyLength());
00144 
00145         clock_t start = clock();
00146         unsigned int i;
00147         double timeTaken;
00148         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
00149                 d.GenerateEphemeralKeyPair(rng, priv, pub);
00150 
00151         OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
00152 
00153         if (!pc && d.GetMaterial().SupportsPrecomputation())
00154         {
00155                 d.AccessMaterial().Precompute(16);
00156                 BenchMarkKeyGen(name, d, timeTotal, true);
00157         }
00158 }
00159 
00160 void BenchMarkAgreement(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
00161 {
00162         LC_RNG rng((word32)time(NULL));
00163         SecByteBlock priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());
00164         SecByteBlock pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());
00165         d.GenerateKeyPair(rng, priv1, pub1);
00166         d.GenerateKeyPair(rng, priv2, pub2);
00167         SecByteBlock val(d.AgreedValueLength());
00168 
00169         clock_t start = clock();
00170         unsigned int i;
00171         double timeTaken;
00172         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i+=2)
00173         {
00174                 d.Agree(val, priv1, pub2);
00175                 d.Agree(val, priv2, pub1);
00176         }
00177 
00178         OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
00179 }
00180 
00181 void BenchMarkAgreement(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
00182 {
00183         LC_RNG rng((word32)time(NULL));
00184         SecByteBlock spriv1(d.StaticPrivateKeyLength()), spriv2(d.StaticPrivateKeyLength());
00185         SecByteBlock epriv1(d.EphemeralPrivateKeyLength()), epriv2(d.EphemeralPrivateKeyLength());
00186         SecByteBlock spub1(d.StaticPublicKeyLength()), spub2(d.StaticPublicKeyLength());
00187         SecByteBlock epub1(d.EphemeralPublicKeyLength()), epub2(d.EphemeralPublicKeyLength());
00188         d.GenerateStaticKeyPair(rng, spriv1, spub1);
00189         d.GenerateStaticKeyPair(rng, spriv2, spub2);
00190         d.GenerateEphemeralKeyPair(rng, epriv1, epub1);
00191         d.GenerateEphemeralKeyPair(rng, epriv2, epub2);
00192         SecByteBlock val(d.AgreedValueLength());
00193 
00194         clock_t start = clock();
00195         unsigned int i;
00196         double timeTaken;
00197         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i+=2)
00198         {
00199                 d.Agree(val, spriv1, epriv1, spub2, epub2);
00200                 d.Agree(val, spriv2, epriv2, spub1, epub1);
00201         }
00202 
00203         OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
00204 }
00205 
00206 //VC60 workaround: compiler bug triggered without the extra dummy parameters
00207 template <class SCHEME>
00208 void BenchMarkCrypto(const char *filename, const char *name, double timeTotal, SCHEME *x=NULL)
00209 {
00210         FileSource f(filename, true, new HexDecoder());
00211         typename SCHEME::Decryptor priv(f);
00212         typename SCHEME::Encryptor pub(priv);
00213         BenchMarkEncryption(name, pub, timeTotal);
00214         BenchMarkDecryption(name, priv, pub, timeTotal);
00215 }
00216 
00217 //VC60 workaround: compiler bug triggered without the extra dummy parameters
00218 template <class SCHEME>
00219 void BenchMarkSignature(const char *filename, const char *name, double timeTotal, SCHEME *x=NULL)
00220 {
00221         FileSource f(filename, true, new HexDecoder());
00222         typename SCHEME::Signer priv(f);
00223         typename SCHEME::Verifier pub(priv);
00224         BenchMarkSigning(name, priv, timeTotal);
00225         BenchMarkVerification(name, priv, pub, timeTotal);
00226 }
00227 
00228 //VC60 workaround: compiler bug triggered without the extra dummy parameters
00229 template <class D>
00230 void BenchMarkKeyAgreement(const char *filename, const char *name, double timeTotal, D *x=NULL)
00231 {
00232         FileSource f(filename, true, new HexDecoder());
00233         D d(f);
00234         BenchMarkKeyGen(name, d, timeTotal);
00235         BenchMarkAgreement(name, d, timeTotal);
00236 }
00237 
00238 extern double g_hertz;
00239 
00240 void BenchmarkAll2(double t, double hertz)
00241 {
00242         g_hertz = hertz;
00243 
00244         cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right>" << endl;
00245         cout << "<THEAD><TR><TH>Operation<TH>Milliseconds/Operation" << (g_hertz ? "<TH>Megacycles/Operation" : "") << endl;
00246 
00247         cout << "\n<TBODY style=\"background: yellow\">";
00248         BenchMarkCrypto<RSAES<OAEP<SHA> > >("rsa1024.dat", "RSA 1024", t);
00249         BenchMarkCrypto<LUCES<OAEP<SHA> > >("luc1024.dat", "LUC 1024", t);
00250         BenchMarkCrypto<DLIES<> >("dlie1024.dat", "DLIES 1024", t);
00251         BenchMarkCrypto<LUC_IES<> >("lucc512.dat", "LUCELG 512", t);
00252 
00253         cout << "\n<TBODY style=\"background: white\">";
00254         BenchMarkCrypto<RSAES<OAEP<SHA> > >("rsa2048.dat", "RSA 2048", t);
00255         BenchMarkCrypto<LUCES<OAEP<SHA> > >("luc2048.dat", "LUC 2048", t);
00256         BenchMarkCrypto<DLIES<> >("dlie2048.dat", "DLIES 2048", t);
00257         BenchMarkCrypto<LUC_IES<> >("lucc1024.dat", "LUCELG 1024", t);
00258 
00259         cout << "\n<TBODY style=\"background: yellow\">";
00260         BenchMarkSignature<RSASS<PSSR, SHA> >("rsa1024.dat", "RSA 1024", t);
00261         BenchMarkSignature<RWSS<PSSR, SHA> >("rw1024.dat", "RW 1024", t);
00262         BenchMarkSignature<LUCSS<PSSR, SHA> >("luc1024.dat", "LUC 1024", t);
00263         BenchMarkSignature<NR<SHA> >("nr1024.dat", "NR 1024", t);
00264         BenchMarkSignature<DSA>("dsa1024.dat", "DSA 1024", t);
00265         BenchMarkSignature<LUC_HMP<SHA> >("lucs512.dat", "LUC-HMP 512", t);
00266         BenchMarkSignature<ESIGN<SHA> >("esig1023.dat", "ESIGN 1023", t);
00267         BenchMarkSignature<ESIGN<SHA> >("esig1536.dat", "ESIGN 1536", t);
00268 
00269         cout << "\n<TBODY style=\"background: white\">";
00270         BenchMarkSignature<RSASS<PSSR, SHA> >("rsa2048.dat", "RSA 2048", t);
00271         BenchMarkSignature<RWSS<PSSR, SHA> >("rw2048.dat", "RW 2048", t);
00272         BenchMarkSignature<LUCSS<PSSR, SHA> >("luc2048.dat", "LUC 2048", t);
00273         BenchMarkSignature<NR<SHA> >("nr2048.dat", "NR 2048", t);
00274         BenchMarkSignature<LUC_HMP<SHA> >("lucs1024.dat", "LUC-HMP 1024", t);
00275         BenchMarkSignature<ESIGN<SHA> >("esig2046.dat", "ESIGN 2046", t);
00276 
00277         cout << "\n<TBODY style=\"background: yellow\">";
00278         BenchMarkKeyAgreement<XTR_DH>("xtrdh171.dat", "XTR-DH 171", t);
00279         BenchMarkKeyAgreement<XTR_DH>("xtrdh342.dat", "XTR-DH 342", t);
00280         BenchMarkKeyAgreement<DH>("dh1024.dat", "DH 1024", t);
00281         BenchMarkKeyAgreement<DH>("dh2048.dat", "DH 2048", t);
00282         BenchMarkKeyAgreement<LUC_DH>("lucd512.dat", "LUCDIF 512", t);
00283         BenchMarkKeyAgreement<LUC_DH>("lucd1024.dat", "LUCDIF 1024", t);
00284         BenchMarkKeyAgreement<MQV>("mqv1024.dat", "MQV 1024", t);
00285         BenchMarkKeyAgreement<MQV>("mqv2048.dat", "MQV 2048", t);
00286 
00287         cout << "\n<TBODY style=\"background: white\">";
00288         {
00289                 RandomPool rng;         // not seeded
00290                 ECIES<ECP>::Decryptor cpriv(rng, ASN1::secp256k1());
00291                 ECIES<ECP>::Encryptor cpub(cpriv);
00292                 ECDSA<ECP, SHA>::Signer spriv(cpriv);
00293                 ECDSA<ECP, SHA>::Verifier spub(spriv);
00294                 ECDH<ECP>::Domain ecdhc(ASN1::secp256k1());
00295                 ECMQV<ECP>::Domain ecmqvc(ASN1::secp256k1());
00296 
00297                 BenchMarkEncryption("ECIES over GF(p) 256", cpub, t);
00298                 BenchMarkDecryption("ECIES over GF(p) 256", cpriv, cpub, t);
00299                 BenchMarkSigning("ECNR over GF(p) 256", spriv, t);
00300                 BenchMarkVerification("ECNR over GF(p) 256", spriv, spub, t);
00301                 BenchMarkKeyGen("ECDHC over GF(p) 256", ecdhc, t);
00302                 BenchMarkAgreement("ECDHC over GF(p) 256", ecdhc, t);
00303                 BenchMarkKeyGen("ECMQVC over GF(p) 256", ecmqvc, t);
00304                 BenchMarkAgreement("ECMQVC over GF(p) 256", ecmqvc, t);
00305         }
00306 
00307         cout << "<TBODY style=\"background: yellow\">" << endl;
00308         {
00309                 RandomPool rng;         // not seeded
00310                 ECIES<EC2N>::Decryptor cpriv(rng, ASN1::sect233r1());
00311                 ECIES<EC2N>::Encryptor cpub(cpriv);
00312                 ECDSA<EC2N, SHA>::Signer spriv(cpriv);
00313                 ECDSA<EC2N, SHA>::Verifier spub(spriv);
00314                 ECDH<EC2N>::Domain ecdhc(ASN1::sect233r1());
00315                 ECMQV<EC2N>::Domain ecmqvc(ASN1::sect233r1());
00316 
00317                 BenchMarkEncryption("ECIES over GF(2^n) 233", cpub, t);
00318                 BenchMarkDecryption("ECIES over GF(2^n) 233", cpriv, cpub, t);
00319                 BenchMarkSigning("ECNR over GF(2^n) 233", spriv, t);
00320                 BenchMarkVerification("ECNR over GF(2^n) 233", spriv, spub, t);
00321                 BenchMarkKeyGen("ECDHC over GF(2^n) 233", ecdhc, t);
00322                 BenchMarkAgreement("ECDHC over GF(2^n) 233", ecdhc, t);
00323                 BenchMarkKeyGen("ECMQVC over GF(2^n) 233", ecmqvc, t);
00324                 BenchMarkAgreement("ECMQVC over GF(2^n) 233", ecmqvc, t);
00325         }
00326         cout << "</TABLE>" << endl;
00327 }

Generated on Fri Jun 1 11:11:19 2007 for Crypto++ by  doxygen 1.5.2