| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Diffie-Hellman-Merkle key exchange (prime generation) | 
|  | 3 | * | 
| Paul Bakker | 5da01ca | 2012-10-03 19:48:33 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2012, Brainspark B.V. | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * | 
|  | 6 | *  This file is part of PolarSSL (http://www.polarssl.org) | 
| Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 7 | *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> | 
| Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 8 | * | 
| Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 9 | *  All rights reserved. | 
| Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 10 | * | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 11 | *  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 Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 32 | #include "polarssl/config.h" | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 33 |  | 
|  | 34 | #include "polarssl/bignum.h" | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 35 | #include "polarssl/entropy.h" | 
|  | 36 | #include "polarssl/ctr_drbg.h" | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 37 |  | 
|  | 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 Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 45 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) ||   \ | 
|  | 46 | !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C) | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 47 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 48 | { | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 49 | ((void) argc); | 
|  | 50 | ((void) argv); | 
|  | 51 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 52 | 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 Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 54 | return( 0 ); | 
|  | 55 | } | 
|  | 56 | #else | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 57 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | { | 
|  | 59 | int ret = 1; | 
|  | 60 |  | 
| Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 61 | #if defined(POLARSSL_GENPRIME) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | mpi G, P, Q; | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 63 | entropy_context entropy; | 
|  | 64 | ctr_drbg_context ctr_drbg; | 
|  | 65 | char *pers = "dh_genprime"; | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | FILE *fout; | 
|  | 67 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 68 | ((void) argc); | 
|  | 69 | ((void) argv); | 
|  | 70 |  | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 71 | mpi_init( &G ); mpi_init( &P ); mpi_init( &Q ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | mpi_read_string( &G, 10, GENERATOR ); | 
|  | 73 |  | 
| Paul Bakker | 5da01ca | 2012-10-03 19:48:33 +0000 | [diff] [blame] | 74 | printf( "\nWARNING: You should not generate and use your own DHM primes\n" ); | 
|  | 75 | printf( "         unless you are very certain of what you are doing!\n" ); | 
|  | 76 | printf( "         Failing to follow this instruction may result in\n" ); | 
|  | 77 | printf( "         weak security for your connections! Use the\n" ); | 
|  | 78 | printf( "         predefined DHM parameters from dhm.h instead!\n\n" ); | 
|  | 79 | printf( "============================================================\n\n" ); | 
|  | 80 |  | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 81 | printf( "\n  . Seeding the random number generator..." ); | 
|  | 82 | fflush( stdout ); | 
|  | 83 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 84 | entropy_init( &entropy ); | 
|  | 85 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, | 
|  | 86 | (unsigned char *) pers, strlen( pers ) ) ) != 0 ) | 
|  | 87 | { | 
|  | 88 | printf( " failed\n  ! ctr_drbg_init returned %d\n", ret ); | 
|  | 89 | goto exit; | 
|  | 90 | } | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 |  | 
|  | 92 | printf( " ok\n  . Generating the modulus, please wait..." ); | 
|  | 93 | fflush( stdout ); | 
|  | 94 |  | 
|  | 95 | /* | 
|  | 96 | * This can take a long time... | 
|  | 97 | */ | 
|  | 98 | if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1, | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 99 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 100 | { | 
|  | 101 | printf( " failed\n  ! mpi_gen_prime returned %d\n\n", ret ); | 
|  | 102 | goto exit; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | printf( " ok\n  . Verifying that Q = (P-1)/2 is prime..." ); | 
|  | 106 | fflush( stdout ); | 
|  | 107 |  | 
|  | 108 | if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 ) | 
|  | 109 | { | 
|  | 110 | printf( " failed\n  ! mpi_sub_int returned %d\n\n", ret ); | 
|  | 111 | goto exit; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) | 
|  | 115 | { | 
|  | 116 | printf( " failed\n  ! mpi_div_int returned %d\n\n", ret ); | 
|  | 117 | goto exit; | 
|  | 118 | } | 
|  | 119 |  | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 120 | if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 ) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 | { | 
|  | 122 | printf( " failed\n  ! mpi_is_prime returned %d\n\n", ret ); | 
|  | 123 | goto exit; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | printf( " ok\n  . Exporting the value in dh_prime.txt..." ); | 
|  | 127 | fflush( stdout ); | 
|  | 128 |  | 
|  | 129 | if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) | 
|  | 130 | { | 
|  | 131 | ret = 1; | 
|  | 132 | printf( " failed\n  ! Could not create dh_prime.txt\n\n" ); | 
|  | 133 | goto exit; | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) || | 
|  | 137 | ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) ) | 
|  | 138 | { | 
|  | 139 | printf( " failed\n  ! mpi_write_file returned %d\n\n", ret ); | 
|  | 140 | goto exit; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | printf( " ok\n\n" ); | 
|  | 144 | fclose( fout ); | 
|  | 145 |  | 
|  | 146 | exit: | 
|  | 147 |  | 
| Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 148 | mpi_free( &G ); mpi_free( &P ); mpi_free( &Q ); | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 149 | #else | 
|  | 150 | printf( "\n  ! Prime-number generation is not available.\n\n" ); | 
|  | 151 | #endif | 
|  | 152 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 153 | #if defined(_WIN32) | 
| Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | printf( "  Press Enter to exit this program.\n" ); | 
|  | 155 | fflush( stdout ); getchar(); | 
|  | 156 | #endif | 
|  | 157 |  | 
|  | 158 | return( ret ); | 
|  | 159 | } | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 160 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO && | 
|  | 161 | POLARSSL_CTR_DRBG_C */ |