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