blob: 5741d4663318e2948078c6c06374010ee9530e28 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Example RSA key generation program
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Rich Evans18b78c72015-02-11 14:06:19 +000036#if defined(POLARSSL_BIGNUM_C) && defined(POLARSSL_ENTROPY_C) &&\
37 defined(POLARSSL_RSA_C) && defined(POLARSSL_GENPRIME) &&\
38 defined(POLARSSL_FS_IO) && defined(POLARSSL_CTR_DRBG_C)
Paul Bakker508ad5a2011-12-04 17:09:26 +000039#include "polarssl/entropy.h"
40#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000041#include "polarssl/bignum.h"
42#include "polarssl/x509.h"
43#include "polarssl/rsa.h"
Rich Evans18b78c72015-02-11 14:06:19 +000044
45#include <stdio.h>
46#include <string.h>
47#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000048
49#define KEY_SIZE 1024
50#define EXPONENT 65537
51
Paul Bakker508ad5a2011-12-04 17:09:26 +000052#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000053 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000054 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000055int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000056{
Paul Bakkercce9d772011-11-18 14:26:47 +000057 ((void) argc);
58 ((void) argv);
59
Rich Evansf90016a2015-01-19 14:26:37 +000060 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000061 "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000062 "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000063 return( 0 );
64}
65#else
Paul Bakkercce9d772011-11-18 14:26:47 +000066int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000067{
68 int ret;
69 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000070 entropy_context entropy;
71 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000072 FILE *fpub = NULL;
73 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020074 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Paul Bakkercce9d772011-11-18 14:26:47 +000076 ((void) argc);
77 ((void) argv);
78
Rich Evansf90016a2015-01-19 14:26:37 +000079 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +000080 fflush( stdout );
81
Paul Bakker508ad5a2011-12-04 17:09:26 +000082 entropy_init( &entropy );
83 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020084 (const unsigned char *) pers,
85 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000086 {
Rich Evansf90016a2015-01-19 14:26:37 +000087 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +000088 goto exit;
89 }
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Rich Evansf90016a2015-01-19 14:26:37 +000091 polarssl_printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
Paul Bakker5121ce52009-01-03 21:22:43 +000092 fflush( stdout );
93
Paul Bakkera802e1a2010-08-16 11:56:45 +000094 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000095
Paul Bakker508ad5a2011-12-04 17:09:26 +000096 if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
97 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000098 {
Rich Evansf90016a2015-01-19 14:26:37 +000099 polarssl_printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100 goto exit;
101 }
102
Rich Evansf90016a2015-01-19 14:26:37 +0000103 polarssl_printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 fflush( stdout );
105
106 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
107 {
Rich Evansf90016a2015-01-19 14:26:37 +0000108 polarssl_printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 ret = 1;
110 goto exit;
111 }
112
113 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
114 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
115 {
Rich Evansf90016a2015-01-19 14:26:37 +0000116 polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117 goto exit;
118 }
119
Rich Evansf90016a2015-01-19 14:26:37 +0000120 polarssl_printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 fflush( stdout );
122
123 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
124 {
Rich Evansf90016a2015-01-19 14:26:37 +0000125 polarssl_printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 ret = 1;
127 goto exit;
128 }
129
130 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
131 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
132 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
133 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
134 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
135 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
136 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
137 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
138 {
Rich Evansf90016a2015-01-19 14:26:37 +0000139 polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 goto exit;
141 }
142/*
Rich Evansf90016a2015-01-19 14:26:37 +0000143 polarssl_printf( " ok\n . Generating the certificate..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145 x509write_init_raw( &cert );
146 x509write_add_pubkey( &cert, &rsa );
147 x509write_add_subject( &cert, "CN='localhost'" );
148 x509write_add_validity( &cert, "2007-09-06 17:00:32",
149 "2010-09-06 17:00:32" );
150 x509write_create_selfsign( &cert, &rsa );
151 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
152 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
153 x509write_free_raw( &cert );
154*/
Rich Evansf90016a2015-01-19 14:26:37 +0000155 polarssl_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
157exit:
158
159 if( fpub != NULL )
160 fclose( fpub );
161
162 if( fpriv != NULL )
163 fclose( fpriv );
164
165 rsa_free( &rsa );
Paul Bakkera317a982014-06-18 16:44:11 +0200166 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200167 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
Paul Bakkercce9d772011-11-18 14:26:47 +0000169#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000170 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 fflush( stdout ); getchar();
172#endif
173
174 return( ret );
175}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000176#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
177 POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */