summaryrefslogtreecommitdiff
path: root/lib/gnutls_dh_primes.c
blob: 80821fa420ee43f5fe061d44395ced95208fb544 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
/*
 *      Copyright (C) 2000,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 <gnutls_int.h>
#include <gnutls_errors.h>
#include <gnutls_datum.h>
#include "debug.h"

static uint8 DH_G_1024[] = { 0x05 };
static uint8 DH_G_4096[] = { 0x05 };
static uint8 DH_G_2048[] = { 0x07 };
static uint8 DH_G_3072[] = { 0x06 };

static uint8 diffie_hellman_prime_1024[128] = { 
	0xe3, 0x79, 0xb5, 0xa7, 0x47, 0x4c, 0xfd, 
	0x9c, 0x78, 0xfe, 0x17, 0x87, 0x44, 0xc4, 
	0x86, 0x2b, 0x92, 0x13, 0x43, 0xf5, 0xac, 
	0x72, 0xd2, 0xf1, 0x2a, 0xf5, 0x39, 0xa2, 
	0x79, 0x01, 0xdd, 0x4c, 0x7e, 0x5b, 0xa0, 
	0x19, 0x11, 0xd4, 0x2f, 0x0a, 0x92, 0x8d, 
	0xfd, 0xde, 0x85, 0x93, 0x99, 0xad, 0xe0, 
	0xd4, 0x0b, 0x62, 0xaa, 0x86, 0xa7, 0xd7, 
	0x63, 0x2e, 0x35, 0x96, 0x88, 0xbe, 0x52, 
	0x2e, 0x8c, 0x27, 0xf0, 0xe0, 0xa1, 0x0e, 
	0xb7, 0xb9, 0xc8, 0xbd, 0x5d, 0xe8, 0xdb, 
	0x63, 0xd8, 0xb4, 0xe7, 0x0d, 0xff, 0x0f, 
	0x55, 0xe7, 0x27, 0x0d, 0xb7, 0x57, 0x33, 
	0x30, 0xd6, 0xeb, 0x51, 0x99, 0x86, 0x17, 
	0x5b, 0x48, 0xb3, 0x0c, 0xae, 0xbd, 0xa1, 
	0x83, 0x6b, 0xbd, 0x9f, 0x83, 0x83, 0x2b, 
	0x46, 0x3e, 0x18, 0xa4, 0x4d, 0x82, 0x95, 
	0xa4, 0x08, 0xdd, 0x28, 0x0c, 0x4f, 0x93, 
	0xfd, 0xd7
};

/* prime - 4096 bits */
static uint8 diffie_hellman_prime_4096[512] = { 
	0x8b, 0x4a, 0x26, 0x05, 0x19, 0x85, 0x6d, 
	0x2f, 0xe7, 0xf3, 0x2d, 0x92, 0x66, 0xf7, 
	0xd2, 0xab, 0xfd, 0x79, 0x6a, 0x90, 0x69, 
	0xee, 0xcb, 0xf0, 0x3c, 0xfd, 0xcd, 0x10, 
	0xc4, 0xf3, 0x59, 0x7f, 0xa2, 0xd8, 0x2c, 
	0x7d, 0x8d, 0x82, 0xf3, 0x95, 0x40, 0x98, 
	0xba, 0x8e, 0xe6, 0xa4, 0xaf, 0x74, 0x58, 
	0xe6, 0x96, 0xf2,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, 
	0xe3, 0xba, 0x98, 0xd5, 0xcc, 0x8d, 0x77, 
	0x4c, 0xcd, 0x62, 0x92, 0xb4, 0x8f, 0xf3, 
	0x75, 0xbc, 0x1f, 0x15, 0x2c, 0x00, 0xec, 
	0xa7, 0x62, 0x81, 0x95, 0x48, 0xfe, 0x0f, 
	0x4b, 0xb8, 0xda, 0x7c, 0xf4, 0x97, 0x2d, 
	0x71, 0x1d, 0x0f, 0xc2, 0xd8, 0xd8, 0x05, 
	0xdd, 0x90, 0x76, 0x77, 0xbe, 0xf0, 0x11, 
	0xb7, 0xbe, 0x4c, 0x71, 0x46, 0xa4, 0x80, 
	0x94, 0xa8, 0x08, 0xe5, 0x3a, 0xe6, 0x36, 
	0x8d, 0xd9, 0x22, 0x73, 0x97, 0x18, 0xbd, 
	0xf3, 0x1e, 0x47, 0x9e, 0x05, 0xac, 0x50, 
	0x1b, 0xde, 0xd0, 0x60, 0x51, 0x1e, 0x56, 
	0x93, 0x19, 0xcd, 0x1c, 0xcd, 0x23, 0x9e, 
	0x3f, 0x20, 0x0f, 0xac, 0x26, 0x2e, 0x5c, 
	0xbd, 0x96, 0x30, 0x1e, 0x88, 0xc9, 0xb0, 
	0xb8, 0x43, 0x64, 0x07, 0xa9, 0x97, 0x50, 
	0xa0, 0x2a, 0x50, 0xe9, 0x16, 0x26, 0x94, 
	0x8d, 0xec, 0xe7, 0x36, 0xc7, 0xcd, 0x33, 
	0x19, 0xa6, 0x2f, 0x11, 0x04, 0xef, 0x3b, 
	0x09, 0xbe, 0xa3, 0xaa, 0x4e, 0x71, 0x11, 
	0xef, 0x20, 0x13, 0x16, 0x8a, 0x14, 0xe4, 
	0xc2, 0x4d, 0xeb, 0x1a, 0x2a, 0xfb
};


