summaryrefslogtreecommitdiff
path: root/tests/keygen.c
blob: ebb708b8df0fdf19ff66bfdf189a07141d3dde20 (plain)
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
94
95
96
97
98
99
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/* keygen.c  -  key generation regression tests
 * Copyright (C) 2003, 2005, 2012 Free Software Foundation, Inc.
 * Copyright (C) 2013, 2015 g10 Code GmbH
 *
 * This file is part of Libgcrypt.
 *
 * Libgcrypt is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Libgcrypt 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "../src/gcrypt-int.h"


#define PGM "keygen"

#define xmalloc(a)    gcry_xmalloc ((a))
#define xcalloc(a,b)  gcry_xcalloc ((a),(b))
#define xstrdup(a)    gcry_xstrdup ((a))
#define xfree(a)      gcry_free ((a))
#define pass()        do { ; } while (0)


static int verbose;
static int debug;
static int error_count;
static int in_fips_mode;


static void
die (const char *format, ...)
{
  va_list arg_ptr ;

  fflush (stdout);
  fprintf (stderr, "%s: ", PGM);
  va_start( arg_ptr, format ) ;
  vfprintf (stderr, format, arg_ptr );
  va_end(arg_ptr);
  if (*format && format[strlen(format)-1] != '\n')
    putc ('\n', stderr);
  exit (1);
}

static void
fail (const char *format, ...)
{
  va_list arg_ptr;

  fflush (stdout);
  fprintf (stderr, "%s: ", PGM);
  /* if (wherestr) */
  /*   fprintf (stderr, "%s: ", wherestr); */
  va_start (arg_ptr, format);
  vfprintf (stderr, format, arg_ptr);
  va_end (arg_ptr);
  if (*format && format[strlen(format)-1] != '\n')
    putc ('\n', stderr);
  error_count++;
  if (error_count >= 50)
    die ("stopped after 50 errors.");
}

static void
show (const char *format, ...)
{
  va_list arg_ptr;

  fprintf (stderr, "%s: ", PGM);
  va_start (arg_ptr, format);
  vfprintf (stderr, format, arg_ptr);
  if (*format && format[strlen(format)-1] != '\n')
    putc ('\n', stderr);
  va_end (arg_ptr);
}


/* static void */
/* show_note (const char *format, ...) */
/* { */
/*   va_list arg_ptr; */

/*   if (!verbose && getenv ("srcdir")) */
/*     fputs ("      ", stderr);  /\* To align above "PASS: ".  *\/ */
/*   else */
/*     fprintf (stderr, "%s: ", PGM); */
/*   va_start (arg_ptr, format); */
/*   vfprintf (stderr, format, arg_ptr); */
/*   if (*format && format[strlen(format)-1] != '\n') */
/*     putc ('\n', stderr); */
/*   va_end (arg_ptr); */
/* } */


static void
show_sexp (const char *prefix, gcry_sexp_t a)
{
  char *buf;
  size_t size;

  fprintf (stderr, "%s: ", PGM);
  if (prefix)
    fputs (prefix, stderr);
  size = gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, NULL, 0);
  buf = xmalloc (size);

  gcry_sexp_sprint (a, GCRYSEXP_FMT_ADVANCED, buf, size);
  fprintf (stderr, "%.*s", (int)size, buf);
  gcry_free (buf);
}


static void
show_mpi (const char *prefix, gcry_mpi_t a)
{
  char *buf;
  void *bufaddr = &buf;
  gcry_error_t rc;

  fprintf (stderr, "%s: ", PGM);
  if (prefix)
    fputs (prefix, stderr);
  rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, bufaddr, NULL, a);
  if (rc)
    fprintf (stderr, "[error printing number: %s]\n",  gpg_strerror (rc));
  else
    {
      fprintf (stderr, "%s\n", buf);
      gcry_free (buf);
    }
}


