Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (prime generation) |
| 3 | * |
Paul Bakker | 83ded91 | 2010-03-21 17:46:26 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, Paul Bakker <polarssl_maintainer at polarssl.org> |
Paul Bakker | 77b385e | 2009-07-28 17:23:11 +0000 | [diff] [blame] | 5 | * All rights reserved. |
Paul Bakker | e0ccd0a | 2009-01-04 16:27:10 +0000 | [diff] [blame] | 6 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 23 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 24 | #endif |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 28 | #include "polarssl/bignum.h" |
| 29 | #include "polarssl/config.h" |
| 30 | #include "polarssl/havege.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * Note: G = 4 is always a quadratic residue mod P, |
| 34 | * so it is a generator of order Q (with P = 2*Q+1). |
| 35 | */ |
| 36 | #define DH_P_SIZE 1024 |
| 37 | #define GENERATOR "4" |
| 38 | |
| 39 | int main( void ) |
| 40 | { |
| 41 | int ret = 1; |
| 42 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 43 | #if defined(POLARSSL_GENPRIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 44 | mpi G, P, Q; |
| 45 | havege_state hs; |
| 46 | FILE *fout; |
| 47 | |
| 48 | mpi_init( &G, &P, &Q, NULL ); |
| 49 | mpi_read_string( &G, 10, GENERATOR ); |
| 50 | |
| 51 | printf( "\n . Seeding the random number generator..." ); |
| 52 | fflush( stdout ); |
| 53 | |
| 54 | havege_init( &hs ); |
| 55 | |
| 56 | printf( " ok\n . Generating the modulus, please wait..." ); |
| 57 | fflush( stdout ); |
| 58 | |
| 59 | /* |
| 60 | * This can take a long time... |
| 61 | */ |
| 62 | if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1, |
| 63 | havege_rand, &hs ) ) != 0 ) |
| 64 | { |
| 65 | printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret ); |
| 66 | goto exit; |
| 67 | } |
| 68 | |
| 69 | printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." ); |
| 70 | fflush( stdout ); |
| 71 | |
| 72 | if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 ) |
| 73 | { |
| 74 | printf( " failed\n ! mpi_sub_int returned %d\n\n", ret ); |
| 75 | goto exit; |
| 76 | } |
| 77 | |
| 78 | if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) |
| 79 | { |
| 80 | printf( " failed\n ! mpi_div_int returned %d\n\n", ret ); |
| 81 | goto exit; |
| 82 | } |
| 83 | |
| 84 | if( ( ret = mpi_is_prime( &Q, havege_rand, &hs ) ) != 0 ) |
| 85 | { |
| 86 | printf( " failed\n ! mpi_is_prime returned %d\n\n", ret ); |
| 87 | goto exit; |
| 88 | } |
| 89 | |
| 90 | printf( " ok\n . Exporting the value in dh_prime.txt..." ); |
| 91 | fflush( stdout ); |
| 92 | |
| 93 | if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) |
| 94 | { |
| 95 | ret = 1; |
| 96 | printf( " failed\n ! Could not create dh_prime.txt\n\n" ); |
| 97 | goto exit; |
| 98 | } |
| 99 | |
| 100 | if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) || |
| 101 | ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) ) |
| 102 | { |
| 103 | printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); |
| 104 | goto exit; |
| 105 | } |
| 106 | |
| 107 | printf( " ok\n\n" ); |
| 108 | fclose( fout ); |
| 109 | |
| 110 | exit: |
| 111 | |
| 112 | mpi_free( &Q, &P, &G, NULL ); |
| 113 | #else |
| 114 | printf( "\n ! Prime-number generation is not available.\n\n" ); |
| 115 | #endif |
| 116 | |
| 117 | #ifdef WIN32 |
| 118 | printf( " Press Enter to exit this program.\n" ); |
| 119 | fflush( stdout ); getchar(); |
| 120 | #endif |
| 121 | |
| 122 | return( ret ); |
| 123 | } |