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