blob: 68e33e56f8c6b953f48a81d11883c50953a3d524 [file] [log] [blame] [raw]
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 Bakkeref3f8c72013-06-24 13:01:08 +020065 const char *pers = "rsa_genkey";
Paul Bakkercce9d772011-11-18 14:26:47 +000066
67 ((void) argc);
68 ((void) argv);
Paul Bakker5121ce52009-01-03 21:22:43 +000069
70 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,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020075 (const unsigned char *) pers,
76 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000077 {
78 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
79 goto exit;
80 }
Paul Bakker5121ce52009-01-03 21:22:43 +000081
82 printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
83 fflush( stdout );
84
Paul Bakkera802e1a2010-08-16 11:56:45 +000085 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Paul Bakker508ad5a2011-12-04 17:09:26 +000087 if( ( ret = rsa_gen_key( &rsa, ctr_drbg_random, &ctr_drbg, KEY_SIZE,
88 EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000089 {
90 printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
91 goto exit;
92 }
93
94 printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
95 fflush( stdout );
96
97 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
98 {
99 printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
100 ret = 1;
101 goto exit;
102 }
103
104 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
105 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
106 {
107 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
108 goto exit;
109 }
110
111 printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
112 fflush( stdout );
113
114 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
115 {
116 printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
117 ret = 1;
118 goto exit;
119 }
120
121 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
122 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
123 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
124 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
125 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
126 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
127 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
128 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
129 {
130 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
131 goto exit;
132 }
133/*
134 printf( " ok\n . Generating the certificate..." );
135
136 x509write_init_raw( &cert );
137 x509write_add_pubkey( &cert, &rsa );
138 x509write_add_subject( &cert, "CN='localhost'" );
139 x509write_add_validity( &cert, "2007-09-06 17:00:32",
140 "2010-09-06 17:00:32" );
141 x509write_create_selfsign( &cert, &rsa );
142 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
143 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
144 x509write_free_raw( &cert );
145*/
146 printf( " ok\n\n" );
147
148exit:
149
150 if( fpub != NULL )
151 fclose( fpub );
152
153 if( fpriv != NULL )
154 fclose( fpriv );
155
156 rsa_free( &rsa );
157
Paul Bakkercce9d772011-11-18 14:26:47 +0000158#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 printf( " Press Enter to exit this program.\n" );
160 fflush( stdout ); getchar();
161#endif
162
163 return( ret );
164}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000165#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_RSA_C &&
166 POLARSSL_GENPRIME && POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */