dh_genprime.c 5.5 KB

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