dh_genprime.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Diffie-Hellman-Merkle key exchange (prime generation)
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #if !defined(MBEDTLS_CONFIG_FILE)
  20. #include "mbedtls/config.h"
  21. #else
  22. #include MBEDTLS_CONFIG_FILE
  23. #endif
  24. #if defined(MBEDTLS_PLATFORM_C)
  25. #include "mbedtls/platform.h"
  26. #else
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #define mbedtls_printf printf
  30. #define mbedtls_time_t time_t
  31. #define mbedtls_exit exit
  32. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  33. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  34. #endif /* MBEDTLS_PLATFORM_C */
  35. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  36. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
  37. !defined(MBEDTLS_GENPRIME)
  38. int main( void )
  39. {
  40. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  41. "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
  42. "MBEDTLS_GENPRIME not defined.\n");
  43. mbedtls_exit( 0 );
  44. }
  45. #else
  46. #include "mbedtls/bignum.h"
  47. #include "mbedtls/entropy.h"
  48. #include "mbedtls/ctr_drbg.h"
  49. #include <stdio.h>
  50. #include <string.h>
  51. #define USAGE \
  52. "\n usage: dh_genprime param=<>...\n" \
  53. "\n acceprable parameters:\n" \
  54. " bits=%%d default: 2048\n"
  55. #define DFL_BITS 2048
  56. /*
  57. * Note: G = 4 is always a quadratic residue mod P,
  58. * so it is a generator of order Q (with P = 2*Q+1).
  59. */
  60. #define GENERATOR "4"
  61. int main( int argc, char **argv )
  62. {
  63. int ret = 1;
  64. int exit_code = MBEDTLS_EXIT_FAILURE;
  65. mbedtls_mpi G, P, Q;
  66. mbedtls_entropy_context entropy;
  67. mbedtls_ctr_drbg_context ctr_drbg;
  68. const char *pers = "dh_genprime";
  69. FILE *fout;
  70. int nbits = DFL_BITS;
  71. int i;
  72. char *p, *q;
  73. mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  74. mbedtls_ctr_drbg_init( &ctr_drbg );
  75. mbedtls_entropy_init( &entropy );
  76. if( argc == 0 )
  77. {
  78. usage:
  79. mbedtls_printf( USAGE );
  80. goto exit;
  81. }
  82. for( i = 1; i < argc; i++ )
  83. {
  84. p = argv[i];
  85. if( ( q = strchr( p, '=' ) ) == NULL )
  86. goto usage;
  87. *q++ = '\0';
  88. if( strcmp( p, "bits" ) == 0 )
  89. {
  90. nbits = atoi( q );
  91. if( nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS )
  92. goto usage;
  93. }
  94. else
  95. goto usage;
  96. }
  97. if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
  98. {
  99. mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret );
  100. goto exit;
  101. }
  102. mbedtls_printf( " ! Generating large primes may take minutes!\n" );
  103. mbedtls_printf( "\n . Seeding the random number generator..." );
  104. fflush( stdout );
  105. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  106. (const unsigned char *) pers,
  107. strlen( pers ) ) ) != 0 )
  108. {
  109. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  110. goto exit;
  111. }
  112. mbedtls_printf( " ok\n . Generating the modulus, please wait..." );
  113. fflush( stdout );
  114. /*
  115. * This can take a long time...
  116. */
  117. if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1,
  118. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  119. {
  120. mbedtls_printf( " failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret );
  121. goto exit;
  122. }
  123. mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
  124. fflush( stdout );
  125. if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 )
  126. {
  127. mbedtls_printf( " failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret );
  128. goto exit;
  129. }
  130. if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
  131. {
  132. mbedtls_printf( " failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret );
  133. goto exit;
  134. }
  135. if( ( ret = mbedtls_mpi_is_prime_ext( &Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  136. {
  137. mbedtls_printf( " failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret );
  138. goto exit;
  139. }
  140. mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." );
  141. fflush( stdout );
  142. if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
  143. {
  144. mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
  145. goto exit;
  146. }
  147. if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
  148. ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
  149. {
  150. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  151. fclose( fout );
  152. goto exit;
  153. }
  154. mbedtls_printf( " ok\n\n" );
  155. fclose( fout );
  156. exit_code = MBEDTLS_EXIT_SUCCESS;
  157. exit:
  158. mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  159. mbedtls_ctr_drbg_free( &ctr_drbg );
  160. mbedtls_entropy_free( &entropy );
  161. #if defined(_WIN32)
  162. mbedtls_printf( " Press Enter to exit this program.\n" );
  163. fflush( stdout ); getchar();
  164. #endif
  165. mbedtls_exit( exit_code );
  166. }
  167. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
  168. MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */