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