Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Key generation application |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
| 45 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 46 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 47 | */ |
| 48 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 53 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 56 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 57 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 58 | #include <stdio.h> |
Andres Amaya Garcia | 208c217 | 2018-04-29 19:51:56 +0100 | [diff] [blame] | 59 | #include <stdlib.h> |
| 60 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 61 | #define mbedtls_exit exit |
Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 62 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 208c217 | 2018-04-29 19:51:56 +0100 | [diff] [blame] | 63 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 64 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 65 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | #if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) && \ |
| 67 | defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 68 | #include "mbedtls/error.h" |
| 69 | #include "mbedtls/pk.h" |
| 70 | #include "mbedtls/ecdsa.h" |
| 71 | #include "mbedtls/rsa.h" |
| 72 | #include "mbedtls/error.h" |
| 73 | #include "mbedtls/entropy.h" |
| 74 | #include "mbedtls/ctr_drbg.h" |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 75 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 76 | #include <stdio.h> |
| 77 | #include <stdlib.h> |
| 78 | #include <string.h> |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 79 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 80 | #if !defined(_WIN32) |
| 81 | #include <unistd.h> |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 82 | |
| 83 | #define DEV_RANDOM_THRESHOLD 32 |
| 84 | |
| 85 | int dev_random_entropy_poll( void *data, unsigned char *output, |
| 86 | size_t len, size_t *olen ) |
| 87 | { |
| 88 | FILE *file; |
| 89 | size_t ret, left = len; |
| 90 | unsigned char *p = output; |
| 91 | ((void) data); |
| 92 | |
| 93 | *olen = 0; |
| 94 | |
| 95 | file = fopen( "/dev/random", "rb" ); |
| 96 | if( file == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 97 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 98 | |
| 99 | while( left > 0 ) |
| 100 | { |
| 101 | /* /dev/random can return much less than requested. If so, try again */ |
| 102 | ret = fread( p, 1, left, file ); |
| 103 | if( ret == 0 && ferror( file ) ) |
| 104 | { |
| 105 | fclose( file ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | p += ret; |
| 110 | left -= ret; |
| 111 | sleep( 1 ); |
| 112 | } |
| 113 | fclose( file ); |
| 114 | *olen = len; |
| 115 | |
| 116 | return( 0 ); |
| 117 | } |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 118 | #endif /* !_WIN32 */ |
| 119 | #endif |
| 120 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 121 | #if defined(MBEDTLS_ECP_C) |
| 122 | #define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 123 | #else |
| 124 | #define DFL_EC_CURVE 0 |
| 125 | #endif |
| 126 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 127 | #if !defined(_WIN32) && defined(MBEDTLS_FS_IO) |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 128 | #define USAGE_DEV_RANDOM \ |
| 129 | " use_dev_random=0|1 default: 0\n" |
| 130 | #else |
| 131 | #define USAGE_DEV_RANDOM "" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | #endif /* !_WIN32 && MBEDTLS_FS_IO */ |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 133 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 134 | #define FORMAT_PEM 0 |
| 135 | #define FORMAT_DER 1 |
| 136 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 137 | #define DFL_TYPE MBEDTLS_PK_RSA |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 138 | #define DFL_RSA_KEYSIZE 4096 |
| 139 | #define DFL_FILENAME "keyfile.key" |
| 140 | #define DFL_FORMAT FORMAT_PEM |
| 141 | #define DFL_USE_DEV_RANDOM 0 |
| 142 | |
| 143 | #define USAGE \ |
| 144 | "\n usage: gen_key param=<>...\n" \ |
| 145 | "\n acceptable parameters:\n" \ |
| 146 | " type=rsa|ec default: rsa\n" \ |
| 147 | " rsa_keysize=%%d default: 4096\n" \ |
| 148 | " ec_curve=%%s see below\n" \ |
| 149 | " filename=%%s default: keyfile.key\n" \ |
| 150 | " format=pem|der default: pem\n" \ |
| 151 | USAGE_DEV_RANDOM \ |
| 152 | "\n" |
| 153 | |
Simon Butcher | 604d399 | 2016-10-06 19:02:49 +0100 | [diff] [blame] | 154 | #if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \ |
| 155 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \ |
| 156 | !defined(MBEDTLS_CTR_DRBG_C) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 157 | int main( void ) |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 158 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | mbedtls_printf( "MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or " |
Simon Butcher | 604d399 | 2016-10-06 19:02:49 +0100 | [diff] [blame] | 160 | "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or " |
| 161 | "MBEDTLS_PEM_WRITE_C" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 162 | "not defined.\n" ); |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 163 | mbedtls_exit( 0 ); |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 164 | } |
| 165 | #else |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 166 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 167 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 168 | /* |
| 169 | * global options |
| 170 | */ |
| 171 | struct options |
| 172 | { |
| 173 | int type; /* the type of key to generate */ |
| 174 | int rsa_keysize; /* length of key in bits */ |
| 175 | int ec_curve; /* curve identifier for EC keys */ |
| 176 | const char *filename; /* filename of the key file */ |
| 177 | int format; /* the output format to use */ |
| 178 | int use_dev_random; /* use /dev/random as entropy source */ |
| 179 | } opt; |
| 180 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | static int write_private_key( mbedtls_pk_context *key, const char *output_file ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 182 | { |
| 183 | int ret; |
| 184 | FILE *f; |
| 185 | unsigned char output_buf[16000]; |
| 186 | unsigned char *c = output_buf; |
| 187 | size_t len = 0; |
| 188 | |
| 189 | memset(output_buf, 0, 16000); |
| 190 | if( opt.format == FORMAT_PEM ) |
| 191 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 193 | return( ret ); |
| 194 | |
| 195 | len = strlen( (char *) output_buf ); |
| 196 | } |
| 197 | else |
| 198 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 200 | return( ret ); |
| 201 | |
| 202 | len = ret; |
Paul Bakker | 3c38f29 | 2014-06-13 17:37:46 +0200 | [diff] [blame] | 203 | c = output_buf + sizeof(output_buf) - len; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 204 | } |
| 205 | |
Paul Bakker | 3966d71 | 2014-07-10 12:03:09 +0200 | [diff] [blame] | 206 | if( ( f = fopen( output_file, "wb" ) ) == NULL ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 207 | return( -1 ); |
| 208 | |
| 209 | if( fwrite( c, 1, len, f ) != len ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 210 | { |
| 211 | fclose( f ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 212 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 213 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 214 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 215 | fclose( f ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 216 | |
| 217 | return( 0 ); |
| 218 | } |
| 219 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 220 | int main( int argc, char *argv[] ) |
| 221 | { |
Andres Amaya Garcia | 208c217 | 2018-04-29 19:51:56 +0100 | [diff] [blame] | 222 | int ret = 1; |
| 223 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | mbedtls_pk_context key; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 225 | char buf[1024]; |
| 226 | int i; |
| 227 | char *p, *q; |
Hanno Becker | 83aad1f | 2017-08-23 06:45:10 +0100 | [diff] [blame] | 228 | mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | mbedtls_entropy_context entropy; |
| 230 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 231 | const char *pers = "gen_key"; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | #if defined(MBEDTLS_ECP_C) |
| 233 | const mbedtls_ecp_curve_info *curve_info; |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 234 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 235 | |
| 236 | /* |
| 237 | * Set to sane values |
| 238 | */ |
Hanno Becker | 83aad1f | 2017-08-23 06:45:10 +0100 | [diff] [blame] | 239 | |
| 240 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 241 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP ); |
| 242 | mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP ); |
| 243 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 244 | mbedtls_pk_init( &key ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 245 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 246 | memset( buf, 0, sizeof( buf ) ); |
| 247 | |
| 248 | if( argc == 0 ) |
| 249 | { |
| 250 | usage: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | mbedtls_printf( USAGE ); |
| 252 | #if defined(MBEDTLS_ECP_C) |
James Cowgill | 07a92d7 | 2015-10-09 00:28:14 +0100 | [diff] [blame] | 253 | mbedtls_printf( " available ec_curve values:\n" ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | curve_info = mbedtls_ecp_curve_list(); |
| 255 | mbedtls_printf( " %s (default)\n", curve_info->name ); |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 256 | while( ( ++curve_info )->name != NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 257 | mbedtls_printf( " %s\n", curve_info->name ); |
Andres Amaya Garcia | 208c217 | 2018-04-29 19:51:56 +0100 | [diff] [blame] | 258 | #endif /* MBEDTLS_ECP_C */ |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 259 | goto exit; |
| 260 | } |
| 261 | |
| 262 | opt.type = DFL_TYPE; |
| 263 | opt.rsa_keysize = DFL_RSA_KEYSIZE; |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 264 | opt.ec_curve = DFL_EC_CURVE; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 265 | opt.filename = DFL_FILENAME; |
| 266 | opt.format = DFL_FORMAT; |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 267 | opt.use_dev_random = DFL_USE_DEV_RANDOM; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 268 | |
| 269 | for( i = 1; i < argc; i++ ) |
| 270 | { |
| 271 | p = argv[i]; |
| 272 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 273 | goto usage; |
| 274 | *q++ = '\0'; |
| 275 | |
| 276 | if( strcmp( p, "type" ) == 0 ) |
| 277 | { |
| 278 | if( strcmp( q, "rsa" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | opt.type = MBEDTLS_PK_RSA; |
Paul Bakker | 247b487 | 2014-02-06 14:33:52 +0100 | [diff] [blame] | 280 | else if( strcmp( q, "ec" ) == 0 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | opt.type = MBEDTLS_PK_ECKEY; |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 282 | else |
| 283 | goto usage; |
| 284 | } |
| 285 | else if( strcmp( p, "format" ) == 0 ) |
| 286 | { |
| 287 | if( strcmp( q, "pem" ) == 0 ) |
| 288 | opt.format = FORMAT_PEM; |
| 289 | else if( strcmp( q, "der" ) == 0 ) |
| 290 | opt.format = FORMAT_DER; |
| 291 | else |
| 292 | goto usage; |
| 293 | } |
| 294 | else if( strcmp( p, "rsa_keysize" ) == 0 ) |
| 295 | { |
| 296 | opt.rsa_keysize = atoi( q ); |
Manuel Pégourié-Gonnard | a39416f | 2014-07-21 17:10:16 +0200 | [diff] [blame] | 297 | if( opt.rsa_keysize < 1024 || |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 299 | goto usage; |
| 300 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | #if defined(MBEDTLS_ECP_C) |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 302 | else if( strcmp( p, "ec_curve" ) == 0 ) |
| 303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | if( ( curve_info = mbedtls_ecp_curve_info_from_name( q ) ) == NULL ) |
Manuel Pégourié-Gonnard | 6e16cdb | 2013-11-30 15:32:47 +0100 | [diff] [blame] | 305 | goto usage; |
| 306 | opt.ec_curve = curve_info->grp_id; |
| 307 | } |
Paul Bakker | d153ef3 | 2014-08-18 12:00:28 +0200 | [diff] [blame] | 308 | #endif |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 309 | else if( strcmp( p, "filename" ) == 0 ) |
| 310 | opt.filename = q; |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 311 | else if( strcmp( p, "use_dev_random" ) == 0 ) |
| 312 | { |
| 313 | opt.use_dev_random = atoi( q ); |
| 314 | if( opt.use_dev_random < 0 || opt.use_dev_random > 1 ) |
| 315 | goto usage; |
| 316 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 317 | else |
| 318 | goto usage; |
| 319 | } |
| 320 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 321 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 322 | fflush( stdout ); |
| 323 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 324 | mbedtls_entropy_init( &entropy ); |
| 325 | #if !defined(_WIN32) && defined(MBEDTLS_FS_IO) |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 326 | if( opt.use_dev_random ) |
| 327 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | if( ( ret = mbedtls_entropy_add_source( &entropy, dev_random_entropy_poll, |
Manuel Pégourié-Gonnard | 7580ba4 | 2015-06-19 10:26:32 +0200 | [diff] [blame] | 329 | NULL, DEV_RANDOM_THRESHOLD, |
| 330 | MBEDTLS_ENTROPY_SOURCE_STRONG ) ) != 0 ) |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 331 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 332 | mbedtls_printf( " failed\n ! mbedtls_entropy_add_source returned -0x%04x\n", -ret ); |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 333 | goto exit; |
| 334 | } |
| 335 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 336 | mbedtls_printf("\n Using /dev/random, so can take a long time! " ); |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 337 | fflush( stdout ); |
| 338 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | #endif /* !_WIN32 && MBEDTLS_FS_IO */ |
Paul Bakker | 1cfc458 | 2014-04-09 15:25:13 +0200 | [diff] [blame] | 340 | |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 341 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 342 | (const unsigned char *) pers, |
| 343 | strlen( pers ) ) ) != 0 ) |
| 344 | { |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 345 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n", -ret ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 346 | goto exit; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * 1.1. Generate the key |
| 351 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 352 | mbedtls_printf( "\n . Generating the private key ..." ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 353 | fflush( stdout ); |
| 354 | |
Hanno Becker | e2dae7e | 2018-11-05 16:54:40 +0000 | [diff] [blame] | 355 | if( ( ret = mbedtls_pk_setup( &key, |
| 356 | mbedtls_pk_info_from_type( (mbedtls_pk_type_t) opt.type ) ) ) != 0 ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 357 | { |
Manuel Pégourié-Gonnard | d9e6a3a | 2015-05-14 19:41:36 +0200 | [diff] [blame] | 358 | mbedtls_printf( " failed\n ! mbedtls_pk_setup returned -0x%04x", -ret ); |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 359 | goto exit; |
| 360 | } |
| 361 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 362 | #if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) |
| 363 | if( opt.type == MBEDTLS_PK_RSA ) |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 364 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 365 | ret = mbedtls_rsa_gen_key( mbedtls_pk_rsa( key ), mbedtls_ctr_drbg_random, &ctr_drbg, |
Hanno Becker | 83aad1f | 2017-08-23 06:45:10 +0100 | [diff] [blame] | 366 | opt.rsa_keysize, 65537 ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 367 | if( ret != 0 ) |
| 368 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 369 | mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned -0x%04x", -ret ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 370 | goto exit; |
| 371 | } |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 372 | } |
| 373 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | #endif /* MBEDTLS_RSA_C */ |
| 375 | #if defined(MBEDTLS_ECP_C) |
| 376 | if( opt.type == MBEDTLS_PK_ECKEY ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 377 | { |
Hanno Becker | e2dae7e | 2018-11-05 16:54:40 +0000 | [diff] [blame] | 378 | ret = mbedtls_ecp_gen_key( (mbedtls_ecp_group_id) opt.ec_curve, |
| 379 | mbedtls_pk_ec( key ), |
Hanno Becker | 83aad1f | 2017-08-23 06:45:10 +0100 | [diff] [blame] | 380 | mbedtls_ctr_drbg_random, &ctr_drbg ); |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 381 | if( ret != 0 ) |
| 382 | { |
Chris Xue | 9a51c03 | 2017-11-05 19:10:51 +0000 | [diff] [blame] | 383 | mbedtls_printf( " failed\n ! mbedtls_ecp_gen_key returned -0x%04x", -ret ); |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 384 | goto exit; |
| 385 | } |
| 386 | } |
| 387 | else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | #endif /* MBEDTLS_ECP_C */ |
Manuel Pégourié-Gonnard | 8c23771 | 2013-11-30 14:36:54 +0100 | [diff] [blame] | 389 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 390 | mbedtls_printf( " failed\n ! key type not supported\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 391 | goto exit; |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * 1.2 Print the key |
| 396 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 397 | mbedtls_printf( " ok\n . Key information:\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 398 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 399 | #if defined(MBEDTLS_RSA_C) |
| 400 | if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 401 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 402 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key ); |
Hanno Becker | 83aad1f | 2017-08-23 06:45:10 +0100 | [diff] [blame] | 403 | |
| 404 | if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 || |
| 405 | ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 ) |
| 406 | { |
| 407 | mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); |
| 408 | goto exit; |
| 409 | } |
| 410 | |
| 411 | mbedtls_mpi_write_file( "N: ", &N, 16, NULL ); |
| 412 | mbedtls_mpi_write_file( "E: ", &E, 16, NULL ); |
| 413 | mbedtls_mpi_write_file( "D: ", &D, 16, NULL ); |
| 414 | mbedtls_mpi_write_file( "P: ", &P, 16, NULL ); |
| 415 | mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ); |
| 416 | mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ); |
| 417 | mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ); |
| 418 | mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 419 | } |
| 420 | else |
| 421 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 422 | #if defined(MBEDTLS_ECP_C) |
| 423 | if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 424 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 425 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key ); |
| 426 | mbedtls_printf( "curve: %s\n", |
| 427 | mbedtls_ecp_curve_info_from_grp_id( ecp->grp.id )->name ); |
| 428 | mbedtls_mpi_write_file( "X_Q: ", &ecp->Q.X, 16, NULL ); |
| 429 | mbedtls_mpi_write_file( "Y_Q: ", &ecp->Q.Y, 16, NULL ); |
| 430 | mbedtls_mpi_write_file( "D: ", &ecp->d , 16, NULL ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 431 | } |
| 432 | else |
| 433 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | mbedtls_printf(" ! key type not supported\n"); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 435 | |
Manuel Pégourié-Gonnard | a39416f | 2014-07-21 17:10:16 +0200 | [diff] [blame] | 436 | /* |
| 437 | * 1.3 Export key |
| 438 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 439 | mbedtls_printf( " . Writing key to file..." ); |
Manuel Pégourié-Gonnard | a39416f | 2014-07-21 17:10:16 +0200 | [diff] [blame] | 440 | |
| 441 | if( ( ret = write_private_key( &key, opt.filename ) ) != 0 ) |
| 442 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 443 | mbedtls_printf( " failed\n" ); |
Manuel Pégourié-Gonnard | a39416f | 2014-07-21 17:10:16 +0200 | [diff] [blame] | 444 | goto exit; |
| 445 | } |
| 446 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 447 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 448 | |
Andres Amaya Garcia | 208c217 | 2018-04-29 19:51:56 +0100 | [diff] [blame] | 449 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 450 | |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 451 | exit: |
| 452 | |
Andres Amaya Garcia | 208c217 | 2018-04-29 19:51:56 +0100 | [diff] [blame] | 453 | if( exit_code != MBEDTLS_EXIT_SUCCESS ) |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 454 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 455 | #ifdef MBEDTLS_ERROR_C |
| 456 | mbedtls_strerror( ret, buf, sizeof( buf ) ); |
| 457 | mbedtls_printf( " - %s\n", buf ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 458 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 459 | mbedtls_printf("\n"); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 460 | #endif |
| 461 | } |
| 462 | |
Hanno Becker | 83aad1f | 2017-08-23 06:45:10 +0100 | [diff] [blame] | 463 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 464 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP ); |
| 465 | mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP ); |
| 466 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 467 | mbedtls_pk_free( &key ); |
| 468 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 469 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 470 | |
| 471 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 472 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 473 | fflush( stdout ); getchar(); |
| 474 | #endif |
| 475 | |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 476 | mbedtls_exit( exit_code ); |
Paul Bakker | 15b9b3a | 2013-09-23 12:05:44 +0200 | [diff] [blame] | 477 | } |
Simon Butcher | 604d399 | 2016-10-06 19:02:49 +0100 | [diff] [blame] | 478 | #endif /* MBEDTLS_PK_WRITE_C && MBEDTLS_PEM_WRITE_C && MBEDTLS_FS_IO && |
| 479 | * MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */ |