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