Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / examples / random-prime.c
blob33e10ab25cc607d882fd6490bda97a161dbe329b
1 /* random-prime.c
3 * Command line tool for prime generation.
5 */
7 /* nettle, low-level cryptographics library
9 * Copyright (C) 2010 Niels Möller
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.
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.
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.
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <time.h>
35 #include "bignum.h"
36 #include "yarrow.h"
38 #include "io.h"
40 #include "getopt.h"
42 static void
43 usage(void)
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");
52 int
53 main(int argc, char **argv)
55 long bits;
56 mpz_t p;
57 struct yarrow256_ctx yarrow;
59 int verbose = 0;
60 const char *random_file = NULL;
62 int c;
63 char *arg_end;
65 clock_t start;
66 clock_t end;
68 enum { OPT_HELP = 300 };
69 static const struct option options[] =
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}
78 while ( (c = getopt_long(argc, argv, "vr:", options, NULL)) != -1)
79 switch (c)
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();
96 argc -= optind;
97 argv += optind;
99 if (argc != 1)
101 usage();
102 return EXIT_FAILURE;
105 bits = strtol(argv[0], &arg_end, 0);
106 if (*arg_end || bits < 0)
108 fprintf(stderr, "Invalid number.\n");
109 return EXIT_FAILURE;
112 if (bits < 3)
114 fprintf(stderr, "Bitsize must be at least 3.\n");
115 return EXIT_FAILURE;
118 /* NOTE: No sources */
119 yarrow256_init(&yarrow, 0, NULL);
121 /* Read some data to seed the generator */
122 if (!simple_random(&yarrow, random_file))
124 werror("Initialization of randomness generator failed.\n");
125 return EXIT_FAILURE;
128 mpz_init(p);
130 start = clock();
132 nettle_random_prime(p, bits, 0,
133 &yarrow, (nettle_random_func *) yarrow256_random,
134 NULL, NULL);
136 end = clock();
138 mpz_out_str(stdout, 10, p);
139 printf("\n");
141 if (verbose)
142 fprintf(stderr, "time: %.3g s\n",
143 (double)(end - start) / CLOCKS_PER_SEC);
145 return EXIT_SUCCESS;