• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *  This file is provided under the Apache License 2.0, or the
8  *  GNU General Public License v2.0 or later.
9  *
10  *  **********
11  *  Apache License 2.0:
12  *
13  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
14  *  not use this file except in compliance with the License.
15  *  You may obtain a copy of the License at
16  *
17  *  http://www.apache.org/licenses/LICENSE-2.0
18  *
19  *  Unless required by applicable law or agreed to in writing, software
20  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the License for the specific language governing permissions and
23  *  limitations under the License.
24  *
25  *  **********
26  *
27  *  **********
28  *  GNU General Public License v2.0 or later:
29  *
30  *  This program is free software; you can redistribute it and/or modify
31  *  it under the terms of the GNU General Public License as published by
32  *  the Free Software Foundation; either version 2 of the License, or
33  *  (at your option) any later version.
34  *
35  *  This program is distributed in the hope that it will be useful,
36  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
37  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38  *  GNU General Public License for more details.
39  *
40  *  You should have received a copy of the GNU General Public License along
41  *  with this program; if not, write to the Free Software Foundation, Inc.,
42  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43  *
44  *  **********
45  */
46 
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52 
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_printf          printf
59 #define mbedtls_exit            exit
60 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
61 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
62 #endif /* MBEDTLS_PLATFORM_C */
63 
64 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
65     defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
66     defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
67 #include "mbedtls/entropy.h"
68 #include "mbedtls/ctr_drbg.h"
69 #include "mbedtls/bignum.h"
70 #include "mbedtls/x509.h"
71 #include "mbedtls/rsa.h"
72 
73 #include <stdio.h>
74 #include <string.h>
75 #endif
76 
77 #define KEY_SIZE 2048
78 #define EXPONENT 65537
79 
80 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||   \
81     !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) ||      \
82     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
main(void)83 int main( void )
84 {
85     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
86            "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
87            "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
88     mbedtls_exit( 0 );
89 }
90 #else
91 
92 
main(void)93 int main( void )
94 {
95     int ret = 1;
96     int exit_code = MBEDTLS_EXIT_FAILURE;
97     mbedtls_rsa_context rsa;
98     mbedtls_entropy_context entropy;
99     mbedtls_ctr_drbg_context ctr_drbg;
100     mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
101     FILE *fpub  = NULL;
102     FILE *fpriv = NULL;
103     const char *pers = "rsa_genkey";
104 
105     mbedtls_ctr_drbg_init( &ctr_drbg );
106     mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
107     mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
108     mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
109     mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
110 
111     mbedtls_printf( "\n  . Seeding the random number generator..." );
112     fflush( stdout );
113 
114     mbedtls_entropy_init( &entropy );
115     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
116                                (const unsigned char *) pers,
117                                strlen( pers ) ) ) != 0 )
118     {
119         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
120         goto exit;
121     }
122 
123     mbedtls_printf( " ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
124     fflush( stdout );
125 
126     if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
127                                      EXPONENT ) ) != 0 )
128     {
129         mbedtls_printf( " failed\n  ! mbedtls_rsa_gen_key returned %d\n\n", ret );
130         goto exit;
131     }
132 
133     mbedtls_printf( " ok\n  . Exporting the public  key in rsa_pub.txt...." );
134     fflush( stdout );
135 
136     if( ( ret = mbedtls_rsa_export    ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
137         ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) )      != 0 )
138     {
139         mbedtls_printf( " failed\n  ! could not export RSA parameters\n\n" );
140         goto exit;
141     }
142 
143     if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
144     {
145         mbedtls_printf( " failed\n  ! could not open rsa_pub.txt for writing\n\n" );
146         goto exit;
147     }
148 
149     if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
150         ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
151     {
152         mbedtls_printf( " failed\n  ! mbedtls_mpi_write_file returned %d\n\n", ret );
153         goto exit;
154     }
155 
156     mbedtls_printf( " ok\n  . Exporting the private key in rsa_priv.txt..." );
157     fflush( stdout );
158 
159     if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
160     {
161         mbedtls_printf( " failed\n  ! could not open rsa_priv.txt for writing\n" );
162         goto exit;
163     }
164 
165     if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
166         ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
167         ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
168         ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
169         ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
170         ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
171         ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
172         ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
173     {
174         mbedtls_printf( " failed\n  ! mbedtls_mpi_write_file returned %d\n\n", ret );
175         goto exit;
176     }
177 /*
178     mbedtls_printf( " ok\n  . Generating the certificate..." );
179 
180     x509write_init_raw( &cert );
181     x509write_add_pubkey( &cert, &rsa );
182     x509write_add_subject( &cert, "CN='localhost'" );
183     x509write_add_validity( &cert, "2007-09-06 17:00:32",
184                                    "2010-09-06 17:00:32" );
185     x509write_create_selfsign( &cert, &rsa );
186     x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
187     x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
188     x509write_free_raw( &cert );
189 */
190     mbedtls_printf( " ok\n\n" );
191 
192     exit_code = MBEDTLS_EXIT_SUCCESS;
193 
194 exit:
195 
196     if( fpub  != NULL )
197         fclose( fpub );
198 
199     if( fpriv != NULL )
200         fclose( fpriv );
201 
202     mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
203     mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
204     mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
205     mbedtls_rsa_free( &rsa );
206     mbedtls_ctr_drbg_free( &ctr_drbg );
207     mbedtls_entropy_free( &entropy );
208 
209 #if defined(_WIN32)
210     mbedtls_printf( "  Press Enter to exit this program.\n" );
211     fflush( stdout ); getchar();
212 #endif
213 
214     mbedtls_exit( exit_code );
215 }
216 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
217           MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
218