blob: 556718cd1ce7fb139fa1005d00ded6742359632b [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
29#include <stdio.h>
30
Paul Bakker508ad5a2011-12-04 17:09:26 +000031#include "polarssl/entropy.h"
32#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000033#include "polarssl/bignum.h"
34#include "polarssl/x509.h"
35#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
37#define KEY_SIZE 1024
38#define EXPONENT 65537
39
Paul Bakker508ad5a2011-12-04 17:09:26 +000040#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000041 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000042 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000043int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000044{
Paul Bakkercce9d772011-11-18 14:26:47 +000045 ((void) argc);
46 ((void) argv);
47
Paul Bakker508ad5a2011-12-04 17:09:26 +000048 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000049 "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000050 "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000051 return( 0 );
52}
53#else
Paul Bakkercce9d772011-11-18 14:26:47 +000054int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000055{
56 int ret;
57 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000058 entropy_context entropy;
59 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000060 FILE *fpub = NULL;
61 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020062 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000063
Paul Bakkercce9d772011-11-18 14:26:47 +000064 ((void) argc);
65 ((void) argv);
66
Paul Bakker5121ce52009-01-03 21:22:43 +000067 printf( "\n . Seeding the random number generator..." );
68 fflush( stdout );
69
Paul Bakker508ad5a2011-12-04 17:09:26 +000070 entropy_init( &entropy );
71 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020072 (const unsigned char *) pers,
73 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000074 {
75 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
76 goto exit;
77 }
Paul Bakker5121ce52009-01-03 21:22:43 +000078
79 printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
80 fflush( stdout );
81
Paul Bakkera802e1a2010-08-16 11:56:45 +000082 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Paul Bakker508ad5a2011-12-04 17:09:26 +000084 if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
85 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000086 {
87 printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
88 goto exit;
89 }
90
91 printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
92 fflush( stdout );
93
94 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
95 {
96 printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
97 ret = 1;
98 goto exit;
99 }
100
101 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
102 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
103 {
104 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
105 goto exit;
106 }
107
108 printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
109 fflush( stdout );
110
111 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
112 {
113 printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
114 ret = 1;
115 goto exit;
116 }
117
118 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
119 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
120 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
121 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
122 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
123 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
124 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
125 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
126 {
127 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
128 goto exit;
129 }
130/*
131 printf( " ok\n . Generating the certificate..." );
132
133 x509write_init_raw( &cert );
134 x509write_add_pubkey( &cert, &rsa );
135 x509write_add_subject( &cert, "CN='localhost'" );
136 x509write_add_validity( &cert, "2007-09-06 17:00:32",
137 "2010-09-06 17:00:32" );
138 x509write_create_selfsign( &cert, &rsa );
139 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
140 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
141 x509write_free_raw( &cert );
142*/
143 printf( " ok\n\n" );
144
145exit:
146
147 if( fpub != NULL )
148 fclose( fpub );
149
150 if( fpriv != NULL )
151 fclose( fpriv );
152
153 rsa_free( &rsa );
Paul Bakkera317a982014-06-18 16:44:11 +0200154 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200155 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Paul Bakkercce9d772011-11-18 14:26:47 +0000157#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 printf( " Press Enter to exit this program.\n" );
159 fflush( stdout ); getchar();
160#endif
161
162 return( ret );
163}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000164#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
165 POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */