summaryrefslogtreecommitdiff
path: root/src/prime.c
blob: 298700eb328dbfe5830dd7f4c4bb017f2d5bbec1 (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
/*
 *      Copyright (C) 2001 Nikos Mavroyanopoulos
 *
 * 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "prime-gaa.h"
#include <gnutls/gnutls.h>
#include "../lib/defines.h"

int main(int argc, char **argv)
{
	gaainfo info;
	unsigned int i;
	gnutls_dh_params dh_params;
	gnutls_datum p, g;

	if (gaa(argc, argv, &info) != -1) {
		fprintf(stderr, "Error in the arguments.\n");
		return -1;
	}
	
	gnutls_global_init();
	
	gnutls_dh_params_init( &dh_params);

	fprintf(stderr, "Generating DH parameters...");
	
	gnutls_dh_params_generate2( dh_params, info.bits);
	gnutls_dh_params_export_raw( dh_params, &p, &g, NULL);

	if (info.cparams) {

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

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

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

		printf("\n\n");
	}

	/* print prime */

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

		printf("\n};\n");
	} else {
		printf( "Prime: ");

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

		printf("\n\n");

	}

	if (!info.cparams) { /* generate a PKCS#3 structure */
	
		unsigned char out[5*1024];
		int ret, len = sizeof(out);
	
		ret = gnutls_dh_params_export_pkcs3( dh_params, GNUTLS_X509_FMT_PEM,
			out, &len);
	
		if (ret == 0) {
			printf("\n%s", out);
		} else {
			fprintf(stderr, "Error: %s\n", gnutls_strerror(ret));
		}

	}

	gnutls_global_deinit();
	
	return 0;
}