From 9539b217899b66c1e614ea89b8e8920e8f38c497 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Thu, 13 Jun 2019 13:36:17 -0600 Subject: [PATCH] ircd::nacl: Improve sodium initialization; various cleanup. --- include/ircd/nacl.h | 8 ----- ircd/ircd.cc | 1 - ircd/sodium.cc | 80 ++++++++++++++++++++++++++------------------- 3 files changed, 47 insertions(+), 42 deletions(-) diff --git a/include/ircd/nacl.h b/include/ircd/nacl.h index deb274ebe..a5fac3a17 100644 --- a/include/ircd/nacl.h +++ b/include/ircd/nacl.h @@ -16,13 +16,5 @@ namespace ircd::nacl { IRCD_EXCEPTION(ircd::error, error) - struct init; - extern const info::versions version_api, version_abi; } - -struct ircd::nacl::init -{ - init(); - ~init() noexcept; -}; diff --git a/ircd/ircd.cc b/ircd/ircd.cc index 1c461bf16..ea25adc38 100644 --- a/ircd/ircd.cc +++ b/ircd/ircd.cc @@ -282,7 +282,6 @@ noexcept try fs::init _fs_; // Local filesystem magic::init _magic_; // libmagic ctx::ole::init _ole_; // Thread OffLoad Engine - nacl::init _nacl_; // nacl crypto openssl::init _ossl_; // openssl crypto net::init _net_; // Networking db::init _db_; // RocksDB diff --git a/ircd/sodium.cc b/ircd/sodium.cc index b4f7aee53..e1f3be9a0 100644 --- a/ircd/sodium.cc +++ b/ircd/sodium.cc @@ -10,18 +10,16 @@ #include -/////////////////////////////////////////////////////////////////////////////// -// -// Internal -// - -struct throw_on_error +namespace ircd::nacl { - throw_on_error(const int &val) - { - if(unlikely(val != 0)) - throw ircd::nacl::error("sodium error"); - } + struct throw_on_error; + + static void init() __attribute__((constructor)); +} + +struct ircd::nacl::throw_on_error +{ + throw_on_error(const int &val); }; /////////////////////////////////////////////////////////////////////////////// @@ -53,21 +51,6 @@ ircd::nacl::version_abi ::sodium_version_string(), }; -// -// init -// - -ircd::nacl::init::init() -{ - if(::sodium_init() < 0) - throw std::runtime_error("sodium_init(): error"); -} - -ircd::nacl::init::~init() -noexcept -{ -} - /////////////////////////////////////////////////////////////////////////////// // // ircd/buffer.h @@ -113,7 +96,7 @@ ircd::ed25519::sk::sk(pk *const &pk_arg, reinterpret_cast(data(seed)) }; - throw_on_error + nacl::throw_on_error { ::crypto_sign_ed25519_seed_keypair(pk_data, key.get(), seed_data) }; @@ -145,7 +128,7 @@ try if(!fs::exists(filename) && !ircd::write_avoid) { - throw_on_error + nacl::throw_on_error { ::crypto_sign_ed25519_keypair(pk_data, key.get()) }; @@ -154,7 +137,7 @@ try } else fs::read(filename, key_data); - throw_on_error + nacl::throw_on_error { ::crypto_sign_ed25519_sk_to_pk(pk_data, key.get()) }; @@ -186,7 +169,7 @@ const reinterpret_cast(buffer::data(msg)) }; - throw_on_error + nacl::throw_on_error { ::crypto_sign_ed25519_detached(sig_data, &sig_sz, @@ -229,7 +212,38 @@ const key_data) }; - return ret == 0? true: - ret == -1? false: - throw nacl::error("verify failed: %d", ret); + if(likely(ret == 0)) + return true; + + if(likely(ret == -1)) + return false; + + throw nacl::error + { + "verify failed: %d", ret + }; +} + +/////////////////////////////////////////////////////////////////////////////// +// +// Internal +// + +void +ircd::nacl::init() +{ + if(::sodium_init() < 0) + throw std::runtime_error + { + "sodium_init(): error" + }; +} + +ircd::nacl::throw_on_error::throw_on_error(const int &val) +{ + if(unlikely(val != 0)) + throw ircd::nacl::error + { + "sodium error" + }; }