static void
check_generated_rsa_key (gcry_sexp_t key, unsigned long expected_e)
{
  gcry_sexp_t skey, pkey, list;

  pkey = gcry_sexp_find_token (key, "public-key", 0);
  if (!pkey)
    fail ("public part missing in return value\n");
  else
    {
      gcry_mpi_t e = NULL;

      list = gcry_sexp_find_token (pkey, "e", 0);
      if (!list || !(e=gcry_sexp_nth_mpi (list, 1, 0)) )
        fail ("public exponent not found\n");
      else if (!expected_e)
        {
          if (verbose)
            show_mpi ("public exponent: ", e);
        }
      else if ( gcry_mpi_cmp_ui (e, expected_e))
        {
          show_mpi ("public exponent: ", e);
          fail ("public exponent is not %lu\n", expected_e);
        }
      gcry_sexp_release (list);
      gcry_mpi_release (e);
      gcry_sexp_release (pkey);
    }

  skey = gcry_sexp_find_token (key, "private-key", 0);
  if (!skey)
    fail ("private part missing in return value\n");
  else
    {
      int rc = gcry_pk_testkey (skey);
      if (rc)
        fail ("gcry_pk_testkey failed: %s\n", gpg_strerror (rc));
      gcry_sexp_release (skey);
    }
}


