dh_genprime.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #ifdef PRINTF_STDLIB
  29. #include <stdio.h>
  30. #endif
  31. #ifdef PRINTF_CUSTOM
  32. #include "tinystdio.h"
  33. #endif
  34. #include "polarssl/config.h"
  35. #include "polarssl/bignum.h"
  36. #include "polarssl/havege.h"
  37. /*
  38. * Note: G = 4 is always a quadratic residue mod P,
  39. * so it is a generator of order Q (with P = 2*Q+1).
  40. */
  41. #define DH_P_SIZE 1024
  42. #define GENERATOR "4"
  43. #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
  44. !defined(POLARSSL_FS_IO)
  45. int main( void )
  46. {
  47. printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
  48. "POLARSSL_FS_IO not defined.\n");
  49. return( 0 );
  50. }
  51. #else
  52. int main( void )
  53. {
  54. int ret = 1;
  55. #if defined(POLARSSL_GENPRIME)
  56. mpi G, P, Q;
  57. havege_state hs;
  58. FILE *fout;
  59. mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
  60. mpi_read_string( &G, 10, GENERATOR );
  61. printf( "\n . Seeding the random number generator..." );
  62. fflush( stdout );
  63. havege_init( &hs );
  64. printf( " ok\n . Generating the modulus, please wait..." );
  65. fflush( stdout );
  66. /*
  67. * This can take a long time...
  68. */
  69. if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
  70. havege_rand, &hs ) ) != 0 )
  71. {
  72. printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
  73. goto exit;
  74. }
  75. printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
  76. fflush( stdout );
  77. if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
  78. {
  79. printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
  80. goto exit;
  81. }
  82. if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
  83. {
  84. printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
  85. goto exit;
  86. }
  87. if( ( ret = mpi_is_prime( &Q, havege_rand, &hs ) ) != 0 )
  88. {
  89. printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
  90. goto exit;
  91. }
  92. printf( " ok\n . Exporting the value in dh_prime.txt..." );
  93. fflush( stdout );
  94. if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
  95. {
  96. ret = 1;
  97. printf( " failed\n ! Could not create dh_prime.txt\n\n" );
  98. goto exit;
  99. }
  100. if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
  101. ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
  102. {
  103. printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
  104. goto exit;
  105. }
  106. printf( " ok\n\n" );
  107. fclose( fout );
  108. exit:
  109. mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
  110. #else
  111. printf( "\n ! Prime-number generation is not available.\n\n" );
  112. #endif
  113. #ifdef WIN32
  114. printf( " Press Enter to exit this program.\n" );
  115. fflush( stdout ); getchar();
  116. #endif
  117. return( ret );
  118. }
  119. #endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_FS_IO */