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