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