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