/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/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 library is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU Lesser General Public
00008  *  License, version 2.1 as published by the Free Software Foundation.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Lesser General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Lesser General Public
00016  *  License along with this library; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00018  *  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/rsa.h"
00030 
00031 #define KEY_SIZE 1024
00032 #define EXPONENT 65537
00033 
00034 int main( void )
00035 {
00036     int ret;
00037     rsa_context rsa;
00038     havege_state hs;
00039     FILE *fpub  = NULL;
00040     FILE *fpriv = NULL;
00041 
00042     printf( "\n  . Seeding the random number generator..." );
00043     fflush( stdout );
00044 
00045     havege_init( &hs );
00046 
00047     printf( " ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
00048     fflush( stdout );
00049 
00050     if( ( ret = rsa_gen_key( &rsa, KEY_SIZE, EXPONENT,
00051                              havege_rand, &hs ) ) != 0 )
00052     {
00053         printf( " failed\n  ! rsa_gen_key returned %08x\n\n", ret );
00054         goto exit;
00055     }
00056 
00057     printf( " ok\n  . Exporting the public  key in rsa_pub.txt...." );
00058     fflush( stdout );
00059 
00060     if( ( fpub = fopen( "rsa_pub.txt", "wb+" ) ) == NULL )
00061     {
00062         printf( " failed\n  ! could not open rsa_pub.txt for writing\n\n" );
00063         ret = 1;
00064         goto exit;
00065     }
00066 
00067     if( ( ret = rsa_write_public( &rsa, fpub ) ) != 0 )
00068     {
00069         printf( " failed\n  ! rsa_write_public returned %08x\n\n", ret );
00070         goto exit;
00071     }
00072 
00073     printf( " ok\n  . Exporting the private key in rsa_priv.txt..." );
00074     fflush( stdout );
00075 
00076     if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
00077     {
00078         printf( " failed\n  ! could not open rsa_priv.txt for writing\n" );
00079         ret = 1;
00080         goto exit;
00081     }
00082 
00083     if( ( ret = rsa_write_private( &rsa, fpriv ) ) != 0 )
00084     {
00085         printf( " failed\n  ! rsa_write_private returned %08x\n\n", ret );
00086         goto exit;
00087     }
00088 
00089     printf( " ok\n\n" );
00090 
00091 exit:
00092 
00093     if( fpub  != NULL )
00094         fclose( fpub );
00095 
00096     if( fpriv != NULL )
00097         fclose( fpriv );
00098 
00099     rsa_free( &rsa );
00100 
00101 #ifdef WIN32
00102     printf( "  Press Enter to exit this program.\n" );
00103     fflush( stdout ); getchar();
00104 #endif
00105 
00106     return( ret );
00107 }

Generated on Fri May 16 14:49:55 2008 for Mobile-C by  doxygen 1.5.4