0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-26 15:33:54 +01:00

ircd::nacl: Improve sodium initialization; various cleanup.

This commit is contained in:
Jason Volk 2019-06-13 13:36:17 -06:00
parent 98ec2d7008
commit 9539b21789
3 changed files with 47 additions and 42 deletions

View file

@ -16,13 +16,5 @@ namespace ircd::nacl
{ {
IRCD_EXCEPTION(ircd::error, error) IRCD_EXCEPTION(ircd::error, error)
struct init;
extern const info::versions version_api, version_abi; extern const info::versions version_api, version_abi;
} }
struct ircd::nacl::init
{
init();
~init() noexcept;
};

View file

@ -282,7 +282,6 @@ noexcept try
fs::init _fs_; // Local filesystem fs::init _fs_; // Local filesystem
magic::init _magic_; // libmagic magic::init _magic_; // libmagic
ctx::ole::init _ole_; // Thread OffLoad Engine ctx::ole::init _ole_; // Thread OffLoad Engine
nacl::init _nacl_; // nacl crypto
openssl::init _ossl_; // openssl crypto openssl::init _ossl_; // openssl crypto
net::init _net_; // Networking net::init _net_; // Networking
db::init _db_; // RocksDB db::init _db_; // RocksDB

View file

@ -10,18 +10,16 @@
#include <sodium.h> #include <sodium.h>
/////////////////////////////////////////////////////////////////////////////// namespace ircd::nacl
// {
// Internal struct throw_on_error;
//
struct throw_on_error static void init() __attribute__((constructor));
{
throw_on_error(const int &val)
{
if(unlikely(val != 0))
throw ircd::nacl::error("sodium error");
} }
struct ircd::nacl::throw_on_error
{
throw_on_error(const int &val);
}; };
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -53,21 +51,6 @@ ircd::nacl::version_abi
::sodium_version_string(), ::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 // ircd/buffer.h
@ -113,7 +96,7 @@ ircd::ed25519::sk::sk(pk *const &pk_arg,
reinterpret_cast<const uint8_t *>(data(seed)) reinterpret_cast<const uint8_t *>(data(seed))
}; };
throw_on_error nacl::throw_on_error
{ {
::crypto_sign_ed25519_seed_keypair(pk_data, key.get(), seed_data) ::crypto_sign_ed25519_seed_keypair(pk_data, key.get(), seed_data)
}; };
@ -145,7 +128,7 @@ try
if(!fs::exists(filename) && !ircd::write_avoid) if(!fs::exists(filename) && !ircd::write_avoid)
{ {
throw_on_error nacl::throw_on_error
{ {
::crypto_sign_ed25519_keypair(pk_data, key.get()) ::crypto_sign_ed25519_keypair(pk_data, key.get())
}; };
@ -154,7 +137,7 @@ try
} }
else fs::read(filename, key_data); else fs::read(filename, key_data);
throw_on_error nacl::throw_on_error
{ {
::crypto_sign_ed25519_sk_to_pk(pk_data, key.get()) ::crypto_sign_ed25519_sk_to_pk(pk_data, key.get())
}; };
@ -186,7 +169,7 @@ const
reinterpret_cast<const uint8_t *>(buffer::data(msg)) reinterpret_cast<const uint8_t *>(buffer::data(msg))
}; };
throw_on_error nacl::throw_on_error
{ {
::crypto_sign_ed25519_detached(sig_data, ::crypto_sign_ed25519_detached(sig_data,
&sig_sz, &sig_sz,
@ -229,7 +212,38 @@ const
key_data) key_data)
}; };
return ret == 0? true: if(likely(ret == 0))
ret == -1? false: return true;
throw nacl::error("verify failed: %d", ret);
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"
};
} }