blob: a58a80e3610c8c6902dd1b2d24ce5f40650fdbb4 [file] [log] [blame]
Paul Bakker15b9b3a2013-09-23 12:05:44 +02001/*
2 * Key generation application
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker15b9b3a2013-09-23 12:05:44 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker15b9b3a2013-09-23 12:05:44 +02007 *
Paul Bakker15b9b3a2013-09-23 12:05:44 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +020028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \
37 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/error.h"
39#include "mbedtls/pk.h"
40#include "mbedtls/ecdsa.h"
41#include "mbedtls/rsa.h"
42#include "mbedtls/error.h"
43#include "mbedtls/entropy.h"
44#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020045
Rich Evans18b78c72015-02-11 14:06:19 +000046#include <stdio.h>
47#include <stdlib.h>
48#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020049
Rich Evans18b78c72015-02-11 14:06:19 +000050#if !defined(_WIN32)
51#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020052
53#define DEV_RANDOM_THRESHOLD 32
54
55int dev_random_entropy_poll( void *data, unsigned char *output,
56 size_t len, size_t *olen )
57{
58 FILE *file;
59 size_t ret, left = len;
60 unsigned char *p = output;
61 ((void) data);
62
63 *olen = 0;
64
65 file = fopen( "/dev/random", "rb" );
66 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020068
69 while( left > 0 )
70 {
71 /* /dev/random can return much less than requested. If so, try again */
72 ret = fread( p, 1, left, file );
73 if( ret == 0 && ferror( file ) )
74 {
75 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker1cfc4582014-04-09 15:25:13 +020077 }
78
79 p += ret;
80 left -= ret;
81 sleep( 1 );
82 }
83 fclose( file );
84 *olen = len;
85
86 return( 0 );
87}
Rich Evans18b78c72015-02-11 14:06:19 +000088#endif /* !_WIN32 */
89#endif
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091#if defined(MBEDTLS_ECP_C)
92#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000093#else
94#define DFL_EC_CURVE 0
95#endif
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000098#define USAGE_DEV_RANDOM \
99 " use_dev_random=0|1 default: 0\n"
100#else
101#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200103
Rich Evans18b78c72015-02-11 14:06:19 +0000104#define FORMAT_PEM 0
105#define FORMAT_DER 1
106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +0000108#define DFL_RSA_KEYSIZE 4096
109#define DFL_FILENAME "keyfile.key"
110#define DFL_FORMAT FORMAT_PEM
111#define DFL_USE_DEV_RANDOM 0
112
113#define USAGE \
114 "\n usage: gen_key param=<>...\n" \
115 "\n acceptable parameters:\n" \
116 " type=rsa|ec default: rsa\n" \
117 " rsa_keysize=%%d default: 4096\n" \
118 " ec_curve=%%s see below\n" \
119 " filename=%%s default: keyfile.key\n" \
120 " format=pem|der default: pem\n" \
121 USAGE_DEV_RANDOM \
122 "\n"
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
125 !defined(MBEDTLS_ENTROPY_C) || !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 "
129 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
Rich Evans18b78c72015-02-11 14:06:19 +0000130 "not defined.\n" );
131 return( 0 );
132}
133#else
134/*
135 * global options
136 */
137struct options
138{
139 int type; /* the type of key to generate */
140 int rsa_keysize; /* length of key in bits */
141 int ec_curve; /* curve identifier for EC keys */
142 const char *filename; /* filename of the key file */
143 int format; /* the output format to use */
144 int use_dev_random; /* use /dev/random as entropy source */
145} opt;
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200148{
149 int ret;
150 FILE *f;
151 unsigned char output_buf[16000];
152 unsigned char *c = output_buf;
153 size_t len = 0;
154
155 memset(output_buf, 0, 16000);
156 if( opt.format == FORMAT_PEM )
157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200159 return( ret );
160
161 len = strlen( (char *) output_buf );
162 }
163 else
164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200166 return( ret );
167
168 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200169 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200170 }
171
Paul Bakker3966d712014-07-10 12:03:09 +0200172 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200173 return( -1 );
174
175 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200176 {
177 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200178 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200179 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200180
Paul Bakker0c226102014-04-17 16:02:36 +0200181 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200182
183 return( 0 );
184}
185
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200186int main( int argc, char *argv[] )
187{
188 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200190 char buf[1024];
191 int i;
192 char *p, *q;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_entropy_context entropy;
194 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200195 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#if defined(MBEDTLS_ECP_C)
197 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100198#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200199
200 /*
201 * Set to sane values
202 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200204 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200205 memset( buf, 0, sizeof( buf ) );
206
207 if( argc == 0 )
208 {
209 usage:
210 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 mbedtls_printf( USAGE );
212#if defined(MBEDTLS_ECP_C)
213 mbedtls_printf( " availabled ec_curve values:\n" );
214 curve_info = mbedtls_ecp_curve_list();
215 mbedtls_printf( " %s (default)\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100216 while( ( ++curve_info )->name != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_printf( " %s\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100218#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200219 goto exit;
220 }
221
222 opt.type = DFL_TYPE;
223 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100224 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200225 opt.filename = DFL_FILENAME;
226 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200227 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200228
229 for( i = 1; i < argc; i++ )
230 {
231 p = argv[i];
232 if( ( q = strchr( p, '=' ) ) == NULL )
233 goto usage;
234 *q++ = '\0';
235
236 if( strcmp( p, "type" ) == 0 )
237 {
238 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 opt.type = MBEDTLS_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100240 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 opt.type = MBEDTLS_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200242 else
243 goto usage;
244 }
245 else if( strcmp( p, "format" ) == 0 )
246 {
247 if( strcmp( q, "pem" ) == 0 )
248 opt.format = FORMAT_PEM;
249 else if( strcmp( q, "der" ) == 0 )
250 opt.format = FORMAT_DER;
251 else
252 goto usage;
253 }
254 else if( strcmp( p, "rsa_keysize" ) == 0 )
255 {
256 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200257 if( opt.rsa_keysize < 1024 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200259 goto usage;
260 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100262 else if( strcmp( p, "ec_curve" ) == 0 )
263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100265 goto usage;
266 opt.ec_curve = curve_info->grp_id;
267 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200268#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200269 else if( strcmp( p, "filename" ) == 0 )
270 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200271 else if( strcmp( p, "use_dev_random" ) == 0 )
272 {
273 opt.use_dev_random = atoi( q );
274 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
275 goto usage;
276 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200277 else
278 goto usage;
279 }
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200282 fflush( stdout );
283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284 mbedtls_entropy_init( &entropy );
285#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Paul Bakker1cfc4582014-04-09 15:25:13 +0200286 if( opt.use_dev_random )
287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200289 NULL, DEV_RANDOM_THRESHOLD,
290 MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
Paul Bakker1cfc4582014-04-09 15:25:13 +0200291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200293 goto exit;
294 }
295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200297 fflush( stdout );
298 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200300
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200301 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200302 (const unsigned char *) pers,
303 strlen( pers ) ) ) != 0 )
304 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200305 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200306 goto exit;
307 }
308
309 /*
310 * 1.1. Generate the key
311 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 mbedtls_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200313 fflush( stdout );
314
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200315 if( ( ret = mbedtls_pk_setup( &key, mbedtls_pk_info_from_type( opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200316 {
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200317 mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100318 goto exit;
319 }
320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200321#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
322 if( opt.type == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200325 opt.rsa_keysize, 65537 );
326 if( ret != 0 )
327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200329 goto exit;
330 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200331 }
332 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333#endif /* MBEDTLS_RSA_C */
334#if defined(MBEDTLS_ECP_C)
335 if( opt.type == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 ret = mbedtls_ecp_gen_key( opt.ec_curve, mbedtls_pk_ec( key ),
338 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100339 if( ret != 0 )
340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100342 goto exit;
343 }
344 }
345 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200349 goto exit;
350 }
351
352 /*
353 * 1.2 Print the key
354 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 mbedtls_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_RSA_C)
358 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
361 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
362 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
363 mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
364 mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
365 mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
366 mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
367 mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
368 mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200369 }
370 else
371#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#if defined(MBEDTLS_ECP_C)
373 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
376 mbedtls_printf( "curve: %s\n",
377 mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
378 mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
379 mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
380 mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200381 }
382 else
383#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200385
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200386 /*
387 * 1.3 Export key
388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200390
391 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
392 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200394 goto exit;
395 }
396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200398
399exit:
400
401 if( ret != 0 && ret != 1)
402 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#ifdef MBEDTLS_ERROR_C
404 mbedtls_strerror( ret, buf, sizeof( buf ) );
405 mbedtls_printf( " - %s\n", buf );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200406#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200408#endif
409 }
410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 mbedtls_pk_free( &key );
412 mbedtls_ctr_drbg_free( &ctr_drbg );
413 mbedtls_entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200414
415#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200417 fflush( stdout ); getchar();
418#endif
419
420 return( ret );
421}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */