blob: 0e90ffccaab192bbc0183a4f3efe5606cf984b10 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Example RSA key generation program
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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 Bakker40e46942009-01-03 21:51:57 +000034#include "polarssl/havege.h"
35#include "polarssl/bignum.h"
36#include "polarssl/x509.h"
37#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000038
39#define KEY_SIZE 1024
40#define EXPONENT 65537
41
Paul Bakker5690efc2011-05-26 13:16:06 +000042#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
43 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_GENPRIME) || \
44 !defined(POLARSSL_FS_IO)
45int main( void )
46{
47 printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
48 "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
49 "POLARSSL_FS_IO not defined.\n");
50 return( 0 );
51}
52#else
Paul Bakker5121ce52009-01-03 21:22:43 +000053int main( void )
54{
55 int ret;
56 rsa_context rsa;
57 havege_state hs;
58 FILE *fpub = NULL;
59 FILE *fpriv = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61 printf( "\n . Seeding the random number generator..." );
62 fflush( stdout );
63
64 havege_init( &hs );
65
66 printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
67 fflush( stdout );
68
Paul Bakkera802e1a2010-08-16 11:56:45 +000069 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakkera802e1a2010-08-16 11:56:45 +000071 if( ( ret = rsa_gen_key( &rsa, havege_rand, &hs, KEY_SIZE, EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000072 {
73 printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
74 goto exit;
75 }
76
77 printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
78 fflush( stdout );
79
80 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
81 {
82 printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
83 ret = 1;
84 goto exit;
85 }
86
87 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
88 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
89 {
90 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
91 goto exit;
92 }
93
94 printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
95 fflush( stdout );
96
97 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
98 {
99 printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
100 ret = 1;
101 goto exit;
102 }
103
104 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
105 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
106 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
107 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
108 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
109 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
110 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
111 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
112 {
113 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
114 goto exit;
115 }
116/*
117 printf( " ok\n . Generating the certificate..." );
118
119 x509write_init_raw( &cert );
120 x509write_add_pubkey( &cert, &rsa );
121 x509write_add_subject( &cert, "CN='localhost'" );
122 x509write_add_validity( &cert, "2007-09-06 17:00:32",
123 "2010-09-06 17:00:32" );
124 x509write_create_selfsign( &cert, &rsa );
125 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
126 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
127 x509write_free_raw( &cert );
128*/
129 printf( " ok\n\n" );
130
131exit:
132
133 if( fpub != NULL )
134 fclose( fpub );
135
136 if( fpriv != NULL )
137 fclose( fpriv );
138
139 rsa_free( &rsa );
140
141#ifdef WIN32
142 printf( " Press Enter to exit this program.\n" );
143 fflush( stdout ); getchar();
144#endif
145
146 return( ret );
147}
Paul Bakker5690efc2011-05-26 13:16:06 +0000148#endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_RSA_C &&
149 POLARSSL_GENPRIME && POLARSSL_FS_IO */