blob: 424097e03f95fa2276b63f276f2b30f98ba83072 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <stdio.h>
31
Paul Bakker5690efc2011-05-26 13:16:06 +000032#include "polarssl/config.h"
33
Paul Bakker508ad5a2011-12-04 17:09:26 +000034#include "polarssl/entropy.h"
35#include "polarssl/ctr_drbg.h"
Paul Bakker40e46942009-01-03 21:51:57 +000036#include "polarssl/bignum.h"
37#include "polarssl/x509.h"
38#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
40#define KEY_SIZE 1024
41#define EXPONENT 65537
42
Paul Bakker508ad5a2011-12-04 17:09:26 +000043#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000044 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000045 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000046int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000047{
Paul Bakkercce9d772011-11-18 14:26:47 +000048 ((void) argc);
49 ((void) argv);
50
Paul Bakker508ad5a2011-12-04 17:09:26 +000051 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Paul Bakker5690efc2011-05-26 13:16:06 +000052 "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000053 "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000054 return( 0 );
55}
56#else
Paul Bakkercce9d772011-11-18 14:26:47 +000057int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000058{
59 int ret;
60 rsa_context rsa;
Paul Bakker508ad5a2011-12-04 17:09:26 +000061 entropy_context entropy;
62 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000063 FILE *fpub = NULL;
64 FILE *fpriv = NULL;
Paul Bakker508ad5a2011-12-04 17:09:26 +000065 char *pers = "rsa_genkey";
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Paul Bakkercce9d772011-11-18 14:26:47 +000067 ((void) argc);
68 ((void) argv);
69
Paul Bakker5121ce52009-01-03 21:22:43 +000070 printf( "\n . Seeding the random number generator..." );
71 fflush( stdout );
72
Paul Bakker508ad5a2011-12-04 17:09:26 +000073 entropy_init( &entropy );
74 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
75 (unsigned char *) pers, strlen( pers ) ) ) != 0 )
76 {
77 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
78 goto exit;
79 }
Paul Bakker5121ce52009-01-03 21:22:43 +000080
81 printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
82 fflush( stdout );
83
Paul Bakkera802e1a2010-08-16 11:56:45 +000084 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Paul Bakker508ad5a2011-12-04 17:09:26 +000086 if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
87 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000088 {
89 printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
90 goto exit;
91 }
92
93 printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
94 fflush( stdout );
95
96 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
97 {
98 printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
99 ret = 1;
100 goto exit;
101 }
102
103 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
104 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
105 {
106 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
107 goto exit;
108 }
109
110 printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
111 fflush( stdout );
112
113 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
114 {
115 printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
116 ret = 1;
117 goto exit;
118 }
119
120 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
121 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
122 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
123 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
124 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
125 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
126 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
127 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
128 {
129 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
130 goto exit;
131 }
132/*
133 printf( " ok\n . Generating the certificate..." );
134
135 x509write_init_raw( &cert );
136 x509write_add_pubkey( &cert, &rsa );
137 x509write_add_subject( &cert, "CN='localhost'" );
138 x509write_add_validity( &cert, "2007-09-06 17:00:32",
139 "2010-09-06 17:00:32" );
140 x509write_create_selfsign( &cert, &rsa );
141 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
142 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
143 x509write_free_raw( &cert );
144*/
145 printf( " ok\n\n" );
146
147exit:
148
149 if( fpub != NULL )
150 fclose( fpub );
151
152 if( fpriv != NULL )
153 fclose( fpriv );
154
155 rsa_free( &rsa );
156
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 */