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 #define mbedtls_printf     printf
33 #endif
34 
35 #if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
36     defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME) && \
37     defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
38 #include "mbedtls/entropy.h"
39 #include "mbedtls/ctr_drbg.h"
40 #include "mbedtls/bignum.h"
41 #include "mbedtls/x509.h"
42 #include "mbedtls/rsa.h"
43 
44 #include <stdio.h>
45 #include <string.h>
46 #endif
47 
48 #define KEY_SIZE 2048
49 #define EXPONENT 65537
50 
51 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||   \
52     !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_GENPRIME) ||      \
53     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
main(void)54 int main( void )
55 {
56     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
57            "MBEDTLS_RSA_C and/or MBEDTLS_GENPRIME and/or "
58            "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C not defined.\n");
59     return( 0 );
60 }
61 #else
main(void)62 int main( void )
63 {
64     int ret;
65     mbedtls_rsa_context rsa;
66     mbedtls_entropy_context entropy;
67     mbedtls_ctr_drbg_context ctr_drbg;
68     FILE *fpub  = NULL;
69     FILE *fpriv = NULL;
70     const char *pers = "rsa_genkey";
71 
72     mbedtls_ctr_drbg_init( &ctr_drbg );
73 
74     mbedtls_printf( "\n  . Seeding the random number generator..." );
75     fflush( stdout );
76 
77     mbedtls_entropy_init( &entropy );
78     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
79                                (const unsigned char *) pers,
80                                strlen( pers ) ) ) != 0 )
81     {
82         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
83         goto exit;
84     }
85 
86     mbedtls_printf( " ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
87     fflush( stdout );
88 
89     mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
90 
91     if( ( ret = mbedtls_rsa_gen_key( &rsa, mbedtls_ctr_drbg_random, &ctr_drbg, KEY_SIZE,
92                              EXPONENT ) ) != 0 )
93     {
94         mbedtls_printf( " failed\n  ! mbedtls_rsa_gen_key returned %d\n\n", ret );
95         goto exit;
96     }
97 
98     mbedtls_printf( " ok\n  . Exporting the public  key in rsa_pub.txt...." );
99     fflush( stdout );
100 
101     if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
102     {
103         mbedtls_printf( " failed\n  ! could not open rsa_pub.txt for writing\n\n" );
104         ret = 1;
105         goto exit;
106     }
107 
108     if( ( ret = mbedtls_mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
109         ( ret = mbedtls_mpi_write_file( "E = ", &rsa.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         ret = 1;
122         goto exit;
123     }
124 
125     if( ( ret = mbedtls_mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
126         ( ret = mbedtls_mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
127         ( ret = mbedtls_mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
128         ( ret = mbedtls_mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
129         ( ret = mbedtls_mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
130         ( ret = mbedtls_mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
131         ( ret = mbedtls_mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
132         ( ret = mbedtls_mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
133     {
134         mbedtls_printf( " failed\n  ! mbedtls_mpi_write_file returned %d\n\n", ret );
135         goto exit;
136     }
137 /*
138     mbedtls_printf( " ok\n  . Generating the certificate..." );
139 
140     x509write_init_raw( &cert );
141     x509write_add_pubkey( &cert, &rsa );
142     x509write_add_subject( &cert, "CN='localhost'" );
143     x509write_add_validity( &cert, "2007-09-06 17:00:32",
144                                    "2010-09-06 17:00:32" );
145     x509write_create_selfsign( &cert, &rsa );
146     x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
147     x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
148     x509write_free_raw( &cert );
149 */
150     mbedtls_printf( " ok\n\n" );
151 
152 exit:
153 
154     if( fpub  != NULL )
155         fclose( fpub );
156 
157     if( fpriv != NULL )
158         fclose( fpriv );
159 
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     return( ret );
170 }
171 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_RSA_C &&
172           MBEDTLS_GENPRIME && MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
173