blob: 0340f147d98fd6f911914c4af073b8d319ddfb79 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (prime generation)
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Paul Bakker508ad5a2011-12-04 17:09:26 +000036#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020037 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C) || \
38 !defined(POLARSSL_GENPRIME)
Rich Evans85b05ec2015-02-12 11:37:29 +000039int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000040{
Rich Evansf90016a2015-01-19 14:26:37 +000041 polarssl_printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or "
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +020042 "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C and/or "
43 "POLARSSL_GENPRIME not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000044 return( 0 );
45}
46#else
Manuel Pégourié-Gonnardf0dd0452015-07-03 17:14:06 +020047
48#include "polarssl/bignum.h"
49#include "polarssl/entropy.h"
50#include "polarssl/ctr_drbg.h"
51
52#include <stdio.h>
53#include <string.h>
54
55#define USAGE \
56 "\n usage: dh_genprime param=<>...\n" \
57 "\n acceprable parameters:\n" \
58 " bits=%%d default: 2048\n"
59
60#define DFL_BITS 2048
61
62/*
63 * Note: G = 4 is always a quadratic residue mod P,
64 * so it is a generator of order Q (with P = 2*Q+1).
65 */
66#define GENERATOR "4"
67
68int main( int argc, char **argv )
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
70 int ret = 1;
Paul Bakker5121ce52009-01-03 21:22:43 +000071 mpi G, P, Q;
Paul Bakker508ad5a2011-12-04 17:09:26 +000072 entropy_context entropy;
73 ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020074 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000075 FILE *fout;
Manuel Pégourié-Gonnardf0dd0452015-07-03 17:14:06 +020076 int nbits = DFL_BITS;
77 int i;
78 char *p, *q;
Paul Bakker5121ce52009-01-03 21:22:43 +000079
Paul Bakker6c591fa2011-05-05 11:49:20 +000080 mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
Paul Bakker0c226102014-04-17 16:02:36 +020081 entropy_init( &entropy );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020082
Manuel Pégourié-Gonnardf0dd0452015-07-03 17:14:06 +020083 if( argc == 0 )
84 {
85 usage:
86 polarssl_printf( USAGE );
87 return( 1 );
88 }
89
90 for( i = 1; i < argc; i++ )
91 {
92 p = argv[i];
93 if( ( q = strchr( p, '=' ) ) == NULL )
94 goto usage;
95 *q++ = '\0';
96
97 if( strcmp( p, "bits" ) == 0 )
98 {
99 nbits = atoi( q );
100 if( nbits < 0 || nbits > POLARSSL_MPI_MAX_BITS )
101 goto usage;
102 }
103 else
104 goto usage;
105 }
106
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200107 if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
108 {
Rich Evansf90016a2015-01-19 14:26:37 +0000109 polarssl_printf( " failed\n ! mpi_read_string returned %d\n", ret );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200110 goto exit;
111 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
Rich Evansf90016a2015-01-19 14:26:37 +0000113 polarssl_printf( " ! Generating large primes may take minutes!\n" );
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +0100114
Rich Evansf90016a2015-01-19 14:26:37 +0000115 polarssl_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 fflush( stdout );
117
Paul Bakker508ad5a2011-12-04 17:09:26 +0000118 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200119 (const unsigned char *) pers,
120 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000121 {
Rich Evansf90016a2015-01-19 14:26:37 +0000122 polarssl_printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000123 goto exit;
124 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000125
Rich Evansf90016a2015-01-19 14:26:37 +0000126 polarssl_printf( " ok\n . Generating the modulus, please wait..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 fflush( stdout );
128
129 /*
130 * This can take a long time...
131 */
Manuel Pégourié-Gonnardf0dd0452015-07-03 17:14:06 +0200132 if( ( ret = mpi_gen_prime( &P, nbits, 1,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000133 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 {
Rich Evansf90016a2015-01-19 14:26:37 +0000135 polarssl_printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 goto exit;
137 }
138
Rich Evansf90016a2015-01-19 14:26:37 +0000139 polarssl_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 fflush( stdout );
141
142 if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
143 {
Rich Evansf90016a2015-01-19 14:26:37 +0000144 polarssl_printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 goto exit;
146 }
147
148 if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
149 {
Rich Evansf90016a2015-01-19 14:26:37 +0000150 polarssl_printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 goto exit;
152 }
153
Paul Bakker508ad5a2011-12-04 17:09:26 +0000154 if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 {
Rich Evansf90016a2015-01-19 14:26:37 +0000156 polarssl_printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 goto exit;
158 }
159
Rich Evansf90016a2015-01-19 14:26:37 +0000160 polarssl_printf( " ok\n . Exporting the value in dh_prime.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 fflush( stdout );
162
163 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
164 {
165 ret = 1;
Rich Evansf90016a2015-01-19 14:26:37 +0000166 polarssl_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 goto exit;
168 }
169
170 if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
171 ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
172 {
Rich Evansf90016a2015-01-19 14:26:37 +0000173 polarssl_printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 goto exit;
175 }
176
Rich Evansf90016a2015-01-19 14:26:37 +0000177 polarssl_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 fclose( fout );
179
180exit:
181
Paul Bakker6c591fa2011-05-05 11:49:20 +0000182 mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
Paul Bakkera317a982014-06-18 16:44:11 +0200183 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200184 entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Paul Bakkercce9d772011-11-18 14:26:47 +0000186#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000187 polarssl_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 fflush( stdout ); getchar();
189#endif
190
191 return( ret );
192}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000193#endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO &&
Manuel Pégourié-Gonnard92e5b592013-09-18 18:57:10 +0200194 POLARSSL_CTR_DRBG_C && POLARSSL_GENPRIME */