2 * Diffie-Hellman-Merkle key exchange (prime generation)
4 * Based on XySSL: Copyright (C) 2006-2008 Christophe Devine
6 * Copyright (C) 2009 Paul Bakker <polarssl_maintainer at polarssl dot org>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * * Neither the names of PolarSSL or XySSL nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #ifndef _CRT_SECURE_NO_DEPRECATE
37 #define _CRT_SECURE_NO_DEPRECATE 1
42 #include "tropicssl/bignum.h"
43 #include "tropicssl/config.h"
44 #include "tropicssl/havege.h"
47 * Note: G = 4 is always a quadratic residue mod P,
48 * so it is a generator of order Q (with P = 2*Q+1).
50 #define DH_P_SIZE 1024
57 #if defined(TROPICSSL_GENPRIME)
62 mpi_init(&G
, &P
, &Q
, NULL
);
63 mpi_read_string(&G
, 10, GENERATOR
);
65 printf("\n . Seeding the random number generator...");
70 printf(" ok\n . Generating the modulus, please wait...");
74 * This can take a long time...
76 if ((ret
= mpi_gen_prime(&P
, DH_P_SIZE
, 1, havege_rand
, &hs
)) != 0) {
77 printf(" failed\n ! mpi_gen_prime returned %d\n\n", ret
);
81 printf(" ok\n . Verifying that Q = (P-1)/2 is prime...");
84 if ((ret
= mpi_sub_int(&Q
, &P
, 1)) != 0) {
85 printf(" failed\n ! mpi_sub_int returned %d\n\n", ret
);
89 if ((ret
= mpi_div_int(&Q
, NULL
, &Q
, 2)) != 0) {
90 printf(" failed\n ! mpi_div_int returned %d\n\n", ret
);
94 if ((ret
= mpi_is_prime(&Q
, havege_rand
, &hs
)) != 0) {
95 printf(" failed\n ! mpi_is_prime returned %d\n\n", ret
);
99 printf(" ok\n . Exporting the value in dh_prime.txt...");
102 if ((fout
= fopen("dh_prime.txt", "wb+")) == NULL
) {
104 printf(" failed\n ! Could not create dh_prime.txt\n\n");
108 if ((ret
= mpi_write_file("P = ", &P
, 16, fout
) != 0) ||
109 (ret
= mpi_write_file("G = ", &G
, 16, fout
) != 0)) {
110 printf(" failed\n ! mpi_write_file returned %d\n\n", ret
);
119 mpi_free(&Q
, &P
, &G
, NULL
);
121 printf("\n ! Prime-number generation is not available.\n\n");
125 printf(" Press Enter to exit this program.\n");