static void
check_rsa_keys (void)
{
  gcry_sexp_t keyparm, key;
  int rc;

  if (verbose)
    show ("creating 2048 bit RSA key\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (rsa\n"
                      "  (nbits 4:2048)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc)
    die ("error generating RSA key: %s\n", gpg_strerror (rc));

  if (verbose)
    show ("creating 1024 bit RSA key\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (rsa\n"
                      "  (nbits 4:1024)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc && !in_fips_mode)
    fail ("error generating RSA key: %s\n", gpg_strerror (rc));
  else if (!rc && in_fips_mode)
    fail ("generating 1024 bit RSA key must not work!");

  if (!rc)
    {
      if (verbose > 1)
        show_sexp ("1024 bit RSA key:\n", key);
      check_generated_rsa_key (key, 65537);
    }
  gcry_sexp_release (key);

  if (verbose)
    show ("creating 2048 bit RSA key with e=65539\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (rsa\n"
                      "  (nbits 4:2048)\n"
                      "  (rsa-use-e 5:65539)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc)
    fail ("error generating RSA key: %s\n", gpg_strerror (rc));

  if (!rc)
    check_generated_rsa_key (key, 65539);
  gcry_sexp_release (key);


  if (verbose)
    show ("creating 512 bit RSA key with e=257\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (rsa\n"
                      "  (nbits 3:512)\n"
                      "  (rsa-use-e 3:257)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc && !in_fips_mode)
    fail ("error generating RSA key: %s\n", gpg_strerror (rc));
  else if (!rc && in_fips_mode)
    fail ("generating 512 bit RSA key must not work!");

  if (verbose && rc && in_fips_mode)
    show ("... correctly rejected key creation in FIPS mode (%s)\n",
          gpg_strerror (rc));

  if (!rc)
    check_generated_rsa_key (key, 257);
  gcry_sexp_release (key);

  if (verbose)
    show ("creating 512 bit RSA key with default e\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (rsa\n"
                      "  (nbits 3:512)\n"
                      "  (rsa-use-e 1:0)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc && !in_fips_mode)
    fail ("error generating RSA key: %s\n", gpg_strerror (rc));
  else if (!rc && in_fips_mode)
    fail ("generating 512 bit RSA key must not work!");

  if (verbose && rc && in_fips_mode)
    show ("... correctly rejected key creation in FIPS mode (%s)\n",
          gpg_strerror (rc));


  if (!rc)
    check_generated_rsa_key (key, 0); /* We don't expect a constant exponent. */
  gcry_sexp_release (key);
}


static void
check_elg_keys (void)
{
  gcry_sexp_t keyparm, key;
  int rc;

  if (verbose)
    show ("creating 1024 bit Elgamal key\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (elg\n"
                      "  (nbits 4:1024)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc)
    die ("error generating Elgamal key: %s\n", gpg_strerror (rc));
  if (verbose > 1)
    show_sexp ("1024 bit Elgamal key:\n", key);
  gcry_sexp_release (key);
}


static void
check_dsa_keys (void)
{
  gcry_sexp_t keyparm, key;
  int rc;
  int i;

  /* Check that DSA generation works and that it can grok the qbits
     argument. */
  if (verbose)
    show ("creating 5 1024 bit DSA keys\n");
  for (i=0; i < 5; i++)
    {
      rc = gcry_sexp_new (&keyparm,
                          "(genkey\n"
                          " (dsa\n"
                          "  (nbits 4:1024)\n"
                          " ))", 0, 1);
      if (rc)
        die ("error creating S-expression: %s\n", gpg_strerror (rc));
      rc = gcry_pk_genkey (&key, keyparm);
      gcry_sexp_release (keyparm);
      if (rc && !in_fips_mode)
        die ("error generating DSA key: %s\n", gpg_strerror (rc));
      else if (!rc && in_fips_mode)
        die ("generating 1024 bit DSA key must not work!");
      if (!i && verbose > 1)
        show_sexp ("1024 bit DSA key:\n", key);
      gcry_sexp_release (key);
    }

  if (verbose)
    show ("creating 1536 bit DSA key\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (dsa\n"
                      "  (nbits 4:1536)\n"
                      "  (qbits 3:224)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc && !in_fips_mode)
    die ("error generating DSA key: %s\n", gpg_strerror (rc));
  else if (!rc && in_fips_mode)
    die ("generating 1536 bit DSA key must not work!");
  if (verbose > 1)
    show_sexp ("1536 bit DSA key:\n", key);
  gcry_sexp_release (key);

  if (verbose)
    show ("creating 3072 bit DSA key\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (dsa\n"
                      "  (nbits 4:3072)\n"
                      "  (qbits 3:256)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc)
    die ("error generating DSA key: %s\n", gpg_strerror (rc));
  if (verbose > 1)
    show_sexp ("3072 bit DSA key:\n", key);
  gcry_sexp_release (key);

  if (verbose)
    show ("creating 2048/256 bit DSA key\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (dsa\n"
                      "  (nbits 4:2048)\n"
                      "  (qbits 3:256)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc)
    die ("error generating DSA key: %s\n", gpg_strerror (rc));
  if (verbose > 1)
    show_sexp ("2048 bit DSA key:\n", key);
  gcry_sexp_release (key);

  if (verbose)
    show ("creating 2048/224 bit DSA key\n");
  rc = gcry_sexp_new (&keyparm,
                      "(genkey\n"
                      " (dsa\n"
                      "  (nbits 4:2048)\n"
                      "  (qbits 3:224)\n"
                      " ))", 0, 1);
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc)
    die ("error generating DSA key: %s\n", gpg_strerror (rc));
  if (verbose > 1)
    show_sexp ("2048 bit DSA key:\n", key);
  gcry_sexp_release (key);
}


static void
check_generated_ecc_key (gcry_sexp_t key)
{
  gcry_sexp_t skey, pkey;

  pkey = gcry_sexp_find_token (key, "public-key", 0);
  if (!pkey)
    fail ("public part missing in return value\n");
  else
    {
      /* Fixme: Check more stuff.  */
      gcry_sexp_release (pkey);
    }

  skey = gcry_sexp_find_token (key, "private-key", 0);
  if (!skey)
    fail ("private part missing in return value\n");
  else
    {
      int rc = gcry_pk_testkey (skey);
      if (rc)
        fail ("gcry_pk_testkey failed: %s\n", gpg_strerror (rc));
      gcry_sexp_release (skey);
    }

  /* Finally check that gcry_pk_testkey also works on the entire
     S-expression.  */
  {
    int rc = gcry_pk_testkey (key);
    if (rc)
      fail ("gcry_pk_testkey failed on key pair: %s\n", gpg_strerror (rc));
  }
}


static void
check_ecc_keys (void)
{
  const char *curves[] = { "NIST P-521", "NIST P-384", "NIST P-256",
                           "Ed25519", NULL };
  int testno;
  gcry_sexp_t keyparm, key;
  int rc;

  for (testno=0; curves[testno]; testno++)
    {
      if (verbose)
        show ("creating ECC key using curve %s\n", curves[testno]);
      if (!strcmp (curves[testno], "Ed25519"))
        {
          /* Ed25519 isn't allowed in fips mode */
          if (in_fips_mode)
            continue;
          rc = gcry_sexp_build (&keyparm, NULL,
                                "(genkey(ecc(curve %s)(flags param eddsa)))",
                                curves[testno]);
        }
      else
        rc = gcry_sexp_build (&keyparm, NULL,
                              "(genkey(ecc(curve %s)(flags param)))",
                              curves[testno]);
      if (rc)
        die ("error creating S-expression: %s\n", gpg_strerror (rc));
      rc = gcry_pk_genkey (&key, keyparm);
      gcry_sexp_release (keyparm);
      if (rc)
        die ("error generating ECC key using curve %s: %s\n",
             curves[testno], gpg_strerror (rc));

      if (verbose > 1)
        show_sexp ("ECC key:\n", key);

      check_generated_ecc_key (key);

      gcry_sexp_release (key);
    }

  if (verbose)
    show ("creating ECC key using curve Ed25519 for ECDSA\n");
  rc = gcry_sexp_build (&keyparm, NULL, "(genkey(ecc(curve Ed25519)))");
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc && !in_fips_mode)
    die ("error generating ECC key using curve Ed25519 for ECDSA: %s\n",
         gpg_strerror (rc));
  else if (!rc && in_fips_mode)
    fail ("generating Ed25519 key must not work!");

  if (verbose && rc && in_fips_mode)
    show ("... correctly rejected key creation in FIPS mode (%s)\n",
          gpg_strerror (rc));

  if (!rc)
    {
      if (verbose > 1)
        show_sexp ("ECC key:\n", key);

      check_generated_ecc_key (key);
    }
  gcry_sexp_release (key);

  if (verbose)
    show ("creating ECC key using curve Ed25519 for ECDSA (nocomp)\n");
  rc = gcry_sexp_build (&keyparm, NULL,
                        "(genkey(ecc(curve Ed25519)(flags nocomp)))");
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc && !in_fips_mode)
    die ("error generating ECC key using curve Ed25519 for ECDSA"
         " (nocomp): %s\n",
         gpg_strerror (rc));
  else if (!rc && in_fips_mode)
    fail ("generating Ed25519 key must not work in FIPS mode!");

  if (verbose && rc && in_fips_mode)
    show ("... correctly rejected key creation in FIPS mode (%s)\n",
          gpg_strerror (rc));

  if (verbose)
    show ("creating ECC key using curve NIST P-384 for ECDSA\n");

  /* Must be specified as nistp384 (one word), because ecc_generate
   * uses _gcry_sexp_nth_string which takes the first word of the name
   * and thus libgcrypt can't find it later in its curves table.  */
  rc = gcry_sexp_build (&keyparm, NULL, "(genkey(ecc(curve nistp384)))");
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc)
    die ("error generating ECC key using curve NIST P-384 for ECDSA: %s\n",
         gpg_strerror (rc));

  if (verbose > 1)
    show_sexp ("ECC key:\n", key);

  check_generated_ecc_key (key);
  gcry_sexp_release (key);

  if (verbose)
    show ("creating ECC key using curve NIST P-384 for ECDSA (nocomp)\n");
  rc = gcry_sexp_build (&keyparm, NULL,
                        "(genkey(ecc(curve nistp384)(flags nocomp)))");
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc)
    die ("error generating ECC key using curve NIST P-384 for ECDSA"
         " (nocomp): %s\n",
         gpg_strerror (rc));

  if (verbose > 1)
    show_sexp ("ECC key:\n", key);

  check_generated_ecc_key (key);
  gcry_sexp_release (key);


  if (verbose)
    show ("creating ECC key using curve Ed25519 for ECDSA (transient-key)\n");
  rc = gcry_sexp_build (&keyparm, NULL,
                        "(genkey(ecc(curve Ed25519)(flags transient-key)))");
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc && !in_fips_mode)
    die ("error generating ECC key using curve Ed25519 for ECDSA"
         " (transient-key): %s\n",
         gpg_strerror (rc));
  else if (!rc && in_fips_mode)
    fail ("generating Ed25519 key must not work in FIPS mode!");

  if (verbose && rc && in_fips_mode)
    show ("... correctly rejected key creation in FIPS mode (%s)\n",
          gpg_strerror (rc));

  if (!rc)
    {
      if (verbose > 1)
        show_sexp ("ECC key:\n", key);
      check_generated_ecc_key (key);
    }
  gcry_sexp_release (key);

  if (verbose)
    show ("creating ECC key using curve Ed25519 for ECDSA "
          "(transient-key no-keytest)\n");
  rc = gcry_sexp_build (&keyparm, NULL,
                        "(genkey(ecc(curve Ed25519)"
                        "(flags transient-key no-keytest)))");
  if (rc)
    die ("error creating S-expression: %s\n", gpg_strerror (rc));
  rc = gcry_pk_genkey (&key, keyparm);
  gcry_sexp_release (keyparm);
  if (rc && !in_fips_mode)
    die ("error generating ECC key using curve Ed25519 for ECDSA"
         " (transient-key no-keytest): %s\n",
         gpg_strerror (rc));
  else if (!rc && in_fips_mode)
    fail ("generating Ed25519 key must not work in FIPS mode!");

  if (verbose && rc && in_fips_mode)
    show ("... correctly rejected key creation in FIPS mode (%s)\n",
          gpg_strerror (rc));

  if (!rc)
    {
      if (verbose > 1)
        show_sexp ("ECC key:\n", key);
      check_generated_ecc_key (key);
    }
  gcry_sexp_release (key);
}


static void
check_nonce (void)
{
  char a[32], b[32];
  int i,j;
  int oops=0;

  if (verbose)
    show ("checking gcry_create_nonce\n");

  gcry_create_nonce (a, sizeof a);
  for (i=0; i < 10; i++)
    {
      gcry_create_nonce (b, sizeof b);
      if (!memcmp (a, b, sizeof a))
        die ("identical nonce found\n");
    }
  for (i=0; i < 10; i++)
    {
      gcry_create_nonce (a, sizeof a);
      if (!memcmp (a, b, sizeof a))
        die ("identical nonce found\n");
    }

 again:
  for (i=1,j=0; i < sizeof a; i++)
    if (a[0] == a[i])
      j++;
  if (j+1 == sizeof (a))
    {
      if (oops)
        die ("impossible nonce found\n");
      oops++;
      gcry_create_nonce (a, sizeof a);
      goto again;
    }
}


static void
progress_cb (void *cb_data, const char *what, int printchar,
		  int current, int total)
{
  (void)cb_data;
  (void)what;
  (void)current;
  (void)total;

  if (printchar == '\n')
    fputs ( "<LF>", stdout);
  else
    putchar (printchar);
  fflush (stdout);
}


static void
usage (int mode)
{
  fputs ("usage: " PGM " [options] [{rsa|elg|dsa|ecc|nonce}]\n"
         "Options:\n"
         "  --verbose       be verbose\n"
         "  --debug         flyswatter\n"
         "  --fips          run in FIPS mode\n"
         "  --progress      print progress indicators\n",
         mode? stderr : stdout);
  if (mode)
    exit (1);
}

int
main (int argc, char **argv)
{
  int last_argc = -1;
  int opt_fips = 0;
  int with_progress = 0;

  if (argc)
    { argc--; argv++; }

  while (argc && last_argc != argc )
    {
      last_argc = argc;
      if (!strcmp (*argv, "--"))
        {
          argc--; argv++;
          break;
        }
      else if (!strcmp (*argv, "--help"))
        {
          usage (0);
          exit (0);
        }
      else if (!strcmp (*argv, "--verbose"))
        {
          verbose++;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--debug"))
        {
          verbose += 2;
          debug++;
          argc--; argv++;
        }
      else if (!strcmp (*argv, "--fips"))
        {
          argc--; argv++;
          opt_fips = 1;
        }
      else if (!strcmp (*argv, "--progress"))
        {
          argc--; argv++;
          with_progress = 1;
        }
      else if (!strncmp (*argv, "--", 2))
        die ("unknown option '%s'", *argv);
      else
        break;
    }

  gcry_control (GCRYCTL_SET_VERBOSITY, (int)verbose);
  if (opt_fips)
    gcry_control (GCRYCTL_FORCE_FIPS_MODE, 0);

  if (!gcry_check_version (GCRYPT_VERSION))
    die ("version mismatch\n");

  if (!opt_fips)
    gcry_control (GCRYCTL_DISABLE_SECMEM, 0);

  gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
  if (debug)
    gcry_control (GCRYCTL_SET_DEBUG_FLAGS, 1u , 0);
  /* No valuable keys are create, so we can speed up our RNG. */
  gcry_control (GCRYCTL_ENABLE_QUICK_RANDOM, 0);
  if (with_progress)
    gcry_set_progress_handler (progress_cb, NULL);

  if ( gcry_fips_mode_active () )
    in_fips_mode = 1;

  if (opt_fips && !in_fips_mode)
    die ("failed to switch into FIPS mode\n");

  if (!argc)
    {
      check_rsa_keys ();
      check_elg_keys ();
      check_dsa_keys ();
      check_ecc_keys ();
      check_nonce ();
    }
  else
    {
      for (; argc; argc--, argv++)
        if (!strcmp (*argv, "rsa"))
          check_rsa_keys ();
        else if (!strcmp (*argv, "elg"))
          check_elg_keys ();
        else if (!strcmp (*argv, "dsa"))
          check_dsa_keys ();
        else if (!strcmp (*argv, "ecc"))
          check_ecc_keys ();
        else if (!strcmp (*argv, "nonce"))
          check_nonce ();
        else
          usage (1);
    }

  return error_count? 1:0;
}