hogweed-benchmark.c 21.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* hogweed-benchmark.c

   Copyright (C) 2013, 2014 Niels Möller

   This file is part of GNU Nettle.

   GNU Nettle is free software: you can redistribute it and/or
   modify it under the terms of either:

     * the GNU Lesser General Public License as published by the Free
       Software Foundation; either version 3 of the License, or (at your
       option) any later version.

   or

     * the GNU General Public License as published by the Free
       Software Foundation; either version 2 of the License, or (at your
       option) any later version.

   or both in parallel, as here.

   GNU Nettle is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received copies of the GNU General Public License and
   the GNU Lesser General Public License along with this program.  If
   not, see http://www.gnu.org/licenses/.
*/
Niels Möller's avatar
Niels Möller committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

#if HAVE_CONFIG_H
# include "config.h"
#endif

#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <errno.h>

#include <time.h>

46
47
#include "timing.h"

48
49
#include "dsa.h"
#include "rsa.h"
50
#include "curve25519.h"
Niels Möller's avatar
Niels Möller committed
51

52
53
54
#include "nettle-meta.h"
#include "sexp.h"
#include "knuth-lfib.h"
Niels Möller's avatar
Niels Möller committed
55
56
57
58
59
60

#include "../ecdsa.h"
#include "../ecc-internal.h"
#include "../gmp-glue.h"

#if WITH_OPENSSL
61
#include <openssl/rsa.h>
62
#include <openssl/bn.h>
Niels Möller's avatar
Niels Möller committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/objects.h>
#endif

#define BENCH_INTERVAL 0.1

static void NORETURN PRINTF_STYLE(1,2)
die(const char *format, ...)
{
  va_list args;
  va_start(args, format);
  vfprintf(stderr, format, args);
  va_end(args);

  exit(EXIT_FAILURE);
}

static void *
xalloc (size_t size)
{
  void *p = malloc (size);
  if (!p)
    {
      fprintf (stderr, "Virtual memory exhausted\n");
      abort ();
    }
  return p;
}

static uint8_t *
94
hash_string (const struct nettle_hash *hash, const char *s)
Niels Möller's avatar
Niels Möller committed
95
96
97
98
{
  void *ctx = xalloc (hash->context_size);
  uint8_t *digest = xalloc (hash->digest_size);
  hash->init (ctx);
99
  hash->update (ctx, strlen(s), (const uint8_t *) s);
Niels Möller's avatar
Niels Möller committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  hash->digest (ctx, hash->digest_size, digest);
  free (ctx);

  return digest;
}

struct alg {
  const char *name;
  unsigned size;
  void *(*init) (unsigned size);
  void (*sign)  (void *);
  void (*verify)(void *);
  void (*clear) (void *);
};

/* Returns second per function call */
static double
time_function(void (*f)(void *arg), void *arg)
{
  unsigned ncalls;
  double elapsed;

  /* Warm up */
  f(arg);
  for (ncalls = 10 ;;)
    {
      unsigned i;

128
      time_start();
Niels Möller's avatar
Niels Möller committed
129
130
      for (i = 0; i < ncalls; i++)
	f(arg);
131
      elapsed = time_end();
Niels Möller's avatar
Niels Möller committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
      if (elapsed > BENCH_INTERVAL)
	break;
      else if (elapsed < BENCH_INTERVAL / 10)
	ncalls *= 10;
      else
	ncalls *= 2;
    }
  return elapsed / ncalls;
}

static void 
bench_alg (const struct alg *alg)
{
  double sign;
  double verify;
  void *ctx;

  ctx = alg->init(alg->size);
150
151
  if (ctx == NULL)
    {
152
      printf("%16s %4d N/A\n", alg->name, alg->size);
153
154
      return;
    }
Niels Möller's avatar
Niels Möller committed
155
156
157
158
159
160

  sign = time_function (alg->sign, ctx);
  verify = time_function (alg->verify, ctx);

  alg->clear (ctx);

161
  printf("%16s %4d %9.4f %9.4f\n",
Niels Möller's avatar
Niels Möller committed
162
163
164
165
166
167
168
	 alg->name, alg->size, 1e-3/sign, 1e-3/verify);
}

struct rsa_ctx
{
  struct rsa_public_key pub;
  struct rsa_private_key key;
169
  struct knuth_lfib_ctx lfib;
Niels Möller's avatar
Niels Möller committed
170
171
172
173
174
175
176
  uint8_t *digest;
  mpz_t s;
};