static uint8 diffie_hellman_prime_2048[256] = { 
	0xf5, 0x8e, 0x4b, 0x03, 0x6d, 0xf6, 0xfc, 
	0x3b, 0x42, 0xd1, 0x9f, 0x47, 0x0d, 0xf4, 
	0x28, 0x7b, 0xed, 0x1b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x8f, 0x81, 0xf2, 
	0x7e, 0x43, 0x36, 0x0f, 0x6a, 0xe3, 0xa5, 
	0x33, 0x83, 0x31, 0x63, 0x4a, 0xf5, 0x07, 
	0x30, 0xa1, 0x90, 0xd0, 0xe4, 0xe1, 0x3c, 
	0x3e, 0xb4, 0xc0, 0x2b, 0x79, 0x3d, 0x62, 
	0xf7, 0x24, 0x58, 0x6b, 0x5f, 0x60, 0x1f, 
	0xb3, 0x3a, 0xc3, 0x44, 0xee, 0x80, 0xe8, 
	0x51, 0xb1, 0xfb, 0x2e, 0x85, 0x71, 0x8e, 
	0x1a, 0x74, 0xff, 0x87, 0x65, 0x5f, 0x10, 
	0xaa, 0xfd, 0x4f, 0xb7
};

/* Holds the prime to be used in DH authentication.
 * Initialy the GNUTLS_MPIs are not calculated (must call global_init, or _gnutls_dh_calc_mpis()).
 */
_GNUTLS_DH_PARAMS _gnutls_dh_default_params[] = {
	{768, NULL, NULL, {DH_G_1024, sizeof(DH_G_1024)}
	 , {diffie_hellman_prime_1024, sizeof diffie_hellman_prime_1024}
	 , 0, 180}
	,
	{1024, NULL, NULL, {DH_G_1024, sizeof(DH_G_1024)}
	 , {diffie_hellman_prime_1024, sizeof diffie_hellman_prime_1024}
	 , 0, 180}
	,
	{2048, NULL, NULL, {DH_G_2048, sizeof(DH_G_2048)}
	 , {diffie_hellman_prime_2048, sizeof diffie_hellman_prime_2048}
	 , 0, 260}
	,
	{3072, NULL, NULL, {DH_G_3072, sizeof(DH_G_3072)}
	 , {diffie_hellman_prime_3072, sizeof diffie_hellman_prime_3072}
	 , 0, 340}
	,
	{4096, NULL, NULL, {DH_G_4096, sizeof(DH_G_4096)}
	 , {diffie_hellman_prime_4096, sizeof diffie_hellman_prime_4096}
	 , 0, 420}
	,
	{0, NULL, NULL, {NULL, 0}
	 , {NULL, 0}
	 , 0}
};

