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