static void *
bench_rsa_init (unsigned size)
{
Niels Möller's avatar
Niels Möller committed
177
  unsigned char rsa1024[] =
Niels Möller's avatar
Niels Möller committed
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    "{KDExOnByaXZhdGUta2V5KDE0OnJzYS1wa2NzMS1zaGExKDE6bjEyOToA90+K5EmjbFJBeJD"
    " xP2KD2Df+0Twc9425uB+vhqTrVijtd2PnwEQDfR2VoducgkKcXJzYYyCNILQJbFAi2Km/sD"
    " jImERBqDtaI217Ze+tOKEmImexYTAgFuqEptp2F3M4DqgRQ7s/3nJQ/bPE5Hfi1OZhJSShu"
    " I80ATTU4fUgrPspKDE6ZTM6AQABKSgxOmQxMjk6APAhKckzvxxkWfHJOpXDACWnaSKcbbvo"
    " vtWK3pGr/F2ya7CrLtE+uOx5F1sLs9G+/7flCy5k4uNILIYg4VTirZ1zQ8fNKPrjK1VMRls"
    " JiRRU/0VAs9d7HdncJfs6rbvRQbCRSRYURo4hWir3Lq8V3UUQVBprc4dO+uWmplvwQ5qxKS"
    " gxOnA2NToA+8aIVkdbk8Jg8dJOuzc7m/hZnwkKSs6eVDw4N/2T0CJKGJYT+B3Ct+fPkxhUR"
    " ggd2DQ9OpkTra7SXHTNkzdPVSkoMTpxNjU6APt11P8vXNnGYF0OC/cPsR8zhSYuFmtRuX6G"
    " ES+DdG0VCU07UeNQkok1UoW5sXqY0IGr1jkJq8AMSgKoNbLQ6w8pKDE6YTY0Ohzkxsan/8F"
    " wQDHgQbrIduXKVXaj0fONzKu8EXOTfUAYf0pdBsOlnq/+QVsPIrS6v7oNHK253YFEG84SdX"
    " kcktUpKDE6YjY1OgCR+cRtY3RWY+f6/TWK9gwPndv03xpasLWrMm71ky1aSbT9pasS9+opR"
    " tAiGzthfSbFsBiLQgb3VOr+AeIybT+XKSgxOmM2NDojigqARWN5u1CVDVuD2L2ManpoGiM6"
    " kQ6FaJjqRjxeRRKFrQxGJa9tM1hqStxokC1oJidgaOLGnn60iwzToug9KSkp}";
    
Niels Möller's avatar
Niels Möller committed
192
  unsigned char rsa2048[] =
Niels Möller's avatar
Niels Möller committed
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
    "{KDExOnByaXZhdGUta2V5KDE0OnJzYS1wa2NzMS1zaGExKDE6bjI1NzoAtxWXiglIdunDK48"
    " 8I0vW0wTqnh/riW9pLk8n1F8MUPBFdhvkkl0bDQqSJPUvSHy+w4fLVwcEzeI4qFyo3b2Avz"
    " JK20MFbt/WfHD1TbxuK8rNqXyqmqjJ9vgjtV9nPzAz7CM9ogs3/RJHpcfZPQF15ifizleUZ"
    " aQT0GAXHZL7cePj10yGI2u3hgTkokVzdNC/1T34guKYpErg0pt0B/KejWpsFTb84z3tkR+B"
    " YVx07p/OoByZwoABgncS/uALl31fRS8jyJ2JqUiZOqe7XoO9hkDHYNCWUGUfNGQ7ZgVp9+e"
    " NQpracSjrp6Jnrj7r/oxJUx5ZDVNi18AzQadE/oKOrSkoMTplMzoBAAEpKDE6ZDI1NjogBT"
    " C5vaHk2kF+LtDvw2XRBj0aZq7FHK0ioklvBSicR0l+vKYfSxVeFIk22YLphJfAjtFraRjYA"
    " Uaze3E1Rt1rkxoweupKV++lWAQvElOaaR/LErirz/Vysjdck1D1ZjLOi+NNofSq2DWbsvY1"
    " iznZhQRP3lVf6XBls0iXrYs4gb0pBZqXLQW+j9Ihx6eantf1/6ZMKPgCkzcAZ0ABsCfaFSg"
    " ouNCzilblsgFEspEbb8QqrNQoStS3F/gMwWgDsr3+vQzBqt+7ykWoCJ9wctbYy9kEPq+hX3"
    " GP0hG6HdS81r9E8pgdf3wloNNMzYUHwn7poXGpOi8tG0pmR56TqD/BKSgxOnAxMjk6AN4AJ"
    " TiGPm9We2ga3Y0jpTfA3mWpUbhYgaXYLWA1/riniwq16fqxRIkWQT/O2KKpBVe6WSvNYq9u"
    " lM8N6bdPtDytJs6AOXy0X5vtJ953ZYVMhHbhmUxhIL9I+s0O1+LxMF8b9U4CrFyaTxd8Un/"
    " FXP1BvYJRrkoup6HYvOlGx36lKSgxOnExMjk6ANMfrfH6z/3o7K56aW6kSiloDDbKZQ0+W5"
    " 8LzP2ZOBLf6LX6jLhN3olU1Z0KGTM0S/1AxvwGjuRqhu+LcOJ7oUCUH3uusR5c5nSnArYPq"
    " +0wbco4BQngot/HmGN7U0EDsIWqPt/qoa/b8bCk+TOwJlknNq/PnZU26SPj48XS05lpKSgx"
    " OmExMjk6AJM2n3gLNW3ZeH5BindkktQU9qWNkV5geqDCaNyrEZ3bpI1WsrEGSj9p3Zz1ipz"
    " a3msdbLJqQS26c72WKUzg8tFltR0s1HJInjolGtIgdNbfNdwrn9+RbQjL2VyPokOg0wXO4W"
    " 14wlmqDhax33dRJmfe50964MvaglkGA8fhorrtKSgxOmIxMjk6AKMe+vrX2xRHf3dfxU5jS"
    " ZmsdqNuxZzx7UB5kazvUU/kCJ1yNH/CSoq5LULkpovVgFDwV84qEwWQ+SjkCBg1hWWsDJc3"
    " ZkobZUQENigM+72LiYiQt/PlyHI2eRuEEdNN0nm0DFhdpQeHXLoq/RBerYJ8tdgpBYxgnMn"
    " KLhaOykbhKSgxOmMxMjg6MVlKj2bjb7qFQVkLO1OPg28jSrtRpnQCR+qegN4ZmNam/qbest"
    " 8yn0JQ6gxX7PvP382+jx7uHHWHYYqPq/Flf8gqtOOcjqS5TJgVHz3F3xHWquo1ZofGtCMro"
    " Dd2c0xjRjIVGvLV6Ngs+HRdljRav40vRpTyEoEdlzHBQiILesopKSk=}";

  struct rsa_ctx *ctx;
  struct sexp_iterator i;

  int res;

  ctx = xalloc(sizeof(*ctx));

  rsa_public_key_init (&ctx->pub);
  rsa_private_key_init (&ctx->key);
  mpz_init (ctx->s);
228
  knuth_lfib_init (&ctx->lfib, 1);
Niels Möller's avatar
Niels Möller committed
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243

  /* NOTE: Base64-decodes the strings in-place */
  if (size == 1024)
    res = sexp_transport_iterator_first (&i, sizeof(rsa1024) - 1, rsa1024);
  else if (size == 2048)
    res = sexp_transport_iterator_first (&i, sizeof(rsa2048) - 1, rsa2048);
  else
    die ("Internal error.\n");

  if (! (res
	 && sexp_iterator_check_type (&i, "private-key")
	 && sexp_iterator_check_type (&i, "rsa-pkcs1-sha1")
	 && rsa_keypair_from_sexp_alist (&ctx->pub, &ctx->key, 0, &i)))
    die ("Internal error.\n");

244
  ctx->digest = hash_string (&nettle_sha256, "foo");
Niels Möller's avatar
Niels Möller committed
245
246
247
248
249
250
251
252
253

  rsa_sha256_sign_digest (&ctx->key, ctx->digest, ctx->s);
  
  return ctx;
}