const 
static _GNUTLS_DH_PARAMS _gnutls_dh_copy_params[] = {
	{768, NULL, NULL, {DH_G_1024, sizeof(DH_G_1024)}
	 , {diffie_hellman_prime_1024, sizeof diffie_hellman_prime_1024}
	 , 0, 180}
	,
	{1024, NULL, NULL, {DH_G_1024, sizeof(DH_G_1024)}
	 , {diffie_hellman_prime_1024, sizeof diffie_hellman_prime_1024}
	 , 0, 180}
	,
	{2048, NULL, NULL, {DH_G_2048, sizeof(DH_G_2048)}
	 , {diffie_hellman_prime_2048, sizeof diffie_hellman_prime_2048}
	 , 0, 260}
	,
	{3072, NULL, NULL, {DH_G_3072, sizeof(DH_G_3072)}
	 , {diffie_hellman_prime_3072, sizeof diffie_hellman_prime_3072}
	 , 0, 340}
	,
	{4096, NULL, NULL, {DH_G_4096, sizeof(DH_G_4096)}
	 , {diffie_hellman_prime_4096, sizeof diffie_hellman_prime_4096}
	 , 0, 420}
	,
	{0, NULL, NULL, {NULL, 0}
	 , {NULL, 0}
	 , 0}
};

/* This function takes a number of bits and returns a supported
 * number of bits. Ie a number of bits that we have a prime in the
 * dh_primes structure.
 */
static int supported_bits[] = { 768, 1024, 2048, 3072, 4096, 0 };
static int normalize_bits(int bits)
{
	if (bits >= 4096)
		bits = 4096;
	else if (bits <= 768)
		bits = 768;
	else if (bits <= 1024)
		bits = 1024;
	else if (bits <= 2048)
		bits = 2048;
	else if (bits <= 3072)
		bits = 3072;
	else if (bits <= 4096)
		bits = 4096;

	return bits;
}

/* Clears allocated GNUTLS_MPIs and data. Only to be called at exit.
 */
void _gnutls_dh_clear_mpis(void) {
int i;

	if (_gnutls_dh_default_params==NULL) return;

	i = 0;
	do {
		_gnutls_mpi_release( &_gnutls_dh_default_params[i]._prime);
		_gnutls_mpi_release( &_gnutls_dh_default_params[i]._generator);
		if (_gnutls_dh_default_params[i].local != 0) {
			gnutls_free( _gnutls_dh_default_params[i].prime.data);
			gnutls_free( _gnutls_dh_default_params[i].generator.data);
		}
		i++;
	} while (_gnutls_dh_default_params[i].bits != 0);

}

/* Generates GNUTLS_MPIs from opaque integer data. Initializes the dh_primes to
 * be used.
 */
int _gnutls_dh_calc_mpis(void)
{
int i, n;

	if (_gnutls_dh_default_params==NULL) {
		gnutls_assert();
		return GNUTLS_E_INVALID_REQUEST;
	}

	i = 0;
	do {
 		n = _gnutls_dh_default_params[i].prime.size;
		_gnutls_mpi_release( &_gnutls_dh_default_params[i]._prime);

		if (_gnutls_mpi_scan(&_gnutls_dh_default_params[i]._prime, _gnutls_dh_default_params[i].prime.data, &n)
		    || _gnutls_dh_default_params[i]._prime == NULL) {
			gnutls_assert();
			return GNUTLS_E_MPI_SCAN_FAILED;
		}


		n = _gnutls_dh_default_params[i].generator.size;
		_gnutls_mpi_release( &_gnutls_dh_default_params[i]._generator);

		if (_gnutls_mpi_scan(&_gnutls_dh_default_params[i]._generator, _gnutls_dh_default_params[i].generator.data, &n)
		    || _gnutls_dh_default_params[i]._generator == NULL) {
			gnutls_assert();
			return GNUTLS_E_MPI_SCAN_FAILED;
		}

		i++;
	} while (_gnutls_dh_default_params[i].bits != 0);

	return 0;
}

/* returns g and p, depends on the requested bits.
 * We only support limited key sizes.
 */
GNUTLS_MPI gnutls_get_dh_params(GNUTLS_DH_PARAMS dh_primes, GNUTLS_MPI * ret_p, int bits, int * qbits)
{
	GNUTLS_MPI g=NULL, prime=NULL;
	int i;

	if (dh_primes==NULL) {
		gnutls_assert();
		return NULL;
	}

	bits = normalize_bits(bits);

	i = 0;
	do {
		if (dh_primes[i].bits == bits) {
			prime = _gnutls_mpi_copy(dh_primes[i]._prime);
			g = _gnutls_mpi_copy(dh_primes[i]._generator);
			if (qbits)
				*qbits = dh_primes[i].qbits;
			break;
		}
		i++;
	} while (dh_primes[i].bits != 0);

	if (prime==NULL || g==NULL) { /* if not prime was found */
		gnutls_assert();
		_gnutls_mpi_release( &g);
		_gnutls_mpi_release( &prime);
		*ret_p = NULL;
		return NULL;
	}

	if (ret_p)
		*ret_p = prime;
	return g;
}

