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 {
78 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
79 goto exit;
80 }
81
82 mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
83 fflush( stdout );
84
85 if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
86 EXPONENT ) ) != 0 )
87 {
88 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
89 goto exit;
90 }
91
92 mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
93 fflush( stdout );
94
95 if( ( ret = mbedtls_rsa_export ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
96 ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
97 {
98 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
99 goto exit;
100 }
101
102 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
103 {
104 mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
105 goto exit;
106 }
107
108 if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
109 ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
110 {
111 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
112 goto exit;
113 }
114
115 mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
116 fflush( stdout );
117
118 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
119 {
120 mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
121 goto exit;
122 }
123
124 if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
125 ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
126 ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
127 ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
128 ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
129 ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
130 ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
131 ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
132 {
133 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
134 goto exit;
135 }
136 mbedtls_printf( " ok\n\n" );
137
138 exit_code = MBEDTLS_EXIT_SUCCESS;
139
140 exit:
141
142 if( fpub != NULL )
143 fclose( fpub );
144
145 if( fpriv != NULL )
146 fclose( fpriv );
147
148 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
149 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
150 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
151 mbedtls_rsa_free( &rsa );
152 mbedtls_ctr_drbg_free( &ctr_drbg );
153 mbedtls_entropy_free( &entropy );
154
155 mbedtls_exit( exit_code );
156 }
157 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
158 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
159