Compare commits

7 Commits
Author SHA1 Message Date
hashlag 3dacd57fef Replace Decryptor CRTP base with a concept.
Chaos Ci / test (push) Successful in 3m4s
Chaos Ci / benchmark (push) Successful in 1m31s
2026-09-06 02:45:45 +03:00
hashlag 2c6a24197a Replace Encryptor CRTP base with a concept.
Chaos Ci / test (push) Successful in 5m6s
Chaos Ci / benchmark (push) Successful in 1m29s
2026-09-06 02:27:35 +03:00
hashlag f0228877b4 DesCrypt: Expose Block type in Encryptor and Decryptor. 2026-09-06 01:05:15 +03:00
hashlag 1c341c6888 Replace Padder CRTP base with a concept.
Chaos Ci / test (push) Successful in 3m1s
Chaos Ci / benchmark (push) Successful in 1m51s
2026-09-01 00:43:27 +03:00
hashlag 7d028cce66 SeArray: Replace enable_if with a concept.
Chaos Ci / test (push) Successful in 3m28s
Chaos Ci / benchmark (push) Successful in 1m52s
2026-08-31 01:32:52 +03:00
hashlag b03fddd40b Replace Hasher CRTP base with a concept.
Chaos Ci / test (push) Successful in 3m17s
Chaos Ci / benchmark (push) Successful in 1m54s
2026-08-31 01:22:07 +03:00
hashlag 2f1b5091cd Replace Hash CRTP base with a concept. 2026-08-31 00:05:18 +03:00
17 changed files with 116 additions and 197 deletions
+16 -33
View File
@@ -1,44 +1,27 @@
#ifndef CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP
#define CHAOS_CIPHER_BLOCK_DECRYPTOR_HPP
#include <concepts>
#include <cstdint>
#include <type_traits>
namespace Chaos::Cipher::Block
{
template<typename T>
class Decryptor
concept Decryptor = requires(T decryptor,
typename T::Block block,
uint8_t * outBegin, uint8_t * outEnd,
uint8_t * inBegin, uint8_t * inEnd)
{
public:
template<typename OutputIt, typename InputIt>
void DecryptBlock(OutputIt outBegin, OutputIt outEnd,
InputIt inBegin, InputIt inEnd) const
{
Impl().DecryptBlock(outBegin, outEnd, inBegin, inEnd);
}
template<typename Block>
auto DecryptBlock(Block block) const
{
return Impl().DecryptBlock(block);
}
auto GetBlockSize() const
{
return Impl().GetBlockSize();
}
protected:
Decryptor() = default;
private:
const T & Impl() const
{
return static_cast<const T &>(*this);
}
T & Impl()
{
return static_cast<T &>(*this);
}
typename T::Block;
typename T::Key;
requires std::constructible_from<T, typename T::Key>;
requires std::unsigned_integral<std::remove_cvref_t<decltype(T::BlockSize)>>;
requires std::unsigned_integral<std::remove_cvref_t<decltype(T::KeySize)>>;
decryptor.DecryptBlock(outBegin, outEnd, inBegin, inEnd);
{ decryptor.DecryptBlock(block) } -> std::same_as<typename T::Block>;
{ decryptor.GetBlockSize() } -> std::unsigned_integral;
};
} // namespace Chaos::Cipher::Block
+8 -2
View File
@@ -233,9 +233,10 @@ public:
Inner_::RawKey Key_;
};
class Encryptor : public Chaos::Cipher::Block::Encryptor<Encryptor>
class Encryptor
{
public:
using Block = DesCrypt::Block;
using Key = DesCrypt::Key;
static constexpr size_t BlockSize = DesCrypt::BlockSize;
static constexpr size_t KeySize = DesCrypt::KeySize;
@@ -278,9 +279,12 @@ public:
Inner_::KeySchedule Schedule_;
};
class Decryptor : public Chaos::Cipher::Block::Decryptor<Decryptor>
static_assert(Chaos::Cipher::Block::Encryptor<Encryptor>);
class Decryptor
{
public:
using Block = DesCrypt::Block;
using Key = DesCrypt::Key;
static constexpr size_t BlockSize = DesCrypt::BlockSize;
static constexpr size_t KeySize = DesCrypt::KeySize;
@@ -323,6 +327,8 @@ public:
Inner_::KeySchedule Schedule_;
};
static_assert(Chaos::Cipher::Block::Decryptor<Decryptor>);
private:
using BlockHalf = uint32_t;
using RawBlockArray = Service::SeArray<uint8_t, 8>;
+16 -33
View File
@@ -1,44 +1,27 @@
#ifndef CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP
#define CHAOS_CIPHER_BLOCK_ENCRYPTOR_HPP
#include <concepts>
#include <cstdint>
#include <type_traits>
namespace Chaos::Cipher::Block
{
template<typename T>
class Encryptor
concept Encryptor = requires(T encryptor,
typename T::Block block,
uint8_t * outBegin, uint8_t * outEnd,
uint8_t * inBegin, uint8_t * inEnd)
{
public:
template<typename OutputIt, typename InputIt>
void EncryptBlock(OutputIt outBegin, OutputIt outEnd,
InputIt inBegin, InputIt inEnd) const
{
Impl().EncryptBlock(outBegin, outEnd, inBegin, inEnd);
}
template<typename Block>
auto EncryptBlock(Block block) const
{
return Impl().EncryptBlock(block);
}
auto GetBlockSize() const
{
return Impl().GetBlockSize();
}
protected:
Encryptor() = default;
private:
const T & Impl() const
{
return static_cast<const T &>(*this);
}
T & Impl()
{
return static_cast<T &>(*this);
}
typename T::Block;
typename T::Key;
requires std::constructible_from<T, typename T::Key>;
requires std::unsigned_integral<std::remove_cvref_t<decltype(T::BlockSize)>>;
requires std::unsigned_integral<std::remove_cvref_t<decltype(T::KeySize)>>;
encryptor.EncryptBlock(outBegin, outEnd, inBegin, inEnd);
{ encryptor.EncryptBlock(block) } -> std::same_as<typename T::Block>;
{ encryptor.GetBlockSize() } -> std::unsigned_integral;
};
} // namespace Chaos::Cipher::Block
+4 -25
View File
@@ -1,38 +1,17 @@
#ifndef CHAOS_HASH_HASH_HPP
#define CHAOS_HASH_HASH_HPP
#include <concepts>
#include <string>
namespace Chaos::Hash
{
template<typename T>
class Hash
concept Hash = requires(T hash)
{
public:
auto GetRawDigest() const
{
return Impl().GetRawDigest();
}
std::string ToHexString() const
{
return Impl().ToHexString();
}
protected:
Hash() = default;
private:
const T & Impl() const
{
return static_cast<const T &>(*this);
}
T & Impl()
{
return static_cast<T &>(*this);
}
hash.GetRawDigest();
{ hash.ToHexString() } -> std::same_as<std::string>;
};
} // namespace Chaos::Hash
+8 -31
View File
@@ -1,42 +1,19 @@
#ifndef CHAOS_HASH_HASHER_HPP
#define CHAOS_HASH_HASHER_HPP
#include <cstdint>
#include "Hash.hpp"
namespace Chaos::Hash
{
template<typename T>
class Hasher
concept Hasher = requires(T hasher, uint8_t * begin, uint8_t * end)
{
public:
void Reset()
{
Impl().Reset();
}
template<typename InputIt>
void Update(InputIt begin, InputIt end)
{
Impl().Update(begin, end);
}
auto Finish()
{
return Impl().Finish();
}
protected:
Hasher() = default;
private:
const T & Impl() const
{
return static_cast<const T &>(*this);
}
T & Impl()
{
return static_cast<T &>(*this);
}
hasher.Reset();
hasher.Update(begin, end);
{ hasher.Finish() } -> Hash;
};
} // namespace Chaos::Hash
+6 -2
View File
@@ -136,7 +136,7 @@ private:
namespace Chaos::Hash::Md4
{
struct Md4Hash : public Hash<Md4Hash>
struct Md4Hash
{
std::array<uint8_t, 16> GetRawDigest() const
{
@@ -161,7 +161,9 @@ struct Md4Hash : public Hash<Md4Hash>
std::array<uint8_t, 16> RawDigest_;
};
class Md4Hasher : public Hasher<Md4Hasher>
static_assert(Hash<Md4Hash>);
class Md4Hasher
{
public:
using HashType = Md4Hash;
@@ -302,6 +304,8 @@ private:
}
};
static_assert(Hasher<Md4Hasher>);
} // namespace Chaos::Hash::Md4
#endif // CHAOS_HASH_MD4_HPP
+6 -2
View File
@@ -160,7 +160,7 @@ private:
namespace Chaos::Hash::Md5
{
struct Md5Hash : public Hash<Md5Hash>
struct Md5Hash
{
std::array<uint8_t, 16> GetRawDigest() const
{
@@ -185,7 +185,9 @@ struct Md5Hash : public Hash<Md5Hash>
std::array<uint8_t, 16> RawDigest_;
};
class Md5Hasher : public Hasher<Md5Hasher>
static_assert(Hash<Md5Hash>);
class Md5Hasher
{
public:
using HashType = Md5Hash;
@@ -326,6 +328,8 @@ private:
}
};
static_assert(Hasher<Md5Hasher>);
} // namespace Chaos::Hash::Md5
#endif // CHAOS_HASH_MD5_HPP
+6 -2
View File
@@ -129,7 +129,7 @@ private:
namespace Chaos::Hash::Sha1
{
struct Sha1Hash : public Hash<Sha1Hash>
struct Sha1Hash
{
std::array<uint8_t, 20> GetRawDigest() const
{
@@ -155,7 +155,9 @@ struct Sha1Hash : public Hash<Sha1Hash>
std::array<uint8_t, 20> RawDigest_;
};
class Sha1Hasher : public Hasher<Sha1Hasher>
static_assert(Hash<Sha1Hash>);
class Sha1Hasher
{
public:
using HashType = Sha1Hash;
@@ -296,6 +298,8 @@ private:
}
};
static_assert(Hasher<Sha1Hasher>);
} // namespace Chaos::Hash::Sha1
#endif // CHAOS_HASH_SHA1_HPP
+1 -3
View File
@@ -3,7 +3,6 @@
#include <array>
#include <cstdint>
#include <type_traits>
#include "Hash/Hasher.hpp"
#include "Service/ChaosException.hpp"
@@ -11,8 +10,7 @@
namespace Chaos::Mac::Hmac
{
template<typename HasherImpl,
typename = std::enable_if_t<std::is_base_of_v<Hash::Hasher<HasherImpl>, HasherImpl>>>
template<Hash::Hasher HasherImpl>
class Hmac
{
public:
+5 -27
View File
@@ -1,38 +1,16 @@
#ifndef CHAOS_PADDING_PADDER_HPP
#define CHAOS_PADDING_PADDER_HPP
#include <cstdint>
namespace Chaos::Padding
{
template<typename T>
class Padder
concept Padder = requires(uint8_t * begin, uint8_t * end)
{
public:
template<typename OutputIt>
void Pad(OutputIt begin, OutputIt end) const
{
Impl().Pad(begin, end);
}
template<typename InputIt>
auto ComputeUnpad(InputIt begin, InputIt end) const noexcept
{
return Impl().ComputeUnpad(begin, end);
}
protected:
Padder() = default;
private:
const T & Impl() const
{
return static_cast<const T &>(*this);
}
T & Impl()
{
return static_cast<T &>(*this);
}
T::Pad(begin, end);
{ T::ComputeUnpad(begin, end) } noexcept;
};
} // namespace Chaos::Padding
+3 -1
View File
@@ -12,7 +12,7 @@
namespace Chaos::Padding
{
class PadderIso7816 : public Padder<PadderIso7816>
class PadderIso7816
{
public:
template<typename OutputIt>
@@ -82,6 +82,8 @@ private:
using Branchless = Service::Branchless;
};
static_assert(Padder<PadderIso7816>);
} // namespace Chaos::Padding
#endif // CHAOS_PADDING_PADDERISO7816_HPP
+3 -1
View File
@@ -13,7 +13,7 @@
namespace Chaos::Padding
{
class PadderPkcs7 : public Padder<PadderPkcs7>
class PadderPkcs7
{
public:
template<typename OutputIt>
@@ -75,6 +75,8 @@ private:
using Branchless = Service::Branchless;
};
static_assert(Padder<PadderPkcs7>);
} // namespace Chaos::Padding
#endif // CHAOS_PADDING_PADDERPKCS7_HPP
+2 -3
View File
@@ -2,14 +2,13 @@
#define CHAOS_SERVICE_SEARRAY_HPP
#include <array>
#include <type_traits>
#include <concepts>
#include <cstddef>
namespace Chaos::Service
{
template<typename T, size_t S,
typename = std::enable_if_t<std::is_integral_v<T>>>
template<std::integral T, size_t S>
class SeArray
{
public:
+2 -2
View File
@@ -4,8 +4,8 @@ cmake_policy(SET CMP0135 NEW)
FetchContent_Declare(
googletest
URL https://git.hashlag.net/hashlag/chaos-deps/raw/branch/main/google_googletest/googletest-1.18.0.tar.gz
URL_HASH SHA256=6e3191c1455468b3fc35a417fb565c1c5071aee1b7e7f85e30cf48a98d37d8b5
URL https://git.hashlag.net/hashlag/chaos-deps/raw/branch/main/google_googletest/googletest-1.17.0.tar.gz
URL_HASH SHA256=65fab701d9829d38cb77c14acdc431d2108bfdbf8979e40eb8ae567edf10b27c
)
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
+16 -16
View File
@@ -486,8 +486,8 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
}
}
template<typename Impl, typename InputIt>
static std::vector<uint8_t> EncryptThroughBase(const Encryptor<Impl> & enc,
template<Encryptor EncryptorImpl, typename InputIt>
static std::vector<uint8_t> EncryptGeneric(const EncryptorImpl & enc,
InputIt begin, InputIt end)
{
std::vector<uint8_t> result;
@@ -497,7 +497,7 @@ static std::vector<uint8_t> EncryptThroughBase(const Encryptor<Impl> & enc,
return result;
}
TEST(DesCryptTests, EncryptThroughBaseTest)
TEST(DesCryptTests, EncryptGenericTest)
{
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
@@ -507,16 +507,16 @@ TEST(DesCryptTests, EncryptThroughBaseTest)
DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Encryptor enc(desKey);
ASSERT_EQ(expected, EncryptThroughBase(enc, data.begin(), data.end()));
ASSERT_EQ(expected, EncryptGeneric(enc, data.begin(), data.end()));
}
template<typename Impl>
static uint64_t EncryptUInt64BlockThroughBase(const Encryptor<Impl> & enc, uint64_t block)
template<Encryptor EncryptorImpl>
static uint64_t EncryptUInt64BlockGeneric(const EncryptorImpl & enc, uint64_t block)
{
return enc.EncryptBlock(block);
}
TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest)
TEST(DesCryptTests, EncryptUInt64BlockGenericTest)
{
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
@@ -526,11 +526,11 @@ TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest)
DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Encryptor enc(desKey);
ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data));
ASSERT_EQ(expected, EncryptUInt64BlockGeneric(enc, data));
}
template<typename Impl, typename InputIt>
static std::vector<uint8_t> DecryptThroughBase(const Decryptor<Impl> & dec,
template<Decryptor DecryptorImpl, typename InputIt>
static std::vector<uint8_t> DecryptGeneric(const DecryptorImpl & dec,
InputIt begin, InputIt end)
{
std::vector<uint8_t> result;
@@ -540,7 +540,7 @@ static std::vector<uint8_t> DecryptThroughBase(const Decryptor<Impl> & dec,
return result;
}
TEST(DesCryptTests, DecryptThroughBaseTest)
TEST(DesCryptTests, DecryptGenericTest)
{
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
@@ -550,16 +550,16 @@ TEST(DesCryptTests, DecryptThroughBaseTest)
DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Decryptor dec(desKey);
ASSERT_EQ(expected, DecryptThroughBase(dec, data.begin(), data.end()));
ASSERT_EQ(expected, DecryptGeneric(dec, data.begin(), data.end()));
}
template<typename Impl>
static uint64_t DecryptUInt64BlockThroughBase(const Decryptor<Impl> & dec, uint64_t block)
template<Decryptor DecryptorImpl>
static uint64_t DecryptUInt64BlockGeneric(const DecryptorImpl & dec, uint64_t block)
{
return dec.DecryptBlock(block);
}
TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest)
TEST(DesCryptTests, DecryptUInt64BlockGenericTest)
{
std::array<uint8_t, DesCrypt::KeySize> key = { 0x13, 0x34, 0x57, 0x79, 0x9b, 0xbc, 0xdf, 0xf1 };
@@ -569,5 +569,5 @@ TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest)
DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::Decryptor dec(desKey);
ASSERT_EQ(expected, DecryptUInt64BlockThroughBase(dec, data));
ASSERT_EQ(expected, DecryptUInt64BlockGeneric(dec, data));
}
+6 -6
View File
@@ -109,8 +109,8 @@ TEST(PadIso7816Tests, PadOutIteratorUsageTest)
}
}
template<typename Impl, typename OutputIt>
void PadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
template<Padder PadderImpl, typename OutputIt>
void PadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end)
{
padder.Pad(begin, end);
}
@@ -127,7 +127,7 @@ TEST(PadIso7816Tests, PadThroughBaseTest)
};
const PadderIso7816 padder;
PadThroughBase(padder, fact.begin(), fact.end());
PadGeneric(padder, fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
@@ -324,8 +324,8 @@ TEST(PadIso7816Tests, UnpadErrorTest)
}
}
template<typename Impl, typename OutputIt>
auto ComputeUnpadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
template<Padder PadderImpl, typename OutputIt>
auto ComputeUnpadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end)
{
return padder.ComputeUnpad(begin, end);
}
@@ -336,7 +336,7 @@ TEST(PadIso7816Tests, ComputeUnpadThroughBaseTest)
std::array<uint8_t, 5> data = { 0x80, 0x00, 0x00, 0x00, 0x00 };
const PadderIso7816 padder;
auto result = ComputeUnpadThroughBase(padder, data.begin(), data.end());
auto result = ComputeUnpadGeneric(padder, data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(5, result.PadSize_);
+6 -6
View File
@@ -137,8 +137,8 @@ TEST(PadPkcs7Tests, PadOutIteratorUsageTest)
}
}
template<typename Impl, typename OutputIt>
void PadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
template<Padder PadderImpl, typename OutputIt>
void PadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end)
{
padder.Pad(begin, end);
}
@@ -153,7 +153,7 @@ TEST(PadPkcs7Tests, PadThroughBaseTest)
};
const PadderPkcs7 padder;
PadThroughBase(padder, fact.begin(), fact.end());
PadGeneric(padder, fact.begin(), fact.end());
ASSERT_EQ(expected, fact);
}
@@ -347,8 +347,8 @@ TEST(PadPkcs7Tests, UnpadErrorTest)
}
}
template<typename Impl, typename OutputIt>
auto ComputeUnpadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
template<Padder PadderImpl, typename OutputIt>
auto ComputeUnpadGeneric(const PadderImpl & padder, OutputIt begin, OutputIt end)
{
return padder.ComputeUnpad(begin, end);
}
@@ -359,7 +359,7 @@ TEST(PadPkcs7Tests, ComputeUnpadThroughBaseTest)
std::array<uint8_t, 5> data = { 0x05, 0x05, 0x05, 0x05, 0x05 };
const PadderPkcs7 padder;
auto result = ComputeUnpadThroughBase(padder, data.begin(), data.end());
auto result = ComputeUnpadGeneric(padder, data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(5, result.PadSize_);