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