dh_genprime.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Diffie-Hellman-Merkle key exchange (prime generation)
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #define mbedtls_printf printf
  32. #define mbedtls_time_t time_t
  33. #define mbedtls_exit exit
  34. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  35. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  36. #endif /* MBEDTLS_PLATFORM_C */
  37. #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
  38. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
  39. !defined(MBEDTLS_GENPRIME)
  40. int main( void )
  41. {
  42. mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
  43. "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
  44. "MBEDTLS_GENPRIME not defined.\n");
  45. return( 0 );
  46. }
  47. #else
  48. #include "mbedtls/bignum.h"
  49. #include "mbedtls/entropy.h"
  50. #include "mbedtls/ctr_drbg.h"
  51. #include <stdio.h>
  52. #include <string.h>
  53. #define USAGE \
  54. "\n usage: dh_genprime param=<>...\n" \
  55. "\n acceprable parameters:\n" \
  56. " bits=%%d default: 2048\n"
  57. #define DFL_BITS 2048
  58. /*
  59. * Note: G = 4 is always a quadratic residue mod P,
  60. * so it is a generator of order Q (with P = 2*Q+1).
  61. */
  62. #define GENERATOR "4"
  63. int main( int argc, char **argv )
  64. {
  65. int ret = 1;
  66. int exit_code = MBEDTLS_EXIT_FAILURE;
  67. mbedtls_mpi G, P, Q;
  68. mbedtls_entropy_context entropy;
  69. mbedtls_ctr_drbg_context ctr_drbg;
  70. const char *pers = "dh_genprime";
  71. FILE *fout;
  72. int nbits = DFL_BITS;
  73. int i;
  74. char *p, *q;
  75. mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
  76. mbedtls_ctr_drbg_init( &ctr_drbg );
  77. mbedtls_entropy_init( &entropy );
  78. if( argc == 0 )
  79. {
  80. usage:
  81. mbedtls_printf( USAGE );
  82. return( exit_code );
  83. }
  84. for( i = 1; i < argc; i++ )
  85. {
  86. p = argv[i];
  87. if( ( q = strchr( p, '=' ) ) == NULL )
  88. goto usage;
  89. *q++ = '\0';
  90. if( strcmp( p, "bits" ) == 0 )
  91. {
  92. nbits = atoi( q );
  93. if( nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS )
  94. goto usage;
  95. }
  96. else
  97. goto usage;
  98. }
  99. if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
  100. {
  101. mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret );
  102. goto exit;
  103. }
  104. mbedtls_printf( " ! Generating large primes may take minutes!\n" );
  105. mbedtls_printf( "\n . Seeding the random number generator..." );
  106. fflush( stdout );
  107. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
  108. (const unsigned char *) pers,
  109. strlen( pers ) ) ) != 0 )
  110. {
  111. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
  112. goto exit;
  113. }
  114. mbedtls_printf( " ok\n . Generating the modulus, please wait..." );
  115. fflush( stdout );
  116. /*
  117. * This can take a long time...
  118. */
  119. if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1,
  120. mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  121. {
  122. mbedtls_printf( " failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret );
  123. goto exit;
  124. }
  125. mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
  126. fflush( stdout );
  127. if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 )
  128. {
  129. mbedtls_printf( " failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret );
  130. goto exit;
  131. }
  132. if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
  133. {
  134. mbedtls_printf( " failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret );
  135. goto exit;
  136. }
  137. if( ( ret = mbedtls_mpi_is_prime_ext( &Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
  138. {
  139. mbedtls_printf( " failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret );
  140. goto exit;
  141. }
  142. mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." );
  143. fflush( stdout );
  144. if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
  145. {
  146. mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
  147. goto exit;
  148. }
  149. if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
  150. ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
  151. {
  152. mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
  153. fclose( fout );
  154. goto exit;
  155. }
  156. mbedtls_printf( " ok\n\n" );
  157. fclose( fout );
  158. exit_code = MBEDTLS_EXIT_SUCCESS;
  159. exit:
  160. mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
  161. mbedtls_ctr_drbg_free( &ctr_drbg );
  162. mbedtls_entropy_free( &entropy );
  163. #if defined(_WIN32)
  164. mbedtls_printf( " Press Enter to exit this program.\n" );
  165. fflush( stdout ); getchar();
  166. #endif
  167. return( exit_code );
  168. }
  169. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
  170. MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */