blob: 3cf682b8aff698d2082fe3fbe87d152f411142a9 [file] [log] [blame]
Paul Bakker15b9b3a2013-09-23 12:05:44 +02001/*
2 * Key generation application
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include "polarssl/config.h"
27
28#include <string.h>
29#include <stdlib.h>
30#include <stdio.h>
31
32#include "polarssl/error.h"
33#include "polarssl/pk.h"
34#include "polarssl/ecdsa.h"
35#include "polarssl/rsa.h"
36#include "polarssl/error.h"
37#include "polarssl/entropy.h"
38#include "polarssl/ctr_drbg.h"
39
40#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) || \
41 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
42int main( int argc, char *argv[] )
43{
44 ((void) argc);
45 ((void) argv);
46
47 printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO and/or "
48 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
49 "not defined.\n" );
50 return( 0 );
51}
52#else
53
54#define TYPE_RSA 0
55
56#define FORMAT_PEM 0
57#define FORMAT_DER 1
58
59#define DFL_TYPE TYPE_RSA
60#define DFL_RSA_KEYSIZE 4096
61#define DFL_FILENAME "keyfile.key"
62#define DFL_FORMAT FORMAT_PEM
63
64/*
65 * global options
66 */
67struct options
68{
69 int type; /* the type of key to generate */
70 int rsa_keysize; /* length of key in bits */
71 const char *filename; /* filename of the key file */
72 int format; /* the output format to use */
73} opt;
74
75static int write_private_key( pk_context *key, const char *output_file )
76{
77 int ret;
78 FILE *f;
79 unsigned char output_buf[16000];
80 unsigned char *c = output_buf;
81 size_t len = 0;
82
83 memset(output_buf, 0, 16000);
84 if( opt.format == FORMAT_PEM )
85 {
86 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
87 return( ret );
88
89 len = strlen( (char *) output_buf );
90 }
91 else
92 {
93 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
94 return( ret );
95
96 len = ret;
97 c = output_buf + sizeof(output_buf) - len - 1;
98 }
99
100 if( ( f = fopen( output_file, "w" ) ) == NULL )
101 return( -1 );
102
103 if( fwrite( c, 1, len, f ) != len )
104 return( -1 );
105
106 fclose(f);
107
108 return( 0 );
109}
110
111#define USAGE \
112 "\n usage: gen_key param=<>...\n" \
113 "\n acceptable parameters:\n" \
114 " type=rsa default: rsa\n" \
115 " rsa_keysize=%%d default: 4096\n" \
116 " filename=%%s default: keyfile.key\n" \
117 " format=pem|der default: pem\n" \
118 "\n"
119
120int main( int argc, char *argv[] )
121{
122 int ret = 0;
123 pk_context key;
124 char buf[1024];
125 int i;
126 char *p, *q;
127 entropy_context entropy;
128 ctr_drbg_context ctr_drbg;
129 const char *pers = "gen_key";
130
131 /*
132 * Set to sane values
133 */
134 pk_init( &key );
135 memset( buf, 0, sizeof( buf ) );
136
137 if( argc == 0 )
138 {
139 usage:
140 ret = 1;
141 printf( USAGE );
142 goto exit;
143 }
144
145 opt.type = DFL_TYPE;
146 opt.rsa_keysize = DFL_RSA_KEYSIZE;
147 opt.filename = DFL_FILENAME;
148 opt.format = DFL_FORMAT;
149
150 for( i = 1; i < argc; i++ )
151 {
152 p = argv[i];
153 if( ( q = strchr( p, '=' ) ) == NULL )
154 goto usage;
155 *q++ = '\0';
156
157 if( strcmp( p, "type" ) == 0 )
158 {
159 if( strcmp( q, "rsa" ) == 0 )
160 opt.type = TYPE_RSA;
161 else
162 goto usage;
163 }
164 else if( strcmp( p, "format" ) == 0 )
165 {
166 if( strcmp( q, "pem" ) == 0 )
167 opt.format = FORMAT_PEM;
168 else if( strcmp( q, "der" ) == 0 )
169 opt.format = FORMAT_DER;
170 else
171 goto usage;
172 }
173 else if( strcmp( p, "rsa_keysize" ) == 0 )
174 {
175 opt.rsa_keysize = atoi( q );
176 if( opt.rsa_keysize < 1024 || opt.rsa_keysize > 8192 )
177 goto usage;
178 }
179 else if( strcmp( p, "filename" ) == 0 )
180 opt.filename = q;
181 else
182 goto usage;
183 }
184
185 printf( "\n . Seeding the random number generator..." );
186 fflush( stdout );
187
188 entropy_init( &entropy );
189 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
190 (const unsigned char *) pers,
191 strlen( pers ) ) ) != 0 )
192 {
193 printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
194 goto exit;
195 }
196
197 /*
198 * 1.1. Generate the key
199 */
200 printf( "\n . Generating the private key ..." );
201 fflush( stdout );
202
203#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
204 if( opt.type == TYPE_RSA )
205 {
206 pk_init_ctx( &key, pk_info_from_type( POLARSSL_PK_RSA ) );
207 ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg,
208 opt.rsa_keysize, 65537 );
209 if( ret != 0 )
210 {
211 printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
212 goto exit;
213 }
214
215 printf( " ok\n" );
216 }
217 else
218#endif /* POLARSSL_RSA_C */
219 {
220 printf( " failed\n ! key type not supported in library" );
221 goto exit;
222 }
223
224 /*
225 * 1.2 Print the key
226 */
227 printf( " . Key information ...\n" );
228
229#if defined(POLARSSL_RSA_C)
230 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
231 {
232 rsa_context *rsa = pk_rsa( key );
233 mpi_write_file( "N: ", &rsa->N, 16, NULL );
234 mpi_write_file( "E: ", &rsa->E, 16, NULL );
235 mpi_write_file( "D: ", &rsa->D, 16, NULL );
236 mpi_write_file( "P: ", &rsa->P, 16, NULL );
237 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
238 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
239 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
240 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
241 }
242 else
243#endif
244#if defined(POLARSSL_ECP_C)
245 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
246 {
247 ecp_keypair *ecp = pk_ec( key );
248 mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL );
249 mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL );
250 mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL );
251 mpi_write_file( "D : ", &ecp->d , 16, NULL );
252 }
253 else
254#endif
255 printf("key type not supported yet\n");
256
257 write_private_key( &key, opt.filename );
258
259exit:
260
261 if( ret != 0 && ret != 1)
262 {
263#ifdef POLARSSL_ERROR_C
264 polarssl_strerror( ret, buf, sizeof( buf ) );
265 printf( " - %s\n", buf );
266#else
267 printf("\n");
268#endif
269 }
270
271 pk_free( &key );
272
273#if defined(_WIN32)
274 printf( " + Press Enter to exit this program.\n" );
275 fflush( stdout ); getchar();
276#endif
277
278 return( ret );
279}
280#endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */