21 Commits

Author SHA1 Message Date
hashlag 617957ce4a Merge branch 'unpad'
Chaos Ci / test (push) Successful in 2m57s
Chaos Ci / benchmark (push) Successful in 1m53s
2026-07-20 03:06:50 +03:00
hashlag eb178853e5 Remove unused #include.
Chaos Ci / test (push) Successful in 3m1s
Chaos Ci / benchmark (push) Successful in 1m55s
2026-07-20 02:56:39 +03:00
hashlag 442f7fbb29 Remove trailing whitespaces. 2026-07-20 02:56:39 +03:00
hashlag 357f00d3e1 Fix missing includes. 2026-07-20 02:56:39 +03:00
hashlag 3560e321d7 Enable clangd's missing includes diagnostic. 2026-07-20 02:56:39 +03:00
hashlag 5eb1aa2ece Set clangd's unused includes diagnostic to strict explicitly. 2026-07-20 02:56:39 +03:00
hashlag d38b77fab2 Remove unused #include.
Chaos Ci / test (push) Successful in 2m58s
Chaos Ci / benchmark (push) Successful in 1m52s
2026-07-20 02:54:15 +03:00
hashlag 233c48bb3f Remove trailing whitespaces. 2026-07-20 02:53:46 +03:00
hashlag 1adec1115c Fix missing includes. 2026-07-20 02:53:13 +03:00
hashlag e6eb390469 Enable clangd's missing includes diagnostic. 2026-07-20 02:36:50 +03:00
hashlag da34f9b499 Set clangd's unused includes diagnostic to strict explicitly. 2026-07-20 02:34:31 +03:00
hashlag e6c4f1fc98 Implement unpad for PKCS#7 padding.
Chaos Ci / test (push) Successful in 3m3s
Chaos Ci / benchmark (push) Successful in 1m55s
2026-07-20 02:20:54 +03:00
hashlag 26f5aa150c Add .clangd.
Chaos Ci / test (push) Successful in 2m51s
Chaos Ci / benchmark (push) Successful in 1m26s
2026-07-07 00:06:06 +03:00
hashlag 0608deb618 CI: Set ASan and UBSan verbosity to 1.
Chaos Ci / test (push) Successful in 2m50s
Chaos Ci / benchmark (push) Successful in 1m25s
2026-07-06 04:09:50 +03:00
hashlag f399c1b0a5 CI: Simplify steps' naming.
Chaos Ci / test (push) Successful in 2m19s
Chaos Ci / benchmark (push) Successful in 1m51s
2026-07-06 03:58:23 +03:00
hashlag 8a772f9fc5 CI: Do not collect coverage under valgrind.
Chaos Ci / test (push) Successful in 2m19s
Chaos Ci / benchmark (push) Successful in 1m57s
2026-07-06 03:56:16 +03:00
hashlag d50a82ad18 CI: Extract benchmarking into a distinct job.
Chaos Ci / test (push) Successful in 2m16s
Chaos Ci / benchmark (push) Successful in 1m49s
2026-07-06 03:48:04 +03:00
hashlag f37271ec5b Add enable no-sanitize-recover for all UBSan checks.
Chaos Ci / test-and-benchmark (push) Successful in 3m22s
2026-07-06 03:41:35 +03:00
hashlag 0126196ea8 Add sanitized test run to CI.
Chaos Ci / test-and-benchmark (push) Successful in 3m26s
2026-07-06 03:25:30 +03:00
hashlag e0768cde3c Make coverage optional for debug builds by introducing CHAOS_COVERAGE
flag.
2026-07-06 02:44:37 +03:00
hashlag f9dbca34c7 Remove trailing whitespace. 2026-07-06 02:31:12 +03:00
23 changed files with 427 additions and 15 deletions
+6
View File
@@ -0,0 +1,6 @@
CompileFlags:
CompilationDatabase: build/debug
Diagnostics:
UnusedIncludes: Strict
MissingIncludes: Strict
+42 -10
View File
@@ -3,7 +3,7 @@ name: Chaos Ci
on: [push, pull_request]
jobs:
test-and-benchmark:
test:
runs-on: ubuntu-latest
steps:
@@ -17,27 +17,36 @@ jobs:
sudo apt-get update
sudo apt-get install -y build-essential cmake valgrind lcov
- name: Configure and build tests [Debug]
- name: Build tests
run: |
mkdir build-debug
cmake -S . -B build-debug/ -D CMAKE_BUILD_TYPE=Debug
cmake --build build-debug/ -t ChaosTests
- name: Configure and build benchmarks [Release]
- name: Build tests with coverage
run: |
mkdir build-release
cmake -S . -B build-release/ -D CMAKE_BUILD_TYPE=Release
cmake --build build-release/ -t ChaosBenches
mkdir build-debug-coverage
cmake -S . -B build-debug-coverage/ -D CMAKE_BUILD_TYPE=Debug -D CHAOS_COVERAGE=ON
cmake --build build-debug-coverage/ -t ChaosTests
- name: Run tests
- name: Build tests with ASan and UBSan
run: |
mkdir build-debug-asanubsan
cmake -S . -B build-debug-asanubsan/ -D CMAKE_BUILD_TYPE=Debug -D CHAOS_ASANUBSAN=ON
cmake --build build-debug-asanubsan/ -t ChaosTests
- name: Run tests under valgrind
run: valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all ./build-debug/ChaosTests/ChaosTests
- name: Run benchmarks
run: ./build-release/ChaosBenches/ChaosBenches
- name: Run tests under ASan and UBSan
run: ASAN_OPTIONS=verbosity=1 UBSAN_OPTIONS=verbosity=1 ./build-debug-asanubsan/ChaosTests/ChaosTests
- name: Run tests for coverage
run: ./build-debug-coverage/ChaosTests/ChaosTests
- name: Process coverage data
run: |
lcov --exclude '*/usr/*' --exclude '*/_deps/*' --exclude '*/ChaosTests/*' -c -d build-debug/ -o lcovout
lcov --exclude '*/usr/*' --exclude '*/_deps/*' --exclude '*/ChaosTests/*' -c -d build-debug-coverage/ -o lcovout
genhtml lcovout -o coverage-report/
- name: Upload coverage report
@@ -48,3 +57,26 @@ jobs:
if-no-files-found: error
include-hidden-files: true
retention-days: 10
benchmark:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: true
- name: Install
run: |
sudo apt-get update
sudo apt-get install -y build-essential cmake
- name: Build benchmarks
run: |
mkdir build-release
cmake -S . -B build-release/ -D CMAKE_BUILD_TYPE=Release
cmake --build build-release/ -t ChaosBenches
- name: Run benchmarks
run: ./build-release/ChaosBenches/ChaosBenches
+3
View File
@@ -1,6 +1,9 @@
#ifndef CHAOS_CIPHER_ARC4_ARC4CRYPT_HPP
#define CHAOS_CIPHER_ARC4_ARC4CRYPT_HPP
#include <cstdint>
#include <algorithm>
#include "Arc4Gen.hpp"
#include "Service/SeArray.hpp"
#include "Service/ChaosException.hpp"
+1
View File
@@ -4,6 +4,7 @@
#include <array>
#include <cstdint>
#include <vector>
#include <utility>
#include "Service/ChaosException.hpp"
+3
View File
@@ -3,6 +3,9 @@
#include <algorithm>
#include <utility>
#include <cstdint>
#include <iterator>
#include <cstddef>
#include "Service/ChaosException.hpp"
#include "Service/SeArray.hpp"
+2
View File
@@ -4,6 +4,8 @@
#include <cstdint>
#include <array>
#include <string>
#include <cstdio>
#include <cstddef>
#include "Hash.hpp"
#include "Hasher.hpp"
+2
View File
@@ -4,6 +4,8 @@
#include <cstdint>
#include <array>
#include <string>
#include <cstdio>
#include <cstddef>
#include "Hash.hpp"
#include "Hasher.hpp"
+4
View File
@@ -4,6 +4,10 @@
#include <cstdint>
#include <array>
#include <string>
#include <cstddef>
#include <cstdio>
#include <tuple>
#include <algorithm>
#include "Hash.hpp"
#include "Hasher.hpp"
+6
View File
@@ -14,6 +14,12 @@ public:
Impl().Pad(begin, end);
}
template<typename InputIt>
auto ComputeUnpad(InputIt begin, InputIt end) const noexcept
{
return Impl().ComputeUnpad(begin, end);
}
protected:
Padder() = default;
+112
View File
@@ -1,9 +1,12 @@
#ifndef CHAOS_PADDING_PADDERPKCS7_HPP
#define CHAOS_PADDING_PADDERPKCS7_HPP
#include <climits>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <limits>
#include <type_traits>
#include "Padding/Padder.hpp"
#include "Service/ChaosException.hpp"
@@ -19,6 +22,7 @@ public:
{
auto dist = std::distance(begin, end);
// TODO: dist > 0
if (dist >= 0 && dist <= std::numeric_limits<uint8_t>::max())
{
for (OutputIt it = begin; it != end; ++it)
@@ -31,6 +35,114 @@ public:
throw Service::ChaosException("PadderPkcs7::Pad(): invalid range");
}
}
struct ComputeUnpadResult
{
bool IsOkay_;
uint8_t PadSize_;
};
template<typename InputIt>
static ComputeUnpadResult ComputeUnpad(InputIt begin, InputIt end) noexcept
{
if (begin == end)
{
return { .IsOkay_ = false, .PadSize_ = 0 };
}
const uint8_t padSize = *std::prev(end);
uint8_t isOkay = ~BlIsZero<uint8_t>(padSize);
size_t suffixSize = 0;
InputIt it = end;
while (it != begin && suffixSize < std::numeric_limits<uint8_t>::max())
{
--it;
++suffixSize;
isOkay &= (BlEq<uint8_t>(*it, padSize) |
BlGt<uint8_t, size_t>(suffixSize, padSize));
}
isOkay &= BlGe<uint8_t, size_t>(suffixSize, padSize);
return
{
.IsOkay_ = static_cast<bool>(isOkay),
.PadSize_ = BlSel<uint8_t>(isOkay, padSize, 0)
};
}
private:
template<typename OutUInt, typename InUInt>
static constexpr OutUInt BlMsb(InUInt in) noexcept
{
static_assert(std::is_unsigned_v<OutUInt> &&
std::is_unsigned_v<InUInt>);
constexpr uint8_t shift = (sizeof(InUInt) * CHAR_BIT) - 1;
return static_cast<OutUInt>(0) - (in >> shift);
}
template<typename OutUInt, typename InUInt>
static constexpr OutUInt BlLt(InUInt lhs, InUInt rhs) noexcept
{
static_assert(std::is_unsigned_v<OutUInt> &&
std::is_unsigned_v<InUInt>);
static_assert(sizeof(InUInt) <= sizeof(uint64_t));
const uint64_t lhsEx = lhs;
const uint64_t rhsEx = rhs;
return BlMsb<OutUInt>(lhsEx ^ ((lhsEx ^ rhsEx) | ((lhsEx - rhsEx) ^ lhsEx)));
}
template<typename OutUInt, typename InUInt>
static constexpr OutUInt BlIsZero(InUInt in) noexcept
{
static_assert(std::is_unsigned_v<OutUInt> &&
std::is_unsigned_v<InUInt>);
static_assert(sizeof(InUInt) <= sizeof(uint64_t));
const uint64_t inEx = in;
return BlMsb<OutUInt>(~inEx & (inEx - 1U));
}
template<typename OutUInt, typename InUInt>
static constexpr OutUInt BlEq(InUInt lhs, InUInt rhs) noexcept
{
static_assert(std::is_unsigned_v<OutUInt> &&
std::is_unsigned_v<InUInt>);
return BlIsZero<OutUInt, InUInt>(lhs ^ rhs);
}
template<typename OutUInt, typename InUInt>
static constexpr OutUInt BlGe(InUInt lhs, InUInt rhs) noexcept
{
static_assert(std::is_unsigned_v<OutUInt> &&
std::is_unsigned_v<InUInt>);
return ~BlLt<OutUInt>(lhs, rhs);
}
template<typename OutUInt, typename InUInt>
static constexpr OutUInt BlGt(InUInt lhs, InUInt rhs) noexcept
{
static_assert(std::is_unsigned_v<OutUInt> &&
std::is_unsigned_v<InUInt>);
return BlLt<OutUInt>(rhs, lhs);
}
template<typename UInt>
static constexpr UInt BlSel(UInt mask, UInt onTrue, UInt onFalse) noexcept
{
static_assert(std::is_unsigned_v<UInt>);
return (mask & onTrue) | (~mask & onFalse);
}
};
} // namespace Chaos::Padding
+1
View File
@@ -2,6 +2,7 @@
#define CHAOS_SERVICE_CHAOSEXCEPTION_HPP
#include <string>
#include <utility>
namespace Chaos::Service
{
+1
View File
@@ -3,6 +3,7 @@
#include <array>
#include <type_traits>
#include <cstddef>
namespace Chaos::Service
{
+9 -1
View File
@@ -31,11 +31,19 @@ target_include_directories(ChaosTests PRIVATE
target_compile_options(ChaosTests PRIVATE -Wunused -Werror=unused)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
option(CHAOS_COVERAGE "Enable coverage" OFF)
option(CHAOS_ASANUBSAN "Enable ASan and UBSan" OFF)
if(CHAOS_COVERAGE)
target_compile_options(ChaosTests PRIVATE --coverage)
target_link_options(ChaosTests PRIVATE --coverage)
endif()
if(CHAOS_ASANUBSAN)
target_compile_options(ChaosTests PRIVATE -fsanitize=address,undefined -fno-sanitize-recover=all)
target_link_options(ChaosTests PRIVATE -fsanitize=address,undefined -fno-sanitize-recover=all)
endif()
include(GoogleTest)
gtest_discover_tests(ChaosTests)
+2 -1
View File
@@ -1,7 +1,8 @@
#include <gtest/gtest.h>
#include <vector>
#include <string>
#include <array>
#include <cstdint>
#include <cstring>
#include "Cipher/Arc4/Arc4Crypt.hpp"
#include "Service/ChaosException.hpp"
+5
View File
@@ -1,6 +1,11 @@
#include <gtest/gtest.h>
#include <cstdint>
#include <array>
#include <iterator>
#include <cstring>
#include "Cipher/Arc4/Arc4Gen.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Cipher::Arc4;
+5
View File
@@ -1,7 +1,12 @@
#include <gtest/gtest.h>
#include <cstdint>
#include <array>
#include <vector>
#include "Cipher/Block/Des/DesCrypt.hpp"
#include "Cipher/Block/Encryptor.hpp"
#include "Cipher/Block/Decryptor.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Cipher::Block::Des;
using namespace Chaos::Cipher::Block;
+2
View File
@@ -1,4 +1,6 @@
#include <gtest/gtest.h>
#include <cstring>
#include <string>
#include "Hash/Md4.hpp"
+2
View File
@@ -1,4 +1,6 @@
#include <gtest/gtest.h>
#include <cstring>
#include <string>
#include "Hash/Md5.hpp"
+2
View File
@@ -1,4 +1,6 @@
#include <gtest/gtest.h>
#include <cstring>
#include <string>
#include "Hash/Sha1.hpp"
+4
View File
@@ -1,8 +1,12 @@
#include <gtest/gtest.h>
#include <cstring>
#include <string>
#include <cstdint>
#include "Hash/Md5.hpp"
#include "Hash/Sha1.hpp"
#include "Mac/Hmac.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Mac::Hmac;
using namespace Chaos::Hash::Md5;
@@ -4,6 +4,7 @@
#include <vector>
#include "Padding/PadderIso7816.hpp"
#include "Padding/Padder.hpp"
using namespace Chaos::Padding;
+208
View File
@@ -4,6 +4,7 @@
#include <vector>
#include "Padding/PadderPkcs7.hpp"
#include "Padding/Padder.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Padding;
@@ -141,3 +142,210 @@ TEST(PadPkcs7Tests, PadThroughBaseTest)
ASSERT_EQ(expected, fact);
}
}
TEST(PadPkcs7Tests, UnpadTest)
{
{
std::array<uint8_t, 1> data = { 0x01 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(1, result.PadSize_);
}
{
std::array<uint8_t, 4> data = { 0xaa, 0xbb, 0x02, 0x02 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(2, result.PadSize_);
}
{
std::array<uint8_t, 10> data = { 0xaa, 0xbb, 0xcc, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07, 0x07 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(7, result.PadSize_);
}
{
std::array<uint8_t, 10> data = { 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5,
0xa6, 0xa7, 0xa8, 0x01 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(1, result.PadSize_);
}
{
std::array<uint8_t, 10> data = { 0xa0, 0xa1, 0xa2, 0x07, 0x07, 0x07,
0x07, 0x07, 0x07, 0x07 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(7, result.PadSize_);
}
{
std::array<uint8_t, 10> data = { 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a,
0x0a, 0x0a, 0x0a, 0x0a };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(10, result.PadSize_);
}
{
std::array<uint8_t, 4> data = { 0x03, 0x03, 0x03, 0x03 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(3, result.PadSize_);
}
{
std::array<uint8_t, 200> data;
data.fill(0x41);
*std::prev(data.end()) = 1;
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(1, result.PadSize_);
}
{
std::array<uint8_t, 200> data;
data.fill(0x41);
std::fill(data.end() - 100, data.end(), 100);
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(100, result.PadSize_);
}
{
std::array<uint8_t, 255> data;
data.fill(255);
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(255, result.PadSize_);
}
{
std::array<uint8_t, 381> data;
data.fill(0x9a);
std::fill(data.end() - 255, data.end(), 255);
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(255, result.PadSize_);
}
}
TEST(PadPkcs7Tests, UnpadErrorTest)
{
{
std::array<uint8_t, 0> data = { };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_FALSE(result.IsOkay_);
ASSERT_EQ(0, result.PadSize_);
}
{
std::array<uint8_t, 1> data = { 0x00 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_FALSE(result.IsOkay_);
ASSERT_EQ(0, result.PadSize_);
}
{
std::array<uint8_t, 5> data = { 0xa0, 0xa1, 0xa2, 0xa3, 0xff };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_FALSE(result.IsOkay_);
ASSERT_EQ(0, result.PadSize_);
}
{
std::array<uint8_t, 5> data = { 0xa0, 0xa1, 0xa2, 0xa3, 0x06 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_FALSE(result.IsOkay_);
ASSERT_EQ(0, result.PadSize_);
}
{
std::array<uint8_t, 5> data = { 0xa0, 0xa1, 0xa2, 0xa3, 0x05 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_FALSE(result.IsOkay_);
ASSERT_EQ(0, result.PadSize_);
}
{
std::array<uint8_t, 5> data = { 0xa0, 0xa1, 0x03, 0x02, 0x03 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_FALSE(result.IsOkay_);
ASSERT_EQ(0, result.PadSize_);
}
{
std::array<uint8_t, 1> data = { 0xff };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_FALSE(result.IsOkay_);
ASSERT_EQ(0, result.PadSize_);
}
{
std::array<uint8_t, 2> data = { 0x03, 0x03 };
auto result = PadderPkcs7::ComputeUnpad(data.begin(), data.end());
ASSERT_FALSE(result.IsOkay_);
ASSERT_EQ(0, result.PadSize_);
}
}
template<typename Impl, typename OutputIt>
auto ComputeUnpadThroughBase(const Padder<Impl> & padder, OutputIt begin, OutputIt end)
{
return padder.ComputeUnpad(begin, end);
}
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());
ASSERT_TRUE(result.IsOkay_);
ASSERT_EQ(5, result.PadSize_);
}
}
+1
View File
@@ -1,6 +1,7 @@
#include <gtest/gtest.h>
#include <cstdint>
#include <iterator>
#include <cstddef>
#include "Service/SeArray.hpp"