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