blob: 066e136fb5083d574e59c55b1c7e3c730e59d6f5 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Example RSA key generation program
3 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00004 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
5 *
Paul Bakker27db1f52009-01-25 15:27:00 +00006 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
Paul Bakker5121ce52009-01-03 21:22:43 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <stdio.h>
28
Paul Bakker40e46942009-01-03 21:51:57 +000029#include "polarssl/havege.h"
30#include "polarssl/bignum.h"
31#include "polarssl/x509.h"
32#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
34#define KEY_SIZE 1024
35#define EXPONENT 65537
36
37int main( void )
38{
39 int ret;
40 rsa_context rsa;
41 havege_state hs;
42 FILE *fpub = NULL;
43 FILE *fpriv = NULL;
44 x509_raw cert;
45
46 printf( "\n . Seeding the random number generator..." );
47 fflush( stdout );
48
49 havege_init( &hs );
50
51 printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
52 fflush( stdout );
53
54 rsa_init( &rsa, RSA_PKCS_V15, 0, havege_rand, &hs );
55
56 if( ( ret = rsa_gen_key( &rsa, KEY_SIZE, EXPONENT ) ) != 0 )
57 {
58 printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
59 goto exit;
60 }
61
62 printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
63 fflush( stdout );
64
65 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
66 {
67 printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
68 ret = 1;
69 goto exit;
70 }
71
72 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
73 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
74 {
75 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
76 goto exit;
77 }
78
79 printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
80 fflush( stdout );
81
82 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
83 {
84 printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
85 ret = 1;
86 goto exit;
87 }
88
89 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
90 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
91 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
92 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
93 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
94 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
95 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
96 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
97 {
98 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
99 goto exit;
100 }
101/*
102 printf( " ok\n . Generating the certificate..." );
103
104 x509write_init_raw( &cert );
105 x509write_add_pubkey( &cert, &rsa );
106 x509write_add_subject( &cert, "CN='localhost'" );
107 x509write_add_validity( &cert, "2007-09-06 17:00:32",
108 "2010-09-06 17:00:32" );
109 x509write_create_selfsign( &cert, &rsa );
110 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
111 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
112 x509write_free_raw( &cert );
113*/
114 printf( " ok\n\n" );
115
116exit:
117
118 if( fpub != NULL )
119 fclose( fpub );
120
121 if( fpriv != NULL )
122 fclose( fpriv );
123
124 rsa_free( &rsa );
125
126#ifdef WIN32
127 printf( " Press Enter to exit this program.\n" );
128 fflush( stdout ); getchar();
129#endif
130
131 return( ret );
132}