blob: 5470b573739cd55c7de3cad0519a6313394c448c [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
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32
Paul Bakker1cfc4582014-04-09 15:25:13 +020033#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
34#include <unistd.h>
35#endif /* !_WIN32 && POLARSSL_FS_IO */
36
Paul Bakker15b9b3a2013-09-23 12:05:44 +020037#include "polarssl/error.h"
38#include "polarssl/pk.h"
39#include "polarssl/ecdsa.h"
40#include "polarssl/rsa.h"
41#include "polarssl/error.h"
42#include "polarssl/entropy.h"
43#include "polarssl/ctr_drbg.h"
44
45#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) || \
46 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
47int main( int argc, char *argv[] )
48{
49 ((void) argc);
50 ((void) argv);
51
52 printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO and/or "
53 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
54 "not defined.\n" );
55 return( 0 );
56}
57#else
58
Paul Bakker15b9b3a2013-09-23 12:05:44 +020059#define FORMAT_PEM 0
60#define FORMAT_DER 1
61
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +010062#define DFL_TYPE POLARSSL_PK_RSA
Paul Bakker15b9b3a2013-09-23 12:05:44 +020063#define DFL_RSA_KEYSIZE 4096
64#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
Paul Bakkerd153ef32014-08-18 12:00:28 +020068#if defined(POLARSSL_ECP_C)
69#define DFL_EC_CURVE ecp_curve_list()->grp_id
70#else
71#define DFL_EC_CURVE 0
72#endif
73
Paul Bakker15b9b3a2013-09-23 12:05:44 +020074/*
75 * global options
76 */
77struct options
78{
79 int type; /* the type of key to generate */
80 int rsa_keysize; /* length of key in bits */
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +010081 int ec_curve; /* curve identifier for EC keys */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020082 const char *filename; /* filename of the key file */
83 int format; /* the output format to use */
Paul Bakker1cfc4582014-04-09 15:25:13 +020084 int use_dev_random; /* use /dev/random as entropy source */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020085} opt;
86
Paul Bakker1cfc4582014-04-09 15:25:13 +020087#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
88
89#define DEV_RANDOM_THRESHOLD 32
90
91int dev_random_entropy_poll( void *data, unsigned char *output,
92 size_t len, size_t *olen )
93{
94 FILE *file;
95 size_t ret, left = len;
96 unsigned char *p = output;
97 ((void) data);
98
99 *olen = 0;
100
101 file = fopen( "/dev/random", "rb" );
102 if( file == NULL )
103 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
104
105 while( left > 0 )
106 {
107 /* /dev/random can return much less than requested. If so, try again */
108 ret = fread( p, 1, left, file );
109 if( ret == 0 && ferror( file ) )
110 {
111 fclose( file );
112 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
113 }
114
115 p += ret;
116 left -= ret;
117 sleep( 1 );
118 }
119 fclose( file );
120 *olen = len;
121
122 return( 0 );
123}
124#endif /* !_WIN32 && POLARSSL_FS_IO */
125
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200126static int write_private_key( pk_context *key, const char *output_file )
127{
128 int ret;
129 FILE *f;
130 unsigned char output_buf[16000];
131 unsigned char *c = output_buf;
132 size_t len = 0;
133
134 memset(output_buf, 0, 16000);
135 if( opt.format == FORMAT_PEM )
136 {
137 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
138 return( ret );
139
140 len = strlen( (char *) output_buf );
141 }
142 else
143 {
144 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
145 return( ret );
146
147 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200148 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200149 }
150
Paul Bakker3966d712014-07-10 12:03:09 +0200151 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200152 return( -1 );
153
154 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200155 {
156 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200157 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200158 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200159
Paul Bakker0c226102014-04-17 16:02:36 +0200160 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200161
162 return( 0 );
163}
164
Paul Bakker1cfc4582014-04-09 15:25:13 +0200165#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
166#define USAGE_DEV_RANDOM \
167 " use_dev_random=0|1 default: 0\n"
168#else
169#define USAGE_DEV_RANDOM ""
170#endif /* !_WIN32 && POLARSSL_FS_IO */
171
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200172#define USAGE \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100173 "\n usage: gen_key param=<>...\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200174 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100175 " type=rsa|ec default: rsa\n" \
176 " rsa_keysize=%%d default: 4096\n" \
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100177 " ec_curve=%%s see below\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100178 " filename=%%s default: keyfile.key\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200179 " format=pem|der default: pem\n" \
Paul Bakker1cfc4582014-04-09 15:25:13 +0200180 USAGE_DEV_RANDOM \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200181 "\n"
182
183int main( int argc, char *argv[] )
184{
185 int ret = 0;
186 pk_context key;
187 char buf[1024];
188 int i;
189 char *p, *q;
190 entropy_context entropy;
191 ctr_drbg_context ctr_drbg;
192 const char *pers = "gen_key";
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100193#if defined(POLARSSL_ECP_C)
194 const ecp_curve_info *curve_info;
195#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200196
197 /*
198 * Set to sane values
199 */
200 pk_init( &key );
201 memset( buf, 0, sizeof( buf ) );
202
203 if( argc == 0 )
204 {
205 usage:
206 ret = 1;
207 printf( USAGE );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100208#if defined(POLARSSL_ECP_C)
209 printf( " availabled ec_curve values:\n" );
210 curve_info = ecp_curve_list();
211 printf( " %s (default)\n", curve_info->name );
212 while( ( ++curve_info )->name != NULL )
213 printf( " %s\n", curve_info->name );
214#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200215 goto exit;
216 }
217
218 opt.type = DFL_TYPE;
219 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100220 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200221 opt.filename = DFL_FILENAME;
222 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200223 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200224
225 for( i = 1; i < argc; i++ )
226 {
227 p = argv[i];
228 if( ( q = strchr( p, '=' ) ) == NULL )
229 goto usage;
230 *q++ = '\0';
231
232 if( strcmp( p, "type" ) == 0 )
233 {
234 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100235 opt.type = POLARSSL_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100236 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100237 opt.type = POLARSSL_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200238 else
239 goto usage;
240 }
241 else if( strcmp( p, "format" ) == 0 )
242 {
243 if( strcmp( q, "pem" ) == 0 )
244 opt.format = FORMAT_PEM;
245 else if( strcmp( q, "der" ) == 0 )
246 opt.format = FORMAT_DER;
247 else
248 goto usage;
249 }
250 else if( strcmp( p, "rsa_keysize" ) == 0 )
251 {
252 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200253 if( opt.rsa_keysize < 1024 ||
254 opt.rsa_keysize > POLARSSL_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200255 goto usage;
256 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200257#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100258 else if( strcmp( p, "ec_curve" ) == 0 )
259 {
260 if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL )
261 goto usage;
262 opt.ec_curve = curve_info->grp_id;
263 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200264#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200265 else if( strcmp( p, "filename" ) == 0 )
266 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200267 else if( strcmp( p, "use_dev_random" ) == 0 )
268 {
269 opt.use_dev_random = atoi( q );
270 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
271 goto usage;
272 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200273 else
274 goto usage;
275 }
276
277 printf( "\n . Seeding the random number generator..." );
278 fflush( stdout );
279
280 entropy_init( &entropy );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200281#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
282 if( opt.use_dev_random )
283 {
284 if( ( ret = entropy_add_source( &entropy, dev_random_entropy_poll,
285 NULL, DEV_RANDOM_THRESHOLD ) ) != 0 )
286 {
287 printf( " failed\n ! entropy_add_source returned -0x%04x\n", -ret );
288 goto exit;
289 }
290
291 printf("\n Using /dev/random, so can take a long time! " );
292 fflush( stdout );
293 }
294#endif /* !_WIN32 && POLARSSL_FS_IO */
295
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200296 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
297 (const unsigned char *) pers,
298 strlen( pers ) ) ) != 0 )
299 {
300 printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
301 goto exit;
302 }
303
304 /*
305 * 1.1. Generate the key
306 */
307 printf( "\n . Generating the private key ..." );
308 fflush( stdout );
309
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100310 if( ( ret = pk_init_ctx( &key, pk_info_from_type( opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200311 {
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100312 printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret );
313 goto exit;
314 }
315
316#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
317 if( opt.type == POLARSSL_PK_RSA )
318 {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200319 ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg,
320 opt.rsa_keysize, 65537 );
321 if( ret != 0 )
322 {
323 printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
324 goto exit;
325 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200326 }
327 else
328#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100329#if defined(POLARSSL_ECP_C)
330 if( opt.type == POLARSSL_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200331 {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100332 ret = ecp_gen_key( opt.ec_curve, pk_ec( key ),
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100333 ctr_drbg_random, &ctr_drbg );
334 if( ret != 0 )
335 {
336 printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
337 goto exit;
338 }
339 }
340 else
341#endif /* POLARSSL_ECP_C */
342 {
343 printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200344 goto exit;
345 }
346
347 /*
348 * 1.2 Print the key
349 */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100350 printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200351
352#if defined(POLARSSL_RSA_C)
353 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
354 {
355 rsa_context *rsa = pk_rsa( key );
356 mpi_write_file( "N: ", &rsa->N, 16, NULL );
357 mpi_write_file( "E: ", &rsa->E, 16, NULL );
358 mpi_write_file( "D: ", &rsa->D, 16, NULL );
359 mpi_write_file( "P: ", &rsa->P, 16, NULL );
360 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
361 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
362 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
363 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
364 }
365 else
366#endif
367#if defined(POLARSSL_ECP_C)
368 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
369 {
370 ecp_keypair *ecp = pk_ec( key );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100371 printf( "curve: %s\n",
372 ecp_curve_info_from_grp_id( ecp->grp.id )->name );
373 mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
374 mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
375 mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200376 }
377 else
378#endif
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100379 printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200380
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200381 /*
382 * 1.3 Export key
383 */
384 printf( " . Writing key to file..." );
385
386 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
387 {
388 printf( " failed\n" );
389 goto exit;
390 }
391
392 printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200393
394exit:
395
396 if( ret != 0 && ret != 1)
397 {
398#ifdef POLARSSL_ERROR_C
399 polarssl_strerror( ret, buf, sizeof( buf ) );
400 printf( " - %s\n", buf );
401#else
402 printf("\n");
403#endif
404 }
405
406 pk_free( &key );
Paul Bakkera317a982014-06-18 16:44:11 +0200407 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200408 entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200409
410#if defined(_WIN32)
411 printf( " + Press Enter to exit this program.\n" );
412 fflush( stdout ); getchar();
413#endif
414
415 return( ret );
416}
417#endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */