blob: 4bcb8a2ee9caea4707280a9cf7b767d1d21939b0 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
29 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
30 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/entropy.h"
32#include "mbedtls/ctr_drbg.h"
33#include "mbedtls/bignum.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/rsa.h"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +000035
Rich Evans18b78c72015-02-11 14:06:19 +000036#include <stdio.h>
37#include <string.h>
38#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Manuel Pégourié-Gonnardd224ff12015-08-27 21:42:49 +020040#define KEY_SIZE 2048
Paul Bakker5121ce52009-01-03 21:22:43 +000041#define EXPONENT 65537
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
44 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
45 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010046int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
50 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
51 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000052}
53#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000054
Simon Butcher63cb97e2018-12-06 17:43:31 +000055
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000057{
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +010058 int ret = 1;
59 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_rsa_context rsa;
61 mbedtls_entropy_context entropy;
62 mbedtls_ctr_drbg_context ctr_drbg;
Hanno Beckerf073de02017-08-23 07:42:28 +010063 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
Paul Bakker5121ce52009-01-03 21:22:43 +000064 FILE *fpub = NULL;
65 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020066 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000067
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 mbedtls_ctr_drbg_init(&ctr_drbg);
69 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
70 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
71 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
72 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 mbedtls_printf("\n . Seeding the random number generator...");
75 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 mbedtls_entropy_init(&entropy);
78 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
79 (const unsigned char *) pers,
80 strlen(pers))) != 0) {
81 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +000082 goto exit;
83 }
Paul Bakker5121ce52009-01-03 21:22:43 +000084
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010085 mbedtls_printf(" ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE);
86 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000087
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 if ((ret = mbedtls_rsa_gen_key(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
89 EXPONENT)) != 0) {
90 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +000091 goto exit;
92 }
93
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 mbedtls_printf(" ok\n . Exporting the public key in rsa_pub.txt....");
95 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010097 if ((ret = mbedtls_rsa_export(&rsa, &N, &P, &Q, &D, &E)) != 0 ||
98 (ret = mbedtls_rsa_export_crt(&rsa, &DP, &DQ, &QP)) != 0) {
99 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
Hanno Beckerf073de02017-08-23 07:42:28 +0100100 goto exit;
101 }
102
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 if ((fpub = fopen("rsa_pub.txt", "wb+")) == NULL) {
104 mbedtls_printf(" failed\n ! could not open rsa_pub.txt for writing\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 goto exit;
106 }
107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpub)) != 0 ||
109 (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpub)) != 0) {
110 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 goto exit;
112 }
113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 mbedtls_printf(" ok\n . Exporting the private key in rsa_priv.txt...");
115 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 if ((fpriv = fopen("rsa_priv.txt", "wb+")) == NULL) {
118 mbedtls_printf(" failed\n ! could not open rsa_priv.txt for writing\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 goto exit;
120 }
121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpriv)) != 0 ||
123 (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpriv)) != 0 ||
124 (ret = mbedtls_mpi_write_file("D = ", &D, 16, fpriv)) != 0 ||
125 (ret = mbedtls_mpi_write_file("P = ", &P, 16, fpriv)) != 0 ||
126 (ret = mbedtls_mpi_write_file("Q = ", &Q, 16, fpriv)) != 0 ||
127 (ret = mbedtls_mpi_write_file("DP = ", &DP, 16, fpriv)) != 0 ||
128 (ret = mbedtls_mpi_write_file("DQ = ", &DQ, 16, fpriv)) != 0 ||
129 (ret = mbedtls_mpi_write_file("QP = ", &QP, 16, fpriv)) != 0) {
130 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 goto exit;
132 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 mbedtls_printf(" ok\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Andres Amaya Garcia70e1ffd2018-04-29 20:12:43 +0100135 exit_code = MBEDTLS_EXIT_SUCCESS;
136
Paul Bakker5121ce52009-01-03 21:22:43 +0000137exit:
138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 if (fpub != NULL) {
140 fclose(fpub);
141 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 if (fpriv != NULL) {
144 fclose(fpriv);
145 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
148 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
149 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
150 mbedtls_rsa_free(&rsa);
151 mbedtls_ctr_drbg_free(&ctr_drbg);
152 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Paul Bakkercce9d772011-11-18 14:26:47 +0000154#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 mbedtls_printf(" Press Enter to exit this program.\n");
156 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000157#endif
158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000160}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
162 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */