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