blob: 9f060b7c286c1277c68c69b0ee6caa4bdcfbd743 [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 )
149 return( -1 );
150
151 fclose(f);
152
153 return( 0 );
154}
155
Paul Bakker1cfc4582014-04-09 15:25:13 +0200156#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
157#define USAGE_DEV_RANDOM \
158 " use_dev_random=0|1 default: 0\n"
159#else
160#define USAGE_DEV_RANDOM ""
161#endif /* !_WIN32 && POLARSSL_FS_IO */
162
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200163#define USAGE \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100164 "\n usage: gen_key param=<>...\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200165 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100166 " type=rsa|ec default: rsa\n" \
167 " rsa_keysize=%%d default: 4096\n" \
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100168 " ec_curve=%%s see below\n" \
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100169 " filename=%%s default: keyfile.key\n" \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200170 " format=pem|der default: pem\n" \
Paul Bakker1cfc4582014-04-09 15:25:13 +0200171 USAGE_DEV_RANDOM \
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200172 "\n"
173
174int main( int argc, char *argv[] )
175{
176 int ret = 0;
177 pk_context key;
178 char buf[1024];
179 int i;
180 char *p, *q;
181 entropy_context entropy;
182 ctr_drbg_context ctr_drbg;
183 const char *pers = "gen_key";
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100184#if defined(POLARSSL_ECP_C)
185 const ecp_curve_info *curve_info;
186#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200187
188 /*
189 * Set to sane values
190 */
191 pk_init( &key );
192 memset( buf, 0, sizeof( buf ) );
193
194 if( argc == 0 )
195 {
196 usage:
197 ret = 1;
198 printf( USAGE );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100199#if defined(POLARSSL_ECP_C)
200 printf( " availabled ec_curve values:\n" );
201 curve_info = ecp_curve_list();
202 printf( " %s (default)\n", curve_info->name );
203 while( ( ++curve_info )->name != NULL )
204 printf( " %s\n", curve_info->name );
205#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200206 goto exit;
207 }
208
209 opt.type = DFL_TYPE;
210 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100211 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200212 opt.filename = DFL_FILENAME;
213 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200214 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200215
216 for( i = 1; i < argc; i++ )
217 {
218 p = argv[i];
219 if( ( q = strchr( p, '=' ) ) == NULL )
220 goto usage;
221 *q++ = '\0';
222
223 if( strcmp( p, "type" ) == 0 )
224 {
225 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100226 opt.type = POLARSSL_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100227 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100228 opt.type = POLARSSL_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200229 else
230 goto usage;
231 }
232 else if( strcmp( p, "format" ) == 0 )
233 {
234 if( strcmp( q, "pem" ) == 0 )
235 opt.format = FORMAT_PEM;
236 else if( strcmp( q, "der" ) == 0 )
237 opt.format = FORMAT_DER;
238 else
239 goto usage;
240 }
241 else if( strcmp( p, "rsa_keysize" ) == 0 )
242 {
243 opt.rsa_keysize = atoi( q );
244 if( opt.rsa_keysize < 1024 || opt.rsa_keysize > 8192 )
245 goto usage;
246 }
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100247 else if( strcmp( p, "ec_curve" ) == 0 )
248 {
249 if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL )
250 goto usage;
251 opt.ec_curve = curve_info->grp_id;
252 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200253 else if( strcmp( p, "filename" ) == 0 )
254 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200255 else if( strcmp( p, "use_dev_random" ) == 0 )
256 {
257 opt.use_dev_random = atoi( q );
258 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
259 goto usage;
260 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200261 else
262 goto usage;
263 }
264
265 printf( "\n . Seeding the random number generator..." );
266 fflush( stdout );
267
268 entropy_init( &entropy );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200269#if !defined(_WIN32) && defined(POLARSSL_FS_IO)
270 if( opt.use_dev_random )
271 {
272 if( ( ret = entropy_add_source( &entropy, dev_random_entropy_poll,
273 NULL, DEV_RANDOM_THRESHOLD ) ) != 0 )
274 {
275 printf( " failed\n ! entropy_add_source returned -0x%04x\n", -ret );
276 goto exit;
277 }
278
279 printf("\n Using /dev/random, so can take a long time! " );
280 fflush( stdout );
281 }
282#endif /* !_WIN32 && POLARSSL_FS_IO */
283
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200284 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
285 (const unsigned char *) pers,
286 strlen( pers ) ) ) != 0 )
287 {
288 printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret );
289 goto exit;
290 }
291
292 /*
293 * 1.1. Generate the key
294 */
295 printf( "\n . Generating the private key ..." );
296 fflush( stdout );
297
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100298 if( ( ret = pk_init_ctx( &key, pk_info_from_type( opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200299 {
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100300 printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret );
301 goto exit;
302 }
303
304#if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME)
305 if( opt.type == POLARSSL_PK_RSA )
306 {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200307 ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg,
308 opt.rsa_keysize, 65537 );
309 if( ret != 0 )
310 {
311 printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
312 goto exit;
313 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200314 }
315 else
316#endif /* POLARSSL_RSA_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100317#if defined(POLARSSL_ECP_C)
318 if( opt.type == POLARSSL_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200319 {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100320 ret = ecp_gen_key( opt.ec_curve, pk_ec( key ),
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100321 ctr_drbg_random, &ctr_drbg );
322 if( ret != 0 )
323 {
324 printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret );
325 goto exit;
326 }
327 }
328 else
329#endif /* POLARSSL_ECP_C */
330 {
331 printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200332 goto exit;
333 }
334
335 /*
336 * 1.2 Print the key
337 */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100338 printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200339
340#if defined(POLARSSL_RSA_C)
341 if( pk_get_type( &key ) == POLARSSL_PK_RSA )
342 {
343 rsa_context *rsa = pk_rsa( key );
344 mpi_write_file( "N: ", &rsa->N, 16, NULL );
345 mpi_write_file( "E: ", &rsa->E, 16, NULL );
346 mpi_write_file( "D: ", &rsa->D, 16, NULL );
347 mpi_write_file( "P: ", &rsa->P, 16, NULL );
348 mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
349 mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
350 mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
351 mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
352 }
353 else
354#endif
355#if defined(POLARSSL_ECP_C)
356 if( pk_get_type( &key ) == POLARSSL_PK_ECKEY )
357 {
358 ecp_keypair *ecp = pk_ec( key );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100359 printf( "curve: %s\n",
360 ecp_curve_info_from_grp_id( ecp->grp.id )->name );
361 mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
362 mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
363 mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200364 }
365 else
366#endif
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100367 printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200368
369 write_private_key( &key, opt.filename );
370
371exit:
372
373 if( ret != 0 && ret != 1)
374 {
375#ifdef POLARSSL_ERROR_C
376 polarssl_strerror( ret, buf, sizeof( buf ) );
377 printf( " - %s\n", buf );
378#else
379 printf("\n");
380#endif
381 }
382
383 pk_free( &key );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200384 entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200385
386#if defined(_WIN32)
387 printf( " + Press Enter to exit this program.\n" );
388 fflush( stdout ); getchar();
389#endif
390
391 return( ret );
392}
393#endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */