x86_prof.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #include <mycrypt.h>
  2. #define KTIMES 25
  3. #define TIMES 100000
  4. struct list {
  5. int id;
  6. unsigned long spd1, spd2, avg;
  7. } results[100];
  8. int no_results;
  9. int sorter(const void *a, const void *b)
  10. {
  11. const struct list *A, *B;
  12. A = a;
  13. B = b;
  14. if (A->avg < B->avg) return -1;
  15. if (A->avg > B->avg) return 1;
  16. return 0;
  17. }
  18. void tally_results(int type)
  19. {
  20. int x;
  21. // qsort the results
  22. qsort(results, no_results, sizeof(struct list), &sorter);
  23. printf("\n");
  24. if (type == 0) {
  25. for (x = 0; x < no_results; x++) {
  26. printf("%-20s: Schedule at %6lu\n", cipher_descriptor[results[x].id].name, (unsigned long)results[x].spd1);
  27. }
  28. } else if (type == 1) {
  29. for (x = 0; x < no_results; x++) {
  30. printf
  31. ("%-20s: Encrypt at %5lu, Decrypt at %5lu\n", cipher_descriptor[results[x].id].name, results[x].spd1, results[x].spd2);
  32. }
  33. } else {
  34. for (x = 0; x < no_results; x++) {
  35. printf
  36. ("%-20s: Process at %5lu\n", hash_descriptor[results[x].id].name, results[x].spd1 / 1000);
  37. }
  38. }
  39. }
  40. /* RDTSC from Scott Duplichan */
  41. static ulong64 rdtsc (void)
  42. {
  43. #if defined __GNUC__
  44. #ifdef __i386__
  45. ulong64 a;
  46. __asm__ __volatile__ ("rdtsc ":"=A" (a));
  47. return a;
  48. #else /* gcc-IA64 version */
  49. unsigned long result;
  50. __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
  51. while (__builtin_expect ((int) result == -1, 0))
  52. __asm__ __volatile__("mov %0=ar.itc" : "=r"(result) :: "memory");
  53. return result;
  54. #endif
  55. // Microsoft and Intel Windows compilers
  56. #elif defined _M_IX86
  57. __asm rdtsc
  58. #elif defined _M_AMD64
  59. return __rdtsc ();
  60. #elif defined _M_IA64
  61. #if defined __INTEL_COMPILER
  62. #include <ia64intrin.h>
  63. #endif
  64. return __getReg (3116);
  65. #else
  66. #error need rdtsc function for this build
  67. #endif
  68. }
  69. ulong64 timer, skew = 0;
  70. prng_state prng;
  71. void t_start(void)
  72. {
  73. timer = rdtsc();
  74. }
  75. ulong64 t_read(void)
  76. {
  77. return rdtsc() - timer;
  78. }
  79. void init_timer(void)
  80. {
  81. ulong64 c1, c2, t1, t2, t3;
  82. unsigned long y1;
  83. c1 = c2 = (ulong64)-1;
  84. for (y1 = 0; y1 < TIMES*100; y1++) {
  85. t_start();
  86. t1 = t_read();
  87. t3 = t_read();
  88. t2 = t_read() - t1;
  89. c1 = (c1 > t1) ? t1 : c1;
  90. c2 = (c2 > t2) ? t2 : c2;
  91. }
  92. skew = c2 - c1;
  93. printf("Clock Skew: %lu\n", (unsigned long)skew);
  94. }
  95. void reg_algs(void)
  96. {
  97. #ifdef RIJNDAEL
  98. register_cipher (&aes_desc);
  99. #endif
  100. #ifdef BLOWFISH
  101. register_cipher (&blowfish_desc);
  102. #endif
  103. #ifdef XTEA
  104. register_cipher (&xtea_desc);
  105. #endif
  106. #ifdef RC5
  107. register_cipher (&rc5_desc);
  108. #endif
  109. #ifdef RC6
  110. register_cipher (&rc6_desc);
  111. #endif
  112. #ifdef SAFERP
  113. register_cipher (&saferp_desc);
  114. #endif
  115. #ifdef TWOFISH
  116. register_cipher (&twofish_desc);
  117. #endif
  118. #ifdef SAFER
  119. register_cipher (&safer_k64_desc);
  120. register_cipher (&safer_sk64_desc);
  121. register_cipher (&safer_k128_desc);
  122. register_cipher (&safer_sk128_desc);
  123. #endif
  124. #ifdef RC2
  125. register_cipher (&rc2_desc);
  126. #endif
  127. #ifdef DES
  128. register_cipher (&des_desc);
  129. register_cipher (&des3_desc);
  130. #endif
  131. #ifdef CAST5
  132. register_cipher (&cast5_desc);
  133. #endif
  134. #ifdef NOEKEON
  135. register_cipher (&noekeon_desc);
  136. #endif
  137. #ifdef SKIPJACK
  138. register_cipher (&skipjack_desc);
  139. #endif
  140. #ifdef TIGER
  141. register_hash (&tiger_desc);
  142. #endif
  143. #ifdef MD2
  144. register_hash (&md2_desc);
  145. #endif
  146. #ifdef MD4
  147. register_hash (&md4_desc);
  148. #endif
  149. #ifdef MD5
  150. register_hash (&md5_desc);
  151. #endif
  152. #ifdef SHA1
  153. register_hash (&sha1_desc);
  154. #endif
  155. #ifdef SHA224
  156. register_hash (&sha224_desc);
  157. #endif
  158. #ifdef SHA256
  159. register_hash (&sha256_desc);
  160. #endif
  161. #ifdef SHA384
  162. register_hash (&sha384_desc);
  163. #endif
  164. #ifdef SHA512
  165. register_hash (&sha512_desc);
  166. #endif
  167. #ifdef RIPEMD128
  168. register_hash (&rmd128_desc);
  169. #endif
  170. #ifdef RIPEMD160
  171. register_hash (&rmd160_desc);
  172. #endif
  173. #ifdef WHIRLPOOL
  174. register_hash (&whirlpool_desc);
  175. #endif
  176. register_prng(&yarrow_desc);
  177. rng_make_prng(128, find_prng("yarrow"), &prng, NULL);
  178. }
  179. int time_keysched(void)
  180. {
  181. unsigned long x, i, y1;
  182. ulong64 t1, c1;
  183. symmetric_key skey;
  184. int kl;
  185. int (*func) (const unsigned char *, int , int , symmetric_key *);
  186. unsigned char key[MAXBLOCKSIZE];
  187. printf ("\n\nKey Schedule Time Trials for the Symmetric Ciphers:\n(Times are cycles per key)\n");
  188. no_results = 0;
  189. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  190. #define DO1(k) func(k, kl, 0, &skey);
  191. func = cipher_descriptor[x].setup;
  192. kl = cipher_descriptor[x].min_key_length;
  193. c1 = (ulong64)-1;
  194. for (y1 = 0; y1 < KTIMES; y1++) {
  195. yarrow_read(key, kl, &prng);
  196. t_start();
  197. DO1(key);
  198. t1 = t_read();
  199. c1 = (t1 > c1) ? c1 : t1;
  200. }
  201. t1 = c1 - skew;
  202. results[no_results].spd1 = results[no_results].avg = t1;
  203. results[no_results++].id = x;
  204. printf("."); fflush(stdout);
  205. #undef DO1
  206. }
  207. tally_results(0);
  208. return 0;
  209. }
  210. int time_cipher(void)
  211. {
  212. unsigned long x, y1;
  213. ulong64 t1, t2, c1, c2, a1, a2;
  214. symmetric_key skey;
  215. void (*func) (const unsigned char *, unsigned char *, symmetric_key *);
  216. unsigned char key[MAXBLOCKSIZE], pt[MAXBLOCKSIZE];
  217. printf ("\n\nECB Time Trials for the Symmetric Ciphers:\n");
  218. no_results = 0;
  219. for (x = 0; cipher_descriptor[x].name != NULL; x++) {
  220. cipher_descriptor[x].setup (key, cipher_descriptor[x].min_key_length, 0,
  221. &skey);
  222. #define DO1 func(pt,pt,&skey);
  223. #define DO2 DO1 DO1
  224. func = cipher_descriptor[x].ecb_encrypt;
  225. c1 = c2 = (ulong64)-1;
  226. for (y1 = 0; y1 < TIMES; y1++) {
  227. t_start();
  228. DO1;
  229. t1 = t_read();
  230. DO2;
  231. t2 = t_read();
  232. t2 -= t1;
  233. c1 = (t1 > c1 ? c1 : t1);
  234. c2 = (t2 > c2 ? c2 : t2);
  235. }
  236. a1 = c2 - c1 - skew;
  237. func = cipher_descriptor[x].ecb_decrypt;
  238. c1 = c2 = (ulong64)-1;
  239. for (y1 = 0; y1 < TIMES; y1++) {
  240. t_start();
  241. DO1;
  242. t1 = t_read();
  243. DO2;
  244. t2 = t_read();
  245. t2 -= t1;
  246. c1 = (t1 > c1 ? c1 : t1);
  247. c2 = (t2 > c2 ? c2 : t2);
  248. }
  249. a2 = c2 - c1 - skew;
  250. results[no_results].id = x;
  251. results[no_results].spd1 = a1/cipher_descriptor[x].block_length;
  252. results[no_results].spd2 = a2/cipher_descriptor[x].block_length;;
  253. results[no_results].avg = (results[no_results].spd1 + results[no_results].spd2+1)/2;
  254. ++no_results;
  255. printf("."); fflush(stdout);
  256. #undef DO2
  257. #undef DO1
  258. }
  259. tally_results(1);
  260. return 0;
  261. }
  262. int time_hash(void)
  263. {
  264. unsigned long x, y1, len;
  265. ulong64 t1, t2, c1, c2;
  266. hash_state md;
  267. int (*func)(hash_state *, const unsigned char *, unsigned long);
  268. unsigned char pt[MAXBLOCKSIZE];
  269. printf ("\n\nHASH Time Trials for:\n");
  270. no_results = 0;
  271. for (x = 0; hash_descriptor[x].name != NULL; x++) {
  272. hash_descriptor[x].init(&md);
  273. #define DO1 func(&md,pt,len);
  274. #define DO2 DO1 DO1
  275. func = hash_descriptor[x].process;
  276. len = hash_descriptor[x].blocksize;
  277. c1 = c2 = (ulong64)-1;
  278. for (y1 = 0; y1 < TIMES; y1++) {
  279. t_start();
  280. DO1;
  281. t1 = t_read();
  282. DO2;
  283. t2 = t_read() - t1;
  284. c1 = (t1 > c1) ? c1 : t1;
  285. c2 = (t2 > c2) ? c2 : t2;
  286. }
  287. t1 = c2 - c1 - skew;
  288. t1 = ((t1 * CONST64(1000))) / ((ulong64)hash_descriptor[x].blocksize);
  289. results[no_results].id = x;
  290. results[no_results].spd1 = results[no_results].avg = t1;
  291. ++no_results;
  292. printf("."); fflush(stdout);
  293. #undef DO2
  294. #undef DO1
  295. }
  296. tally_results(2);
  297. return 0;
  298. }
  299. int main(void)
  300. {
  301. reg_algs();
  302. printf("Timings for ciphers and hashes. Times are listed as cycles per byte processed.\n\n");
  303. // init_timer();
  304. time_cipher();
  305. time_keysched();
  306. time_hash();
  307. return EXIT_SUCCESS;
  308. }