blob: 2d981abc58a8497866d31c2eb336c3e398f205c1 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Paul Bakker15b9b3a2013-09-23 12:05:44 +020035#include <string.h>
36#include <stdlib.h>
37#include <stdio.h>
38
Paul Bakker1cfc4582014-04-09 15:25:13 +020039#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
40#include <unistd.h>
41#endif /* !_WIN32 && POLARSSL_FS_IO */
42
Paul Bakker15b9b3a2013-09-23 12:05:44 +020043#include "polarssl/error.h"
44#include "polarssl/pk.h"
45#include "polarssl/ecdsa.h"
46#include "polarssl/rsa.h"
47#include "polarssl/error.h"
48#include "polarssl/entropy.h"
49#include "polarssl/ctr_drbg.h"
50
51#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) || \
52 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
53int main( int argc, char *argv[] )
54{
55 ((void) argc);
56 ((void) argv);
57
Rich Evansf90016a2015-01-19 14:26:37 +000058 polarssl_printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO and/or "
Paul Bakker15b9b3a2013-09-23 12:05:44 +020059 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
60 "not defined.\n" );
61 return( 0 );
62}
63#else
64
Paul Bakker15b9b3a2013-09-23 12:05:44 +020065#define FORMAT_PEM 0
66#define FORMAT_DER 1
67
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +010068#define DFL_TYPE POLARSSL_PK_RSA
Paul Bakker15b9b3a2013-09-23 12:05:44 +020069#define DFL_RSA_KEYSIZE 4096
70#define DFL_FILENAME "keyfile.key"
71#define DFL_FORMAT FORMAT_PEM
Paul Bakker1cfc4582014-04-09 15:25:13 +020072#define DFL_USE_DEV_RANDOM 0
Paul Bakker15b9b3a2013-09-23 12:05:44 +020073
Paul Bakkerd153ef32014-08-18 12:00:28 +020074#if defined(POLARSSL_ECP_C)
75#define DFL_EC_CURVE ecp_curve_list()->grp_id
76#else
77#define DFL_EC_CURVE 0
78#endif
79
Paul Bakker15b9b3a2013-09-23 12:05:44 +020080/*
81 * global options
82 */
83struct options
84{
85 int type; /* the type of key to generate */
86 int rsa_keysize; /* length of key in bits */
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +010087 int ec_curve; /* curve identifier for EC keys */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020088 const char *filename; /* filename of the key file */
89 int format; /* the output format to use */
Paul Bakker1cfc4582014-04-09 15:25:13 +020090 int use_dev_random; /* use /dev/random as entropy source */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020091} opt;
92
Paul Bakker1cfc4582014-04-09 15:25:13 +020093#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
94
95#define DEV_RANDOM_THRESHOLD 32
96
97int dev_random_entropy_poll( void *data, unsigned char *output,
98 size_t len, size_t *olen )
99{
100 FILE *file;
101 size_t ret, left = len;
102 unsigned char *p = output;
103 ((void) data);
104
105 *olen = 0;
106
107 file = fopen( "/dev/random", "rb" );
108 if( file == NULL )
109 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
110
111 while( left > 0 )
112 {
113 /* /dev/random can return much less than requested. If so, try again */
114 ret = fread( p, 1, left, file );
115 if( ret == 0 && ferror( file ) )
116 {
117 fclose( file );
118 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
119 }
120
121 p += ret;
122 left -= ret;
123 sleep( 1 );
124 }
125 fclose( file );
126 *olen = len;
127
128 return( 0 );
129}
130#endif /* !_WIN32 && POLARSSL_FS_IO */
131
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200132static int write_private_key( pk_context *key, const char *output_file )
133{
134 int ret;
135 FILE *f;
136 unsigned char output_buf[16000];
137 unsigned char *c = output_buf;
138 size_t len = 0;
139
140 memset(output_buf, 0, 16000);
141 if( opt.format == FORMAT_PEM )
142 {
143 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
144 return( ret );
145
146 len = strlen( (char *) output_buf );
147 }
148 else
149 {
150 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
151 return( ret );
152
153 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200154 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200155 }
156
Paul Bakker3966d712014-07-10 12:03:09 +0200157 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200158 return( -1 );
159
160 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200161 {
162 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200163 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200164 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200165
Paul Bakker0c226102014-04-17 16:02:36 +0200166 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200167
168 return( 0 );
169}
170
Paul Bakker1cfc4582014-04-09 15:25:13 +0200171#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
172#define USAGE_DEV_RANDOM \
173 " use_dev_random=0|1 default: 0\n"
174#else
175#define USAGE_DEV_RANDOM ""
176#endif /* !_WIN32 && POLARSSL_FS_IO */
177
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200178#define USAGE \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100179 "\n usage: gen_key param=<>...\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200180 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100181 " type=rsa|ec default: rsa\n" \
182 " rsa_keysize=%%d default: 4096\n" \
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100183 " ec_curve=%%s see below\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100184 " filename=%%s default: keyfile.key\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200185 " format=pem|der default: pem\n" \
Paul Bakker1cfc4582014-04-09 15:25:13 +0200186 USAGE_DEV_RANDOM \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200187 "\n"
188
189int main( int argc, char *argv[] )
190{
191 int ret = 0;
192 pk_context key;
193 char buf[1024];
194 int i;
195 char *p, *q;
196 entropy_context entropy;
197 ctr_drbg_context ctr_drbg;
198 const char *pers = "gen_key";
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100199#if defined(POLARSSL_ECP_C)
200 const ecp_curve_info *curve_info;
201#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200202
203 /*
204 * Set to sane values
205 */
206 pk_init( &key );
207 memset( buf, 0, sizeof( buf ) );
208
209 if( argc == 0 )
210 {
211 usage:
212 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000213 polarssl_printf( USAGE );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100214#if defined(POLARSSL_ECP_C)
Rich Evansf90016a2015-01-19 14:26:37 +0000215 polarssl_printf( " availabled ec_curve values:\n" );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100216 curve_info = ecp_curve_list();
Rich Evansf90016a2015-01-19 14:26:37 +0000217 polarssl_printf( " %s (default)\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100218 while( ( ++curve_info )->name != NULL )
Rich Evansf90016a2015-01-19 14:26:37 +0000219 polarssl_printf( " %s\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100220#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200221 goto exit;
222 }
223
224 opt.type = DFL_TYPE;
225 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100226 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200227 opt.filename = DFL_FILENAME;
228 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200229 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200230
231 for( i = 1; i < argc; i++ )
232 {
233 p = argv[i];
234 if( ( q = strchr( p, '=' ) ) == NULL )
235 goto usage;
236 *q++ = '\0';
237
238 if( strcmp( p, "type" ) == 0 )
239 {
240 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100241 opt.type = POLARSSL_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100242 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100243 opt.type = POLARSSL_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200244 else
245 goto usage;
246 }
247 else if( strcmp( p, "format" ) == 0 )
248 {
249 if( strcmp( q, "pem" ) == 0 )
250 opt.format = FORMAT_PEM;
251 else if( strcmp( q, "der" ) == 0 )
252 opt.format = FORMAT_DER;
253 else
254 goto usage;
255 }
256 else if( strcmp( p, "rsa_keysize" ) == 0 )
257 {
258 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200259 if( opt.rsa_keysize < 1024 ||
260 opt.rsa_keysize > POLARSSL_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200261 goto usage;
262 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200263#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100264 else if( strcmp( p, "ec_curve" ) == 0 )
265 {
266 if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL )
267 goto usage;
268 opt.ec_curve = curve_info->grp_id;
269 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200270#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200271 else if( strcmp( p, "filename" ) == 0 )
272 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200273 else if( strcmp( p, "use_dev_random" ) == 0 )
274 {
275 opt.use_dev_random = atoi( q );
276 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
277 goto usage;
278 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200279 else
280 goto usage;
281 }
282
Rich Evansf90016a2015-01-19 14:26:37 +0000283 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200284 fflush( stdout );
285
286 entropy_init( &entropy );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200287#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
288 if( opt.use_dev_random )
289 {
290 if( ( ret = entropy_add_source( &entropy, dev_random_entropy_poll,
291 NULL, DEV_RANDOM_THRESHOLD ) ) != 0 )
292 {
Rich Evansf90016a2015-01-19 14:26:37 +0000293 polarssl_printf( " failed\n ! entropy_add_source returned -0x%04x\n", -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200294 goto exit;
295 }
296
Rich Evansf90016a2015-01-19 14:26:37 +0000297 polarssl_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200298 fflush( stdout );
299 }
300#endif /* !_WIN32 && POLARSSL_FS_IO */
301
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200302 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
303 (const unsigned char *) pers,
304 strlen( pers ) ) ) != 0 )
305 {
Rich Evansf90016a2015-01-19 14:26:37 +0000306 polarssl_printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200307 goto exit;
308 }
309
310 /*
311 * 1.1. Generate the key
312 */
Rich Evansf90016a2015-01-19 14:26:37 +0000313 polarssl_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200314 fflush( stdout );
315
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100316 if( ( ret = pk_init_ctx( &key, pk_info_from_type( opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200317 {
Rich Evansf90016a2015-01-19 14:26:37 +0000318 polarssl_printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100319 goto exit;
320 }
321
322#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
323 if( opt.type == POLARSSL_PK_RSA )
324 {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200325 ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg,
326 opt.rsa_keysize, 65537 );
327 if( ret != 0 )
328 {
Rich Evansf90016a2015-01-19 14:26:37 +0000329 polarssl_printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200330 goto exit;
331 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200332 }
333 else
334#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100335#if defined(POLARSSL_ECP_C)
336 if( opt.type == POLARSSL_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200337 {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100338 ret = ecp_gen_key( opt.ec_curve, pk_ec( key ),
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100339 ctr_drbg_random, &ctr_drbg );
340 if( ret != 0 )
341 {
Rich Evansf90016a2015-01-19 14:26:37 +0000342 polarssl_printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100343 goto exit;
344 }
345 }
346 else
347#endif /* POLARSSL_ECP_C */
348 {
Rich Evansf90016a2015-01-19 14:26:37 +0000349 polarssl_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200350 goto exit;
351 }
352
353 /*
354 * 1.2 Print the key
355 */
Rich Evansf90016a2015-01-19 14:26:37 +0000356 polarssl_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200357
358#if defined(POLARSSL_RSA_C)
359 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
360 {
361 rsa_context *rsa = pk_rsa( key );
362 mpi_write_file( "N: ", &rsa->N, 16, NULL );
363 mpi_write_file( "E: ", &rsa->E, 16, NULL );
364 mpi_write_file( "D: ", &rsa->D, 16, NULL );
365 mpi_write_file( "P: ", &rsa->P, 16, NULL );
366 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
367 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
368 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
369 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
370 }
371 else
372#endif
373#if defined(POLARSSL_ECP_C)
374 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
375 {
376 ecp_keypair *ecp = pk_ec( key );
Rich Evansf90016a2015-01-19 14:26:37 +0000377 polarssl_printf( "curve: %s\n",
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100378 ecp_curve_info_from_grp_id( ecp->grp.id )->name );
379 mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
380 mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
381 mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200382 }
383 else
384#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000385 polarssl_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200386
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200387 /*
388 * 1.3 Export key
389 */
Rich Evansf90016a2015-01-19 14:26:37 +0000390 polarssl_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200391
392 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
393 {
Rich Evansf90016a2015-01-19 14:26:37 +0000394 polarssl_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200395 goto exit;
396 }
397
Rich Evansf90016a2015-01-19 14:26:37 +0000398 polarssl_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200399
400exit:
401
402 if( ret != 0 && ret != 1)
403 {
404#ifdef POLARSSL_ERROR_C
405 polarssl_strerror( ret, buf, sizeof( buf ) );
Rich Evansf90016a2015-01-19 14:26:37 +0000406 polarssl_printf( " - %s\n", buf );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200407#else
Rich Evansf90016a2015-01-19 14:26:37 +0000408 polarssl_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200409#endif
410 }
411
412 pk_free( &key );
Paul Bakkera317a982014-06-18 16:44:11 +0200413 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200414 entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200415
416#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000417 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200418 fflush( stdout ); getchar();
419#endif
420
421 return( ret );
422}
423#endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */