00001
00002
00003 #define _CRT_SECURE_NO_DEPRECATE
00004
00005 #include "bench.h"
00006 #include "validate.h"
00007 #include "aes.h"
00008 #include "blumshub.h"
00009 #include "files.h"
00010 #include "hex.h"
00011 #include "modes.h"
00012 #include "factory.h"
00013 #include "cpu.h"
00014
00015 #include <time.h>
00016 #include <math.h>
00017 #include <iostream>
00018 #include <iomanip>
00019
00020 USING_NAMESPACE(CryptoPP)
00021 USING_NAMESPACE(std)
00022
00023 #ifdef CLOCKS_PER_SEC
00024 const double CLOCK_TICKS_PER_SECOND = (double)CLOCKS_PER_SEC;
00025 #elif defined(CLK_TCK)
00026 const double CLOCK_TICKS_PER_SECOND = (double)CLK_TCK;
00027 #else
00028 const double CLOCK_TICKS_PER_SECOND = 1000000.0;
00029 #endif
00030
00031 double logtotal = 0, g_allocatedTime, g_hertz;
00032 unsigned int logcount = 0;
00033
00034 static const byte *const key=(byte *)"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
00035
00036 void OutputResultBytes(const char *name, double length, double timeTaken)
00037 {
00038 double mbs = length / timeTaken / (1024*1024);
00039 cout << "\n<TR><TH>" << name;
00040
00041 cout << setiosflags(ios::fixed);
00042
00043 cout << "<TD>" << setprecision(0) << setiosflags(ios::fixed) << mbs;
00044 if (g_hertz)
00045 cout << "<TD>" << setprecision(1) << setiosflags(ios::fixed) << timeTaken * g_hertz / length;
00046 cout << resetiosflags(ios::fixed);
00047 logtotal += log(mbs);
00048 logcount++;
00049 }
00050
00051 void OutputResultKeying(double iterations, double timeTaken)
00052 {
00053 cout << "<TD>" << setprecision(3) << setiosflags(ios::fixed) << (1000*1000*timeTaken/iterations);
00054 if (g_hertz)
00055 cout << "<TD>" << setprecision(0) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations;
00056 }
00057
00058 void OutputResultOperations(const char *name, const char *operation, bool pc, unsigned long iterations, double timeTaken)
00059 {
00060 cout << "\n<TR><TH>" << name << " " << operation << (pc ? " with precomputation" : "");
00061
00062
00063
00064 cout << "<TD>" << setprecision(2) << setiosflags(ios::fixed) << (1000*timeTaken/iterations);
00065 if (g_hertz)
00066 cout << "<TD>" << setprecision(2) << setiosflags(ios::fixed) << timeTaken * g_hertz / iterations / 1000000;
00067 cout << resetiosflags(ios::fixed);
00068
00069 logtotal += log(iterations/timeTaken);
00070 logcount++;
00071 }
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 void BenchMark(const char *name, StreamTransformation &cipher, double timeTotal)
00097 {
00098 const int BUF_SIZE=RoundUpToMultipleOf(2048U, cipher.OptimalBlockSize());
00099 AlignedSecByteBlock buf(BUF_SIZE);
00100 GlobalRNG().GenerateBlock(buf, BUF_SIZE);
00101 clock_t start = clock();
00102
00103 unsigned long i=0, blocks=1;
00104 double timeTaken;
00105 do
00106 {
00107 blocks *= 2;
00108 for (; i<blocks; i++)
00109 cipher.ProcessString(buf, BUF_SIZE);
00110 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
00111 }
00112 while (timeTaken < 2.0/3*timeTotal);
00113
00114 OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
00115 }
00116
00117 void BenchMark(const char *name, AuthenticatedSymmetricCipher &cipher, double timeTotal)
00118 {
00119 if (cipher.NeedsPrespecifiedDataLengths())
00120 cipher.SpecifyDataLengths(0, cipher.MaxMessageLength(), 0);
00121
00122 BenchMark(name, static_cast<StreamTransformation &>(cipher), timeTotal);
00123 }
00124
00125 void BenchMark(const char *name, HashTransformation &ht, double timeTotal)
00126 {
00127 const int BUF_SIZE=2048U;
00128 AlignedSecByteBlock buf(BUF_SIZE);
00129 GlobalRNG().GenerateBlock(buf, BUF_SIZE);
00130 clock_t start = clock();
00131
00132 unsigned long i=0, blocks=1;
00133 double timeTaken;
00134 do
00135 {
00136 blocks *= 2;
00137 for (; i<blocks; i++)
00138 ht.Update(buf, BUF_SIZE);
00139 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
00140 }
00141 while (timeTaken < 2.0/3*timeTotal);
00142
00143 OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
00144 }
00145
00146 void BenchMark(const char *name, BufferedTransformation &bt, double timeTotal)
00147 {
00148 const int BUF_SIZE=2048U;
00149 AlignedSecByteBlock buf(BUF_SIZE);
00150 GlobalRNG().GenerateBlock(buf, BUF_SIZE);
00151 clock_t start = clock();
00152
00153 unsigned long i=0, blocks=1;
00154 double timeTaken;
00155 do
00156 {
00157 blocks *= 2;
00158 for (; i<blocks; i++)
00159 bt.Put(buf, BUF_SIZE);
00160 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
00161 }
00162 while (timeTaken < 2.0/3*timeTotal);
00163
00164 OutputResultBytes(name, double(blocks) * BUF_SIZE, timeTaken);
00165 }
00166
00167 void BenchMarkKeying(SimpleKeyingInterface &c, size_t keyLength, const NameValuePairs ¶ms)
00168 {
00169 unsigned long iterations = 0;
00170 clock_t start = clock();
00171 double timeTaken;
00172 do
00173 {
00174 for (unsigned int i=0; i<1024; i++)
00175 c.SetKey(key, keyLength, params);
00176 timeTaken = double(clock() - start) / CLOCK_TICKS_PER_SECOND;
00177 iterations += 1024;
00178 }
00179 while (timeTaken < g_allocatedTime);
00180
00181 OutputResultKeying(iterations, timeTaken);
00182 }
00183
00184
00185
00186 template <class T_FactoryOutput, class T_Interface>
00187 void BenchMarkByName2(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs ¶ms = g_nullNameValuePairs, T_FactoryOutput *x=NULL, T_Interface *y=NULL)
00188 {
00189 std::string name = factoryName;
00190 if (displayName)
00191 name = displayName;
00192 else if (keyLength)
00193 name += " (" + IntToString(keyLength * 8) + "-bit key)";
00194
00195 std::auto_ptr<T_FactoryOutput> obj(ObjectFactoryRegistry<T_FactoryOutput>::Registry().CreateObject(factoryName));
00196 if (!keyLength)
00197 keyLength = obj->DefaultKeyLength();
00198 obj->SetKey(key, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(key, obj->IVSize()), false)));
00199 BenchMark(name.c_str(), *static_cast<T_Interface *>(obj.get()), g_allocatedTime);
00200 BenchMarkKeying(*obj, keyLength, CombinedNameValuePairs(params, MakeParameters(Name::IV(), ConstByteArrayParameter(key, obj->IVSize()), false)));
00201 }
00202
00203
00204 template <class T_FactoryOutput>
00205 void BenchMarkByName(const char *factoryName, size_t keyLength = 0, const char *displayName=NULL, const NameValuePairs ¶ms = g_nullNameValuePairs, T_FactoryOutput *x=NULL)
00206 {
00207 BenchMarkByName2<T_FactoryOutput, T_FactoryOutput>(factoryName, keyLength, displayName, params, x, x);
00208 }
00209
00210 template <class T>
00211 void BenchMarkByNameKeyLess(const char *factoryName, const char *displayName=NULL, const NameValuePairs ¶ms = g_nullNameValuePairs, T *x=NULL)
00212 {
00213 std::string name = factoryName;
00214 if (displayName)
00215 name = displayName;
00216
00217 std::auto_ptr<T> obj(ObjectFactoryRegistry<T>::Registry().CreateObject(factoryName));
00218 BenchMark(name.c_str(), *obj, g_allocatedTime);
00219 }
00220
00221 void BenchmarkAll(double t, double hertz)
00222 {
00223 #if 1
00224 logtotal = 0;
00225 logcount = 0;
00226 g_allocatedTime = t;
00227 g_hertz = hertz;
00228
00229 const char *cpb, *cpk;
00230 if (g_hertz)
00231 {
00232 cpb = "<TH>Cycles Per Byte";
00233 cpk = "<TH>Cycles to<br>Setup Key and IV";
00234 cout << "CPU frequency of the test platform is " << g_hertz << " Hz.\n";
00235 }
00236 else
00237 {
00238 cpb = cpk = "";
00239 cout << "CPU frequency of the test platform was not provided.\n";
00240 }
00241
00242 cout << "<TABLE border=1><COLGROUP><COL align=left><COL align=right><COL align=right><COL align=right><COL align=right>" << endl;
00243 cout << "<THEAD><TR><TH>Algorithm<TH>MiB/Second" << cpb << "<TH>Microseconds to<br>Setup Key and IV" << cpk << endl;
00244
00245 cout << "\n<TBODY style=\"background: yellow\">";
00246 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
00247 if (HasCLMUL())
00248 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM");
00249 else
00250 #endif
00251 {
00252 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (2K tables)", MakeParameters(Name::TableSize(), 2048));
00253 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/GCM", 0, "AES/GCM (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
00254 }
00255 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/CCM");
00256 BenchMarkByName2<AuthenticatedSymmetricCipher, AuthenticatedSymmetricCipher>("AES/EAX");
00257
00258 cout << "\n<TBODY style=\"background: white\">";
00259 #if CRYPTOPP_BOOL_AESNI_INTRINSICS_AVAILABLE
00260 if (HasCLMUL())
00261 BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES)");
00262 else
00263 #endif
00264 {
00265 BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (2K tables)", MakeParameters(Name::TableSize(), 2048));
00266 BenchMarkByName2<AuthenticatedSymmetricCipher, MessageAuthenticationCode>("AES/GCM", 0, "GMAC(AES) (64K tables)", MakeParameters(Name::TableSize(), 64*1024));
00267 }
00268 BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-64");
00269 BenchMarkByName<MessageAuthenticationCode>("VMAC(AES)-128");
00270 BenchMarkByName<MessageAuthenticationCode>("HMAC(SHA-1)");
00271 BenchMarkByName<MessageAuthenticationCode>("Two-Track-MAC");
00272 BenchMarkByName<MessageAuthenticationCode>("CMAC(AES)");
00273 BenchMarkByName<MessageAuthenticationCode>("DMAC(AES)");
00274
00275 cout << "\n<TBODY style=\"background: yellow\">";
00276 BenchMarkByNameKeyLess<HashTransformation>("CRC32");
00277 BenchMarkByNameKeyLess<HashTransformation>("Adler32");
00278 BenchMarkByNameKeyLess<HashTransformation>("MD5");
00279 BenchMarkByNameKeyLess<HashTransformation>("SHA-1");
00280 BenchMarkByNameKeyLess<HashTransformation>("SHA-256");
00281 BenchMarkByNameKeyLess<HashTransformation>("SHA-512");
00282 BenchMarkByNameKeyLess<HashTransformation>("Tiger");
00283 BenchMarkByNameKeyLess<HashTransformation>("Whirlpool");
00284 BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-160");
00285 BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-320");
00286 BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-128");
00287 BenchMarkByNameKeyLess<HashTransformation>("RIPEMD-256");
00288
00289 cout << "\n<TBODY style=\"background: white\">";
00290 BenchMarkByName<SymmetricCipher>("Panama-LE");
00291 BenchMarkByName<SymmetricCipher>("Panama-BE");
00292 BenchMarkByName<SymmetricCipher>("Salsa20");
00293 BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/12", MakeParameters(Name::Rounds(), 12));
00294 BenchMarkByName<SymmetricCipher>("Salsa20", 0, "Salsa20/8", MakeParameters(Name::Rounds(), 8));
00295 BenchMarkByName<SymmetricCipher>("Sosemanuk");
00296 BenchMarkByName<SymmetricCipher>("MARC4");
00297 BenchMarkByName<SymmetricCipher>("SEAL-3.0-LE");
00298 BenchMarkByName<SymmetricCipher>("WAKE-OFB-LE");
00299
00300 cout << "\n<TBODY style=\"background: yellow\">";
00301 BenchMarkByName<SymmetricCipher>("AES/CTR", 16);
00302 BenchMarkByName<SymmetricCipher>("AES/CTR", 24);
00303 BenchMarkByName<SymmetricCipher>("AES/CTR", 32);
00304 BenchMarkByName<SymmetricCipher>("AES/CBC", 16);
00305 BenchMarkByName<SymmetricCipher>("AES/CBC", 24);
00306 BenchMarkByName<SymmetricCipher>("AES/CBC", 32);
00307 BenchMarkByName<SymmetricCipher>("AES/OFB", 16);
00308 BenchMarkByName<SymmetricCipher>("AES/CFB", 16);
00309 BenchMarkByName<SymmetricCipher>("AES/ECB", 16);
00310 BenchMarkByName<SymmetricCipher>("Camellia/CTR", 16);
00311 BenchMarkByName<SymmetricCipher>("Camellia/CTR", 32);
00312 BenchMarkByName<SymmetricCipher>("Twofish/CTR");
00313 BenchMarkByName<SymmetricCipher>("Serpent/CTR");
00314 BenchMarkByName<SymmetricCipher>("CAST-256/CTR");
00315 BenchMarkByName<SymmetricCipher>("RC6/CTR");
00316 BenchMarkByName<SymmetricCipher>("MARS/CTR");
00317 BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 16);
00318 BenchMarkByName<SymmetricCipher>("SHACAL-2/CTR", 64);
00319 BenchMarkByName<SymmetricCipher>("DES/CTR");
00320 BenchMarkByName<SymmetricCipher>("DES-XEX3/CTR");
00321 BenchMarkByName<SymmetricCipher>("DES-EDE3/CTR");
00322 BenchMarkByName<SymmetricCipher>("IDEA/CTR");
00323 BenchMarkByName<SymmetricCipher>("RC5/CTR", 0, "RC5 (r=16)");
00324 BenchMarkByName<SymmetricCipher>("Blowfish/CTR");
00325 BenchMarkByName<SymmetricCipher>("TEA/CTR");
00326 BenchMarkByName<SymmetricCipher>("XTEA/CTR");
00327 BenchMarkByName<SymmetricCipher>("CAST-128/CTR");
00328 BenchMarkByName<SymmetricCipher>("SKIPJACK/CTR");
00329 BenchMarkByName<SymmetricCipher>("SEED/CTR", 0, "SEED/CTR (1/2 K table)");
00330 cout << "</TABLE>" << endl;
00331
00332 BenchmarkAll2(t, hertz);
00333
00334 cout << "Throughput Geometric Average: " << setiosflags(ios::fixed) << exp(logtotal/logcount) << endl;
00335
00336 time_t endTime = time(NULL);
00337 cout << "\nTest ended at " << asctime(localtime(&endTime));
00338 #endif
00339 }