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

Go to the documentation of this file.
00001 /* SVN FILE INFO
00002  * $Revision: 174 $ : Last Committed Revision
00003  * $Date: 2008-06-24 10:50:29 -0700 (Tue, 24 Jun 2008) $ : Last Committed Date */
00004 /*
00005  *  Example RSA key generation program
00006  *
00007  *  Copyright (C) 2006-2007  Christophe Devine
00008  *
00009  *  This library is free software; you can redistribute it and/or
00010  *  modify it under the terms of the GNU Lesser General Public
00011  *  License, version 2.1 as published by the Free Software Foundation.
00012  *
00013  *  This library is distributed in the hope that it will be useful,
00014  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  *  Lesser General Public License for more details.
00017  *
00018  *  You should have received a copy of the GNU Lesser General Public
00019  *  License along with this library; if not, write to the Free Software
00020  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00021  *  MA  02110-1301  USA
00022  */
00023 
00024 #ifndef _CRT_SECURE_NO_DEPRECATE
00025 #define _CRT_SECURE_NO_DEPRECATE 1
00026 #endif
00027 
00028 #include <stdio.h>
00029 
00030 #include "xyssl/havege.h"
00031 #include "xyssl/bignum.h"
00032 #include "xyssl/rsa.h"
00033 
00034 #define KEY_SIZE 1024
00035 #define EXPONENT 65537
00036 
00037 int main( void )
00038 {
00039     int ret;
00040     rsa_context rsa;
00041     havege_state hs;
00042     FILE *fpub  = NULL;
00043     FILE *fpriv = NULL;
00044 
00045     printf( "\n  . Seeding the random number generator..." );
00046     fflush( stdout );
00047 
00048     havege_init( &hs );
00049 
00050     printf( " ok\n  . Generating the RSA key [ %d-bit ]...", KEY_SIZE );
00051     fflush( stdout );
00052 
00053     if( ( ret = rsa_gen_key( &rsa, KEY_SIZE, EXPONENT,
00054                              havege_rand, &hs ) ) != 0 )
00055     {
00056         printf( " failed\n  ! rsa_gen_key returned %08x\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 = rsa_write_public( &rsa, fpub ) ) != 0 )
00071     {
00072         printf( " failed\n  ! rsa_write_public returned %08x\n\n", ret );
00073         goto exit;
00074     }
00075 
00076     printf( " ok\n  . Exporting the private key in rsa_priv.txt..." );
00077     fflush( stdout );
00078 
00079     if( ( fpriv = fopen( "rsa_priv.txt", "wb+" ) ) == NULL )
00080     {
00081         printf( " failed\n  ! could not open rsa_priv.txt for writing\n" );
00082         ret = 1;
00083         goto exit;
00084     }
00085 
00086     if( ( ret = rsa_write_private( &rsa, fpriv ) ) != 0 )
00087     {
00088         printf( " failed\n  ! rsa_write_private returned %08x\n\n", ret );
00089         goto exit;
00090     }
00091 
00092     printf( " ok\n\n" );
00093 
00094 exit:
00095 
00096     if( fpub  != NULL )
00097         fclose( fpub );
00098 
00099     if( fpriv != NULL )
00100         fclose( fpriv );
00101 
00102     rsa_free( &rsa );
00103 
00104 #ifdef WIN32
00105     printf( "  Press Enter to exit this program.\n" );
00106     fflush( stdout ); getchar();
00107 #endif
00108 
00109     return( ret );
00110 }

Generated on Tue Jul 1 15:29:59 2008 for Mobile-C by  doxygen 1.5.4