chiark / gitweb /
072ab3186169733ceb66cb22ba4bac254a20a201
[catacomb] / progs / perftest.c
1 /* -*-c-*-
2  *
3  * Measure performance of various operations (Unix-specific)
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #define _FILE_OFFSET_BITS 64
31
32 #include "config.h"
33
34 #include <errno.h>
35 #include <limits.h>
36 #include <math.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <time.h>
41
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45
46 #include <mLib/alloc.h>
47 #include <mLib/dstr.h>
48 #include <mLib/mdwopt.h>
49 #include <mLib/quis.h>
50 #include <mLib/report.h>
51 #include <mLib/sub.h>
52 #include <mLib/tv.h>
53
54 #include "rand.h"
55 #include "mp.h"
56 #include "mprand.h"
57 #include "fibrand.h"
58 #include "rsa.h"
59 #include "mpmont.h"
60 #include "mpbarrett.h"
61 #include "dh.h"
62 #include "pgen.h"
63 #include "ec.h"
64 #include "group.h"
65
66 #include "cc.h"
67 #include "gcipher.h"
68 #include "ghash.h"
69 #include "gmac.h"
70
71 #include "ectab.h"
72 #include "ptab.h"
73
74 /*----- Options -----------------------------------------------------------*/
75
76 typedef struct opts {
77   const char *name;                     /* Pre-configured named thing */
78   unsigned fbits;                       /* Field size bits */
79   unsigned gbits;                       /* Group size bits */
80   unsigned n;                           /* Number of factors */
81   unsigned i;                           /* Number of intervals (or zero) */
82   double t;                             /* Time for each interval (secs) */
83   unsigned f;                           /* Flags */
84 #define OF_NOCHECK 1u                   /*   Don't do group checking */
85 } opts;
86
87 /*----- Job switch --------------------------------------------------------*/
88
89 /* --- Barrett exponentiation --- */
90
91 typedef struct bar_ctx {
92   size_t n;
93   mpbarrett b;
94   mp_expfactor *e;
95 } bar_ctx;
96
97 static void *bar_init(opts *o)
98 {
99   bar_ctx *c = CREATE(bar_ctx);
100   gprime_param gp;
101   qd_parse qd;
102   size_t i;
103
104   if (o->name) {
105     qd.p = o->name;
106     if (dh_parse(&qd, &gp))
107       die(1, "bad prime group: %s", qd.e);
108   } else {
109     if (!o->fbits) o->fbits = 1024;
110     dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
111   }
112   mpbarrett_create(&c->b, gp.p);
113   if (!o->n) o->n = 1;
114   c->n = o->n;
115   c->e = xmalloc(c->n * sizeof(group_expfactor));
116   for (i = 0; i < c->n; i++) {
117     c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
118     c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
119   }
120   dh_paramfree(&gp);
121   return (c);
122 }
123
124 static void bar_run(void *cc)
125 {
126   bar_ctx *c = cc;
127   mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp);
128   MP_DROP(d);
129 }
130
131 static void barsim_run(void *cc)
132 {
133   bar_ctx *c = cc;
134   mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n);
135   MP_DROP(d);
136 }
137
138 /* --- Montgomery exponentiation --- */
139
140 typedef struct mont_ctx {
141   size_t n;
142   mpmont m;
143   mp_expfactor *e;
144 } mont_ctx;
145
146 static void *mont_init(opts *o)
147 {
148   mont_ctx *c = CREATE(mont_ctx);
149   gprime_param gp;
150   qd_parse qd;
151   size_t i;
152
153   if (o->name) {
154     qd.p = o->name;
155     if (dh_parse(&qd, &gp))
156       die(1, "bad prime group: %s", qd.e);
157   } else {
158     if (!o->fbits) o->fbits = 1024;
159     dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
160   }
161   mpmont_create(&c->m, gp.p);
162   if (!o->n) o->n = 1;
163   c->n = o->n;
164   c->e = xmalloc(c->n * sizeof(mp_expfactor));
165   for (i = 0; i < c->n; i++) {
166     c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
167     c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
168   }
169   dh_paramfree(&gp);
170   return (c);
171 }
172
173 static void mont_run(void *cc)
174 {
175   mont_ctx *c = cc;
176   mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp);
177   MP_DROP(d);
178 }
179
180 static void montsim_run(void *cc)
181 {
182   mont_ctx *c = cc;
183   mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n);
184   MP_DROP(d);
185 }
186
187 /* --- Group exponentiation --- */
188
189 typedef struct gr_ctx {
190   size_t n;
191   group *g;
192   group_expfactor *e;
193 } gr_ctx;
194
195 static void *grp_init(opts *o)
196 {
197   gr_ctx *c = CREATE(gr_ctx);
198   const char *e;
199   gprime_param gp;
200   qd_parse qd;
201   size_t i;
202
203   if (o->name) {
204     qd.p = o->name;
205     if (dh_parse(&qd, &gp))
206       die(1, "bad prime group: %s", qd.e);
207   } else {
208     if (!o->fbits) o->fbits = 1024;
209     dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
210   }
211   c->g = group_prime(&gp);
212   if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
213     die(1, "bad group: %s", e);
214   if (!o->n) o->n = 1;
215   c->n = o->n;
216   c->e = xmalloc(c->n * sizeof(group_expfactor));
217   for (i = 0; i < c->n; i++) {
218     c->e[i].base = G_CREATE(c->g);
219     G_FROMINT(c->g, c->e[i].base,
220               mprand_range(MP_NEW, gp.p, &rand_global, 0));
221     c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
222   }
223   dh_paramfree(&gp);
224   return (c);
225 }
226
227 static void *grec_init(opts *o)
228 {
229   gr_ctx *c = CREATE(gr_ctx);
230   const char *e;
231   ec_info ei;
232   ec p = EC_INIT;
233   size_t i;
234
235   if (!o->name)
236     die(1, "can't generate elliptic curves");
237   if ((e = ec_getinfo(&ei, o->name)) != 0)
238     die(1, "bad curve: %s", e);
239   c->g = group_ec(&ei);
240   if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
241     die(1, "bad group: %s", e);
242   if (!o->n) o->n = 1;
243   c->n = o->n;
244   c->e = xmalloc(c->n * sizeof(group_expfactor));
245   for (i = 0; i < c->n; i++) {
246     c->e[i].base = G_CREATE(c->g);
247     ec_rand(ei.c, &p, &rand_global);
248     G_FROMEC(c->g, c->e[i].base, &p);
249     c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0);
250   }
251   EC_DESTROY(&p);
252   return (c);
253 }
254
255 static void gr_run(void *cc)
256 {
257   gr_ctx *c = cc;
258   ge *x = G_CREATE(c->g);
259   G_EXP(c->g, x, c->e[0].base, c->e[0].exp);
260   G_DESTROY(c->g, x);
261 }
262
263 static void grsim_run(void *cc)
264 {
265   gr_ctx *c = cc;
266   ge *x = G_CREATE(c->g);
267   G_MEXP(c->g, x, c->e, c->n);
268   G_DESTROY(c->g, x);
269 }
270
271 /* --- RSA --- */
272
273 typedef struct rsapriv_ctx {
274   rsa_priv rp;
275   rsa_privctx rpc;
276   mp *m;
277 } rsapriv_ctx;
278
279 static void *rsapriv_init(opts *o)
280 {
281   rsapriv_ctx *c = CREATE(rsapriv_ctx);
282
283   if (!o->fbits) o->fbits = 1024;
284   rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
285   rsa_privcreate(&c->rpc, &c->rp, 0);
286   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
287   return (c);
288 }
289
290 static void *rsaprivblind_init(opts *o)
291 {
292   rsapriv_ctx *c = CREATE(rsapriv_ctx);
293
294   if (!o->fbits) o->fbits = 1024;
295   rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
296   rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
297   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
298   return (c);
299 }
300
301 static void rsapriv_run(void *cc)
302 {
303   rsapriv_ctx *c = cc;
304   mp *d = rsa_privop(&c->rpc, MP_NEW, c->m);
305   MP_DROP(d);
306 }
307
308 typedef struct rsapub_ctx {
309   rsa_pub rp;
310   rsa_pubctx rpc;
311   mp *m;
312 } rsapub_ctx;
313
314 static void *rsapub_init(opts *o)
315 {
316   rsapub_ctx *c = CREATE(rsapub_ctx);
317   rsa_priv rp;
318
319   if (!o->fbits) o->fbits = 1024;
320   rsa_gen(&rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
321   c->rp.n = MP_COPY(rp.n);
322   c->rp.e = MP_COPY(rp.e);
323   rsa_privfree(&rp);
324   rsa_pubcreate(&c->rpc, &c->rp);
325   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
326   return (c);
327 }
328
329 static void rsapub_run(void *cc)
330 {
331   rsapub_ctx *c = cc;
332   mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m);
333   MP_DROP(d);
334 }
335
336 /* --- Symmetric encryption --- */
337
338 typedef struct ksched_ctx {
339   const gccipher *c;
340   octet *k;
341   size_t ksz;
342 } ksched_ctx;
343
344 static void *ksched_init(opts *o)
345 {
346   ksched_ctx *c = CREATE(ksched_ctx);
347   if (!o->name)
348     die(1, "must specify encryption scheme name");
349   if ((c->c = gcipher_byname(o->name)) == 0)
350     die(1, "encryption scheme `%s' not known", o->name);
351   c->ksz = keysz(o->gbits/8, c->c->keysz);
352   c->k = xmalloc(c->ksz);
353   rand_get(RAND_GLOBAL, c->k, c->ksz);
354   return (c);
355 }
356
357 static void ksched_run(void *cc)
358 {
359   ksched_ctx *c = cc;
360   gcipher *gc = GC_INIT(c->c, c->k, c->ksz);
361   GC_DESTROY(gc);
362 }
363
364 typedef struct enc_ctx {
365   gcipher *c;
366   octet *m;
367   size_t sz;
368   size_t n;
369 } enc_ctx;
370
371 static void *enc_init(opts *o)
372 {
373   enc_ctx *c = CREATE(enc_ctx);
374   const gccipher *cc;
375   size_t ksz;
376   octet *k;
377   if (!o->name)
378     die(1, "must specify encryption scheme name");
379   if ((cc = gcipher_byname(o->name)) == 0)
380     die(1, "encryption scheme `%s' not known", o->name);
381   ksz = keysz(0, cc->keysz);
382   k = xmalloc(ksz);
383   rand_get(RAND_GLOBAL, k, ksz);
384   c->c = GC_INIT(cc, k, ksz);
385   xfree(k);
386   c->sz = o->gbits ? o->gbits : 65536;
387   c->n = o->n ? o->n : 16;
388   c->m = xmalloc(c->sz);
389   return (c);
390 }
391
392 static void enc_run(void *cc)
393 {
394   enc_ctx *c = cc;
395   size_t i;
396   for (i = 0; i < c->n; i++)
397     GC_ENCRYPT(c->c, c->m, c->m, c->sz);
398 }
399
400 /* --- Hashing --- */
401
402 typedef struct hash_ctx {
403   const gchash *h;
404   octet *m;
405   size_t sz;
406   size_t n;
407 } hash_ctx;
408
409 static void *hash_init(opts *o)
410 {
411   hash_ctx *c = CREATE(hash_ctx);
412   if (!o->name)
413     die(1, "must specify hash function name");
414   if ((c->h = ghash_byname(o->name)) == 0)
415     die(1, "hash function `%s' not known", o->name);
416   c->sz = o->gbits ? o->gbits : 65536;
417   c->n = o->n ? o->n : 16;
418   c->m = xmalloc(c->sz);
419   return (c);
420 }
421
422 static void hash_run(void *cc)
423 {
424   hash_ctx *c = cc;
425   size_t i;
426   ghash *h = GH_INIT(c->h);
427   for (i = 0; i < c->n; i++)
428     GH_HASH(h, c->m, c->sz);
429   GH_DONE(h, 0);
430   GH_DESTROY(h);
431 }
432
433 /* --- Job table --- */
434
435 typedef struct jobops {
436   const char *name;
437   void *(*init)(opts *);
438   void (*run)(void *);
439 } jobops;
440
441 static const jobops jobtab[] = {
442   { "g-prime-exp",              grp_init,               gr_run },
443   { "g-ec-mul",                 grec_init,              gr_run },
444   { "g-prime-exp-sim",          grp_init,               grsim_run },
445   { "g-ec-mul-sim",             grec_init,              grsim_run },
446   { "barrett-exp",              bar_init,               bar_run },
447   { "barrett-exp-sim",          bar_init,               barsim_run },
448   { "mont-exp",                 mont_init,              mont_run },
449   { "mont-exp-sim",             mont_init,              montsim_run },
450   { "rsa-priv",                 rsapriv_init,           rsapriv_run },
451   { "rsa-priv-blind",           rsaprivblind_init,      rsapriv_run },
452   { "rsa-pub",                  rsapub_init,            rsapub_run },
453   { "ksched",                   ksched_init,            ksched_run },
454   { "enc",                      enc_init,               enc_run },
455   { "hash",                     hash_init,              hash_run },
456   { 0,                          0,                      0 }
457 };
458
459 /*----- Main code ---------------------------------------------------------*/
460
461 void version(FILE *fp)
462 {
463   pquis(fp, "$, Catacomb " VERSION "\n");
464 }
465
466 static void usage(FILE *fp)
467 {
468   pquis(fp, "Usage: $ [-options] job\n");
469 }
470
471 static void help(FILE *fp)
472 {
473   version(fp);
474   putc('\n', fp);
475   usage(fp);
476   pquis(fp, "\n\
477 Various performance tests.\n\
478 \n\
479 Options:\n\
480 \n\
481 -h, --help              Show this help text.\n\
482 -v, --version           Show program version number.\n\
483 -u, --usage             Show terse usage message.\n\
484 -l, --list [ITEM...]    List all the various names of things.\n\
485 \n\
486 -C, --name=NAME         Select curve/DH-group/enc/hash name.\n\
487 -b, --field-bits        Field size for g-prime and rsa.\n\
488 -q, --no-check          Don't check field/group for validity.\n\
489 -B, --group-bits        Group size for g-prime; key size for ksched;\n\
490                           data size for enc and hash.\n\
491 -n, --factors=COUNT     Number of factors for {exp,mul}-sim.\n\
492 -i, --intervals=COUNT   Number of intervals to run for.  [0; forever]\n\
493 -t, --time=TIME         Length of an interval in seconds.  [1]\n\
494 ");
495 }
496
497 #define LISTS(LI)                                                       \
498   LI("Lists", list,                                                     \
499      listtab[i].name, listtab[i].name)                                  \
500   LI("Jobs", job,                                                       \
501      jobtab[i].name, jobtab[i].name)                                    \
502   LI("Elliptic curves", ec,                                             \
503      ectab[i].name, ectab[i].name)                                      \
504   LI("Diffie-Hellman groups", dh,                                       \
505      ptab[i].name, ptab[i].name)                                        \
506   LI("Encryption algorithms", cipher,                                   \
507      gciphertab[i], gciphertab[i]->name)                                \
508   LI("Hash functions", hash,                                            \
509      ghashtab[i], ghashtab[i]->name)
510
511 MAKELISTTAB(listtab, LISTS)
512
513 static unsigned uarg(const char *what, const char *p)
514 {
515   char *q;
516   unsigned long u;
517   errno = 0;
518   u = strtoul(p, &q, 0);
519   if (*q || u > UINT_MAX || q == p || errno)
520     die(1, "bad %s `%s'", what, p);
521   return (u);
522 }
523
524 static double farg(const char *what, const char *p)
525 {
526   char *q;
527   double f;
528   errno = 0;
529   f = strtod(p, &q);
530   if (*q || q == p || errno)
531     die(1, "bad %s `%s'", what, p);
532   return (f);
533 }
534
535 int main(int argc, char *argv[])
536 {
537   int i;
538   opts o = { 0 };
539   const jobops *j;
540   struct timeval tv_next, tv_now;
541   double t, ttot;
542   unsigned n;
543   unsigned long ii;
544   clock_t c_start, c_stop;
545   double itot;
546   void *p;
547
548   ego(argv[0]);
549   o.t = 1;
550   for (;;) {
551     static const struct option opts[] = {
552       { "help",         0,              0,      'h' },
553       { "version",      0,              0,      'v' },
554       { "usage",        0,              0,      'u' },
555       { "list",         0,              0,      'l' },
556       { "name",         OPTF_ARGREQ,    0,      'C' },
557       { "field-bits",   OPTF_ARGREQ,    0,      'b' },
558       { "group-bits",   OPTF_ARGREQ,    0,      'B' },
559       { "factors",      OPTF_ARGREQ,    0,      'n' },
560       { "intervals",    OPTF_ARGREQ,    0,      'i' },
561       { "time",         OPTF_ARGREQ,    0,      't' },
562       { "no-check",     0,              0,      'q' },
563       { 0,              0,              0,      0 }
564     };
565
566     i = mdwopt(argc, argv, "hvulC:b:B:n:i:t:q", opts, 0, 0, 0);
567     if (i < 0) break;
568     switch (i) {
569       case 'h': help(stdout); exit(0);
570       case 'v': version(stdout); exit(0);
571       case 'u': usage(stdout); exit(0);
572       case 'l': exit(displaylists(listtab, argv + optind));
573       case 'C': o.name = optarg; break;
574       case 'b': o.fbits = uarg("field bits", optarg); break;
575       case 'B': o.gbits = uarg("subgroup bits", optarg); break;
576       case 'n': o.n = uarg("factor count", optarg); break;
577       case 'i': o.i = uarg("interval count", optarg); break;
578       case 't': o.t = farg("interval length", optarg); break;
579       case 'q': o.f |= OF_NOCHECK; break;
580       default: usage(stderr); exit(1);
581     }
582   }
583   if (optind + 1 != argc) { usage(stderr); exit(1); }
584
585   for (j = jobtab; j->name; j++)
586     if (strcmp(j->name, argv[optind]) == 0) break;
587   if (!j->name) die(1, "unknown job type `%s'", argv[optind]);
588   p = j->init(&o);
589
590   n = 0;
591   ttot = itot =  0;
592   gettimeofday(&tv_now, 0);
593   do {
594     tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
595     ii = 0;
596     c_start = clock();
597     do {
598       j->run(p);
599       ii++;
600       gettimeofday(&tv_now, 0);
601     } while (TV_CMP(&tv_now, <, &tv_next));
602     c_stop = clock();
603     t = (double)(c_stop - c_start)/CLOCKS_PER_SEC;
604     itot += ii;
605     ttot += t;
606     printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
607            n, ii, ii/t, itot/ttot);
608     fflush(stdout);
609     n++;
610   } while (!o.i || n < o.i);
611
612   return (0);
613 }
614
615 /*----- That's all, folks -------------------------------------------------*/