botan: Add support for X25519
authorTobias Brunner <tobias@strongswan.org>
Tue, 11 Sep 2018 09:05:21 +0000 (11:05 +0200)
committerTobias Brunner <tobias@strongswan.org>
Wed, 12 Sep 2018 14:25:00 +0000 (16:25 +0200)
src/libstrongswan/plugins/botan/Makefile.am
src/libstrongswan/plugins/botan/botan_plugin.c
src/libstrongswan/plugins/botan/botan_x25519.c [new file with mode: 0644]
src/libstrongswan/plugins/botan/botan_x25519.h [new file with mode: 0644]

index 1188841..c116014 100644 (file)
@@ -25,7 +25,8 @@ libstrongswan_botan_la_SOURCES = \
        botan_ec_private_key.h botan_ec_private_key.c \
        botan_util.h botan_util.c \
        botan_util_keys.h botan_util_keys.c \
-       botan_gcm.h botan_gcm.c
+       botan_gcm.h botan_gcm.c \
+       botan_x25519.h botan_x25519.c
 
 libstrongswan_botan_la_LDFLAGS = -module -avoid-version
 libstrongswan_botan_la_LIBADD = $(botan_LIBS)
index b387a2b..fd8e5f5 100644 (file)
@@ -38,6 +38,7 @@
 #include "botan_ec_private_key.h"
 #include "botan_gcm.h"
 #include "botan_util_keys.h"
+#include "botan_x25519.h"
 
 #include <library.h>
 
@@ -94,6 +95,11 @@ METHOD(plugin_t, get_features, int,
                        PLUGIN_PROVIDE(DH, ECP_384_BP),
                        PLUGIN_PROVIDE(DH, ECP_512_BP),
 #endif
+#ifdef BOTAN_HAS_X25519
+               PLUGIN_REGISTER(DH, botan_x25519_create),
+                       PLUGIN_PROVIDE(DH, CURVE_25519),
+#endif
+
                /* crypters */
                PLUGIN_REGISTER(CRYPTER, botan_crypter_create),
 #ifdef BOTAN_HAS_AES
diff --git a/src/libstrongswan/plugins/botan/botan_x25519.c b/src/libstrongswan/plugins/botan/botan_x25519.c
new file mode 100644 (file)
index 0000000..519f29f
--- /dev/null
@@ -0,0 +1,176 @@
+/*
+ * Copyright (C) 2018 Tobias Brunner
+ * HSR Hochschule fuer Technik Rapperswil
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "botan_x25519.h"
+
+#include <botan/build.h>
+
+#ifdef BOTAN_HAS_X25519
+
+#include "botan_util.h"
+
+#include <utils/debug.h>
+
+#include <botan/ffi.h>
+
+typedef struct private_diffie_hellman_t private_diffie_hellman_t;
+
+/**
+ * Private data
+ */
+struct private_diffie_hellman_t {
+
+       /**
+        * Public interface
+        */
+       diffie_hellman_t public;
+
+       /**
+        * Private key
+        */
+       botan_privkey_t key;
+
+       /**
+        * Shared secret
+        */
+       chunk_t shared_secret;
+};
+
+METHOD(diffie_hellman_t, set_other_public_value, bool,
+       private_diffie_hellman_t *this, chunk_t value)
+{
+       if (!diffie_hellman_verify_value(CURVE_25519, value))
+       {
+               return FALSE;
+       }
+
+       chunk_clear(&this->shared_secret);
+
+       return botan_dh_key_derivation(this->key, value, &this->shared_secret);
+}
+
+METHOD(diffie_hellman_t, get_my_public_value, bool,
+       private_diffie_hellman_t *this, chunk_t *value)
+{
+       value->len = 0;
+       if (botan_pk_op_key_agreement_export_public(this->key, NULL, &value->len)
+               != BOTAN_FFI_ERROR_INSUFFICIENT_BUFFER_SPACE)
+       {
+               return FALSE;
+       }
+
+       *value = chunk_alloc(value->len);
+       if (botan_pk_op_key_agreement_export_public(this->key, value->ptr,
+                                                                                               &value->len))
+       {
+               chunk_free(value);
+               return FALSE;
+       }
+       return TRUE;
+}
+
+METHOD(diffie_hellman_t, set_private_value, bool,
+       private_diffie_hellman_t *this, chunk_t value)
+{
+       if (value.len != 32)
+       {
+               return FALSE;
+       }
+
+       chunk_clear(&this->shared_secret);
+
+       if (botan_privkey_destroy(this->key))
+       {
+               return FALSE;
+       }
+
+       if (botan_privkey_load_x25519(&this->key, value.ptr))
+       {
+               return FALSE;
+       }
+       return TRUE;
+}
+
+METHOD(diffie_hellman_t, get_shared_secret, bool,
+       private_diffie_hellman_t *this, chunk_t *secret)
+{
+       if (!this->shared_secret.len)
+       {
+               return FALSE;
+       }
+       *secret = chunk_clone(this->shared_secret);
+       return TRUE;
+}
+
+METHOD(diffie_hellman_t, get_dh_group, diffie_hellman_group_t,
+       private_diffie_hellman_t *this)
+{
+       return CURVE_25519;
+}
+
+METHOD(diffie_hellman_t, destroy, void,
+       private_diffie_hellman_t *this)
+{
+       botan_privkey_destroy(this->key);
+       chunk_clear(&this->shared_secret);
+       free(this);
+}
+
+/*
+ * Described in header
+ */
+diffie_hellman_t *botan_x25519_create(diffie_hellman_group_t group)
+{
+       private_diffie_hellman_t *this;
+       botan_rng_t rng;
+
+       INIT(this,
+               .public = {
+                       .get_shared_secret = _get_shared_secret,
+                       .set_other_public_value = _set_other_public_value,
+                       .get_my_public_value = _get_my_public_value,
+                       .set_private_value = _set_private_value,
+                       .get_dh_group = _get_dh_group,
+                       .destroy = _destroy,
+               },
+       );
+
+       if (botan_rng_init(&rng, "user"))
+       {
+               free(this);
+               return NULL;
+       }
+
+       if (botan_privkey_create_ecdh(&this->key, rng, "curve25519"))
+       {
+               DBG1(DBG_LIB, "x25519 private key generation failed");
+               botan_rng_destroy(rng);
+               free(this);
+               return NULL;
+       }
+
+       botan_rng_destroy(rng);
+       return &this->public;
+}
+
+#endif
diff --git a/src/libstrongswan/plugins/botan/botan_x25519.h b/src/libstrongswan/plugins/botan/botan_x25519.h
new file mode 100644 (file)
index 0000000..e95d6cd
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2018 Tobias Brunner
+ * HSR Hochschule fuer Technik Rapperswil
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/**
+ * @defgroup botan_x25519 botan_x25519
+ * @{ @ingroup botan_p
+ */
+
+#ifndef BOTAN_X25519_H_
+#define BOTAN_X25519_H_
+
+#include <library.h>
+
+/**
+ * Creates a new X25519 implementation using Botan.
+ *
+ * @param group                DH group, must be CURVE_25519
+ * @return                     object, NULL if not supported
+ */
+diffie_hellman_t *botan_x25519_create(diffie_hellman_group_t group);
+
+#endif /** BOTAN_X25519_H_ @}*/