20 Commits

Author SHA1 Message Date
hashlag 242a35bd96 Implement unpad for PKCS#7 padding.
Chaos Ci / test (push) Successful in 3m4s
Chaos Ci / benchmark (push) Successful in 1m54s
2026-07-20 03:08:21 +03:00
hashlag 1dc703922b Remove unused #include. 2026-07-20 03:08:21 +03:00
hashlag a9c1e52c45 Remove trailing whitespaces. 2026-07-20 03:08:21 +03:00
hashlag 34b19434a1 Fix missing includes. 2026-07-20 03:08:21 +03:00
hashlag bcf96061cd Enable clangd's missing includes diagnostic. 2026-07-20 03:08:21 +03:00
hashlag a5b7b1d7e1 Set clangd's unused includes diagnostic to strict explicitly. 2026-07-20 03:08:21 +03:00
hashlag c0f2f15d08 Add .clangd.
Chaos Ci / test (push) Successful in 3m1s
Chaos Ci / benchmark (push) Successful in 1m49s
2026-07-07 00:07:12 +03:00
hashlag 3c3ff4af15 CI: Set ASan and UBSan verbosity to 1.
Chaos Ci / test (push) Successful in 2m54s
Chaos Ci / benchmark (push) Successful in 1m23s
2026-07-06 04:18:50 +03:00
hashlag b2ea94c18b CI: Simplify steps' naming. 2026-07-06 04:18:50 +03:00
hashlag 00dc60eee6 CI: Do not collect coverage under valgrind. 2026-07-06 04:18:50 +03:00
hashlag bdd63f560a CI: Extract benchmarking into a distinct job. 2026-07-06 04:18:50 +03:00
hashlag b220221c8b Add enable no-sanitize-recover for all UBSan checks. 2026-07-06 04:18:50 +03:00
hashlag 8351bf2b74 Add sanitized test run to CI. 2026-07-06 04:18:50 +03:00
hashlag ac8424b2b4 Make coverage optional for debug builds by introducing CHAOS_COVERAGE
flag.
2026-07-06 04:18:50 +03:00
hashlag 809d243d38 Remove trailing whitespace. 2026-07-06 04:18:50 +03:00
hashlag bea8a84a4f Add ECB-mode encryptor draft implementation.
Chaos Ci / test-and-benchmark (push) Successful in 4m16s
2026-07-05 21:34:36 +03:00
hashlag 6c5a12f339 Standartize block cipher's inner encryptor/decryptor class names.
Chaos Ci / test-and-benchmark (push) Successful in 2m43s
2026-06-22 02:02:44 +03:00
hashlag aafe22106e Merge branch 'pad'
Chaos Ci / test-and-benchmark (push) Successful in 2m39s
2026-06-19 01:09:50 +03:00
hashlag 6c5b2a6ca9 Add .DS_Store to .gitignore.
Chaos Ci / test-and-benchmark (push) Successful in 2m6s
2026-06-17 23:44:17 +03:00
hashlag 4df35b5256 Fix compilation with apple clang.
Chaos Ci / test-and-benchmark (push) Successful in 3m44s
Provide the template parameter for std::min<> explicitly. :)
2026-03-22 23:53:33 +03:00
25 changed files with 1002 additions and 37 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] on: [push, pull_request]
jobs: jobs:
test-and-benchmark: test:
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
@@ -17,27 +17,36 @@ jobs:
sudo apt-get update sudo apt-get update
sudo apt-get install -y build-essential cmake valgrind lcov sudo apt-get install -y build-essential cmake valgrind lcov
- name: Configure and build tests [Debug] - name: Build tests
run: | run: |
mkdir build-debug mkdir build-debug
cmake -S . -B build-debug/ -D CMAKE_BUILD_TYPE=Debug cmake -S . -B build-debug/ -D CMAKE_BUILD_TYPE=Debug
cmake --build build-debug/ -t ChaosTests cmake --build build-debug/ -t ChaosTests
- name: Configure and build benchmarks [Release] - name: Build tests with coverage
run: | run: |
mkdir build-release mkdir build-debug-coverage
cmake -S . -B build-release/ -D CMAKE_BUILD_TYPE=Release cmake -S . -B build-debug-coverage/ -D CMAKE_BUILD_TYPE=Debug -D CHAOS_COVERAGE=ON
cmake --build build-release/ -t ChaosBenches 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 run: valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all ./build-debug/ChaosTests/ChaosTests
- name: Run benchmarks - name: Run tests under ASan and UBSan
run: ./build-release/ChaosBenches/ChaosBenches 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 - name: Process coverage data
run: | 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/ genhtml lcovout -o coverage-report/
- name: Upload coverage report - name: Upload coverage report
@@ -48,3 +57,26 @@ jobs:
if-no-files-found: error if-no-files-found: error
include-hidden-files: true include-hidden-files: true
retention-days: 10 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 #ifndef CHAOS_CIPHER_ARC4_ARC4CRYPT_HPP
#define CHAOS_CIPHER_ARC4_ARC4CRYPT_HPP #define CHAOS_CIPHER_ARC4_ARC4CRYPT_HPP
#include <cstdint>
#include <algorithm>
#include "Arc4Gen.hpp" #include "Arc4Gen.hpp"
#include "Service/SeArray.hpp" #include "Service/SeArray.hpp"
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
+1
View File
@@ -4,6 +4,7 @@
#include <array> #include <array>
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <utility>
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
+7 -4
View File
@@ -3,6 +3,9 @@
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
#include <cstdint>
#include <iterator>
#include <cstddef>
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
#include "Service/SeArray.hpp" #include "Service/SeArray.hpp"
@@ -230,14 +233,14 @@ public:
Inner_::RawKey Key_; Inner_::RawKey Key_;
}; };
class DesEncryptor : public Encryptor<DesEncryptor> class Encryptor : public Chaos::Cipher::Block::Encryptor<Encryptor>
{ {
public: public:
using Key = DesCrypt::Key; using Key = DesCrypt::Key;
static constexpr size_t BlockSize = DesCrypt::BlockSize; static constexpr size_t BlockSize = DesCrypt::BlockSize;
static constexpr size_t KeySize = DesCrypt::KeySize; static constexpr size_t KeySize = DesCrypt::KeySize;
DesEncryptor(const Key & key) Encryptor(const Key & key)
: Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_) : Schedule_(Inner_::KeySchedule::Direction::Encrypt, key.Key_)
{ } { }
@@ -275,14 +278,14 @@ public:
Inner_::KeySchedule Schedule_; Inner_::KeySchedule Schedule_;
}; };
class DesDecryptor : public Decryptor<DesDecryptor> class Decryptor : public Chaos::Cipher::Block::Decryptor<Decryptor>
{ {
public: public:
using Key = DesCrypt::Key; using Key = DesCrypt::Key;
static constexpr size_t BlockSize = DesCrypt::BlockSize; static constexpr size_t BlockSize = DesCrypt::BlockSize;
static constexpr size_t KeySize = DesCrypt::KeySize; static constexpr size_t KeySize = DesCrypt::KeySize;
DesDecryptor(const Key & key) Decryptor(const Key & key)
: Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_) : Schedule_(Inner_::KeySchedule::Direction::Decrypt, key.Key_)
{ } { }
+113
View File
@@ -0,0 +1,113 @@
#ifndef CHAOS_CIPHER_BLOCK_MODE_ECB_HPP
#define CHAOS_CIPHER_BLOCK_MODE_ECB_HPP
#include <cstddef>
#include <cstdint>
#include "Service/ChaosException.hpp"
#include "Service/SeArray.hpp"
namespace Chaos::Cipher::Block::Mode
{
template<typename CipherT, typename PadderT>
class EcbMode
{
public:
class Encryptor
{
public:
Encryptor(const typename CipherT::Key & key)
: Encryptor_(key)
, BlockBytesPacked_(0)
{ }
static constexpr uint64_t PredictMaxUpdateOutput(uint64_t in)
{
return in + CipherT::BlockSize - 1;
}
static constexpr uint64_t PredictMaxFinishOutput()
{
return CipherT::BlockSize;
}
template<typename OutputIt, typename InputIt>
uint64_t Update(OutputIt outBegin, OutputIt outEnd,
InputIt inBegin, InputIt inEnd)
{
return UpdateImpl(outBegin, outEnd, inBegin, inEnd);
}
template<typename OutputIt>
uint64_t Finish(OutputIt outBegin, OutputIt outEnd)
{
PadderT::Pad(Block_.Begin() + BlockBytesPacked_, Block_.End());
Encryptor_.EncryptBlock(EncryptedBlock_.Begin(), EncryptedBlock_.End(),
Block_.Begin(), Block_.End());
EnsureCopy(outBegin, outEnd, EncryptedBlock_.Begin(), EncryptedBlock_.End());
return CipherT::BlockSize;
}
private:
typename CipherT::Encryptor Encryptor_;
uint64_t BlockBytesPacked_;
Service::SeArray<uint8_t, CipherT::BlockSize> Block_;
Service::SeArray<uint8_t, CipherT::BlockSize> EncryptedBlock_;
template<typename OutputIt, typename InputIt>
static OutputIt EnsureCopy(OutputIt outBegin, OutputIt outEnd,
InputIt inBegin, InputIt inEnd)
{
OutputIt out = outBegin;
InputIt in = inBegin;
for (; out != outEnd && in != inEnd; ++out, ++in)
{
*out = *in;
}
if (out == outEnd && in != inEnd)
{
throw Service::ChaosException("EcbMode<>::Encryptor: insufficient output "
"buffer size");
}
return out;
}
template<typename OutputIt, typename InputIt>
uint64_t UpdateImpl(OutputIt outBegin, OutputIt outEnd,
InputIt inBegin, InputIt inEnd)
{
uint64_t written = 0;
OutputIt out = outBegin;
for (InputIt in = inBegin; in != inEnd; ++in)
{
Block_[BlockBytesPacked_++] = *in;
if (BlockBytesPacked_ == Block_.Size())
{
BlockBytesPacked_ = 0;
Encryptor_.EncryptBlock(EncryptedBlock_.Begin(), EncryptedBlock_.End(),
Block_.Begin(), Block_.End());
out = EnsureCopy(out, outEnd, EncryptedBlock_.Begin(), EncryptedBlock_.End());
written += CipherT::BlockSize;
}
}
return written;
}
};
};
} // namespace Chaos::Cipher::Block::Mode
#endif // CHAOS_CIPHER_BLOCK_MODE_ECB_HPP
+2
View File
@@ -4,6 +4,8 @@
#include <cstdint> #include <cstdint>
#include <array> #include <array>
#include <string> #include <string>
#include <cstdio>
#include <cstddef>
#include "Hash.hpp" #include "Hash.hpp"
#include "Hasher.hpp" #include "Hasher.hpp"
+2
View File
@@ -4,6 +4,8 @@
#include <cstdint> #include <cstdint>
#include <array> #include <array>
#include <string> #include <string>
#include <cstdio>
#include <cstddef>
#include "Hash.hpp" #include "Hash.hpp"
#include "Hasher.hpp" #include "Hasher.hpp"
+4
View File
@@ -4,6 +4,10 @@
#include <cstdint> #include <cstdint>
#include <array> #include <array>
#include <string> #include <string>
#include <cstddef>
#include <cstdio>
#include <tuple>
#include <algorithm>
#include "Hash.hpp" #include "Hash.hpp"
#include "Hasher.hpp" #include "Hasher.hpp"
+6
View File
@@ -14,6 +14,12 @@ public:
Impl().Pad(begin, end); Impl().Pad(begin, end);
} }
template<typename InputIt>
auto ComputeUnpad(InputIt begin, InputIt end) const noexcept
{
return Impl().ComputeUnpad(begin, end);
}
protected: protected:
Padder() = default; Padder() = default;
+112
View File
@@ -1,9 +1,12 @@
#ifndef CHAOS_PADDING_PADDERPKCS7_HPP #ifndef CHAOS_PADDING_PADDERPKCS7_HPP
#define CHAOS_PADDING_PADDERPKCS7_HPP #define CHAOS_PADDING_PADDERPKCS7_HPP
#include <climits>
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <iterator> #include <iterator>
#include <limits> #include <limits>
#include <type_traits>
#include "Padding/Padder.hpp" #include "Padding/Padder.hpp"
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
@@ -19,6 +22,7 @@ public:
{ {
auto dist = std::distance(begin, end); auto dist = std::distance(begin, end);
// TODO: dist > 0
if (dist >= 0 && dist <= std::numeric_limits<uint8_t>::max()) if (dist >= 0 && dist <= std::numeric_limits<uint8_t>::max())
{ {
for (OutputIt it = begin; it != end; ++it) for (OutputIt it = begin; it != end; ++it)
@@ -31,6 +35,114 @@ public:
throw Service::ChaosException("PadderPkcs7::Pad(): invalid range"); 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 } // namespace Chaos::Padding
+1
View File
@@ -2,6 +2,7 @@
#define CHAOS_SERVICE_CHAOSEXCEPTION_HPP #define CHAOS_SERVICE_CHAOSEXCEPTION_HPP
#include <string> #include <string>
#include <utility>
namespace Chaos::Service namespace Chaos::Service
{ {
+1
View File
@@ -3,6 +3,7 @@
#include <array> #include <array>
#include <type_traits> #include <type_traits>
#include <cstddef>
namespace Chaos::Service namespace Chaos::Service
{ {
+10 -1
View File
@@ -18,6 +18,7 @@ set(ChaosTests_SOURCE Hash/Md4HasherTests.cpp
Cipher/Arc4GenTests.cpp Cipher/Arc4GenTests.cpp
Cipher/Arc4CryptTests.cpp Cipher/Arc4CryptTests.cpp
Cipher/DesCryptTests.cpp Cipher/DesCryptTests.cpp
Cipher/EcbModeTests.cpp
Padding/PadderPkcs7Tests.cpp Padding/PadderPkcs7Tests.cpp
Padding/PadderIso7816Tests.cpp Padding/PadderIso7816Tests.cpp
Service/SeArrayTests.cpp Service/SeArrayTests.cpp
@@ -31,11 +32,19 @@ target_include_directories(ChaosTests PRIVATE
target_compile_options(ChaosTests PRIVATE -Wunused -Werror=unused) 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_compile_options(ChaosTests PRIVATE --coverage)
target_link_options(ChaosTests PRIVATE --coverage) target_link_options(ChaosTests PRIVATE --coverage)
endif() 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) include(GoogleTest)
gtest_discover_tests(ChaosTests) gtest_discover_tests(ChaosTests)
+2 -1
View File
@@ -1,7 +1,8 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <vector> #include <vector>
#include <string>
#include <array> #include <array>
#include <cstdint>
#include <cstring>
#include "Cipher/Arc4/Arc4Crypt.hpp" #include "Cipher/Arc4/Arc4Crypt.hpp"
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
+5
View File
@@ -1,6 +1,11 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstdint>
#include <array>
#include <iterator>
#include <cstring>
#include "Cipher/Arc4/Arc4Gen.hpp" #include "Cipher/Arc4/Arc4Gen.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Cipher::Arc4; using namespace Chaos::Cipher::Arc4;
+23 -18
View File
@@ -1,7 +1,12 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstdint>
#include <array>
#include <vector>
#include "Cipher/Block/Des/DesCrypt.hpp" #include "Cipher/Block/Des/DesCrypt.hpp"
#include "Cipher/Block/Encryptor.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::Des;
using namespace Chaos::Cipher::Block; using namespace Chaos::Cipher::Block;
@@ -49,7 +54,7 @@ TEST(DesCryptTests, EncryptTest)
std::array<uint8_t, DesCrypt::BlockSize> result = {}; std::array<uint8_t, DesCrypt::BlockSize> result = {};
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesEncryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end()); enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end());
return result; return result;
@@ -94,7 +99,7 @@ TEST(DesCryptTests, EncryptUInt64BlockTest)
const std::array<uint8_t, DesCrypt::KeySize> & key) const const std::array<uint8_t, DesCrypt::KeySize> & key) const
{ {
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesEncryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
return enc.EncryptBlock(data); return enc.EncryptBlock(data);
} }
@@ -141,7 +146,7 @@ TEST(DesCryptTests, EncryptShortDataTest)
result.resize(DesCrypt::BlockSize, 0); result.resize(DesCrypt::BlockSize, 0);
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesEncryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end()); enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end());
return result; return result;
@@ -175,7 +180,7 @@ TEST(DesCryptTests, EncryptLongDataTest)
result.resize(DesCrypt::BlockSize, 0); result.resize(DesCrypt::BlockSize, 0);
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesEncryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end()); enc.EncryptBlock(result.begin(), result.end(), data.begin(), data.end());
return result; return result;
@@ -208,7 +213,7 @@ TEST(DesCryptTests, DecryptTest)
std::array<uint8_t, DesCrypt::BlockSize> result = {}; std::array<uint8_t, DesCrypt::BlockSize> result = {};
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesDecryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end()); dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end());
return result; return result;
@@ -253,7 +258,7 @@ TEST(DesCryptTests, DecryptUInt64BlockTest)
const std::array<uint8_t, DesCrypt::KeySize> & key) const const std::array<uint8_t, DesCrypt::KeySize> & key) const
{ {
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesDecryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
return dec.DecryptBlock(data); return dec.DecryptBlock(data);
} }
@@ -300,7 +305,7 @@ TEST(DesCryptTests, DecryptShortDataTest)
result.resize(DesCrypt::BlockSize, 0); result.resize(DesCrypt::BlockSize, 0);
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesDecryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end()); dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end());
return result; return result;
@@ -334,7 +339,7 @@ TEST(DesCryptTests, DecryptLongDataTest)
result.resize(DesCrypt::BlockSize, 0); result.resize(DesCrypt::BlockSize, 0);
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesDecryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end()); dec.DecryptBlock(result.begin(), result.end(), data.begin(), data.end());
return result; return result;
@@ -384,7 +389,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest)
std::array<uint8_t, 11> expected = { 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x00, 0x00, 0x00 }; std::array<uint8_t, 11> expected = { 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x7b, 0x00, 0x00, 0x00 };
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesEncryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end()); enc.EncryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
@@ -399,7 +404,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest)
std::array<uint8_t, 12> expected = { 0x00, 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x00, 0x00, 0x00, 0x00 }; std::array<uint8_t, 12> expected = { 0x00, 0x00, 0x00, 0x00, 0x42, 0x27, 0x88, 0xa6, 0x00, 0x00, 0x00, 0x00 };
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesEncryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end()); enc.EncryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
@@ -413,7 +418,7 @@ TEST(DesCryptTests, OutIteratorUsageEncryptTest)
std::array<uint8_t, 12> expected = {}; std::array<uint8_t, 12> expected = {};
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesEncryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
enc.EncryptBlock(fact.begin() + 3, fact.begin() + 3, data.begin(), data.end()); enc.EncryptBlock(fact.begin() + 3, fact.begin() + 3, data.begin(), data.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
@@ -431,7 +436,7 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
std::array<uint8_t, 11> expected = { 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x13, 0x00, 0x00, 0x00 }; std::array<uint8_t, 11> expected = { 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x13, 0x00, 0x00, 0x00 };
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesDecryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end()); dec.DecryptBlock(fact.begin() + 3, fact.end() - 3, data.begin(), data.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
@@ -446,7 +451,7 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
std::array<uint8_t, 12> expected = { 0x00, 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x00, 0x00, 0x00, 0x00 }; std::array<uint8_t, 12> expected = { 0x00, 0x00, 0x00, 0x00, 0x45, 0x69, 0x71, 0x17, 0x00, 0x00, 0x00, 0x00 };
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesDecryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end()); dec.DecryptBlock(fact.begin() + 4, fact.end() - 4, data.begin(), data.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
@@ -460,7 +465,7 @@ TEST(DesCryptTests, OutIteratorUsageDecryptTest)
std::array<uint8_t, 12> expected = {}; std::array<uint8_t, 12> expected = {};
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesDecryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
dec.DecryptBlock(fact.begin() + 3, fact.begin() + 3, data.begin(), data.end()); dec.DecryptBlock(fact.begin() + 3, fact.begin() + 3, data.begin(), data.end());
ASSERT_EQ(expected, fact); ASSERT_EQ(expected, fact);
@@ -486,7 +491,7 @@ TEST(DesCryptTests, EncryptThroughBaseTest)
std::vector<uint8_t> expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 }; std::vector<uint8_t> expected = { 0x85, 0xe8, 0x13, 0x54, 0x0f, 0x0a, 0xb4, 0x05 };
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesEncryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
ASSERT_EQ(expected, EncryptThroughBase(enc, data.begin(), data.end())); ASSERT_EQ(expected, EncryptThroughBase(enc, data.begin(), data.end()));
} }
@@ -505,7 +510,7 @@ TEST(DesCryptTests, EncryptUInt64BlockThroughBaseTest)
uint64_t expected = 0x85e813540f0ab405; uint64_t expected = 0x85e813540f0ab405;
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesEncryptor enc(desKey); DesCrypt::Encryptor enc(desKey);
ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data)); ASSERT_EQ(expected, EncryptUInt64BlockThroughBase(enc, data));
} }
@@ -529,7 +534,7 @@ TEST(DesCryptTests, DecryptThroughBaseTest)
std::vector<uint8_t> expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef }; std::vector<uint8_t> expected = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesDecryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
ASSERT_EQ(expected, DecryptThroughBase(dec, data.begin(), data.end())); ASSERT_EQ(expected, DecryptThroughBase(dec, data.begin(), data.end()));
} }
@@ -548,7 +553,7 @@ TEST(DesCryptTests, DecryptUInt64BlockThroughBaseTest)
uint64_t expected = 0x0123456789abcdef; uint64_t expected = 0x0123456789abcdef;
DesCrypt::Key desKey(key.begin(), key.end()); DesCrypt::Key desKey(key.begin(), key.end());
DesCrypt::DesDecryptor dec(desKey); DesCrypt::Decryptor dec(desKey);
ASSERT_EQ(expected, DecryptUInt64BlockThroughBase(dec, data)); ASSERT_EQ(expected, DecryptUInt64BlockThroughBase(dec, data));
} }
+439
View File
@@ -0,0 +1,439 @@
#include <gtest/gtest.h>
#include <cstring>
#include <cstdint>
#include <iterator>
#include <vector>
#include "Cipher/Block/Mode/Ecb.hpp"
#include "Cipher/Block/Des/DesCrypt.hpp"
#include "Padding/PadderPkcs7.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Cipher::Block::Mode;
using namespace Chaos::Cipher::Block;
using namespace Chaos::Padding;
TEST(EcbModeTests, EncryptTest)
{
struct Helper
{
std::vector<uint8_t> operator()(const std::vector<uint8_t> & data,
const std::vector<uint8_t> & key)
{
Des::DesCrypt::Key desKey(key.begin(), key.end());
EcbMode<Des::DesCrypt, PadderPkcs7>::Encryptor enc(desKey);
std::vector<uint8_t> out;
out.resize(enc.PredictMaxUpdateOutput(data.size()) +
enc.PredictMaxFinishOutput());
uint64_t written = enc.Update(out.begin(), out.end(), data.begin(), data.end());
written += enc.Finish(out.begin() + written, out.end());
out.resize(written);
return out;
}
};
Helper ecbEnc;
{
std::vector<uint8_t> data = {};
std::vector<uint8_t> key = { 0xaa, 0xbb, 0xaa, 0xbb, 0xaa, 0xbb, 0xaa, 0xbb };
std::vector<uint8_t> expected = { 0xef, 0xe2, 0x4d, 0xe9, 0x9d, 0xe7, 0x9b, 0xbf };
ASSERT_EQ(expected, ecbEnc(data, key));
}
{
const char * str = "smoke and mirrors";
std::vector<uint8_t> data(str, str + strlen(str));
std::vector<uint8_t> key = { 0x0c, 0xfd, 0x01, 0xa3, 0x60, 0xd0, 0x15, 0xa6 };
std::vector<uint8_t> expected = { 0x84, 0x39, 0xcd, 0xe5, 0x1f, 0x3d,
0x2a, 0x11, 0x63, 0xc3, 0x56, 0x28,
0xf5, 0x89, 0xdb, 0xc4, 0xa6, 0xca,
0x52, 0xb3, 0xa8, 0xce, 0x64, 0x2d };
ASSERT_EQ(expected, ecbEnc(data, key));
}
{
std::vector<uint8_t> data = { 0x51, 0x89, 0x9f, 0x0c, 0x32, 0x9b, 0x1d };
std::vector<uint8_t> key = { 0x28, 0x1c, 0xf3, 0x11, 0xce, 0xc6, 0xc2, 0x38 };
std::vector<uint8_t> expected = { 0x64, 0x32, 0x13, 0xe7, 0x31, 0x06, 0xc6, 0x6f };
ASSERT_EQ(expected, ecbEnc(data, key));
}
{
std::vector<uint8_t> data = { 0x32, 0x00, 0x93, 0x61, 0xc6, 0x9a, 0x25, 0x25, 0x2e };
std::vector<uint8_t> key = { 0x28, 0x1c, 0xf3, 0x11, 0xce, 0xc6, 0xc2, 0x38 };
std::vector<uint8_t> expected = { 0xee, 0x5b, 0x16, 0x33, 0xad, 0x2d, 0x32, 0x42,
0xdc, 0x0d, 0x9f, 0x87, 0x55, 0x5b, 0xb1, 0x53 };
ASSERT_EQ(expected, ecbEnc(data, key));
}
{
std::vector<uint8_t> data = { 0x4d, 0x0d, 0xfb, 0x59, 0xe6, 0xc4, 0xf2, 0xe7,
0xab, 0x46, 0x4f, 0x59, 0x48, 0x11, 0x1b, 0x4f };
std::vector<uint8_t> key = { 0x44, 0x0a, 0xb1, 0x6d, 0xe0, 0x67, 0x28, 0x9d };
std::vector<uint8_t> expected = { 0x55, 0xc1, 0x6f, 0xbb, 0xb0, 0xf3, 0xd3, 0xa6, 0xb7, 0xe6, 0x12, 0xb4,
0x49, 0x86, 0x64, 0x07, 0x82, 0xbb, 0x45, 0x46, 0x70, 0xae, 0x64, 0x5f };
ASSERT_EQ(expected, ecbEnc(data, key));
}
{
std::vector<uint8_t> data = { 0x8a, 0xe3, 0x85, 0xe0, 0x59, 0xe7, 0xee, 0xce, 0xc0, 0xbf, 0x50, 0x53,
0x95, 0xbf, 0x4b, 0xdd, 0x3b, 0x02, 0x2f, 0xcb, 0xd0, 0xd3, 0x62, 0x60 };
std::vector<uint8_t> key = { 0xe4, 0x60, 0xb9, 0xfa, 0x4b, 0x79, 0x6a, 0xf3 };
std::vector<uint8_t> expected = { 0xf4, 0x46, 0x31, 0xc5, 0xe8, 0xff, 0xdc, 0xac, 0x6c, 0x0e, 0xe8, 0x0e,
0x9f, 0xc2, 0xeb, 0xdf, 0xca, 0xac, 0xc3, 0xc0, 0xea, 0xba, 0x6e, 0xc2,
0x33, 0x5d, 0x16, 0x39, 0x74, 0xd1, 0x1d, 0xe1 };
ASSERT_EQ(expected, ecbEnc(data, key));
}
{
std::vector<uint8_t> data = { 0x96, 0x9f, 0x42, 0xd3, 0x47, 0x10, 0x4d, 0xb4, 0x89, 0x54,
0x5c, 0xad, 0x0a, 0xb9, 0xac, 0x4e, 0xbf, 0x00, 0x1a, 0x47,
0x3d, 0xd1, 0xe7, 0xb3, 0x70, 0xb7, 0x4e, 0xd9, 0x73, 0x7d,
0xb3, 0x24 };
std::vector<uint8_t> key = { 0xe9, 0xeb, 0x1f, 0xfa, 0x39, 0x95, 0xf0, 0xb9 };
std::vector<uint8_t> expected = { 0x53, 0x06, 0x73, 0x79, 0xb8, 0xd7, 0xbe, 0x5d, 0x6e, 0xa0,
0x1f, 0x03, 0x46, 0x07, 0xd6, 0x48, 0x65, 0x4f, 0x9a, 0x8f,
0x1a, 0xe4, 0xa7, 0x7f, 0x13, 0xc9, 0x06, 0x8c, 0x57, 0xbd,
0x79, 0x5e, 0x3d, 0x21, 0x02, 0x13, 0x5f, 0xae, 0xc9, 0x45 };
ASSERT_EQ(expected, ecbEnc(data, key));
}
{
std::vector<uint8_t> data = { 0xde, 0xbf, 0x28, 0x16, 0x0d, 0xfe, 0x64, 0xbf, 0xc6, 0xfd,
0x2d, 0x32, 0x45, 0xb0, 0xb7, 0x53, 0xde, 0x5a, 0x8e, 0x45,
0x87, 0xb3, 0x94, 0x0b, 0xdb, 0x9d, 0x23, 0x33, 0xd7, 0x8e,
0x63, 0x2d, 0x56 };
std::vector<uint8_t> key = { 0x3b, 0x4d, 0xdd, 0x0a, 0x28, 0xf5, 0x96, 0x36 };
std::vector<uint8_t> expected = { 0xc2, 0x88, 0xe5, 0x27, 0xd1, 0xb4, 0xa5, 0xf9, 0xb6, 0x02,
0x43, 0x3e, 0xae, 0x59, 0x43, 0xd4, 0xe5, 0x87, 0x20, 0xfe,
0xd0, 0x34, 0x28, 0x6a, 0xef, 0xb1, 0x9e, 0x5a, 0xdf, 0xfb,
0xf7, 0x94, 0xb4, 0x0c, 0xc2, 0x92, 0x4e, 0x73, 0xda, 0xca };
ASSERT_EQ(expected, ecbEnc(data, key));
}
{
std::vector<uint8_t> data = { 0x7e, 0x2c, 0x68, 0x20, 0x22, 0xb1, 0xdd, 0x6d, 0x6e, 0x8b, 0x8b,
0x30, 0x99, 0xaf, 0x79, 0x2f, 0x6c, 0x95, 0x11, 0x8d, 0xd4, 0x2f,
0xcf, 0x1d, 0xe6, 0xa0, 0xc7, 0x73, 0x48, 0xb1, 0x65, 0xa9, 0xf6,
0xc5, 0x1c, 0x42, 0x2d, 0xdd, 0xcf, 0xf5, 0xd4, 0xee, 0xa5, 0xa7,
0x69, 0xf9, 0x27, 0x93, 0xce };
std::vector<uint8_t> key = { 0x3c, 0xd0, 0xab, 0xd3, 0xb4, 0xc3, 0xac, 0x53 };
std::vector<uint8_t> expected = { 0xf7, 0x44, 0x18, 0x9b, 0x9d, 0x8d, 0xe1, 0x14, 0xcd, 0x0a, 0xdb, 0xbd,
0x5b, 0xd1, 0x42, 0x6e, 0x17, 0x73, 0x60, 0x9e, 0x14, 0xad, 0xd2, 0x4c,
0x33, 0x30, 0xdd, 0xb9, 0xf3, 0x46, 0xd0, 0x8d, 0xa6, 0x37, 0x49, 0xd7,
0x90, 0x23, 0x05, 0xcc, 0x56, 0xde, 0x1f, 0xde, 0x39, 0x6c, 0x04, 0x97,
0x1d, 0xaf, 0xb5, 0xff, 0xab, 0x47, 0xc8, 0x51 };
ASSERT_EQ(expected, ecbEnc(data, key));
}
}
TEST(EcbModeTests, EncryptManyUpdatesTest)
{
struct Helper
{
Helper(const std::vector<uint8_t> & key)
: Key_(key.begin(), key.end())
, Enc_(Key_)
, Written_(0)
{ }
void Update(const std::vector<uint8_t> & data)
{
Out_.resize(Out_.size() + Enc_.PredictMaxUpdateOutput(data.size()));
Written_ += Enc_.Update(Out_.begin() + Written_, Out_.end(),
data.begin(), data.end());
}
std::vector<uint8_t> Finish()
{
Out_.resize(Out_.size() + Enc_.PredictMaxFinishOutput());
Written_ += Enc_.Finish(Out_.begin() + Written_, Out_.end());
Out_.resize(Written_);
return Out_;
}
Des::DesCrypt::Key Key_;
EcbMode<Des::DesCrypt, PadderPkcs7>::Encryptor Enc_;
std::vector<uint8_t> Out_;
uint64_t Written_;
};
{
std::vector<uint8_t> key = { 0xab, 0x39, 0x20, 0xea, 0xaa, 0x95, 0x1c, 0x90 };
std::vector<uint8_t> data1 = { 0x97, 0x65, 0xe2, 0xb1, 0xae, 0x3e, 0x55, 0xff, 0x1d };
std::vector<uint8_t> data2 = { 0xac, 0x8a, 0xa6, 0xac, 0xa5, 0x9a, 0xf9, 0xb6 };
std::vector<uint8_t> data3 = { 0x3c, 0xba, 0x95, 0x5e, 0x78, 0x29, 0x22, 0x7c, 0x12, 0x7e, 0x4c };
std::vector<uint8_t> expected = { 0xf4, 0xde, 0x9a, 0xe4, 0x78, 0x8c, 0xf2, 0xe6, 0x75, 0x34, 0xe7,
0x9f, 0xe8, 0x16, 0x09, 0x12, 0x55, 0x75, 0x5b, 0xe4, 0x21, 0x65,
0x98, 0x0e, 0xc6, 0x7a, 0x62, 0xf6, 0x88, 0xcd, 0x3e, 0x83 };
Helper ecbEnc(key);
ecbEnc.Update(data1);
ecbEnc.Update(data2);
ecbEnc.Update(data3);
ASSERT_EQ(expected, ecbEnc.Finish());
}
{
std::vector<uint8_t> key = { 0xd5, 0xc1, 0x3a, 0xcc, 0x6f, 0x86, 0x1d, 0x4b };
std::vector<uint8_t> data1 = { 0x43, 0x73, 0x1b, 0xde, 0xbd, 0xfe, 0x16, 0x58 };
std::vector<uint8_t> data2 = { 0x8c, 0x16, 0x13, 0x53, 0x22, 0x66, 0xb4, 0xf2 };
std::vector<uint8_t> data3 = { 0x25, 0xcf, 0xcd, 0xab, 0x7d, 0x29, 0x9a, 0xd7 };
std::vector<uint8_t> expected = { 0x5f, 0x18, 0x61, 0x15, 0x72, 0x75, 0x73, 0xb3, 0x44,
0xa5, 0xf0, 0x46, 0xa9, 0x4b, 0x7e, 0x5e, 0x5b, 0x88,
0x46, 0x07, 0x5e, 0x33, 0x16, 0x01, 0x69, 0x13, 0x76,
0x8b, 0x7f, 0xbc, 0xf2, 0x75 };
Helper ecbEnc(key);
ecbEnc.Update(data1);
ecbEnc.Update(data2);
ecbEnc.Update(data3);
ASSERT_EQ(expected, ecbEnc.Finish());
}
{
std::vector<uint8_t> key = { 0xc0, 0x98, 0x56, 0xb3, 0x27, 0xc9, 0x78, 0x89 };
std::vector<uint8_t> data1 = { 0xb7 };
std::vector<uint8_t> data2 = { 0xb3 };
std::vector<uint8_t> data3 = { 0x95, 0x56, 0xc0, 0x9f, 0x74, 0xd0, 0x49, 0xb5 };
std::vector<uint8_t> data4 = { 0x15 };
std::vector<uint8_t> expected = { 0xf9, 0x79, 0x6d, 0x46, 0xb6, 0x4a, 0xa8, 0xaa, 0x82,
0xb9, 0xca, 0x65, 0xe9, 0x3c, 0xa8, 0xdc };
Helper ecbEnc(key);
ecbEnc.Update(data1);
ecbEnc.Update(data2);
ecbEnc.Update(data3);
ecbEnc.Update(data4);
ASSERT_EQ(expected, ecbEnc.Finish());
}
{
std::vector<uint8_t> key = { 0xcb, 0x22, 0x90, 0x27, 0x87, 0x42, 0xd1, 0x58 };
std::vector<uint8_t> data1 = { 0xca, 0x40, 0xeb, 0x08, 0x75, 0x62, 0x41 };
std::vector<uint8_t> data2 = { 0x16, 0xa4, 0x12, 0x21, 0x3d, 0x5a, 0xf2, 0xb4, 0xa7, 0xb5, 0xa7 };
std::vector<uint8_t> data3 = { 0x59, 0x51, 0xd0, 0x41, 0x21, 0xf0, 0x2b, 0x76,
0xd8, 0xe4, 0x60, 0xb5, 0xab, 0xd8, 0x25, 0x29 };
std::vector<uint8_t> data4 = { 0x94, 0x9f, 0xed };
std::vector<uint8_t> data5 = { 0x1a, 0xa1, 0x57, 0x35, 0x2c };
std::vector<uint8_t> expected = { 0xfb, 0x9d, 0x21, 0x86, 0x9b, 0xf1, 0x79, 0xeb, 0x6c,
0xcf, 0xcc, 0xe7, 0x55, 0x48, 0x05, 0x42, 0x37, 0x23,
0xcc, 0xda, 0x82, 0x8f, 0xe7, 0x5a, 0x08, 0x85, 0x00,
0x0b, 0x38, 0x5f, 0x54, 0x53, 0xf9, 0x40, 0x59, 0xc7,
0xf3, 0x06, 0x10, 0x10, 0xed, 0xfc, 0x76, 0xff, 0xe6,
0x55, 0xb0, 0xe1 };
Helper ecbEnc(key);
ecbEnc.Update(data1);
ecbEnc.Update(data2);
ecbEnc.Update(data3);
ecbEnc.Update(data4);
ecbEnc.Update(data5);
ASSERT_EQ(expected, ecbEnc.Finish());
}
}
TEST(EcbModeTests, EncryptLongInputTest)
{
struct Helper
{
Helper(const std::vector<uint8_t> & key)
: Key_(key.begin(), key.end())
, Enc_(Key_)
, Written_(0)
{ }
void Update(const std::vector<uint8_t> & data)
{
Out_.resize(Out_.size() + Enc_.PredictMaxUpdateOutput(data.size()));
Written_ += Enc_.Update(Out_.begin() + Written_, Out_.end(),
data.begin(), data.end());
}
void Update(std::vector<uint8_t>::const_iterator begin, std::vector<uint8_t>::const_iterator end)
{
Out_.resize(Out_.size() + Enc_.PredictMaxUpdateOutput(std::distance(begin, end)));
Written_ += Enc_.Update(Out_.begin() + Written_, Out_.end(), begin, end);
}
std::vector<uint8_t> Finish()
{
Out_.resize(Out_.size() + Enc_.PredictMaxFinishOutput());
Written_ += Enc_.Finish(Out_.begin() + Written_, Out_.end());
Out_.resize(Written_);
return Out_;
}
Des::DesCrypt::Key Key_;
EcbMode<Des::DesCrypt, PadderPkcs7>::Encryptor Enc_;
std::vector<uint8_t> Out_;
uint64_t Written_;
};
{
std::vector<uint8_t> key = { 0x27, 0x07, 0x7c, 0xc9, 0xd2, 0xbe, 0x76, 0x6c };
std::vector<uint8_t> data(2048, 0xab);
std::vector<uint8_t> expected;
{
std::vector<uint8_t> block = { 0x90, 0xf0, 0x72, 0xae, 0xcc, 0x98, 0x93, 0x8d };
for (int i = 0; i < 256; ++i)
{
expected.insert(expected.end(), block.begin(), block.end());
}
std::vector<uint8_t> lastBlock = { 0xce, 0x7c, 0x12, 0x8e, 0x8c, 0xc0, 0x88, 0x02 };
expected.insert(expected.end(), lastBlock.begin(), lastBlock.end());
}
Helper ecbEnc(key);
ecbEnc.Update(data);
ASSERT_EQ(expected, ecbEnc.Finish());
}
{
std::vector<uint8_t> key = { 0xcc, 0xc5, 0x08, 0x98, 0xe9, 0x8c, 0xeb, 0x23 };
std::vector<uint8_t> data(4095, 0x7a);
std::vector<uint8_t> expected;
{
std::vector<uint8_t> block = { 0xb7, 0x9f, 0x07, 0x2c, 0xe9, 0x54, 0x83, 0xc5 };
for (int i = 0; i < 2048; ++i)
{
expected.insert(expected.end(), block.begin(), block.end());
}
std::vector<uint8_t> lastBlock = { 0x14, 0x59, 0x29, 0x72, 0x45, 0xb3, 0x03, 0xb3 };
expected.insert(expected.end(), lastBlock.begin(), lastBlock.end());
}
Helper ecbEnc(key);
ecbEnc.Update(data.begin(), data.begin() + 2340);
ecbEnc.Update(data.begin(), data.begin() + 1171);
ecbEnc.Update(data.begin(), data.begin() + 2925);
ecbEnc.Update(data.begin(), data.begin() + 1);
ecbEnc.Update(data.begin(), data.begin() + 4095);
ecbEnc.Update(data.begin(), data.begin() + 586);
ecbEnc.Update(data.begin(), data.begin() + 3510);
ecbEnc.Update(data.begin(), data.begin() + 1756);
ASSERT_EQ(expected, ecbEnc.Finish());
}
}
TEST(EcbModeTests, EncryptInsufficientBufferTest)
{
std::vector<uint8_t> key = { 0xcc, 0xc5, 0x08, 0x98, 0xe9, 0x8c, 0xeb, 0x23 };
Des::DesCrypt::Key desKey(key.begin(), key.end());
{
std::vector<uint8_t> data(8, 0xaa);
std::vector<uint8_t> out;
EcbMode<Des::DesCrypt, PadderPkcs7>::Encryptor enc(desKey);
ASSERT_THROW(enc.Update(out.begin(), out.end(), data.begin(), data.end()),
Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data(8, 0xaa);
std::vector<uint8_t> out(7, 0);
EcbMode<Des::DesCrypt, PadderPkcs7>::Encryptor enc(desKey);
ASSERT_THROW(enc.Update(out.begin(), out.end(), data.begin(), data.end()),
Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data1(7, 0xaa);
std::vector<uint8_t> data2(2, 0xbb);
std::vector<uint8_t> out1(7, 0);
std::vector<uint8_t> out2(2, 0);
EcbMode<Des::DesCrypt, PadderPkcs7>::Encryptor enc(desKey);
enc.Update(out1.begin(), out1.end(), data1.begin(), data1.end());
ASSERT_THROW(enc.Update(out2.begin(), out2.end(), data2.begin(), data2.end()),
Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data(8, 0xaa);
std::vector<uint8_t> out(8, 0);
EcbMode<Des::DesCrypt, PadderPkcs7>::Encryptor enc(desKey);
uint64_t written = enc.Update(out.begin(), out.end(), data.begin(), data.end());
ASSERT_THROW(written += enc.Finish(out.begin() + written, out.end()),
Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data(15, 0xaa);
std::vector<uint8_t> out(15, 0);
EcbMode<Des::DesCrypt, PadderPkcs7>::Encryptor enc(desKey);
uint64_t written = enc.Update(out.begin(), out.end(), data.begin(), data.end());
ASSERT_THROW(written += enc.Finish(out.begin() + written, out.end()),
Chaos::Service::ChaosException);
}
}
+2
View File
@@ -1,4 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstring>
#include <string>
#include "Hash/Md4.hpp" #include "Hash/Md4.hpp"
+2
View File
@@ -1,4 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstring>
#include <string>
#include "Hash/Md5.hpp" #include "Hash/Md5.hpp"
+2
View File
@@ -1,4 +1,6 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstring>
#include <string>
#include "Hash/Sha1.hpp" #include "Hash/Sha1.hpp"
+4
View File
@@ -1,8 +1,12 @@
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <cstring>
#include <string>
#include <cstdint>
#include "Hash/Md5.hpp" #include "Hash/Md5.hpp"
#include "Hash/Sha1.hpp" #include "Hash/Sha1.hpp"
#include "Mac/Hmac.hpp" #include "Mac/Hmac.hpp"
#include "Service/ChaosException.hpp"
using namespace Chaos::Mac::Hmac; using namespace Chaos::Mac::Hmac;
using namespace Chaos::Hash::Md5; using namespace Chaos::Hash::Md5;
@@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include "Padding/PadderIso7816.hpp" #include "Padding/PadderIso7816.hpp"
#include "Padding/Padder.hpp"
using namespace Chaos::Padding; using namespace Chaos::Padding;
+208
View File
@@ -4,6 +4,7 @@
#include <vector> #include <vector>
#include "Padding/PadderPkcs7.hpp" #include "Padding/PadderPkcs7.hpp"
#include "Padding/Padder.hpp"
#include "Service/ChaosException.hpp" #include "Service/ChaosException.hpp"
using namespace Chaos::Padding; using namespace Chaos::Padding;
@@ -141,3 +142,210 @@ TEST(PadPkcs7Tests, PadThroughBaseTest)
ASSERT_EQ(expected, fact); 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 <gtest/gtest.h>
#include <cstdint> #include <cstdint>
#include <iterator> #include <iterator>
#include <cstddef>
#include "Service/SeArray.hpp" #include "Service/SeArray.hpp"