static void
bench_rsa_sign (void *p)
{
254
  struct rsa_ctx *ctx = p;
Niels Möller's avatar
Niels Möller committed
255
256
257
258
259
260
261

  mpz_t s;
  mpz_init (s);
  rsa_sha256_sign_digest (&ctx->key, ctx->digest, s);
  mpz_clear (s);
}

262
263
264
265
266
267
268
269
270
271
272
273
274
static void
bench_rsa_sign_tr (void *p)
{
  struct rsa_ctx *ctx = p;

  mpz_t s;
  mpz_init (s);
  rsa_sha256_sign_digest_tr (&ctx->pub, &ctx->key,
			     &ctx->lfib, (nettle_random_func *)knuth_lfib_random,
			     ctx->digest, s);
  mpz_clear (s);
}

Niels Möller's avatar
Niels Möller committed
275
276
277
static void
bench_rsa_verify (void *p)
{
278
279
  struct rsa_ctx *ctx = p;
  if (! rsa_sha256_verify_digest (&ctx->pub, ctx->digest, ctx->s))
Niels Möller's avatar
Niels Möller committed
280
281
282
283
284
285
    die ("Internal error, rsa_sha256_verify_digest failed.\n");
}

static void
bench_rsa_clear (void *p)
{
286
  struct rsa_ctx *ctx = p;
Niels Möller's avatar
Niels Möller committed
287
288
289
290
291
292
293
294
295
296

  rsa_public_key_clear (&ctx->pub);
  rsa_private_key_clear (&ctx->key);
  mpz_clear (ctx->s);
  
  free (ctx->digest);
  free (ctx);
}

struct dsa_ctx
297
298
299
300
{
  struct dsa_params params;
  mpz_t pub;
  mpz_t key;
Niels Möller's avatar
Niels Möller committed
301
302
303
304
305
306
307
308
309
310
311
  struct knuth_lfib_ctx lfib;
  struct dsa_signature s;
  uint8_t *digest;
};

