Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / examples / rsa-keygen.c
blobb46239e251667b75581e72ef51941c68cb5c0ad2
1 /* rsa-keygen.c
3 */
5 /* nettle, low-level cryptographics library
7 * Copyright (C) 2002 Niels Möller
8 *
9 * The nettle library is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or (at your
12 * option) any later version.
14 * The nettle library is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17 * License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with the nettle library; see the file COPYING.LIB. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 * MA 02111-1301, USA.
25 #if HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
35 #include "buffer.h"
36 #include "rsa.h"
37 #include "sexp.h"
38 #include "yarrow.h"
40 #include "io.h"
42 #include "getopt.h"
44 #define DEFAULT_KEYSIZE 2048
45 #define ESIZE 30
47 static void
48 progress(void *ctx, int c)
50 (void) ctx;
51 fputc(c, stderr);
54 static unsigned long
55 uint_arg (char c, const char *arg)
57 unsigned long val;
58 char *end;
60 val = strtoul(arg, &end, 0);
61 if (*arg == '\0' || *end != '\0')
63 werror ("Invalid integer argument for -%c option.\n", c);
64 exit (EXIT_FAILURE);
67 return val;
70 int
71 main(int argc, char **argv)
73 struct yarrow256_ctx yarrow;
74 struct rsa_public_key pub;
75 struct rsa_private_key priv;
77 int c;
78 char *pub_name = NULL;
79 const char *priv_name = NULL;
80 const char *random_name = NULL;
82 struct nettle_buffer pub_buffer;
83 struct nettle_buffer priv_buffer;
85 unsigned long key_size = DEFAULT_KEYSIZE;
86 unsigned long key_e = 0;
88 enum { OPT_HELP = 300 };
89 static const struct option options[] =
91 /* Name, args, flag, val */
92 { "help", no_argument, NULL, OPT_HELP },
93 { "random", required_argument, NULL, 'r' },
94 { NULL, 0, NULL, 0}
97 while ( (c = getopt_long(argc, argv, "o:r:e:s:", options, NULL)) != -1)
98 switch (c)
100 case 'o':
101 priv_name = optarg;
102 break;
104 case 'r':
105 random_name = optarg;
106 break;
108 case 's':
109 key_size = uint_arg ('s', optarg);
110 break;
112 case 'e':
113 key_e = uint_arg ('e', optarg);
114 break;
116 case OPT_HELP:
117 printf("FIXME: Usage information.\n");
118 return EXIT_SUCCESS;
120 case '?':
121 return EXIT_FAILURE;
123 default:
124 abort();
127 if (!priv_name)
129 werror("No filename provided.\n");
130 return EXIT_FAILURE;
133 pub_name = xalloc(strlen(priv_name) + 5);
134 sprintf(pub_name, "%s.pub", priv_name);
136 /* NOTE: No sources */
137 yarrow256_init(&yarrow, 0, NULL);
139 /* Read some data to seed the generator */
140 if (!simple_random(&yarrow, random_name))
142 werror("Initialization of randomness generator failed.\n");
143 return EXIT_FAILURE;
146 rsa_public_key_init(&pub);
147 rsa_private_key_init(&priv);
149 if (key_e)
150 mpz_set_ui (pub.e, key_e);
152 if (!rsa_generate_keypair
153 (&pub, &priv,
154 (void *) &yarrow, (nettle_random_func *) yarrow256_random,
155 NULL, progress,
156 key_size, key_e == 0 ? ESIZE : 0))
158 werror("Key generation failed.\n");
159 return EXIT_FAILURE;
162 nettle_buffer_init(&priv_buffer);
163 nettle_buffer_init(&pub_buffer);
165 if (!rsa_keypair_to_sexp(&pub_buffer, "rsa-pkcs1-sha1", &pub, NULL))
167 werror("Formatting public key failed.\n");
168 return EXIT_FAILURE;
171 if (!rsa_keypair_to_sexp(&priv_buffer, "rsa-pkcs1-sha1", &pub, &priv))
173 werror("Formatting private key failed.\n");
174 return EXIT_FAILURE;
177 if (!write_file(pub_name, pub_buffer.size, pub_buffer.contents))
179 werror("Failed to write public key: %s\n",
180 strerror(errno));
181 return EXIT_FAILURE;
184 /* NOTE: This doesn't set up paranoid access restrictions on the
185 * private key file, like a serious key generation tool would do. */
186 if (!write_file(priv_name, priv_buffer.size, priv_buffer.contents))
188 werror("Failed to write private key: %s\n",
189 strerror(errno));
190 return EXIT_FAILURE;
193 nettle_buffer_clear(&priv_buffer);
194 nettle_buffer_clear(&pub_buffer);
195 rsa_public_key_clear(&pub);
196 rsa_private_key_clear(&priv);
197 free (pub_name);
199 return EXIT_SUCCESS;