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