blob: d7158b4c060e42f35b6cf2514f19d2a9bd6952dd [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 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)
Paul Bakkercce9d772011-11-18 14:26:47 +000045int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000046{
Paul Bakkercce9d772011-11-18 14:26:47 +000047 ((void) argc);
48 ((void) argv);
49
Paul Bakker5690efc2011-05-26 13:16:06 +000050 printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
51 "POLARSSL_RSA_C and/or POLARSSL_GENPRIME and/or "
52 "POLARSSL_FS_IO not defined.\n");
53 return( 0 );
54}
55#else
Paul Bakkercce9d772011-11-18 14:26:47 +000056int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000057{
58 int ret;
59 rsa_context rsa;
60 havege_state hs;
61 FILE *fpub = NULL;
62 FILE *fpriv = NULL;
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
70 havege_init( &hs );
71
72 printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
73 fflush( stdout );
74
Paul Bakkera802e1a2010-08-16 11:56:45 +000075 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Paul Bakkera802e1a2010-08-16 11:56:45 +000077 if( ( ret = rsa_gen_key( &rsa, havege_rand, &hs, KEY_SIZE, EXPONENT ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000078 {
79 printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
80 goto exit;
81 }
82
83 printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
84 fflush( stdout );
85
86 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
87 {
88 printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
89 ret = 1;
90 goto exit;
91 }
92
93 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
94 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
95 {
96 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
97 goto exit;
98 }
99
100 printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
101 fflush( stdout );
102
103 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
104 {
105 printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
106 ret = 1;
107 goto exit;
108 }
109
110 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
111 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
112 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
113 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
114 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
115 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
116 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
117 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
118 {
119 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
120 goto exit;
121 }
122/*
123 printf( " ok\n . Generating the certificate..." );
124
125 x509write_init_raw( &cert );
126 x509write_add_pubkey( &cert, &rsa );
127 x509write_add_subject( &cert, "CN='localhost'" );
128 x509write_add_validity( &cert, "2007-09-06 17:00:32",
129 "2010-09-06 17:00:32" );
130 x509write_create_selfsign( &cert, &rsa );
131 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
132 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
133 x509write_free_raw( &cert );
134*/
135 printf( " ok\n\n" );
136
137exit:
138
139 if( fpub != NULL )
140 fclose( fpub );
141
142 if( fpriv != NULL )
143 fclose( fpriv );
144
145 rsa_free( &rsa );
146
Paul Bakkercce9d772011-11-18 14:26:47 +0000147#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 printf( " Press Enter to exit this program.\n" );
149 fflush( stdout ); getchar();
150#endif
151
152 return( ret );
153}
Paul Bakker5690efc2011-05-26 13:16:06 +0000154#endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_RSA_C &&
155 POLARSSL_GENPRIME && POLARSSL_FS_IO */