2 * Copyright (C) 2000-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #include <gnutls_int.h>
24 #include <gnutls_errors.h>
25 #include <gnutls_dh.h>
33 your_key = Y ^ x mod p;
34 his_key = X ^ y mod p;
36 // generate our secret and the public value (X) for it
37 X = gnutls_calc_dh_secret(&x, g, p);
38 // now we can calculate the shared secret
39 key = gnutls_calc_dh_key(Y, x, g, p);
40 _gnutls_mpi_release(x);
41 _gnutls_mpi_release(g);
44 #define MAX_BITS 18000
46 /* returns the public value (X), and the secret (ret_x).
49 gnutls_calc_dh_secret (bigint_t
* ret_x
, bigint_t g
, bigint_t prime
,
56 x_size
= _gnutls_mpi_get_nbits (prime
) - 1;
60 if (x_size
> MAX_BITS
|| x_size
<= 0)
66 x
= _gnutls_mpi_randomize (NULL
, x_size
, GNUTLS_RND_RANDOM
);
73 e
= _gnutls_mpi_alloc_like (prime
);
80 _gnutls_mpi_release (&x
);
84 _gnutls_mpi_powm (e
, g
, x
, prime
);
89 _gnutls_mpi_release (&x
);
95 gnutls_calc_dh_key (bigint_t f
, bigint_t x
, bigint_t prime
)
100 bits
= _gnutls_mpi_get_nbits (prime
);
101 if (bits
<= 0 || bits
> MAX_BITS
)
107 k
= _gnutls_mpi_alloc_like (prime
);
110 _gnutls_mpi_powm (k
, f
, x
, prime
);
115 * _gnutls_get_dh_params - Returns the DH parameters pointer
116 * @dh_params: is an DH parameters structure, or NULL.
117 * @func: is a callback function to receive the parameters or NULL.
118 * @session: a gnutls session.
120 * This function will return the dh parameters pointer.
123 _gnutls_get_dh_params (gnutls_dh_params_t dh_params
,
124 gnutls_params_function
* func
,
125 gnutls_session_t session
)
127 gnutls_params_st params
;
130 /* if cached return the cached */
131 if (session
->internals
.params
.dh_params
)
132 return session
->internals
.params
.dh_params
;
136 session
->internals
.params
.dh_params
= dh_params
;
140 ret
= func (session
, GNUTLS_PARAMS_DH
, ¶ms
);
141 if (ret
== 0 && params
.type
== GNUTLS_PARAMS_DH
)
143 session
->internals
.params
.dh_params
= params
.params
.dh
;
144 session
->internals
.params
.free_dh_params
= params
.deinit
;
148 return session
->internals
.params
.dh_params
;