benchmark.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* benchmark.c */
  2. /* CTaoCrypt benchmark */
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include "des3.h"
  6. #include "arc4.h"
  7. #include "hc128.h"
  8. #include "rabbit.h"
  9. #include "ctc_aes.h"
  10. #include "ctc_md5.h"
  11. #include "ctc_sha.h"
  12. #include "sha256.h"
  13. #include "sha512.h"
  14. #include "ctc_rsa.h"
  15. #include "asn.h"
  16. #include "ctc_ripemd.h"
  17. #include "ctc_dh.h"
  18. #ifdef _MSC_VER
  19. /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
  20. #pragma warning(disable: 4996)
  21. #endif
  22. void bench_des();
  23. void bench_arc4();
  24. void bench_hc128();
  25. void bench_rabbit();
  26. void bench_aes(int);
  27. void bench_md5();
  28. void bench_sha();
  29. void bench_sha256();
  30. void bench_sha512();
  31. void bench_ripemd();
  32. void bench_rsa();
  33. void bench_rsaKeyGen();
  34. void bench_dh();
  35. double current_time();
  36. int main(int argc, char** argv)
  37. {
  38. #ifndef NO_AES
  39. bench_aes(0);
  40. bench_aes(1);
  41. #endif
  42. bench_arc4();
  43. #ifndef NO_HC128
  44. bench_hc128();
  45. #endif
  46. #ifndef NO_RABBIT
  47. bench_rabbit();
  48. #endif
  49. #ifndef NO_DES3
  50. bench_des();
  51. #endif
  52. printf("\n");
  53. bench_md5();
  54. bench_sha();
  55. #ifndef NO_SHA256
  56. bench_sha256();
  57. #endif
  58. #ifdef CYASSL_SHA512
  59. bench_sha512();
  60. #endif
  61. #ifdef CYASSL_RIPEMD
  62. bench_ripemd();
  63. #endif
  64. printf("\n");
  65. bench_rsa();
  66. #ifndef NO_DH
  67. bench_dh();
  68. #endif
  69. #ifdef CYASSL_KEY_GEN
  70. bench_rsaKeyGen();
  71. #endif
  72. return 0;
  73. }
  74. const int megs = 5; /* how many megs to test (en/de)cryption */
  75. const int times = 100; /* public key iterations */
  76. const byte key[] =
  77. {
  78. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  79. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  80. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  81. };
  82. const byte iv[] =
  83. {
  84. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  85. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  86. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  87. };
  88. byte plain [1024*1024];
  89. byte cipher[1024*1024];
  90. #ifndef NO_AES
  91. void bench_aes(int show)
  92. {
  93. Aes enc;
  94. double start, total, persec;
  95. int i;
  96. AesSetKey(&enc, key, 16, iv, AES_ENCRYPTION);
  97. start = current_time();
  98. for(i = 0; i < megs; i++)
  99. AesCbcEncrypt(&enc, plain, cipher, sizeof(plain));
  100. total = current_time() - start;
  101. persec = 1 / total * megs;
  102. if (show)
  103. printf("AES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  104. persec);
  105. }
  106. #endif
  107. #ifndef NO_DES3
  108. void bench_des()
  109. {
  110. Des3 enc;
  111. double start, total, persec;
  112. int i;
  113. Des3_SetKey(&enc, key, iv, DES_ENCRYPTION);
  114. start = current_time();
  115. for(i = 0; i < megs; i++)
  116. Des3_CbcEncrypt(&enc, plain, cipher, sizeof(plain));
  117. total = current_time() - start;
  118. persec = 1 / total * megs;
  119. printf("3DES %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  120. persec);
  121. }
  122. #endif
  123. void bench_arc4()
  124. {
  125. Arc4 enc;
  126. double start, total, persec;
  127. int i;
  128. Arc4SetKey(&enc, key, 16);
  129. start = current_time();
  130. for(i = 0; i < megs; i++)
  131. Arc4Process(&enc, cipher, plain, sizeof(plain));
  132. total = current_time() - start;
  133. persec = 1 / total * megs;
  134. printf("ARC4 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  135. persec);
  136. }
  137. #ifndef NO_HC128
  138. void bench_hc128()
  139. {
  140. HC128 enc;
  141. double start, total, persec;
  142. int i;
  143. Hc128_SetKey(&enc, key, iv);
  144. start = current_time();
  145. for(i = 0; i < megs; i++)
  146. Hc128_Process(&enc, cipher, plain, sizeof(plain));
  147. total = current_time() - start;
  148. persec = 1 / total * megs;
  149. printf("HC128 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  150. persec);
  151. }
  152. #endif /* NO_HC128 */
  153. #ifndef NO_RABBIT
  154. void bench_rabbit()
  155. {
  156. Rabbit enc;
  157. double start, total, persec;
  158. int i;
  159. RabbitSetKey(&enc, key, iv);
  160. start = current_time();
  161. for(i = 0; i < megs; i++)
  162. RabbitProcess(&enc, cipher, plain, sizeof(plain));
  163. total = current_time() - start;
  164. persec = 1 / total * megs;
  165. printf("RABBIT %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  166. persec);
  167. }
  168. #endif /* NO_RABBIT */
  169. void bench_md5()
  170. {
  171. Md5 hash;
  172. byte digest[MD5_DIGEST_SIZE];
  173. double start, total, persec;
  174. int i;
  175. InitMd5(&hash);
  176. start = current_time();
  177. for(i = 0; i < megs; i++)
  178. Md5Update(&hash, plain, sizeof(plain));
  179. Md5Final(&hash, digest);
  180. total = current_time() - start;
  181. persec = 1 / total * megs;
  182. printf("MD5 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  183. persec);
  184. }
  185. void bench_sha()
  186. {
  187. Sha hash;
  188. byte digest[SHA_DIGEST_SIZE];
  189. double start, total, persec;
  190. int i;
  191. InitSha(&hash);
  192. start = current_time();
  193. for(i = 0; i < megs; i++)
  194. ShaUpdate(&hash, plain, sizeof(plain));
  195. ShaFinal(&hash, digest);
  196. total = current_time() - start;
  197. persec = 1 / total * megs;
  198. printf("SHA %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  199. persec);
  200. }
  201. #ifndef NO_SHA256
  202. void bench_sha256()
  203. {
  204. Sha256 hash;
  205. byte digest[SHA256_DIGEST_SIZE];
  206. double start, total, persec;
  207. int i;
  208. InitSha256(&hash);
  209. start = current_time();
  210. for(i = 0; i < megs; i++)
  211. Sha256Update(&hash, plain, sizeof(plain));
  212. Sha256Final(&hash, digest);
  213. total = current_time() - start;
  214. persec = 1 / total * megs;
  215. printf("SHA-256 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  216. persec);
  217. }
  218. #endif
  219. #ifdef CYASSL_SHA512
  220. void bench_sha512()
  221. {
  222. Sha512 hash;
  223. byte digest[SHA512_DIGEST_SIZE];
  224. double start, total, persec;
  225. int i;
  226. InitSha512(&hash);
  227. start = current_time();
  228. for(i = 0; i < megs; i++)
  229. Sha512Update(&hash, plain, sizeof(plain));
  230. Sha512Final(&hash, digest);
  231. total = current_time() - start;
  232. persec = 1 / total * megs;
  233. printf("SHA-512 %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  234. persec);
  235. }
  236. #endif
  237. #ifdef CYASSL_RIPEMD
  238. void bench_ripemd()
  239. {
  240. RipeMd hash;
  241. byte digest[RIPEMD_DIGEST_SIZE];
  242. double start, total, persec;
  243. int i;
  244. InitRipeMd(&hash);
  245. start = current_time();
  246. for(i = 0; i < megs; i++)
  247. RipeMdUpdate(&hash, plain, sizeof(plain));
  248. RipeMdFinal(&hash, digest);
  249. total = current_time() - start;
  250. persec = 1 / total * megs;
  251. printf("RIPEMD %d megs took %5.3f seconds, %6.2f MB/s\n", megs, total,
  252. persec);
  253. }
  254. #endif
  255. RNG rng;
  256. void bench_rsa()
  257. {
  258. int i;
  259. byte tmp[4096];
  260. size_t bytes;
  261. word32 idx = 0;
  262. byte message[] = "Everyone gets Friday off.";
  263. byte cipher[512]; /* for up to 4096 bit */
  264. byte* output;
  265. const int len = (int)strlen((char*)message);
  266. double start, total, each, milliEach;
  267. RsaKey key;
  268. FILE* file = fopen("./rsa1024.der", "rb");
  269. if (!file) {
  270. printf("can't find ./rsa1024.der\n");
  271. return;
  272. }
  273. InitRng(&rng);
  274. bytes = fread(tmp, 1, sizeof(tmp), file);
  275. InitRsaKey(&key, 0);
  276. bytes = RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  277. start = current_time();
  278. for (i = 0; i < times; i++)
  279. bytes = RsaPublicEncrypt(message,len,cipher,sizeof(cipher), &key, &rng);
  280. total = current_time() - start;
  281. each = total / times; /* per second */
  282. milliEach = each * 1000; /* milliseconds */
  283. printf("RSA 1024 encryption took %6.2f milliseconds, avg over %d"
  284. " iterations\n", milliEach, times);
  285. start = current_time();
  286. for (i = 0; i < times; i++)
  287. RsaPrivateDecryptInline(cipher, (word32)bytes, &output, &key);
  288. total = current_time() - start;
  289. each = total / times; /* per second */
  290. milliEach = each * 1000; /* milliseconds */
  291. printf("RSA 1024 decryption took %6.2f milliseconds, avg over %d"
  292. " iterations\n", milliEach, times);
  293. fclose(file);
  294. FreeRsaKey(&key);
  295. }
  296. #ifndef NO_DH
  297. void bench_dh()
  298. {
  299. int i;
  300. byte tmp[1024];
  301. size_t bytes;
  302. word32 idx = 0, pubSz, privSz, pubSz2, privSz2, agreeSz;
  303. byte pub[128]; /* for 1024 bit */
  304. byte priv[128]; /* for 1024 bit */
  305. byte pub2[128]; /* for 1024 bit */
  306. byte priv2[128]; /* for 1024 bit */
  307. byte agree[128]; /* for 1024 bit */
  308. double start, total, each, milliEach;
  309. DhKey key;
  310. FILE* file = fopen("./dh1024.der", "rb");
  311. if (!file) {
  312. printf("can't find ./dh1024.der\n");
  313. return;
  314. }
  315. bytes = fread(tmp, 1, 1024, file);
  316. InitDhKey(&key);
  317. bytes = DhKeyDecode(tmp, &idx, &key, (word32)bytes);
  318. start = current_time();
  319. for (i = 0; i < times; i++)
  320. DhGenerateKeyPair(&key, &rng, priv, &privSz, pub, &pubSz);
  321. total = current_time() - start;
  322. each = total / times; /* per second */
  323. milliEach = each * 1000; /* milliseconds */
  324. printf("DH 1024 key generation %6.2f milliseconds, avg over %d"
  325. " iterations\n", milliEach, times);
  326. DhGenerateKeyPair(&key, &rng, priv2, &privSz2, pub2, &pubSz2);
  327. start = current_time();
  328. for (i = 0; i < times; i++)
  329. DhAgree(&key, agree, &agreeSz, priv, privSz, pub2, pubSz2);
  330. total = current_time() - start;
  331. each = total / times; /* per second */
  332. milliEach = each * 1000; /* milliseconds */
  333. printf("DH 1024 key agreement %6.2f milliseconds, avg over %d"
  334. " iterations\n", milliEach, times);
  335. fclose(file);
  336. FreeDhKey(&key);
  337. }
  338. #endif
  339. #ifdef CYASSL_KEY_GEN
  340. void bench_rsaKeyGen()
  341. {
  342. RsaKey genKey;
  343. double start, total, each, milliEach;
  344. int i;
  345. const int genTimes = 5;
  346. /* 1024 bit */
  347. start = current_time();
  348. for(i = 0; i < genTimes; i++) {
  349. InitRsaKey(&genKey, 0);
  350. MakeRsaKey(&genKey, 1024, 65537, &rng);
  351. FreeRsaKey(&genKey);
  352. }
  353. total = current_time() - start;
  354. each = total / genTimes; /* per second */
  355. milliEach = each * 1000; /* millisconds */
  356. printf("\n");
  357. printf("RSA 1024 key generation %6.2f milliseconds, avg over %d"
  358. " iterations\n", milliEach, genTimes);
  359. /* 2048 bit */
  360. start = current_time();
  361. for(i = 0; i < genTimes; i++) {
  362. InitRsaKey(&genKey, 0);
  363. MakeRsaKey(&genKey, 2048, 65537, &rng);
  364. FreeRsaKey(&genKey);
  365. }
  366. total = current_time() - start;
  367. each = total / genTimes; /* per second */
  368. milliEach = each * 1000; /* millisconds */
  369. printf("RSA 2048 key generation %6.2f milliseconds, avg over %d"
  370. " iterations\n", milliEach, genTimes);
  371. }
  372. #endif /* CYASSL_KEY_GEN */
  373. #ifdef _WIN32
  374. #define WIN32_LEAN_AND_MEAN
  375. #include <windows.h>
  376. double current_time()
  377. {
  378. static int init = 0;
  379. static LARGE_INTEGER freq;
  380. LARGE_INTEGER count;
  381. if (!init) {
  382. QueryPerformanceFrequency(&freq);
  383. init = 1;
  384. }
  385. QueryPerformanceCounter(&count);
  386. return (double)count.QuadPart / freq.QuadPart;
  387. }
  388. #else
  389. #include <sys/time.h>
  390. double current_time()
  391. {
  392. struct timeval tv;
  393. gettimeofday(&tv, 0);
  394. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
  395. }
  396. #endif /* _WIN32 */