]> git.meshlink.io Git - meshlink/commitdiff
Preliminary implementation of Elliptic Curve Diffie-Hellman Ephemeral key exchange.
authorGuus Sliepen <guus@tinc-vpn.org>
Mon, 27 Jun 2011 19:52:23 +0000 (21:52 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Mon, 27 Jun 2011 19:52:23 +0000 (21:52 +0200)
src/Makefile.am
src/openssl/ecdh.c [new file with mode: 0644]
src/openssl/ecdh.h [new file with mode: 0644]

index 23bbfbcd2e7a1d2c6009c56002e8666d76693269..f78769073ddc9cc0bf163f688853f544e35ed5b1 100644 (file)
@@ -11,7 +11,7 @@ tincd_SOURCES = \
        protocol_key.c protocol_subnet.c route.c subnet.c tincd.c
 
 nodist_tincd_SOURCES = \
-       device.c cipher.c crypto.c digest.c rsa.c                    
+       device.c cipher.c crypto.c ecdh.c digest.c rsa.c                     
 
 tincctl_SOURCES = \
        utils.c getopt.c getopt1.c dropin.c \
@@ -32,7 +32,7 @@ INCLUDES = @INCLUDES@ -I$(top_builddir)
 
 noinst_HEADERS = \
        xalloc.h utils.h getopt.h list.h splay_tree.h dropin.h fake-getaddrinfo.h fake-getnameinfo.h fake-gai-errnos.h ipv6.h ipv4.h ethernet.h \
-       buffer.h cipher.h conf.h connection.h control.h control_common.h crypto.h device.h digest.h edge.h graph.h logger.h meta.h net.h netutl.h node.h process.h \
+       buffer.h cipher.h conf.h connection.h control.h control_common.h crypto.h device.h ecdh.h digest.h edge.h graph.h logger.h meta.h net.h netutl.h node.h process.h \
        protocol.h route.h rsa.h rsagen.h subnet.h tincctl.h top.h bsd/tunemu.h
 
 LIBS = @LIBS@ @LIBGCRYPT_LIBS@
diff --git a/src/openssl/ecdh.c b/src/openssl/ecdh.c
new file mode 100644 (file)
index 0000000..642b3c3
--- /dev/null
@@ -0,0 +1,87 @@
+/*
+    ecdh.c -- Diffie-Hellman key exchange handling
+    Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
+
+    This program 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.
+
+    This program 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.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "system.h"
+#include "utils.h"
+#include "xalloc.h"
+
+#include <openssl/err.h>
+#include <openssl/ec.h>
+#include <openssl/obj_mac.h>
+
+#include "ecdh.h"
+#include "logger.h"
+
+EC_GROUP *secp256k1 = NULL;
+EC_GROUP *secp384r1 = NULL;
+EC_GROUP *secp521r1 = NULL;
+
+// TODO: proper KDF
+static void *kdf(const void *in, size_t inlen, void *out, size_t *outlen) {
+       memcpy(out, in, inlen);
+       *outlen = inlen;
+       return out;
+}
+
+bool ecdh_generate_public(ecdh_t *ecdh, void *pubkey) {
+       if(!secp521r1)
+               secp521r1 = EC_GROUP_new_by_curve_name(NID_secp521r1);
+
+       *ecdh = EC_KEY_new_by_curve_name(NID_secp521r1);
+       if(!EC_KEY_generate_key(*ecdh)) {
+               logger(LOG_ERR, "Generating EC key failed: %s", ERR_error_string(ERR_get_error(), NULL));
+               abort();
+       }
+       
+       const EC_POINT *point = EC_KEY_get0_public_key(*ecdh);
+       if(!point) {
+               logger(LOG_ERR, "Getting public key failed: %s", ERR_error_string(ERR_get_error(), NULL));
+               abort();
+       }
+
+       size_t result = EC_POINT_point2oct(secp521r1, point, POINT_CONVERSION_COMPRESSED, pubkey, ECDH_SIZE, NULL);
+       if(!result) {
+               logger(LOG_ERR, "Converting EC_POINT to binary failed: %s", ERR_error_string(ERR_get_error(), NULL));
+               abort();
+       }
+
+       return true;
+}
+
+bool ecdh_compute_shared(ecdh_t *ecdh, const void *pubkey, void *shared) {
+       EC_POINT *point = EC_POINT_new(secp521r1);
+
+       int result = EC_POINT_oct2point(secp521r1, point, pubkey, ECDH_SIZE, NULL);
+       if(!point) {
+               logger(LOG_ERR, "Converting binary to EC_POINT failed: %s", ERR_error_string(ERR_get_error(), NULL));
+               abort();
+       }
+
+       result = ECDH_compute_key(shared, ECDH_SIZE, point, *ecdh, kdf);
+       EC_POINT_free(point);
+       EC_KEY_free(*ecdh);
+       *ecdh = NULL;
+
+       if(!result) {
+               logger(LOG_ERR, "Computing Elliptic Curve Diffie-Hellman shared key failed: %s", ERR_error_string(ERR_get_error(), NULL));
+               return false;
+       }
+
+       return true;
+}
diff --git a/src/openssl/ecdh.h b/src/openssl/ecdh.h
new file mode 100644 (file)
index 0000000..c5b43d8
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+    ecdh.h -- header file for ecdh.c
+    Copyright (C) 2011 Guus Sliepen <guus@tinc-vpn.org>
+
+    This program 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.
+
+    This program 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.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#ifndef __TINC_ECDH_H__
+#define __TINC_ECDH_H__
+
+#include <openssl/ecdh.h>
+
+#define ECDH_SIZE 67
+
+typedef EC_KEY *ecdh_t;
+
+extern bool ecdh_generate_public(ecdh_t *ecdh, void *pubkey);
+extern bool ecdh_compute_shared(ecdh_t *ecdh, const void *pubkey, void *shared);
+
+#endif