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