28
28
#include <x509_b64.h> /* for PKCS3 PEM decoding */
29
29
#include <gnutls_global.h>
30
30
#include <gnutls_dh.h>
31
#include <gnutls_pk.h>
32
#include <gnutls/crypto.h>
31
33
#include "x509/x509_int.h"
35
37
/* returns the prime and the generator of DH params.
38
40
_gnutls_dh_params_to_mpi (gnutls_dh_params_t dh_primes)
40
42
if (dh_primes == NULL || dh_primes->params[1] == NULL ||
46
48
return dh_primes->params;
50
_gnutls_dh_generate_prime (mpi_t * ret_g, mpi_t * ret_n, unsigned int bits)
52
mpi_t g = NULL, prime = NULL;
54
int result, times = 0, qbits;
55
mpi_t *factors = NULL;
57
/* Calculate the size of a prime factor of (prime-1)/2.
58
* This is an emulation of the values in "Selecting Cryptographic Key Sizes" paper.
64
qbits = (bits/40) + 105;
67
if (qbits & 1) /* better have an even number */
70
/* find a prime number of size bits.
77
_gnutls_mpi_release (&prime);
78
gcry_prime_release_factors (factors);
81
err = gcry_prime_generate (&prime, bits, qbits,
82
&factors, NULL, NULL, GCRY_STRONG_RANDOM,
83
GCRY_PRIME_FLAG_SPECIAL_FACTOR);
88
result = GNUTLS_E_INTERNAL_ERROR;
92
err = gcry_prime_check (prime, 0);
96
while (err != 0 && times < 10);
101
result = GNUTLS_E_INTERNAL_ERROR;
105
/* generate the group generator.
107
err = gcry_prime_group_generator (&g, prime, factors, NULL);
111
result = GNUTLS_E_INTERNAL_ERROR;
115
gcry_prime_release_factors (factors);
121
_gnutls_mpi_release (&g);
125
_gnutls_mpi_release (&prime);
130
gcry_prime_release_factors (factors);
131
_gnutls_mpi_release (&g);
132
_gnutls_mpi_release (&prime);
138
52
/* Replaces the prime in the static DH parameters, with a randomly
154
68
const gnutls_datum_t * prime,
155
69
const gnutls_datum_t * generator)
157
mpi_t tmp_prime, tmp_g;
71
bigint_t tmp_prime, tmp_g;
160
74
siz = prime->size;
161
if (_gnutls_mpi_scan_nz (&tmp_prime, prime->data, &siz))
75
if (_gnutls_mpi_scan_nz (&tmp_prime, prime->data, siz))
164
78
return GNUTLS_E_MPI_SCAN_FAILED;
167
81
siz = generator->size;
168
if (_gnutls_mpi_scan_nz (&tmp_g, generator->data, &siz))
82
if (_gnutls_mpi_scan_nz (&tmp_g, generator->data, siz))
170
84
_gnutls_mpi_release (&tmp_prime);
268
182
gnutls_dh_params_generate2 (gnutls_dh_params_t params, unsigned int bits)
185
gnutls_group_st group;
272
ret = _gnutls_dh_generate_prime (¶ms->params[1],
273
¶ms->params[0], bits);
187
ret = _gnutls_mpi_generate_group (&group, bits);
276
190
gnutls_assert ();
194
params->params[0] = group.p;
195
params->params[1] = group.g;
421
338
opaque *p_data, *g_data;
422
339
opaque *all_data;
424
_gnutls_mpi_print_lz (NULL, &g_size, params->params[1]);
425
_gnutls_mpi_print_lz (NULL, &p_size, params->params[0]);
341
_gnutls_mpi_print_lz (params->params[1], NULL, &g_size);
342
_gnutls_mpi_print_lz (params->params[0], NULL, &p_size);
427
344
all_data = gnutls_malloc (g_size + p_size);
428
345
if (all_data == NULL)
434
351
p_data = &all_data[0];
435
352
g_data = &all_data[p_size];
437
_gnutls_mpi_print_lz (p_data, &p_size, params->params[0]);
438
_gnutls_mpi_print_lz (g_data, &g_size, params->params[1]);
354
_gnutls_mpi_print_lz (params->params[0], p_data, &p_size);
355
_gnutls_mpi_print_lz (params->params[1], g_data, &g_size);
440
357
/* Ok. Now we have the data. Create the asn1 structures
547
464
return GNUTLS_E_INTERNAL_ERROR;
550
if ((unsigned) result + 1 > *params_data_size)
467
if ((unsigned) result > *params_data_size)
552
469
gnutls_assert ();
553
470
gnutls_free (out);
554
*params_data_size = result + 1;
471
*params_data_size = result;
555
472
return GNUTLS_E_SHORT_MEMORY_BUFFER;
558
*params_data_size = result;
475
*params_data_size = result - 1;
562
memcpy (params_data, out, result);
563
params_data[result] = 0;
478
memcpy (params_data, out, result);
565
480
gnutls_free (out);
595
509
return GNUTLS_E_INVALID_REQUEST;
599
_gnutls_mpi_print (NULL, &size, params->params[1]);
601
generator->data = gnutls_malloc (size);
602
if (generator->data == NULL)
604
return GNUTLS_E_MEMORY_ERROR;
607
generator->size = size;
608
_gnutls_mpi_print (generator->data, &size, params->params[1]);
612
_gnutls_mpi_print (NULL, &size, params->params[0]);
614
prime->data = gnutls_malloc (size);
615
if (prime->data == NULL)
617
gnutls_free (generator->data);
618
generator->data = NULL;
619
return GNUTLS_E_MEMORY_ERROR;
622
_gnutls_mpi_print (prime->data, &size, params->params[0]);
512
ret = _gnutls_mpi_dprint (params->params[1], generator);
519
ret = _gnutls_mpi_dprint (params->params[0], prime);
523
_gnutls_free_datum (generator);
625
528
*bits = _gnutls_mpi_get_nbits (params->params[0]);