Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (prime generation) |
| 3 | * |
Paul Bakker | 530927b | 2015-02-13 14:24:10 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +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 | |
| 23 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 24 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 25 | #endif |
| 26 | |
| 27 | #include <stdio.h> |
Manuel Pégourié-Gonnard | f333174 | 2015-07-03 17:18:10 +0200 | [diff] [blame^] | 28 | #include <stdlib.h> |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 29 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 30 | #include "polarssl/config.h" |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 31 | |
| 32 | #include "polarssl/bignum.h" |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 33 | #include "polarssl/entropy.h" |
| 34 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | f333174 | 2015-07-03 17:18:10 +0200 | [diff] [blame^] | 36 | #define USAGE \ |
| 37 | "\n usage: dh_genprime param=<>...\n" \ |
| 38 | "\n acceprable parameters:\n" \ |
| 39 | " bits=%%d default: 2048\n" |
| 40 | |
| 41 | #define DFL_BITS 2048 |
| 42 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 43 | /* |
| 44 | * Note: G = 4 is always a quadratic residue mod P, |
| 45 | * so it is a generator of order Q (with P = 2*Q+1). |
| 46 | */ |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 47 | #define GENERATOR "4" |
| 48 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 49 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_ENTROPY_C) || \ |
| 50 | !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 51 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 52 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 53 | ((void) argc); |
| 54 | ((void) argv); |
| 55 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 56 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_ENTROPY_C and/or " |
| 57 | "POLARSSL_FS_IO and/or POLARSSL_CTR_DRBG_C not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 58 | return( 0 ); |
| 59 | } |
| 60 | #else |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 61 | int main( int argc, char *argv[] ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 62 | { |
| 63 | int ret = 1; |
| 64 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 65 | #if defined(POLARSSL_GENPRIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 66 | mpi G, P, Q; |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 67 | entropy_context entropy; |
| 68 | ctr_drbg_context ctr_drbg; |
Paul Bakker | e0225e4 | 2013-06-06 12:52:24 +0200 | [diff] [blame] | 69 | const char *pers = "dh_genprime"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 70 | FILE *fout; |
Manuel Pégourié-Gonnard | f333174 | 2015-07-03 17:18:10 +0200 | [diff] [blame^] | 71 | int nbits = DFL_BITS; |
| 72 | int i; |
| 73 | char *p, *q; |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 74 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 75 | mpi_init( &G ); mpi_init( &P ); mpi_init( &Q ); |
Paul Bakker | 3914840 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 76 | entropy_init( &entropy ); |
Paul Bakker | 993f02c | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 77 | |
Manuel Pégourié-Gonnard | f333174 | 2015-07-03 17:18:10 +0200 | [diff] [blame^] | 78 | if( argc == 0 ) |
| 79 | { |
| 80 | usage: |
| 81 | printf( USAGE ); |
| 82 | return( 1 ); |
| 83 | } |
| 84 | |
| 85 | for( i = 1; i < argc; i++ ) |
| 86 | { |
| 87 | p = argv[i]; |
| 88 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 89 | goto usage; |
| 90 | *q++ = '\0'; |
| 91 | |
| 92 | if( strcmp( p, "bits" ) == 0 ) |
| 93 | { |
| 94 | nbits = atoi( q ); |
| 95 | if( nbits < 0 || nbits > POLARSSL_MPI_MAX_BITS ) |
| 96 | goto usage; |
| 97 | } |
| 98 | else |
| 99 | goto usage; |
| 100 | } |
| 101 | |
Paul Bakker | 993f02c | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 102 | if( ( ret = mpi_read_string( &G, 10, GENERATOR ) ) != 0 ) |
| 103 | { |
| 104 | printf( " failed\n ! mpi_read_string returned %d\n", ret ); |
| 105 | goto exit; |
| 106 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | |
| 108 | printf( "\n . Seeding the random number generator..." ); |
| 109 | fflush( stdout ); |
| 110 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 111 | if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, |
Paul Bakker | e0225e4 | 2013-06-06 12:52:24 +0200 | [diff] [blame] | 112 | (const unsigned char *) pers, |
| 113 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 114 | { |
| 115 | printf( " failed\n ! ctr_drbg_init returned %d\n", ret ); |
| 116 | goto exit; |
| 117 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | |
| 119 | printf( " ok\n . Generating the modulus, please wait..." ); |
| 120 | fflush( stdout ); |
| 121 | |
| 122 | /* |
| 123 | * This can take a long time... |
| 124 | */ |
Manuel Pégourié-Gonnard | f333174 | 2015-07-03 17:18:10 +0200 | [diff] [blame^] | 125 | if( ( ret = mpi_gen_prime( &P, nbits, 1, |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 126 | ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | { |
| 128 | printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret ); |
| 129 | goto exit; |
| 130 | } |
| 131 | |
| 132 | printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." ); |
| 133 | fflush( stdout ); |
| 134 | |
| 135 | if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 ) |
| 136 | { |
| 137 | printf( " failed\n ! mpi_sub_int returned %d\n\n", ret ); |
| 138 | goto exit; |
| 139 | } |
| 140 | |
| 141 | if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) |
| 142 | { |
| 143 | printf( " failed\n ! mpi_div_int returned %d\n\n", ret ); |
| 144 | goto exit; |
| 145 | } |
| 146 | |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 147 | if( ( ret = mpi_is_prime( &Q, ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | { |
| 149 | printf( " failed\n ! mpi_is_prime returned %d\n\n", ret ); |
| 150 | goto exit; |
| 151 | } |
| 152 | |
| 153 | printf( " ok\n . Exporting the value in dh_prime.txt..." ); |
| 154 | fflush( stdout ); |
| 155 | |
| 156 | if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) |
| 157 | { |
| 158 | ret = 1; |
| 159 | printf( " failed\n ! Could not create dh_prime.txt\n\n" ); |
| 160 | goto exit; |
| 161 | } |
| 162 | |
| 163 | if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) || |
| 164 | ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) ) |
| 165 | { |
| 166 | printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); |
| 167 | goto exit; |
| 168 | } |
| 169 | |
| 170 | printf( " ok\n\n" ); |
| 171 | fclose( fout ); |
| 172 | |
| 173 | exit: |
| 174 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 175 | mpi_free( &G ); mpi_free( &P ); mpi_free( &Q ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | #else |
| 177 | printf( "\n ! Prime-number generation is not available.\n\n" ); |
| 178 | #endif |
| 179 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 180 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | printf( " Press Enter to exit this program.\n" ); |
| 182 | fflush( stdout ); getchar(); |
| 183 | #endif |
| 184 | |
| 185 | return( ret ); |
| 186 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 187 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_ENTROPY_C && POLARSSL_FS_IO && |
| 188 | POLARSSL_CTR_DRBG_C */ |