00001 // benchmark.cpp 00002 // TaoCrypt benchmark 00003 00004 #include <string.h> 00005 #include <stdio.h> 00006 00007 #include "runtime.hpp" 00008 #include "des.hpp" 00009 #include "aes.hpp" 00010 #include "twofish.hpp" 00011 #include "blowfish.hpp" 00012 #include "arc4.hpp" 00013 #include "md5.hpp" 00014 #include "sha.hpp" 00015 #include "ripemd.hpp" 00016 #include "rsa.hpp" 00017 #include "dh.hpp" 00018 #include "dsa.hpp" 00019 00020 00021 using namespace TaoCrypt; 00022 00023 void bench_aes(bool show); 00024 void bench_des(); 00025 void bench_blowfish(); 00026 void bench_twofish(); 00027 void bench_arc4(); 00028 00029 void bench_md5(); 00030 void bench_sha(); 00031 void bench_ripemd(); 00032 00033 void bench_rsa(); 00034 void bench_dh(); 00035 void bench_dsa(); 00036 00037 double current_time(); 00038 00039 00040 00041 00042 int main(int argc, char** argv) 00043 { 00044 bench_aes(false); 00045 bench_aes(true); 00046 bench_blowfish(); 00047 bench_twofish(); 00048 bench_arc4(); 00049 bench_des(); 00050 00051 printf("\n"); 00052 00053 bench_md5(); 00054 bench_sha(); 00055 bench_ripemd(); 00056 00057 printf("\n"); 00058 00059 bench_rsa(); 00060 bench_dh(); 00061 bench_dsa(); 00062 00063 return 0; 00064 } 00065 00066 const int megs = 5; // how much to test 00067 00068 const byte key[] = 00069 { 00070 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef, 00071 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10, 00072 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67 00073 }; 00074 00075 const byte iv[] = 00076 { 00077 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef, 00078 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01, 00079 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81 00080 00081 }; 00082 00083 00084 byte plain [1024*1024]; 00085 byte cipher[1024*1024]; 00086 00087 00088 void bench_des() 00089 { 00090 DES_EDE3_CBC_Encryption enc; 00091 enc.SetKey(key, 16, iv); 00092 00093 double start = current_time(); 00094 00095 for(int i = 0; i < megs; i++) 00096 enc.Process(plain, cipher, sizeof(plain)); 00097 00098 double total = current_time() - start; 00099 00100 double persec = 1 / total * megs; 00101 00102 printf("3DES %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, 00103 persec); 00104 } 00105 00106 00107 void bench_aes(bool show) 00108 { 00109 AES_CBC_Encryption enc; 00110 enc.SetKey(key, 16, iv); 00111 00112 double start = current_time(); 00113 00114 for(int i = 0; i < megs; i++) 00115 enc.Process(plain, cipher, sizeof(plain)); 00116 00117 double total = current_time() - start; 00118 00119 double persec = 1 / total * megs; 00120 00121 if (show) 00122 printf("AES %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, 00123 persec); 00124 } 00125 00126 00127 void bench_twofish() 00128 { 00129 Twofish_CBC_Encryption enc; 00130 enc.SetKey(key, 16, iv); 00131 00132 double start = current_time(); 00133 00134 for(int i = 0; i < megs; i++) 00135 enc.Process(plain, cipher, sizeof(plain)); 00136 00137 double total = current_time() - start; 00138 00139 double persec = 1 / total * megs; 00140 00141 printf("Twofish %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, 00142 persec); 00143 00144 } 00145 00146 00147 void bench_blowfish() 00148 { 00149 Blowfish_CBC_Encryption enc; 00150 enc.SetKey(key, 16, iv); 00151 00152 double start = current_time(); 00153 00154 for(int i = 0; i < megs; i++) 00155 enc.Process(plain, cipher, sizeof(plain)); 00156 00157 double total = current_time() - start; 00158 00159 double persec = 1 / total * megs; 00160 00161 printf("Blowfish %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, 00162 persec); 00163 } 00164 00165 00166 void bench_arc4() 00167 { 00168 ARC4 enc; 00169 enc.SetKey(key, 16); 00170 00171 double start = current_time(); 00172 00173 for(int i = 0; i < megs; i++) 00174 enc.Process(cipher, plain, sizeof(plain)); 00175 00176 double total = current_time() - start; 00177 00178 double persec = 1 / total * megs; 00179 00180 printf("ARC4 %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, 00181 persec); 00182 } 00183 00184 00185 void bench_md5() 00186 { 00187 MD5 hash; 00188 byte digest[MD5::DIGEST_SIZE]; 00189 00190 double start = current_time(); 00191 00192 00193 for(int i = 0; i < megs; i++) 00194 hash.Update(plain, sizeof(plain)); 00195 00196 hash.Final(digest); 00197 00198 double total = current_time() - start; 00199 00200 double persec = 1 / total * megs; 00201 00202 printf("MD5 %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, 00203 persec); 00204 } 00205 00206 00207 void bench_sha() 00208 { 00209 SHA hash; 00210 byte digest[SHA::DIGEST_SIZE]; 00211 00212 double start = current_time(); 00213 00214 00215 for(int i = 0; i < megs; i++) 00216 hash.Update(plain, sizeof(plain)); 00217 00218 hash.Final(digest); 00219 00220 /* 00221 for(int i = 0; i < megs; i++) 00222 hash.AsmTransform(plain, 16384); 00223 */ 00224 00225 00226 double total = current_time() - start; 00227 00228 double persec = 1 / total * megs; 00229 00230 printf("SHA %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, 00231 persec); 00232 } 00233 00234 00235 void bench_ripemd() 00236 { 00237 RIPEMD160 hash; 00238 byte digest[RIPEMD160::DIGEST_SIZE]; 00239 00240 double start = current_time(); 00241 00242 00243 for(int i = 0; i < megs; i++) 00244 hash.Update(plain, sizeof(plain)); 00245 00246 hash.Final(digest); 00247 00248 double total = current_time() - start; 00249 00250 double persec = 1 / total * megs; 00251 00252 printf("RIPEMD %d megs took %5.3f seconds, %5.2f MB/s\n", megs, total, 00253 persec); 00254 } 00255 00256 RandomNumberGenerator rng; 00257 00258 void bench_rsa() 00259 { 00260 const int times = 100; 00261 00262 Source source; 00263 FileSource("./rsa1024.der", source); 00264 00265 if (source.size() == 0) { 00266 printf("can't find ./rsa1024.der\n"); 00267 return; 00268 } 00269 RSA_PrivateKey priv(source); 00270 RSAES_Encryptor enc(priv); 00271 00272 byte message[] = "Everyone gets Friday off."; 00273 byte cipher[128]; // for 1024 bit 00274 byte plain[128]; // for 1024 bit 00275 const int len = strlen((char*)message); 00276 00277 int i; 00278 double start = current_time(); 00279 00280 for (i = 0; i < times; i++) 00281 enc.Encrypt(message, len, cipher, rng); 00282 00283 double total = current_time() - start; 00284 double each = total / times; // per second 00285 double milliEach = each * 1000; // milliseconds 00286 00287 printf("RSA 1024 encryption took %6.2f milliseconds, avg over %d" 00288 " iterations\n", milliEach, times); 00289 00290 RSAES_Decryptor dec(priv); 00291 00292 start = current_time(); 00293 00294 for (i = 0; i < times; i++) 00295 dec.Decrypt(cipher, 128, plain, rng); 00296 00297 total = current_time() - start; 00298 each = total / times; // per second 00299 milliEach = each * 1000; // milliseconds 00300 00301 printf("RSA 1024 decryption took %6.2f milliseconds, avg over %d" 00302 " iterations\n", milliEach, times); 00303 } 00304 00305 00306 void bench_dh() 00307 { 00308 const int times = 100; 00309 00310 Source source; 00311 FileSource("./dh1024.der", source); 00312 00313 if (source.size() == 0) { 00314 printf("can't find ./dh1024.der\n"); 00315 return; 00316 } 00317 DH dh(source); 00318 00319 byte pub[128]; // for 1024 bit 00320 byte priv[128]; // for 1024 bit 00321 00322 int i; 00323 double start = current_time(); 00324 00325 for (i = 0; i < times; i++) 00326 dh.GenerateKeyPair(rng, priv, pub); 00327 00328 double total = current_time() - start; 00329 double each = total / times; // per second 00330 double milliEach = each * 1000; // milliseconds 00331 00332 printf("DH 1024 key generation %6.2f milliseconds, avg over %d" 00333 " iterations\n", milliEach, times); 00334 00335 DH dh2(dh); 00336 byte pub2[128]; // for 1024 bit 00337 byte priv2[128]; // for 1024 bit 00338 dh2.GenerateKeyPair(rng, priv2, pub2); 00339 unsigned char key[256]; 00340 00341 start = current_time(); 00342 00343 for (i = 0; i < times; i++) 00344 dh.Agree(key, priv, pub2); 00345 00346 total = current_time() - start; 00347 each = total / times; // per second 00348 milliEach = each * 1000; // in milliseconds 00349 00350 printf("DH 1024 key agreement %6.2f milliseconds, avg over %d" 00351 " iterations\n", milliEach, times); 00352 } 00353 00354 void bench_dsa() 00355 { 00356 const int times = 100; 00357 00358 Source source; 00359 FileSource("./dsa1024.der", source); 00360 00361 if (source.size() == 0) { 00362 printf("can't find ./dsa1024.der\n"); 00363 return; 00364 } 00365 00366 DSA_PrivateKey key(source); 00367 DSA_Signer signer(key); 00368 00369 SHA sha; 00370 byte digest[SHA::DIGEST_SIZE]; 00371 byte signature[40]; 00372 const char msg[] = "this is the message"; 00373 sha.Update((byte*)msg, sizeof(msg)); 00374 sha.Final(digest); 00375 00376 int i; 00377 double start = current_time(); 00378 00379 for (i = 0; i < times; i++) 00380 signer.Sign(digest, signature, rng); 00381 00382 double total = current_time() - start; 00383 double each = total / times; // per second 00384 double milliEach = each * 1000; // milliseconds 00385 00386 printf("DSA 1024 sign took %6.2f milliseconds, avg over %d" 00387 " iterations\n", milliEach, times); 00388 00389 DSA_Verifier verifier(key); 00390 00391 start = current_time(); 00392 00393 for (i = 0; i < times; i++) 00394 verifier.Verify(digest, signature); 00395 00396 total = current_time() - start; 00397 each = total / times; // per second 00398 milliEach = each * 1000; // in milliseconds 00399 00400 printf("DSA 1024 verify took %6.2f milliseconds, avg over %d" 00401 " iterations\n", milliEach, times); 00402 } 00403 00404 00405 00406 #ifdef _WIN32 00407 00408 #define WIN32_LEAN_AND_MEAN 00409 #include <windows.h> 00410 00411 double current_time() 00412 { 00413 static bool init(false); 00414 static LARGE_INTEGER freq; 00415 00416 if (!init) { 00417 QueryPerformanceFrequency(&freq); 00418 init = true; 00419 } 00420 00421 LARGE_INTEGER count; 00422 QueryPerformanceCounter(&count); 00423 00424 return static_cast<double>(count.QuadPart) / freq.QuadPart; 00425 } 00426 00427 #else 00428 00429 #include <sys/time.h> 00430 00431 double current_time() 00432 { 00433 struct timeval tv; 00434 gettimeofday(&tv, 0); 00435 00436 return static_cast<double>(tv.tv_sec) 00437 + static_cast<double>(tv.tv_usec) / 1000000; 00438 } 00439 00440 #endif // _WIN32
1.4.7

