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