dh_genprime.c 6.7 KB

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