Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / examples / random-prime.c
1 /* random-prime.c
2  *
3  * Command line tool for prime generation.
4  *
5  */
6  
7 /* nettle, low-level cryptographics library
8  *
9  * Copyright (C) 2010 Niels Möller
10  *  
11  * The nettle library is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or (at your
14  * option) any later version.
15  * 
16  * The nettle library is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
19  * License for more details.
20  * 
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with the nettle library; see the file COPYING.LIB.  If not, write to
23  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
24  * MA 02111-1301, USA.
25  */
26
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <time.h>
34
35 #include "bignum.h"
36 #include "yarrow.h"
37
38 #include "io.h"
39
40 #include "getopt.h"
41
42 static void
43 usage(void)
44 {
45   fprintf(stderr, "Usage: random-prime [OPTIONS] bits\n\n"
46           "Options:\n"
47           "      --help         Display this message.\n"
48           "  -v, --verbose      Display timing information.\n"
49           "  -r, --random FILE  Random data to use for seeding.\n");
50 }
51
52 int
53 main(int argc, char **argv)
54 {
55   long bits;
56   mpz_t p;
57   struct yarrow256_ctx yarrow;
58
59   int verbose = 0;  
60   const char *random_file = NULL;
61
62   int c;
63   char *arg_end;
64
65   clock_t start;
66   clock_t end;
67   
68   enum { OPT_HELP = 300 };
69   static const struct option options[] =
70     {
71       /* Name, args, flag, val */
72       { "help", no_argument, NULL, OPT_HELP },
73       { "verbose", no_argument, NULL, 'v' },
74       { "random", required_argument, NULL, 'r' },
75       { NULL, 0, NULL, 0}
76     };
77
78   while ( (c = getopt_long(argc, argv, "vr:", options, NULL)) != -1)
79     switch (c)
80       {
81       case 'v':
82         verbose = 1;
83         break;
84       case 'r':
85         random_file = optarg;
86         break;
87       case OPT_HELP:
88         usage();
89         return EXIT_SUCCESS;
90       case '?':
91         return EXIT_FAILURE;
92       default:
93         abort();
94       }
95
96   argc -= optind;
97   argv += optind;
98
99   if (argc != 1)
100     {
101       usage();
102       return EXIT_FAILURE;
103     }
104
105   bits = strtol(argv[0], &arg_end, 0);
106   if (*arg_end || bits < 0)
107     {
108       fprintf(stderr, "Invalid number.\n");
109       return EXIT_FAILURE;
110     }
111
112   if (bits < 3)
113     {
114       fprintf(stderr, "Bitsize must be at least 3.\n");
115       return EXIT_FAILURE;
116     }
117
118   /* NOTE: No sources */
119   yarrow256_init(&yarrow, 0, NULL);
120
121   /* Read some data to seed the generator */
122   if (!simple_random(&yarrow, random_file))
123     {
124       werror("Initialization of randomness generator failed.\n");
125       return EXIT_FAILURE;
126     }
127   
128   mpz_init(p);
129
130   start = clock();
131
132   nettle_random_prime(p, bits, 0,
133                       &yarrow, (nettle_random_func *) yarrow256_random,
134                       NULL, NULL);
135
136   end = clock();
137
138   mpz_out_str(stdout, 10, p);
139   printf("\n");
140
141   if (verbose)
142     fprintf(stderr, "time: %.3g s\n",
143             (double)(end - start) / CLOCKS_PER_SEC);
144
145   return EXIT_SUCCESS;
146 }