Initialize Tizen 2.3
[external/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., 59 Temple Place - Suite 330, Boston,
24  * MA 02111-1307, 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   static const struct option options[] =
69     {
70       /* Name, args, flag, val */
71       { "help", no_argument, NULL, '?' },
72       { "verbose", no_argument, NULL, 'v' },
73       { "random", required_argument, NULL, 'r' },
74       { NULL, 0, NULL, 0}
75     };
76
77   while ( (c = getopt_long(argc, argv, "v?r:", options, NULL)) != -1)
78     switch (c)
79       {
80       case 'v':
81         verbose = 1;
82         break;
83       case 'r':
84         random_file = optarg;
85         break;
86       case '?':
87         usage();
88         return EXIT_FAILURE;
89       default:
90         abort();
91       }
92
93   argc -= optind;
94   argv += optind;
95
96   if (argc != 1)
97     {
98       usage();
99       return EXIT_FAILURE;
100     }
101
102   bits = strtol(argv[0], &arg_end, 0);
103   if (*arg_end || bits < 0)
104     {
105       fprintf(stderr, "Invalid number.\n");
106       return EXIT_FAILURE;
107     }
108
109   if (bits < 3)
110     {
111       fprintf(stderr, "Bitsize must be at least 3.\n");
112       return EXIT_FAILURE;
113     }
114
115   /* NOTE: No sources */
116   yarrow256_init(&yarrow, 0, NULL);
117
118   /* Read some data to seed the generator */
119   if (!simple_random(&yarrow, random_file))
120     {
121       werror("Initialization of randomness generator failed.\n");
122       return EXIT_FAILURE;
123     }
124   
125   mpz_init(p);
126
127   start = clock();
128
129   nettle_random_prime(p, bits, 0,
130                       &yarrow, (nettle_random_func *) yarrow256_random,
131                       NULL, NULL);
132
133   end = clock();
134
135   mpz_out_str(stdout, 10, p);
136   printf("\n");
137
138   if (verbose)
139     fprintf(stderr, "time: %.3g s\n",
140             (double)(end - start) / CLOCKS_PER_SEC);
141
142   return EXIT_SUCCESS;
143 }