dh_genprime.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Diffie-Hellman-Merkle key exchange (prime generation)
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #include <stdio.h>
  29. #include "polarssl/config.h"
  30. #include "polarssl/bignum.h"
  31. #include "polarssl/havege.h"
  32. /*
  33. * Note: G = 4 is always a quadratic residue mod P,
  34. * so it is a generator of order Q (with P = 2*Q+1).
  35. */
  36. #define DH_P_SIZE 1024
  37. #define GENERATOR "4"
  38. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  39. !defined(POLARSSL_FS_IO)
  40. int main( void )
  41. {
  42. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  43. "POLARSSL_FS_IO not defined.\n");
  44. return( 0 );
  45. }
  46. #else
  47. int main( void )
  48. {
  49. int ret = 1;
  50. #if defined(POLARSSL_GENPRIME)
  51. mpi G, P, Q;
  52. havege_state hs;
  53. FILE *fout;
  54. mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
  55. mpi_read_string( &G, 10, GENERATOR );
  56. printf( "\n . Seeding the random number generator..." );
  57. fflush( stdout );
  58. havege_init( &hs );
  59. printf( " ok\n . Generating the modulus, please wait..." );
  60. fflush( stdout );
  61. /*
  62. * This can take a long time...
  63. */
  64. if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
  65. havege_rand, &hs ) ) != 0 )
  66. {
  67. printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
  68. goto exit;
  69. }
  70. printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
  71. fflush( stdout );
  72. if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
  73. {
  74. printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
  75. goto exit;
  76. }
  77. if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
  78. {
  79. printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
  80. goto exit;
  81. }
  82. if( ( ret = mpi_is_prime( &Q, havege_rand, &hs ) ) != 0 )
  83. {
  84. printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
  85. goto exit;
  86. }
  87. printf( " ok\n . Exporting the value in dh_prime.txt..." );
  88. fflush( stdout );
  89. if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
  90. {
  91. ret = 1;
  92. printf( " failed\n ! Could not create dh_prime.txt\n\n" );
  93. goto exit;
  94. }
  95. if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
  96. ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
  97. {
  98. printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
  99. goto exit;
  100. }
  101. printf( " ok\n\n" );
  102. fclose( fout );
  103. exit:
  104. mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
  105. #else
  106. printf( "\n ! Prime-number generation is not available.\n\n" );
  107. #endif
  108. #ifdef WIN32
  109. printf( " Press Enter to exit this program.\n" );
  110. fflush( stdout ); getchar();
  111. #endif
  112. return( ret );
  113. }
  114. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_FS_IO */