blob: fb454bdc74bfbe1435acef153173a2e1c23a1507 [file] [log] [blame]
Paul Bakker15b9b3a2013-09-23 12:05:44 +02001/*
2 * Key generation application
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker15b9b3a2013-09-23 12:05:44 +020024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker15b9b3a2013-09-23 12:05:44 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garcia7c55e792018-04-29 19:51:56 +010057#include <stdlib.h>
58#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020059#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010060#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia7c55e792018-04-29 19:51:56 +010061#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
62#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
65 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/error.h"
67#include "mbedtls/pk.h"
68#include "mbedtls/ecdsa.h"
69#include "mbedtls/rsa.h"
70#include "mbedtls/error.h"
71#include "mbedtls/entropy.h"
72#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020073
Rich Evans18b78c72015-02-11 14:06:19 +000074#include <stdio.h>
75#include <stdlib.h>
76#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020077
Rich Evans18b78c72015-02-11 14:06:19 +000078#if !defined(_WIN32)
79#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020080
81#define DEV_RANDOM_THRESHOLD 32
82
83int dev_random_entropy_poll( void *data, unsigned char *output,
84 size_t len, size_t *olen )
85{
86 FILE *file;
87 size_t ret, left = len;
88 unsigned char *p = output;
89 ((void) data);
90
91 *olen = 0;
92
93 file = fopen( "/dev/random", "rb" );
94 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020096
97 while( left > 0 )
98 {
99 /* /dev/random can return much less than requested. If so, try again */
100 ret = fread( p, 1, left, file );
101 if( ret == 0 && ferror( file ) )
102 {
103 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200105 }
106
107 p += ret;
108 left -= ret;
109 sleep( 1 );
110 }
111 fclose( file );
112 *olen = len;
113
114 return( 0 );
115}
Rich Evans18b78c72015-02-11 14:06:19 +0000116#endif /* !_WIN32 */
117#endif
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119#if defined(MBEDTLS_ECP_C)
120#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +0000121#else
122#define DFL_EC_CURVE 0
123#endif
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +0000126#define USAGE_DEV_RANDOM \
127 " use_dev_random=0|1 default: 0\n"
128#else
129#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200131
Rich Evans18b78c72015-02-11 14:06:19 +0000132#define FORMAT_PEM 0
133#define FORMAT_DER 1
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +0000136#define DFL_RSA_KEYSIZE 4096
137#define DFL_FILENAME "keyfile.key"
138#define DFL_FORMAT FORMAT_PEM
139#define DFL_USE_DEV_RANDOM 0
140
141#define USAGE \
142 "\n usage: gen_key param=<>...\n" \
143 "\n acceptable parameters:\n" \
144 " type=rsa|ec default: rsa\n" \
145 " rsa_keysize=%%d default: 4096\n" \
146 " ec_curve=%%s see below\n" \
147 " filename=%%s default: keyfile.key\n" \
148 " format=pem|der default: pem\n" \
149 USAGE_DEV_RANDOM \
150 "\n"
151
Simon Butcher604d3992016-10-06 19:02:49 +0100152#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
153 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
154 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000155int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000156{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
Simon Butcher604d3992016-10-06 19:02:49 +0100158 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
159 "MBEDTLS_PEM_WRITE_C"
Rich Evans18b78c72015-02-11 14:06:19 +0000160 "not defined.\n" );
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200161 mbedtls_exit( 0 );
Rich Evans18b78c72015-02-11 14:06:19 +0000162}
163#else
164/*
165 * global options
166 */
167struct options
168{
169 int type; /* the type of key to generate */
170 int rsa_keysize; /* length of key in bits */
171 int ec_curve; /* curve identifier for EC keys */
172 const char *filename; /* filename of the key file */
173 int format; /* the output format to use */
174 int use_dev_random; /* use /dev/random as entropy source */
175} opt;
176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200178{
179 int ret;
180 FILE *f;
181 unsigned char output_buf[16000];
182 unsigned char *c = output_buf;
183 size_t len = 0;
184
185 memset(output_buf, 0, 16000);
186 if( opt.format == FORMAT_PEM )
187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200189 return( ret );
190
191 len = strlen( (char *) output_buf );
192 }
193 else
194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200196 return( ret );
197
198 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200199 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200200 }
201
Paul Bakker3966d712014-07-10 12:03:09 +0200202 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200203 return( -1 );
204
205 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200206 {
207 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200208 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200209 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200210
Paul Bakker0c226102014-04-17 16:02:36 +0200211 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200212
213 return( 0 );
214}
215
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200216int main( int argc, char *argv[] )
217{
Andres Amaya Garcia7c55e792018-04-29 19:51:56 +0100218 int ret = 1;
219 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200221 char buf[1024];
222 int i;
223 char *p, *q;
Hanno Becker83aad1f2017-08-23 06:45:10 +0100224 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_entropy_context entropy;
226 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200227 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#if defined(MBEDTLS_ECP_C)
229 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100230#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200231
232 /*
233 * Set to sane values
234 */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100235
236 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
237 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
238 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200241 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200242 memset( buf, 0, sizeof( buf ) );
243
244 if( argc == 0 )
245 {
246 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 mbedtls_printf( USAGE );
248#if defined(MBEDTLS_ECP_C)
James Cowgill07a92d72015-10-09 00:28:14 +0100249 mbedtls_printf( " available ec_curve values:\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 curve_info = mbedtls_ecp_curve_list();
251 mbedtls_printf( " %s (default)\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100252 while( ( ++curve_info )->name != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 mbedtls_printf( " %s\n", curve_info->name );
Andres Amaya Garcia7c55e792018-04-29 19:51:56 +0100254#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200255 goto exit;
256 }
257
258 opt.type = DFL_TYPE;
259 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100260 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200261 opt.filename = DFL_FILENAME;
262 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200263 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200264
265 for( i = 1; i < argc; i++ )
266 {
267 p = argv[i];
268 if( ( q = strchr( p, '=' ) ) == NULL )
269 goto usage;
270 *q++ = '\0';
271
272 if( strcmp( p, "type" ) == 0 )
273 {
274 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 opt.type = MBEDTLS_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100276 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 opt.type = MBEDTLS_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200278 else
279 goto usage;
280 }
281 else if( strcmp( p, "format" ) == 0 )
282 {
283 if( strcmp( q, "pem" ) == 0 )
284 opt.format = FORMAT_PEM;
285 else if( strcmp( q, "der" ) == 0 )
286 opt.format = FORMAT_DER;
287 else
288 goto usage;
289 }
290 else if( strcmp( p, "rsa_keysize" ) == 0 )
291 {
292 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200293 if( opt.rsa_keysize < 1024 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200295 goto usage;
296 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100298 else if( strcmp( p, "ec_curve" ) == 0 )
299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300 if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100301 goto usage;
302 opt.ec_curve = curve_info->grp_id;
303 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200304#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200305 else if( strcmp( p, "filename" ) == 0 )
306 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200307 else if( strcmp( p, "use_dev_random" ) == 0 )
308 {
309 opt.use_dev_random = atoi( q );
310 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
311 goto usage;
312 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200313 else
314 goto usage;
315 }
316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200318 fflush( stdout );
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320 mbedtls_entropy_init( &entropy );
321#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Paul Bakker1cfc4582014-04-09 15:25:13 +0200322 if( opt.use_dev_random )
323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200325 NULL, DEV_RANDOM_THRESHOLD,
326 MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
Paul Bakker1cfc4582014-04-09 15:25:13 +0200327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200329 goto exit;
330 }
331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 mbedtls_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200333 fflush( stdout );
334 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200336
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200337 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200338 (const unsigned char *) pers,
339 strlen( pers ) ) ) != 0 )
340 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200341 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200342 goto exit;
343 }
344
345 /*
346 * 1.1. Generate the key
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200349 fflush( stdout );
350
Hanno Beckerdc631fb2018-11-05 16:54:40 +0000351 if( ( ret = mbedtls_pk_setup( &key,
352 mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200353 {
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200354 mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100355 goto exit;
356 }
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
359 if( opt.type == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
Hanno Becker83aad1f2017-08-23 06:45:10 +0100362 opt.rsa_keysize, 65537 );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200363 if( ret != 0 )
364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200366 goto exit;
367 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200368 }
369 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370#endif /* MBEDTLS_RSA_C */
371#if defined(MBEDTLS_ECP_C)
372 if( opt.type == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200373 {
Hanno Beckerdc631fb2018-11-05 16:54:40 +0000374 ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve,
375 mbedtls_pk_ec( key ),
Hanno Becker83aad1f2017-08-23 06:45:10 +0100376 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100377 if( ret != 0 )
378 {
Chris Xue9a51c032017-11-05 19:10:51 +0000379 mbedtls_printf( " failed\n ! mbedtls_ecp_gen_key returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100380 goto exit;
381 }
382 }
383 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100385 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200386 mbedtls_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200387 goto exit;
388 }
389
390 /*
391 * 1.2 Print the key
392 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#if defined(MBEDTLS_RSA_C)
396 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200397 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
Hanno Becker83aad1f2017-08-23 06:45:10 +0100399
400 if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
401 ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 )
402 {
403 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
404 goto exit;
405 }
406
407 mbedtls_mpi_write_file( "N: ", &N, 16, NULL );
408 mbedtls_mpi_write_file( "E: ", &E, 16, NULL );
409 mbedtls_mpi_write_file( "D: ", &D, 16, NULL );
410 mbedtls_mpi_write_file( "P: ", &P, 16, NULL );
411 mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL );
412 mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL );
413 mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL );
414 mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200415 }
416 else
417#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_ECP_C)
419 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
422 mbedtls_printf( "curve: %s\n",
423 mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
424 mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
425 mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
426 mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200427 }
428 else
429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200431
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200432 /*
433 * 1.3 Export key
434 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435 mbedtls_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200436
437 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200440 goto exit;
441 }
442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200443 mbedtls_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200444
Andres Amaya Garcia7c55e792018-04-29 19:51:56 +0100445 exit_code = MBEDTLS_EXIT_SUCCESS;
446
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200447exit:
448
Andres Amaya Garcia7c55e792018-04-29 19:51:56 +0100449 if( exit_code != MBEDTLS_EXIT_SUCCESS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451#ifdef MBEDTLS_ERROR_C
452 mbedtls_strerror( ret, buf, sizeof( buf ) );
453 mbedtls_printf( " - %s\n", buf );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200454#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200456#endif
457 }
458
Hanno Becker83aad1f2017-08-23 06:45:10 +0100459 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
460 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
461 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_pk_free( &key );
464 mbedtls_ctr_drbg_free( &ctr_drbg );
465 mbedtls_entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200466
467#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200468 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200469 fflush( stdout ); getchar();
470#endif
471
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200472 mbedtls_exit( exit_code );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200473}
Simon Butcher604d3992016-10-06 19:02:49 +0100474#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
475 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */