blob: 83d7b71875ecb69c064a4010ee1a88b0fff0fdac [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/error.h"
26#include "mbedtls/pk.h"
27#include "mbedtls/ecdsa.h"
28#include "mbedtls/rsa.h"
29#include "mbedtls/error.h"
30#include "mbedtls/entropy.h"
31#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020032
Rich Evans18b78c72015-02-11 14:06:19 +000033#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020036
Rich Evans18b78c72015-02-11 14:06:19 +000037#if !defined(_WIN32)
38#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020039
40#define DEV_RANDOM_THRESHOLD 32
41
Michael Schuster6fa32fd2024-06-01 21:15:02 +020042static int dev_random_entropy_poll(void *data, unsigned char *output,
Michael Schuster82984bc2024-06-12 00:05:25 +020043 size_t len, size_t *olen)
Paul Bakker1cfc4582014-04-09 15:25:13 +020044{
45 FILE *file;
46 size_t ret, left = len;
47 unsigned char *p = output;
48 ((void) data);
49
50 *olen = 0;
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052 file = fopen("/dev/random", "rb");
53 if (file == NULL) {
54 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
55 }
Paul Bakker1cfc4582014-04-09 15:25:13 +020056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 while (left > 0) {
Paul Bakker1cfc4582014-04-09 15:25:13 +020058 /* /dev/random can return much less than requested. If so, try again */
Gilles Peskine449bd832023-01-11 14:50:10 +010059 ret = fread(p, 1, left, file);
60 if (ret == 0 && ferror(file)) {
61 fclose(file);
62 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker1cfc4582014-04-09 15:25:13 +020063 }
64
65 p += ret;
66 left -= ret;
Gilles Peskine449bd832023-01-11 14:50:10 +010067 sleep(1);
Paul Bakker1cfc4582014-04-09 15:25:13 +020068 }
Gilles Peskine449bd832023-01-11 14:50:10 +010069 fclose(file);
Paul Bakker1cfc4582014-04-09 15:25:13 +020070 *olen = len;
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072 return 0;
Paul Bakker1cfc4582014-04-09 15:25:13 +020073}
Rich Evans18b78c72015-02-11 14:06:19 +000074#endif /* !_WIN32 */
Rich Evans18b78c72015-02-11 14:06:19 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076#if defined(MBEDTLS_ECP_C)
Gilles Peskinea73b5772021-07-19 14:36:03 +020077#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000078#else
79#define DFL_EC_CURVE 0
80#endif
81
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000083#define USAGE_DEV_RANDOM \
84 " use_dev_random=0|1 default: 0\n"
85#else
86#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +020088
Rich Evans18b78c72015-02-11 14:06:19 +000089#define FORMAT_PEM 0
90#define FORMAT_DER 1
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +000093#define DFL_RSA_KEYSIZE 4096
94#define DFL_FILENAME "keyfile.key"
95#define DFL_FORMAT FORMAT_PEM
96#define DFL_USE_DEV_RANDOM 0
97
98#define USAGE \
99 "\n usage: gen_key param=<>...\n" \
100 "\n acceptable parameters:\n" \
101 " type=rsa|ec default: rsa\n" \
102 " rsa_keysize=%%d default: 4096\n" \
103 " ec_curve=%%s see below\n" \
104 " filename=%%s default: keyfile.key\n" \
105 " format=pem|der default: pem\n" \
106 USAGE_DEV_RANDOM \
107 "\n"
108
Simon Butcher63cb97e2018-12-06 17:43:31 +0000109
Rich Evans18b78c72015-02-11 14:06:19 +0000110/*
111 * global options
112 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100113struct options {
Rich Evans18b78c72015-02-11 14:06:19 +0000114 int type; /* the type of key to generate */
115 int rsa_keysize; /* length of key in bits */
116 int ec_curve; /* curve identifier for EC keys */
117 const char *filename; /* filename of the key file */
118 int format; /* the output format to use */
119 int use_dev_random; /* use /dev/random as entropy source */
120} opt;
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200123{
124 int ret;
125 FILE *f;
126 unsigned char output_buf[16000];
127 unsigned char *c = output_buf;
128 size_t len = 0;
129
130 memset(output_buf, 0, 16000);
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 if (opt.format == FORMAT_PEM) {
132 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
133 return ret;
134 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 len = strlen((char *) output_buf);
137 } else {
138 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
139 return ret;
140 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200141
142 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200143 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200144 }
145
Gilles Peskine449bd832023-01-11 14:50:10 +0100146 if ((f = fopen(output_file, "wb")) == NULL) {
147 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200148 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (fwrite(c, 1, len, f) != len) {
151 fclose(f);
152 return -1;
153 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 fclose(f);
156
157 return 0;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200158}
159
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200160#if defined(MBEDTLS_ECP_C)
161static int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private)
162{
163 int ret = 0;
164
165 const mbedtls_ecp_curve_info *curve_info =
166 mbedtls_ecp_curve_info_from_grp_id(
167 mbedtls_ecp_keypair_get_group_id(ecp));
168 mbedtls_printf("curve: %s\n", curve_info->name);
169
170 mbedtls_ecp_group grp;
171 mbedtls_ecp_group_init(&grp);
172 mbedtls_mpi D;
173 mbedtls_mpi_init(&D);
174 mbedtls_ecp_point pt;
175 mbedtls_ecp_point_init(&pt);
176 mbedtls_mpi X, Y;
177 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
178
179 MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp,
180 (has_private ? &D : NULL),
181 &pt));
182
183 unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN];
184 size_t len = 0;
185 MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary(
186 &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED,
187 &len, point_bin, sizeof(point_bin)));
188 switch (mbedtls_ecp_get_type(&grp)) {
189 case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
190 if ((len & 1) == 0 || point_bin[0] != 0x04) {
191 /* Point in an unxepected format. This shouldn't happen. */
192 ret = -1;
193 goto cleanup;
194 }
195 MBEDTLS_MPI_CHK(
196 mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2));
197 MBEDTLS_MPI_CHK(
198 mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2));
199 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
200 mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL);
201 break;
202 case MBEDTLS_ECP_TYPE_MONTGOMERY:
203 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len));
204 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
205 break;
206 default:
207 mbedtls_printf(
208 "This program does not yet support listing coordinates for this curve type.\n");
209 break;
210 }
211
212 if (has_private) {
213 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
214 }
215
216cleanup:
217 mbedtls_ecp_group_free(&grp);
218 mbedtls_mpi_free(&D);
219 mbedtls_ecp_point_free(&pt);
220 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
221 return ret;
222}
223#endif
224
Gilles Peskine449bd832023-01-11 14:50:10 +0100225int main(int argc, char *argv[])
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200226{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100227 int ret = 1;
228 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200230 char buf[1024];
231 int i;
232 char *p, *q;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200233#if defined(MBEDTLS_RSA_C)
Hanno Becker83aad1f2017-08-23 06:45:10 +0100234 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200235#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 mbedtls_entropy_context entropy;
237 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200238 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_ECP_C)
240 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100241#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200242
243 /*
244 * Set to sane values
245 */
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200246#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
248 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
249 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200250#endif /* MBEDTLS_RSA_C */
PiotrBzdregaf6a9cfa2024-02-11 09:41:56 +0100251
252 mbedtls_entropy_init(&entropy);
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_pk_init(&key);
254 mbedtls_ctr_drbg_init(&ctr_drbg);
255 memset(buf, 0, sizeof(buf));
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200256
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200257#if defined(MBEDTLS_USE_PSA_CRYPTO)
258 psa_status_t status = psa_crypto_init();
259 if (status != PSA_SUCCESS) {
260 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
261 (int) status);
262 goto exit;
263 }
264#endif /* MBEDTLS_USE_PSA_CRYPTO */
265
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000266 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100267usage:
268 mbedtls_printf(USAGE);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 mbedtls_printf(" available ec_curve values:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 curve_info = mbedtls_ecp_curve_list();
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 mbedtls_printf(" %s (default)\n", curve_info->name);
273 while ((++curve_info)->name != NULL) {
274 mbedtls_printf(" %s\n", curve_info->name);
275 }
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100276#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200277 goto exit;
278 }
279
280 opt.type = DFL_TYPE;
281 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100282 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200283 opt.filename = DFL_FILENAME;
284 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200285 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 for (i = 1; i < argc; i++) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200288 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200290 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200292 *q++ = '\0';
293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 if (strcmp(p, "type") == 0) {
295 if (strcmp(q, "rsa") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 opt.type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 } else if (strcmp(q, "ec") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 opt.type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200300 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 }
302 } else if (strcmp(p, "format") == 0) {
303 if (strcmp(q, "pem") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200304 opt.format = FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100305 } else if (strcmp(q, "der") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200306 opt.format = FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200308 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 }
310 } else if (strcmp(p, "rsa_keysize") == 0) {
311 opt.rsa_keysize = atoi(q);
312 if (opt.rsa_keysize < 1024 ||
313 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200314 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200316 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 else if (strcmp(p, "ec_curve") == 0) {
319 if ((curve_info = mbedtls_ecp_curve_info_from_name(q)) == NULL) {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100320 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 }
Gilles Peskinea73b5772021-07-19 14:36:03 +0200322 opt.ec_curve = curve_info->grp_id;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100323 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200324#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 else if (strcmp(p, "filename") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200326 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 } else if (strcmp(p, "use_dev_random") == 0) {
328 opt.use_dev_random = atoi(q);
329 if (opt.use_dev_random < 0 || opt.use_dev_random > 1) {
Paul Bakker1cfc4582014-04-09 15:25:13 +0200330 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 }
332 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200333 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200335 }
336
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 mbedtls_printf("\n . Seeding the random number generator...");
338 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (opt.use_dev_random) {
342 if ((ret = mbedtls_entropy_add_source(&entropy, dev_random_entropy_poll,
343 NULL, DEV_RANDOM_THRESHOLD,
344 MBEDTLS_ENTROPY_SOURCE_STRONG)) != 0) {
345 mbedtls_printf(" failed\n ! mbedtls_entropy_add_source returned -0x%04x\n",
346 (unsigned int) -ret);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200347 goto exit;
348 }
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 mbedtls_printf("\n Using /dev/random, so can take a long time! ");
351 fflush(stdout);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200352 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200353#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
356 (const unsigned char *) pers,
357 strlen(pers))) != 0) {
358 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
359 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200360 goto exit;
361 }
362
363 /*
364 * 1.1. Generate the key
365 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 mbedtls_printf("\n . Generating the private key ...");
367 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if ((ret = mbedtls_pk_setup(&key,
370 mbedtls_pk_info_from_type((mbedtls_pk_type_t) opt.type))) != 0) {
371 mbedtls_printf(" failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100372 goto exit;
373 }
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 if (opt.type == MBEDTLS_PK_RSA) {
377 ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg,
378 opt.rsa_keysize, 65537);
379 if (ret != 0) {
380 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned -0x%04x",
381 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200382 goto exit;
383 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385#endif /* MBEDTLS_RSA_C */
386#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 if (opt.type == MBEDTLS_PK_ECKEY) {
388 ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve,
389 mbedtls_pk_ec(key),
390 mbedtls_ctr_drbg_random, &ctr_drbg);
391 if (ret != 0) {
392 mbedtls_printf(" failed\n ! mbedtls_ecp_gen_key returned -0x%04x",
393 (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100394 goto exit;
395 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100398 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 mbedtls_printf(" failed\n ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200400 goto exit;
401 }
402
403 /*
404 * 1.2 Print the key
405 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 mbedtls_printf(" ok\n . Key information:\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
410 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
413 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
414 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker83aad1f2017-08-23 06:45:10 +0100415 goto exit;
416 }
417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
419 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
420 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
421 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
422 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
423 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
424 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
425 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
426 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200427#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200430 if (show_ecp_key(mbedtls_pk_ec(key), 1) != 0) {
431 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
432 goto exit;
433 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200435#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200437
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200438 /*
439 * 1.3 Export key
440 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_printf(" . Writing key to file...");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if ((ret = write_private_key(&key, opt.filename)) != 0) {
444 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200445 goto exit;
446 }
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 mbedtls_printf(" ok\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200449
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100450 exit_code = MBEDTLS_EXIT_SUCCESS;
451
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200452exit:
453
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 mbedtls_strerror(ret, buf, sizeof(buf));
457 mbedtls_printf(" - %s\n", buf);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200458#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200460#endif
461 }
462
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200463#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
465 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
466 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200467#endif /* MBEDTLS_RSA_C */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 mbedtls_pk_free(&key);
470 mbedtls_ctr_drbg_free(&ctr_drbg);
471 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200472#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200473 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200474#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200475
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 mbedtls_exit(exit_code);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200477}
Gilles Peskine9552a522023-12-23 18:44:20 +0100478#endif /* program viability conditions */