blob: f92c50b6e946cda936ee99ec6ca0153d9d5194a8 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Example RSA key generation program
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#include <stdio.h>
29
Paul Bakker508ad5a2011-12-04 17:09:26 +000030#include "polarssl/entropy.h"
31#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/bignum.h"
33#include "polarssl/x509.h"
34#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
36#define KEY_SIZE 1024
37#define EXPONENT 65537
38
Paul Bakker508ad5a2011-12-04 17:09:26 +000039#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000040 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000041 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000042int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000043{
Paul Bakkercce9d772011-11-18 14:26:47 +000044 ((void) argc);
45 ((void) argv);
46
Paul Bakker508ad5a2011-12-04 17:09:26 +000047 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000048 "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000049 "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000050 return( 0 );
51}
52#else
Paul Bakkercce9d772011-11-18 14:26:47 +000053int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000054{
55 int ret;
56 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000057 entropy_context entropy;
58 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000059 FILE *fpub = NULL;
60 FILE *fpriv = NULL;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020061 const char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Paul Bakkercce9d772011-11-18 14:26:47 +000063 ((void) argc);
64 ((void) argv);
65
Paul Bakker5121ce52009-01-03 21:22:43 +000066 printf( "\n . Seeding the random number generator..." );
67 fflush( stdout );
68
Paul Bakker508ad5a2011-12-04 17:09:26 +000069 entropy_init( &entropy );
70 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020071 (const unsigned char *) pers,
72 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000073 {
74 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
75 goto exit;
76 }
Paul Bakker5121ce52009-01-03 21:22:43 +000077
78 printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
79 fflush( stdout );
80
Paul Bakkera802e1a2010-08-16 11:56:45 +000081 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Paul Bakker508ad5a2011-12-04 17:09:26 +000083 if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
84 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000085 {
86 printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
87 goto exit;
88 }
89
90 printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
91 fflush( stdout );
92
93 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
94 {
95 printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
96 ret = 1;
97 goto exit;
98 }
99
100 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
101 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
102 {
103 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
104 goto exit;
105 }
106
107 printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
108 fflush( stdout );
109
110 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
111 {
112 printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
113 ret = 1;
114 goto exit;
115 }
116
117 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
118 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
119 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
120 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
121 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
122 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
123 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
124 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
125 {
126 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
127 goto exit;
128 }
129/*
130 printf( " ok\n . Generating the certificate..." );
131
132 x509write_init_raw( &cert );
133 x509write_add_pubkey( &cert, &rsa );
134 x509write_add_subject( &cert, "CN='localhost'" );
135 x509write_add_validity( &cert, "2007-09-06 17:00:32",
136 "2010-09-06 17:00:32" );
137 x509write_create_selfsign( &cert, &rsa );
138 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
139 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
140 x509write_free_raw( &cert );
141*/
142 printf( " ok\n\n" );
143
144exit:
145
146 if( fpub != NULL )
147 fclose( fpub );
148
149 if( fpriv != NULL )
150 fclose( fpriv );
151
152 rsa_free( &rsa );
153
Paul Bakkercce9d772011-11-18 14:26:47 +0000154#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 printf( " Press Enter to exit this program.\n" );
156 fflush( stdout ); getchar();
157#endif
158
159 return( ret );
160}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000161#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
162 POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */