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

ircd: Reorg crypto related by dependency.

This commit is contained in:
Jason Volk 2017-09-29 16:35:55 -07:00
parent fae5984d5e
commit 9ae9ea7357
4 changed files with 213 additions and 197 deletions

View file

@ -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 \

View file

@ -19,25 +19,11 @@
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <openssl/sha.h>
///////////////////////////////////////////////////////////////////////////////
//
// 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<uint8_t *>(&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<struct ctx>()}
{
}
/// 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(&copy, 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<uint8_t *>(data(buf))
std::min(len, max - 1)
};
throw_on_error
buf[size] = '\0';
return string(dict, size, reinterpret_cast<uint8_t *>(buf));
}
ircd::string_view
ircd::rand::string(const std::string &dict,
const size_t &len,
uint8_t *const &buf)
{
std::uniform_int_distribution<size_t> 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<const char *>(buf), len
};
}

129
ircd/openssl.cc Normal file
View file

@ -0,0 +1,129 @@
/*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
*
* 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 <openssl/err.h>
#include <openssl/sha.h>
///////////////////////////////////////////////////////////////////////////////
//
// hash.h
//
namespace ircd::crh
{
static void finalize(struct sha256::ctx *const &, const mutable_raw_buffer &);
}
template<class exception = ircd::error,
int ERR_CODE = 0,
class function,
class... args>
int
call_openssl(function&& f, args&&... a)
{
const int ret
{
f(std::forward<args>(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<struct ctx>()}
{
}
/// 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(&copy, 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<uint8_t *>(data(buf))
};
call_openssl(::SHA256_Final, md, ctx);
}

View file

@ -1,106 +0,0 @@
/*
* Copyright (C) 2017 Charybdis Development Team
* Copyright (C) 2017 Jason Volk <jason@zemos.net>
*
* 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<uint8_t *>(&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<uint8_t *>(buf));
}
ircd::string_view
ircd::rand::string(const std::string &dict,
const size_t &len,
uint8_t *const &buf)
{
std::uniform_int_distribution<size_t> dist
{
0, dict.size() - 1
};
std::generate(buf, buf + len, [&dict, &dist]
() -> uint8_t
{
return dict.at(dist(mt));
});
return string_view
{
reinterpret_cast<const char *>(buf), len
};
}