summaryrefslogtreecommitdiff
path: root/lib/gnutls_dh.c
diff options
context:
space:
mode:
authorNikos Mavrogiannopoulos <nmav@gnutls.org>2000-04-10 14:26:28 +0000
committerNikos Mavrogiannopoulos <nmav@gnutls.org>2000-04-10 14:26:28 +0000
commit1f9db0b3c5877b9a3b545be65ae55c6fcd7fd82a (patch)
tree7d5f0b5d5b5fa89e5dcb4b632860f3a635910397 /lib/gnutls_dh.c
parent25f9f4858cc2fe31f36f18a1bfb88513e17e15d4 (diff)
downloadgnutls-1f9db0b3c5877b9a3b545be65ae55c6fcd7fd82a.tar.gz
Added anonymous diffie-hellman key exchange. It does not work yet, and
the whole code is mess.
Diffstat (limited to 'lib/gnutls_dh.c')
-rw-r--r--lib/gnutls_dh.c78
1 files changed, 66 insertions, 12 deletions
diff --git a/lib/gnutls_dh.c b/lib/gnutls_dh.c
index a593b1a669..f7718dff40 100644
--- a/lib/gnutls_dh.c
+++ b/lib/gnutls_dh.c
@@ -1,5 +1,7 @@
#include <defines.h>
-#include <gcrypt.h>
+#include <gnutls_int.h>
+
+/* Taken from gsti */
static const uint8 diffie_hellman_group1_prime[130] = { 0x04, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2,
@@ -15,23 +17,27 @@ static const uint8 diffie_hellman_group1_prime[130] = { 0x04, 0x00,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
#if 0
- --Example--
- you: X = g^x mod p;
+ --Example--
+ you: X = g^x mod p;
peer: Y = g^y mod p;
- g = mpi_set_ui( NULL, 2 );
+ your_key = Y^x mod p;
+ his_key = X^y mod p;
+
/* generate our secret and the public value for it */
- X = calc_dh_secret( &x );
+ X = _gnutls_calc_dh_secret( &x );
/* now we can calculate the shared secret */
- key = calc_dh_key( Y, x );
+ key = _gnutls_calc_dh_key( Y, x);
mpi_release( x );
+ mpi_release( g );
#endif
/****************
* Choose a random value x and calculate e = g^x mod p.
* Return: e and if ret_x is not NULL x.
+ * It also returns g and p.
*/
-MPI calc_dh_secret( MPI *ret_x )
+MPI _gnutls_calc_dh_secret( MPI *ret_x )
{
MPI e, g, x, prime;
size_t n = sizeof diffie_hellman_group1_prime;
@@ -53,24 +59,72 @@ MPI calc_dh_secret( MPI *ret_x )
*ret_x = x;
else
mpi_release(x);
- mpi_release(g);
- mpi_release(prime);
+ mpi_release(g);
+ mpi_release(prime);
return e;
}
+MPI __gnutls_calc_dh_secret( MPI *ret_x, MPI g, MPI prime )
+{
+ MPI e, x;
+
+ x = mpi_new( 200 ); /* FIXME: allocate in secure memory */
+ gcry_mpi_randomize( x, 200, GCRY_STRONG_RANDOM );
+ /* fixme: set high bit of x and select a larger one */
+
+ e = mpi_new(1024);
+ mpi_powm( e, g, x, prime );
+
+ if( ret_x )
+ *ret_x = x;
+ else
+ mpi_release(x);
+ return e;
+}
-MPI calc_dh_key( MPI f, MPI x )
+/* returns g and p */
+MPI _gnutls_get_dh_params( MPI *ret_p )
{
- MPI k, prime;
+ MPI g, prime;
size_t n = sizeof diffie_hellman_group1_prime;
- if( gcry_mpi_scan( &prime, GCRYMPI_FMT_STD,
+ if( gcry_mpi_scan( &prime, GCRYMPI_FMT_STD,
diffie_hellman_group1_prime, &n ) )
abort();
+ g = mpi_set_ui( NULL, 2 );
+
+ if( ret_p )
+ *ret_p = prime;
+ else
+ mpi_release(prime);
+ return g;
+}
+
+
+MPI _gnutls_calc_dh_key( MPI f, MPI x )
+{
+ MPI k, prime;
+ size_t n = sizeof diffie_hellman_group1_prime;
+
k = mpi_new( 1024 ); /* FIXME: allocate in secure memory */
+ if( gcry_mpi_scan( &prime, GCRYMPI_FMT_STD,
+ diffie_hellman_group1_prime, &n ) )
+ abort();
+ /*dump_mpi(stderr, "prime=", prime );*/
+
mpi_powm( k, f, x, prime );
mpi_release(prime);
return k;
}
+MPI __gnutls_calc_dh_key( MPI f, MPI x, MPI prime )
+{
+ MPI k;
+
+ k = mpi_new( 1024 ); /* FIXME: allocate in secure memory */
+
+ mpi_powm( k, f, x, prime );
+ return k;
+}
+