~ubuntu-branches/ubuntu/natty/libgcrypt11/natty-proposed

« back to all changes in this revision

Viewing changes to tests/pkbench.c

  • Committer: Bazaar Package Importer
  • Author(s): Bhavani Shankar
  • Date: 2009-05-16 20:13:32 UTC
  • mfrom: (1.1.6 upstream) (2.1.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090516201332-czkobpu32w318i16
Tags: 1.4.4-2ubuntu1
* Merge from Debian unstable (LP: #364535), remaining changes:
  - Add libgcrypt11-udeb for use by cryptsetup-udeb.
  - Add clean-la.mk, and add a symlink for the .la
  - Install to /lib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* pkbench.c - Pubkey menchmarking
2
 
 *      Copyright (C) 2004, 2005 Free Software Foundation, Inc.
 
2
 * Copyright (C) 2004, 2005, 2008 Free Software Foundation, Inc.
3
3
 *
4
4
 * This file is part of Libgcrypt.
5
5
 *
14
14
 * GNU Lesser General Public License for more details.
15
15
 *
16
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
 
17
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19
18
 */
20
19
 
21
20
#ifdef HAVE_CONFIG_H
26
25
#include <assert.h>
27
26
#include <stdlib.h>
28
27
#include <ctype.h>
29
 
#include <sys/mman.h>
30
28
#include <sys/stat.h>
31
29
#ifndef HAVE_W32_SYSTEM
32
 
#include <sys/times.h>
 
30
# include <sys/times.h>
33
31
#endif /*HAVE_W32_SYSTEM*/
34
32
#include <unistd.h>
35
33
#include <fcntl.h>
36
34
#include <time.h>
 
35
#include <errno.h>
37
36
 
38
37
#define PGM "pkbench"
39
38
 
88
87
 
89
88
  fputs (prefix, stderr);
90
89
  size = gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, NULL, 0);
91
 
  buf = malloc (size);
92
 
  if (!buf)
93
 
    die ("out of core\n");
 
90
  buf = gcry_xmalloc (size);
94
91
 
95
92
  gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, buf, size);
96
93
  fprintf (stderr, "%.*s", (int)size, buf);
 
94
  gcry_free (buf);
 
95
}
 
96
 
 
97
 
 
98
static void *
 
99
read_file (const char *fname, size_t *r_length)
 
100
{
 
101
  FILE *fp;
 
102
  struct stat st;
 
103
  char *buf;
 
104
  size_t buflen;
 
105
  
 
106
  fp = fopen (fname, "rb");
 
107
  if (!fp)
 
108
    {
 
109
      fail ("can't open `%s': %s\n", fname, strerror (errno));
 
110
      return NULL;
 
111
    }
 
112
  
 
113
  if (fstat (fileno(fp), &st))
 
114
    {
 
115
      fail ("can't stat `%s': %s\n", fname, strerror (errno));
 
116
      fclose (fp);
 
117
      return NULL;
 
118
    }
 
119
 
 
120
  buflen = st.st_size;
 
121
  buf = gcry_xmalloc (buflen+1);
 
122
  if (fread (buf, buflen, 1, fp) != 1)
 
123
    {
 
124
      fail ("error reading `%s': %s\n", fname, strerror (errno));
 
125
      fclose (fp);
 
126
      gcry_free (buf);
 
127
      return NULL;
 
128
    }
 
129
  fclose (fp);
 
130
 
 
131
  if (r_length)
 
132
    *r_length = buflen;
 
133
  return buf;
97
134
}
98
135
 
99
136
 
312
349
  gcry_sexp_t key_secret_sexp = NULL;
313
350
  gcry_sexp_t key_public_sexp = NULL;
314
351
  struct context context = { NULL };
315
 
  struct stat statbuf;
316
 
  int key_pair_fd = -1;
317
 
  int ret = 0;
318
 
 
319
 
  ret = stat (key_pair_file, &statbuf);
320
 
  assert (! ret);
321
 
 
322
 
  key_pair_fd = open (key_pair_file, O_RDONLY);
323
 
  assert (key_pair_fd != -1);
