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 |
| 67 | #define DFL_FILENAME "keyfile.key" |
| 68 | #define DFL_FORMAT FORMAT_PEM |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 69 | #define DFL_USE_DEV_RANDOM 0 |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 70 | |
Paul Bakker | d153ef3 | 2014-08-18 12:00:28 +0200 | [diff] [blame^] | 71 | #if defined(POLARSSL_ECP_C) |
| 72 | #define DFL_EC_CURVE ecp_curve_list()->grp_id |
| 73 | #else |
| 74 | #define DFL_EC_CURVE 0 |
| 75 | #endif |
| 76 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 77 | /* |
| 78 | * global options |
| 79 | */ |
| 80 | struct options |
| 81 | { |
| 82 | int type; /* the type of key to generate */ |
| 83 | int rsa_keysize; /* length of key in bits */ |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 84 | int ec_curve; /* curve identifier for EC keys */ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 85 | const char *filename; /* filename of the key file */ |
| 86 | int format; /* the output format to use */ |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 87 | int use_dev_random; /* use /dev/random as entropy source */ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 88 | } opt; |
| 89 | |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 90 | #if !defined(_WIN32) && defined(POLARSSL_FS_IO) |
| 91 | |
| 92 | #define DEV_RANDOM_THRESHOLD 32 |
| 93 | |
| 94 | int dev_random_entropy_poll( void *data, unsigned char *output, |
| 95 | size_t len, size_t *olen ) |
| 96 | { |
| 97 | FILE *file; |
| 98 | size_t ret, left = len; |
| 99 | unsigned char *p = output; |
| 100 | ((void) data); |
| 101 | |
| 102 | *olen = 0; |
| 103 | |
| 104 | file = fopen( "/dev/random", "rb" ); |
| 105 | if( file == NULL ) |
| 106 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 107 | |
| 108 | while( left > 0 ) |
| 109 | { |
| 110 | /* /dev/random can return much less than requested. If so, try again */ |
| 111 | ret = fread( p, 1, left, file ); |
| 112 | if( ret == 0 && ferror( file ) ) |
| 113 | { |
| 114 | fclose( file ); |
| 115 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 116 | } |
| 117 | |
| 118 | p += ret; |
| 119 | left -= ret; |
| 120 | sleep( 1 ); |
| 121 | } |
| 122 | fclose( file ); |
| 123 | *olen = len; |
| 124 | |
| 125 | return( 0 ); |
| 126 | } |
| 127 | #endif /* !_WIN32 && POLARSSL_FS_IO */ |
| 128 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 129 | static int write_private_key( pk_context *key, const char *output_file ) |
| 130 | { |
| 131 | int ret; |
| 132 | FILE *f; |
| 133 | unsigned char output_buf[16000]; |
| 134 | unsigned char *c = output_buf; |
| 135 | size_t len = 0; |
| 136 | |
| 137 | memset(output_buf, 0, 16000); |
| 138 | if( opt.format == FORMAT_PEM ) |
| 139 | { |
| 140 | if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 ) |
| 141 | return( ret ); |
| 142 | |
| 143 | len = strlen( (char *) output_buf ); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 ) |
| 148 | return( ret ); |
| 149 | |
| 150 | len = ret; |
Paul Bakker | 3c38f29 | 2014-06-13 17:37:46 +0200 | [diff] [blame] | 151 | c = output_buf + sizeof(output_buf) - len; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Paul Bakker | 3966d71 | 2014-07-10 12:03:09 +0200 | [diff] [blame] | 154 | if( ( f = fopen( output_file, "wb" ) ) == NULL ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 155 | return( -1 ); |
| 156 | |
| 157 | if( fwrite( c, 1, len, f ) != len ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 158 | { |
| 159 | fclose( f ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 160 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 161 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 162 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 163 | fclose( f ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 164 | |
| 165 | return( 0 ); |
| 166 | } |
| 167 | |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 168 | #if !defined(_WIN32) && defined(POLARSSL_FS_IO) |
| 169 | #define USAGE_DEV_RANDOM \ |
| 170 | " use_dev_random=0|1 default: 0\n" |
| 171 | #else |
| 172 | #define USAGE_DEV_RANDOM "" |
| 173 | #endif /* !_WIN32 && POLARSSL_FS_IO */ |
| 174 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 175 | #define USAGE \ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 176 | "\n usage: gen_key param=<>...\n" \ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 177 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 178 | " type=rsa|ec default: rsa\n" \ |
| 179 | " rsa_keysize=%%d default: 4096\n" \ |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 180 | " ec_curve=%%s see below\n" \ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 181 | " filename=%%s default: keyfile.key\n" \ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 182 | " format=pem|der default: pem\n" \ |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 183 | USAGE_DEV_RANDOM \ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 184 | "\n" |
| 185 | |
| 186 | int main( int argc, char *argv[] ) |
| 187 | { |
| 188 | int ret = 0; |
| 189 | pk_context key; |
| 190 | char buf[1024]; |
| 191 | int i; |
| 192 | char *p, *q; |
| 193 | entropy_context entropy; |
| 194 | ctr_drbg_context ctr_drbg; |
| 195 | const char *pers = "gen_key"; |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 196 | #if defined(POLARSSL_ECP_C) |
| 197 | const ecp_curve_info *curve_info; |
| 198 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 199 | |
| 200 | /* |
| 201 | * Set to sane values |
| 202 | */ |
| 203 | pk_init( &key ); |
| 204 | memset( buf, 0, sizeof( buf ) ); |
| 205 | |
| 206 | if( argc == 0 ) |
| 207 | { |
| 208 | usage: |
| 209 | ret = 1; |
| 210 | printf( USAGE ); |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 211 | #if defined(POLARSSL_ECP_C) |
| 212 | printf( " availabled ec_curve values:\n" ); |
| 213 | curve_info = ecp_curve_list(); |
| 214 | printf( " %s (default)\n", curve_info->name ); |
| 215 | while( ( ++curve_info )->name != NULL ) |
| 216 | printf( " %s\n", curve_info->name ); |
| 217 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 218 | goto exit; |
| 219 | } |
| 220 | |
| 221 | opt.type = DFL_TYPE; |
| 222 | opt.rsa_keysize = DFL_RSA_KEYSIZE; |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 223 | opt.ec_curve = DFL_EC_CURVE; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 224 | opt.filename = DFL_FILENAME; |
| 225 | opt.format = DFL_FORMAT; |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 226 | opt.use_dev_random = DFL_USE_DEV_RANDOM; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 227 | |
| 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é-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 238 | opt.type = POLARSSL_PK_RSA; |
Paul Bakker | 247b487 | 2014-02-06 14:33:52 +0100 | [diff] [blame] | 239 | else if( strcmp( q, "ec" ) == 0 ) |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 240 | opt.type = POLARSSL_PK_ECKEY; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 241 | 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é-Gonnard | a39416f | 2014-07-21 17:10:16 +0200 | [diff] [blame] | 256 | if( opt.rsa_keysize < 1024 || |
| 257 | opt.rsa_keysize > POLARSSL_MPI_MAX_BITS ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 258 | goto usage; |
| 259 | } |
Paul Bakker | d153ef3 | 2014-08-18 12:00:28 +0200 | [diff] [blame^] | 260 | #if defined(POLARSSL_ECP_C) |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 261 | else if( strcmp( p, "ec_curve" ) == 0 ) |
| 262 | { |
| 263 | if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL ) |
| 264 | goto usage; |
| 265 | opt.ec_curve = curve_info->grp_id; |
| 266 | } |
Paul Bakker | d153ef3 | 2014-08-18 12:00:28 +0200 | [diff] [blame^] | 267 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 268 | else if( strcmp( p, "filename" ) == 0 ) |
| 269 | opt.filename = q; |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 270 | 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 Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 276 | else |
| 277 | goto usage; |
| 278 | } |
| 279 | |
| 280 | printf( "\n . Seeding the random number generator..." ); |
| 281 | fflush( stdout ); |
| 282 | |
| 283 | entropy_init( &entropy ); |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 284 | #if !defined(_WIN32) && defined(POLARSSL_FS_IO) |
| 285 | if( opt.use_dev_random ) |
| 286 | { |
| 287 | if( ( ret = entropy_add_source( &entropy, dev_random_entropy_poll, |
| 288 | NULL, DEV_RANDOM_THRESHOLD ) ) != 0 ) |
| 289 | { |
| 290 | printf( " failed\n ! entropy_add_source returned -0x%04x\n", -ret ); |
| 291 | goto exit; |
| 292 | } |
| 293 | |
| 294 | printf("\n Using /dev/random, so can take a long time! " ); |
| 295 | fflush( stdout ); |
| 296 | } |
| 297 | #endif /* !_WIN32 && POLARSSL_FS_IO */ |
| 298 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 299 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 300 | (const unsigned char *) pers, |
| 301 | strlen( pers ) ) ) != 0 ) |
| 302 | { |
| 303 | printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret ); |
| 304 | goto exit; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * 1.1. Generate the key |
| 309 | */ |
| 310 | printf( "\n . Generating the private key ..." ); |
| 311 | fflush( stdout ); |
| 312 | |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 313 | 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] | 314 | { |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 315 | printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret ); |
| 316 | goto exit; |
| 317 | } |
| 318 | |
| 319 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME) |
| 320 | if( opt.type == POLARSSL_PK_RSA ) |
| 321 | { |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 322 | ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg, |
| 323 | opt.rsa_keysize, 65537 ); |
| 324 | if( ret != 0 ) |
| 325 | { |
| 326 | printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret ); |
| 327 | goto exit; |
| 328 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 329 | } |
| 330 | else |
| 331 | #endif /* POLARSSL_RSA_C */ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 332 | #if defined(POLARSSL_ECP_C) |
| 333 | if( opt.type == POLARSSL_PK_ECKEY ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 334 | { |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 335 | ret = ecp_gen_key( opt.ec_curve, pk_ec( key ), |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 336 | ctr_drbg_random, &ctr_drbg ); |
| 337 | if( ret != 0 ) |
| 338 | { |
| 339 | printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret ); |
| 340 | goto exit; |
| 341 | } |
| 342 | } |
| 343 | else |
| 344 | #endif /* POLARSSL_ECP_C */ |
| 345 | { |
| 346 | printf( " failed\n ! key type not supported\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 347 | goto exit; |
| 348 | } |
| 349 | |
| 350 | /* |
| 351 | * 1.2 Print the key |
| 352 | */ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 353 | printf( " ok\n . Key information:\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 354 | |
| 355 | #if defined(POLARSSL_RSA_C) |
| 356 | if( pk_get_type( &key ) == POLARSSL_PK_RSA ) |
| 357 | { |
| 358 | rsa_context *rsa = pk_rsa( key ); |
| 359 | mpi_write_file( "N: ", &rsa->N, 16, NULL ); |
| 360 | mpi_write_file( "E: ", &rsa->E, 16, NULL ); |
| 361 | mpi_write_file( "D: ", &rsa->D, 16, NULL ); |
| 362 | mpi_write_file( "P: ", &rsa->P, 16, NULL ); |
| 363 | mpi_write_file( "Q: ", &rsa->Q, 16, NULL ); |
| 364 | mpi_write_file( "DP: ", &rsa->DP, 16, NULL ); |
| 365 | mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ); |
| 366 | mpi_write_file( "QP: ", &rsa->QP, 16, NULL ); |
| 367 | } |
| 368 | else |
| 369 | #endif |
| 370 | #if defined(POLARSSL_ECP_C) |
| 371 | if( pk_get_type( &key ) == POLARSSL_PK_ECKEY ) |
| 372 | { |
| 373 | ecp_keypair *ecp = pk_ec( key ); |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 374 | printf( "curve: %s\n", |
| 375 | ecp_curve_info_from_grp_id( ecp->grp.id )->name ); |
| 376 | mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL ); |
| 377 | mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL ); |
| 378 | mpi_write_file( "D: ", &ecp->d , 16, NULL ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 379 | } |
| 380 | else |
| 381 | #endif |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 382 | printf(" ! key type not supported\n"); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 383 | |
Manuel Pégourié-Gonnard | a39416f | 2014-07-21 17:10:16 +0200 | [diff] [blame] | 384 | /* |
| 385 | * 1.3 Export key |
| 386 | */ |
| 387 | printf( " . Writing key to file..." ); |
| 388 | |
| 389 | if( ( ret = write_private_key( &key, opt.filename ) ) != 0 ) |
| 390 | { |
| 391 | printf( " failed\n" ); |
| 392 | goto exit; |
| 393 | } |
| 394 | |
| 395 | printf( " ok\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 396 | |
| 397 | exit: |
| 398 | |
| 399 | if( ret != 0 && ret != 1) |
| 400 | { |
| 401 | #ifdef POLARSSL_ERROR_C |
| 402 | polarssl_strerror( ret, buf, sizeof( buf ) ); |
| 403 | printf( " - %s\n", buf ); |
| 404 | #else |
| 405 | printf("\n"); |
| 406 | #endif |
| 407 | } |
| 408 | |
| 409 | pk_free( &key ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 410 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 411 | entropy_free( &entropy ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 412 | |
| 413 | #if defined(_WIN32) |
| 414 | printf( " + Press Enter to exit this program.\n" ); |
| 415 | fflush( stdout ); getchar(); |
| 416 | #endif |
| 417 | |
| 418 | return( ret ); |
| 419 | } |
| 420 | #endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */ |