blob: f6ae03fb43909704ca83a1f9a93322046ffa7715 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (prime generation)
3 *
Paul Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +00008 *
Paul Bakker77b385e2009-07-28 17:23:11 +00009 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 *
Paul Bakker5121ce52009-01-03 21:22:43 +000011 * 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 Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/config.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000033
34#include "polarssl/bignum.h"
Paul Bakker40e46942009-01-03 21:51:57 +000035#include "polarssl/havege.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000036
37/*
38 * Note: G = 4 is always a quadratic residue mod P,
39 * so it is a generator of order Q (with P = 2*Q+1).
40 */
41#define DH_P_SIZE 1024
42#define GENERATOR "4"
43
Paul Bakker5690efc2011-05-26 13:16:06 +000044#if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_HAVEGE_C) || \
45 !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000046int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000047{
Paul Bakkercce9d772011-11-18 14:26:47 +000048 ((void) argc);
49 ((void) argv);
50
Paul Bakker5690efc2011-05-26 13:16:06 +000051 printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
52 "POLARSSL_FS_IO not defined.\n");
53 return( 0 );
54}
55#else
Paul Bakkercce9d772011-11-18 14:26:47 +000056int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000057{
58 int ret = 1;
59
Paul Bakker40e46942009-01-03 21:51:57 +000060#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000061 mpi G, P, Q;
62 havege_state hs;
63 FILE *fout;
64
Paul Bakkercce9d772011-11-18 14:26:47 +000065 ((void) argc);
66 ((void) argv);
67
Paul Bakker6c591fa2011-05-05 11:49:20 +000068 mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
Paul Bakker5121ce52009-01-03 21:22:43 +000069 mpi_read_string( &G, 10, GENERATOR );
70
71 printf( "\n . Seeding the random number generator..." );
72 fflush( stdout );
73
74 havege_init( &hs );
75
76 printf( " ok\n . Generating the modulus, please wait..." );
77 fflush( stdout );
78
79 /*
80 * This can take a long time...
81 */
82 if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
83 havege_rand, &hs ) ) != 0 )
84 {
85 printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
86 goto exit;
87 }
88
89 printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
90 fflush( stdout );
91
92 if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
93 {
94 printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
95 goto exit;
96 }
97
98 if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
99 {
100 printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
101 goto exit;
102 }
103
104 if( ( ret = mpi_is_prime( &Q, havege_rand, &hs ) ) != 0 )
105 {
106 printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
107 goto exit;
108 }
109
110 printf( " ok\n . Exporting the value in dh_prime.txt..." );
111 fflush( stdout );
112
113 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
114 {
115 ret = 1;
116 printf( " failed\n ! Could not create dh_prime.txt\n\n" );
117 goto exit;
118 }
119
120 if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
121 ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
122 {
123 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
124 goto exit;
125 }
126
127 printf( " ok\n\n" );
128 fclose( fout );
129
130exit:
131
Paul Bakker6c591fa2011-05-05 11:49:20 +0000132 mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000133#else
134 printf( "\n ! Prime-number generation is not available.\n\n" );
135#endif
136
Paul Bakkercce9d772011-11-18 14:26:47 +0000137#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 printf( " Press Enter to exit this program.\n" );
139 fflush( stdout ); getchar();
140#endif
141
142 return( ret );
143}
Paul Bakker5690efc2011-05-26 13:16:06 +0000144#endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_FS_IO */