blob: 840a569b8a6f37b9f8d8e3730fe6292a55bdc30a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (prime generation)
3 *
Paul Bakker84f12b72010-07-18 10:13:04 +00004 * Copyright (C) 2006-2010, 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/bignum.h"
33#include "polarssl/config.h"
34#include "polarssl/havege.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
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
43int main( void )
44{
45 int ret = 1;
46
Paul Bakker40e46942009-01-03 21:51:57 +000047#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000048 mpi G, P, Q;
49 havege_state hs;
50 FILE *fout;
51
Paul Bakker6c591fa2011-05-05 11:49:20 +000052 mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
Paul Bakker5121ce52009-01-03 21:22:43 +000053 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
114exit:
115
Paul Bakker6c591fa2011-05-05 11:49:20 +0000116 mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000117#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}