summaryrefslogtreecommitdiff
path: root/src/prime.c
blob: f340b9b70a2eb2001c3ea7e2218fefe8f299512d (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
/*
 *      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 "../lib/defines.h"
#include "../lib/gnutls_int.h"
#include "../lib/gnutls_srp.h"
#include "../lib/crypt.h"
#include "../lib/x509_b64.h"
#include "prime-gaa.h"

MPI _gcry_generate_elg_prime( int mode, unsigned pbits, unsigned qbits,
	                MPI g, MPI **ret_factors );

int main(int argc, char **argv)
{
	gaainfo info;
	int size, i, qbits;
	MPI prime;
	uint8 * tmp;
	MPI g;

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

	/* this is an emulation of Michael Wiener's table
	 * bad emulation.
	 */
	qbits = 120 + ( ((info.bits/256)-1)*20 );
	if( qbits & 1 ) /* better have a even one */
	qbits++;

	g = mpi_new(16);
	prime = _gcry_generate_elg_prime( 0, info.bits, qbits, g, NULL);

	/* print generator */
	size = 0;
	gcry_mpi_print(GCRYMPI_FMT_USG, NULL, &size, g);

	tmp = malloc(size);
   	gcry_mpi_print(GCRYMPI_FMT_USG, tmp, &size, g);

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

	printf("\n};\n\n");
	free(tmp);

	/* print prime */
	size = 0;
	gcry_mpi_print(GCRYMPI_FMT_USG, NULL, &size, prime);

	tmp = malloc(size);
   	gcry_mpi_print(GCRYMPI_FMT_USG, tmp, &size, prime);

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

	printf("\n};\n");
	free(tmp);

	return 0;
}