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 | |
| 26 | #include "polarssl/config.h" |
| 27 | |
| 28 | #include <string.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <stdio.h> |
| 31 | |
| 32 | #include "polarssl/error.h" |
| 33 | #include "polarssl/pk.h" |
| 34 | #include "polarssl/ecdsa.h" |
| 35 | #include "polarssl/rsa.h" |
| 36 | #include "polarssl/error.h" |
| 37 | #include "polarssl/entropy.h" |
| 38 | #include "polarssl/ctr_drbg.h" |
| 39 | |
| 40 | #if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) || \ |
| 41 | !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_CTR_DRBG_C) |
| 42 | int main( int argc, char *argv[] ) |
| 43 | { |
| 44 | ((void) argc); |
| 45 | ((void) argv); |
| 46 | |
| 47 | printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO and/or " |
| 48 | "POLARSSL_ENTROPY_C and/or POLARSSL_CTR_DRBG_C " |
| 49 | "not defined.\n" ); |
| 50 | return( 0 ); |
| 51 | } |
| 52 | #else |
| 53 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 54 | #define FORMAT_PEM 0 |
| 55 | #define FORMAT_DER 1 |
| 56 | |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 57 | #define DFL_TYPE POLARSSL_PK_RSA |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 58 | #define DFL_RSA_KEYSIZE 4096 |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame^] | 59 | #define DFL_EC_CURVE ecp_curve_list()->grp_id |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 60 | #define DFL_FILENAME "keyfile.key" |
| 61 | #define DFL_FORMAT FORMAT_PEM |
| 62 | |
| 63 | /* |
| 64 | * global options |
| 65 | */ |
| 66 | struct options |
| 67 | { |
| 68 | int type; /* the type of key to generate */ |
| 69 | int rsa_keysize; /* length of key in bits */ |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame^] | 70 | int ec_curve; /* curve identifier for EC keys */ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 71 | const char *filename; /* filename of the key file */ |
| 72 | int format; /* the output format to use */ |
| 73 | } opt; |
| 74 | |
| 75 | static int write_private_key( pk_context *key, const char *output_file ) |
| 76 | { |
| 77 | int ret; |
| 78 | FILE *f; |
| 79 | unsigned char output_buf[16000]; |
| 80 | unsigned char *c = output_buf; |
| 81 | size_t len = 0; |
| 82 | |
| 83 | memset(output_buf, 0, 16000); |
| 84 | if( opt.format == FORMAT_PEM ) |
| 85 | { |
| 86 | if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 ) |
| 87 | return( ret ); |
| 88 | |
| 89 | len = strlen( (char *) output_buf ); |
| 90 | } |
| 91 | else |
| 92 | { |
| 93 | if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 ) |
| 94 | return( ret ); |
| 95 | |
| 96 | len = ret; |
| 97 | c = output_buf + sizeof(output_buf) - len - 1; |
| 98 | } |
| 99 | |
| 100 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 101 | return( -1 ); |
| 102 | |
| 103 | if( fwrite( c, 1, len, f ) != len ) |
| 104 | return( -1 ); |
| 105 | |
| 106 | fclose(f); |
| 107 | |
| 108 | return( 0 ); |
| 109 | } |
| 110 | |
| 111 | #define USAGE \ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 112 | "\n usage: gen_key param=<>...\n" \ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 113 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 114 | " type=rsa|ec default: rsa\n" \ |
| 115 | " rsa_keysize=%%d default: 4096\n" \ |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame^] | 116 | " ec_curve=%%s see below\n" \ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 117 | " filename=%%s default: keyfile.key\n" \ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 118 | " format=pem|der default: pem\n" \ |
| 119 | "\n" |
| 120 | |
| 121 | int main( int argc, char *argv[] ) |
| 122 | { |
| 123 | int ret = 0; |
| 124 | pk_context key; |
| 125 | char buf[1024]; |
| 126 | int i; |
| 127 | char *p, *q; |
| 128 | entropy_context entropy; |
| 129 | ctr_drbg_context ctr_drbg; |
| 130 | const char *pers = "gen_key"; |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame^] | 131 | #if defined(POLARSSL_ECP_C) |
| 132 | const ecp_curve_info *curve_info; |
| 133 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * Set to sane values |
| 137 | */ |
| 138 | pk_init( &key ); |
| 139 | memset( buf, 0, sizeof( buf ) ); |
| 140 | |
| 141 | if( argc == 0 ) |
| 142 | { |
| 143 | usage: |
| 144 | ret = 1; |
| 145 | printf( USAGE ); |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame^] | 146 | #if defined(POLARSSL_ECP_C) |
| 147 | printf( " availabled ec_curve values:\n" ); |
| 148 | curve_info = ecp_curve_list(); |
| 149 | printf( " %s (default)\n", curve_info->name ); |
| 150 | while( ( ++curve_info )->name != NULL ) |
| 151 | printf( " %s\n", curve_info->name ); |
| 152 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 153 | goto exit; |
| 154 | } |
| 155 | |
| 156 | opt.type = DFL_TYPE; |
| 157 | opt.rsa_keysize = DFL_RSA_KEYSIZE; |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame^] | 158 | opt.ec_curve = DFL_EC_CURVE; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 159 | opt.filename = DFL_FILENAME; |
| 160 | opt.format = DFL_FORMAT; |
| 161 | |
| 162 | for( i = 1; i < argc; i++ ) |
| 163 | { |
| 164 | p = argv[i]; |
| 165 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 166 | goto usage; |
| 167 | *q++ = '\0'; |
| 168 | |
| 169 | if( strcmp( p, "type" ) == 0 ) |
| 170 | { |
| 171 | if( strcmp( q, "rsa" ) == 0 ) |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 172 | opt.type = POLARSSL_PK_RSA; |
| 173 | if( strcmp( q, "ec" ) == 0 ) |
| 174 | opt.type = POLARSSL_PK_ECKEY; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 175 | else |
| 176 | goto usage; |
| 177 | } |
| 178 | else if( strcmp( p, "format" ) == 0 ) |
| 179 | { |
| 180 | if( strcmp( q, "pem" ) == 0 ) |
| 181 | opt.format = FORMAT_PEM; |
| 182 | else if( strcmp( q, "der" ) == 0 ) |
| 183 | opt.format = FORMAT_DER; |
| 184 | else |
| 185 | goto usage; |
| 186 | } |
| 187 | else if( strcmp( p, "rsa_keysize" ) == 0 ) |
| 188 | { |
| 189 | opt.rsa_keysize = atoi( q ); |
| 190 | if( opt.rsa_keysize < 1024 || opt.rsa_keysize > 8192 ) |
| 191 | goto usage; |
| 192 | } |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame^] | 193 | else if( strcmp( p, "ec_curve" ) == 0 ) |
| 194 | { |
| 195 | if( ( curve_info = ecp_curve_info_from_name( q ) ) == NULL ) |
| 196 | goto usage; |
| 197 | opt.ec_curve = curve_info->grp_id; |
| 198 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 199 | else if( strcmp( p, "filename" ) == 0 ) |
| 200 | opt.filename = q; |
| 201 | else |
| 202 | goto usage; |
| 203 | } |
| 204 | |
| 205 | printf( "\n . Seeding the random number generator..." ); |
| 206 | fflush( stdout ); |
| 207 | |
| 208 | entropy_init( &entropy ); |
| 209 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
| 210 | (const unsigned char *) pers, |
| 211 | strlen( pers ) ) ) != 0 ) |
| 212 | { |
| 213 | printf( " failed\n ! ctr_drbg_init returned -0x%04x\n", -ret ); |
| 214 | goto exit; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * 1.1. Generate the key |
| 219 | */ |
| 220 | printf( "\n . Generating the private key ..." ); |
| 221 | fflush( stdout ); |
| 222 | |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 223 | 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] | 224 | { |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 225 | printf( " failed\n ! pk_init_ctx returned -0x%04x", -ret ); |
| 226 | goto exit; |
| 227 | } |
| 228 | |
| 229 | #if defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME) |
| 230 | if( opt.type == POLARSSL_PK_RSA ) |
| 231 | { |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 232 | ret = rsa_gen_key( pk_rsa( key ), ctr_drbg_random, &ctr_drbg, |
| 233 | opt.rsa_keysize, 65537 ); |
| 234 | if( ret != 0 ) |
| 235 | { |
| 236 | printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret ); |
| 237 | goto exit; |
| 238 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 239 | } |
| 240 | else |
| 241 | #endif /* POLARSSL_RSA_C */ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 242 | #if defined(POLARSSL_ECP_C) |
| 243 | if( opt.type == POLARSSL_PK_ECKEY ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 244 | { |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame^] | 245 | ret = ecp_gen_key( opt.ec_curve, pk_ec( key ), |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 246 | ctr_drbg_random, &ctr_drbg ); |
| 247 | if( ret != 0 ) |
| 248 | { |
| 249 | printf( " failed\n ! rsa_gen_key returned -0x%04x", -ret ); |
| 250 | goto exit; |
| 251 | } |
| 252 | } |
| 253 | else |
| 254 | #endif /* POLARSSL_ECP_C */ |
| 255 | { |
| 256 | printf( " failed\n ! key type not supported\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 257 | goto exit; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * 1.2 Print the key |
| 262 | */ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 263 | printf( " ok\n . Key information:\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 264 | |
| 265 | #if defined(POLARSSL_RSA_C) |
| 266 | if( pk_get_type( &key ) == POLARSSL_PK_RSA ) |
| 267 | { |
| 268 | rsa_context *rsa = pk_rsa( key ); |
| 269 | mpi_write_file( "N: ", &rsa->N, 16, NULL ); |
| 270 | mpi_write_file( "E: ", &rsa->E, 16, NULL ); |
| 271 | mpi_write_file( "D: ", &rsa->D, 16, NULL ); |
| 272 | mpi_write_file( "P: ", &rsa->P, 16, NULL ); |
| 273 | mpi_write_file( "Q: ", &rsa->Q, 16, NULL ); |
| 274 | mpi_write_file( "DP: ", &rsa->DP, 16, NULL ); |
| 275 | mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ); |
| 276 | mpi_write_file( "QP: ", &rsa->QP, 16, NULL ); |
| 277 | } |
| 278 | else |
| 279 | #endif |
| 280 | #if defined(POLARSSL_ECP_C) |
| 281 | if( pk_get_type( &key ) == POLARSSL_PK_ECKEY ) |
| 282 | { |
| 283 | ecp_keypair *ecp = pk_ec( key ); |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame^] | 284 | printf( "curve: %s\n", |
| 285 | ecp_curve_info_from_grp_id( ecp->grp.id )->name ); |
| 286 | mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL ); |
| 287 | mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL ); |
| 288 | mpi_write_file( "D: ", &ecp->d , 16, NULL ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 289 | } |
| 290 | else |
| 291 | #endif |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 292 | printf(" ! key type not supported\n"); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 293 | |
| 294 | write_private_key( &key, opt.filename ); |
| 295 | |
| 296 | exit: |
| 297 | |
| 298 | if( ret != 0 && ret != 1) |
| 299 | { |
| 300 | #ifdef POLARSSL_ERROR_C |
| 301 | polarssl_strerror( ret, buf, sizeof( buf ) ); |
| 302 | printf( " - %s\n", buf ); |
| 303 | #else |
| 304 | printf("\n"); |
| 305 | #endif |
| 306 | } |
| 307 | |
| 308 | pk_free( &key ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 309 | entropy_free( &entropy ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 310 | |
| 311 | #if defined(_WIN32) |
| 312 | printf( " + Press Enter to exit this program.\n" ); |
| 313 | fflush( stdout ); getchar(); |
| 314 | #endif |
| 315 | |
| 316 | return( ret ); |
| 317 | } |
| 318 | #endif /* POLARSSL_PK_WRITE_C && POLARSSL_FS_IO */ |