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