aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/benchmark.c
diff options
context:
space:
mode:
Diffstat (limited to 'linden/indra/libgcrypt/libgcrypt-1.2.2/tests/benchmark.c')
-rwxr-xr-xlinden/indra/libgcrypt/libgcrypt-1.2.2/tests/benchmark.c440
1 files changed, 440 insertions, 0 deletions
diff --git a/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/benchmark.c b/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/benchmark.c
new file mode 100755
index 0000000..5f3fcff
--- /dev/null
+++ b/linden/indra/libgcrypt/libgcrypt-1.2.2/tests/benchmark.c
@@ -0,0 +1,440 @@
1/* benchmark.c - for libgcrypt
2 * Copyright (C) 2002, 2004 Free Software Foundation, Inc.
3 *
4 * This file is part of Libgcrypt.
5 *
6 * Libgcrypt is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser general Public License as
8 * published by the Free Software Foundation; either version 2.1 of
9 * the License, or (at your option) any later version.
10 *
11 * Libgcrypt is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 */
20
21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24#include <stdio.h>
25#include <stdlib.h>
26#include <time.h>
27#ifndef _WIN32
28#include <sys/times.h>
29#endif
30#include <gcrypt.h>
31
32#define PGM "benchmark"
33#define BUG() do {fprintf ( stderr, "Ooops at %s:%d\n", __FILE__ , __LINE__ );\
34 exit(2);} while(0)
35
36
37/* Helper for the start and stop timer. */
38static clock_t started_at, stopped_at;
39
40
41static void
42start_timer (void)
43{
44#ifdef _WIN32
45 started_at = stopped_at = clock ();
46#else
47 struct tms tmp;
48
49 times (&tmp);
50 started_at = stopped_at = tmp.tms_utime;
51#endif
52}
53
54static void
55stop_timer (void)
56{
57#ifdef _WIN32
58 stopped_at = clock ();
59#else
60 struct tms tmp;
61
62 times (&tmp);
63 stopped_at = tmp.tms_utime;
64#endif
65}
66
67static const char *
68elapsed_time (void)
69{
70 static char buf[50];
71
72 sprintf (buf, "%5.0fms",
73 (((double) (stopped_at - started_at))/CLOCKS_PER_SEC)*10000000);
74 return buf;
75}
76
77
78static void
79random_bench (void)
80{
81 char buf[128];
82 int i;
83
84 printf ("%-10s", "random");
85
86 start_timer ();
87 for (i=0; i < 100; i++)
88 gcry_randomize (buf, sizeof buf, GCRY_STRONG_RANDOM);
89 stop_timer ();
90 printf (" %s", elapsed_time ());
91
92 start_timer ();
93 for (i=0; i < 100; i++)
94 gcry_randomize (buf, 8, GCRY_STRONG_RANDOM);
95 stop_timer ();
96 printf (" %s", elapsed_time ());
97
98 putchar ('\n');
99}
100
101
102
103static void
104md_bench ( const char *algoname )
105{
106 int algo;
107 gcry_md_hd_t hd;
108 int i;
109 char buf[1000];
110 gcry_error_t err = GPG_ERR_NO_ERROR;
111
112 if (!algoname)
113 {
114 for (i=1; i < 400; i++)
115 if ( !gcry_md_test_algo (i) )
116 md_bench (gcry_md_algo_name (i));
117 return;
118 }
119
120 algo = gcry_md_map_name (algoname);
121 if (!algo)
122 {
123 fprintf (stderr, PGM ": invalid hash algorithm `%s'\n", algoname);
124 exit (1);
125 }
126
127 err = gcry_md_open (&hd, algo, 0);
128 if (err)
129 {
130 fprintf (stderr, PGM ": error opening hash algorithm `%s'\n", algoname);
131 exit (1);
132 }
133
134 for (i=0; i < sizeof buf; i++)
135 buf[i] = i;
136
137 printf ("%-12s", gcry_md_algo_name (algo));
138
139 start_timer ();
140 for (i=0; i < 1000; i++)
141 gcry_md_write (hd, buf, sizeof buf);
142 gcry_md_final (hd);
143 stop_timer ();
144 printf (" %s", elapsed_time ());
145
146 gcry_md_reset (hd);
147 start_timer ();
148 for (i=0; i < 10000; i++)
149 gcry_md_write (hd, buf, sizeof buf/10);
150 gcry_md_final (hd);
151 stop_timer ();
152 printf (" %s", elapsed_time ());
153
154 gcry_md_reset (hd);
155 start_timer ();
156 for (i=0; i < 1000000; i++)
157 gcry_md_write (hd, "", 1);
158 gcry_md_final (hd);
159 stop_timer ();
160 printf (" %s", elapsed_time ());
161
162 gcry_md_close (hd);
163 putchar ('\n');
164}
165
166static void
167cipher_bench ( const char *algoname )
168{
169 static int header_printed;
170 int algo;
171 gcry_cipher_hd_t hd;
172 int i;
173 int keylen, blklen;
174 char key[128];
175 char outbuf[1000], buf[1000];
176 size_t buflen;
177 static struct { int mode; const char *name; int blocked; } modes[] = {
178 { GCRY_CIPHER_MODE_ECB, "ECB", 1 },
179 { GCRY_CIPHER_MODE_CBC, "CBC", 1 },
180 { GCRY_CIPHER_MODE_CFB, "CFB", 0 },
181 { GCRY_CIPHER_MODE_CTR, "CTR", 0 },
182 { GCRY_CIPHER_MODE_STREAM, "STREAM", 0 },
183 {0}
184 };
185 int modeidx;
186 gcry_error_t err = GPG_ERR_NO_ERROR;
187
188
189 if (!algoname)
190 {
191 for (i=1; i < 400; i++)
192 if ( !gcry_cipher_test_algo (i) )
193 cipher_bench (gcry_cipher_algo_name (i));
194 return;
195 }
196
197
198 if (!header_printed)
199 {
200 printf ("%-10s", "");
201 for (modeidx=0; modes[modeidx].mode; modeidx++)
202 printf (" %-15s", modes[modeidx].name );
203 putchar ('\n');
204 printf ("%-10s", "");
205 for (modeidx=0; modes[modeidx].mode; modeidx++)
206 printf (" ---------------" );
207 putchar ('\n');
208 header_printed = 1;
209 }
210
211 algo = gcry_cipher_map_name (algoname);
212 if (!algo)
213 {
214 fprintf (stderr, PGM ": invalid cipher algorithm `%s'\n", algoname);
215 exit (1);
216 }
217
218 keylen = gcry_cipher_get_algo_keylen (algo);
219 if (!keylen)
220 {
221 fprintf (stderr, PGM ": failed to get key length for algorithm `%s'\n",
222 algoname);
223 exit (1);
224 }
225 if ( keylen > sizeof key )
226 {
227 fprintf (stderr, PGM ": algo %d, keylength problem (%d)\n",
228 algo, keylen );
229 exit (1);
230 }
231 for (i=0; i < keylen; i++)
232 key[i] = i + (clock () & 0xff);
233
234 blklen = gcry_cipher_get_algo_blklen (algo);
235 if (!blklen)
236 {
237 fprintf (stderr, PGM ": failed to get block length for algorithm `%s'\n",
238 algoname);
239 exit (1);
240 }
241
242 printf ("%-10s", gcry_cipher_algo_name (algo));
243 fflush (stdout);
244
245 for (modeidx=0; modes[modeidx].mode; modeidx++)
246 {
247 if ((blklen > 1 && modes[modeidx].mode == GCRY_CIPHER_MODE_STREAM)
248 | (blklen == 1 && modes[modeidx].mode != GCRY_CIPHER_MODE_STREAM))
249 {
250 printf (" " );
251 continue;
252 }
253
254 for (i=0; i < sizeof buf; i++)
255 buf[i] = i;
256
257 err = gcry_cipher_open (&hd, algo, modes[modeidx].mode, 0);
258 if (err)
259 {
260 fprintf (stderr, PGM ": error opening cipher `%s'\n", algoname);
261 exit (1);
262 }
263
264 err = gcry_cipher_setkey (hd, key, keylen);
265 if (err)
266 {
267 fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
268 gpg_strerror (err));
269 gcry_cipher_close (hd);
270 exit (1);
271 }
272
273 buflen = sizeof buf;
274 if (modes[modeidx].blocked)
275 buflen = (buflen / blklen) * blklen;
276
277 start_timer ();
278 for (i=err=0; !err && i < 1000; i++)
279 err = gcry_cipher_encrypt ( hd, outbuf, buflen, buf, buflen);
280 stop_timer ();
281 printf (" %s", elapsed_time ());
282 fflush (stdout);
283 gcry_cipher_close (hd);
284 if (err)
285 {
286 fprintf (stderr, "gcry_cipher_encrypt failed: %s\n",
287 gpg_strerror (err) );
288 exit (1);
289 }
290
291 err = gcry_cipher_open (&hd, algo, modes[modeidx].mode, 0);
292 if (err)
293 {
294 fprintf (stderr, PGM ": error opening cipher `%s'/n", algoname);
295 exit (1);
296 }
297
298 err = gcry_cipher_setkey (hd, key, keylen);
299 if (err)
300 {
301 fprintf (stderr, "gcry_cipher_setkey failed: %s\n",
302 gpg_strerror (err));
303 gcry_cipher_close (hd);
304 exit (1);
305 }
306
307 start_timer ();
308 for (i=err=0; !err && i < 1000; i++)
309 err = gcry_cipher_decrypt ( hd, outbuf, buflen, buf, buflen);
310 stop_timer ();
311 printf (" %s", elapsed_time ());
312 fflush (stdout);
313 gcry_cipher_close (hd);
314 if (err)
315 {
316 fprintf (stderr, "gcry_cipher_decrypt failed: %s\n",
317 gpg_strerror (err) );
318 exit (1);
319 }
320 }
321
322 putchar ('\n');
323}
324
325
326static void
327do_powm ( const char *n_str, const char *e_str, const char *m_str)
328{
329 gcry_mpi_t e, n, msg, cip;
330 gcry_error_t err;
331 int i;
332
333 err = gcry_mpi_scan (&n, GCRYMPI_FMT_HEX, n_str, 0, 0);
334 if (err) BUG ();
335 err = gcry_mpi_scan (&e, GCRYMPI_FMT_HEX, e_str, 0, 0);
336 if (err) BUG ();
337 err = gcry_mpi_scan (&msg, GCRYMPI_FMT_HEX, m_str, 0, 0);
338 if (err) BUG ();
339
340 cip = gcry_mpi_new (0);
341
342 start_timer ();
343 for (i=0; i < 1000; i++)
344 gcry_mpi_powm (cip, msg, e, n);
345 stop_timer ();
346 printf (" %s", elapsed_time ()); fflush (stdout);
347/* { */
348/* char *buf; */
349
350/* if (gcry_mpi_aprint (GCRYMPI_FMT_HEX, (void**)&buf, NULL, cip)) */
351/* BUG (); */
352/* printf ("result: %s\n", buf); */
353/* gcry_free (buf); */
354/* } */
355 gcry_mpi_release (cip);
356 gcry_mpi_release (msg);
357 gcry_mpi_release (n);
358 gcry_mpi_release (e);
359}
360
361
362static void
363mpi_bench (void)
364{
365 printf ("%-10s", "powm"); fflush (stdout);
366
367 do_powm (
368"20A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E4",
369 "29",
370"B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8"
371 );
372 do_powm (
373 "20A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E41071F0246879A442658FBD18C1771571E7073EEEB2160BA0CBFB3404D627069A6CFBD53867AD2D9D40231648000787B5C84176B4336144644AE71A403CA40716",
374 "29",
375 "B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8FC4EE4BB446B83E48ABED7DB81CBF5E81DE4759E8D68AC985846D999F96B0D8A80E5C69D272C766AB8A23B40D50A4FA889FBC2BD2624222D8EB297F4BAEF8593847"
376 );
377 do_powm (
378 "20A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E41071F0246879A442658FBD18C1771571E7073EEEB2160BA0CBFB3404D627069A6CFBD53867AD2D9D40231648000787B5C84176B4336144644AE71A403CA4071620A94417D4D5EF2B2DA99165C7DC87DADB3979B72961AF90D09D59BA24CB9A10166FDCCC9C659F2B9626EC23F3FA425F564A072BA941B03FA81767CC289E41071F0246879A442658FBD18C1771571E7073EEEB2160BA0CBFB3404D627069A6CFBD53867AD2D9D40231648000787B5C84176B4336144644AE71A403CA40716",
379 "29",
380 "B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8FC4EE4BB446B83E48ABED7DB81CBF5E81DE4759E8D68AC985846D999F96B0D8A80E5C69D272C766AB8A23B40D50A4FA889FBC2BD2624222D8EB297F4BAEF8593847B870187A323F1ECD5B8A0B4249507335A1C4CE8394F38FD76B08C78A42C58F6EA136ACF90DFE8603697B1694A3D81114D6117AC1811979C51C4DD013D52F8FC4EE4BB446B83E48ABED7DB81CBF5E81DE4759E8D68AC985846D999F96B0D8A80E5C69D272C766AB8A23B40D50A4FA889FBC2BD2624222D8EB297F4BAEF8593847"
381 );
382
383 putchar ('\n');
384
385
386}
387
388
389int
390main( int argc, char **argv )
391{
392 if (argc)
393 { argc--; argv++; }
394
395 if ( !argc )
396 {
397 md_bench (NULL);
398 putchar ('\n');
399 cipher_bench (NULL);
400 putchar ('\n');
401 mpi_bench ();
402 putchar ('\n');
403 random_bench ();
404 }
405 else if ( !strcmp (*argv, "--help"))
406 fputs ("usage: benchmark [md|cipher|random|mpi [algonames]]\n", stdout);
407 else if ( !strcmp (*argv, "random"))
408 {
409 random_bench ();
410 }
411 else if ( !strcmp (*argv, "md"))
412 {
413 if (argc == 1)
414 md_bench (NULL);
415 else
416 for (argc--, argv++; argc; argc--, argv++)
417 md_bench ( *argv );
418 }
419 else if ( !strcmp (*argv, "cipher"))
420 {
421 if (argc == 1)
422 cipher_bench (NULL);
423 else
424 for (argc--, argv++; argc; argc--, argv++)
425 cipher_bench ( *argv );
426 }
427 else if ( !strcmp (*argv, "mpi"))
428 {
429 mpi_bench ();
430 }
431 else
432 {
433 fprintf (stderr, PGM ": bad arguments\n");
434 return 1;
435 }
436
437 return 0;
438}
439
440