certs: add proper const attributes to global variables
[tropicssl.git] / programs / pkey / dh_genprime.c
blob08157c538b4914020d2757e7643bcf036ba1500a
1 /*
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>
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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
38 #endif
40 #include <stdio.h>
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
51 #define GENERATOR "4"
53 int main(void)
55 int ret = 1;
57 #if defined(TROPICSSL_GENPRIME)
58 mpi G, P, Q;
59 havege_state hs;
60 FILE *fout;
62 mpi_init(&G, &P, &Q, NULL);
63 mpi_read_string(&G, 10, GENERATOR);
65 printf("\n . Seeding the random number generator...");
66 fflush(stdout);
68 havege_init(&hs);
70 printf(" ok\n . Generating the modulus, please wait...");
71 fflush(stdout);
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);
78 goto exit;
81 printf(" ok\n . Verifying that Q = (P-1)/2 is prime...");
82 fflush(stdout);
84 if ((ret = mpi_sub_int(&Q, &P, 1)) != 0) {
85 printf(" failed\n ! mpi_sub_int returned %d\n\n", ret);
86 goto exit;
89 if ((ret = mpi_div_int(&Q, NULL, &Q, 2)) != 0) {
90 printf(" failed\n ! mpi_div_int returned %d\n\n", ret);
91 goto exit;
94 if ((ret = mpi_is_prime(&Q, havege_rand, &hs)) != 0) {
95 printf(" failed\n ! mpi_is_prime returned %d\n\n", ret);
96 goto exit;
99 printf(" ok\n . Exporting the value in dh_prime.txt...");
100 fflush(stdout);
102 if ((fout = fopen("dh_prime.txt", "wb+")) == NULL) {
103 ret = 1;
104 printf(" failed\n ! Could not create dh_prime.txt\n\n");
105 goto exit;
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);
111 goto exit;
114 printf(" ok\n\n");
115 fclose(fout);
117 exit:
119 mpi_free(&Q, &P, &G, NULL);
120 #else
121 printf("\n ! Prime-number generation is not available.\n\n");
122 #endif
124 #ifdef WIN32
125 printf(" Press Enter to exit this program.\n");
126 fflush(stdout);
127 getchar();
128 #endif
130 return (ret);