static void *
bench_dsa_init (unsigned size)
{
  struct dsa_ctx *ctx;
  struct sexp_iterator i;  

Niels Möller's avatar
Niels Möller committed
312
  unsigned char dsa1024[] =
Niels Möller's avatar
Niels Möller committed
313
314
315
316
317
318
319
320
321
322
323
324
325
    "{KDExOnByaXZhdGUta2V5KDM6ZHNhKDE6cDEyOToA2q4hOXEClLMXXMOl9xaPcGC/GeGmCMv"
    " VCaaW0uWc50DvvmJDPQPdCehyfZr/1dv2UDbx06TC7ls/IFd+BsDzGBRxqIQ44J20cn+0gt"
    " NMIXAocE1QhCCFaT5gXrk8zMlqBEGaP3RdpgxNanEXkTj2Wma8r1GtrLX3HPafio62jicpK"
    " DE6cTIxOgDN9pcW3exdVAesC9WsxwCGoJK24ykoMTpnMTI5OgCJr9DmKdiE0WJZB7HACESv"
    " Tpg1qZgc8E15byQ+OsHUyOTRrJRTcrgKZJW7dFRJ9cXmyi7XYCd3bJtu/2HRHLY1vd4qMvU"
    " 7Y8x08ooCoABGV7nGqcmdQfEho1OY6TZh2NikmPKZLeur3PZFpcZ8Dl+KVWtwC55plNC7Om"
    " iAQy8MaCkoMTp5MTI5OgDakk0LOUQwzOKt9FHOBmBKrWsvTm7549eScTUqH4OMm3btjUsXz"
    " MmlqEe+imwQCOW/AE3Xw9pXZeETWK0jlLe8k5vnKcNskerFwZ1eQKtOPPQty8IqQ9PEfF6B"
    " 0oVQiJg2maHUDWFnDkIBd7ZR1z8FnZMUxH9mH4kEUo6YQgtCdykoMTp4MjA6cOl3ijiiMjI"
    " pesFD8jxESWb2mn8pKSk=}";

  ctx = xalloc(sizeof(*ctx));

326
327
328
  dsa_params_init (&ctx->params);
  mpz_init (ctx->pub);
  mpz_init (ctx->key);
Niels Möller's avatar
Niels Möller committed
329
330
331
332
333
334
335
336
337
  dsa_signature_init (&ctx->s);
  knuth_lfib_init (&ctx->lfib, 1);

  if (size != 1024)
    die ("Internal error.\n");
  
  if (! (sexp_transport_iterator_first (&i, sizeof(dsa1024) - 1, dsa1024)
	 && sexp_iterator_check_type (&i, "private-key")
	 && sexp_iterator_check_type (&i, "dsa")
338
	 && dsa_keypair_from_sexp_alist (&ctx->params, ctx->pub, ctx->key,
339
					 0, DSA_SHA1_Q_BITS, &i)) )
Niels Möller's avatar
Niels Möller committed
340
341
    die ("Internal error.\n");

342
  ctx->digest = hash_string (&nettle_sha1, "foo");
Niels Möller's avatar
Niels Möller committed
343

344
345
346
  dsa_sign (&ctx->params, ctx->key,
	    &ctx->lfib, (nettle_random_func *)knuth_lfib_random,
	    SHA1_DIGEST_SIZE, ctx->digest, &ctx->s);
Niels Möller's avatar
Niels Möller committed
347
348
349
350
351
352
353

  return ctx;
}

static void
bench_dsa_sign (void *p)
{
354
  struct dsa_ctx *ctx = p;
Niels Möller's avatar
Niels Möller committed
355
356
357
  struct dsa_signature s;

  dsa_signature_init (&s);
358
359
360
  dsa_sign (&ctx->params, ctx->key,
	    &ctx->lfib, (nettle_random_func *)knuth_lfib_random,
	    SHA1_DIGEST_SIZE, ctx->digest, &s);
Niels Möller's avatar
Niels Möller committed
361
362
363
364
365
366
  dsa_signature_clear (&s);
}

static void
bench_dsa_verify (void *p)
{
367
  struct dsa_ctx *ctx = p;
368
  if (! dsa_verify (&ctx->params, ctx->pub, SHA1_DIGEST_SIZE, ctx->digest, &ctx->s))
Niels Möller's avatar
Niels Möller committed
369
370
371
372
373
374
    die ("Internal error, dsa_sha1_verify_digest failed.\n");
}

static void
bench_dsa_clear (void *p)
{
375
  struct dsa_ctx *ctx = p;
376
377
378
  dsa_params_clear (&ctx->params);
  mpz_clear (ctx->pub);
  mpz_clear (ctx->key);
Niels Möller's avatar
Niels Möller committed
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
  dsa_signature_clear (&ctx->s);
  free (ctx->digest);
  free (ctx);
}

struct ecdsa_ctx
{
  struct ecc_point pub;
  struct ecc_scalar key;
  struct knuth_lfib_ctx rctx;
  unsigned digest_size;
  uint8_t *digest;
  struct dsa_signature s;
};
  
