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