blob: 6169e916f3fb4c1134868a00b38a183f5df5275d [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_BIGNUM_C) && defined(MBEDTLS_ENTROPY_C) && \
37 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
38 defined(MBEDTLS_GENPRIME)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/bignum.h"
40#include "mbedtls/entropy.h"
41#include "mbedtls/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000042
Rich Evans18b78c72015-02-11 14:06:19 +000043#include <stdio.h>
44#include <string.h>
45#endif
46
Paul Bakker5121ce52009-01-03 21:22:43 +000047/*
48 * Note: G = 4 is always a quadratic residue mod P,
49 * so it is a generator of order Q (with P = 2*Q+1).
50 */
51#define DH_P_SIZE 1024
52#define GENERATOR "4"
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
55 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
56 !defined(MBEDTLS_GENPRIME)
Rich Evans85b05ec2015-02-12 11:37:29 +000057int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000058{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
60 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
61 "MBEDTLS_GENPRIME not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000062 return( 0 );
63}
64#else
Rich Evans85b05ec2015-02-12 11:37:29 +000065int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000066{
67 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 mbedtls_mpi G, P, Q;
69 mbedtls_entropy_context entropy;
70 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020071 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000072 FILE *fout;
73
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020075 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_entropy_init( &entropy );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020081 goto exit;
82 }
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_printf( "\nWARNING: You should not generate and use your own DHM primes\n" );
85 mbedtls_printf( " unless you are very certain of what you are doing!\n" );
86 mbedtls_printf( " Failing to follow this instruction may result in\n" );
87 mbedtls_printf( " weak security for your connections! Use the\n" );
88 mbedtls_printf( " predefined DHM parameters from dhm.h instead!\n\n" );
89 mbedtls_printf( "============================================================\n\n" );
Paul Bakker5da01ca2012-10-03 19:48:33 +000090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 mbedtls_printf( " ! Generating large primes may take minutes!\n" );
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +010092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +000094 fflush( stdout );
95
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020096 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +020097 (const unsigned char *) pers,
98 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000099 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200100 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000101 goto exit;
102 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_printf( " ok\n . Generating the modulus, please wait..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 fflush( stdout );
106
107 /*
108 * This can take a long time...
109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 if( ( ret = mbedtls_mpi_gen_prime( &P, DH_P_SIZE, 1,
111 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_printf( " failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 goto exit;
115 }
116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 fflush( stdout );
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_printf( " failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 goto exit;
124 }
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( " failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 goto exit;
130 }
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( ( ret = mbedtls_mpi_is_prime( &Q, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 fflush( stdout );
140
141 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
142 {
143 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 goto exit;
146 }
147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
149 ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 goto exit;
153 }
154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 fclose( fout );
157
158exit:
159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
161 mbedtls_ctr_drbg_free( &ctr_drbg );
162 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163
Paul Bakkercce9d772011-11-18 14:26:47 +0000164#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 fflush( stdout ); getchar();
167#endif
168
169 return( ret );
170}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
172 MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */