blob: 94604ceeb60e31e73dc5574681105f06a544e4ed [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
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Ronald Cron0815c672025-04-12 11:52:18 +020010#include "tf-psa-crypto/build_info.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Gilles Peskine9552a522023-12-23 18:44:20 +010014#if !defined(MBEDTLS_PK_WRITE_C) || !defined(MBEDTLS_PEM_WRITE_C) || \
15 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_ENTROPY_C) || \
16 !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_BIGNUM_C)
17int main(void)
18{
19 mbedtls_printf("MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO and/or "
20 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
21 "MBEDTLS_PEM_WRITE_C and/or MBEDTLS_BIGNUM_C "
22 "not defined.\n");
23 mbedtls_exit(0);
24}
25#else
26
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/pk.h"
Ben Taylorc801d322025-07-03 15:01:39 +010028#if defined(MBEDTLS_PK_HAVE_PRIVATE_HEADER)
29#include <mbedtls/private/pk_private.h>
30#endif /* MBEDTLS_PK_HAVE_PRIVATE_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/ecdsa.h"
32#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/entropy.h"
34#include "mbedtls/ctr_drbg.h"
Paul Bakker15b9b3a2013-09-23 12:05:44 +020035
Rich Evans18b78c72015-02-11 14:06:19 +000036#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
Paul Bakker15b9b3a2013-09-23 12:05:44 +020039
Rich Evans18b78c72015-02-11 14:06:19 +000040#if !defined(_WIN32)
41#include <unistd.h>
Paul Bakker1cfc4582014-04-09 15:25:13 +020042
43#define DEV_RANDOM_THRESHOLD 32
44
Michael Schuster8db8d612024-06-01 21:15:02 +020045static int dev_random_entropy_poll(void *data, unsigned char *output,
Michael Schuster04200932024-06-12 00:05:25 +020046 size_t len, size_t *olen)
Paul Bakker1cfc4582014-04-09 15:25:13 +020047{
48 FILE *file;
49 size_t ret, left = len;
50 unsigned char *p = output;
51 ((void) data);
52
53 *olen = 0;
54
Gilles Peskine449bd832023-01-11 14:50:10 +010055 file = fopen("/dev/random", "rb");
56 if (file == NULL) {
57 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
58 }
Paul Bakker1cfc4582014-04-09 15:25:13 +020059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 while (left > 0) {
Paul Bakker1cfc4582014-04-09 15:25:13 +020061 /* /dev/random can return much less than requested. If so, try again */
Gilles Peskine449bd832023-01-11 14:50:10 +010062 ret = fread(p, 1, left, file);
63 if (ret == 0 && ferror(file)) {
64 fclose(file);
65 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker1cfc4582014-04-09 15:25:13 +020066 }
67
68 p += ret;
69 left -= ret;
Gilles Peskine449bd832023-01-11 14:50:10 +010070 sleep(1);
Paul Bakker1cfc4582014-04-09 15:25:13 +020071 }
Gilles Peskine449bd832023-01-11 14:50:10 +010072 fclose(file);
Paul Bakker1cfc4582014-04-09 15:25:13 +020073 *olen = len;
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 return 0;
Paul Bakker1cfc4582014-04-09 15:25:13 +020076}
Rich Evans18b78c72015-02-11 14:06:19 +000077#endif /* !_WIN32 */
Rich Evans18b78c72015-02-11 14:06:19 +000078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#if defined(MBEDTLS_ECP_C)
Gilles Peskinea73b5772021-07-19 14:36:03 +020080#define DFL_EC_CURVE mbedtls_ecp_curve_list()->grp_id
Rich Evans18b78c72015-02-11 14:06:19 +000081#else
82#define DFL_EC_CURVE 0
83#endif
84
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Rich Evans18b78c72015-02-11 14:06:19 +000086#define USAGE_DEV_RANDOM \
87 " use_dev_random=0|1 default: 0\n"
88#else
89#define USAGE_DEV_RANDOM ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +020091
Rich Evans18b78c72015-02-11 14:06:19 +000092#define FORMAT_PEM 0
93#define FORMAT_DER 1
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095#define DFL_TYPE MBEDTLS_PK_RSA
Rich Evans18b78c72015-02-11 14:06:19 +000096#define DFL_RSA_KEYSIZE 4096
97#define DFL_FILENAME "keyfile.key"
98#define DFL_FORMAT FORMAT_PEM
99#define DFL_USE_DEV_RANDOM 0
100
101#define USAGE \
102 "\n usage: gen_key param=<>...\n" \
103 "\n acceptable parameters:\n" \
104 " type=rsa|ec default: rsa\n" \
105 " rsa_keysize=%%d default: 4096\n" \
106 " ec_curve=%%s see below\n" \
107 " filename=%%s default: keyfile.key\n" \
108 " format=pem|der default: pem\n" \
109 USAGE_DEV_RANDOM \
110 "\n"
111
Simon Butcher63cb97e2018-12-06 17:43:31 +0000112
Rich Evans18b78c72015-02-11 14:06:19 +0000113/*
114 * global options
115 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116struct options {
Rich Evans18b78c72015-02-11 14:06:19 +0000117 int type; /* the type of key to generate */
118 int rsa_keysize; /* length of key in bits */
119 int ec_curve; /* curve identifier for EC keys */
120 const char *filename; /* filename of the key file */
121 int format; /* the output format to use */
122 int use_dev_random; /* use /dev/random as entropy source */
123} opt;
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125static int write_private_key(mbedtls_pk_context *key, const char *output_file)
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200126{
127 int ret;
128 FILE *f;
129 unsigned char output_buf[16000];
130 unsigned char *c = output_buf;
131 size_t len = 0;
132
133 memset(output_buf, 0, 16000);
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 if (opt.format == FORMAT_PEM) {
135 if ((ret = mbedtls_pk_write_key_pem(key, output_buf, 16000)) != 0) {
136 return ret;
137 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 len = strlen((char *) output_buf);
140 } else {
141 if ((ret = mbedtls_pk_write_key_der(key, output_buf, 16000)) < 0) {
142 return ret;
143 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200144
145 len = ret;
Paul Bakker3c38f292014-06-13 17:37:46 +0200146 c = output_buf + sizeof(output_buf) - len;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200147 }
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 if ((f = fopen(output_file, "wb")) == NULL) {
150 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200151 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 if (fwrite(c, 1, len, f) != len) {
154 fclose(f);
155 return -1;
156 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 fclose(f);
159
160 return 0;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200161}
162
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200163#if defined(MBEDTLS_ECP_C)
164static int show_ecp_key(const mbedtls_ecp_keypair *ecp, int has_private)
165{
166 int ret = 0;
167
168 const mbedtls_ecp_curve_info *curve_info =
169 mbedtls_ecp_curve_info_from_grp_id(
170 mbedtls_ecp_keypair_get_group_id(ecp));
171 mbedtls_printf("curve: %s\n", curve_info->name);
172
173 mbedtls_ecp_group grp;
174 mbedtls_ecp_group_init(&grp);
175 mbedtls_mpi D;
176 mbedtls_mpi_init(&D);
177 mbedtls_ecp_point pt;
178 mbedtls_ecp_point_init(&pt);
179 mbedtls_mpi X, Y;
180 mbedtls_mpi_init(&X); mbedtls_mpi_init(&Y);
181
182 MBEDTLS_MPI_CHK(mbedtls_ecp_export(ecp, &grp,
183 (has_private ? &D : NULL),
184 &pt));
185
186 unsigned char point_bin[MBEDTLS_ECP_MAX_PT_LEN];
187 size_t len = 0;
188 MBEDTLS_MPI_CHK(mbedtls_ecp_point_write_binary(
189 &grp, &pt, MBEDTLS_ECP_PF_UNCOMPRESSED,
190 &len, point_bin, sizeof(point_bin)));
191 switch (mbedtls_ecp_get_type(&grp)) {
192 case MBEDTLS_ECP_TYPE_SHORT_WEIERSTRASS:
193 if ((len & 1) == 0 || point_bin[0] != 0x04) {
194 /* Point in an unxepected format. This shouldn't happen. */
195 ret = -1;
196 goto cleanup;
197 }
198 MBEDTLS_MPI_CHK(
199 mbedtls_mpi_read_binary(&X, point_bin + 1, len / 2));
200 MBEDTLS_MPI_CHK(
201 mbedtls_mpi_read_binary(&Y, point_bin + 1 + len / 2, len / 2));
202 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
203 mbedtls_mpi_write_file("Y_Q: ", &Y, 16, NULL);
204 break;
205 case MBEDTLS_ECP_TYPE_MONTGOMERY:
206 MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(&X, point_bin, len));
207 mbedtls_mpi_write_file("X_Q: ", &X, 16, NULL);
208 break;
209 default:
210 mbedtls_printf(
211 "This program does not yet support listing coordinates for this curve type.\n");
212 break;
213 }
214
215 if (has_private) {
216 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
217 }
218
219cleanup:
220 mbedtls_ecp_group_free(&grp);
221 mbedtls_mpi_free(&D);
222 mbedtls_ecp_point_free(&pt);
223 mbedtls_mpi_free(&X); mbedtls_mpi_free(&Y);
224 return ret;
225}
226#endif
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228int main(int argc, char *argv[])
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200229{
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100230 int ret = 1;
231 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_pk_context key;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200233 char buf[1024];
234 int i;
235 char *p, *q;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200236#if defined(MBEDTLS_RSA_C)
Hanno Becker83aad1f2017-08-23 06:45:10 +0100237 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200238#endif /* MBEDTLS_RSA_C */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_entropy_context entropy;
240 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200241 const char *pers = "gen_key";
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_ECP_C)
243 const mbedtls_ecp_curve_info *curve_info;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100244#endif
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200245
246 /*
247 * Set to sane values
248 */
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200249#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
251 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
252 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200253#endif /* MBEDTLS_RSA_C */
PiotrBzdregaf6a9cfa2024-02-11 09:41:56 +0100254
255 mbedtls_entropy_init(&entropy);
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 mbedtls_pk_init(&key);
257 mbedtls_ctr_drbg_init(&ctr_drbg);
258 memset(buf, 0, sizeof(buf));
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200259
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200260#if defined(MBEDTLS_USE_PSA_CRYPTO)
261 psa_status_t status = psa_crypto_init();
262 if (status != PSA_SUCCESS) {
263 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
264 (int) status);
265 goto exit;
266 }
267#endif /* MBEDTLS_USE_PSA_CRYPTO */
268
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000269 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100270usage:
271 mbedtls_printf(USAGE);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 mbedtls_printf(" available ec_curve values:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 curve_info = mbedtls_ecp_curve_list();
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_printf(" %s (default)\n", curve_info->name);
276 while ((++curve_info)->name != NULL) {
277 mbedtls_printf(" %s\n", curve_info->name);
278 }
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100279#endif /* MBEDTLS_ECP_C */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200280 goto exit;
281 }
282
283 opt.type = DFL_TYPE;
284 opt.rsa_keysize = DFL_RSA_KEYSIZE;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100285 opt.ec_curve = DFL_EC_CURVE;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200286 opt.filename = DFL_FILENAME;
287 opt.format = DFL_FORMAT;
Paul Bakker1cfc4582014-04-09 15:25:13 +0200288 opt.use_dev_random = DFL_USE_DEV_RANDOM;
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 for (i = 1; i < argc; i++) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200291 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200293 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200295 *q++ = '\0';
296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (strcmp(p, "type") == 0) {
298 if (strcmp(q, "rsa") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 opt.type = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 } else if (strcmp(q, "ec") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 opt.type = MBEDTLS_PK_ECKEY;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200303 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 }
305 } else if (strcmp(p, "format") == 0) {
306 if (strcmp(q, "pem") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200307 opt.format = FORMAT_PEM;
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 } else if (strcmp(q, "der") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200309 opt.format = FORMAT_DER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200311 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
313 } else if (strcmp(p, "rsa_keysize") == 0) {
314 opt.rsa_keysize = atoi(q);
315 if (opt.rsa_keysize < 1024 ||
316 opt.rsa_keysize > MBEDTLS_MPI_MAX_BITS) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200317 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200319 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 else if (strcmp(p, "ec_curve") == 0) {
322 if ((curve_info = mbedtls_ecp_curve_info_from_name(q)) == NULL) {
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100323 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 }
Gilles Peskinea73b5772021-07-19 14:36:03 +0200325 opt.ec_curve = curve_info->grp_id;
Manuel Pégourié-Gonnard6e16cdb2013-11-30 15:32:47 +0100326 }
Paul Bakkerd153ef32014-08-18 12:00:28 +0200327#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 else if (strcmp(p, "filename") == 0) {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200329 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 } else if (strcmp(p, "use_dev_random") == 0) {
331 opt.use_dev_random = atoi(q);
332 if (opt.use_dev_random < 0 || opt.use_dev_random > 1) {
Paul Bakker1cfc4582014-04-09 15:25:13 +0200333 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 }
335 } else {
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200336 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 }
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200338 }
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 mbedtls_printf("\n . Seeding the random number generator...");
341 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#if !defined(_WIN32) && defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 if (opt.use_dev_random) {
345 if ((ret = mbedtls_entropy_add_source(&entropy, dev_random_entropy_poll,
346 NULL, DEV_RANDOM_THRESHOLD,
347 MBEDTLS_ENTROPY_SOURCE_STRONG)) != 0) {
348 mbedtls_printf(" failed\n ! mbedtls_entropy_add_source returned -0x%04x\n",
349 (unsigned int) -ret);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200350 goto exit;
351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 mbedtls_printf("\n Using /dev/random, so can take a long time! ");
354 fflush(stdout);
Paul Bakker1cfc4582014-04-09 15:25:13 +0200355 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356#endif /* !_WIN32 && MBEDTLS_FS_IO */
Paul Bakker1cfc4582014-04-09 15:25:13 +0200357
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
359 (const unsigned char *) pers,
360 strlen(pers))) != 0) {
361 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%04x\n",
362 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200363 goto exit;
364 }
365
366 /*
367 * 1.1. Generate the key
368 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 mbedtls_printf("\n . Generating the private key ...");
370 fflush(stdout);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if ((ret = mbedtls_pk_setup(&key,
373 mbedtls_pk_info_from_type((mbedtls_pk_type_t) opt.type))) != 0) {
374 mbedtls_printf(" failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100375 goto exit;
376 }
377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (opt.type == MBEDTLS_PK_RSA) {
380 ret = mbedtls_rsa_gen_key(mbedtls_pk_rsa(key), mbedtls_ctr_drbg_random, &ctr_drbg,
381 opt.rsa_keysize, 65537);
382 if (ret != 0) {
383 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned -0x%04x",
384 (unsigned int) -ret);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200385 goto exit;
386 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388#endif /* MBEDTLS_RSA_C */
389#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (opt.type == MBEDTLS_PK_ECKEY) {
391 ret = mbedtls_ecp_gen_key((mbedtls_ecp_group_id) opt.ec_curve,
392 mbedtls_pk_ec(key),
393 mbedtls_ctr_drbg_random, &ctr_drbg);
394 if (ret != 0) {
395 mbedtls_printf(" failed\n ! mbedtls_ecp_gen_key returned -0x%04x",
396 (unsigned int) -ret);
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100397 goto exit;
398 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 } else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard8c237712013-11-30 14:36:54 +0100401 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 mbedtls_printf(" failed\n ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200403 goto exit;
404 }
405
406 /*
407 * 1.2 Print the key
408 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 mbedtls_printf(" ok\n . Key information:\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_RSA) {
413 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(key);
Hanno Becker83aad1f2017-08-23 06:45:10 +0100414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 ||
416 (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) {
417 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Becker83aad1f2017-08-23 06:45:10 +0100418 goto exit;
419 }
420
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 mbedtls_mpi_write_file("N: ", &N, 16, NULL);
422 mbedtls_mpi_write_file("E: ", &E, 16, NULL);
423 mbedtls_mpi_write_file("D: ", &D, 16, NULL);
424 mbedtls_mpi_write_file("P: ", &P, 16, NULL);
425 mbedtls_mpi_write_file("Q: ", &Q, 16, NULL);
426 mbedtls_mpi_write_file("DP: ", &DP, 16, NULL);
427 mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL);
428 mbedtls_mpi_write_file("QP: ", &QP, 16, NULL);
429 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200430#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431#if defined(MBEDTLS_ECP_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (mbedtls_pk_get_type(&key) == MBEDTLS_PK_ECKEY) {
Gilles Peskine52cc2a62023-06-22 22:32:05 +0200433 if (show_ecp_key(mbedtls_pk_ec(key), 1) != 0) {
434 mbedtls_printf(" failed\n ! could not export ECC parameters\n\n");
435 goto exit;
436 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 } else
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200438#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_printf(" ! key type not supported\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200440
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200441 /*
442 * 1.3 Export key
443 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 mbedtls_printf(" . Writing key to file...");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 if ((ret = write_private_key(&key, opt.filename)) != 0) {
447 mbedtls_printf(" failed\n");
Manuel Pégourié-Gonnarda39416f2014-07-21 17:10:16 +0200448 goto exit;
449 }
450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 mbedtls_printf(" ok\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200452
Andres Amaya Garcia208c2172018-04-29 19:51:56 +0100453 exit_code = MBEDTLS_EXIT_SUCCESS;
454
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200455exit:
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200458#ifdef MBEDTLS_ERROR_C
Harry Ramsey9c664052024-10-16 14:08:19 +0100459 mbedtls_printf("Error code: %d", ret);
460 /* mbedtls_strerror(ret, buf, sizeof(buf));
461 mbedtls_printf(" - %s\n", buf); */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200462#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 mbedtls_printf("\n");
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200464#endif
465 }
466
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200467#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
469 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
470 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
Manuel Pégourié-Gonnard660bbf22023-06-12 18:42:40 +0200471#endif /* MBEDTLS_RSA_C */
Hanno Becker83aad1f2017-08-23 06:45:10 +0100472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 mbedtls_pk_free(&key);
474 mbedtls_ctr_drbg_free(&ctr_drbg);
475 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200476#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiel2c1ef092023-04-19 09:38:12 +0200477 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200478#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 mbedtls_exit(exit_code);
Paul Bakker15b9b3a2013-09-23 12:05:44 +0200481}
Gilles Peskine9552a522023-12-23 18:44:20 +0100482#endif /* program viability conditions */