1 /*
2 * Example RSA key generation program
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #include <stdlib.h>
33 #define mbedtls_printf printf
34 #define mbedtls_exit exit
35 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
36 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37 #endif /* MBEDTLS_PLATFORM_C */
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)
42 #include "mbedtls/entropy.h"
43 #include "mbedtls/ctr_drbg.h"
44 #include "mbedtls/bignum.h"
45 #include "mbedtls/x509.h"
46 #include "mbedtls/rsa.h"
47
48 #include <stdio.h>
49 #include <string.h>
50 #endif
51
52 #define KEY_SIZE 2048
53 #define EXPONENT 65537
54
55 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
56 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) || \
57 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
main(void)58 int main( void )
59 {
60 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
61 "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
62 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
63 return( 0 );
64 }
65 #else
66
67 #if defined(MBEDTLS_CHECK_PARAMS)
68 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)69 void mbedtls_param_failed( const char *failure_condition,
70 const char *file,
71 int line )
72 {
73 mbedtls_printf( "%s:%i: Input param failed - %s\n",
74 file, line, failure_condition );
75 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
76 }
77 #endif
78
main(void)79 int main( void )
80 {
81 int ret = 1;
82 int exit_code = MBEDTLS_EXIT_FAILURE;
83 mbedtls_rsa_context rsa;
84 mbedtls_entropy_context entropy;
85 mbedtls_ctr_drbg_context ctr_drbg;
86 mbedtls_mpi N, P, Q, D, E, DP, DQ, QP;
87 FILE *fpub = NULL;
88 FILE *fpriv = NULL;
89 const char *pers = "rsa_genkey";
90
91 mbedtls_ctr_drbg_init( &ctr_drbg );
92 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
93 mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
94 mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP );
95 mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP );
96
97 mbedtls_printf( "\n . Seeding the random number generator..." );
98 fflush( stdout );
99
100 mbedtls_entropy_init( &entropy );
101 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
102 (const unsigned char *) pers,
103 strlen( pers ) ) ) != 0 )
104 {
105 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
106 goto exit;
107 }
108
109 mbedtls_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
110 fflush( stdout );
111
112 if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
113 EXPONENT ) ) != 0 )
114 {
115 mbedtls_printf( " failed\n ! mbedtls_rsa_gen_key returned %d\n\n", ret );
116 goto exit;
117 }
118
119 mbedtls_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
120 fflush( stdout );
121
122 if( ( ret = mbedtls_rsa_export ( &rsa, &N, &P, &Q, &D, &E ) ) != 0 ||
123 ( ret = mbedtls_rsa_export_crt( &rsa, &DP, &DQ, &QP ) ) != 0 )
124 {
125 mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" );
126 goto exit;
127 }
128
129 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
130 {
131 mbedtls_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
132 goto exit;
133 }
134
135 if( ( ret = mbedtls_mpi_write_file( "N = ", &N, 16, fpub ) ) != 0 ||
136 ( ret = mbedtls_mpi_write_file( "E = ", &E, 16, fpub ) ) != 0 )
137 {
138 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
139 goto exit;
140 }
141
142 mbedtls_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
143 fflush( stdout );
144
145 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
146 {
147 mbedtls_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
148 goto exit;
149 }
150
151 if( ( ret = mbedtls_mpi_write_file( "N = " , &N , 16, fpriv ) ) != 0 ||
152 ( ret = mbedtls_mpi_write_file( "E = " , &E , 16, fpriv ) ) != 0 ||
153 ( ret = mbedtls_mpi_write_file( "D = " , &D , 16, fpriv ) ) != 0 ||
154 ( ret = mbedtls_mpi_write_file( "P = " , &P , 16, fpriv ) ) != 0 ||
155 ( ret = mbedtls_mpi_write_file( "Q = " , &Q , 16, fpriv ) ) != 0 ||
156 ( ret = mbedtls_mpi_write_file( "DP = ", &DP, 16, fpriv ) ) != 0 ||
157 ( ret = mbedtls_mpi_write_file( "DQ = ", &DQ, 16, fpriv ) ) != 0 ||
158 ( ret = mbedtls_mpi_write_file( "QP = ", &QP, 16, fpriv ) ) != 0 )
159 {
160 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
161 goto exit;
162 }
163 /*
164 mbedtls_printf( " ok\n . Generating the certificate..." );
165
166 x509write_init_raw( &cert );
167 x509write_add_pubkey( &cert, &rsa );
168 x509write_add_subject( &cert, "CN='localhost'" );
169 x509write_add_validity( &cert, "2007-09-06 17:00:32",
170 "2010-09-06 17:00:32" );
171 x509write_create_selfsign( &cert, &rsa );
172 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
173 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
174 x509write_free_raw( &cert );
175 */
176 mbedtls_printf( " ok\n\n" );
177
178 exit_code = MBEDTLS_EXIT_SUCCESS;
179
180 exit:
181
182 if( fpub != NULL )
183 fclose( fpub );
184
185 if( fpriv != NULL )
186 fclose( fpriv );
187
188 mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
189 mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP );
190 mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP );
191 mbedtls_rsa_free( &rsa );
192 mbedtls_ctr_drbg_free( &ctr_drbg );
193 mbedtls_entropy_free( &entropy );
194
195 #if defined(_WIN32)
196 mbedtls_printf( " Press Enter to exit this program.\n" );
197 fflush( stdout ); getchar();
198 #endif
199
200 return( exit_code );
201 }
202 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
203 MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
204