static void *
bench_ecdsa_init (unsigned size)
{
  struct ecdsa_ctx *ctx;
  const struct ecc_curve *ecc;

  const char *xs;
  const char *ys;
  const char *zs;
  mpz_t x, y, z;
  
  ctx = xalloc (sizeof(*ctx));

  dsa_signature_init (&ctx->s);  
  knuth_lfib_init (&ctx->rctx, 17);

  switch (size)
    {
    case 192:
413
      ecc = &_nettle_secp_192r1;
Niels Möller's avatar
Niels Möller committed
414
415
416
      xs = "8e8e07360350fb6b7ad8370cfd32fa8c6bba785e6e200599";
      ys = "7f82ddb58a43d59ff8dc66053002b918b99bd01bd68d6736";
      zs = "f2e620e086d658b4b507996988480917640e4dc107808bdd";
417
      ctx->digest = hash_string (&nettle_sha1, "abc");
Niels Möller's avatar
Niels Möller committed
418
419
420
      ctx->digest_size = 20;
      break;
    case 224:
421
      ecc = &_nettle_secp_224r1;
Niels Möller's avatar
Niels Möller committed
422
423
424
      xs = "993bf363f4f2bc0f255f22563980449164e9c894d9efd088d7b77334";
      ys = "b75fff9849997d02d135140e4d0030944589586e22df1fc4b629082a";
      zs = "cdfd01838247f5de3cc70b688418046f10a2bfaca6de9ec836d48c27";
425
      ctx->digest = hash_string (&nettle_sha224, "abc");
Niels Möller's avatar
Niels Möller committed
426
427
428
429
430
      ctx->digest_size = 28;
      break;

      /* From RFC 4754 */
    case 256:
431
      ecc = &_nettle_secp_256r1;
Niels Möller's avatar
Niels Möller committed
432
433
434
      xs = "2442A5CC 0ECD015F A3CA31DC 8E2BBC70 BF42D60C BCA20085 E0822CB0 4235E970";
      ys = "6FC98BD7 E50211A4 A27102FA 3549DF79 EBCB4BF2 46B80945 CDDFE7D5 09BBFD7D";
      zs = "DC51D386 6A15BACD E33D96F9 92FCA99D A7E6EF09 34E70975 59C27F16 14C88A7F";
435
      ctx->digest = hash_string (&nettle_sha256, "abc");
Niels Möller's avatar
Niels Möller committed
436
437
438
      ctx->digest_size = 32;
      break;
    case 384:
439
      ecc = &_nettle_secp_384r1;
Niels Möller's avatar
Niels Möller committed
440
441
442
443
444
445
      xs = "96281BF8 DD5E0525 CA049C04 8D345D30 82968D10 FEDF5C5A CA0C64E6 465A97EA"
	"5CE10C9D FEC21797 41571072 1F437922";
      ys = "447688BA 94708EB6 E2E4D59F 6AB6D7ED FF9301D2 49FE49C3 3096655F 5D502FAD"
	"3D383B91 C5E7EDAA 2B714CC9 9D5743CA";
      zs = "0BEB6466 34BA8773 5D77AE48 09A0EBEA 865535DE 4C1E1DCB 692E8470 8E81A5AF"
	"62E528C3 8B2A81B3 5309668D 73524D9F";
446
      ctx->digest = hash_string (&nettle_sha384, "abc");
Niels Möller's avatar
Niels Möller committed
447
448
449
      ctx->digest_size = 48;
      break;
    case 521:
450
      ecc = &_nettle_secp_521r1;
Niels Möller's avatar
Niels Möller committed
451
452
453
454
455
456
457
458
459
460
      xs = "0151518F 1AF0F563 517EDD54 85190DF9 5A4BF57B 5CBA4CF2 A9A3F647 4725A35F"
	"7AFE0A6D DEB8BEDB CD6A197E 592D4018 8901CECD 650699C9 B5E456AE A5ADD190"
	"52A8";
      ys = "006F3B14 2EA1BFFF 7E2837AD 44C9E4FF 6D2D34C7 3184BBAD 90026DD5 E6E85317"
	"D9DF45CA D7803C6C 20035B2F 3FF63AFF 4E1BA64D 1C077577 DA3F4286 C58F0AEA"
	"E643";
      zs = "0065FDA3 409451DC AB0A0EAD 45495112 A3D813C1 7BFD34BD F8C1209D 7DF58491"
	"20597779 060A7FF9 D704ADF7 8B570FFA D6F062E9 5C7E0C5D 5481C5B1 53B48B37"
	"5FA1";

461
      ctx->digest = hash_string (&nettle_sha512, "abc");
Niels Möller's avatar
Niels Möller committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
      ctx->digest_size = 64;
      break;
    default:
      die ("Internal error.\n");
    }
  ecc_point_init (&ctx->pub, ecc);
  ecc_scalar_init (&ctx->key, ecc);

  mpz_init_set_str (x, xs, 16);
  mpz_init_set_str (y, ys, 16);
  mpz_init_set_str (z, zs, 16);

  ecc_point_set (&ctx->pub, x, y);
  ecc_scalar_set (&ctx->key, z);

  mpz_clear (x);
  mpz_clear (y);
  mpz_clear (z);

  ecdsa_sign (&ctx->key,
	      &ctx->rctx, (nettle_random_func *) knuth_lfib_random,
	      ctx->digest_size, ctx->digest,
	      &ctx->s);

  return ctx;
}

