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
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.
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.
46 generate_prime (int bits
, int how
)
50 gnutls_dh_params dh_params
;
53 gnutls_dh_params_init (&dh_params
);
55 fprintf (stderr
, "Generating DH parameters...");
59 ret
= gnutls_dh_params_generate2 (dh_params
, bits
);
62 fprintf (stderr
, "Error generating parameters: %s\n",
63 gnutls_strerror (ret
));
67 ret
= gnutls_dh_params_export_raw (dh_params
, &p
, &g
, NULL
);
70 fprintf (stderr
, "Error exporting parameters: %s\n",
71 gnutls_strerror (ret
));
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
;
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
);
97 fprintf (stderr
, "Error exporting parameters: %s\n",
98 gnutls_strerror (ret
));
102 fprintf (stderr
, "Parameters unavailable as SRP disabled.\n");
109 fprintf (outfile
, "/* generator */\n");
110 fprintf (outfile
, "\nconst uint8 g[%d] = { ", g
.size
);
112 for (i
= 0; i
< g
.size
; i
++)
115 fprintf (outfile
, "\n\t");
116 fprintf (outfile
, "0x%.2x", g
.data
[i
]);
118 fprintf (outfile
, ", ");
121 fprintf (outfile
, "\n};\n\n");
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");
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
++)
150 fprintf (outfile
, "\n\t");
151 fprintf (outfile
, "0x%.2x", p
.data
[i
]);
153 fprintf (outfile
, ", ");
156 fprintf (outfile
, "\n};\n");
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");
176 { /* generate a PKCS#3 structure */
179 size_t len
= buffer_size
;
181 ret
= gnutls_dh_params_export_pkcs3 (dh_params
, GNUTLS_X509_FMT_PEM
,
186 fprintf (outfile
, "\n%s", buffer
);
190 fprintf (stderr
, "Error: %s\n", gnutls_strerror (ret
));