/home/dko/projects/mobilec/trunk/src/security/xyssl-0.9/programs/pkey/rsa_genkey.c

Go to the documentation of this file.
00001 /*
00002  * Example RSA key generation program
00003  *
00004  * Copyright (C) 2006-2007 Christophe Devine
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License along
00017  * with this program; if not, write to the Free Software Foundation, Inc.,
00018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00019  */
00020 
00021 #ifndef _CRT_SECURE_NO_DEPRECATE
00022 #define _CRT_SECURE_NO_DEPRECATE 1
00023 #endif
00024 
00025 #include <stdio.h>
00026 
00027 #include "xyssl/havege.h"
00028 #include "xyssl/bignum.h"
00029 #include "xyssl/x509.h"
00030 #include "xyssl/rsa.h"
00031 
00032 #define KEY_SIZE 1024
00033 #define EXPONENT 65537
00034 
00035 int main( void )
00036 {
00037 int ret;
00038 rsa_context rsa;
00039 havege_state hs;
00040 FILE *fpub = NULL;
00041 FILE *fpriv = NULL;
00042 x509_raw cert;
00043 
00044 printf( "\n . Seeding the random number generator..." );
00045 fflush( stdout );
00046 
00047 havege_init( &hs );
00048 
00049 printf( " ok\n . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
00050 fflush( stdout );
00051 
00052 rsa_init( &rsa, RSA_PKCS_V15, 0, havege_rand, &hs );
00053 
00054 if( ( ret = rsa_gen_key( &rsa, KEY_SIZE, EXPONENT ) ) != 0 )
00055 {
00056 printf( " failed\n ! rsa_gen_key returned %d\n\n", ret );
00057 goto exit;
00058 }
00059 
00060 printf( " ok\n . Exporting the public key in rsa_pub.txt...." );
00061 fflush( stdout );
00062 
00063 if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
00064 {
00065 printf( " failed\n ! could not open rsa_pub.txt for writing\n\n" );
00066 ret = 1;
00067 goto exit;
00068 }
00069 
00070 if( ( ret = mpi_write_file( "N = ", &rsa.N, 16, fpub ) ) != 0 ||
00071 ( ret = mpi_write_file( "E = ", &rsa.E, 16, fpub ) ) != 0 )
00072 {
00073 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
00074 goto exit;
00075 }
00076 
00077 printf( " ok\n . Exporting the private key in rsa_priv.txt..." );
00078 fflush( stdout );
00079 
00080 if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
00081 {
00082 printf( " failed\n ! could not open rsa_priv.txt for writing\n" );
00083 ret = 1;
00084 goto exit;
00085 }
00086 
00087 if( ( ret = mpi_write_file( "N = " , &rsa.N , 16, fpriv ) ) != 0 ||
00088 ( ret = mpi_write_file( "E = " , &rsa.E , 16, fpriv ) ) != 0 ||
00089 ( ret = mpi_write_file( "D = " , &rsa.D , 16, fpriv ) ) != 0 ||
00090 ( ret = mpi_write_file( "P = " , &rsa.P , 16, fpriv ) ) != 0 ||
00091 ( ret = mpi_write_file( "Q = " , &rsa.Q , 16, fpriv ) ) != 0 ||
00092 ( ret = mpi_write_file( "DP = ", &rsa.DP, 16, fpriv ) ) != 0 ||
00093 ( ret = mpi_write_file( "DQ = ", &rsa.DQ, 16, fpriv ) ) != 0 ||
00094 ( ret = mpi_write_file( "QP = ", &rsa.QP, 16, fpriv ) ) != 0 )
00095 {
00096 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
00097 goto exit;
00098 }
00099 /*
00100  printf( " ok\n . Generating the certificate..." );
00101 
00102  x509write_init_raw( &cert );
00103  x509write_add_pubkey( &cert, &rsa );
00104  x509write_add_subject( &cert, "CN='localhost'" );
00105  x509write_add_validity( &cert, "2007-09-06 17:00:32",
00106  "2010-09-06 17:00:32" );
00107  x509write_create_selfsign( &cert, &rsa );
00108  x509write_crtfile( &cert, "cert.der", X509_OUTPUT_DER );
00109  x509write_crtfile( &cert, "cert.pem", X509_OUTPUT_PEM );
00110  x509write_free_raw( &cert );
00111 */
00112 printf( " ok\n\n" );
00113 
00114 exit:
00115 
00116 if( fpub != NULL )
00117 fclose( fpub );
00118 
00119 if( fpriv != NULL )
00120 fclose( fpriv );
00121 
00122 rsa_free( &rsa );
00123 
00124 #ifdef WIN32
00125 printf( " Press Enter to exit this program.\n" );
00126 fflush( stdout ); getchar();
00127 #endif
00128 
00129 return( ret );
00130 }

Generated on Tue Oct 28 17:03:23 2008 for Mobile-C by doxygen 1.5.5

AltStyle によって変換されたページ (->オリジナル) /