Update.
[gnutls.git] / src / prime.c
blob6945abed5449f4fd11b7a69500fdd1bd622aa450
1 /*
2 * Copyright (C) 2004,2005 Free Software Foundation
3 * Copyright (C) 2001,2002,2003 Nikos Mavroyanopoulos
5 * This file is part of GNUTLS.
7 * GNUTLS is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * GNUTLS is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #include <config.h>
24 #ifdef ENABLE_PKI
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <gnutls/gnutls.h>
32 /* Generates Diffie Hellman parameters (a prime and a generator
33 * of the group). Exports them in PKCS #3 format. Used by certtool.
36 extern FILE *outfile;
37 extern FILE *infile;
38 extern unsigned char buffer[];
39 extern const int buffer_size;
41 static int cparams = 0;
43 /* If how is zero then the included parameters are used.
45 int
46 generate_prime (int bits, int how)
48 unsigned int i;
49 int ret;
50 gnutls_dh_params dh_params;
51 gnutls_datum p, g;
53 gnutls_dh_params_init (&dh_params);
55 fprintf (stderr, "Generating DH parameters...");
57 if (how != 0)
59 ret = gnutls_dh_params_generate2 (dh_params, bits);
60 if (ret < 0)
62 fprintf (stderr, "Error generating parameters: %s\n",
63 gnutls_strerror (ret));
64 exit (1);
67 ret = gnutls_dh_params_export_raw (dh_params, &p, &g, NULL);
68 if (ret < 0)
70 fprintf (stderr, "Error exporting parameters: %s\n",
71 gnutls_strerror (ret));
72 exit (1);
75 else
77 #ifdef ENABLE_SRP
78 if (bits <= 1024)
80 p = gnutls_srp_1024_group_prime;
81 g = gnutls_srp_1024_group_generator;
83 else if (bits <= 1536)
85 p = gnutls_srp_1536_group_prime;
86 g = gnutls_srp_1536_group_generator;
88 else
90 p = gnutls_srp_2048_group_prime;
91 g = gnutls_srp_2048_group_generator;
94 ret = gnutls_dh_params_import_raw (dh_params, &p, &g);
95 if (ret < 0)
97 fprintf (stderr, "Error exporting parameters: %s\n",
98 gnutls_strerror (ret));
99 exit (1);
101 #else
102 fprintf (stderr, "Parameters unavailable as SRP disabled.\n");
103 #endif
106 if (cparams)
109 fprintf (outfile, "/* generator */\n");
110 fprintf (outfile, "\nconst uint8 g[%d] = { ", g.size);
112 for (i = 0; i < g.size; i++)
114 if (i % 7 == 0)
115 fprintf (outfile, "\n\t");
116 fprintf (outfile, "0x%.2x", g.data[i]);
117 if (i != g.size - 1)
118 fprintf (outfile, ", ");
121 fprintf (outfile, "\n};\n\n");
123 else
125 fprintf (outfile, "\nGenerator: ");
127 for (i = 0; i < g.size; i++)
129 if (i != 0 && i % 12 == 0)
130 fprintf (outfile, "\n\t");
131 else if (i != 0 && i != g.size)
132 fprintf (outfile, ":");
134 fprintf (outfile, "%.2x", g.data[i]);
137 fprintf (outfile, "\n\n");
140 /* print prime */
142 if (cparams)
144 fprintf (outfile, "/* prime - %d bits */\n", p.size * 8);
145 fprintf (outfile, "\nconst uint8 prime[%d] = { ", p.size);
147 for (i = 0; i < p.size; i++)
149 if (i % 7 == 0)
150 fprintf (outfile, "\n\t");
151 fprintf (outfile, "0x%.2x", p.data[i]);
152 if (i != p.size - 1)
153 fprintf (outfile, ", ");
156 fprintf (outfile, "\n};\n");
158 else
160 fprintf (outfile, "Prime: ");
162 for (i = 0; i < p.size; i++)
164 if (i != 0 && i % 12 == 0)
165 fprintf (outfile, "\n\t");
166 else if (i != 0 && i != p.size)
167 fprintf (outfile, ":");
168 fprintf (outfile, "%.2x", p.data[i]);
171 fprintf (outfile, "\n\n");
175 if (!cparams)
176 { /* generate a PKCS#3 structure */
178 int ret;
179 size_t len = buffer_size;
181 ret = gnutls_dh_params_export_pkcs3 (dh_params, GNUTLS_X509_FMT_PEM,
182 buffer, &len);
184 if (ret == 0)
186 fprintf (outfile, "\n%s", buffer);
188 else
190 fprintf (stderr, "Error: %s\n", gnutls_strerror (ret));
195 return 0;
198 #endif