Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

# 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 "rabin.h"
    00013 #include "rw.h"
    00014 #include "eccrypto.h"
    00015 #include "ecp.h"
    00016 #include "ec2n.h"
    00017 #include "asn.h"
    00018 #include "dh.h"
    00019 #include "mqv.h"
    00020 #include "xtrcrypt.h"
    00021 #include "esign.h"
    00022
    00023 #include <time.h>
    00024 #include <math.h>
    00025 #include <iostream>
    00026 #include <iomanip>
    00027
    00028 USING_NAMESPACE(CryptoPP)
    00029 USING_NAMESPACE(std)
    00030
    00031 void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken);
    00032
    00033 void BenchMarkEncryption(const char *name, PK_Encryptor &key, double timeTotal, bool pc=false)
    00034 {
    00035         unsigned int len = 16;
    00036         LC_RNG rng(time(NULL));
    00037         SecByteBlock plaintext(len), ciphertext(key.CiphertextLength(len));
    00038         rng.GenerateBlock(plaintext, len);
    00039
    00040         clock_t start = clock();
    00041         unsigned int i;
    00042         double timeTaken;
    00043         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
    00044                 key.Encrypt(rng, plaintext, len, ciphertext);
    00045
    00046         OutputResultOperations(name, "Encryption", pc, i, timeTaken);
    00047
    00048         if (!pc && key.GetMaterial().SupportsPrecomputation())
    00049         {
    00050                 key.AccessMaterial().Precompute(16);
    00051                 BenchMarkEncryption(name, key, timeTotal, true);
    00052         }
    00053 }
    00054
    00055 void BenchMarkDecryption(const char *name, PK_Decryptor &priv, PK_Encryptor &pub, double timeTotal)
    00056 {
    00057         unsigned int len = 16;
    00058         LC_RNG rng(time(NULL));
    00059         SecByteBlock ciphertext(pub.CiphertextLength(len));
    00060         SecByteBlock plaintext(pub.MaxPlaintextLength(ciphertext.size()));
    00061         rng.GenerateBlock(plaintext, len);
    00062         pub.Encrypt(rng, plaintext, len, ciphertext);
    00063
    00064         clock_t start = clock();
    00065         unsigned int i;
    00066         double timeTaken;
    00067         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
    00068                 priv.Decrypt(rng, ciphertext, ciphertext.size(), plaintext);
    00069
    00070         OutputResultOperations(name, "Decryption", false, i, timeTaken);
    00071 }
    00072
    00073 void BenchMarkSigning(const char *name, PK_Signer &key, double timeTotal, bool pc=false)
    00074 {
    00075         unsigned int len = 16;
    00076         LC_RNG rng(time(NULL));
    00077         SecByteBlock message(len), signature(key.SignatureLength());
    00078         rng.GenerateBlock(message, len);
    00079
    00080         clock_t start = clock();
    00081         unsigned int i;
    00082         double timeTaken;
    00083         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
    00084                 key.SignMessage(rng, message, len, signature);
    00085
    00086         OutputResultOperations(name, "Signature", pc, i, timeTaken);
    00087
    00088         if (!pc && key.GetMaterial().SupportsPrecomputation())
    00089         {
    00090                 key.AccessMaterial().Precompute(16);
    00091                 BenchMarkSigning(name, key, timeTotal, true);
    00092         }
    00093 }
    00094
    00095 void BenchMarkVerification(const char *name, const PK_Signer &priv, PK_Verifier &pub, double timeTotal, bool pc=false)
    00096 {
    00097         unsigned int len = 16;
    00098         LC_RNG rng(time(NULL));
    00099         SecByteBlock message(len), signature(pub.SignatureLength());
    00100         rng.GenerateBlock(message, len);
    00101         priv.SignMessage(rng, message, len, signature);
    00102
    00103         clock_t start = clock();
    00104         unsigned int i;
    00105         double timeTaken;
    00106         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
    00107                 pub.VerifyMessage(message, len, signature, signature.size());
    00108
    00109         OutputResultOperations(name, "Verification", pc, i, timeTaken);
    00110
    00111         if (!pc && pub.GetMaterial().SupportsPrecomputation())
    00112         {
    00113                 pub.AccessMaterial().Precompute(16);
    00114                 BenchMarkVerification(name, priv, pub, timeTotal, true);
    00115         }
    00116 }
    00117
    00118 void BenchMarkKeyGen(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
    00119 {
    00120         LC_RNG rng(time(NULL));
    00121         SecByteBlock priv(d.PrivateKeyLength()), pub(d.PublicKeyLength());
    00122
    00123         clock_t start = clock();
    00124         unsigned int i;
    00125         double timeTaken;
    00126         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
    00127                 d.GenerateKeyPair(rng, priv, pub);
    00128
    00129         OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
    00130
    00131         if (!pc && d.GetMaterial().SupportsPrecomputation())
    00132         {
    00133                 d.AccessMaterial().Precompute(16);
    00134                 BenchMarkKeyGen(name, d, timeTotal, true);
    00135         }
    00136 }
    00137
    00138 void BenchMarkKeyGen(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
    00139 {
    00140         LC_RNG rng(time(NULL));
    00141         SecByteBlock priv(d.EphemeralPrivateKeyLength()), pub(d.EphemeralPublicKeyLength());
    00142
    00143         clock_t start = clock();
    00144         unsigned int i;
    00145         double timeTaken;
    00146         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i++)
    00147                 d.GenerateEphemeralKeyPair(rng, priv, pub);
    00148
    00149         OutputResultOperations(name, "Key-Pair Generation", pc, i, timeTaken);
    00150
    00151         if (!pc && d.GetMaterial().SupportsPrecomputation())
    00152         {
    00153                 d.AccessMaterial().Precompute(16);
    00154                 BenchMarkKeyGen(name, d, timeTotal, true);
    00155         }
    00156 }
    00157
    00158 void BenchMarkAgreement(const char *name, SimpleKeyAgreementDomain &d, double timeTotal, bool pc=false)
    00159 {
    00160         LC_RNG rng(time(NULL));
    00161         SecByteBlock priv1(d.PrivateKeyLength()), priv2(d.PrivateKeyLength());
    00162         SecByteBlock pub1(d.PublicKeyLength()), pub2(d.PublicKeyLength());
    00163         d.GenerateKeyPair(rng, priv1, pub1);
    00164         d.GenerateKeyPair(rng, priv2, pub2);
    00165         SecByteBlock val(d.AgreedValueLength());
    00166
    00167         clock_t start = clock();
    00168         unsigned int i;
    00169         double timeTaken;
    00170         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i+=2)
    00171         {
    00172                 d.Agree(val, priv1, pub2);
    00173                 d.Agree(val, priv2, pub1);
    00174         }
    00175
    00176         OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
    00177 }
    00178
    00179 void BenchMarkAgreement(const char *name, AuthenticatedKeyAgreementDomain &d, double timeTotal, bool pc=false)
    00180 {
    00181         LC_RNG rng(time(NULL));
    00182         SecByteBlock spriv1(d.StaticPrivateKeyLength()), spriv2(d.StaticPrivateKeyLength());
    00183         SecByteBlock epriv1(d.EphemeralPrivateKeyLength()), epriv2(d.EphemeralPrivateKeyLength());
    00184         SecByteBlock spub1(d.StaticPublicKeyLength()), spub2(d.StaticPublicKeyLength());
    00185         SecByteBlock epub1(d.EphemeralPublicKeyLength()), epub2(d.EphemeralPublicKeyLength());
    00186         d.GenerateStaticKeyPair(rng, spriv1, spub1);
    00187         d.GenerateStaticKeyPair(rng, spriv2, spub2);
    00188         d.GenerateEphemeralKeyPair(rng, epriv1, epub1);
    00189         d.GenerateEphemeralKeyPair(rng, epriv2, epub2);
    00190         SecByteBlock val(d.AgreedValueLength());
    00191
    00192         clock_t start = clock();
    00193         unsigned int i;
    00194         double timeTaken;
    00195         for (timeTaken=(double)0, i=0; timeTaken < timeTotal; timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND, i+=2)
    00196         {
    00197                 d.Agree(val, spriv1, epriv1, spub2, epub2);
    00198                 d.Agree(val, spriv2, epriv2, spub1, epub1);
    00199         }
    00200
    00201         OutputResultOperations(name, "Key Agreement", pc, i, timeTaken);
    00202 }
    00203
    00204 //VC60 workaround: compiler bug triggered without the extra dummy parameters
    00205 template <class SCHEME>
    00206 void BenchMarkCrypto(const char *filename, const char *name, double timeTotal, SCHEME *x=NULL)
    00207 {
    00208         FileSource f(filename, true, new HexDecoder());
    00209         typename SCHEME::Decryptor priv(f);
    00210         typename SCHEME::Encryptor pub(priv);
    00211         BenchMarkEncryption(name, pub, timeTotal);
    00212         BenchMarkDecryption(name, priv, pub, timeTotal);
    00213 }
    00214
    00215 //VC60 workaround: compiler bug triggered without the extra dummy parameters
    00216 template <class SCHEME>
    00217 void BenchMarkSignature(const char *filename, const char *name, double timeTotal, SCHEME *x=NULL)
    00218 {
    00219         FileSource f(filename, true, new HexDecoder());
    00220         typename SCHEME::Signer priv(f);
    00221         typename SCHEME::Verifier pub(priv);
    00222         BenchMarkSigning(name, priv, timeTotal);
    00223         BenchMarkVerification(name, priv, pub, timeTotal);
    00224 }
    00225
    00226 //VC60 workaround: compiler bug triggered without the extra dummy parameters
    00227 template <class D>
    00228 void BenchMarkKeyAgreement(const char *filename, const char *name, double timeTotal, D *x=NULL)
    00229 {
    00230         FileSource f(filename, true, new HexDecoder());
    00231         D d(f);
    00232         BenchMarkKeyGen(name, d, timeTotal);
    00233         BenchMarkAgreement(name, d, timeTotal);
    00234 }
    00235
    00236 void BenchmarkAll2(double t)
    00237 {
    00238         cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right><COL align=right>" << endl;
    00239         cout << "<THEAD><TR><TH>Operation<TH>Iterations<TH>Total Time<TH>Milliseconds/Operation" << endl;
    00240
    00241         cout << "<TBODY style=\"background: yellow\">" << endl;
    00242         BenchMarkCrypto<RSAES<OAEP<SHA> > >("rsa1024.dat", "RSA 1024", t);
    00243         BenchMarkCrypto<RabinES<OAEP<SHA> > >("rabi1024.dat", "Rabin 1024", t);
    00244         BenchMarkCrypto<LUCES<OAEP<SHA> > >("luc1024.dat", "LUC 1024", t);
    00245         BenchMarkCrypto<DLIES<> >("dlie1024.dat", "DLIES 1024", t);
    00246         BenchMarkCrypto<LUC_IES<> >("lucc512.dat", "LUCELG 512", t);
    00247
    00248         cout << "<TBODY style=\"background: white\">" << endl;
    00249         BenchMarkCrypto<RSAES<OAEP<SHA> > >("rsa2048.dat", "RSA 2048", t);
    00250         BenchMarkCrypto<RabinES<OAEP<SHA> > >("rabi2048.dat", "Rabin 2048", t);
    00251         BenchMarkCrypto<LUCES<OAEP<SHA> > >("luc2048.dat", "LUC 2048", t);
    00252         BenchMarkCrypto<DLIES<> >("dlie2048.dat", "DLIES 2048", t);
    00253         BenchMarkCrypto<LUC_IES<> >("lucc1024.dat", "LUCELG 1024", t);
    00254
    00255         cout << "<TBODY style=\"background: yellow\">" << endl;
    00256         BenchMarkSignature<RSASS<PSSR, SHA> >("rsa1024.dat", "RSA 1024", t);
    00257         BenchMarkSignature<RabinSS<PSSR, SHA> >("rabi1024.dat", "Rabin 1024", t);
    00258         BenchMarkSignature<RWSS<PSSR, SHA> >("rw1024.dat", "RW 1024", t);
    00259         BenchMarkSignature<LUCSS<PSSR, SHA> >("luc1024.dat", "LUC 1024", t);
    00260         BenchMarkSignature<NR<SHA> >("nr1024.dat", "NR 1024", t);
    00261         BenchMarkSignature<DSA>("dsa1024.dat", "DSA 1024", t);
    00262         BenchMarkSignature<LUC_HMP<SHA> >("lucs512.dat", "LUC-HMP 512", t);
    00263         BenchMarkSignature<ESIGN<SHA> >("esig1023.dat", "ESIGN 1023", t);
    00264         BenchMarkSignature<ESIGN<SHA> >("esig1536.dat", "ESIGN 1536", t);
    00265
    00266         cout << "<TBODY style=\"background: white\">" << endl;
    00267         BenchMarkSignature<RSASS<PSSR, SHA> >("rsa2048.dat", "RSA 2048", t);
    00268         BenchMarkSignature<RabinSS<PSSR, SHA> >("rabi2048.dat", "Rabin 2048", t);
    00269         BenchMarkSignature<RWSS<PSSR, SHA> >("rw2048.dat", "RW 2048", t);
    00270         BenchMarkSignature<LUCSS<PSSR, SHA> >("luc2048.dat", "LUC 2048", t);
    00271         BenchMarkSignature<NR<SHA> >("nr2048.dat", "NR 2048", t);
    00272         BenchMarkSignature<LUC_HMP<SHA> >("lucs1024.dat", "LUC-HMP 1024", t);
    00273         BenchMarkSignature<ESIGN<SHA> >("esig2046.dat", "ESIGN 2046", t);
    00274
    00275         cout << "<TBODY style=\"background: yellow\">" << endl;
    00276         BenchMarkKeyAgreement<XTR_DH>("xtrdh171.dat", "XTR-DH 171", t);
    00277         BenchMarkKeyAgreement<XTR_DH>("xtrdh342.dat", "XTR-DH 342", t);
    00278         BenchMarkKeyAgreement<DH>("dh1024.dat", "DH 1024", t);
    00279         BenchMarkKeyAgreement<DH>("dh2048.dat", "DH 2048", t);
    00280         BenchMarkKeyAgreement<LUC_DH>("lucd512.dat", "LUCDIF 512", t);
    00281         BenchMarkKeyAgreement<LUC_DH>("lucd1024.dat", "LUCDIF 1024", t);
    00282         BenchMarkKeyAgreement<MQV>("mqv1024.dat", "MQV 1024", t);
    00283         BenchMarkKeyAgreement<MQV>("mqv2048.dat", "MQV 2048", t);
    00284
    00285         cout << "<TBODY style=\"background: white\">" << endl;
    00286         {
    00287                 Integer modulus("199999999999999999999999980586675243082581144187569");
    00288                 Integer a("659942,b7261b,249174,c86bd5,e2a65b,45fe07,37d110h");
    00289                 Integer b("3ece7d,09473d,666000,5baef5,d4e00e,30159d,2df49ah");
    00290                 Integer x("25dd61,4c0667,81abc0,fe6c84,fefaa3,858ca6,96d0e8h");
    00291                 Integer y("4e2477,05aab0,b3497f,d62b5e,78a531,446729,6c3fach");
    00292                 Integer r("100000000000000000000000000000000000000000000000151");
    00293                 Integer k(2);
    00294                 Integer d("76572944925670636209790912427415155085360939712345");
    00295
    00296                 ECP ec(modulus, a, b);
    00297                 ECP::Point P(x, y);
    00298                 P = ec.Multiply(k, P);
    00299                 ECP::Point Q(ec.Multiply(d, P));
    00300                 ECIES<ECP>::Decryptor cpriv(ec, P, r, d);
    00301                 ECIES<ECP>::Encryptor cpub(cpriv);
    00302                 ECDSA<ECP, SHA>::Signer spriv(cpriv);
    00303                 ECDSA<ECP, SHA>::Verifier spub(spriv);
    00304                 ECDH<ECP>::Domain ecdhc(ec, P, r, k);
    00305                 ECMQV<ECP>::Domain ecmqvc(ec, P, r, k);
    00306
    00307                 BenchMarkEncryption("ECIES over GF(p) 168", cpub, t);
    00308                 BenchMarkDecryption("ECIES over GF(p) 168", cpriv, cpub, t);
    00309                 BenchMarkSigning("ECNR over GF(p) 168", spriv, t);
    00310                 BenchMarkVerification("ECNR over GF(p) 168", spriv, spub, t);
    00311                 BenchMarkKeyGen("ECDHC over GF(p) 168", ecdhc, t);
    00312                 BenchMarkAgreement("ECDHC over GF(p) 168", ecdhc, t);
    00313                 BenchMarkKeyGen("ECMQVC over GF(p) 168", ecmqvc, t);
    00314                 BenchMarkAgreement("ECMQVC over GF(p) 168", ecmqvc, t);
    00315         }
    00316
    00317         cout << "<TBODY style=\"background: yellow\">" << endl;
    00318         {
    00319                 Integer r("3805993847215893016155463826195386266397436443");
    00320                 Integer k(12);
    00321                 Integer d("2065729449256706362097909124274151550853609397");
    00322
    00323                 GF2NT gf2n(155, 62, 0);
    00324                 byte b[]={0x7, 0x33, 0x8f};
    00325                 EC2N ec(gf2n, PolynomialMod2::Zero(), PolynomialMod2(b,3));
    00326                 EC2N::Point P(0x7B, 0x1C8);
    00327                 P = ec.Multiply(k, P);
    00328                 EC2N::Point Q(ec.Multiply(d, P));
    00329                 ECIES<EC2N>::Decryptor cpriv(ec, P, r, d);
    00330                 ECIES<EC2N>::Encryptor cpub(cpriv);
    00331                 ECDSA<EC2N, SHA>::Signer spriv(cpriv);
    00332                 ECDSA<EC2N, SHA>::Verifier spub(spriv);
    00333                 ECDH<EC2N>::Domain ecdhc(ec, P, r, k);
    00334                 ECMQV<EC2N>::Domain ecmqvc(ec, P, r, k);
    00335
    00336                 BenchMarkEncryption("ECIES over GF(2^n) 155", cpub, t);
    00337                 BenchMarkDecryption("ECIES over GF(2^n) 155", cpriv, cpub, t);
    00338                 BenchMarkSigning("ECNR over GF(2^n) 155", spriv, t);
    00339                 BenchMarkVerification("ECNR over GF(2^n) 155", spriv, spub, t);
    00340                 BenchMarkKeyGen("ECDHC over GF(2^n) 155", ecdhc, t);
    00341                 BenchMarkAgreement("ECDHC over GF(2^n) 155", ecdhc, t);
    00342                 BenchMarkKeyGen("ECMQVC over GF(2^n) 155", ecmqvc, t);
    00343                 BenchMarkAgreement("ECMQVC over GF(2^n) 155", ecmqvc, t);
    00344         }
    00345         cout << "</TABLE>" << endl;
    00346 }