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