Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (prime generation) |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * 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é-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
| 29 | #include <stdio.h> |
| 30 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 31 | #include "polarssl/bignum.h" |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 32 | #include "polarssl/entropy.h" |
| 33 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 34 | |
| 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 Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 42 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 43 | !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C) || \ |
| 44 | !defined(POLARSSL_GENPRIME) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 45 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 46 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 47 | ((void) argc); |
| 48 | ((void) argv); |
| 49 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 50 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 51 | "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C and/or " |
| 52 | "POLARSSL_GENPRIME not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 53 | return( 0 ); |
| 54 | } |
| 55 | #else |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 56 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | { |
| 58 | int ret = 1; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 59 | mpi G, P, Q; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 60 | entropy_context entropy; |
| 61 | ctr_drbg_context ctr_drbg; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 62 | const char *pers = "dh_genprime"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 63 | FILE *fout; |
| 64 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 65 | ((void) argc); |
| 66 | ((void) argv); |
| 67 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 68 | mpi_init( &G ); mpi_init( &P ); mpi_init( &Q ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 69 | entropy_init( &entropy ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 70 | |
| 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 76 | |
Paul Bakker | 5da01ca | 2012-10-03 19:48:33 +0000 | [diff] [blame] | 77 | 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 | |
Manuel Pégourié-Gonnard | 5e1e611 | 2013-11-22 21:16:10 +0100 | [diff] [blame] | 84 | printf( " ! Generating large primes may take minutes!\n" ); |
| 85 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 86 | printf( "\n . Seeding the random number generator..." ); |
| 87 | fflush( stdout ); |
| 88 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 89 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 90 | (const unsigned char *) pers, |
| 91 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 92 | { |
| 93 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 94 | goto exit; |
| 95 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | |
| 97 | printf( " ok\n . Generating the modulus, please wait..." ); |
| 98 | fflush( stdout ); |
| 99 | |
| 100 | /* |
| 101 | * This can take a long time... |
| 102 | */ |
| 103 | if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1, |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 104 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | { |
| 106 | printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret ); |
| 107 | goto exit; |
| 108 | } |
| 109 | |
| 110 | printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." ); |
| 111 | fflush( stdout ); |
| 112 | |
| 113 | if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 ) |
| 114 | { |
| 115 | printf( " failed\n ! mpi_sub_int returned %d\n\n", ret ); |
| 116 | goto exit; |
| 117 | } |
| 118 | |
| 119 | if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) |
| 120 | { |
| 121 | printf( " failed\n ! mpi_div_int returned %d\n\n", ret ); |
| 122 | goto exit; |
| 123 | } |
| 124 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 125 | if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 126 | { |
| 127 | printf( " failed\n ! mpi_is_prime returned %d\n\n", ret ); |
| 128 | goto exit; |
| 129 | } |
| 130 | |
| 131 | printf( " ok\n . Exporting the value in dh_prime.txt..." ); |
| 132 | fflush( stdout ); |
| 133 | |
| 134 | if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) |
| 135 | { |
| 136 | ret = 1; |
| 137 | printf( " failed\n ! Could not create dh_prime.txt\n\n" ); |
| 138 | goto exit; |
| 139 | } |
| 140 | |
| 141 | if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) || |
| 142 | ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) ) |
| 143 | { |
| 144 | printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); |
| 145 | goto exit; |
| 146 | } |
| 147 | |
| 148 | printf( " ok\n\n" ); |
| 149 | fclose( fout ); |
| 150 | |
| 151 | exit: |
| 152 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 153 | mpi_free( &G ); mpi_free( &P ); mpi_free( &Q ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 154 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 155 | entropy_free( &entropy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 157 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 158 | printf( " Press Enter to exit this program.\n" ); |
| 159 | fflush( stdout ); getchar(); |
| 160 | #endif |
| 161 | |
| 162 | return( ret ); |
| 163 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 164 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO && |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 165 | POLARSSL_CTR_DRBG_C && POLARSSL_GENPRIME */ |