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