/home/dko/projects/mobilec/trunk/src/security/xyssl-0.7/programs/pkey/dh_genprime.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  *  Diffie-Hellman-Merkle key exchange (prime generation)
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 
00033 /*
00034  * Note: G = 4 is always a quadratic residue mod P,
00035  * so it is a generator of order Q (with P = 2*Q+1).
00036  */
00037 #define DH_P_SIZE 1024
00038 #define GENERATOR "4"
00039 
00040 int main( void )
00041 {
00042     int ret;
00043     mpi G, P, Q;
00044     havege_state hs;
00045     FILE *fout;
00046 
00047     mpi_init( &G, &P, &Q, NULL );
00048     mpi_read_string( &G, 10, GENERATOR );
00049 
00050     printf( "\n  . Seeding the random number generator..." );
00051     fflush( stdout );
00052 
00053     havege_init( &hs );
00054 
00055     printf( " ok\n  . Generating the modulus, please wait..." );
00056     fflush( stdout );
00057 
00058     /*
00059      * This can take a long time...
00060      */
00061     if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
00062                                havege_rand, &hs ) ) != 0 )
00063     {
00064         printf( " failed\n  ! mpi_gen_prime returned %08x\n\n", ret );
00065         goto exit;
00066     }
00067 
00068     printf( " ok\n  . Verifying that Q = (P-1)/2 is prime..." );
00069     fflush( stdout );
00070 
00071     if( ( ret = mpi_sub_int( &Q,       &P, 1 ) ) != 0 ||
00072         ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ||
00073         ( ret = mpi_is_prime( &Q ) ) != 0 )
00074     {
00075         printf( " failed\n  ! mpi_xx returned %08x\n\n", ret );
00076         goto exit;
00077     }
00078 
00079     printf( " ok\n  . Exporting the value in dh_prime.txt..." );
00080     fflush( stdout );
00081 
00082     if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
00083     {
00084         ret = 1;
00085         printf( " failed\n  ! Could not create dh_prime.txt\n\n" );
00086         goto exit;
00087     }
00088 
00089     if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
00090         ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
00091     {
00092         printf( " failed\n  ! mpi_write_file returned %08x\n\n", ret );
00093         goto exit;
00094     }
00095 
00096     printf( " ok\n\n" );
00097     fclose( fout );
00098 
00099 exit:
00100 
00101     mpi_free( &Q, &P, &G, NULL );
00102 
00103 #ifdef WIN32
00104     printf( "  Press Enter to exit this program.\n" );
00105     fflush( stdout ); getchar();
00106 #endif
00107 
00108     return( ret );
00109 }

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