blob: 43ae6e16d757002f1f78db5926352e53ce2f2812 [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
Paul Bakker1cfc4582014-04-09 15:25:13 +020032#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
33#include <unistd.h>
34#endif /* !_WIN32 && POLARSSL_FS_IO */
35
Paul Bakker15b9b3a2013-09-23 12:05:44 +020036#include "polarssl/error.h"
37#include "polarssl/pk.h"
38#include "polarssl/ecdsa.h"
39#include "polarssl/rsa.h"
40#include "polarssl/error.h"
41#include "polarssl/entropy.h"
42#include "polarssl/ctr_drbg.h"
43
44#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) || \
45 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
46int main( int argc, char *argv[] )
47{
48 ((void) argc);
49 ((void) argv);
50
51 printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO and/or "
52 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
53 "not defined.\n" );
54 return( 0 );
55}
56#else
57
Paul Bakker15b9b3a2013-09-23 12:05:44 +020058#define FORMAT_PEM 0
59#define FORMAT_DER 1
60
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +010061#define DFL_TYPE POLARSSL_PK_RSA
Paul Bakker15b9b3a2013-09-23 12:05:44 +020062#define DFL_RSA_KEYSIZE 4096
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +010063#define DFL_EC_CURVE ecp_curve_list()->grp_id
Paul Bakker15b9b3a2013-09-23 12:05:44 +020064#define DFL_FILENAME "keyfile.key"
65#define DFL_FORMAT FORMAT_PEM
Paul Bakker1cfc4582014-04-09 15:25:13 +020066#define DFL_USE_DEV_RANDOM 0
Paul Bakker15b9b3a2013-09-23 12:05:44 +020067
68/*
69 * global options
70 */
71struct options
72{
73 int type; /* the type of key to generate */
74 int rsa_keysize; /* length of key in bits */
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +010075 int ec_curve; /* curve identifier for EC keys */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020076 const char *filename; /* filename of the key file */
77 int format; /* the output format to use */
Paul Bakker1cfc4582014-04-09 15:25:13 +020078 int use_dev_random; /* use /dev/random as entropy source */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020079} opt;
80
Paul Bakker1cfc4582014-04-09 15:25:13 +020081#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
82
83#define DEV_RANDOM_THRESHOLD 32
84
85int dev_random_entropy_poll( void *data, unsigned char *output,
86 size_t len, size_t *olen )
87{
88 FILE *file;
89 size_t ret, left = len;
90 unsigned char *p = output;
91 ((void) data);
92
93 *olen = 0;
94
95 file = fopen( "/dev/random", "rb" );
96 if( file == NULL )
97 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
98
99 while( left > 0 )
100 {
101 /* /dev/random can return much less than requested. If so, try again */
102 ret = fread( p, 1, left, file );
103 if( ret == 0 && ferror( file ) )
104 {
105 fclose( file );
106 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
107 }
108
109 p += ret;
110 left -= ret;
111 sleep( 1 );
112 }
113 fclose( file );
114 *olen = len;
115
116 return( 0 );
117}
118#endif /* !_WIN32 && POLARSSL_FS_IO */
119
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200120static int write_private_key( pk_context *key, const char *output_file )
121{
122 int ret;
123 FILE *f;
124 unsigned char output_buf[16000];
125 unsigned char *c = output_buf;
126 size_t len = 0;
127
128 memset(output_buf, 0, 16000);
129 if( opt.format == FORMAT_PEM )
130 {
131 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
132 return( ret );
133
134 len = strlen( (char *) output_buf );
135 }
136 else
137 {
138 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
139 return( ret );
140
141 len = ret;
142 c = output_buf + sizeof(output_buf) - len - 1;
143 }
144
145 if( ( f = fopen( output_file, "w" ) ) == NULL )
146 return( -1 );
147
148 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200149 {
150 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200151 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200152 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200153
Paul Bakker0c226102014-04-17 16:02:36 +0200154 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200155
156 return( 0 );
157}
158
Paul Bakker1cfc4582014-04-09 15:25:13 +0200159#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
160#define USAGE_DEV_RANDOM \
161 " use_dev_random=0|1 default: 0\n"
162#else
163#define USAGE_DEV_RANDOM ""
164#endif /* !_WIN32 && POLARSSL_FS_IO */
165
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200166#define USAGE \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100167 "\n usage: gen_key param=<>...\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200168 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100169 " type=rsa|ec default: rsa\n" \
170 " rsa_keysize=%%d default: 4096\n" \
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100171 " ec_curve=%%s see below\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100172 " filename=%%s default: keyfile.key\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200173 " format=pem|der default: pem\n" \
Paul Bakker1cfc4582014-04-09 15:25:13 +0200174 USAGE_DEV_RANDOM \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200175 "\n"
176
177int main( int argc, char *argv[] )
178{
179 int ret = 0;
180 pk_context key;
181 char buf[1024];
182 int i;
183 char *p, *q;
184 entropy_context entropy;
185 ctr_drbg_context ctr_drbg;
186 const char *pers = "gen_key";
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100187#if defined(POLARSSL_ECP_C)
188 const ecp_curve_info *curve_info;
189#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200190
191 /*
192 * Set to sane values
193 */
194 pk_init( &key );
195 memset( buf, 0, sizeof( buf ) );
196
197 if( argc == 0 )
198 {
199 usage:
200 ret = 1;
201 printf( USAGE );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100202#if defined(POLARSSL_ECP_C)
203 printf( " availabled ec_curve values:\n" );
204 curve_info = ecp_curve_list();
205 printf( " %s (default)\n", curve_info->name );
206 while( ( ++curve_info )->name != NULL )
207 printf( " %s\n", curve_info->name );
208#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200209 goto exit;
210 }
211
212 opt.type = DFL_TYPE;
213 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100214 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200215 opt.filename = DFL_FILENAME;
216 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200217 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200218
219 for( i = 1; i < argc; i++ )
220 {
221 p = argv[i];
222 if( ( q = strchr( p, '=' ) ) == NULL )
223 goto usage;
224 *q++ = '\0';
225
226 if( strcmp( p, "type" ) == 0 )
227 {
228 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100229 opt.type = POLARSSL_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100230 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100231 opt.type = POLARSSL_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200232 else
233 goto usage;
234 }
235 else if( strcmp( p, "format" ) == 0 )
236 {
237 if( strcmp( q, "pem" ) == 0 )
238 opt.format = FORMAT_PEM;
239 else if( strcmp( q, "der" ) == 0 )
240 opt.format = FORMAT_DER;
241 else
242 goto usage;
243 }
244 else if( strcmp( p, "rsa_keysize" ) == 0 )
245 {
246 opt.rsa_keysize = atoi( q );
247 if( opt.rsa_keysize < 1024 || opt.rsa_keysize > 8192 )
248 goto usage;
249 }
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100250 else if( strcmp( p, "ec_curve" ) == 0 )
251 {
252 if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL )
253 goto usage;
254 opt.ec_curve = curve_info->grp_id;
255 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200256 else if( strcmp( p, "filename" ) == 0 )
257 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200258 else if( strcmp( p, "use_dev_random" ) == 0 )
259 {
260 opt.use_dev_random = atoi( q );
261 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
262 goto usage;
263 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200264 else
265 goto usage;
266 }
267
268 printf( "\n . Seeding the random number generator..." );
269 fflush( stdout );
270
271 entropy_init( &entropy );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200272#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
273 if( opt.use_dev_random )
274 {
275 if( ( ret = entropy_add_source( &entropy, dev_random_entropy_poll,
276 NULL, DEV_RANDOM_THRESHOLD ) ) != 0 )
277 {
278 printf( " failed\n ! entropy_add_source returned -0x%04x\n", -ret );
279 goto exit;
280 }
281
282 printf("\n Using /dev/random, so can take a long time! " );
283 fflush( stdout );
284 }
285#endif /* !_WIN32 && POLARSSL_FS_IO */
286
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200287 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
288 (const unsigned char *) pers,
289 strlen( pers ) ) ) != 0 )
290 {
291 printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
292 goto exit;
293 }
294
295 /*
296 * 1.1. Generate the key
297 */
298 printf( "\n . Generating the private key ..." );
299 fflush( stdout );
300
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100301 if( ( ret = pk_init_ctx( &key, pk_info_from_type( opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200302 {
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100303 printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret );
304 goto exit;
305 }
306
307#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
308 if( opt.type == POLARSSL_PK_RSA )
309 {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200310 ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg,
311 opt.rsa_keysize, 65537 );
312 if( ret != 0 )
313 {
314 printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
315 goto exit;
316 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200317 }
318 else
319#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100320#if defined(POLARSSL_ECP_C)
321 if( opt.type == POLARSSL_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200322 {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100323 ret = ecp_gen_key( opt.ec_curve, pk_ec( key ),
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100324 ctr_drbg_random, &ctr_drbg );
325 if( ret != 0 )
326 {
327 printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
328 goto exit;
329 }
330 }
331 else
332#endif /* POLARSSL_ECP_C */
333 {
334 printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200335 goto exit;
336 }
337
338 /*
339 * 1.2 Print the key
340 */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100341 printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200342
343#if defined(POLARSSL_RSA_C)
344 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
345 {
346 rsa_context *rsa = pk_rsa( key );
347 mpi_write_file( "N: ", &rsa->N, 16, NULL );
348 mpi_write_file( "E: ", &rsa->E, 16, NULL );
349 mpi_write_file( "D: ", &rsa->D, 16, NULL );
350 mpi_write_file( "P: ", &rsa->P, 16, NULL );
351 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
352 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
353 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
354 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
355 }
356 else
357#endif
358#if defined(POLARSSL_ECP_C)
359 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
360 {
361 ecp_keypair *ecp = pk_ec( key );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100362 printf( "curve: %s\n",
363 ecp_curve_info_from_grp_id( ecp->grp.id )->name );
364 mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
365 mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
366 mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200367 }
368 else
369#endif
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100370 printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200371
372 write_private_key( &key, opt.filename );
373
374exit:
375
376 if( ret != 0 && ret != 1)
377 {
378#ifdef POLARSSL_ERROR_C
379 polarssl_strerror( ret, buf, sizeof( buf ) );
380 printf( " - %s\n", buf );
381#else
382 printf("\n");
383#endif
384 }
385
386 pk_free( &key );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200387 entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200388
389#if defined(_WIN32)
390 printf( " + Press Enter to exit this program.\n" );
391 fflush( stdout ); getchar();
392#endif
393
394 return( ret );
395}
396#endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */