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