static void
bench_ecdsa_sign (void *p)
{
492
  struct ecdsa_ctx *ctx = p;
Niels Möller's avatar
Niels Möller committed
493
494
495
496
497
498
499
500
501
502
503
504
505
  struct dsa_signature s;

  dsa_signature_init (&s);
  ecdsa_sign (&ctx->key,
	      &ctx->rctx, (nettle_random_func *) knuth_lfib_random,
	      ctx->digest_size, ctx->digest,
	      &s);
  dsa_signature_clear (&s);
}

static void
bench_ecdsa_verify (void *p)
{
506
507
508
509
  struct ecdsa_ctx *ctx = p;
  if (! ecdsa_verify (&ctx->pub, 
		      ctx->digest_size, ctx->digest,
		      &ctx->s))
Niels Möller's avatar
Niels Möller committed
510
511
512
513
514
515
    die ("Internal error, _ecdsa_verify failed.\n");    
}

static void
bench_ecdsa_clear (void *p)
{
516
  struct ecdsa_ctx *ctx = p;
Niels Möller's avatar
Niels Möller committed
517
518
519
520
521
522
523
524
525
526

  ecc_point_clear (&ctx->pub);
  ecc_scalar_clear (&ctx->key);
  dsa_signature_clear (&ctx->s);
  free (ctx->digest);

  free (ctx);
}

#if WITH_OPENSSL
527
528
529
530
531
532
533
534
535
struct openssl_rsa_ctx
{
  RSA *key;
  unsigned char *ref;
  unsigned char *signature;
  unsigned int siglen;
  uint8_t *digest;
};

536
537
static struct openssl_rsa_ctx*
make_openssl_rsa_ctx (unsigned size)
538
539
{
  struct openssl_rsa_ctx *ctx = xalloc (sizeof (*ctx));
540
541
542
543
  BIGNUM *e = BN_new();
  BN_set_word(e, 65537);
  ctx->key = RSA_new();
  RSA_generate_key_ex (ctx->key, size, e, NULL);
544
545
  ctx->ref = xalloc (RSA_size (ctx->key));
  ctx->signature = xalloc (RSA_size (ctx->key));
546
  ctx->digest = hash_string (&nettle_sha1, "foo");
547
548
549

  if (! RSA_sign (NID_sha1, ctx->digest, SHA1_DIGEST_SIZE,
		  ctx->ref, &ctx->siglen, ctx->key))
550
    die ("OpenSSL RSA_sign failed.\n");
551

552
  BN_free(e);
553
554
555
  return ctx;
}

556
static void *
557
bench_openssl_rsa_init (unsigned size)
558
{
559
560
  struct openssl_rsa_ctx *ctx = make_openssl_rsa_ctx (size);
  RSA_blinding_off(ctx->key);
561
562
563
  return ctx;
}

564
565
566
567
568
569
static void *
bench_openssl_rsa_tr_init (unsigned size)
{
  return make_openssl_rsa_ctx (size);
}

570
571
572
static void
bench_openssl_rsa_sign (void *p)
{
573
  const struct openssl_rsa_ctx *ctx = p;
574
575
576
577
  unsigned siglen;

  if (! RSA_sign (NID_sha1, ctx->digest, SHA1_DIGEST_SIZE,
		  ctx->signature, &siglen, ctx->key))
578
    die ("OpenSSL RSA_sign failed.\n");
579
580
581
582
583
}

static void
bench_openssl_rsa_verify (void *p)
{
584
  const struct openssl_rsa_ctx *ctx = p;
585
586
  if (! RSA_verify (NID_sha1, ctx->digest, SHA1_DIGEST_SIZE,
		    ctx->ref, ctx->siglen, ctx->key))
587
    die ("OpenSSL RSA_verify failed.\n");    
588
589
590
591
592
}

static void
bench_openssl_rsa_clear (void *p)
{
593
  struct openssl_rsa_ctx *ctx = p;
594
595
596
597
598
599
600
601
  RSA_free (ctx->key);
  free (ctx->ref);
  free (ctx->signature);
  free (ctx->digest);
  free (ctx);
}

struct openssl_ecdsa_ctx
Niels Möller's avatar
Niels Möller committed
602
603
604
605
606
607
608
609
{
  EC_KEY *key;
  ECDSA_SIG *signature;
  unsigned digest_length;
  uint8_t *digest;
};

