blob: 0f7f68c3917c68ef4040f68d9c83648f4cdbed22 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Example RSA key generation program
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
17 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
18 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/entropy.h"
20#include "mbedtls/ctr_drbg.h"
21#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000023
Rich Evans18b78c72015-02-11 14:06:19 +000024#include <stdio.h>
25#include <string.h>
26#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnardd224ff12015-08-27 21:42:49 +020028#define KEY_SIZE 2048
Paul Bakker5121ce52009-01-03 21:22:43 +000029#define EXPONENT 65537
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
32 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
33 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000035{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010037 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
38 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
39 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000040}
41#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000042
Simon Butcher63cb97e2018-12-06 17:43:31 +000043
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000045{
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010046 int ret = 1;
47 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_rsa_context rsa;
49 mbedtls_entropy_context entropy;
50 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Beckerf073de02017-08-23 07:42:28 +010051 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +000052 FILE *fpub = NULL;
53 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020054 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 mbedtls_ctr_drbg_init(&ctr_drbg);
57 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
58 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
59 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
60 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 mbedtls_printf("\n . Seeding the random number generator...");
63 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000064
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 mbedtls_entropy_init(&entropy);
66 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
67 (const unsigned char *) pers,
68 strlen(pers))) != 0) {
69 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +000070 goto exit;
71 }
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010073 mbedtls_printf(" ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE);
74 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 if ((ret = mbedtls_rsa_gen_key(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
77 EXPONENT)) != 0) {
78 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +000079 goto exit;
80 }
81
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010082 mbedtls_printf(" ok\n . Exporting the public key in rsa_pub.txt....");
83 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 if ((ret = mbedtls_rsa_export(&rsa, &N, &P, &Q, &D, &E)) != 0 ||
86 (ret = mbedtls_rsa_export_crt(&rsa, &DP, &DQ, &QP)) != 0) {
87 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Beckerf073de02017-08-23 07:42:28 +010088 goto exit;
89 }
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 if ((fpub = fopen("rsa_pub.txt", "wb+")) == NULL) {
92 mbedtls_printf(" failed\n ! could not open rsa_pub.txt for writing\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000093 goto exit;
94 }
95
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpub)) != 0 ||
97 (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpub)) != 0) {
98 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +000099 goto exit;
100 }
101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 mbedtls_printf(" ok\n . Exporting the private key in rsa_priv.txt...");
103 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 if ((fpriv = fopen("rsa_priv.txt", "wb+")) == NULL) {
106 mbedtls_printf(" failed\n ! could not open rsa_priv.txt for writing\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 goto exit;
108 }
109
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpriv)) != 0 ||
111 (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpriv)) != 0 ||
112 (ret = mbedtls_mpi_write_file("D = ", &D, 16, fpriv)) != 0 ||
113 (ret = mbedtls_mpi_write_file("P = ", &P, 16, fpriv)) != 0 ||
114 (ret = mbedtls_mpi_write_file("Q = ", &Q, 16, fpriv)) != 0 ||
115 (ret = mbedtls_mpi_write_file("DP = ", &DP, 16, fpriv)) != 0 ||
116 (ret = mbedtls_mpi_write_file("DQ = ", &DQ, 16, fpriv)) != 0 ||
117 (ret = mbedtls_mpi_write_file("QP = ", &QP, 16, fpriv)) != 0) {
118 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 goto exit;
120 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 mbedtls_printf(" ok\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000122
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +0100123 exit_code = MBEDTLS_EXIT_SUCCESS;
124
Paul Bakker5121ce52009-01-03 21:22:43 +0000125exit:
126
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 if (fpub != NULL) {
128 fclose(fpub);
129 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 if (fpriv != NULL) {
132 fclose(fpriv);
133 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
136 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
137 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
138 mbedtls_rsa_free(&rsa);
139 mbedtls_ctr_drbg_free(&ctr_drbg);
140 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000141
Paul Bakkercce9d772011-11-18 14:26:47 +0000142#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 mbedtls_printf(" Press Enter to exit this program.\n");
144 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000145#endif
146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000148}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
150 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */