1 /*
2 * Example RSA key generation program
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #include "mbedtls/build_info.h"
9
10 #include "mbedtls/platform.h"
11
12 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
13 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
14 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
15 #include "mbedtls/entropy.h"
16 #include "mbedtls/ctr_drbg.h"
17 #include "mbedtls/bignum.h"
18 #include "mbedtls/rsa.h"
19
20 #include <stdio.h>
21 #include <string.h>
22 #endif
23
24 #define KEY_SIZE 2048
25 #define EXPONENT 65537
26
27 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
28 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
29 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
main(void)30 int main(void)
31 {
32 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
33 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
34 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
35 mbedtls_exit(0);
36 }
37 #else
38
39
main(void)40 int main(void)
41 {
42 int ret = 1;
43 int exit_code = MBEDTLS_EXIT_FAILURE;
44 mbedtls_rsa_context rsa;
45 mbedtls_entropy_context entropy;
46 mbedtls_ctr_drbg_context ctr_drbg;
47 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
48 FILE *fpub = NULL;
49 FILE *fpriv = NULL;
50 const char *pers = "rsa_genkey";
51
52 mbedtls_ctr_drbg_init(&ctr_drbg);
53 mbedtls_rsa_init(&rsa);
54 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
55 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP);
56 mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP);
57
58 mbedtls_printf("\n . Seeding the random number generator...");
59 fflush(stdout);
60
61 mbedtls_entropy_init(&entropy);
62 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
63 (const unsigned char *) pers,
64 strlen(pers))) != 0) {
65 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
66 goto exit;
67 }
68
69 mbedtls_printf(" ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE);
70 fflush(stdout);
71
72 if ((ret = mbedtls_rsa_gen_key(&rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
73 EXPONENT)) != 0) {
74 mbedtls_printf(" failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret);
75 goto exit;
76 }
77
78 mbedtls_printf(" ok\n . Exporting the public key in rsa_pub.txt....");
79 fflush(stdout);
80
81 if ((ret = mbedtls_rsa_export(&rsa, &N, &P, &Q, &D, &E)) != 0 ||
82 (ret = mbedtls_rsa_export_crt(&rsa, &DP, &DQ, &QP)) != 0) {
83 mbedtls_printf(" failed\n ! could not export RSA parameters\n\n");
84 goto exit;
85 }
86
87 if ((fpub = fopen("rsa_pub.txt", "wb+")) == NULL) {
88 mbedtls_printf(" failed\n ! could not open rsa_pub.txt for writing\n\n");
89 goto exit;
90 }
91
92 if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpub)) != 0 ||
93 (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpub)) != 0) {
94 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
95 goto exit;
96 }
97
98 mbedtls_printf(" ok\n . Exporting the private key in rsa_priv.txt...");
99 fflush(stdout);
100
101 if ((fpriv = fopen("rsa_priv.txt", "wb+")) == NULL) {
102 mbedtls_printf(" failed\n ! could not open rsa_priv.txt for writing\n");
103 goto exit;
104 }
105
106 if ((ret = mbedtls_mpi_write_file("N = ", &N, 16, fpriv)) != 0 ||
107 (ret = mbedtls_mpi_write_file("E = ", &E, 16, fpriv)) != 0 ||
108 (ret = mbedtls_mpi_write_file("D = ", &D, 16, fpriv)) != 0 ||
109 (ret = mbedtls_mpi_write_file("P = ", &P, 16, fpriv)) != 0 ||
110 (ret = mbedtls_mpi_write_file("Q = ", &Q, 16, fpriv)) != 0 ||
111 (ret = mbedtls_mpi_write_file("DP = ", &DP, 16, fpriv)) != 0 ||
112 (ret = mbedtls_mpi_write_file("DQ = ", &DQ, 16, fpriv)) != 0 ||
113 (ret = mbedtls_mpi_write_file("QP = ", &QP, 16, fpriv)) != 0) {
114 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
115 goto exit;
116 }
117 mbedtls_printf(" ok\n\n");
118
119 exit_code = MBEDTLS_EXIT_SUCCESS;
120
121 exit:
122
123 if (fpub != NULL) {
124 fclose(fpub);
125 }
126
127 if (fpriv != NULL) {
128 fclose(fpriv);
129 }
130
131 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
132 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP);
133 mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP);
134 mbedtls_rsa_free(&rsa);
135 mbedtls_ctr_drbg_free(&ctr_drbg);
136 mbedtls_entropy_free(&entropy);
137
138 mbedtls_exit(exit_code);
139 }
140 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
141 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
142