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