From 4a6b3f5fccc75ad63d9ad1ea4c9f6f52a62db7f4 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 30 Sep 2017 19:14:45 -0700 Subject: [PATCH] Add support for libsodium (NaCl) cryptography. --- README.md | 4 ++-- charybdis/Makefile.am | 3 +++ configure.ac | 21 +++++++++++++++++++ include/ircd/buffer.h | 1 + ircd/Makefile.am | 4 ++++ ircd/ircd.cc | 6 ++++-- ircd/sodium.cc | 47 +++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 ircd/sodium.cc diff --git a/README.md b/README.md index 0b374fd0f..4b9519c0b 100644 --- a/README.md +++ b/README.md @@ -37,11 +37,11 @@ developed Boost libraries. These are included as a submodule in this repository. **RocksDB** (based on LevelDB) - We replace sqlite3 with a lightweight and embedded database and have furthered the mission of eliminating the need for external "IRC services" +*Other dependencies:* **sodium** (NaCl crypto), **OpenSSL**, **zlib**, **snappy** (for rocksdb) + *Build dependencies:* **gnu++14 compiler**, **automake**, **autoconf**, **autoconf2.13**, **autoconf-archive**, **libtool**, **shtool** -*Other dependencies:* **OpenSSL**, **zlib**, **snappy** (for rocksdb) - #### Downloading Charybdis diff --git a/charybdis/Makefile.am b/charybdis/Makefile.am index 3957e9967..c898b0d61 100644 --- a/charybdis/Makefile.am +++ b/charybdis/Makefile.am @@ -8,6 +8,7 @@ AM_CPPFLAGS = \ @ROCKSDB_CPPFLAGS@ \ @JS_CPPFLAGS@ \ @BOOST_CPPFLAGS@ \ + @SODIUM_CPPFLAGS@ \ ### AM_LDFLAGS = \ @@ -29,6 +30,7 @@ charybdis_LDFLAGS = \ @ROCKSDB_LDFLAGS@ \ @JS_LDFLAGS@ \ @BOOST_LDFLAGS@ \ + @SODIUM_LDFLAGS@ \ ### charybdis_LDADD = \ @@ -36,6 +38,7 @@ charybdis_LDADD = \ @ROCKSDB_LIBS@ \ @JS_LIBS@ \ @BOOST_LIBS@ \ + @SODIUM_LIBS@ \ -lcrypto \ -lssl \ -lz \ diff --git a/configure.ac b/configure.ac index 69cefde0b..af75cfab0 100644 --- a/configure.ac +++ b/configure.ac @@ -848,6 +848,26 @@ AC_SUBST(GMP_LDFLAGS, []) AC_SUBST(GMP_LIBS, ["-lgmp"]) + +dnl +dnl libsodium (NaCl) crypto support +dnl + +dnl PKG_CHECK_MODULES(SODIUM, [sodium], [have_sodium="yes"], [have_sodium="no"]) +AC_CHECK_LIB([sodium], sodium_init, [have_sodium="yes"], [have_sodium="no"]) +AM_CONDITIONAL([SODIUM], [test "x$have_sodium" = "xyes"]) + +AM_COND_IF([SODIUM], [], +[ + AC_MSG_ERROR([Failed to find libsodium (The NaCl cryptographic library)]) +]) + +AC_SUBST(SODIUM_CPPFLAGS, []) +AC_SUBST(SODIUM_LDFLAGS, []) +AC_SUBST(SODIUM_LIBS, ["-lsodium"]) + + + dnl dnl OpenSSL support dnl @@ -1254,6 +1274,7 @@ echo "Compiler flags (CXXFLAGS) ......... $CXXFLAGS" echo "Building boost .................... $with_included_boost" echo "Building RocksDB................... $with_included_rocksdb" echo "Building JS (SpiderMonkey) ........ $with_included_js" +echo "Sodium support .................... $have_sodium" echo "Precompiled headers ............... $build_pch" echo "Developer debug ................... $debug" echo "GNU MP support .................... $have_gmp" diff --git a/include/ircd/buffer.h b/include/ircd/buffer.h index 1bcd041ff..8e529634f 100644 --- a/include/ircd/buffer.h +++ b/include/ircd/buffer.h @@ -81,6 +81,7 @@ namespace ircd::buffer template it copy(it &dest, const it &stop, const const_raw_buffer &); template size_t copy(const it &dest, const size_t &max, const const_raw_buffer &buffer); size_t copy(const mutable_raw_buffer &dst, const const_raw_buffer &src); + void zero(const mutable_raw_buffer &buf); // Iterable of buffers tools template class I, class T> size_t size(const I &buffers); diff --git a/ircd/Makefile.am b/ircd/Makefile.am index e3dffcc9e..b340d7a7f 100644 --- a/ircd/Makefile.am +++ b/ircd/Makefile.am @@ -11,6 +11,7 @@ AM_CPPFLAGS = \ @ROCKSDB_CPPFLAGS@ \ @JS_CPPFLAGS@ \ @BOOST_CPPFLAGS@ \ + @SODIUM_CPPFLAGS@ \ -include ircd/ircd.h \ ### @@ -42,12 +43,14 @@ libircd_la_LDFLAGS = \ @ROCKSDB_LDFLAGS@ \ @JS_LDFLAGS@ \ @BOOST_LDFLAGS@ \ + @SODIUM_LDFLAGS@ \ ### libircd_la_LIBADD = \ @ROCKSDB_LIBS@ \ @JS_LIBS@ \ @BOOST_LIBS@ \ + @SODIUM_LIBS@ \ -lcrypto \ -lssl \ -lz \ @@ -75,6 +78,7 @@ libircd_la_SOURCES = \ parse.cc \ resource.cc \ rfc1459.cc \ + sodium.cc \ ### if JS diff --git a/ircd/ircd.cc b/ircd/ircd.cc index 87e11cc5b..670f357cf 100644 --- a/ircd/ircd.cc +++ b/ircd/ircd.cc @@ -83,12 +83,13 @@ try ircd::main_context = main_context.detach(); ircd::runlevel_changed = std::move(runlevel_changed); - log::info("%s. boost %u.%u.%u. rocksdb %s.", + log::info("%s. boost %u.%u.%u. rocksdb %s. sodium %s.", PACKAGE_STRING, boost_version[0], boost_version[1], boost_version[2], - db::version); + db::version, + nacl::version()); log::info("%s %ld %s. configured: %s. compiled: %s %s", BRANDING_VERSION, @@ -170,6 +171,7 @@ try // more appropriate. ctx::ole::init _ole_; // Thread OffLoad Engine + nacl::init _nacl_; // nacl crypto net::init _net_; // Networking client::init _client_; // Client related db::init _db_; // RocksDB diff --git a/ircd/sodium.cc b/ircd/sodium.cc new file mode 100644 index 000000000..b13b4c70f --- /dev/null +++ b/ircd/sodium.cc @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2017 Charybdis Development Team + * Copyright (C) 2017 Jason Volk + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + */ + +#include + +/////////////////////////////////////////////////////////////////////////////// +// +// Internal +// + +__attribute__((constructor)) +static void +ircd_init() +{ + if(sodium_init() < 0) + throw std::runtime_error("sodium_init(): error"); +} + +/////////////////////////////////////////////////////////////////////////////// +// +// ircd/buffer.h +// + +void +ircd::buffer::zero(const mutable_raw_buffer &buf) +{ + sodium_memzero(data(buf), size(buf)); +}