/* returns g and p, depends on the requested bits.
 * We only support limited key sizes.
 */
GNUTLS_MPI _gnutls_get_rnd_srp_params( GNUTLS_MPI * ret_p, int bits)
{
	GNUTLS_MPI g=NULL, prime=NULL;
	int i;

	if (_gnutls_dh_default_params==NULL) {
		gnutls_assert();
		return NULL;
	}

	bits = normalize_bits(bits);

	i = 0;
	do {
		if (_gnutls_dh_default_params[i].bits == bits) {
			prime = _gnutls_mpi_copy(_gnutls_dh_default_params[i]._prime);
			g = _gnutls_mpi_copy(_gnutls_dh_default_params[i]._generator);
			break;
		}
		i++;
	} while (_gnutls_dh_default_params[i].bits != 0);

	if (prime==NULL || g==NULL) { /* if not prime was found */
		gnutls_assert();
		_gnutls_mpi_release( &g);
		_gnutls_mpi_release( &prime);
		*ret_p = NULL;
		return NULL;
	}

	if (ret_p)
		*ret_p = prime;
	return g;
}

/* These should be added in gcrypt.h */
GNUTLS_MPI _gcry_generate_elg_prime(int mode, unsigned pbits,
			     unsigned qbits, GNUTLS_MPI g, GNUTLS_MPI ** ret_factors);

int _gnutls_dh_generate_prime(GNUTLS_MPI * ret_g, GNUTLS_MPI * ret_n, int bits, int* _qbits)
{

	GNUTLS_MPI g, prime;
	int qbits;

	g = mpi_new(16);	/* this should be ok */
	if (g == NULL) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}

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

	prime = _gcry_generate_elg_prime(0, bits, qbits, g, NULL);
	if (prime == NULL || g == NULL) {
		_gnutls_mpi_release(&g);
		_gnutls_mpi_release(&prime);
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}

	if (ret_g)
		*ret_g = g;
	if (ret_n)
		*ret_n = prime;

	return 0;

}

/* returns a negative value if the bits is not supported 
 */
static int check_bits(int bits) {
int i=0;
	do {
		if (supported_bits[i]==bits) return 0;
		i++;
	} while(supported_bits[i]!=0);

	gnutls_assert();
	return GNUTLS_E_INVALID_PARAMETERS;
}

/* Replaces the prime in the static DH parameters, with a randomly
 * generated one.
 */
/**
  * gnutls_dh_params_set - This function will replace the old DH parameters
  * @dh_params: Is a structure will hold the prime numbers
  * @prime: holds the new prime
  * @generator: holds the new generator
  * @bits: is the prime's number of bits
  * @qbits: is the number of bits of q
  *
  * This function will replace the pair of prime and generator for use in 
  * the Diffie-Hellman key exchange. The new parameters should be stored in the
  * appropriate gnutls_datum. 
  * 
  * Note that the bits value should be one of 768, 1024, 2048, 3072 or 4096.
  *
  **/
int gnutls_dh_params_set( GNUTLS_DH_PARAMS dh_params, gnutls_datum prime, gnutls_datum generator, int bits, int qbits)
{
	GNUTLS_MPI tmp_prime, tmp_g;
	int siz=0, i=0;
	GNUTLS_DH_PARAMS sprime;

	if (check_bits(bits)<0) {
		gnutls_assert();
		return GNUTLS_E_INVALID_PARAMETERS;
	}

	i = 0;
	do {
		if (dh_params[i].bits==bits) {
			sprime = &dh_params[i];
			break;
		}
	} while(dh_params[++i].bits!=0);
		
	siz = prime.size;
	if (_gnutls_mpi_scan(&tmp_prime, prime.data, &siz)) {
		gnutls_assert();
		return GNUTLS_E_MPI_SCAN_FAILED;
	}

	siz = generator.size;
	if (_gnutls_mpi_scan(&tmp_g, generator.data, &siz)) {
		_gnutls_mpi_release( &tmp_prime);
		gnutls_assert();
		return GNUTLS_E_MPI_SCAN_FAILED;
	}

	/* copy the generated values to the structure
	 */
	if (sprime->local != 0) {
		gnutls_free(sprime->prime.data);
		_gnutls_mpi_release(&sprime->_prime);
		gnutls_free(sprime->generator.data);
		_gnutls_mpi_release(&sprime->_generator);
	}
	sprime->local = 1;
	sprime->_prime = tmp_prime;
	sprime->_generator = tmp_g;

	sprime->qbits = qbits;

/*	sprime->_prime = _gnutls_mpi_copy(tmp_prime);
	sprime->_generator = _gnutls_mpi_copy(tmp_g);
*/
	if (gnutls_set_datum( &sprime->prime, prime.data, prime.size) < 0) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}
	if (gnutls_set_datum( &sprime->prime, generator.data, generator.size) < 0) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}

	return 0;

}

