summaryrefslogtreecommitdiff
path: root/src/prime.c
blob: 13c078027ca4da3bb2c26dcfd33c062b4cb18e08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2001,2002,2003 Nikos Mavroyanopoulos
 * Copyright (C) 2004 Free Software Foundation
 *
 * This file is part of GNUTLS.
 *
 * GNUTLS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GNUTLS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */

#include <config.h>

#ifdef ENABLE_PKI

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <gnutls/gnutls.h>

/* Generates Diffie Hellman parameters (a prime and a generator
 * of the group). Exports them in PKCS #3 format. Used by certtool.
 */

extern FILE* outfile;
extern FILE* infile;
extern unsigned char buffer[];
extern const int buffer_size;

static int cparams = 0;

int generate_prime(int bits)
{
	unsigned int i;
	int ret;
	gnutls_dh_params dh_params;
	gnutls_datum p, g;

	gnutls_dh_params_init( &dh_params);

	fprintf(stderr, "Generating DH parameters...");
	
	ret = gnutls_dh_params_generate2( dh_params, bits);
	if (ret < 0) {
		fprintf(stderr, "Error generating parameters: %s\n",
			gnutls_strerror(ret));
		exit(1);
	}

	ret = gnutls_dh_params_export_raw( dh_params, &p, &g, NULL);
	if (ret < 0) {
		fprintf(stderr, "Error exporting parameters: %s\n",
			gnutls_strerror(ret));
		exit(1);
	}

	if (cparams) {

		fprintf( outfile, "/* generator */\n"); 
		fprintf( outfile, "\nconst uint8 g[%d] = { ", g.size);
	
		for (i=0;i<g.size;i++) {
			if (i%7==0) fprintf(outfile, "\n\t");
			fprintf(outfile, "0x%.2x", g.data[i]);
			if (i!=g.size-1) fprintf(outfile, ", ");
		}

		fprintf(outfile, "\n};\n\n");
	} else {
		fprintf( outfile, "\nGenerator: ");
	
		for (i=0;i<g.size;i++) {
			if (i!=0 && i%12==0) fprintf( outfile,"\n\t");
			else if (i!=0 && i!=g.size) fprintf( outfile, ":");

			fprintf( outfile, "%.2x", g.data[i]);
		}

		fprintf( outfile,"\n\n");
	}

	/* print prime */

	if (cparams) {
		fprintf( outfile, "/* prime - %d bits */\n", p.size*8);
		fprintf( outfile, "\nconst uint8 prime[%d] = { ", p.size);
	
		for (i=0;i<p.size;i++) {
			if (i%7==0) fprintf( outfile,"\n\t");
			fprintf( outfile, "0x%.2x", p.data[i]);
			if (i!=p.size-1) fprintf( outfile, ", ");
		}

		fprintf( outfile,"\n};\n");
	} else {
		fprintf( outfile, "Prime: ");

		for (i=0;i<p.size;i++) {
			if (i!=0 && i%12==0) fprintf( outfile,"\n\t");
			else if (i!=0 && i!=p.size) fprintf( outfile, ":");
			fprintf( outfile, "%.2x", p.data[i]);
		}

		fprintf( outfile,"\n\n");

	}

	if (!cparams) { /* generate a PKCS#3 structure */
	
		int ret;
		size_t len = buffer_size;
	
		ret = gnutls_dh_params_export_pkcs3( dh_params, GNUTLS_X509_FMT_PEM,
			buffer, &len);
	
		if (ret == 0) {
			fprintf( outfile,"\n%s", buffer);
		} else {
			fprintf(stderr, "Error: %s\n", gnutls_strerror(ret));
		}

	}

	return 0;
}

#endif