blob: 1f9d73044342f36112f0005c0229f245cb228a31 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (prime generation)
3 *
Paul Bakker5da01ca2012-10-03 19:48:33 +00004 * Copyright (C) 2006-2012, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020026#include "polarssl/config.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000027
28#include <stdio.h>
29
Paul Bakker5690efc2011-05-26 13:16:06 +000030#include "polarssl/bignum.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000031#include "polarssl/entropy.h"
32#include "polarssl/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000033
34/*
35 * Note: G = 4 is always a quadratic residue mod P,
36 * so it is a generator of order Q (with P = 2*Q+1).
37 */
38#define DH_P_SIZE 1024
39#define GENERATOR "4"
40
Paul Bakker508ad5a2011-12-04 17:09:26 +000041#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020042 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C) || \
43 !defined(POLARSSL_GENPRIME)
Paul Bakkercce9d772011-11-18 14:26:47 +000044int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000045{
Paul Bakkercce9d772011-11-18 14:26:47 +000046 ((void) argc);
47 ((void) argv);
48
Paul Bakker508ad5a2011-12-04 17:09:26 +000049 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020050 "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C and/or "
51 "POLARSSL_GENPRIME not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000052 return( 0 );
53}
54#else
Paul Bakkercce9d772011-11-18 14:26:47 +000055int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000056{
57 int ret = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +000058 mpi G, P, Q;
Paul Bakker508ad5a2011-12-04 17:09:26 +000059 entropy_context entropy;
60 ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020061 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000062 FILE *fout;
63
Paul Bakkercce9d772011-11-18 14:26:47 +000064 ((void) argc);
65 ((void) argv);
66
Paul Bakker6c591fa2011-05-05 11:49:20 +000067 mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020068
69 if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
70 {
71 printf( " failed\n ! mpi_read_string returned %d\n", ret );
72 goto exit;
73 }
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Paul Bakker5da01ca2012-10-03 19:48:33 +000075 printf( "\nWARNING: You should not generate and use your own DHM primes\n" );
76 printf( " unless you are very certain of what you are doing!\n" );
77 printf( " Failing to follow this instruction may result in\n" );
78 printf( " weak security for your connections! Use the\n" );
79 printf( " predefined DHM parameters from dhm.h instead!\n\n" );
80 printf( "============================================================\n\n" );
81
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +010082 printf( " ! Generating large primes may take minutes!\n" );
83
Paul Bakker5121ce52009-01-03 21:22:43 +000084 printf( "\n . Seeding the random number generator..." );
85 fflush( stdout );
86
Paul Bakker508ad5a2011-12-04 17:09:26 +000087 entropy_init( &entropy );
88 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020089 (const unsigned char *) pers,
90 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000091 {
92 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
93 goto exit;
94 }
Paul Bakker5121ce52009-01-03 21:22:43 +000095
96 printf( " ok\n . Generating the modulus, please wait..." );
97 fflush( stdout );
98
99 /*
100 * This can take a long time...
101 */
102 if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000103 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000104 {
105 printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
106 goto exit;
107 }
108
109 printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
110 fflush( stdout );
111
112 if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
113 {
114 printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
115 goto exit;
116 }
117
118 if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
119 {
120 printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
121 goto exit;
122 }
123
Paul Bakker508ad5a2011-12-04 17:09:26 +0000124 if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 {
126 printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
127 goto exit;
128 }
129
130 printf( " ok\n . Exporting the value in dh_prime.txt..." );
131 fflush( stdout );
132
133 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
134 {
135 ret = 1;
136 printf( " failed\n ! Could not create dh_prime.txt\n\n" );
137 goto exit;
138 }
139
140 if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
141 ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
142 {
143 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
144 goto exit;
145 }
146
147 printf( " ok\n\n" );
148 fclose( fout );
149
150exit:
151
Paul Bakker6c591fa2011-05-05 11:49:20 +0000152 mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200153 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Paul Bakkercce9d772011-11-18 14:26:47 +0000155#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 printf( " Press Enter to exit this program.\n" );
157 fflush( stdout ); getchar();
158#endif
159
160 return( ret );
161}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000162#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200163 POLARSSL_CTR_DRBG_C && POLARSSL_GENPRIME */