324
 
 
325
 
  key_pair_buffer = mmap (NULL, statbuf.st_size, PROT_READ,
326
 
                          MAP_PRIVATE, key_pair_fd, 0);
327
 
  assert (key_pair_buffer != MAP_FAILED);
 
352
  size_t file_length;
 
353
 
 
354
  key_pair_buffer = read_file (key_pair_file, &file_length);
 
355
  if (!key_pair_buffer)
 
356
    die ("failed to open `%s'\n", key_pair_file);
328
357
 
329
358
  err = gcry_sexp_sscan (&key_pair_sexp, NULL,
330
 
                         key_pair_buffer, statbuf.st_size);
331
 
  assert (! err);
 
359
                         key_pair_buffer, file_length);
 
360
  if (err)
 
361
    die ("gcry_sexp_sscan failed\n");
332
362
 
333
363
  key_secret_sexp = gcry_sexp_find_token (key_pair_sexp, "private-key", 0);
334
364
  assert (key_secret_sexp);
336
366
  assert (key_public_sexp);
337
367
 
338
368
  gcry_sexp_release (key_pair_sexp);
339
 
  ret = munmap (key_pair_buffer, statbuf.st_size);
340
 
  assert (! ret);
341
 
  ret = close (key_pair_fd);
342
 
  assert (! ret);
343
369
 
344
370
  context_init (&context, key_secret_sexp, key_public_sexp);
345
371
 
348
374
  printf ("\n");
349
375
 
350
376
  context_destroy (&context);
 
377
  gcry_free (key_pair_buffer);
351
378
}
352
379
 
353
380
 
380
407
 
381
408
  key_pair_buffer_size = gcry_sexp_sprint (key_pair, GCRYSEXP_FMT_ADVANCED,
382
409
                                           NULL, 0);
383
 
  key_pair_buffer = malloc (key_pair_buffer_size);
384
 
  assert (key_pair_buffer);
 
410
  key_pair_buffer = gcry_xmalloc (key_pair_buffer_size);
385
411
 
386
412
  gcry_sexp_sprint (key_pair, GCRYSEXP_FMT_ADVANCED,
387
413
                    key_pair_buffer, key_pair_buffer_size);
388
414
 
389
415
  printf ("%.*s", (int)key_pair_buffer_size, key_pair_buffer);
 
416
  gcry_free (key_pair_buffer);
390
417
}
391
418
 
392
419
 
396
423
{
397
424
  int last_argc = -1;
398
425
  int genkey_mode = 0;
 
426
  int fips_mode = 0;
399
427
 
400
428
  if (argc)
401
429
    { argc--; argv++; }
402
430
 
403
 
  gcry_control (GCRYCTL_DISABLE_SECMEM);
404
 
  if (!gcry_check_version (GCRYPT_VERSION))
405
 
    {
406
 
      fprintf (stderr, PGM ": version mismatch\n");
407
 
      exit (1);
408
 
    }
409
 
 
410
431
  while (argc && last_argc != argc )
411
432
    {
412
433
      last_argc = argc;
429
450
        }
430
451
      else if (!strcmp (*argv, "--verbose"))
431
452
        {
432
 
          verbose = 1;
 
453
          verbose++;
433
454
          argc--; argv++;
434
455
        }
435
456
      else if (!strcmp (*argv, "--debug"))
442
463
          genkey_mode = 1;
443
464
          argc--; argv++;
444
465
        }
 
466
      else if (!strcmp (*argv, "--fips"))
 
467
        {
 
468
          fips_mode = 1;
 
469
          argc--; argv++;
 
470
        }
445
471
    }          
446
472
 
 
473
  gcry_control (GCRYCTL_SET_VERBOSITY, (int)verbose);
 
474
 
 
475
  if (fips_mode)
 
476
    gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);
 
477
 
 
478
  gcry_control (GCRYCTL_DISABLE_SECMEM);
 
479
  if (!gcry_check_version (GCRYPT_VERSION))
 
480
    {
 
481
      fprintf (stderr, PGM ": version mismatch\n");
 
482
      exit (1);
 
483
    }
 
484
 
447
485
  if (genkey_mode)
448
486
    {
449
487
      /* No valuable keys are create, so we can speed up our RNG. */