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