blob: 99999c7a5bc137e3e2fa0869ae0f9a6d2ad725fa [file] [log] [blame]
Paul Bakker15b9b3a2013-09-23 12:05:44 +02001/*
2 * Key generation application
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker15b9b3a2013-09-23 12:05:44 +02006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +02009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Gilles Peskine9552a522023-12-23 18:44:20 +010012#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)
15int 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é-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/pk.h"
26#include "mbedtls/ecdsa.h"
27#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/entropy.h"
29#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020030
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020034
Rich Evans18b78c72015-02-11 14:06:19 +000035#if !defined(_WIN32)
36#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020037
38#define DEV_RANDOM_THRESHOLD 32
39
Michael Schuster8db8d612024-06-01 21:15:02 +020040static int dev_random_entropy_poll(void *data, unsigned char *output,
Michael Schuster04200932024-06-12 00:05:25 +020041 size_t len, size_t *olen)
Paul Bakker1cfc4582014-04-09 15:25:13 +020042{
43 FILE *file;
44 size_t ret, left = len;
45 unsigned char *p = output;
46 ((void) data);
47
48 *olen = 0;
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050 file = fopen("/dev/random", "rb");
51 if (file == NULL) {
52 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
53 }
Paul Bakker1cfc4582014-04-09 15:25:13 +020054
Gilles Peskine449bd832023-01-11 14:50:10 +010055 while (left > 0) {
Paul Bakker1cfc4582014-04-09 15:25:13 +020056 /* /dev/random can return much less than requested. If so, try again */
Gilles Peskine449bd832023-01-11 14:50:10 +010057 ret = fread(p, 1, left, file);
58 if (ret == 0 && ferror(file)) {
59 fclose(file);
60 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker1cfc4582014-04-09 15:25:13 +020061 }
62
63 p += ret;
64 left -= ret;
Gilles Peskine449bd832023-01-11 14:50:10 +010065 sleep(1);
Paul Bakker1cfc4582014-04-09 15:25:13 +020066 }
Gilles Peskine449bd832023-01-11 14:50:10 +010067 fclose(file);
Paul Bakker1cfc4582014-04-09 15:25:13 +020068 *olen = len;
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070 return 0;
Paul Bakker1cfc4582014-04-09 15:25:13 +020071}
Rich Evans18b78c72015-02-11 14:06:19 +000072#endif /* !_WIN32 */
Rich Evans18b78c72015-02-11 14:06:19 +000073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074#if defined(MBEDTLS_ECP_C)
Gilles Peskinea73b5772021-07-19 14:36:03 +020075#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000076#else
77#define DFL_EC_CURVE 0
78#endif
79
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000081#define USAGE_DEV_RANDOM \
82 " use_dev_random=0|1 default: 0\n"
83#else
84#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +020086
Rich Evans18b78c72015-02-11 14:06:19 +000087#define FORMAT_PEM 0
88#define FORMAT_DER 1
89
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +000091#define DFL_RSA_KEYSIZE 4096
92#define DFL_FILENAME "keyfile.key"
93#define DFL_FORMAT FORMAT_PEM
94#define DFL_USE_DEV_RANDOM 0
95
96#define USAGE \
97 "\n usage: gen_key param=<>...\n" \
98 "\n acceptable parameters:\n" \
99 " type=rsa|ec default: rsa\n" \
100 " rsa_keysize=%%d default: 4096\n" \
101 " ec_curve=%%s see below\n" \
102 " filename=%%s default: keyfile.key\n" \
103 " format=pem|der default: pem\n" \
104 USAGE_DEV_RANDOM \
105 "\n"
106
Simon Butcher63cb97e2018-12-06 17:43:31 +0000107
Rich Evans18b78c72015-02-11 14:06:19 +0000108/*
109 * global options
110 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100111struct options {
Rich Evans18b78c72015-02-11 14:06:19 +0000112 int type; /* the type of key to generate */
113 int rsa_keysize; /* length of key in bits */
114 int ec_curve; /* curve identifier for EC keys */
115 const char *filename; /* filename of the key file */
116 int format; /* the output format to use */
117 int use_dev_random; /* use /dev/random as entropy source */
118} opt;
119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200121{
122 int ret;
123 FILE *f;
124 unsigned char output_buf[16000];
125 unsigned char *c = output_buf;
126 size_t len = 0;
127
128 memset(output_buf, 0, 16000);
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if (opt.format == FORMAT_PEM) {
130 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
131 return ret;
132 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 len = strlen((char *) output_buf);
135 } else {
136 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
137 return ret;
138 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200139
140 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200141 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200142 }
143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if ((f = fopen(output_file, "wb")) == NULL) {
145 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200146 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (fwrite(c, 1, len, f) != len) {
149 fclose(f);
150 return -1;
151 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 fclose(f);
154
155 return 0;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200156}
157
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200158#if defined(MBEDTLS_ECP_C)
159static int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private)
160{
161 int ret = 0;
162
163 const mbedtls_ecp_curve_info *curve_info =
164 mbedtls_ecp_curve_info_from_grp_id(
165 mbedtls_ecp_keypair_get_group_id(ecp));
166 mbedtls_printf("curve: %s\n", curve_info->name);
167
168 mbedtls_ecp_group grp;
169 mbedtls_ecp_group_init(&grp);
170 mbedtls_mpi D;
171 mbedtls_mpi_init(&D);
172 mbedtls_ecp_point pt;
173 mbedtls_ecp_point_init(&pt);
174 mbedtls_mpi X, Y;
175 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
176
177 MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp,
178 (has_private ? &D : NULL),
179 &pt));
180
181 unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN];
182 size_t len = 0;
183 MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary(
184 &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED,
185 &len, point_bin, sizeof(point_bin)));
186 switch (mbedtls_ecp_get_type(&grp)) {
187 case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
188 if ((len & 1) == 0 || point_bin[0] != 0x04) {
189 /* Point in an unxepected format. This shouldn't happen. */
190 ret = -1;
191 goto cleanup;
192 }
193 MBEDTLS_MPI_CHK(
194 mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2));
195 MBEDTLS_MPI_CHK(
196 mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2));
197 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
198 mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL);
199 break;
200 case MBEDTLS_ECP_TYPE_MONTGOMERY:
201 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len));
202 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
203 break;
204 default:
205 mbedtls_printf(
206 "This program does not yet support listing coordinates for this curve type.\n");
207 break;
208 }
209
210 if (has_private) {
211 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
212 }
213
214cleanup:
215 mbedtls_ecp_group_free(&grp);
216 mbedtls_mpi_free(&D);
217 mbedtls_ecp_point_free(&pt);
218 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
219 return ret;
220}
221#endif
222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223int main(int argc, char *argv[])
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200224{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100225 int ret = 1;
226 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200228 char buf[1024];
229 int i;
230 char *p, *q;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200231#if defined(MBEDTLS_RSA_C)
Hanno Becker83aad1f2017-08-23 06:45:10 +0100232 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200233#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_entropy_context entropy;
235 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200236 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#if defined(MBEDTLS_ECP_C)
238 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100239#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200240
241 /*
242 * Set to sane values
243 */
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200244#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
246 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
247 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200248#endif /* MBEDTLS_RSA_C */
PiotrBzdregaf6a9cfa2024-02-11 09:41:56 +0100249
250 mbedtls_entropy_init(&entropy);
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 mbedtls_pk_init(&key);
252 mbedtls_ctr_drbg_init(&ctr_drbg);
253 memset(buf, 0, sizeof(buf));
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200254
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200255#if defined(MBEDTLS_USE_PSA_CRYPTO)
256 psa_status_t status = psa_crypto_init();
257 if (status != PSA_SUCCESS) {
258 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
259 (int) status);
260 goto exit;
261 }
262#endif /* MBEDTLS_USE_PSA_CRYPTO */
263
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000264 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100265usage:
266 mbedtls_printf(USAGE);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 mbedtls_printf(" available ec_curve values:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 curve_info = mbedtls_ecp_curve_list();
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_printf(" %s (default)\n", curve_info->name);
271 while ((++curve_info)->name != NULL) {
272 mbedtls_printf(" %s\n", curve_info->name);
273 }
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100274#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200275 goto exit;
276 }
277
278 opt.type = DFL_TYPE;
279 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100280 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200281 opt.filename = DFL_FILENAME;
282 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200283 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 for (i = 1; i < argc; i++) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200286 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200288 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200290 *q++ = '\0';
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if (strcmp(p, "type") == 0) {
293 if (strcmp(q, "rsa") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 opt.type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 } else if (strcmp(q, "ec") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 opt.type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200298 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 }
300 } else if (strcmp(p, "format") == 0) {
301 if (strcmp(q, "pem") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200302 opt.format = FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 } else if (strcmp(q, "der") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200304 opt.format = FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200306 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 }
308 } else if (strcmp(p, "rsa_keysize") == 0) {
309 opt.rsa_keysize = atoi(q);
310 if (opt.rsa_keysize < 1024 ||
311 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200312 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100313 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200314 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 else if (strcmp(p, "ec_curve") == 0) {
317 if ((curve_info = mbedtls_ecp_curve_info_from_name(q)) == NULL) {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100318 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 }
Gilles Peskinea73b5772021-07-19 14:36:03 +0200320 opt.ec_curve = curve_info->grp_id;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100321 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200322#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 else if (strcmp(p, "filename") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200324 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 } else if (strcmp(p, "use_dev_random") == 0) {
326 opt.use_dev_random = atoi(q);
327 if (opt.use_dev_random < 0 || opt.use_dev_random > 1) {
Paul Bakker1cfc4582014-04-09 15:25:13 +0200328 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 }
330 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200331 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200333 }
334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 mbedtls_printf("\n . Seeding the random number generator...");
336 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 if (opt.use_dev_random) {
340 if ((ret = mbedtls_entropy_add_source(&entropy, dev_random_entropy_poll,
341 NULL, DEV_RANDOM_THRESHOLD,
342 MBEDTLS_ENTROPY_SOURCE_STRONG)) != 0) {
343 mbedtls_printf(" failed\n ! mbedtls_entropy_add_source returned -0x%04x\n",
344 (unsigned int) -ret);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200345 goto exit;
346 }
347
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 mbedtls_printf("\n Using /dev/random, so can take a long time! ");
349 fflush(stdout);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
354 (const unsigned char *) pers,
355 strlen(pers))) != 0) {
356 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
357 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200358 goto exit;
359 }
360
361 /*
362 * 1.1. Generate the key
363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 mbedtls_printf("\n . Generating the private key ...");
365 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if ((ret = mbedtls_pk_setup(&key,
368 mbedtls_pk_info_from_type((mbedtls_pk_type_t) opt.type))) != 0) {
369 mbedtls_printf(" failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100370 goto exit;
371 }
372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (opt.type == MBEDTLS_PK_RSA) {
375 ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg,
376 opt.rsa_keysize, 65537);
377 if (ret != 0) {
378 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned -0x%04x",
379 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200380 goto exit;
381 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383#endif /* MBEDTLS_RSA_C */
384#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (opt.type == MBEDTLS_PK_ECKEY) {
386 ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve,
387 mbedtls_pk_ec(key),
388 mbedtls_ctr_drbg_random, &ctr_drbg);
389 if (ret != 0) {
390 mbedtls_printf(" failed\n ! mbedtls_ecp_gen_key returned -0x%04x",
391 (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100392 goto exit;
393 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200395#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100396 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 mbedtls_printf(" failed\n ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200398 goto exit;
399 }
400
401 /*
402 * 1.2 Print the key
403 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100404 mbedtls_printf(" ok\n . Key information:\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200406#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
408 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100409
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
411 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
412 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker83aad1f2017-08-23 06:45:10 +0100413 goto exit;
414 }
415
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
417 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
418 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
419 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
420 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
421 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
422 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
423 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
424 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200425#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200426#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200428 if (show_ecp_key(mbedtls_pk_ec(key), 1) != 0) {
429 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
430 goto exit;
431 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200433#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200435
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200436 /*
437 * 1.3 Export key
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_printf(" . Writing key to file...");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200440
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 if ((ret = write_private_key(&key, opt.filename)) != 0) {
442 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200443 goto exit;
444 }
445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 mbedtls_printf(" ok\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200447
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100448 exit_code = MBEDTLS_EXIT_SUCCESS;
449
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200450exit:
451
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200453#ifdef MBEDTLS_ERROR_C
Harry Ramsey9c664052024-10-16 14:08:19 +0100454 mbedtls_printf("Error code: %d", ret);
455 /* mbedtls_strerror(ret, buf, sizeof(buf));
456 mbedtls_printf(" - %s\n", buf); */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200457#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200459#endif
460 }
461
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200462#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
464 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
465 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200466#endif /* MBEDTLS_RSA_C */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 mbedtls_pk_free(&key);
469 mbedtls_ctr_drbg_free(&ctr_drbg);
470 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200471#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200472 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200473#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 mbedtls_exit(exit_code);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200476}
Gilles Peskine9552a522023-12-23 18:44:20 +0100477#endif /* program viability conditions */