From 9ae9ea7357ae3369cf48a872c41e5055a40b5d2f Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 29 Sep 2017 16:35:55 -0700 Subject: [PATCH] ircd: Reorg crypto related by dependency. --- ircd/Makefile.am | 2 +- ircd/crypto.cc | 173 +++++++++++++++++++++++------------------------ ircd/openssl.cc | 129 +++++++++++++++++++++++++++++++++++ ircd/rand.cc | 106 ----------------------------- 4 files changed, 213 insertions(+), 197 deletions(-) create mode 100644 ircd/openssl.cc delete mode 100644 ircd/rand.cc diff --git a/ircd/Makefile.am b/ircd/Makefile.am index c207deb8d..45dc2f827 100644 --- a/ircd/Makefile.am +++ b/ircd/Makefile.am @@ -71,8 +71,8 @@ libircd_la_SOURCES = \ logger.cc \ matrix.cc \ mods.cc \ + openssl.cc \ parse.cc \ - rand.cc \ resource.cc \ rfc1459.cc \ socket.cc \ diff --git a/ircd/crypto.cc b/ircd/crypto.cc index 9a827f9e9..56ef3cbae 100644 --- a/ircd/crypto.cc +++ b/ircd/crypto.cc @@ -19,25 +19,11 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include +/////////////////////////////////////////////////////////////////////////////// +// +// hash.h +// -namespace ircd::crh -{ - struct throw_on_error; - - static void finalize(struct sha256::ctx *const &, const mutable_raw_buffer &); -} - -struct ircd::crh::throw_on_error -{ - throw_on_error(const int &openssl_return_value) - { - if(unlikely(openssl_return_value != 1)) - throw error("OpenSSL error code: %d", openssl_return_value); - } -}; - -/// vtable ircd::crh::hash::~hash() noexcept { @@ -65,85 +51,92 @@ const extract(buf); } -struct ircd::crh::sha256::ctx -:SHA256_CTX +/////////////////////////////////////////////////////////////////////////////// +// +// rand.h +// + +decltype(ircd::rand::device) ircd::rand::device { - ctx(); - ~ctx() noexcept; + // on linux: uses RDRND or /dev/urandom + // on windows: TODO: verify construction source }; -ircd::crh::sha256::ctx::ctx() +decltype(ircd::rand::mt) ircd::rand::mt { - throw_on_error + device() +}; + +decltype(ircd::rand::dict::alnum) ircd::rand::dict::alnum +{ + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +}; + +decltype(ircd::rand::dict::alpha) ircd::rand::dict::alpha +{ + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" +}; + +decltype(ircd::rand::dict::upper) ircd::rand::dict::upper +{ + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +}; + +decltype(ircd::rand::dict::lower) ircd::rand::dict::lower +{ + "abcdefghijklmnopqrstuvwxyz" +}; + +decltype(ircd::rand::dict::numeric) ircd::rand::dict::numeric +{ + "0123456789" +}; + +std::string +ircd::rand::string(const std::string &dict, + const size_t &len) +{ + std::string ret(len, char()); + string(dict, len, reinterpret_cast(&ret.front())); + return ret; +} + +ircd::string_view +ircd::rand::string(const std::string &dict, + const size_t &len, + char *const &buf, + const size_t &max) +{ + if(unlikely(!max)) + return { buf, max }; + + const auto size { - SHA256_Init(this) - }; -} - -ircd::crh::sha256::ctx::~ctx() -noexcept -{ -} - -ircd::crh::sha256::sha256() -:ctx{std::make_unique()} -{ -} - -/// One-shot functor. Immediately calls operator(). -ircd::crh::sha256::sha256(const mutable_raw_buffer &out, - const const_buffer &in) -:sha256{} -{ - operator()(out, in); -} - -ircd::crh::sha256::~sha256() -noexcept -{ -} - -void -ircd::crh::sha256::update(const const_buffer &buf) -{ - throw_on_error - { - SHA256_Update(ctx.get(), data(buf), size(buf)) - }; -} - -void -ircd::crh::sha256::extract(const mutable_raw_buffer &buf) -const -{ - auto copy(*ctx); - crh::finalize(©, buf); -} - -void -ircd::crh::sha256::finalize(const mutable_raw_buffer &buf) -{ - crh::finalize(ctx.get(), buf); -} - -size_t -ircd::crh::sha256::length() -const -{ - return bytes; -} - -void -ircd::crh::finalize(struct sha256::ctx *const &ctx, - const mutable_raw_buffer &buf) -{ - uint8_t *const md - { - reinterpret_cast(data(buf)) + std::min(len, max - 1) }; - throw_on_error + buf[size] = '\0'; + return string(dict, size, reinterpret_cast(buf)); +} + +ircd::string_view +ircd::rand::string(const std::string &dict, + const size_t &len, + uint8_t *const &buf) +{ + std::uniform_int_distribution dist { - SHA256_Final(md, ctx) + 0, dict.size() - 1 + }; + + std::generate(buf, buf + len, [&dict, &dist] + () -> uint8_t + { + return dict.at(dist(mt)); + }); + + return string_view + { + reinterpret_cast(buf), len }; } diff --git a/ircd/openssl.cc b/ircd/openssl.cc new file mode 100644 index 000000000..94d8b34fc --- /dev/null +++ b/ircd/openssl.cc @@ -0,0 +1,129 @@ +/* + * 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 +#include + +/////////////////////////////////////////////////////////////////////////////// +// +// hash.h +// + +namespace ircd::crh +{ + static void finalize(struct sha256::ctx *const &, const mutable_raw_buffer &); +} + +template +int +call_openssl(function&& f, args&&... a) +{ + const int ret + { + f(std::forward(a)...) + }; + + if(unlikely(ret == ERR_CODE)) + { + const unsigned long code{ERR_get_error()}; + const auto &msg{ERR_reason_error_string(code)?: "UNKNOWN ERROR"}; + throw exception("OpenSSL #%lu: %s", code, msg); + } + + return ret; +}; + +struct ircd::crh::sha256::ctx +:SHA256_CTX +{ + ctx(); + ~ctx() noexcept; +}; + +ircd::crh::sha256::ctx::ctx() +{ + call_openssl(::SHA256_Init, this); +} + +ircd::crh::sha256::ctx::~ctx() +noexcept +{ +} + +ircd::crh::sha256::sha256() +:ctx{std::make_unique()} +{ +} + +/// One-shot functor. Immediately calls operator(). +ircd::crh::sha256::sha256(const mutable_raw_buffer &out, + const const_buffer &in) +:sha256{} +{ + operator()(out, in); +} + +ircd::crh::sha256::~sha256() +noexcept +{ +} + +void +ircd::crh::sha256::update(const const_buffer &buf) +{ + call_openssl(::SHA256_Update, ctx.get(), data(buf), size(buf)); +} + +void +ircd::crh::sha256::extract(const mutable_raw_buffer &buf) +const +{ + auto copy(*ctx); + crh::finalize(©, buf); +} + +void +ircd::crh::sha256::finalize(const mutable_raw_buffer &buf) +{ + crh::finalize(ctx.get(), buf); +} + +size_t +ircd::crh::sha256::length() +const +{ + return bytes; +} + +void +ircd::crh::finalize(struct sha256::ctx *const &ctx, + const mutable_raw_buffer &buf) +{ + uint8_t *const md + { + reinterpret_cast(data(buf)) + }; + + call_openssl(::SHA256_Final, md, ctx); +} diff --git a/ircd/rand.cc b/ircd/rand.cc deleted file mode 100644 index 0076968f4..000000000 --- a/ircd/rand.cc +++ /dev/null @@ -1,106 +0,0 @@ -/* - * 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. - * - */ - -decltype(ircd::rand::device) ircd::rand::device -{ - // on linux: uses RDRND or /dev/urandom - // on windows: TODO: verify construction source -}; - -decltype(ircd::rand::mt) ircd::rand::mt -{ - device() -}; - -decltype(ircd::rand::dict::alnum) ircd::rand::dict::alnum -{ - "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" -}; - -decltype(ircd::rand::dict::alpha) ircd::rand::dict::alpha -{ - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" -}; - -decltype(ircd::rand::dict::upper) ircd::rand::dict::upper -{ - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" -}; - -decltype(ircd::rand::dict::lower) ircd::rand::dict::lower -{ - "abcdefghijklmnopqrstuvwxyz" -}; - -decltype(ircd::rand::dict::numeric) ircd::rand::dict::numeric -{ - "0123456789" -}; - -std::string -ircd::rand::string(const std::string &dict, - const size_t &len) -{ - std::string ret(len, char()); - string(dict, len, reinterpret_cast(&ret.front())); - return ret; -} - -ircd::string_view -ircd::rand::string(const std::string &dict, - const size_t &len, - char *const &buf, - const size_t &max) -{ - if(unlikely(!max)) - return { buf, max }; - - const auto size - { - std::min(len, max - 1) - }; - - buf[size] = '\0'; - return string(dict, size, reinterpret_cast(buf)); -} - -ircd::string_view -ircd::rand::string(const std::string &dict, - const size_t &len, - uint8_t *const &buf) -{ - std::uniform_int_distribution dist - { - 0, dict.size() - 1 - }; - - std::generate(buf, buf + len, [&dict, &dist] - () -> uint8_t - { - return dict.at(dist(mt)); - }); - - return string_view - { - reinterpret_cast(buf), len - }; -}