1 /*
2 * Key generation application
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #include <stdlib.h>
33 #define mbedtls_printf printf
34 #define mbedtls_exit exit
35 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
36 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37 #endif /* MBEDTLS_PLATFORM_C */
38
39 #if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
40 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
41 #include "mbedtls/error.h"
42 #include "mbedtls/pk.h"
43 #include "mbedtls/ecdsa.h"
44 #include "mbedtls/rsa.h"
45 #include "mbedtls/error.h"
46 #include "mbedtls/entropy.h"
47 #include "mbedtls/ctr_drbg.h"
48
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52
53 #if !defined(_WIN32)
54 #include <unistd.h>
55
56 #define DEV_RANDOM_THRESHOLD 32
57
dev_random_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)58 int dev_random_entropy_poll( void *data, unsigned char *output,
59 size_t len, size_t *olen )
60 {
61 FILE *file;
62 size_t ret, left = len;
63 unsigned char *p = output;
64 ((void) data);
65
66 *olen = 0;
67
68 file = fopen( "/dev/random", "rb" );
69 if( file == NULL )
70 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
71
72 while( left > 0 )
73 {
74 /* /dev/random can return much less than requested. If so, try again */
75 ret = fread( p, 1, left, file );
76 if( ret == 0 && ferror( file ) )
77 {
78 fclose( file );
79 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
80 }
81
82 p += ret;
83 left -= ret;
84 sleep( 1 );
85 }
86 fclose( file );
87 *olen = len;
88
89 return( 0 );
90 }
91 #endif /* !_WIN32 */
92 #endif
93
94 #if defined(MBEDTLS_ECP_C)
95 #define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
96 #else
97 #define DFL_EC_CURVE 0
98 #endif
99
100 #if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
101 #define USAGE_DEV_RANDOM \
102 " use_dev_random=0|1 default: 0\n"
103 #else
104 #define USAGE_DEV_RANDOM ""
105 #endif /* !_WIN32 && MBEDTLS_FS_IO */
106
107 #define FORMAT_PEM 0
108 #define FORMAT_DER 1
109
110 #define DFL_TYPE MBEDTLS_PK_RSA
111 #define DFL_RSA_KEYSIZE 4096
112 #define DFL_FILENAME "keyfile.key"
113 #define DFL_FORMAT FORMAT_PEM
114 #define DFL_USE_DEV_RANDOM 0
115
116 #define USAGE \
117 "\n usage: gen_key param=<>...\n" \
118 "\n acceptable parameters:\n" \
119 " type=rsa|ec default: rsa\n" \
120 " rsa_keysize=%%d default: 4096\n" \
121 " ec_curve=%%s see below\n" \
122 " filename=%%s default: keyfile.key\n" \
123 " format=pem|der default: pem\n" \
124 USAGE_DEV_RANDOM \
125 "\n"
126
127 #if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
128 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
129 !defined(MBEDTLS_CTR_DRBG_C)
main(void)130 int main( void )
131 {
132 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
133 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
134 "MBEDTLS_PEM_WRITE_C"
135 "not defined.\n" );
136 return( 0 );
137 }
138 #else
139
140 #if defined(MBEDTLS_CHECK_PARAMS)
141 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)142 void mbedtls_param_failed( const char *failure_condition,
143 const char *file,
144 int line )
145 {
146 mbedtls_printf( "%s:%i: Input param failed - %s\n",
147 file, line, failure_condition );
148 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
149 }
150 #endif
151
152 /*
153 * global options
154 */
155 struct options
156 {
157 int type; /* the type of key to generate */
158 int rsa_keysize; /* length of key in bits */
159 int ec_curve; /* curve identifier for EC keys */
160 const char *filename; /* filename of the key file */
161 int format; /* the output format to use */
162 int use_dev_random; /* use /dev/random as entropy source */
163 } opt;
164
write_private_key(mbedtls_pk_context * key,const char * output_file)165 static int write_private_key( mbedtls_pk_context *key, const char *output_file )
166 {
167 int ret;
168 FILE *f;
169 unsigned char output_buf[16000];
170 unsigned char *c = output_buf;
171 size_t len = 0;
172
173 memset(output_buf, 0, 16000);
174 if( opt.format == FORMAT_PEM )
175 {
176 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
177 return( ret );
178
179 len = strlen( (char *) output_buf );
180 }
181 else
182 {
183 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
184 return( ret );
185
186 len = ret;
187 c = output_buf + sizeof(output_buf) - len;
188 }
189
190 if( ( f = fopen( output_file, "wb" ) ) == NULL )
191 return( -1 );
192
193 if( fwrite( c, 1, len, f ) != len )
194 {
195 fclose( f );
196 return( -1 );
197 }
198
199 fclose( f );
200
201 return( 0 );
202 }
203
main(int argc,char * argv[])204 int main( int argc, char *argv[] )
205 {
206 int ret = 1;
207 int exit_code = MBEDTLS_EXIT_FAILURE;
208 mbedtls_pk_context key;
209 char buf[1024];
210 int i;
211 char *p, *q;
212 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
213 mbedtls_entropy_context entropy;
214 mbedtls_ctr_drbg_context ctr_drbg;
215 const char *pers = "gen_key";
216 #if defined(MBEDTLS_ECP_C)
217 const mbedtls_ecp_curve_info *curve_info;
218 #endif
219
220 /*
221 * Set to sane values
222 */
223
224 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
225 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
226 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
227
228 mbedtls_pk_init( &key );
229 mbedtls_ctr_drbg_init( &ctr_drbg );
230 memset( buf, 0, sizeof( buf ) );
231
232 if( argc == 0 )
233 {
234 usage:
235 mbedtls_printf( USAGE );
236 #if defined(MBEDTLS_ECP_C)
237 mbedtls_printf( " available ec_curve values:\n" );
238 curve_info = mbedtls_ecp_curve_list();
239 mbedtls_printf( " %s (default)\n", curve_info->name );
240 while( ( ++curve_info )->name != NULL )
241 mbedtls_printf( " %s\n", curve_info->name );
242 #endif /* MBEDTLS_ECP_C */
243 goto exit;
244 }
245
246 opt.type = DFL_TYPE;
247 opt.rsa_keysize = DFL_RSA_KEYSIZE;
248 opt.ec_curve = DFL_EC_CURVE;
249 opt.filename = DFL_FILENAME;
250 opt.format = DFL_FORMAT;
251 opt.use_dev_random = DFL_USE_DEV_RANDOM;
252
253 for( i = 1; i < argc; i++ )
254 {
255 p = argv[i];
256 if( ( q = strchr( p, '=' ) ) == NULL )
257 goto usage;
258 *q++ = '\0';
259
260 if( strcmp( p, "type" ) == 0 )
261 {
262 if( strcmp( q, "rsa" ) == 0 )
263 opt.type = MBEDTLS_PK_RSA;
264 else if( strcmp( q, "ec" ) == 0 )
265 opt.type = MBEDTLS_PK_ECKEY;
266 else
267 goto usage;
268 }
269 else if( strcmp( p, "format" ) == 0 )
270 {
271 if( strcmp( q, "pem" ) == 0 )
272 opt.format = FORMAT_PEM;
273 else if( strcmp( q, "der" ) == 0 )
274 opt.format = FORMAT_DER;
275 else
276 goto usage;
277 }
278 else if( strcmp( p, "rsa_keysize" ) == 0 )
279 {
280 opt.rsa_keysize = atoi( q );
281 if( opt.rsa_keysize < 1024 ||
282 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
283 goto usage;
284 }
285 #if defined(MBEDTLS_ECP_C)
286 else if( strcmp( p, "ec_curve" ) == 0 )
287 {
288 if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
289 goto usage;
290 opt.ec_curve = curve_info->grp_id;
291 }
292 #endif
293 else if( strcmp( p, "filename" ) == 0 )
294 opt.filename = q;
295 else if( strcmp( p, "use_dev_random" ) == 0 )
296 {
297 opt.use_dev_random = atoi( q );
298 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
299 goto usage;
300 }
301 else
302 goto usage;
303 }
304
305 mbedtls_printf( "\n . Seeding the random number generator..." );
306 fflush( stdout );
307
308 mbedtls_entropy_init( &entropy );
309 #if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
310 if( opt.use_dev_random )
311 {
312 if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
313 NULL, DEV_RANDOM_THRESHOLD,
314 MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
315 {
316 mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", -ret );
317 goto exit;
318 }
319
320 mbedtls_printf("\n Using /dev/random, so can take a long time! " );
321 fflush( stdout );
322 }
323 #endif /* !_WIN32 && MBEDTLS_FS_IO */
324
325 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
326 (const unsigned char *) pers,
327 strlen( pers ) ) ) != 0 )
328 {
329 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
330 goto exit;
331 }
332
333 /*
334 * 1.1. Generate the key
335 */
336 mbedtls_printf( "\n . Generating the private key ..." );
337 fflush( stdout );
338
339 if( ( ret = mbedtls_pk_setup( &key,
340 mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 )
341 {
342 mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", -ret );
343 goto exit;
344 }
345
346 #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
347 if( opt.type == MBEDTLS_PK_RSA )
348 {
349 ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
350 opt.rsa_keysize, 65537 );
351 if( ret != 0 )
352 {
353 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
354 goto exit;
355 }
356 }
357 else
358 #endif /* MBEDTLS_RSA_C */
359 #if defined(MBEDTLS_ECP_C)
360 if( opt.type == MBEDTLS_PK_ECKEY )
361 {
362 ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve,
363 mbedtls_pk_ec( key ),
364 mbedtls_ctr_drbg_random, &ctr_drbg );
365 if( ret != 0 )
366 {
367 mbedtls_printf( " failed\n ! mbedtls_ecp_gen_key returned -0x%04x", -ret );
368 goto exit;
369 }
370 }
371 else
372 #endif /* MBEDTLS_ECP_C */
373 {
374 mbedtls_printf( " failed\n ! key type not supported\n" );
375 goto exit;
376 }
377
378 /*
379 * 1.2 Print the key
380 */
381 mbedtls_printf( " ok\n . Key information:\n" );
382
383 #if defined(MBEDTLS_RSA_C)
384 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
385 {
386 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
387
388 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
389 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
390 {
391 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
392 goto exit;
393 }
394
395 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
396 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
397 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
398 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
399 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
400 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
401 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
402 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
403 }
404 else
405 #endif
406 #if defined(MBEDTLS_ECP_C)
407 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
408 {
409 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
410 mbedtls_printf( "curve: %s\n",
411 mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
412 mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
413 mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
414 mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
415 }
416 else
417 #endif
418 mbedtls_printf(" ! key type not supported\n");
419
420 /*
421 * 1.3 Export key
422 */
423 mbedtls_printf( " . Writing key to file..." );
424
425 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
426 {
427 mbedtls_printf( " failed\n" );
428 goto exit;
429 }
430
431 mbedtls_printf( " ok\n" );
432
433 exit_code = MBEDTLS_EXIT_SUCCESS;
434
435 exit:
436
437 if( exit_code != MBEDTLS_EXIT_SUCCESS )
438 {
439 #ifdef MBEDTLS_ERROR_C
440 mbedtls_strerror( ret, buf, sizeof( buf ) );
441 mbedtls_printf( " - %s\n", buf );
442 #else
443 mbedtls_printf("\n");
444 #endif
445 }
446
447 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
448 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
449 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
450
451 mbedtls_pk_free( &key );
452 mbedtls_ctr_drbg_free( &ctr_drbg );
453 mbedtls_entropy_free( &entropy );
454
455 #if defined(_WIN32)
456 mbedtls_printf( " + Press Enter to exit this program.\n" );
457 fflush( stdout ); getchar();
458 #endif
459
460 return( exit_code );
461 }
462 #endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
463 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */
464