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