0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-11 08:02:59 +01:00
construct/include/ircd/crh.h

200 lines
4.4 KiB
C
Raw Normal View History

2018-02-04 03:22:01 +01:00
// Matrix Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2018 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. The
// full license for this software is available in the LICENSE file.
#pragma once
#define HAVE_IRCD_CRH_H
/// Collision-Resistant Hashing
///
/// ircd::crh contains support for collision-resistant hash functions including
/// cryptographic hash functions.
2017-09-23 01:29:06 +02:00
namespace ircd::crh
{
2017-09-23 01:29:06 +02:00
IRCD_EXCEPTION(ircd::error, error)
struct hash;
2019-01-19 21:46:44 +01:00
struct hmac;
2019-01-19 20:59:51 +01:00
struct sha1;
2017-09-23 01:29:06 +02:00
struct sha256;
2018-04-08 18:27:10 +02:00
struct ripemd160;
2017-09-23 01:29:06 +02:00
}
// Export aliases down to ircd::
2017-09-23 01:29:06 +02:00
namespace ircd
{
2019-01-19 21:46:44 +01:00
using crh::hmac;
2019-01-19 20:59:51 +01:00
using crh::sha1;
2017-09-23 01:29:06 +02:00
using crh::sha256;
2018-04-08 18:27:10 +02:00
using crh::ripemd160;
2017-09-23 01:29:06 +02:00
}
/// Abstract interface to a hashing context for any algorithm in ircd::crh
///
/// Use this type when dealing with algorithm-agnostic hashing.
2017-09-23 01:29:06 +02:00
struct ircd::crh::hash
{
/// Returns the byte length of the mutable_buffer for digests
2017-09-23 01:29:06 +02:00
virtual size_t length() const = 0;
/// Samples the digest at the current state (without modifying)
virtual void digest(const mutable_buffer &) const = 0;
/// Samples the digest and modifies the state (depending on impl)
virtual void finalize(const mutable_buffer &b);
/// Appends to the message
virtual void update(const const_buffer &) = 0;
2017-09-23 01:29:06 +02:00
// conveniences for output
template<size_t SIZE> fixed_const_buffer<SIZE> digest() const;
template<size_t SIZE> operator fixed_const_buffer<SIZE>() const;
// conveniences for input
void operator()(const mutable_buffer &out, const const_buffer &in);
hash &operator+=(const const_buffer &);
2017-09-23 01:29:06 +02:00
virtual ~hash() noexcept;
};
2019-01-19 21:46:44 +01:00
struct ircd::crh::hmac
{
struct ctx;
protected:
std::unique_ptr<ctx> ctx;
public:
/// Returns the byte length of the mutable_buffer for digests
size_t length() const;
/// Samples the digest and modifies the state (depending on impl)
const_buffer finalize(const mutable_buffer &b);
/// Appends to the message
void update(const const_buffer &);
hmac(const string_view &algorithm, const const_buffer &key);
hmac(hmac &&) = default;
hmac(const hmac &) = delete;
~hmac() noexcept;
};
2019-01-19 20:59:51 +01:00
/// SHA-1 hashing device.
struct ircd::crh::sha1
final
:hash
{
struct ctx;
static constexpr const size_t &digest_size
{
160 / 8
};
using buf = fixed_const_buffer<digest_size>;
protected:
std::unique_ptr<ctx> ctx;
public:
size_t length() const override;
void digest(const mutable_buffer &) const override;
void finalize(const mutable_buffer &) override;
void update(const const_buffer &) override;
sha1(const mutable_buffer &, const const_buffer &); // note: finalizes
sha1(const const_buffer &); // note: finalizes
sha1();
~sha1() noexcept;
};
/// SHA-256 hashing device.
2017-09-23 01:29:06 +02:00
struct ircd::crh::sha256
final
2017-09-23 01:29:06 +02:00
:hash
{
struct ctx;
static constexpr const size_t &digest_size
2017-09-23 01:29:06 +02:00
{
256 / 8
};
using buf = fixed_const_buffer<digest_size>;
2017-09-23 01:29:06 +02:00
protected:
std::unique_ptr<ctx> ctx;
public:
size_t length() const override;
void digest(const mutable_buffer &) const override;
void finalize(const mutable_buffer &) override;
void update(const const_buffer &) override;
2017-09-23 01:29:06 +02:00
sha256(const mutable_buffer &, const const_buffer &); // note: finalizes
sha256(const const_buffer &); // note: finalizes
2017-09-23 01:29:06 +02:00
sha256();
~sha256() noexcept;
};
2018-04-08 18:27:10 +02:00
/// RIPEMD160 hashing device.
struct ircd::crh::ripemd160
final
:hash
{
struct ctx;
static constexpr const size_t &digest_size
2018-04-08 18:27:10 +02:00
{
160 / 8
};
using buf = fixed_const_buffer<digest_size>;
protected:
std::unique_ptr<ctx> ctx;
public:
size_t length() const override;
void digest(const mutable_buffer &) const override;
void finalize(const mutable_buffer &) override;
void update(const const_buffer &) override;
ripemd160(const mutable_buffer &, const const_buffer &); // note: finalizes
ripemd160(const const_buffer &); // note: finalizes
2018-04-08 18:27:10 +02:00
ripemd160();
~ripemd160() noexcept;
};
/// Automatic gratification from hash::digest()
template<size_t SIZE>
ircd::crh::hash::operator
fixed_const_buffer<SIZE>()
const
{
return digest<SIZE>();
}
/// Digests the hash into the buffer of the specified SIZE and returns it
template<size_t SIZE>
ircd::fixed_const_buffer<SIZE>
ircd::crh::hash::digest()
const
{
assert(SIZE >= length());
return fixed_const_buffer<SIZE>
{
[this](const auto &buffer)
{
this->digest(buffer);
}
};
}