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