Tizen 2.0 Release
[external/nettle.git] / examples / rsa-keygen.c
1 /* rsa-keygen.c
2  *
3  */
4
5 /* nettle, low-level cryptographics library
6  *
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.
13  * 
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.
18  * 
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., 59 Temple Place - Suite 330, Boston,
22  * MA 02111-1307, USA.
23  */
24
25 #if HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include "buffer.h"
36 #include "rsa.h"
37 #include "sexp.h"
38 #include "yarrow.h"
39
40 #include "io.h"
41
42 #include "getopt.h"
43
44 #define KEYSIZE 900
45 #define ESIZE 30
46
47 static void
48 progress(void *ctx, int c)
49 {
50   (void) ctx;
51   fputc(c, stderr);
52 }
53
54
55 int
56 main(int argc, char **argv)
57 {
58   struct yarrow256_ctx yarrow;
59   struct rsa_public_key pub;
60   struct rsa_private_key priv;
61
62   int c;
63   char *pub_name = NULL;
64   const char *priv_name = NULL;
65   const char *random_name = NULL;
66   
67   struct nettle_buffer pub_buffer;
68   struct nettle_buffer priv_buffer;
69
70   while ( (c = getopt(argc, argv, "o:r:")) != -1)
71     switch (c)
72       {
73       case 'o':
74         priv_name = optarg;
75         break;
76
77       case 'r':
78         random_name = optarg;
79         break;
80         
81       case '?':
82         if (isprint (optopt))
83           werror("Unknown option `-%c'.\n", optopt);
84         else
85           werror("Unknown option character `\\x%x'.\n",
86                   optopt);
87         return EXIT_FAILURE;
88       default:
89         abort();
90       }
91
92   if (!priv_name)
93     {
94       werror("No filename provided.\n");
95       return EXIT_FAILURE;
96     }
97
98   pub_name = xalloc(strlen(priv_name) + 5);  
99   sprintf(pub_name, "%s.pub", priv_name);
100
101   /* NOTE: No sources */
102   yarrow256_init(&yarrow, 0, NULL);
103
104   /* Read some data to seed the generator */
105   if (!simple_random(&yarrow, random_name))
106     {
107       werror("Initialization of randomness generator failed.\n");
108       return EXIT_FAILURE;
109     }
110
111   rsa_public_key_init(&pub);
112   rsa_private_key_init(&priv);
113
114   if (!rsa_generate_keypair
115       (&pub, &priv,
116        (void *) &yarrow, (nettle_random_func *) yarrow256_random,
117        NULL, progress,
118        KEYSIZE, ESIZE))
119     {
120       werror("Key generation failed.\n");
121       return EXIT_FAILURE;
122     }
123
124   nettle_buffer_init(&priv_buffer);
125   nettle_buffer_init(&pub_buffer);
126   
127   if (!rsa_keypair_to_sexp(&pub_buffer, "rsa-pkcs1-sha1", &pub, NULL))
128     {
129       werror("Formatting public key failed.\n");
130       return EXIT_FAILURE;
131     }
132
133   if (!rsa_keypair_to_sexp(&priv_buffer, "rsa-pkcs1-sha1", &pub, &priv))
134     {
135       werror("Formatting private key failed.\n");
136       return EXIT_FAILURE;
137     }
138   
139   if (!write_file(pub_name, pub_buffer.size, pub_buffer.contents))
140     {
141       werror("Failed to write public key: %s\n",
142               strerror(errno));
143       return EXIT_FAILURE;
144     }
145
146   /* NOTE: This doesn't set up paranoid access restrictions on the
147    * private key file, like a serious key generation tool would do. */
148   if (!write_file(priv_name, priv_buffer.size, priv_buffer.contents))
149     {
150       werror("Failed to write private key: %s\n",
151               strerror(errno));
152       return EXIT_FAILURE;
153     }
154
155   return EXIT_SUCCESS;
156 }