blob: c918c656dad6e2447f1333f5a504d50d4ffeeb86 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (prime generation)
3 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <stdio.h>
28
Paul Bakker40e46942009-01-03 21:51:57 +000029#include "polarssl/config.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000030
31#include "polarssl/bignum.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000032#include "polarssl/entropy.h"
33#include "polarssl/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000034
35/*
36 * Note: G = 4 is always a quadratic residue mod P,
37 * so it is a generator of order Q (with P = 2*Q+1).
38 */
39#define DH_P_SIZE 1024
40#define GENERATOR "4"
41
Paul Bakker508ad5a2011-12-04 17:09:26 +000042#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
43 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
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 "
50 "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000051 return( 0 );
52}
53#else
Paul Bakkercce9d772011-11-18 14:26:47 +000054int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000055{
56 int ret = 1;
57
Paul Bakker40e46942009-01-03 21:51:57 +000058#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000059 mpi G, P, Q;
Paul Bakker508ad5a2011-12-04 17:09:26 +000060 entropy_context entropy;
61 ctr_drbg_context ctr_drbg;
Paul Bakkere0225e42013-06-06 12:52:24 +020062 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000063 FILE *fout;
64
Paul Bakkercce9d772011-11-18 14:26:47 +000065 ((void) argc);
66 ((void) argv);
67
Paul Bakker6c591fa2011-05-05 11:49:20 +000068 mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
Paul Bakker39148402014-04-17 16:02:36 +020069 entropy_init( &entropy );
Paul Bakker993f02c2014-04-17 16:00:59 +020070
71 if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
72 {
73 printf( " failed\n ! mpi_read_string returned %d\n", ret );
74 goto exit;
75 }
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Paul Bakker5da01ca2012-10-03 19:48:33 +000077 printf( "\nWARNING: You should not generate and use your own DHM primes\n" );
78 printf( " unless you are very certain of what you are doing!\n" );
79 printf( " Failing to follow this instruction may result in\n" );
80 printf( " weak security for your connections! Use the\n" );
81 printf( " predefined DHM parameters from dhm.h instead!\n\n" );
82 printf( "============================================================\n\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 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +020088 (const unsigned char *) pers,
89 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000090 {
91 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
92 goto exit;
93 }
Paul Bakker5121ce52009-01-03 21:22:43 +000094
95 printf( " ok\n . Generating the modulus, please wait..." );
96 fflush( stdout );
97
98 /*
99 * This can take a long time...
100 */
101 if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000102 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 {
104 printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
105 goto exit;
106 }
107
108 printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
109 fflush( stdout );
110
111 if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
112 {
113 printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
114 goto exit;
115 }
116
117 if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
118 {
119 printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
120 goto exit;
121 }
122
Paul Bakker508ad5a2011-12-04 17:09:26 +0000123 if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 {
125 printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
126 goto exit;
127 }
128
129 printf( " ok\n . Exporting the value in dh_prime.txt..." );
130 fflush( stdout );
131
132 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
133 {
134 ret = 1;
135 printf( " failed\n ! Could not create dh_prime.txt\n\n" );
136 goto exit;
137 }
138
139 if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
140 ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
141 {
142 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
143 goto exit;
144 }
145
146 printf( " ok\n\n" );
147 fclose( fout );
148
149exit:
150
Paul Bakker6c591fa2011-05-05 11:49:20 +0000151 mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152#else
153 printf( "\n ! Prime-number generation is not available.\n\n" );
154#endif
155
Paul Bakkercce9d772011-11-18 14:26:47 +0000156#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 printf( " Press Enter to exit this program.\n" );
158 fflush( stdout ); getchar();
159#endif
160
161 return( ret );
162}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000163#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO &&
164 POLARSSL_CTR_DRBG_C */