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