static void *
610
bench_openssl_ecdsa_init (unsigned size)
Niels Möller's avatar
Niels Möller committed
611
{
612
  struct openssl_ecdsa_ctx *ctx = xalloc (sizeof (*ctx));
Niels Möller's avatar
Niels Möller committed
613
614
615
616

  switch (size)
    {
    case 192:
617
      ctx->key = EC_KEY_new_by_curve_name (NID_X9_62_prime192v1);
Niels Möller's avatar
Niels Möller committed
618
      ctx->digest_length = 24; /* truncated */
619
      ctx->digest = hash_string (&nettle_sha224, "abc");
Niels Möller's avatar
Niels Möller committed
620
621
622
623
      break;
    case 224:
      ctx->key = EC_KEY_new_by_curve_name (NID_secp224r1);
      ctx->digest_length = SHA224_DIGEST_SIZE;
624
      ctx->digest = hash_string (&nettle_sha224, "abc");
Niels Möller's avatar
Niels Möller committed
625
626
      break;
    case 256:
627
      ctx->key = EC_KEY_new_by_curve_name (NID_X9_62_prime256v1);
Niels Möller's avatar
Niels Möller committed
628
      ctx->digest_length = SHA256_DIGEST_SIZE;
629
      ctx->digest = hash_string (&nettle_sha256, "abc");
Niels Möller's avatar
Niels Möller committed
630
631
632
633
      break;
    case 384:
      ctx->key = EC_KEY_new_by_curve_name (NID_secp384r1);
      ctx->digest_length = SHA384_DIGEST_SIZE;
634
      ctx->digest = hash_string (&nettle_sha384, "abc");
Niels Möller's avatar
Niels Möller committed
635
636
637
638
      break;
    case 521:
      ctx->key = EC_KEY_new_by_curve_name (NID_secp521r1);
      ctx->digest_length = SHA512_DIGEST_SIZE;
639
      ctx->digest = hash_string (&nettle_sha512, "abc");
Niels Möller's avatar
Niels Möller committed
640
641
642
643
      break;
    default:
      die ("Internal error.\n");
    }
644
645
646

  /* This curve isn't supported in this build of openssl */
  if (ctx->key == NULL)
647
648
649
650
    {
      free(ctx);
      return NULL;
    }
Niels Möller's avatar
Niels Möller committed
651
652
653
654
655
656
657
658
659
660

  if (!EC_KEY_generate_key( ctx->key))
    die ("Openssl EC_KEY_generate_key failed.\n");
  
  ctx->signature = ECDSA_do_sign (ctx->digest, ctx->digest_length, ctx->key);
  
  return ctx;
}

static void
661
bench_openssl_ecdsa_sign (void *p)
Niels Möller's avatar
Niels Möller committed
662
{
663
  const struct openssl_ecdsa_ctx *ctx = p;
Niels Möller's avatar
Niels Möller committed
664
665
666
667
668
  ECDSA_SIG *sig = ECDSA_do_sign (ctx->digest, ctx->digest_length, ctx->key);
  ECDSA_SIG_free (sig);
}

static void
669
bench_openssl_ecdsa_verify (void *p)
Niels Möller's avatar
Niels Möller committed
670
{
671
672
673
  const struct openssl_ecdsa_ctx *ctx = p;
  if (ECDSA_do_verify (ctx->digest, ctx->digest_length,
			 ctx->signature, ctx->key) != 1)
Niels Möller's avatar
Niels Möller committed
674
675
676
    die ("Openssl ECDSA_do_verify failed.\n");      
}
static void
677
bench_openssl_ecdsa_clear (void *p)
Niels Möller's avatar
Niels Möller committed
678
{
679
  struct openssl_ecdsa_ctx *ctx = p;
Niels Möller's avatar
Niels Möller committed
680
681
682
683
684
685
686
  ECDSA_SIG_free (ctx->signature);
  EC_KEY_free (ctx->key);
  free (ctx->digest);
  free (ctx);
}
#endif

687
688
struct curve25519_ctx
{
689
690
  uint8_t x[CURVE25519_SIZE];
  uint8_t s[CURVE25519_SIZE];
691
692
693
694
695
696
};

static void
bench_curve25519_mul_g (void *p)
{
  struct curve25519_ctx *ctx = p;
697
  uint8_t q[CURVE25519_SIZE];
698
699
700
701
702
703
704
  curve25519_mul_g (q, ctx->s);
}

static void
bench_curve25519_mul (void *p)
{
  struct curve25519_ctx *ctx = p;
705
  uint8_t q[CURVE25519_SIZE];
706
  curve25519_mul (q, ctx->s, ctx->x);
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
}

