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

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

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