Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (prime generation) |
| 3 | * |
Paul Bakker | 84f12b7 | 2010-07-18 10:13:04 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2010, 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/bignum.h" |
| 33 | #include "polarssl/config.h" |
| 34 | #include "polarssl/havege.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * Note: G = 4 is always a quadratic residue mod P, |
| 38 | * so it is a generator of order Q (with P = 2*Q+1). |
| 39 | */ |
| 40 | #define DH_P_SIZE 1024 |
| 41 | #define GENERATOR "4" |
| 42 | |
| 43 | int main( void ) |
| 44 | { |
| 45 | int ret = 1; |
| 46 | |
Paul Bakker | 40e4694 | 2009-01-03 21:51:57 +0000 | [diff] [blame] | 47 | #if defined(POLARSSL_GENPRIME) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | mpi G, P, Q; |
| 49 | havege_state hs; |
| 50 | FILE *fout; |
| 51 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 52 | mpi_init( &G ); mpi_init( &P ); mpi_init( &Q ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 | mpi_read_string( &G, 10, GENERATOR ); |
| 54 | |
| 55 | printf( "\n . Seeding the random number generator..." ); |
| 56 | fflush( stdout ); |
| 57 | |
| 58 | havege_init( &hs ); |
| 59 | |
| 60 | printf( " ok\n . Generating the modulus, please wait..." ); |
| 61 | fflush( stdout ); |
| 62 | |
| 63 | /* |
| 64 | * This can take a long time... |
| 65 | */ |
| 66 | if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1, |
| 67 | havege_rand, &hs ) ) != 0 ) |
| 68 | { |
| 69 | printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret ); |
| 70 | goto exit; |
| 71 | } |
| 72 | |
| 73 | printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." ); |
| 74 | fflush( stdout ); |
| 75 | |
| 76 | if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 ) |
| 77 | { |
| 78 | printf( " failed\n ! mpi_sub_int returned %d\n\n", ret ); |
| 79 | goto exit; |
| 80 | } |
| 81 | |
| 82 | if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) |
| 83 | { |
| 84 | printf( " failed\n ! mpi_div_int returned %d\n\n", ret ); |
| 85 | goto exit; |
| 86 | } |
| 87 | |
| 88 | if( ( ret = mpi_is_prime( &Q, havege_rand, &hs ) ) != 0 ) |
| 89 | { |
| 90 | printf( " failed\n ! mpi_is_prime returned %d\n\n", ret ); |
| 91 | goto exit; |
| 92 | } |
| 93 | |
| 94 | printf( " ok\n . Exporting the value in dh_prime.txt..." ); |
| 95 | fflush( stdout ); |
| 96 | |
| 97 | if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) |
| 98 | { |
| 99 | ret = 1; |
| 100 | printf( " failed\n ! Could not create dh_prime.txt\n\n" ); |
| 101 | goto exit; |
| 102 | } |
| 103 | |
| 104 | if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) || |
| 105 | ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) ) |
| 106 | { |
| 107 | printf( " failed\n ! mpi_write_file returned %d\n\n", ret ); |
| 108 | goto exit; |
| 109 | } |
| 110 | |
| 111 | printf( " ok\n\n" ); |
| 112 | fclose( fout ); |
| 113 | |
| 114 | exit: |
| 115 | |
Paul Bakker | 6c591fa | 2011-05-05 11:49:20 +0000 | [diff] [blame] | 116 | mpi_free( &G ); mpi_free( &P ); mpi_free( &Q ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | #else |
| 118 | printf( "\n ! Prime-number generation is not available.\n\n" ); |
| 119 | #endif |
| 120 | |
| 121 | #ifdef WIN32 |
| 122 | printf( " Press Enter to exit this program.\n" ); |
| 123 | fflush( stdout ); getchar(); |
| 124 | #endif |
| 125 | |
| 126 | return( ret ); |
| 127 | } |