/**
  * gnutls_dh_params_init - This function will initialize the DH parameters
  * @dh_params: Is a structure that will hold the prime numbers
  *
  * This function will initialize the DH parameters structure.
  *
  **/
int gnutls_dh_params_init( GNUTLS_DH_PARAMS* dh_params)
{

	(*dh_params) = gnutls_calloc( 1, sizeof( _gnutls_dh_copy_params));
	if (*dh_params==NULL) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}
	
	memcpy( (*dh_params), _gnutls_dh_copy_params, sizeof(_gnutls_dh_copy_params));

	return 0;

}

/**
  * gnutls_dh_params_deinit - This function will initialize the DH parameters
  * @dh_params: Is a structure that will hold the prime numbers
  *
  * This function will initialize the DH parameters structure.
  *
  **/
void gnutls_dh_params_deinit( GNUTLS_DH_PARAMS dh_params)
{
int i;
	if (dh_params==NULL) return;

	i = 0;
	do {
		_gnutls_mpi_release( &dh_params[i]._prime);
		_gnutls_mpi_release( &dh_params[i]._generator);
		if (dh_params[i].local != 0) {
			gnutls_free( dh_params[i].prime.data);
			gnutls_free( dh_params[i].generator.data);
		}
		i++;
	} while (dh_params[i].bits != 0);

	gnutls_free( dh_params);

}

/* Generates a prime number and a generator, and returns 2 gnutls_datums that contain these
 * numbers.
 */
/**
  * gnutls_dh_params_generate - This function will generate new DH parameters
  * @prime: will hold the new prime
  * @generator: will hold the new generator
  * @bits: is the prime's number of bits
  * @qbits: The bits of q
  *
  * This function will generate a new pair of prime and generator for use in 
  * the Diffie-Hellman key exchange. The new parameters will be allocated using
  * malloc and will be stored in the appropriate datum.
  * This function is normally very slow. An other function
  * (gnutls_dh_replace_params()) should be called in order to replace the 
  * included DH primes in the gnutls library.
  * 
  * Note that the bits value should be one of 768, 1024, 2048, 3072 or 4096.
  * Also note that the generation of new DH parameters is only usefull
  * to servers. Clients use the parameters sent by the server, thus it's
  * no use calling this in client side.
  *
  **/
int gnutls_dh_params_generate( gnutls_datum* prime, gnutls_datum* generator, int bits, int* qbits)
{

	GNUTLS_MPI tmp_prime, tmp_g;
	int siz;

	if (check_bits(bits) < 0) {
		gnutls_assert();
		return GNUTLS_E_INVALID_PARAMETERS;
	}

	if (_gnutls_dh_generate_prime(&tmp_g, &tmp_prime, bits, qbits) < 0) {
		gnutls_assert();
		return GNUTLS_E_MEMORY_ERROR;
	}

	siz = 0;
	_gnutls_mpi_print( NULL, &siz, tmp_g);

	generator->data = malloc(siz);
	if (generator->data == NULL) {
		_gnutls_mpi_release(&tmp_g);
		_gnutls_mpi_release(&tmp_prime);
		return GNUTLS_E_MEMORY_ERROR;
	}

	generator->size = siz;
	_gnutls_mpi_print( generator->data, &siz, tmp_g);


	siz = 0;
	_gnutls_mpi_print( NULL, &siz, tmp_prime);

	prime->data = malloc(siz);
	if (prime->data == NULL) {
		gnutls_free( generator->data);
		_gnutls_mpi_release(&tmp_g);
		_gnutls_mpi_release(&tmp_prime);
		return GNUTLS_E_MEMORY_ERROR;
	}
	prime->size = siz;
	_gnutls_mpi_print( prime->data, &siz, tmp_prime);

	_gnutls_log( "Generated %d bits prime %s, generator %s.\n", 
		bits, _gnutls_bin2hex( prime->data, prime->size),
		_gnutls_bin2hex( generator->data, generator->size));

	return 0;

}