blob: 410139e313dd87b55a3d441cfd1f2c78718a86c3 [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/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)
46int main( void )
47{
48 printf("POLARSSL_BIGNUM_C and/or POLARSSL_HAVEGE_C and/or "
49 "POLARSSL_FS_IO not defined.\n");
50 return( 0 );
51}
52#else
Paul Bakker5121ce52009-01-03 21:22:43 +000053int main( void )
54{
55 int ret = 1;
56
Paul Bakker40e46942009-01-03 21:51:57 +000057#if defined(POLARSSL_GENPRIME)
Paul Bakker5121ce52009-01-03 21:22:43 +000058 mpi G, P, Q;
59 havege_state hs;
60 FILE *fout;
61
Paul Bakker6c591fa2011-05-05 11:49:20 +000062 mpi_init( &G ); mpi_init( &P ); mpi_init( &Q );
Paul Bakker5121ce52009-01-03 21:22:43 +000063 mpi_read_string( &G, 10, GENERATOR );
64
65 printf( "\n . Seeding the random number generator..." );
66 fflush( stdout );
67
68 havege_init( &hs );
69
70 printf( " ok\n . Generating the modulus, please wait..." );
71 fflush( stdout );
72
73 /*
74 * This can take a long time...
75 */
76 if( ( ret = mpi_gen_prime( &P, DH_P_SIZE, 1,
77 havege_rand, &hs ) ) != 0 )
78 {
79 printf( " failed\n ! mpi_gen_prime returned %d\n\n", ret );
80 goto exit;
81 }
82
83 printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
84 fflush( stdout );
85
86 if( ( ret = mpi_sub_int( &Q, &P, 1 ) ) != 0 )
87 {
88 printf( " failed\n ! mpi_sub_int returned %d\n\n", ret );
89 goto exit;
90 }
91
92 if( ( ret = mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
93 {
94 printf( " failed\n ! mpi_div_int returned %d\n\n", ret );
95 goto exit;
96 }
97
98 if( ( ret = mpi_is_prime( &Q, havege_rand, &hs ) ) != 0 )
99 {
100 printf( " failed\n ! mpi_is_prime returned %d\n\n", ret );
101 goto exit;
102 }
103
104 printf( " ok\n . Exporting the value in dh_prime.txt..." );
105 fflush( stdout );
106
107 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
108 {
109 ret = 1;
110 printf( " failed\n ! Could not create dh_prime.txt\n\n" );
111 goto exit;
112 }
113
114 if( ( ret = mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
115 ( ret = mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
116 {
117 printf( " failed\n ! mpi_write_file returned %d\n\n", ret );
118 goto exit;
119 }
120
121 printf( " ok\n\n" );
122 fclose( fout );
123
124exit:
125
Paul Bakker6c591fa2011-05-05 11:49:20 +0000126 mpi_free( &G ); mpi_free( &P ); mpi_free( &Q );
Paul Bakker5121ce52009-01-03 21:22:43 +0000127#else
128 printf( "\n ! Prime-number generation is not available.\n\n" );
129#endif
130
131#ifdef WIN32
132 printf( " Press Enter to exit this program.\n" );
133 fflush( stdout ); getchar();
134#endif
135
136 return( ret );
137}
Paul Bakker5690efc2011-05-26 13:16:06 +0000138#endif /* POLARSSL_BIGNUM_C && POLARSSL_HAVEGE_C && POLARSSL_FS_IO */