blob: e96878627d7499e093fdb8aabcdca34b76a28d93 [file] [log] [blame]
Paul Bakker15b9b3a2013-09-23 12:05:44 +02001/*
2 * Key generation application
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker15b9b3a2013-09-23 12:05:44 +02005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakker15b9b3a2013-09-23 12:05:44 +02007 *
Paul Bakker15b9b3a2013-09-23 12:05:44 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker15b9b3a2013-09-23 12:05:44 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker15b9b3a2013-09-23 12:05:44 +020038#include <string.h>
39#include <stdlib.h>
40#include <stdio.h>
41
Paul Bakker1cfc4582014-04-09 15:25:13 +020042#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
43#include <unistd.h>
44#endif /* !_WIN32 && POLARSSL_FS_IO */
45
Paul Bakker15b9b3a2013-09-23 12:05:44 +020046#include "polarssl/error.h"
47#include "polarssl/pk.h"
48#include "polarssl/ecdsa.h"
49#include "polarssl/rsa.h"
50#include "polarssl/error.h"
51#include "polarssl/entropy.h"
52#include "polarssl/ctr_drbg.h"
53
54#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) || \
55 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
56int main( int argc, char *argv[] )
57{
58 ((void) argc);
59 ((void) argv);
60
Rich Evansf90016a2015-01-19 14:26:37 +000061 polarssl_printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO and/or "
Paul Bakker15b9b3a2013-09-23 12:05:44 +020062 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
63 "not defined.\n" );
64 return( 0 );
65}
66#else
67
Paul Bakker15b9b3a2013-09-23 12:05:44 +020068#define FORMAT_PEM 0
69#define FORMAT_DER 1
70
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +010071#define DFL_TYPE POLARSSL_PK_RSA
Paul Bakker15b9b3a2013-09-23 12:05:44 +020072#define DFL_RSA_KEYSIZE 4096
73#define DFL_FILENAME "keyfile.key"
74#define DFL_FORMAT FORMAT_PEM
Paul Bakker1cfc4582014-04-09 15:25:13 +020075#define DFL_USE_DEV_RANDOM 0
Paul Bakker15b9b3a2013-09-23 12:05:44 +020076
Paul Bakkerd153ef32014-08-18 12:00:28 +020077#if defined(POLARSSL_ECP_C)
78#define DFL_EC_CURVE ecp_curve_list()->grp_id
79#else
80#define DFL_EC_CURVE 0
81#endif
82
Paul Bakker15b9b3a2013-09-23 12:05:44 +020083/*
84 * global options
85 */
86struct options
87{
88 int type; /* the type of key to generate */
89 int rsa_keysize; /* length of key in bits */
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +010090 int ec_curve; /* curve identifier for EC keys */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020091 const char *filename; /* filename of the key file */
92 int format; /* the output format to use */
Paul Bakker1cfc4582014-04-09 15:25:13 +020093 int use_dev_random; /* use /dev/random as entropy source */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020094} opt;
95
Paul Bakker1cfc4582014-04-09 15:25:13 +020096#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
97
98#define DEV_RANDOM_THRESHOLD 32
99
100int dev_random_entropy_poll( void *data, unsigned char *output,
101 size_t len, size_t *olen )
102{
103 FILE *file;
104 size_t ret, left = len;
105 unsigned char *p = output;
106 ((void) data);
107
108 *olen = 0;
109
110 file = fopen( "/dev/random", "rb" );
111 if( file == NULL )
112 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
113
114 while( left > 0 )
115 {
116 /* /dev/random can return much less than requested. If so, try again */
117 ret = fread( p, 1, left, file );
118 if( ret == 0 && ferror( file ) )
119 {
120 fclose( file );
121 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
122 }
123
124 p += ret;
125 left -= ret;
126 sleep( 1 );
127 }
128 fclose( file );
129 *olen = len;
130
131 return( 0 );
132}
133#endif /* !_WIN32 && POLARSSL_FS_IO */
134
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200135static int write_private_key( pk_context *key, const char *output_file )
136{
137 int ret;
138 FILE *f;
139 unsigned char output_buf[16000];
140 unsigned char *c = output_buf;
141 size_t len = 0;
142
143 memset(output_buf, 0, 16000);
144 if( opt.format == FORMAT_PEM )
145 {
146 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
147 return( ret );
148
149 len = strlen( (char *) output_buf );
150 }
151 else
152 {
153 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
154 return( ret );
155
156 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200157 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200158 }
159
Paul Bakker3966d712014-07-10 12:03:09 +0200160 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200161 return( -1 );
162
163 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200164 {
165 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200166 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200167 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200168
Paul Bakker0c226102014-04-17 16:02:36 +0200169 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200170
171 return( 0 );
172}
173
Paul Bakker1cfc4582014-04-09 15:25:13 +0200174#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
175#define USAGE_DEV_RANDOM \
176 " use_dev_random=0|1 default: 0\n"
177#else
178#define USAGE_DEV_RANDOM ""
179#endif /* !_WIN32 && POLARSSL_FS_IO */
180
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200181#define USAGE \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100182 "\n usage: gen_key param=<>...\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200183 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100184 " type=rsa|ec default: rsa\n" \
185 " rsa_keysize=%%d default: 4096\n" \
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100186 " ec_curve=%%s see below\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100187 " filename=%%s default: keyfile.key\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200188 " format=pem|der default: pem\n" \
Paul Bakker1cfc4582014-04-09 15:25:13 +0200189 USAGE_DEV_RANDOM \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200190 "\n"
191
192int main( int argc, char *argv[] )
193{
194 int ret = 0;
195 pk_context key;
196 char buf[1024];
197 int i;
198 char *p, *q;
199 entropy_context entropy;
200 ctr_drbg_context ctr_drbg;
201 const char *pers = "gen_key";
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100202#if defined(POLARSSL_ECP_C)
203 const ecp_curve_info *curve_info;
204#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200205
206 /*
207 * Set to sane values
208 */
209 pk_init( &key );
210 memset( buf, 0, sizeof( buf ) );
211
212 if( argc == 0 )
213 {
214 usage:
215 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000216 polarssl_printf( USAGE );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100217#if defined(POLARSSL_ECP_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000218 polarssl_printf( " availabled ec_curve values:\n" );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100219 curve_info = ecp_curve_list();
Rich Evansf90016a2015-01-19 14:26:37 +0000220 polarssl_printf( " %s (default)\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100221 while( ( ++curve_info )->name != NULL )
Rich Evansf90016a2015-01-19 14:26:37 +0000222 polarssl_printf( " %s\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100223#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200224 goto exit;
225 }
226
227 opt.type = DFL_TYPE;
228 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100229 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200230 opt.filename = DFL_FILENAME;
231 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200232 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200233
234 for( i = 1; i < argc; i++ )
235 {
236 p = argv[i];
237 if( ( q = strchr( p, '=' ) ) == NULL )
238 goto usage;
239 *q++ = '\0';
240
241 if( strcmp( p, "type" ) == 0 )
242 {
243 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100244 opt.type = POLARSSL_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100245 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100246 opt.type = POLARSSL_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200247 else
248 goto usage;
249 }
250 else if( strcmp( p, "format" ) == 0 )
251 {
252 if( strcmp( q, "pem" ) == 0 )
253 opt.format = FORMAT_PEM;
254 else if( strcmp( q, "der" ) == 0 )
255 opt.format = FORMAT_DER;
256 else
257 goto usage;
258 }
259 else if( strcmp( p, "rsa_keysize" ) == 0 )
260 {
261 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200262 if( opt.rsa_keysize < 1024 ||
263 opt.rsa_keysize > POLARSSL_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200264 goto usage;
265 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200266#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100267 else if( strcmp( p, "ec_curve" ) == 0 )
268 {
269 if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL )
270 goto usage;
271 opt.ec_curve = curve_info->grp_id;
272 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200273#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200274 else if( strcmp( p, "filename" ) == 0 )
275 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200276 else if( strcmp( p, "use_dev_random" ) == 0 )
277 {
278 opt.use_dev_random = atoi( q );
279 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
280 goto usage;
281 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200282 else
283 goto usage;
284 }
285
Rich Evansf90016a2015-01-19 14:26:37 +0000286 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200287 fflush( stdout );
288
289 entropy_init( &entropy );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200290#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
291 if( opt.use_dev_random )
292 {
293 if( ( ret = entropy_add_source( &entropy, dev_random_entropy_poll,
294 NULL, DEV_RANDOM_THRESHOLD ) ) != 0 )
295 {
Rich Evansf90016a2015-01-19 14:26:37 +0000296 polarssl_printf( " failed\n ! entropy_add_source returned -0x%04x\n", -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200297 goto exit;
298 }
299
Rich Evansf90016a2015-01-19 14:26:37 +0000300 polarssl_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200301 fflush( stdout );
302 }
303#endif /* !_WIN32 && POLARSSL_FS_IO */
304
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200305 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
306 (const unsigned char *) pers,
307 strlen( pers ) ) ) != 0 )
308 {
Rich Evansf90016a2015-01-19 14:26:37 +0000309 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200310 goto exit;
311 }
312
313 /*
314 * 1.1. Generate the key
315 */
Rich Evansf90016a2015-01-19 14:26:37 +0000316 polarssl_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200317 fflush( stdout );
318
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100319 if( ( ret = pk_init_ctx( &key, pk_info_from_type( opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200320 {
Rich Evansf90016a2015-01-19 14:26:37 +0000321 polarssl_printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100322 goto exit;
323 }
324
325#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
326 if( opt.type == POLARSSL_PK_RSA )
327 {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200328 ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg,
329 opt.rsa_keysize, 65537 );
330 if( ret != 0 )
331 {
Rich Evansf90016a2015-01-19 14:26:37 +0000332 polarssl_printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200333 goto exit;
334 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200335 }
336 else
337#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100338#if defined(POLARSSL_ECP_C)
339 if( opt.type == POLARSSL_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200340 {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100341 ret = ecp_gen_key( opt.ec_curve, pk_ec( key ),
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100342 ctr_drbg_random, &ctr_drbg );
343 if( ret != 0 )
344 {
Rich Evansf90016a2015-01-19 14:26:37 +0000345 polarssl_printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100346 goto exit;
347 }
348 }
349 else
350#endif /* POLARSSL_ECP_C */
351 {
Rich Evansf90016a2015-01-19 14:26:37 +0000352 polarssl_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200353 goto exit;
354 }
355
356 /*
357 * 1.2 Print the key
358 */
Rich Evansf90016a2015-01-19 14:26:37 +0000359 polarssl_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200360
361#if defined(POLARSSL_RSA_C)
362 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
363 {
364 rsa_context *rsa = pk_rsa( key );
365 mpi_write_file( "N: ", &rsa->N, 16, NULL );
366 mpi_write_file( "E: ", &rsa->E, 16, NULL );
367 mpi_write_file( "D: ", &rsa->D, 16, NULL );
368 mpi_write_file( "P: ", &rsa->P, 16, NULL );
369 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
370 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
371 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
372 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
373 }
374 else
375#endif
376#if defined(POLARSSL_ECP_C)
377 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
378 {
379 ecp_keypair *ecp = pk_ec( key );
Rich Evansf90016a2015-01-19 14:26:37 +0000380 polarssl_printf( "curve: %s\n",
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100381 ecp_curve_info_from_grp_id( ecp->grp.id )->name );
382 mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
383 mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
384 mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200385 }
386 else
387#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000388 polarssl_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200389
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200390 /*
391 * 1.3 Export key
392 */
Rich Evansf90016a2015-01-19 14:26:37 +0000393 polarssl_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200394
395 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
396 {
Rich Evansf90016a2015-01-19 14:26:37 +0000397 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200398 goto exit;
399 }
400
Rich Evansf90016a2015-01-19 14:26:37 +0000401 polarssl_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200402
403exit:
404
405 if( ret != 0 && ret != 1)
406 {
407#ifdef POLARSSL_ERROR_C
408 polarssl_strerror( ret, buf, sizeof( buf ) );
Rich Evansf90016a2015-01-19 14:26:37 +0000409 polarssl_printf( " - %s\n", buf );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200410#else
Rich Evansf90016a2015-01-19 14:26:37 +0000411 polarssl_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200412#endif
413 }
414
415 pk_free( &key );
Paul Bakkera317a982014-06-18 16:44:11 +0200416 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200417 entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200418
419#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000420 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200421 fflush( stdout ); getchar();
422#endif
423
424 return( ret );
425}
426#endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */