00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00032
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
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 }