Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Key generation application |
| 3 | * |
| 4 | * Copyright (C) 2006-2013, Brainspark B.V. |
| 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 27 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
| 29 | #include POLARSSL_CONFIG_FILE |
| 30 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 31 | |
| 32 | #include <string.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <stdio.h> |
| 35 | |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 36 | #if !defined(_WIN32) && defined(POLARSSL_FS_IO) |
| 37 | #include <unistd.h> |
| 38 | #endif /* !_WIN32 && POLARSSL_FS_IO */ |
| 39 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 40 | #include "polarssl/error.h" |
| 41 | #include "polarssl/pk.h" |
| 42 | #include "polarssl/ecdsa.h" |
| 43 | #include "polarssl/rsa.h" |
| 44 | #include "polarssl/error.h" |
| 45 | #include "polarssl/entropy.h" |
| 46 | #include "polarssl/ctr_drbg.h" |
| 47 | |
| 48 | #if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) || \ |
| 49 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) |
| 50 | int main( int argc, char *argv[] ) |
| 51 | { |
| 52 | ((void) argc); |
| 53 | ((void) argv); |
| 54 | |
| 55 | printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO and/or " |
| 56 | "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C " |
| 57 | "not defined.\n" ); |
| 58 | return( 0 ); |
| 59 | } |
| 60 | #else |
| 61 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 62 | #define FORMAT_PEM 0 |
| 63 | #define FORMAT_DER 1 |
| 64 | |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 65 | #define DFL_TYPE POLARSSL_PK_RSA |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 66 | #define DFL_RSA_KEYSIZE 4096 |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 67 | #define DFL_EC_CURVE ecp_curve_list()->grp_id |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 68 | #define DFL_FILENAME "keyfile.key" |
| 69 | #define DFL_FORMAT FORMAT_PEM |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 70 | #define DFL_USE_DEV_RANDOM 0 |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 71 | |
| 72 | /* |
| 73 | * global options |
| 74 | */ |
| 75 | struct options |
| 76 | { |
| 77 | int type; /* the type of key to generate */ |
| 78 | int rsa_keysize; /* length of key in bits */ |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 79 | int ec_curve; /* curve identifier for EC keys */ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 80 | const char *filename; /* filename of the key file */ |
| 81 | int format; /* the output format to use */ |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 82 | int use_dev_random; /* use /dev/random as entropy source */ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 83 | } opt; |
| 84 | |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 85 | #if !defined(_WIN32) && defined(POLARSSL_FS_IO) |
| 86 | |
| 87 | #define DEV_RANDOM_THRESHOLD 32 |
| 88 | |
| 89 | int dev_random_entropy_poll( void *data, unsigned char *output, |
| 90 | size_t len, size_t *olen ) |
| 91 | { |
| 92 | FILE *file; |
| 93 | size_t ret, left = len; |
| 94 | unsigned char *p = output; |
| 95 | ((void) data); |
| 96 | |
| 97 | *olen = 0; |
| 98 | |
| 99 | file = fopen( "/dev/random", "rb" ); |
| 100 | if( file == NULL ) |
| 101 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 102 | |
| 103 | while( left > 0 ) |
| 104 | { |
| 105 | /* /dev/random can return much less than requested. If so, try again */ |
| 106 | ret = fread( p, 1, left, file ); |
| 107 | if( ret == 0 && ferror( file ) ) |
| 108 | { |
| 109 | fclose( file ); |
| 110 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 111 | } |
| 112 | |
| 113 | p += ret; |
| 114 | left -= ret; |
| 115 | sleep( 1 ); |
| 116 | } |
| 117 | fclose( file ); |
| 118 | *olen = len; |
| 119 | |
| 120 | return( 0 ); |
| 121 | } |
| 122 | #endif /* !_WIN32 && POLARSSL_FS_IO */ |
| 123 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 124 | static int write_private_key( pk_context *key, const char *output_file ) |
| 125 | { |
| 126 | int ret; |
| 127 | FILE *f; |
| 128 | unsigned char output_buf[16000]; |
| 129 | unsigned char *c = output_buf; |
| 130 | size_t len = 0; |
| 131 | |
| 132 | memset(output_buf, 0, 16000); |
| 133 | if( opt.format == FORMAT_PEM ) |
| 134 | { |
| 135 | if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 ) |
| 136 | return( ret ); |
| 137 | |
| 138 | len = strlen( (char *) output_buf ); |
| 139 | } |
| 140 | else |
| 141 | { |
| 142 | if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 ) |
| 143 | return( ret ); |
| 144 | |
| 145 | len = ret; |
Paul Bakker | 3c38f29 | 2014-06-13 17:37:46 +0200 | [diff] [blame] | 146 | c = output_buf + sizeof(output_buf) - len; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Paul Bakker | 3966d71 | 2014-07-10 12:03:09 +0200 | [diff] [blame] | 149 | if( ( f = fopen( output_file, "wb" ) ) == NULL ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 150 | return( -1 ); |
| 151 | |
| 152 | if( fwrite( c, 1, len, f ) != len ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 153 | { |
| 154 | fclose( f ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 155 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 156 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 157 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 158 | fclose( f ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 159 | |
| 160 | return( 0 ); |
| 161 | } |
| 162 | |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 163 | #if !defined(_WIN32) && defined(POLARSSL_FS_IO) |
| 164 | #define USAGE_DEV_RANDOM \ |
| 165 | " use_dev_random=0|1 default: 0\n" |
| 166 | #else |
| 167 | #define USAGE_DEV_RANDOM "" |
| 168 | #endif /* !_WIN32 && POLARSSL_FS_IO */ |
| 169 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 170 | #define USAGE \ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 171 | "\n usage: gen_key param=<>...\n" \ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 172 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 173 | " type=rsa|ec default: rsa\n" \ |
| 174 | " rsa_keysize=%%d default: 4096\n" \ |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 175 | " ec_curve=%%s see below\n" \ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 176 | " filename=%%s default: keyfile.key\n" \ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 177 | " format=pem|der default: pem\n" \ |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 178 | USAGE_DEV_RANDOM \ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 179 | "\n" |
| 180 | |
| 181 | int main( int argc, char *argv[] ) |
| 182 | { |
| 183 | int ret = 0; |
| 184 | pk_context key; |
| 185 | char buf[1024]; |
| 186 | int i; |
| 187 | char *p, *q; |
| 188 | entropy_context entropy; |
| 189 | ctr_drbg_context ctr_drbg; |
| 190 | const char *pers = "gen_key"; |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 191 | #if defined(POLARSSL_ECP_C) |
| 192 | const ecp_curve_info *curve_info; |
| 193 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 194 | |
| 195 | /* |
| 196 | * Set to sane values |
| 197 | */ |
| 198 | pk_init( &key ); |
| 199 | memset( buf, 0, sizeof( buf ) ); |
| 200 | |
| 201 | if( argc == 0 ) |
| 202 | { |
| 203 | usage: |
| 204 | ret = 1; |
| 205 | printf( USAGE ); |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 206 | #if defined(POLARSSL_ECP_C) |
| 207 | printf( " availabled ec_curve values:\n" ); |
| 208 | curve_info = ecp_curve_list(); |
| 209 | printf( " %s (default)\n", curve_info->name ); |
| 210 | while( ( ++curve_info )->name != NULL ) |
| 211 | printf( " %s\n", curve_info->name ); |
| 212 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 213 | goto exit; |
| 214 | } |
| 215 | |
| 216 | opt.type = DFL_TYPE; |
| 217 | opt.rsa_keysize = DFL_RSA_KEYSIZE; |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 218 | opt.ec_curve = DFL_EC_CURVE; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 219 | opt.filename = DFL_FILENAME; |
| 220 | opt.format = DFL_FORMAT; |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 221 | opt.use_dev_random = DFL_USE_DEV_RANDOM; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 222 | |
| 223 | for( i = 1; i < argc; i++ ) |
| 224 | { |
| 225 | p = argv[i]; |
| 226 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 227 | goto usage; |
| 228 | *q++ = '\0'; |
| 229 | |
| 230 | if( strcmp( p, "type" ) == 0 ) |
| 231 | { |
| 232 | if( strcmp( q, "rsa" ) == 0 ) |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 233 | opt.type = POLARSSL_PK_RSA; |
Paul Bakker | 247b487 | 2014-02-06 14:33:52 +0100 | [diff] [blame] | 234 | else if( strcmp( q, "ec" ) == 0 ) |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 235 | opt.type = POLARSSL_PK_ECKEY; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 236 | else |
| 237 | goto usage; |
| 238 | } |
| 239 | else if( strcmp( p, "format" ) == 0 ) |
| 240 | { |
| 241 | if( strcmp( q, "pem" ) == 0 ) |
| 242 | opt.format = FORMAT_PEM; |
| 243 | else if( strcmp( q, "der" ) == 0 ) |
| 244 | opt.format = FORMAT_DER; |
| 245 | else |
| 246 | goto usage; |
| 247 | } |
| 248 | else if( strcmp( p, "rsa_keysize" ) == 0 ) |
| 249 | { |
| 250 | opt.rsa_keysize = atoi( q ); |
Manuel Pégourié-Gonnard | a39416f | 2014-07-21 17:10:16 +0200 | [diff] [blame^] | 251 | if( opt.rsa_keysize < 1024 || |
| 252 | opt.rsa_keysize > POLARSSL_MPI_MAX_BITS ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 253 | goto usage; |
| 254 | } |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 255 | else if( strcmp( p, "ec_curve" ) == 0 ) |
| 256 | { |
| 257 | if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL ) |
| 258 | goto usage; |
| 259 | opt.ec_curve = curve_info->grp_id; |
| 260 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 261 | else if( strcmp( p, "filename" ) == 0 ) |
| 262 | opt.filename = q; |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 263 | else if( strcmp( p, "use_dev_random" ) == 0 ) |
| 264 | { |
| 265 | opt.use_dev_random = atoi( q ); |
| 266 | if( opt.use_dev_random < 0 || opt.use_dev_random > 1 ) |
| 267 | goto usage; |
| 268 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 269 | else |
| 270 | goto usage; |
| 271 | } |
| 272 | |
| 273 | printf( "\n . Seeding the random number generator..." ); |
| 274 | fflush( stdout ); |
| 275 | |
| 276 | entropy_init( &entropy ); |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 277 | #if !defined(_WIN32) && defined(POLARSSL_FS_IO) |
| 278 | if( opt.use_dev_random ) |
| 279 | { |
| 280 | if( ( ret = entropy_add_source( &entropy, dev_random_entropy_poll, |
| 281 | NULL, DEV_RANDOM_THRESHOLD ) ) != 0 ) |
| 282 | { |
| 283 | printf( " failed\n ! entropy_add_source returned -0x%04x\n", -ret ); |
| 284 | goto exit; |
| 285 | } |
| 286 | |
| 287 | printf("\n Using /dev/random, so can take a long time! " ); |
| 288 | fflush( stdout ); |
| 289 | } |
| 290 | #endif /* !_WIN32 && POLARSSL_FS_IO */ |
| 291 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 292 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 293 | (const unsigned char *) pers, |
| 294 | strlen( pers ) ) ) != 0 ) |
| 295 | { |
| 296 | printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret ); |
| 297 | goto exit; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * 1.1. Generate the key |
| 302 | */ |
| 303 | printf( "\n . Generating the private key ..." ); |
| 304 | fflush( stdout ); |
| 305 | |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 306 | if( ( ret = pk_init_ctx( &key, pk_info_from_type( opt.type ) ) ) != 0 ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 307 | { |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 308 | printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret ); |
| 309 | goto exit; |
| 310 | } |
| 311 | |
| 312 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME) |
| 313 | if( opt.type == POLARSSL_PK_RSA ) |
| 314 | { |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 315 | ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg, |
| 316 | opt.rsa_keysize, 65537 ); |
| 317 | if( ret != 0 ) |
| 318 | { |
| 319 | printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret ); |
| 320 | goto exit; |
| 321 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 322 | } |
| 323 | else |
| 324 | #endif /* POLARSSL_RSA_C */ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 325 | #if defined(POLARSSL_ECP_C) |
| 326 | if( opt.type == POLARSSL_PK_ECKEY ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 327 | { |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 328 | ret = ecp_gen_key( opt.ec_curve, pk_ec( key ), |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 329 | ctr_drbg_random, &ctr_drbg ); |
| 330 | if( ret != 0 ) |
| 331 | { |
| 332 | printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret ); |
| 333 | goto exit; |
| 334 | } |
| 335 | } |
| 336 | else |
| 337 | #endif /* POLARSSL_ECP_C */ |
| 338 | { |
| 339 | printf( " failed\n ! key type not supported\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 340 | goto exit; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * 1.2 Print the key |
| 345 | */ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 346 | printf( " ok\n . Key information:\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 347 | |
| 348 | #if defined(POLARSSL_RSA_C) |
| 349 | if( pk_get_type( &key ) == POLARSSL_PK_RSA ) |
| 350 | { |
| 351 | rsa_context *rsa = pk_rsa( key ); |
| 352 | mpi_write_file( "N: ", &rsa->N, 16, NULL ); |
| 353 | mpi_write_file( "E: ", &rsa->E, 16, NULL ); |
| 354 | mpi_write_file( "D: ", &rsa->D, 16, NULL ); |
| 355 | mpi_write_file( "P: ", &rsa->P, 16, NULL ); |
| 356 | mpi_write_file( "Q: ", &rsa->Q, 16, NULL ); |
| 357 | mpi_write_file( "DP: ", &rsa->DP, 16, NULL ); |
| 358 | mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ); |
| 359 | mpi_write_file( "QP: ", &rsa->QP, 16, NULL ); |
| 360 | } |
| 361 | else |
| 362 | #endif |
| 363 | #if defined(POLARSSL_ECP_C) |
| 364 | if( pk_get_type( &key ) == POLARSSL_PK_ECKEY ) |
| 365 | { |
| 366 | ecp_keypair *ecp = pk_ec( key ); |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 367 | printf( "curve: %s\n", |
| 368 | ecp_curve_info_from_grp_id( ecp->grp.id )->name ); |
| 369 | mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL ); |
| 370 | mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL ); |
| 371 | mpi_write_file( "D: ", &ecp->d , 16, NULL ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 372 | } |
| 373 | else |
| 374 | #endif |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 375 | printf(" ! key type not supported\n"); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 376 | |
Manuel Pégourié-Gonnard | a39416f | 2014-07-21 17:10:16 +0200 | [diff] [blame^] | 377 | /* |
| 378 | * 1.3 Export key |
| 379 | */ |
| 380 | printf( " . Writing key to file..." ); |
| 381 | |
| 382 | if( ( ret = write_private_key( &key, opt.filename ) ) != 0 ) |
| 383 | { |
| 384 | printf( " failed\n" ); |
| 385 | goto exit; |
| 386 | } |
| 387 | |
| 388 | printf( " ok\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 389 | |
| 390 | exit: |
| 391 | |
| 392 | if( ret != 0 && ret != 1) |
| 393 | { |
| 394 | #ifdef POLARSSL_ERROR_C |
| 395 | polarssl_strerror( ret, buf, sizeof( buf ) ); |
| 396 | printf( " - %s\n", buf ); |
| 397 | #else |
| 398 | printf("\n"); |
| 399 | #endif |
| 400 | } |
| 401 | |
| 402 | pk_free( &key ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 403 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 404 | entropy_free( &entropy ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 405 | |
| 406 | #if defined(_WIN32) |
| 407 | printf( " + Press Enter to exit this program.\n" ); |
| 408 | fflush( stdout ); getchar(); |
| 409 | #endif |
| 410 | |
| 411 | return( ret ); |
| 412 | } |
| 413 | #endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */ |