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