blob: b111991816d430340b3f0e51af792b4768803350 [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <stdio.h>
31
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000033
34#include "polarssl/bignum.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000035#include "polarssl/entropy.h"
36#include "polarssl/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
38/*
39 * Note: G = 4 is always a quadratic residue mod P,
40 * so it is a generator of order Q (with P = 2*Q+1).
41 */
42#define DH_P_SIZE 1024
43#define GENERATOR "4"
44
Paul Bakker508ad5a2011-12-04 17:09:26 +000045#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
46 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000047int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000048{
Paul Bakkercce9d772011-11-18 14:26:47 +000049 ((void) argc);
50 ((void) argv);
51
Paul Bakker508ad5a2011-12-04 17:09:26 +000052 printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
53 "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000054 return( 0 );
55}
56#else
Paul Bakkercce9d772011-11-18 14:26:47 +000057int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000058{
59 int ret = 1;
60
Paul Bakker40e46942009-01-03 21:51:57 +000061#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000062 mpi G, P, Q;
Paul Bakker508ad5a2011-12-04 17:09:26 +000063 entropy_context entropy;
64 ctr_drbg_context ctr_drbg;
Paul Bakkere0225e42013-06-06 12:52:24 +020065 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000066 FILE *fout;
67
Paul Bakkercce9d772011-11-18 14:26:47 +000068 ((void) argc);
69 ((void) argv);
70
Paul Bakker6c591fa2011-05-05 11:49:20 +000071 mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
Paul Bakker993f02c2014-04-17 16:00:59 +020072
73 if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
74 {
75 printf( " failed\n ! mpi_read_string returned %d\n", ret );
76 goto exit;
77 }
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Paul Bakker5da01ca2012-10-03 19:48:33 +000079 printf( "\nWARNING: You should not generate and use your own DHM primes\n" );
80 printf( " unless you are very certain of what you are doing!\n" );
81 printf( " Failing to follow this instruction may result in\n" );
82 printf( " weak security for your connections! Use the\n" );
83 printf( " predefined DHM parameters from dhm.h instead!\n\n" );
84 printf( "============================================================\n\n" );
85
Paul Bakker5121ce52009-01-03 21:22:43 +000086 printf( "\n . Seeding the random number generator..." );
87 fflush( stdout );
88
Paul Bakker508ad5a2011-12-04 17:09:26 +000089 entropy_init( &entropy );
90 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +020091 (const unsigned char *) pers,
92 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000093 {
94 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
95 goto exit;
96 }
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98 printf( " ok\n . Generating the modulus, please wait..." );
99 fflush( stdout );
100
101 /*
102 * This can take a long time...
103 */
104 if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000105 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 {
107 printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
108 goto exit;
109 }
110
111 printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
112 fflush( stdout );
113
114 if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
115 {
116 printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
117 goto exit;
118 }
119
120 if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
121 {
122 printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
123 goto exit;
124 }
125
Paul Bakker508ad5a2011-12-04 17:09:26 +0000126 if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 {
128 printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
129 goto exit;
130 }
131
132 printf( " ok\n . Exporting the value in dh_prime.txt..." );
133 fflush( stdout );
134
135 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
136 {
137 ret = 1;
138 printf( " failed\n ! Could not create dh_prime.txt\n\n" );
139 goto exit;
140 }
141
142 if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
143 ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
144 {
145 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
146 goto exit;
147 }
148
149 printf( " ok\n\n" );
150 fclose( fout );
151
152exit:
153
Paul Bakker6c591fa2011-05-05 11:49:20 +0000154 mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000155#else
156 printf( "\n ! Prime-number generation is not available.\n\n" );
157#endif
158
Paul Bakkercce9d772011-11-18 14:26:47 +0000159#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 printf( " Press Enter to exit this program.\n" );
161 fflush( stdout ); getchar();
162#endif
163
164 return( ret );
165}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000166#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO &&
167 POLARSSL_CTR_DRBG_C */