blob: da7e0d072d6e283e64365850f201379e1ff9d528 [file] [log] [blame]
Paul Bakker15b9b3a2013-09-23 12:05:44 +02001/*
2 * Key generation application
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker15b9b3a2013-09-23 12:05:44 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker15b9b3a2013-09-23 12:05:44 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#endif
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
36 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/error.h"
38#include "mbedtls/pk.h"
39#include "mbedtls/ecdsa.h"
40#include "mbedtls/rsa.h"
41#include "mbedtls/error.h"
42#include "mbedtls/entropy.h"
43#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020044
Rich Evans18b78c72015-02-11 14:06:19 +000045#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020048
Rich Evans18b78c72015-02-11 14:06:19 +000049#if !defined(_WIN32)
50#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020051
52#define DEV_RANDOM_THRESHOLD 32
53
54int dev_random_entropy_poll( void *data, unsigned char *output,
55 size_t len, size_t *olen )
56{
57 FILE *file;
58 size_t ret, left = len;
59 unsigned char *p = output;
60 ((void) data);
61
62 *olen = 0;
63
64 file = fopen( "/dev/random", "rb" );
65 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020067
68 while( left > 0 )
69 {
70 /* /dev/random can return much less than requested. If so, try again */
71 ret = fread( p, 1, left, file );
72 if( ret == 0 && ferror( file ) )
73 {
74 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020076 }
77
78 p += ret;
79 left -= ret;
80 sleep( 1 );
81 }
82 fclose( file );
83 *olen = len;
84
85 return( 0 );
86}
Rich Evans18b78c72015-02-11 14:06:19 +000087#endif /* !_WIN32 */
88#endif
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#if defined(MBEDTLS_ECP_C)
91#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000092#else
93#define DFL_EC_CURVE 0
94#endif
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000097#define USAGE_DEV_RANDOM \
98 " use_dev_random=0|1 default: 0\n"
99#else
100#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200102
Rich Evans18b78c72015-02-11 14:06:19 +0000103#define FORMAT_PEM 0
104#define FORMAT_DER 1
105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +0000107#define DFL_RSA_KEYSIZE 4096
108#define DFL_FILENAME "keyfile.key"
109#define DFL_FORMAT FORMAT_PEM
110#define DFL_USE_DEV_RANDOM 0
111
112#define USAGE \
113 "\n usage: gen_key param=<>...\n" \
114 "\n acceptable parameters:\n" \
115 " type=rsa|ec default: rsa\n" \
116 " rsa_keysize=%%d default: 4096\n" \
117 " ec_curve=%%s see below\n" \
118 " filename=%%s default: keyfile.key\n" \
119 " format=pem|der default: pem\n" \
120 USAGE_DEV_RANDOM \
121 "\n"
122
Simon Butcher411fd7c2016-10-06 19:02:49 +0100123#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
124 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
125 !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000126int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
Simon Butcher411fd7c2016-10-06 19:02:49 +0100129 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
130 "MBEDTLS_PEM_WRITE_C"
Rich Evans18b78c72015-02-11 14:06:19 +0000131 "not defined.\n" );
132 return( 0 );
133}
134#else
135/*
136 * global options
137 */
138struct options
139{
140 int type; /* the type of key to generate */
141 int rsa_keysize; /* length of key in bits */
142 int ec_curve; /* curve identifier for EC keys */
143 const char *filename; /* filename of the key file */
144 int format; /* the output format to use */
145 int use_dev_random; /* use /dev/random as entropy source */
146} opt;
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200149{
150 int ret;
151 FILE *f;
152 unsigned char output_buf[16000];
153 unsigned char *c = output_buf;
154 size_t len = 0;
155
156 memset(output_buf, 0, 16000);
157 if( opt.format == FORMAT_PEM )
158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200160 return( ret );
161
162 len = strlen( (char *) output_buf );
163 }
164 else
165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200167 return( ret );
168
169 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200170 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200171 }
172
Paul Bakker3966d712014-07-10 12:03:09 +0200173 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200174 return( -1 );
175
176 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200177 {
178 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200179 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200180 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200181
Paul Bakker0c226102014-04-17 16:02:36 +0200182 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200183
184 return( 0 );
185}
186
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200187int main( int argc, char *argv[] )
188{
189 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200191 char buf[1024];
192 int i;
193 char *p, *q;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_entropy_context entropy;
195 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200196 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_ECP_C)
198 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100199#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200200
201 /*
202 * Set to sane values
203 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200205 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200206 memset( buf, 0, sizeof( buf ) );
207
208 if( argc == 0 )
209 {
210 usage:
211 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_printf( USAGE );
213#if defined(MBEDTLS_ECP_C)
James Cowgill6bfa1d82015-10-09 00:28:14 +0100214 mbedtls_printf( " available ec_curve values:\n" );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215 curve_info = mbedtls_ecp_curve_list();
216 mbedtls_printf( " %s (default)\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100217 while( ( ++curve_info )->name != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_printf( " %s\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100219#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200220 goto exit;
221 }
222
223 opt.type = DFL_TYPE;
224 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100225 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200226 opt.filename = DFL_FILENAME;
227 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200228 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200229
230 for( i = 1; i < argc; i++ )
231 {
232 p = argv[i];
233 if( ( q = strchr( p, '=' ) ) == NULL )
234 goto usage;
235 *q++ = '\0';
236
237 if( strcmp( p, "type" ) == 0 )
238 {
239 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 opt.type = MBEDTLS_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100241 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 opt.type = MBEDTLS_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200243 else
244 goto usage;
245 }
246 else if( strcmp( p, "format" ) == 0 )
247 {
248 if( strcmp( q, "pem" ) == 0 )
249 opt.format = FORMAT_PEM;
250 else if( strcmp( q, "der" ) == 0 )
251 opt.format = FORMAT_DER;
252 else
253 goto usage;
254 }
255 else if( strcmp( p, "rsa_keysize" ) == 0 )
256 {
257 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200258 if( opt.rsa_keysize < 1024 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200260 goto usage;
261 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100263 else if( strcmp( p, "ec_curve" ) == 0 )
264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100266 goto usage;
267 opt.ec_curve = curve_info->grp_id;
268 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200269#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200270 else if( strcmp( p, "filename" ) == 0 )
271 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200272 else if( strcmp( p, "use_dev_random" ) == 0 )
273 {
274 opt.use_dev_random = atoi( q );
275 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
276 goto usage;
277 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200278 else
279 goto usage;
280 }
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200283 fflush( stdout );
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_entropy_init( &entropy );
286#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Paul Bakker1cfc4582014-04-09 15:25:13 +0200287 if( opt.use_dev_random )
288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200290 NULL, DEV_RANDOM_THRESHOLD,
291 MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
Paul Bakker1cfc4582014-04-09 15:25:13 +0200292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200294 goto exit;
295 }
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200298 fflush( stdout );
299 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200301
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200302 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200303 (const unsigned char *) pers,
304 strlen( pers ) ) ) != 0 )
305 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200306 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200307 goto exit;
308 }
309
310 /*
311 * 1.1. Generate the key
312 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200314 fflush( stdout );
315
Hanno Beckerd3e42442018-11-05 16:54:40 +0000316 if( ( ret = mbedtls_pk_setup( &key,
317 mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200318 {
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200319 mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100320 goto exit;
321 }
322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
324 if( opt.type == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200327 opt.rsa_keysize, 65537 );
328 if( ret != 0 )
329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200331 goto exit;
332 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200333 }
334 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#endif /* MBEDTLS_RSA_C */
336#if defined(MBEDTLS_ECP_C)
337 if( opt.type == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200338 {
Hanno Beckerd3e42442018-11-05 16:54:40 +0000339 ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve,
340 mbedtls_pk_ec( key ),
341 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100342 if( ret != 0 )
343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100345 goto exit;
346 }
347 }
348 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351 mbedtls_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200352 goto exit;
353 }
354
355 /*
356 * 1.2 Print the key
357 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 mbedtls_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360#if defined(MBEDTLS_RSA_C)
361 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
364 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
365 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
366 mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
367 mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
368 mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
369 mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
370 mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
371 mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200372 }
373 else
374#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#if defined(MBEDTLS_ECP_C)
376 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
379 mbedtls_printf( "curve: %s\n",
380 mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
381 mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
382 mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
383 mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200384 }
385 else
386#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200388
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200389 /*
390 * 1.3 Export key
391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200393
394 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200397 goto exit;
398 }
399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 mbedtls_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200401
402exit:
403
404 if( ret != 0 && ret != 1)
405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#ifdef MBEDTLS_ERROR_C
407 mbedtls_strerror( ret, buf, sizeof( buf ) );
408 mbedtls_printf( " - %s\n", buf );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200409#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200411#endif
412 }
413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 mbedtls_pk_free( &key );
415 mbedtls_ctr_drbg_free( &ctr_drbg );
416 mbedtls_entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200417
418#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200419 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200420 fflush( stdout ); getchar();
421#endif
422
423 return( ret );
424}
Simon Butcher411fd7c2016-10-06 19:02:49 +0100425#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO &&
426 * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */