blob: f60cad762829d0da3e700960508987d2c434cd47 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
124 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +0000125int main( void )
Rich Evans18b78c72015-02-11 14:06:19 +0000126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
128 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
Rich Evans18b78c72015-02-11 14:06:19 +0000129 "not defined.\n" );
130 return( 0 );
131}
132#else
133/*
134 * global options
135 */
136struct options
137{
138 int type; /* the type of key to generate */
139 int rsa_keysize; /* length of key in bits */
140 int ec_curve; /* curve identifier for EC keys */
141 const char *filename; /* filename of the key file */
142 int format; /* the output format to use */
143 int use_dev_random; /* use /dev/random as entropy source */
144} opt;
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146static int write_private_key( mbedtls_pk_context *key, const char *output_file )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200147{
148 int ret;
149 FILE *f;
150 unsigned char output_buf[16000];
151 unsigned char *c = output_buf;
152 size_t len = 0;
153
154 memset(output_buf, 0, 16000);
155 if( opt.format == FORMAT_PEM )
156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200158 return( ret );
159
160 len = strlen( (char *) output_buf );
161 }
162 else
163 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200165 return( ret );
166
167 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200168 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200169 }
170
Paul Bakker3966d712014-07-10 12:03:09 +0200171 if( ( f = fopen( output_file, "wb" ) ) == NULL )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200172 return( -1 );
173
174 if( fwrite( c, 1, len, f ) != len )
Paul Bakker0c226102014-04-17 16:02:36 +0200175 {
176 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200177 return( -1 );
Paul Bakker0c226102014-04-17 16:02:36 +0200178 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200179
Paul Bakker0c226102014-04-17 16:02:36 +0200180 fclose( f );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200181
182 return( 0 );
183}
184
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200185int main( int argc, char *argv[] )
186{
187 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200189 char buf[1024];
190 int i;
191 char *p, *q;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_entropy_context entropy;
193 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200194 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#if defined(MBEDTLS_ECP_C)
196 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100197#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200198
199 /*
200 * Set to sane values
201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_pk_init( &key );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200203 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200204 memset( buf, 0, sizeof( buf ) );
205
206 if( argc == 0 )
207 {
208 usage:
209 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_printf( USAGE );
211#if defined(MBEDTLS_ECP_C)
212 mbedtls_printf( " availabled ec_curve values:\n" );
213 curve_info = mbedtls_ecp_curve_list();
214 mbedtls_printf( " %s (default)\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100215 while( ( ++curve_info )->name != NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 mbedtls_printf( " %s\n", curve_info->name );
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100217#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200218 goto exit;
219 }
220
221 opt.type = DFL_TYPE;
222 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100223 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200224 opt.filename = DFL_FILENAME;
225 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200226 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200227
228 for( i = 1; i < argc; i++ )
229 {
230 p = argv[i];
231 if( ( q = strchr( p, '=' ) ) == NULL )
232 goto usage;
233 *q++ = '\0';
234
235 if( strcmp( p, "type" ) == 0 )
236 {
237 if( strcmp( q, "rsa" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 opt.type = MBEDTLS_PK_RSA;
Paul Bakker247b4872014-02-06 14:33:52 +0100239 else if( strcmp( q, "ec" ) == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 opt.type = MBEDTLS_PK_ECKEY;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200241 else
242 goto usage;
243 }
244 else if( strcmp( p, "format" ) == 0 )
245 {
246 if( strcmp( q, "pem" ) == 0 )
247 opt.format = FORMAT_PEM;
248 else if( strcmp( q, "der" ) == 0 )
249 opt.format = FORMAT_DER;
250 else
251 goto usage;
252 }
253 else if( strcmp( p, "rsa_keysize" ) == 0 )
254 {
255 opt.rsa_keysize = atoi( q );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200256 if( opt.rsa_keysize < 1024 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200258 goto usage;
259 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100261 else if( strcmp( p, "ec_curve" ) == 0 )
262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL )
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100264 goto usage;
265 opt.ec_curve = curve_info->grp_id;
266 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200267#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200268 else if( strcmp( p, "filename" ) == 0 )
269 opt.filename = q;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200270 else if( strcmp( p, "use_dev_random" ) == 0 )
271 {
272 opt.use_dev_random = atoi( q );
273 if( opt.use_dev_random < 0 || opt.use_dev_random > 1 )
274 goto usage;
275 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200276 else
277 goto usage;
278 }
279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200281 fflush( stdout );
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_entropy_init( &entropy );
284#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Paul Bakker1cfc4582014-04-09 15:25:13 +0200285 if( opt.use_dev_random )
286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll,
Manuel Pégourié-Gonnard7580ba42015-06-19 10:26:32 +0200288 NULL, DEV_RANDOM_THRESHOLD,
289 MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 )
Paul Bakker1cfc4582014-04-09 15:25:13 +0200290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200291 mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", -ret );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200292 goto exit;
293 }
294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 mbedtls_printf("\n Using /dev/random, so can take a long time! " );
Paul Bakker1cfc4582014-04-09 15:25:13 +0200296 fflush( stdout );
297 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200299
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200300 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200301 (const unsigned char *) pers,
302 strlen( pers ) ) ) != 0 )
303 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200304 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200305 goto exit;
306 }
307
308 /*
309 * 1.1. Generate the key
310 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 mbedtls_printf( "\n . Generating the private key ..." );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200312 fflush( stdout );
313
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200314 if( ( ret = mbedtls_pk_setup( &key, mbedtls_pk_info_from_type( opt.type ) ) ) != 0 )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200315 {
Manuel Pégourié-Gonnardd9e6a3a2015-05-14 19:41:36 +0200316 mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100317 goto exit;
318 }
319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
321 if( opt.type == MBEDTLS_PK_RSA )
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200323 ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg,
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200324 opt.rsa_keysize, 65537 );
325 if( ret != 0 )
326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200328 goto exit;
329 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200330 }
331 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332#endif /* MBEDTLS_RSA_C */
333#if defined(MBEDTLS_ECP_C)
334 if( opt.type == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200335 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 ret = mbedtls_ecp_gen_key( opt.ec_curve, mbedtls_pk_ec( key ),
337 mbedtls_ctr_drbg_random, &ctr_drbg );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100338 if( ret != 0 )
339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret );
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100341 goto exit;
342 }
343 }
344 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347 mbedtls_printf( " failed\n ! key type not supported\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200348 goto exit;
349 }
350
351 /*
352 * 1.2 Print the key
353 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354 mbedtls_printf( " ok\n . Key information:\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#if defined(MBEDTLS_RSA_C)
357 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key );
360 mbedtls_mpi_write_file( "N: ", &rsa->N, 16, NULL );
361 mbedtls_mpi_write_file( "E: ", &rsa->E, 16, NULL );
362 mbedtls_mpi_write_file( "D: ", &rsa->D, 16, NULL );
363 mbedtls_mpi_write_file( "P: ", &rsa->P, 16, NULL );
364 mbedtls_mpi_write_file( "Q: ", &rsa->Q, 16, NULL );
365 mbedtls_mpi_write_file( "DP: ", &rsa->DP, 16, NULL );
366 mbedtls_mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL );
367 mbedtls_mpi_write_file( "QP: ", &rsa->QP, 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200368 }
369 else
370#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371#if defined(MBEDTLS_ECP_C)
372 if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY )
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200374 mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key );
375 mbedtls_printf( "curve: %s\n",
376 mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name );
377 mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL );
378 mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL );
379 mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200380 }
381 else
382#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200384
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200385 /*
386 * 1.3 Export key
387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388 mbedtls_printf( " . Writing key to file..." );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200389
390 if( ( ret = write_private_key( &key, opt.filename ) ) != 0 )
391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_printf( " failed\n" );
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200393 goto exit;
394 }
395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_printf( " ok\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200397
398exit:
399
400 if( ret != 0 && ret != 1)
401 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402#ifdef MBEDTLS_ERROR_C
403 mbedtls_strerror( ret, buf, sizeof( buf ) );
404 mbedtls_printf( " - %s\n", buf );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200405#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200407#endif
408 }
409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 mbedtls_pk_free( &key );
411 mbedtls_ctr_drbg_free( &ctr_drbg );
412 mbedtls_entropy_free( &entropy );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200413
414#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200416 fflush( stdout ); getchar();
417#endif
418
419 return( ret );
420}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */