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