blob: 767d621a3c6d3a84b335895675f77928989f5166 [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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker15b9b3a2013-09-23 12:05:44 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +020031
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
35
Paul Bakker1cfc4582014-04-09 15:25:13 +020036#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
37#include <unistd.h>
38#endif /* !_WIN32 && POLARSSL_FS_IO */
39
Paul Bakker15b9b3a2013-09-23 12:05:44 +020040#include "polarssl/error.h"
41#include "polarssl/pk.h"
42#include "polarssl/ecdsa.h"
43#include "polarssl/rsa.h"
44#include "polarssl/error.h"
45#include "polarssl/entropy.h"
46#include "polarssl/ctr_drbg.h"
47
48#if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) || \
49 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C)
50int main( int argc, char *argv[] )
51{
52 ((void) argc);
53 ((void) argv);
54
55 printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO and/or "
56 "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C "
57 "not defined.\n" );
58 return( 0 );
59}
60#else
61
Paul Bakker15b9b3a2013-09-23 12:05:44 +020062#define FORMAT_PEM 0
63#define FORMAT_DER 1
64
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +010065#define DFL_TYPE POLARSSL_PK_RSA
Paul Bakker15b9b3a2013-09-23 12:05:44 +020066#define DFL_RSA_KEYSIZE 4096
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +010067#define DFL_EC_CURVE ecp_curve_list()->grp_id
Paul Bakker15b9b3a2013-09-23 12:05:44 +020068#define DFL_FILENAME "keyfile.key"
69#define DFL_FORMAT FORMAT_PEM
Paul Bakker1cfc4582014-04-09 15:25:13 +020070#define DFL_USE_DEV_RANDOM 0
Paul Bakker15b9b3a2013-09-23 12:05:44 +020071
72/*
73 * global options
74 */
75struct options
76{
77 int type; /* the type of key to generate */
78 int rsa_keysize; /* length of key in bits */
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +010079 int ec_curve; /* curve identifier for EC keys */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020080 const char *filename; /* filename of the key file */
81 int format; /* the output format to use */
Paul Bakker1cfc4582014-04-09 15:25:13 +020082 int use_dev_random; /* use /dev/random as entropy source */
Paul Bakker15b9b3a2013-09-23 12:05:44 +020083} opt;
84
Paul Bakker1cfc4582014-04-09 15:25:13 +020085#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
86
87#define DEV_RANDOM_THRESHOLD 32
88
89int dev_random_entropy_poll( void *data, unsigned char *output,
90 size_t len, size_t *olen )
91{
92 FILE *file;
93 size_t ret, left = len;
94 unsigned char *p = output;
95 ((void) data);
96
97 *olen = 0;
98
99 file = fopen( "/dev/random", "rb" );
100 if( file == NULL )
101 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
102
103 while( left > 0 )
104 {
105 /* /dev/random can return much less than requested. If so, try again */
106 ret = fread( p, 1, left, file );
107 if( ret == 0 && ferror( file ) )
108 {
109 fclose( file );
110 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
111 }
112
113 p += ret;
114 left -= ret;
115 sleep( 1 );
116 }
117 fclose( file );
118 *olen = len;
119
120 return( 0 );
121}
122#endif /* !_WIN32 && POLARSSL_FS_IO */
123
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200124static int write_private_key( pk_context *key, const char *output_file )
125{
126 int ret;
127 FILE *f;
128 unsigned char output_buf[16000];
129 unsigned char *c = output_buf;
130 size_t len = 0;
131
132 memset(output_buf, 0, 16000);
133 if( opt.format == FORMAT_PEM )
134 {
135 if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
136 return( ret );
137
138 len = strlen( (char *) output_buf );
139 }
140 else
141 {
142 if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
143 return( ret );
144
145 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200146 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200147 }
148
Paul Bakker3966d712014-07-10 12:03:09 +0200149 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200150 return( -1 );
151
152 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200153 {
154 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200155 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200156 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200157
Paul Bakker0c226102014-04-17 16:02:36 +0200158 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200159
160 return( 0 );
161}
162
Paul Bakker1cfc4582014-04-09 15:25:13 +0200163#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
164#define USAGE_DEV_RANDOM \
165 " use_dev_random=0|1 default: 0\n"
166#else
167#define USAGE_DEV_RANDOM ""
168#endif /* !_WIN32 && POLARSSL_FS_IO */
169
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200170#define USAGE \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100171 "\n usage: gen_key param=<>...\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200172 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100173 " type=rsa|ec default: rsa\n" \
174 " rsa_keysize=%%d default: 4096\n" \
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100175 " ec_curve=%%s see below\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100176 " filename=%%s default: keyfile.key\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200177 " format=pem|der default: pem\n" \
Paul Bakker1cfc4582014-04-09 15:25:13 +0200178 USAGE_DEV_RANDOM \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200179 "\n"
180
181int main( int argc, char *argv[] )
182{
183 int ret = 0;
184 pk_context key;
185 char buf[1024];
186 int i;
187 char *p, *q;
188 entropy_context entropy;
189 ctr_drbg_context ctr_drbg;
190 const char *pers = "gen_key";
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100191#if defined(POLARSSL_ECP_C)
192 const ecp_curve_info *curve_info;
193#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200194
195 /*
196 * Set to sane values
197 */
198 pk_init( &key );
199 memset( buf, 0, sizeof( buf ) );
200
201 if( argc == 0 )
202 {
203 usage:
204 ret = 1;
205 printf( USAGE );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100206#if defined(POLARSSL_ECP_C)
207 printf( " availabled ec_curve values:\n" );
208 curve_info = ecp_curve_list();
209 printf( " %s (default)\n", curve_info->name );
210 while( ( ++curve_info )->name != NULL )
211 printf( " %s\n", curve_info->name );
212#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200213 goto exit;
214 }
215
216 opt.type = DFL_TYPE;
217 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100218 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200219 opt.filename = DFL_FILENAME;
220 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200221 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200222
223 for( i = 1; i < argc; i++ )
224 {
225 p = argv[i];
226 if( ( q = strchr( p, '=' ) ) == NULL )
227 goto usage;
228 *q++ = '\0';
229
230 if( strcmp( p, "type" ) == 0 )
231 {
232 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100233 opt.type = POLARSSL_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100234 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100235 opt.type = POLARSSL_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200236 else
237 goto usage;
238 }
239 else if( strcmp( p, "format" ) == 0 )
240 {
241 if( strcmp( q, "pem" ) == 0 )
242 opt.format = FORMAT_PEM;
243 else if( strcmp( q, "der" ) == 0 )
244 opt.format = FORMAT_DER;
245 else
246 goto usage;
247 }
248 else if( strcmp( p, "rsa_keysize" ) == 0 )
249 {
250 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200251 if( opt.rsa_keysize < 1024 ||
252 opt.rsa_keysize > POLARSSL_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200253 goto usage;
254 }
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100255 else if( strcmp( p, "ec_curve" ) == 0 )
256 {
257 if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL )
258 goto usage;
259 opt.ec_curve = curve_info->grp_id;
260 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200261 else if( strcmp( p, "filename" ) == 0 )
262 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200263 else if( strcmp( p, "use_dev_random" ) == 0 )
264 {
265 opt.use_dev_random = atoi( q );
266 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
267 goto usage;
268 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200269 else
270 goto usage;
271 }
272
273 printf( "\n . Seeding the random number generator..." );
274 fflush( stdout );
275
276 entropy_init( &entropy );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200277#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
278 if( opt.use_dev_random )
279 {
280 if( ( ret = entropy_add_source( &entropy, dev_random_entropy_poll,
281 NULL, DEV_RANDOM_THRESHOLD ) ) != 0 )
282 {
283 printf( " failed\n ! entropy_add_source returned -0x%04x\n", -ret );
284 goto exit;
285 }
286
287 printf("\n Using /dev/random, so can take a long time! " );
288 fflush( stdout );
289 }
290#endif /* !_WIN32 && POLARSSL_FS_IO */
291
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200292 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
293 (const unsigned char *) pers,
294 strlen( pers ) ) ) != 0 )
295 {
296 printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
297 goto exit;
298 }
299
300 /*
301 * 1.1. Generate the key
302 */
303 printf( "\n . Generating the private key ..." );
304 fflush( stdout );
305
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100306 if( ( ret = pk_init_ctx( &key, pk_info_from_type( opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200307 {
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100308 printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret );
309 goto exit;
310 }
311
312#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
313 if( opt.type == POLARSSL_PK_RSA )
314 {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200315 ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg,
316 opt.rsa_keysize, 65537 );
317 if( ret != 0 )
318 {
319 printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
320 goto exit;
321 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200322 }
323 else
324#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100325#if defined(POLARSSL_ECP_C)
326 if( opt.type == POLARSSL_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200327 {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100328 ret = ecp_gen_key( opt.ec_curve, pk_ec( key ),
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100329 ctr_drbg_random, &ctr_drbg );
330 if( ret != 0 )
331 {
332 printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
333 goto exit;
334 }
335 }
336 else
337#endif /* POLARSSL_ECP_C */
338 {
339 printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200340 goto exit;
341 }
342
343 /*
344 * 1.2 Print the key
345 */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100346 printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200347
348#if defined(POLARSSL_RSA_C)
349 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
350 {
351 rsa_context *rsa = pk_rsa( key );
352 mpi_write_file( "N: ", &rsa->N, 16, NULL );
353 mpi_write_file( "E: ", &rsa->E, 16, NULL );
354 mpi_write_file( "D: ", &rsa->D, 16, NULL );
355 mpi_write_file( "P: ", &rsa->P, 16, NULL );
356 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
357 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
358 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
359 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
360 }
361 else
362#endif
363#if defined(POLARSSL_ECP_C)
364 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
365 {
366 ecp_keypair *ecp = pk_ec( key );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100367 printf( "curve: %s\n",
368 ecp_curve_info_from_grp_id( ecp->grp.id )->name );
369 mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
370 mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
371 mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200372 }
373 else
374#endif
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100375 printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200376
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200377 /*
378 * 1.3 Export key
379 */
380 printf( " . Writing key to file..." );
381
382 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
383 {
384 printf( " failed\n" );
385 goto exit;
386 }
387
388 printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200389
390exit:
391
392 if( ret != 0 && ret != 1)
393 {
394#ifdef POLARSSL_ERROR_C
395 polarssl_strerror( ret, buf, sizeof( buf ) );
396 printf( " - %s\n", buf );
397#else
398 printf("\n");
399#endif
400 }
401
402 pk_free( &key );
Paul Bakkera317a982014-06-18 16:44:11 +0200403 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200404 entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200405
406#if defined(_WIN32)
407 printf( " + Press Enter to exit this program.\n" );
408 fflush( stdout ); getchar();
409#endif
410
411 return( ret );
412}
413#endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */