Commit | Line | Data |
---|---|---|
e2edda68 | 1 | /* -*-c-*- |
2 | * | |
e2edda68 | 3 | * Measure performance of various operations (Unix-specific) |
4 | * | |
5 | * (c) 2004 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
e2edda68 | 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. | |
45c0fd36 | 16 | * |
e2edda68 | 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. | |
45c0fd36 | 21 | * |
e2edda68 | 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 | ||
cd6eca43 MW |
30 | #define _FILE_OFFSET_BITS 64 |
31 | ||
e2edda68 | 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 | ||
8ae2bc5c MW |
46 | #ifdef HAVE_LINUX_PERF_EVENT_H |
47 | # include <linux/perf_event.h> | |
48 | # include <asm/unistd.h> | |
49 | #endif | |
50 | ||
e2edda68 | 51 | #include <mLib/alloc.h> |
8ae2bc5c | 52 | #include <mLib/bits.h> |
e2edda68 | 53 | #include <mLib/dstr.h> |
141c1284 | 54 | #include <mLib/macros.h> |
e2edda68 | 55 | #include <mLib/mdwopt.h> |
56 | #include <mLib/quis.h> | |
57 | #include <mLib/report.h> | |
58 | #include <mLib/sub.h> | |
59 | #include <mLib/tv.h> | |
60 | ||
61 | #include "rand.h" | |
62 | #include "mp.h" | |
63 | #include "mprand.h" | |
64 | #include "fibrand.h" | |
65 | #include "rsa.h" | |
57fe52c7 MW |
66 | #include "mpint.h" |
67 | #include "mptext.h" | |
e2edda68 | 68 | #include "mpmont.h" |
69 | #include "mpbarrett.h" | |
70 | #include "dh.h" | |
71 | #include "pgen.h" | |
72 | #include "ec.h" | |
73 | #include "group.h" | |
fc2d44af | 74 | #include "x25519.h" |
643eb1bb | 75 | #include "x448.h" |
d56fd9d1 | 76 | #include "ed25519.h" |
c578d5d8 | 77 | #include "ed448.h" |
e2edda68 | 78 | |
c65df279 | 79 | #include "cc.h" |
ceded834 | 80 | #include "gaead.h" |
e2edda68 | 81 | #include "gcipher.h" |
82 | #include "ghash.h" | |
83 | #include "gmac.h" | |
ef7cf21d | 84 | #include "poly1305.h" |
19e6e199 | 85 | |
c65df279 | 86 | #include "ectab.h" |
87 | #include "ptab.h" | |
e2edda68 | 88 | |
89 | /*----- Options -----------------------------------------------------------*/ | |
90 | ||
91 | typedef struct opts { | |
92 | const char *name; /* Pre-configured named thing */ | |
8ae2bc5c | 93 | const char *opwhat; /* What to call operations */ |
e2edda68 | 94 | unsigned fbits; /* Field size bits */ |
95 | unsigned gbits; /* Group size bits */ | |
96 | unsigned n; /* Number of factors */ | |
97 | unsigned i; /* Number of intervals (or zero) */ | |
a43e80e3 | 98 | unsigned k; /* Main loop batch size */ |
8ae2bc5c | 99 | unsigned long sc; /* Scale factor */ |
e2edda68 | 100 | double t; /* Time for each interval (secs) */ |
57fe52c7 | 101 | mp *e; /* Public exponent */ |
e74e12bc MW |
102 | unsigned f; /* Flags */ |
103 | #define OF_NOCHECK 1u /* Don't do group checking */ | |
e2edda68 | 104 | } opts; |
105 | ||
106 | /*----- Job switch --------------------------------------------------------*/ | |
107 | ||
108 | /* --- Barrett exponentiation --- */ | |
109 | ||
110 | typedef struct bar_ctx { | |
111 | size_t n; | |
112 | mpbarrett b; | |
113 | mp_expfactor *e; | |
114 | } bar_ctx; | |
115 | ||
116 | static void *bar_init(opts *o) | |
117 | { | |
118 | bar_ctx *c = CREATE(bar_ctx); | |
119 | gprime_param gp; | |
120 | qd_parse qd; | |
121 | size_t i; | |
122 | ||
123 | if (o->name) { | |
124 | qd.p = o->name; | |
125 | if (dh_parse(&qd, &gp)) | |
126 | die(1, "bad prime group: %s", qd.e); | |
127 | } else { | |
128 | if (!o->fbits) o->fbits = 1024; | |
129 | dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0); | |
130 | } | |
131 | mpbarrett_create(&c->b, gp.p); | |
132 | if (!o->n) o->n = 1; | |
133 | c->n = o->n; | |
134 | c->e = xmalloc(c->n * sizeof(group_expfactor)); | |
135 | for (i = 0; i < c->n; i++) { | |
136 | c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0); | |
137 | c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0); | |
138 | } | |
139 | dh_paramfree(&gp); | |
140 | return (c); | |
141 | } | |
142 | ||
143 | static void bar_run(void *cc) | |
144 | { | |
145 | bar_ctx *c = cc; | |
146 | mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp); | |
147 | MP_DROP(d); | |
148 | } | |
149 | ||
150 | static void barsim_run(void *cc) | |
151 | { | |
152 | bar_ctx *c = cc; | |
153 | mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n); | |
154 | MP_DROP(d); | |
155 | } | |
156 | ||
157 | /* --- Montgomery exponentiation --- */ | |
158 | ||
159 | typedef struct mont_ctx { | |
160 | size_t n; | |
161 | mpmont m; | |
162 | mp_expfactor *e; | |
163 | } mont_ctx; | |
164 | ||
165 | static void *mont_init(opts *o) | |
166 | { | |
167 | mont_ctx *c = CREATE(mont_ctx); | |
168 | gprime_param gp; | |
169 | qd_parse qd; | |
170 | size_t i; | |
171 | ||
172 | if (o->name) { | |
173 | qd.p = o->name; | |
174 | if (dh_parse(&qd, &gp)) | |
175 | die(1, "bad prime group: %s", qd.e); | |
176 | } else { | |
177 | if (!o->fbits) o->fbits = 1024; | |
178 | dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0); | |
179 | } | |
180 | mpmont_create(&c->m, gp.p); | |
181 | if (!o->n) o->n = 1; | |
182 | c->n = o->n; | |
183 | c->e = xmalloc(c->n * sizeof(mp_expfactor)); | |
184 | for (i = 0; i < c->n; i++) { | |
185 | c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0); | |
186 | c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0); | |
187 | } | |
188 | dh_paramfree(&gp); | |
189 | return (c); | |
190 | } | |
191 | ||
192 | static void mont_run(void *cc) | |
193 | { | |
194 | mont_ctx *c = cc; | |
195 | mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp); | |
196 | MP_DROP(d); | |
197 | } | |
198 | ||
199 | static void montsim_run(void *cc) | |
200 | { | |
201 | mont_ctx *c = cc; | |
202 | mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n); | |
203 | MP_DROP(d); | |
204 | } | |
205 | ||
206 | /* --- Group exponentiation --- */ | |
207 | ||
208 | typedef struct gr_ctx { | |
209 | size_t n; | |
210 | group *g; | |
211 | group_expfactor *e; | |
212 | } gr_ctx; | |
213 | ||
214 | static void *grp_init(opts *o) | |
215 | { | |
216 | gr_ctx *c = CREATE(gr_ctx); | |
217 | const char *e; | |
218 | gprime_param gp; | |
219 | qd_parse qd; | |
220 | size_t i; | |
221 | ||
222 | if (o->name) { | |
223 | qd.p = o->name; | |
224 | if (dh_parse(&qd, &gp)) | |
225 | die(1, "bad prime group: %s", qd.e); | |
226 | } else { | |
227 | if (!o->fbits) o->fbits = 1024; | |
228 | dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0); | |
229 | } | |
230 | c->g = group_prime(&gp); | |
e74e12bc | 231 | if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0) |
e2edda68 | 232 | die(1, "bad group: %s", e); |
233 | if (!o->n) o->n = 1; | |
234 | c->n = o->n; | |
235 | c->e = xmalloc(c->n * sizeof(group_expfactor)); | |
236 | for (i = 0; i < c->n; i++) { | |
237 | c->e[i].base = G_CREATE(c->g); | |
238 | G_FROMINT(c->g, c->e[i].base, | |
239 | mprand_range(MP_NEW, gp.p, &rand_global, 0)); | |
240 | c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0); | |
241 | } | |
242 | dh_paramfree(&gp); | |
243 | return (c); | |
244 | } | |
245 | ||
246 | static void *grec_init(opts *o) | |
247 | { | |
248 | gr_ctx *c = CREATE(gr_ctx); | |
249 | const char *e; | |
250 | ec_info ei; | |
251 | ec p = EC_INIT; | |
252 | size_t i; | |
253 | ||
254 | if (!o->name) | |
255 | die(1, "can't generate elliptic curves"); | |
256 | if ((e = ec_getinfo(&ei, o->name)) != 0) | |
257 | die(1, "bad curve: %s", e); | |
258 | c->g = group_ec(&ei); | |
e74e12bc | 259 | if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0) |
e2edda68 | 260 | die(1, "bad group: %s", e); |
261 | if (!o->n) o->n = 1; | |
262 | c->n = o->n; | |
263 | c->e = xmalloc(c->n * sizeof(group_expfactor)); | |
264 | for (i = 0; i < c->n; i++) { | |
265 | c->e[i].base = G_CREATE(c->g); | |
266 | ec_rand(ei.c, &p, &rand_global); | |
267 | G_FROMEC(c->g, c->e[i].base, &p); | |
268 | c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0); | |
269 | } | |
270 | EC_DESTROY(&p); | |
271 | return (c); | |
272 | } | |
273 | ||
274 | static void gr_run(void *cc) | |
275 | { | |
276 | gr_ctx *c = cc; | |
277 | ge *x = G_CREATE(c->g); | |
278 | G_EXP(c->g, x, c->e[0].base, c->e[0].exp); | |
279 | G_DESTROY(c->g, x); | |
280 | } | |
281 | ||
282 | static void grsim_run(void *cc) | |
283 | { | |
284 | gr_ctx *c = cc; | |
285 | ge *x = G_CREATE(c->g); | |
286 | G_MEXP(c->g, x, c->e, c->n); | |
287 | G_DESTROY(c->g, x); | |
288 | } | |
289 | ||
fc2d44af MW |
290 | /* --- x25519 --- */ |
291 | ||
292 | typedef struct x25519_jobctx { | |
293 | octet k[X25519_KEYSZ]; | |
294 | octet p[X25519_PUBSZ]; | |
295 | } x25519_jobctx; | |
296 | ||
297 | static void *x25519_jobinit(opts *o) | |
298 | { | |
299 | x25519_jobctx *c = CREATE(x25519_jobctx); | |
300 | rand_get(RAND_GLOBAL, c->k, sizeof(c->k)); | |
301 | rand_get(RAND_GLOBAL, c->p, sizeof(c->p)); | |
302 | return (c); | |
303 | } | |
304 | ||
305 | static void x25519_jobrun(void *cc) | |
306 | { x25519_jobctx *c = cc; octet z[X25519_OUTSZ]; x25519(z, c->k, c->p); } | |
307 | ||
643eb1bb MW |
308 | /* --- x448 --- */ |
309 | ||
310 | typedef struct x448_jobctx { | |
311 | octet k[X448_KEYSZ]; | |
312 | octet p[X448_PUBSZ]; | |
313 | } x448_jobctx; | |
314 | ||
315 | static void *x448_jobinit(opts *o) | |
316 | { | |
317 | x448_jobctx *c = CREATE(x448_jobctx); | |
318 | rand_get(RAND_GLOBAL, c->k, sizeof(c->k)); | |
319 | rand_get(RAND_GLOBAL, c->p, sizeof(c->p)); | |
320 | return (c); | |
321 | } | |
322 | ||
323 | static void x448_jobrun(void *cc) | |
324 | { x448_jobctx *c = cc; octet z[X448_OUTSZ]; x448(z, c->k, c->p); } | |
325 | ||
d56fd9d1 MW |
326 | /* --- Ed25519 --- */ |
327 | ||
328 | typedef struct ed25519_signctx { | |
329 | octet k[ED25519_KEYSZ]; | |
330 | octet K[ED25519_PUBSZ]; | |
331 | octet m[64]; | |
332 | } ed25519_signctx; | |
333 | ||
334 | typedef struct ed25519_vrfctx { | |
335 | octet K[ED25519_PUBSZ]; | |
336 | octet m[64]; | |
337 | octet sig[ED25519_SIGSZ]; | |
338 | } ed25519_vrfctx; | |
339 | ||
340 | static void *ed25519_signinit(opts *o) | |
341 | { | |
342 | ed25519_signctx *c = CREATE(ed25519_signctx); | |
343 | ||
344 | rand_get(RAND_GLOBAL, c->k, sizeof(c->k)); | |
345 | rand_get(RAND_GLOBAL, c->m, sizeof(c->m)); | |
346 | ed25519_pubkey(c->K, c->k, sizeof(c->k)); | |
347 | return (c); | |
348 | } | |
349 | ||
350 | static void ed25519_signrun(void *cc) | |
351 | { | |
352 | ed25519_signctx *c = cc; | |
353 | octet sig[ED25519_SIGSZ]; | |
354 | ||
355 | ed25519_sign(sig, c->k, sizeof(c->k), c->K, c->m, sizeof(c->m)); | |
356 | } | |
357 | ||
358 | static void *ed25519_vrfinit(opts *o) | |
359 | { | |
360 | octet k[ED25519_KEYSZ]; | |
361 | ed25519_vrfctx *c = CREATE(ed25519_vrfctx); | |
362 | ||
363 | rand_get(RAND_GLOBAL, k, sizeof(k)); | |
364 | rand_get(RAND_GLOBAL, c->m, sizeof(c->m)); | |
365 | ed25519_pubkey(c->K, k, sizeof(k)); | |
366 | ed25519_sign(c->sig, k, sizeof(k), c->K, c->m, sizeof(c->m)); | |
367 | return (c); | |
368 | } | |
369 | ||
370 | static void ed25519_vrfrun(void *cc) | |
371 | { | |
372 | ed25519_vrfctx *c = cc; | |
373 | ed25519_verify(c->K, c->m, sizeof(c->m), c->sig); | |
374 | } | |
375 | ||
c578d5d8 MW |
376 | /* --- Ed448 --- */ |
377 | ||
378 | typedef struct ed448_signctx { | |
379 | octet k[ED448_KEYSZ]; | |
380 | octet K[ED448_PUBSZ]; | |
381 | octet m[64]; | |
382 | } ed448_signctx; | |
383 | ||
384 | typedef struct ed448_vrfctx { | |
385 | octet K[ED448_PUBSZ]; | |
386 | octet m[64]; | |
387 | octet sig[ED448_SIGSZ]; | |
388 | } ed448_vrfctx; | |
389 | ||
390 | static void *ed448_signinit(opts *o) | |
391 | { | |
392 | ed448_signctx *c = CREATE(ed448_signctx); | |
393 | ||
394 | rand_get(RAND_GLOBAL, c->k, sizeof(c->k)); | |
395 | rand_get(RAND_GLOBAL, c->m, sizeof(c->m)); | |
396 | ed448_pubkey(c->K, c->k, sizeof(c->k)); | |
397 | return (c); | |
398 | } | |
399 | ||
400 | static void ed448_signrun(void *cc) | |
401 | { | |
402 | ed448_signctx *c = cc; | |
403 | octet sig[ED448_SIGSZ]; | |
404 | ||
405 | ed448_sign(sig, c->k, sizeof(c->k), c->K, 0, 0, 0, c->m, sizeof(c->m)); | |
406 | } | |
407 | ||
408 | static void *ed448_vrfinit(opts *o) | |
409 | { | |
410 | octet k[ED448_KEYSZ]; | |
411 | ed448_vrfctx *c = CREATE(ed448_vrfctx); | |
412 | ||
413 | rand_get(RAND_GLOBAL, k, sizeof(k)); | |
414 | rand_get(RAND_GLOBAL, c->m, sizeof(c->m)); | |
415 | ed448_pubkey(c->K, k, sizeof(k)); | |
416 | ed448_sign(c->sig, k, sizeof(k), c->K, 0, 0, 0, c->m, sizeof(c->m)); | |
417 | return (c); | |
418 | } | |
419 | ||
420 | static void ed448_vrfrun(void *cc) | |
421 | { | |
422 | ed448_vrfctx *c = cc; | |
423 | ed448_verify(c->K, 0, 0, 0, c->m, sizeof(c->m), c->sig); | |
424 | } | |
425 | ||
e2edda68 | 426 | /* --- RSA --- */ |
427 | ||
428 | typedef struct rsapriv_ctx { | |
429 | rsa_priv rp; | |
430 | rsa_privctx rpc; | |
431 | mp *m; | |
432 | } rsapriv_ctx; | |
433 | ||
434 | static void *rsapriv_init(opts *o) | |
435 | { | |
436 | rsapriv_ctx *c = CREATE(rsapriv_ctx); | |
437 | ||
438 | if (!o->fbits) o->fbits = 1024; | |
57fe52c7 MW |
439 | if (!o->e) o->e = mp_fromulong(MP_NEW, 65537); |
440 | rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0); | |
e2edda68 | 441 | rsa_privcreate(&c->rpc, &c->rp, 0); |
442 | c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0); | |
443 | return (c); | |
444 | } | |
445 | ||
446 | static void *rsaprivblind_init(opts *o) | |
447 | { | |
448 | rsapriv_ctx *c = CREATE(rsapriv_ctx); | |
449 | ||
450 | if (!o->fbits) o->fbits = 1024; | |
57fe52c7 MW |
451 | if (!o->e) o->e = mp_fromulong(MP_NEW, 65537); |
452 | rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0); | |
e2edda68 | 453 | rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0)); |
454 | c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0); | |
455 | return (c); | |
456 | } | |
457 | ||
458 | static void rsapriv_run(void *cc) | |
459 | { | |
460 | rsapriv_ctx *c = cc; | |
461 | mp *d = rsa_privop(&c->rpc, MP_NEW, c->m); | |
462 | MP_DROP(d); | |
463 | } | |
464 | ||
465 | typedef struct rsapub_ctx { | |
466 | rsa_pub rp; | |
467 | rsa_pubctx rpc; | |
468 | mp *m; | |
469 | } rsapub_ctx; | |
470 | ||
471 | static void *rsapub_init(opts *o) | |
472 | { | |
473 | rsapub_ctx *c = CREATE(rsapub_ctx); | |
474 | rsa_priv rp; | |
475 | ||
476 | if (!o->fbits) o->fbits = 1024; | |
57fe52c7 MW |
477 | if (!o->e) o->e = mp_fromulong(MP_NEW, 65537); |
478 | rsa_gen_e(&rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0); | |
e2edda68 | 479 | c->rp.n = MP_COPY(rp.n); |
480 | c->rp.e = MP_COPY(rp.e); | |
481 | rsa_privfree(&rp); | |
482 | rsa_pubcreate(&c->rpc, &c->rp); | |
483 | c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0); | |
484 | return (c); | |
485 | } | |
486 | ||
487 | static void rsapub_run(void *cc) | |
488 | { | |
489 | rsapub_ctx *c = cc; | |
490 | mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m); | |
491 | MP_DROP(d); | |
492 | } | |
493 | ||
494 | /* --- Symmetric encryption --- */ | |
495 | ||
496 | typedef struct ksched_ctx { | |
497 | const gccipher *c; | |
498 | octet *k; | |
499 | size_t ksz; | |
500 | } ksched_ctx; | |
501 | ||
502 | static void *ksched_init(opts *o) | |
503 | { | |
504 | ksched_ctx *c = CREATE(ksched_ctx); | |
505 | if (!o->name) | |
506 | die(1, "must specify encryption scheme name"); | |
507 | if ((c->c = gcipher_byname(o->name)) == 0) | |
508 | die(1, "encryption scheme `%s' not known", o->name); | |
178c2540 MW |
509 | c->ksz = keysz(o->fbits/8, c->c->keysz); |
510 | if (o->fbits%8 || (o->fbits && c->ksz != o->fbits/8)) | |
511 | die(1, "bad key size %u for %s", o->fbits, o->name); | |
e2edda68 | 512 | c->k = xmalloc(c->ksz); |
513 | rand_get(RAND_GLOBAL, c->k, c->ksz); | |
514 | return (c); | |
515 | } | |
516 | ||
517 | static void ksched_run(void *cc) | |
518 | { | |
519 | ksched_ctx *c = cc; | |
520 | gcipher *gc = GC_INIT(c->c, c->k, c->ksz); | |
521 | GC_DESTROY(gc); | |
522 | } | |
523 | ||
524 | typedef struct enc_ctx { | |
525 | gcipher *c; | |
526 | octet *m; | |
527 | size_t sz; | |
528 | size_t n; | |
529 | } enc_ctx; | |
530 | ||
531 | static void *enc_init(opts *o) | |
532 | { | |
533 | enc_ctx *c = CREATE(enc_ctx); | |
534 | const gccipher *cc; | |
535 | size_t ksz; | |
536 | octet *k; | |
537 | if (!o->name) | |
538 | die(1, "must specify encryption scheme name"); | |
539 | if ((cc = gcipher_byname(o->name)) == 0) | |
540 | die(1, "encryption scheme `%s' not known", o->name); | |
178c2540 MW |
541 | ksz = keysz(o->fbits/8, cc->keysz); |
542 | if (o->fbits%8 || (o->fbits && ksz != o->fbits/8)) | |
543 | die(1, "bad key size %u for %s", o->fbits, o->name); | |
e2edda68 | 544 | k = xmalloc(ksz); |
545 | rand_get(RAND_GLOBAL, k, ksz); | |
546 | c->c = GC_INIT(cc, k, ksz); | |
547 | xfree(k); | |
548 | c->sz = o->gbits ? o->gbits : 65536; | |
549 | c->n = o->n ? o->n : 16; | |
8ae2bc5c | 550 | o->opwhat = "byte"; o->sc = c->n*c->sz; |
e2edda68 | 551 | c->m = xmalloc(c->sz); |
552 | return (c); | |
553 | } | |
554 | ||
555 | static void enc_run(void *cc) | |
556 | { | |
557 | enc_ctx *c = cc; | |
558 | size_t i; | |
559 | for (i = 0; i < c->n; i++) | |
560 | GC_ENCRYPT(c->c, c->m, c->m, c->sz); | |
561 | } | |
562 | ||
ceded834 MW |
563 | /* --- Authenticated encryption --- */ |
564 | ||
565 | typedef struct aeadsetup_ctx { | |
566 | const gcaead *aec; | |
567 | octet *k; size_t ksz; | |
568 | octet *n; size_t nsz; | |
569 | size_t tsz; | |
570 | } aeadsetup_ctx; | |
571 | ||
572 | static void *aeadsetup_init(opts *o) | |
573 | { | |
574 | aeadsetup_ctx *c = CREATE(aeadsetup_ctx); | |
575 | if (!o->name) | |
576 | die(1, "must specify encryption scheme name"); | |
577 | if ((c->aec = gaead_byname(o->name)) == 0) | |
578 | die(1, "aead scheme `%s' not known", o->name); | |
579 | c->ksz = keysz(o->fbits/8, c->aec->keysz); | |
580 | c->nsz = keysz_pad(o->gbits/8, c->aec->noncesz); | |
581 | c->tsz = keysz(0, c->aec->tagsz); | |
582 | if (o->fbits%8 || (o->fbits && c->ksz != o->fbits/8)) | |
583 | die(1, "bad key size %u for %s", o->fbits, o->name); | |
584 | if (o->gbits%8 || (o->gbits && c->nsz != o->gbits/8)) | |
585 | die(1, "bad nonce size %u for %s", o->gbits, o->name); | |
586 | c->k = xmalloc(c->ksz); rand_get(RAND_GLOBAL, c->k, c->ksz); | |
587 | c->n = xmalloc(c->nsz); rand_get(RAND_GLOBAL, c->n, c->nsz); | |
588 | return (c); | |
589 | } | |
590 | ||
591 | static void aeadsetup_run(void *cc) | |
592 | { | |
593 | aeadsetup_ctx *c = cc; | |
594 | gaead_key *k = GAEAD_KEY(c->aec, c->k, c->ksz); | |
595 | gaead_enc *e = GAEAD_ENC(k, c->n, c->nsz, 0, 0, c->tsz); | |
596 | GAEAD_DESTROY(e); GAEAD_DESTROY(k); | |
597 | } | |
598 | ||
599 | typedef struct aeadenc_ctx { | |
600 | gaead_enc *enc; | |
601 | octet *n; size_t nsz; | |
602 | octet *p, *q; size_t sz; size_t nn; | |
603 | size_t tsz; | |
604 | } aeadenc_ctx; | |
605 | ||
606 | static void *aeadenc_init(opts *o) | |
607 | { | |
608 | aeadenc_ctx *c = CREATE(aeadenc_ctx); | |
609 | const gcaead *aec; | |
610 | gaead_key *key; | |
611 | octet *k; size_t ksz; | |
612 | ||
613 | if (!o->name) | |
614 | die(1, "must specify encryption scheme name"); | |
615 | if ((aec = gaead_byname(o->name)) == 0) | |
616 | die(1, "aead scheme `%s' not known", o->name); | |
617 | c->sz = o->gbits ? o->gbits : 65536; | |
618 | c->nn = o->n ? o->n : 16; | |
619 | ksz = keysz(o->fbits/8, aec->keysz); | |
620 | c->nsz = keysz(0, aec->noncesz); | |
621 | c->tsz = keysz(0, aec->tagsz); | |
622 | if (o->fbits%8 || (o->fbits && ksz != o->fbits/8)) | |
623 | die(1, "bad key size %u for %s", o->fbits, o->name); | |
624 | ||
625 | k = xmalloc(ksz); rand_get(RAND_GLOBAL, k, ksz); | |
626 | c->n = xmalloc(c->nsz); rand_get(RAND_GLOBAL, c->n, c->nsz); | |
627 | c->p = xmalloc(c->sz); c->q = xmalloc(c->sz + aec->bufsz); | |
628 | ||
629 | key = GAEAD_KEY(aec, k, ksz); | |
630 | c->enc = GAEAD_ENC(key, c->n, c->nsz, 0, 0, c->tsz); | |
631 | GAEAD_DESTROY(key); xfree(k); | |
632 | ||
633 | o->opwhat = "byte"; o->sc = c->nn*c->sz; | |
634 | return (c); | |
635 | } | |
636 | ||
637 | static void aeadaad_run(void *cc) | |
638 | { | |
639 | aeadenc_ctx *c = cc; | |
640 | gaead_aad *a; | |
641 | size_t i; | |
642 | ||
643 | GAEAD_REINIT(c->enc, c->n, c->nsz, c->nn*c->sz, 0, c->tsz); | |
644 | a = GAEAD_AAD(c->enc); | |
645 | for (i = 0; i < c->nn; i++) GAEAD_HASH(a, c->p, c->sz); | |
646 | GAEAD_DESTROY(a); | |
647 | } | |
648 | ||
649 | static void aeadenc_run(void *cc) | |
650 | { | |
651 | aeadenc_ctx *c = cc; | |
652 | buf b; | |
653 | size_t i; | |
654 | ||
655 | GAEAD_REINIT(c->enc, c->n, c->nsz, 0, c->nn*c->sz, c->tsz); | |
656 | for (i = 0; i < c->nn; i++) { | |
657 | buf_init(&b, c->q, c->sz + c->enc->ops->c->bufsz); | |
658 | GAEAD_ENCRYPT(c->enc, c->p, c->sz, &b); | |
659 | } | |
660 | } | |
661 | ||
e2edda68 | 662 | /* --- Hashing --- */ |
663 | ||
664 | typedef struct hash_ctx { | |
665 | const gchash *h; | |
666 | octet *m; | |
667 | size_t sz; | |
668 | size_t n; | |
669 | } hash_ctx; | |
670 | ||
671 | static void *hash_init(opts *o) | |
672 | { | |
673 | hash_ctx *c = CREATE(hash_ctx); | |
674 | if (!o->name) | |
675 | die(1, "must specify hash function name"); | |
676 | if ((c->h = ghash_byname(o->name)) == 0) | |
677 | die(1, "hash function `%s' not known", o->name); | |
678 | c->sz = o->gbits ? o->gbits : 65536; | |
679 | c->n = o->n ? o->n : 16; | |
8ae2bc5c | 680 | o->opwhat = "byte"; o->sc = c->n*c->sz; |
e2edda68 | 681 | c->m = xmalloc(c->sz); |
682 | return (c); | |
683 | } | |
684 | ||
685 | static void hash_run(void *cc) | |
686 | { | |
687 | hash_ctx *c = cc; | |
688 | size_t i; | |
689 | ghash *h = GH_INIT(c->h); | |
690 | for (i = 0; i < c->n; i++) | |
691 | GH_HASH(h, c->m, c->sz); | |
692 | GH_DONE(h, 0); | |
693 | GH_DESTROY(h); | |
694 | } | |
695 | ||
ef7cf21d MW |
696 | /* --- Poly1305 --- */ |
697 | ||
698 | typedef struct poly1305_jobctx { | |
699 | poly1305_key k; | |
700 | octet s[POLY1305_MASKSZ]; | |
701 | octet *m; | |
702 | size_t sz; | |
703 | size_t n; | |
704 | } poly1305_jobctx; | |
705 | ||
706 | static void *poly1305_jobinit(opts *o) | |
707 | { | |
708 | octet k[POLY1305_KEYSZ]; | |
709 | poly1305_jobctx *c = CREATE(poly1305_jobctx); | |
710 | rand_get(RAND_GLOBAL, k, sizeof(k)); | |
711 | poly1305_keyinit(&c->k, k, sizeof(k)); | |
712 | rand_get(RAND_GLOBAL, c->s, sizeof(c->s)); | |
713 | c->sz = o->gbits ? o->gbits : 65536; | |
714 | c->n = o->n ? o->n : 16; | |
8ae2bc5c | 715 | o->opwhat = "byte"; o->sc = c->n*c->sz; |
ef7cf21d MW |
716 | c->m = xmalloc(c->sz); |
717 | return (c); | |
718 | } | |
719 | ||
720 | static void poly1305_jobrun(void *cc) | |
721 | { | |
722 | poly1305_jobctx *c = cc; | |
723 | poly1305_ctx ctx; | |
724 | octet t[POLY1305_TAGSZ]; | |
725 | size_t i; | |
726 | poly1305_macinit(&ctx, &c->k, c->s); | |
727 | for (i = 0; i < c->n; i++) poly1305_hash(&ctx, c->m, c->sz); | |
728 | poly1305_done(&ctx, t); | |
729 | } | |
730 | ||
e2edda68 | 731 | /* --- Job table --- */ |
732 | ||
c65df279 | 733 | typedef struct jobops { |
e2edda68 | 734 | const char *name; |
735 | void *(*init)(opts *); | |
736 | void (*run)(void *); | |
737 | } jobops; | |
738 | ||
739 | static const jobops jobtab[] = { | |
45c0fd36 | 740 | { "g-prime-exp", grp_init, gr_run }, |
e2edda68 | 741 | { "g-ec-mul", grec_init, gr_run }, |
742 | { "g-prime-exp-sim", grp_init, grsim_run }, | |
743 | { "g-ec-mul-sim", grec_init, grsim_run }, | |
744 | { "barrett-exp", bar_init, bar_run }, | |
745 | { "barrett-exp-sim", bar_init, barsim_run }, | |
746 | { "mont-exp", mont_init, mont_run }, | |
747 | { "mont-exp-sim", mont_init, montsim_run }, | |
748 | { "rsa-priv", rsapriv_init, rsapriv_run }, | |
749 | { "rsa-priv-blind", rsaprivblind_init, rsapriv_run }, | |
750 | { "rsa-pub", rsapub_init, rsapub_run }, | |
fc2d44af | 751 | { "x25519", x25519_jobinit, x25519_jobrun }, |
643eb1bb | 752 | { "x448", x448_jobinit, x448_jobrun }, |
d56fd9d1 MW |
753 | { "ed25519-sign", ed25519_signinit, ed25519_signrun }, |
754 | { "ed25519-vrf", ed25519_vrfinit, ed25519_vrfrun }, | |
c578d5d8 MW |
755 | { "ed448-sign", ed448_signinit, ed448_signrun }, |
756 | { "ed448-vrf", ed448_vrfinit, ed448_vrfrun }, | |
e2edda68 | 757 | { "ksched", ksched_init, ksched_run }, |
758 | { "enc", enc_init, enc_run }, | |
ceded834 MW |
759 | { "aead-setup", aeadsetup_init, aeadsetup_run }, |
760 | { "aead-aad", aeadenc_init, aeadaad_run }, | |
761 | { "aead-enc", aeadenc_init, aeadenc_run }, | |
e2edda68 | 762 | { "hash", hash_init, hash_run }, |
ef7cf21d | 763 | { "poly1305", poly1305_jobinit, poly1305_jobrun }, |
e2edda68 | 764 | { 0, 0, 0 } |
765 | }; | |
766 | ||
8ae2bc5c MW |
767 | /*----- Cycle counting ----------------------------------------------------*/ |
768 | ||
769 | typedef kludge64 cycles; | |
770 | static int cyclecount_active_p = 0; | |
771 | ||
772 | #if defined(__GNUC__) && (CPUFAM_X86 || CPUFAM_AMD64) | |
773 | ||
774 | static void init_cyclecount(void) { cyclecount_active_p = 1; } | |
775 | ||
776 | static cycles cyclecount(void) | |
777 | { | |
778 | uint32 lo, hi; | |
779 | kludge64 cy; | |
780 | ||
781 | __asm__("rdtsc" : "=a"(lo), "=d"(hi)); | |
782 | SET64(cy, hi, lo); | |
783 | return cy; | |
784 | } | |
785 | ||
786 | #elif defined(HAVE_LINUX_PERF_EVENT_H) && defined(HAVE_UINT64) | |
787 | ||
788 | static int perf_fd = -1; | |
789 | ||
790 | static void init_cyclecount(void) | |
791 | { | |
792 | struct perf_event_attr attr = { 0 }; | |
793 | ||
794 | attr.type = PERF_TYPE_HARDWARE; | |
795 | attr.size = sizeof(attr); | |
796 | attr.config = PERF_COUNT_HW_CPU_CYCLES; | |
797 | attr.disabled = 0; | |
798 | attr.exclude_kernel = 1; | |
799 | attr.exclude_hv = 1; | |
800 | ||
801 | if ((perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0)) < 0) | |
802 | moan("failed to open perf event: %s", strerror(errno)); | |
803 | else | |
804 | cyclecount_active_p = 1; | |
805 | } | |
806 | ||
807 | static cycles cyclecount(void) | |
808 | { | |
809 | kludge64 cy; | |
810 | ssize_t n; | |
811 | ||
812 | if (!cyclecount_active_p) | |
813 | goto fail; | |
814 | else if ((n = read(perf_fd, &cy.i, sizeof(cy.i))) != sizeof(cy.i)) { | |
815 | if (n < 0) moan("error reading perf event: %s", strerror(errno)); | |
816 | else moan("unexpected short read from perf event"); | |
817 | cyclecount_active_p = 0; close(perf_fd); perf_fd = -1; | |
818 | goto fail; | |
819 | } | |
820 | end: | |
821 | return (cy); | |
822 | fail: | |
823 | SET64(cy, 0, 0); | |
824 | goto end; | |
825 | } | |
826 | ||
827 | #else | |
828 | ||
829 | static void init_cyclecount(void) { cyclecount_active_p = 0; } | |
830 | static cycles cyclecount(void) { kludge64 cy; SET64(cy, 0, 0); return (cy); } | |
831 | ||
832 | #endif | |
833 | ||
e2edda68 | 834 | /*----- Main code ---------------------------------------------------------*/ |
835 | ||
c65df279 | 836 | void version(FILE *fp) |
e2edda68 | 837 | { |
838 | pquis(fp, "$, Catacomb " VERSION "\n"); | |
839 | } | |
840 | ||
841 | static void usage(FILE *fp) | |
842 | { | |
843 | pquis(fp, "Usage: $ [-options] job\n"); | |
844 | } | |
845 | ||
846 | static void help(FILE *fp) | |
847 | { | |
848 | version(fp); | |
849 | putc('\n', fp); | |
850 | usage(fp); | |
851 | pquis(fp, "\n\ | |
852 | Various performance tests.\n\ | |
c65df279 | 853 | \n\ |
854 | Options:\n\ | |
855 | \n\ | |
856 | -h, --help Show this help text.\n\ | |
857 | -v, --version Show program version number.\n\ | |
858 | -u, --usage Show terse usage message.\n\ | |
859 | -l, --list [ITEM...] List all the various names of things.\n\ | |
860 | \n\ | |
861 | -C, --name=NAME Select curve/DH-group/enc/hash name.\n\ | |
178c2540 | 862 | -b, --field-bits Field size for g-prime and rsa;\n\ |
ceded834 | 863 | key bits for ksched, enc, aead-setup, aead-enc.\n\ |
78614e02 | 864 | -q, --no-check Don't check field/group for validity.\n\ |
ceded834 MW |
865 | -B, --group-bits Group size for g-prime; nonce bits for aead-setup;\n\ |
866 | data size for enc, aead-aad, aead-enc, and hash.\n\ | |
6fbaed95 | 867 | -n, --factors=COUNT Number of factors for {exp,mul}-sim;\n\ |
ceded834 | 868 | inner iters for enc, aead-aad, aead-enc, hash.\n\ |
c65df279 | 869 | -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\ |
a43e80e3 | 870 | -k, --batch=COUNT Number of operations to batch between timer checks.\n\ |
c65df279 | 871 | -t, --time=TIME Length of an interval in seconds. [1]\n\ |
e2edda68 | 872 | "); |
873 | } | |
874 | ||
c65df279 | 875 | #define LISTS(LI) \ |
876 | LI("Lists", list, \ | |
877 | listtab[i].name, listtab[i].name) \ | |
878 | LI("Jobs", job, \ | |
879 | jobtab[i].name, jobtab[i].name) \ | |
880 | LI("Elliptic curves", ec, \ | |
881 | ectab[i].name, ectab[i].name) \ | |
882 | LI("Diffie-Hellman groups", dh, \ | |
883 | ptab[i].name, ptab[i].name) \ | |
884 | LI("Encryption algorithms", cipher, \ | |
885 | gciphertab[i], gciphertab[i]->name) \ | |
ceded834 MW |
886 | LI("Authenticated encryption schemes", aead, \ |
887 | gaeadtab[i], gaeadtab[i]->name) \ | |
c65df279 | 888 | LI("Hash functions", hash, \ |
889 | ghashtab[i], ghashtab[i]->name) | |
890 | ||
891 | MAKELISTTAB(listtab, LISTS) | |
892 | ||
e2edda68 | 893 | static unsigned uarg(const char *what, const char *p) |
894 | { | |
895 | char *q; | |
896 | unsigned long u; | |
897 | errno = 0; | |
898 | u = strtoul(p, &q, 0); | |
899 | if (*q || u > UINT_MAX || q == p || errno) | |
900 | die(1, "bad %s `%s'", what, p); | |
901 | return (u); | |
902 | } | |
903 | ||
57fe52c7 MW |
904 | static mp *mparg(const char *what, const char *p) |
905 | { | |
906 | char *q; | |
907 | mp *x = mp_readstring(MP_NEW, p, &q, 0); | |
908 | if (!x || *q) die(1, "bad %s `%s'", what, p); | |
909 | return (x); | |
910 | } | |
911 | ||
e2edda68 | 912 | static double farg(const char *what, const char *p) |
913 | { | |
914 | char *q; | |
915 | double f; | |
916 | errno = 0; | |
917 | f = strtod(p, &q); | |
918 | if (*q || q == p || errno) | |
919 | die(1, "bad %s `%s'", what, p); | |
920 | return (f); | |
921 | } | |
922 | ||
923 | int main(int argc, char *argv[]) | |
924 | { | |
925 | int i; | |
926 | opts o = { 0 }; | |
927 | const jobops *j; | |
928 | struct timeval tv_next, tv_now; | |
8ae2bc5c | 929 | double t, ttot, cy, cytot; |
a43e80e3 | 930 | unsigned n, k; |
e2edda68 | 931 | unsigned long ii; |
24d2e65e | 932 | clock_t c0, c1; |
8ae2bc5c | 933 | kludge64 cy0, cy1, cydiff; |
e2edda68 | 934 | double itot; |
935 | void *p; | |
936 | ||
937 | ego(argv[0]); | |
8ae2bc5c | 938 | o.t = 1; o.k = 1; o.sc = 1; o.opwhat = "op"; |
e2edda68 | 939 | for (;;) { |
940 | static const struct option opts[] = { | |
941 | { "help", 0, 0, 'h' }, | |
942 | { "version", 0, 0, 'v' }, | |
943 | { "usage", 0, 0, 'u' }, | |
c65df279 | 944 | { "list", 0, 0, 'l' }, |
e2edda68 | 945 | { "name", OPTF_ARGREQ, 0, 'C' }, |
946 | { "field-bits", OPTF_ARGREQ, 0, 'b' }, | |
947 | { "group-bits", OPTF_ARGREQ, 0, 'B' }, | |
948 | { "factors", OPTF_ARGREQ, 0, 'n' }, | |
949 | { "intervals", OPTF_ARGREQ, 0, 'i' }, | |
a43e80e3 | 950 | { "batch", OPTF_ARGREQ, 0, 'k' }, |
57fe52c7 | 951 | { "public-exponent", OPTF_ARGREQ, 0, 'e' }, |
e2edda68 | 952 | { "time", OPTF_ARGREQ, 0, 't' }, |
e74e12bc | 953 | { "no-check", 0, 0, 'q' }, |
e2edda68 | 954 | { 0, 0, 0, 0 } |
955 | }; | |
956 | ||
a43e80e3 | 957 | i = mdwopt(argc, argv, "hvulC:b:B:n:i:k:e:t:q", opts, 0, 0, 0); |
e2edda68 | 958 | if (i < 0) break; |
959 | switch (i) { | |
960 | case 'h': help(stdout); exit(0); | |
961 | case 'v': version(stdout); exit(0); | |
962 | case 'u': usage(stdout); exit(0); | |
c65df279 | 963 | case 'l': exit(displaylists(listtab, argv + optind)); |
e2edda68 | 964 | case 'C': o.name = optarg; break; |
965 | case 'b': o.fbits = uarg("field bits", optarg); break; | |
966 | case 'B': o.gbits = uarg("subgroup bits", optarg); break; | |
967 | case 'n': o.n = uarg("factor count", optarg); break; | |
57fe52c7 MW |
968 | case 'e': |
969 | mp_drop(o.e); o.e = mparg("public exponent", optarg); | |
970 | if (MP_CMP(o.e, <, MP_THREE) || MP_EVENP(o.e)) | |
971 | die(1, "invalid public exponent"); | |
972 | break; | |
e2edda68 | 973 | case 'i': o.i = uarg("interval count", optarg); break; |
974 | case 't': o.t = farg("interval length", optarg); break; | |
a43e80e3 | 975 | case 'k': o.k = uarg("batch size", optarg); break; |
e74e12bc | 976 | case 'q': o.f |= OF_NOCHECK; break; |
e2edda68 | 977 | default: usage(stderr); exit(1); |
978 | } | |
979 | } | |
980 | if (optind + 1 != argc) { usage(stderr); exit(1); } | |
981 | ||
982 | for (j = jobtab; j->name; j++) | |
141c1284 | 983 | if (STRCMP(j->name, ==, argv[optind])) break; |
e2edda68 | 984 | if (!j->name) die(1, "unknown job type `%s'", argv[optind]); |
985 | p = j->init(&o); | |
986 | ||
987 | n = 0; | |
8ae2bc5c | 988 | ttot = itot = 0; cytot = 0; init_cyclecount(); |
e2edda68 | 989 | gettimeofday(&tv_now, 0); |
990 | do { | |
991 | tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION)); | |
992 | ii = 0; | |
8ae2bc5c | 993 | c0 = clock(); cy0 = cyclecount(); |
e2edda68 | 994 | do { |
a43e80e3 MW |
995 | for (k = 0; k < o.k; k++) { j->run(p); } |
996 | ii += k; | |
e2edda68 | 997 | gettimeofday(&tv_now, 0); |
998 | } while (TV_CMP(&tv_now, <, &tv_next)); | |
8ae2bc5c | 999 | cy1 = cyclecount(); c1 = clock(); |
24d2e65e MW |
1000 | t = (double)(c1 - c0)/CLOCKS_PER_SEC; |
1001 | itot += ii; ttot += t; | |
8ae2bc5c | 1002 | printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f", |
e2edda68 | 1003 | n, ii, ii/t, itot/ttot); |
8ae2bc5c MW |
1004 | if (cyclecount_active_p) { |
1005 | SUB64(cydiff, cy1, cy0); cy = LO64(cydiff) + ldexp(HI64(cydiff), 32); | |
1006 | cytot += cy; | |
1007 | printf(" (cy/%s = %3f; avg cy/%s = %3f)", | |
1008 | o.opwhat, cy/ii/o.sc, o.opwhat, cytot/itot/o.sc); | |
1009 | } | |
1010 | putchar('\n'); | |
e2edda68 | 1011 | fflush(stdout); |
1012 | n++; | |
1013 | } while (!o.i || n < o.i); | |
1014 | ||
1015 | return (0); | |
1016 | } | |
1017 | ||
1018 | /*----- That's all, folks -------------------------------------------------*/ |