1 /*
2  *  Diffie-Hellman-Merkle key exchange (prime generation)
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: GPL-2.0
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License along
18  *  with this program; if not, write to the Free Software Foundation, Inc.,
19  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 
24 #if !defined(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
29 
30 #if defined(MBEDTLS_PLATFORM_C)
31 #include "mbedtls/platform.h"
32 #else
33 #include <stdio.h>
34 #include <stdlib.h>
35 #define mbedtls_printf          printf
36 #define mbedtls_time_t          time_t
37 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
38 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
39 #endif /* MBEDTLS_PLATFORM_C */
40 
41 #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) ||   \
42     !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) ||     \
43     !defined(MBEDTLS_GENPRIME)
main(void)44 int main( void )
45 {
46     mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
47            "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
48            "MBEDTLS_GENPRIME not defined.\n");
49     return( 0 );
50 }
51 #else
52 
53 #include "mbedtls/bignum.h"
54 #include "mbedtls/entropy.h"
55 #include "mbedtls/ctr_drbg.h"
56 
57 #include <stdio.h>
58 #include <string.h>
59 
60 #define USAGE \
61     "\n usage: dh_genprime param=<>...\n"                                   \
62     "\n acceprable parameters:\n"                                           \
63     "    bits=%%d           default: 2048\n"
64 
65 #define DFL_BITS    2048
66 
67 /*
68  * Note: G = 4 is always a quadratic residue mod P,
69  * so it is a generator of order Q (with P = 2*Q+1).
70  */
71 #define GENERATOR "4"
72 
main(int argc,char ** argv)73 int main( int argc, char **argv )
74 {
75     int ret = 1;
76     int exit_code = MBEDTLS_EXIT_FAILURE;
77     mbedtls_mpi G, P, Q;
78     mbedtls_entropy_context entropy;
79     mbedtls_ctr_drbg_context ctr_drbg;
80     const char *pers = "dh_genprime";
81     FILE *fout;
82     int nbits = DFL_BITS;
83     int i;
84     char *p, *q;
85 
86     mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
87     mbedtls_ctr_drbg_init( &ctr_drbg );
88     mbedtls_entropy_init( &entropy );
89 
90     if( argc == 0 )
91     {
92     usage:
93         mbedtls_printf( USAGE );
94         return( exit_code );
95     }
96 
97     for( i = 1; i < argc; i++ )
98     {
99         p = argv[i];
100         if( ( q = strchr( p, '=' ) ) == NULL )
101             goto usage;
102         *q++ = '\0';
103 
104         if( strcmp( p, "bits" ) == 0 )
105         {
106             nbits = atoi( q );
107             if( nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS )
108                 goto usage;
109         }
110         else
111             goto usage;
112     }
113 
114     if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
115     {
116         mbedtls_printf( " failed\n  ! mbedtls_mpi_read_string returned %d\n", ret );
117         goto exit;
118     }
119 
120     mbedtls_printf( "  ! Generating large primes may take minutes!\n" );
121 
122     mbedtls_printf( "\n  . Seeding the random number generator..." );
123     fflush( stdout );
124 
125     if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
126                                (const unsigned char *) pers,
127                                strlen( pers ) ) ) != 0 )
128     {
129         mbedtls_printf( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
130         goto exit;
131     }
132 
133     mbedtls_printf( " ok\n  . Generating the modulus, please wait..." );
134     fflush( stdout );
135 
136     /*
137      * This can take a long time...
138      */
139     if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1,
140                                mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
141     {
142         mbedtls_printf( " failed\n  ! mbedtls_mpi_gen_prime returned %d\n\n", ret );
143         goto exit;
144     }
145 
146     mbedtls_printf( " ok\n  . Verifying that Q = (P-1)/2 is prime..." );
147     fflush( stdout );
148 
149     if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 )
150     {
151         mbedtls_printf( " failed\n  ! mbedtls_mpi_sub_int returned %d\n\n", ret );
152         goto exit;
153     }
154 
155     if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
156     {
157         mbedtls_printf( " failed\n  ! mbedtls_mpi_div_int returned %d\n\n", ret );
158         goto exit;
159     }
160 
161     if( ( ret = mbedtls_mpi_is_prime( &Q, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
162     {
163         mbedtls_printf( " failed\n  ! mbedtls_mpi_is_prime returned %d\n\n", ret );
164         goto exit;
165     }
166 
167     mbedtls_printf( " ok\n  . Exporting the value in dh_prime.txt..." );
168     fflush( stdout );
169 
170     if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
171     {
172         mbedtls_printf( " failed\n  ! Could not create dh_prime.txt\n\n" );
173         goto exit;
174     }
175 
176     if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
177         ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
178     {
179         mbedtls_printf( " failed\n  ! mbedtls_mpi_write_file returned %d\n\n", ret );
180         fclose( fout );
181         goto exit;
182     }
183 
184     mbedtls_printf( " ok\n\n" );
185     fclose( fout );
186 
187     exit_code = MBEDTLS_EXIT_SUCCESS;
188 
189 exit:
190 
191     mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
192     mbedtls_ctr_drbg_free( &ctr_drbg );
193     mbedtls_entropy_free( &entropy );
194 
195 #if defined(_WIN32)
196     mbedtls_printf( "  Press Enter to exit this program.\n" );
197     fflush( stdout ); getchar();
198 #endif
199 
200     return( exit_code );
201 }
202 #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
203           MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */
204