static void
bench_curve25519 (void)
{
  double mul_g;
  double mul;
  struct knuth_lfib_ctx lfib;
  struct curve25519_ctx ctx;
  knuth_lfib_init (&lfib, 2);

  knuth_lfib_random (&lfib, sizeof(ctx.s), ctx.s);
  curve25519_mul_g (ctx.x, ctx.s);

  mul_g = time_function (bench_curve25519_mul_g, &ctx);
  mul = time_function (bench_curve25519_mul, &ctx);

724
  printf("%16s %4d %9.4f %9.4f\n",
725
726
727
	 "curve25519", 255, 1e-3/mul_g, 1e-3/mul);
}

Niels Möller's avatar
Niels Möller committed
728
729
730
struct alg alg_list[] = {
  { "rsa",   1024, bench_rsa_init,   bench_rsa_sign,   bench_rsa_verify,   bench_rsa_clear },
  { "rsa",   2048, bench_rsa_init,   bench_rsa_sign,   bench_rsa_verify,   bench_rsa_clear },
731
732
  { "rsa-tr",   1024, bench_rsa_init,   bench_rsa_sign_tr,   bench_rsa_verify,   bench_rsa_clear },
  { "rsa-tr",   2048, bench_rsa_init,   bench_rsa_sign_tr,   bench_rsa_verify,   bench_rsa_clear },
733
734
735
#if WITH_OPENSSL
  { "rsa (openssl)",  1024, bench_openssl_rsa_init, bench_openssl_rsa_sign, bench_openssl_rsa_verify, bench_openssl_rsa_clear },
  { "rsa (openssl)",  2048, bench_openssl_rsa_init, bench_openssl_rsa_sign, bench_openssl_rsa_verify, bench_openssl_rsa_clear },
736
737
  { "rsa-tr (openssl)",  1024, bench_openssl_rsa_tr_init, bench_openssl_rsa_sign, bench_openssl_rsa_verify, bench_openssl_rsa_clear },
  { "rsa-tr (openssl)",  2048, bench_openssl_rsa_tr_init, bench_openssl_rsa_sign, bench_openssl_rsa_verify, bench_openssl_rsa_clear },
738
#endif
Niels Möller's avatar
Niels Möller committed
739
740
741
742
743
744
745
746
747
748
  { "dsa",   1024, bench_dsa_init,   bench_dsa_sign,   bench_dsa_verify,   bench_dsa_clear },
#if 0
  { "dsa",2048, bench_dsa_init, bench_dsa_sign,   bench_dsa_verify, bench_dsa_clear },
#endif
  { "ecdsa",  192, bench_ecdsa_init, bench_ecdsa_sign, bench_ecdsa_verify, bench_ecdsa_clear },
  { "ecdsa",  224, bench_ecdsa_init, bench_ecdsa_sign, bench_ecdsa_verify, bench_ecdsa_clear },
  { "ecdsa",  256, bench_ecdsa_init, bench_ecdsa_sign, bench_ecdsa_verify, bench_ecdsa_clear },
  { "ecdsa",  384, bench_ecdsa_init, bench_ecdsa_sign, bench_ecdsa_verify, bench_ecdsa_clear },
  { "ecdsa",  521, bench_ecdsa_init, bench_ecdsa_sign, bench_ecdsa_verify, bench_ecdsa_clear },
#if WITH_OPENSSL
749
  { "ecdsa (openssl)",  192, bench_openssl_ecdsa_init, bench_openssl_ecdsa_sign, bench_openssl_ecdsa_verify, bench_openssl_ecdsa_clear },
750
  { "ecdsa (openssl)",  224, bench_openssl_ecdsa_init, bench_openssl_ecdsa_sign, bench_openssl_ecdsa_verify, bench_openssl_ecdsa_clear },
751
  { "ecdsa (openssl)",  256, bench_openssl_ecdsa_init, bench_openssl_ecdsa_sign, bench_openssl_ecdsa_verify, bench_openssl_ecdsa_clear },
752
753
  { "ecdsa (openssl)",  384, bench_openssl_ecdsa_init, bench_openssl_ecdsa_sign, bench_openssl_ecdsa_verify, bench_openssl_ecdsa_clear },
  { "ecdsa (openssl)",  521, bench_openssl_ecdsa_init, bench_openssl_ecdsa_sign, bench_openssl_ecdsa_verify, bench_openssl_ecdsa_clear },
Niels Möller's avatar
Niels Möller committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
#endif
};

#define numberof(x)  (sizeof (x) / sizeof ((x)[0]))

int
main (int argc, char **argv)
{
  const char *filter = NULL;
  unsigned i;

  if (argc > 1)
    filter = argv[1];

768
  time_init();
769
  printf ("%16s %4s %9s %9s\n",
Niels Möller's avatar
Niels Möller committed
770
771
772
773
774
775
	  "name", "size", "sign/ms", "verify/ms");

  for (i = 0; i < numberof(alg_list); i++)
    if (!filter || strstr (alg_list[i].name, filter))
      bench_alg (&alg_list[i]);

776
777
778
  if (!filter || strstr("curve25519", filter))
    bench_curve25519();

Niels Möller's avatar
Niels Möller committed
779
780
  return EXIT_SUCCESS;
}