blob: c42662861f2617781e511ccf935c492a8d010667 [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 Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/havege.h"
33#include "polarssl/bignum.h"
34#include "polarssl/x509.h"
35#include "polarssl/rsa.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
37#define KEY_SIZE 1024
38#define EXPONENT 65537
39
40int main( void )
41{
42 int ret;
43 rsa_context rsa;
44 havege_state hs;
45 FILE *fpub = NULL;
46 FILE *fpriv = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +000047
48 printf( "\n . Seeding the random number generator..." );
49 fflush( stdout );
50
51 havege_init( &hs );
52
53 printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
54 fflush( stdout );
55
56 rsa_init( &rsa, RSA_PKCS_V15, 0, havege_rand, &hs );
57
58 if( ( ret = rsa_gen_key( &rsa, KEY_SIZE, EXPONENT ) ) != 0 )
59 {
60 printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
61 goto exit;
62 }
63
64 printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
65 fflush( stdout );
66
67 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
68 {
69 printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
70 ret = 1;
71 goto exit;
72 }
73
74 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
75 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
76 {
77 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
78 goto exit;
79 }
80
81 printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
82 fflush( stdout );
83
84 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
85 {
86 printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
87 ret = 1;
88 goto exit;
89 }
90
91 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
92 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
93 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
94 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
95 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
96 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
97 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
98 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
99 {
100 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
101 goto exit;
102 }
103/*
104 printf( " ok\n . Generating the certificate..." );
105
106 x509write_init_raw( &cert );
107 x509write_add_pubkey( &cert, &rsa );
108 x509write_add_subject( &cert, "CN='localhost'" );
109 x509write_add_validity( &cert, "2007-09-06 17:00:32",
110 "2010-09-06 17:00:32" );
111 x509write_create_selfsign( &cert, &rsa );
112 x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
113 x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
114 x509write_free_raw( &cert );
115*/
116 printf( " ok\n\n" );
117
118exit:
119
120 if( fpub != NULL )
121 fclose( fpub );
122
123 if( fpriv != NULL )
124 fclose( fpriv );
125
126 rsa_free( &rsa );
127
128#ifdef WIN32
129 printf( " Press Enter to exit this program.\n" );
130 fflush( stdout ); getchar();
131#endif
132
133 return( ret );
134}