blob: 3dfa8529ebfe3c3feec469288d24bfe281fe7530 [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 Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
15 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
16 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/entropy.h"
18#include "mbedtls/ctr_drbg.h"
19#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000021
Rich Evans18b78c72015-02-11 14:06:19 +000022#include <stdio.h>
23#include <string.h>
24#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnardd224ff12015-08-27 21:42:49 +020026#define KEY_SIZE 2048
Paul Bakker5121ce52009-01-03 21:22:43 +000027#define EXPONENT 65537
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
30 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
31 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010032int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010035 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
36 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
37 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000038}
39#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000040
Simon Butcher63cb97e2018-12-06 17:43:31 +000041
Gilles Peskine449bd832023-01-11 14:50:10 +010042int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000043{
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010044 int ret = 1;
45 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_rsa_context rsa;
47 mbedtls_entropy_context entropy;
48 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Beckerf073de02017-08-23 07:42:28 +010049 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +000050 FILE *fpub = NULL;
51 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020052 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 mbedtls_ctr_drbg_init(&ctr_drbg);
55 mbedtls_rsa_init(&rsa);
56 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
57 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
58 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 mbedtls_printf("\n . Seeding the random number generator...");
61 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Gilles Peskine449bd832023-01-11 14:50:10 +010063 mbedtls_entropy_init(&entropy);
64 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
65 (const unsigned char *) pers,
66 strlen(pers))) != 0) {
67 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +000068 goto exit;
69 }
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_printf(" ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE);
72 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Gilles Peskine449bd832023-01-11 14:50:10 +010074 if ((ret = mbedtls_rsa_gen_key(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
75 EXPONENT)) != 0) {
76 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +000077 goto exit;
78 }
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_printf(" ok\n . Exporting the public key in rsa_pub.txt....");
81 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 if ((ret = mbedtls_rsa_export(&rsa, &N, &P, &Q, &D, &E)) != 0 ||
84 (ret = mbedtls_rsa_export_crt(&rsa, &DP, &DQ, &QP)) != 0) {
85 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Beckerf073de02017-08-23 07:42:28 +010086 goto exit;
87 }
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089 if ((fpub = fopen("rsa_pub.txt", "wb+")) == NULL) {
90 mbedtls_printf(" failed\n ! could not open rsa_pub.txt for writing\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +000091 goto exit;
92 }
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpub)) != 0 ||
95 (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpub)) != 0) {
96 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +000097 goto exit;
98 }
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_printf(" ok\n . Exporting the private key in rsa_priv.txt...");
101 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 if ((fpriv = fopen("rsa_priv.txt", "wb+")) == NULL) {
104 mbedtls_printf(" failed\n ! could not open rsa_priv.txt for writing\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 goto exit;
106 }
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpriv)) != 0 ||
109 (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpriv)) != 0 ||
110 (ret = mbedtls_mpi_write_file("D = ", &D, 16, fpriv)) != 0 ||
111 (ret = mbedtls_mpi_write_file("P = ", &P, 16, fpriv)) != 0 ||
112 (ret = mbedtls_mpi_write_file("Q = ", &Q, 16, fpriv)) != 0 ||
113 (ret = mbedtls_mpi_write_file("DP = ", &DP, 16, fpriv)) != 0 ||
114 (ret = mbedtls_mpi_write_file("DQ = ", &DQ, 16, fpriv)) != 0 ||
115 (ret = mbedtls_mpi_write_file("QP = ", &QP, 16, fpriv)) != 0) {
116 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 goto exit;
118 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_printf(" ok\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +0100121 exit_code = MBEDTLS_EXIT_SUCCESS;
122
Paul Bakker5121ce52009-01-03 21:22:43 +0000123exit:
124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (fpub != NULL) {
126 fclose(fpub);
127 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if (fpriv != NULL) {
130 fclose(fpriv);
131 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
134 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
135 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
136 mbedtls_rsa_free(&rsa);
137 mbedtls_ctr_drbg_free(&ctr_drbg);
138 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
143 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */