Add ECB-mode decryptor draft implementation.
Chaos Ci / test (push) Successful in 2m59s
Chaos Ci / benchmark (push) Successful in 1m31s

This commit is contained in:
hashlag
2026-07-29 02:25:51 +03:00
parent 78ab3a8c27
commit 6ae4df4bfc
2 changed files with 675 additions and 0 deletions
+146
View File
@@ -106,6 +106,152 @@ public:
return written;
}
};
class Decryptor
{
public:
Decryptor(const typename CipherT::Key & key)
: Decryptor_(key)
, BlockBytesPacked_(0)
, PreviousBlockSaved_(false)
{ }
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)
{
if (BlockBytesPacked_ != 0)
{
throw Service::ChaosException("EcbMode<>::Decryptor: ciphertext size "
"is not a multiple of the algorithm's "
"block size");
}
if (!PreviousBlockSaved_)
{
return 0;
}
auto unpadResult = PadderT::ComputeUnpad(PreviousBlock_.Begin(),
PreviousBlock_.End());
if (!unpadResult.IsOkay_)
{
throw Service::ChaosException("EcbMode<>::Decryptor: invalid ciphertext");
}
const uint64_t lastChunkSize = CipherT::BlockSize - unpadResult.PadSize_;
EnsureCopy(outBegin, outEnd,
PreviousBlock_.Begin(),
PreviousBlock_.Begin() + lastChunkSize);
return lastChunkSize;
}
private:
using BlockArray = Service::SeArray<uint8_t, CipherT::BlockSize>;
typename CipherT::Decryptor Decryptor_;
uint64_t BlockBytesPacked_;
BlockArray Block_;
BlockArray DecryptedBlock_;
bool PreviousBlockSaved_;
BlockArray PreviousBlock_;
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<>::Decryptor: insufficient output "
"buffer size");
}
return out;
}
template<typename OutputIt>
struct DelayedFlushResult
{
OutputIt Next_;
uint64_t Written_;
};
template<typename OutputIt>
DelayedFlushResult<OutputIt> DelayedFlush(OutputIt outBegin, OutputIt outEnd)
{
if (PreviousBlockSaved_)
{
OutputIt result = EnsureCopy(outBegin, outEnd,
PreviousBlock_.Begin(), PreviousBlock_.End());
PreviousBlock_ = DecryptedBlock_;
return { .Next_ = result, .Written_ = CipherT::BlockSize };
}
else
{
PreviousBlock_ = DecryptedBlock_;
PreviousBlockSaved_ = true;
return { .Next_ = outBegin, .Written_ = 0 };
}
}
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;
Decryptor_.DecryptBlock(DecryptedBlock_.Begin(), DecryptedBlock_.End(),
Block_.Begin(), Block_.End());
auto result = DelayedFlush(out, outEnd);
out = result.Next_;
written += result.Written_;
}
}
return written;
}
};
};
} // namespace Chaos::Cipher::Block::Mode
+529
View File
@@ -437,3 +437,532 @@ TEST(EcbModeTests, EncryptInsufficientBufferTest)
Chaos::Service::ChaosException);
}
}
TEST(EcbModeTests, DecryptTest)
{
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>::Decryptor dec(desKey);
std::vector<uint8_t> out;
out.resize(dec.PredictMaxUpdateOutput(data.size()) +
dec.PredictMaxFinishOutput());
uint64_t written = dec.Update(out.begin(), out.end(), data.begin(), data.end());
written += dec.Finish(out.begin() + written, out.end());
out.resize(written);
return out;
}
};
Helper ecbDec;
{
std::vector<uint8_t> data = { };
std::vector<uint8_t> key = { 0xaa, 0xbb, 0xaa, 0xbb, 0xaa, 0xbb, 0xaa, 0xbb };
std::vector<uint8_t> expected = { };
ASSERT_EQ(expected, ecbDec(data, key));
}
{
std::vector<uint8_t> data = { 0xef, 0xe2, 0x4d, 0xe9, 0x9d, 0xe7, 0x9b, 0xbf };
std::vector<uint8_t> key = { 0xaa, 0xbb, 0xaa, 0xbb, 0xaa, 0xbb, 0xaa, 0xbb };
std::vector<uint8_t> expected = { };
ASSERT_EQ(expected, ecbDec(data, key));
}
{
std::vector<uint8_t> data = { 0x84, 0x39, 0xcd, 0xe5, 0x1f, 0x3d,
0x2a, 0x11, 0x63, 0xc3, 0x56, 0x28,
0xf5, 0x89, 0xdb, 0xc4, 0xa6, 0xca,
0x52, 0xb3, 0xa8, 0xce, 0x64, 0x2d };
std::vector<uint8_t> key = { 0x0c, 0xfd, 0x01, 0xa3, 0x60, 0xd0, 0x15, 0xa6 };
const char * str = "smoke and mirrors";
std::vector<uint8_t> expected(str, str + strlen(str));
ASSERT_EQ(expected, ecbDec(data, key));
}
{
std::vector<uint8_t> data = { 0x64, 0x32, 0x13, 0xe7, 0x31, 0x06, 0xc6, 0x6f };
std::vector<uint8_t> key = { 0x28, 0x1c, 0xf3, 0x11, 0xce, 0xc6, 0xc2, 0x38 };
std::vector<uint8_t> expected = { 0x51, 0x89, 0x9f, 0x0c, 0x32, 0x9b, 0x1d };
ASSERT_EQ(expected, ecbDec(data, key));
}
{
std::vector<uint8_t> data = { 0xee, 0x5b, 0x16, 0x33, 0xad, 0x2d, 0x32, 0x42,
0xdc, 0x0d, 0x9f, 0x87, 0x55, 0x5b, 0xb1, 0x53 };
std::vector<uint8_t> key = { 0x28, 0x1c, 0xf3, 0x11, 0xce, 0xc6, 0xc2, 0x38 };
std::vector<uint8_t> expected = { 0x32, 0x00, 0x93, 0x61, 0xc6, 0x9a, 0x25, 0x25, 0x2e };
ASSERT_EQ(expected, ecbDec(data, key));
}
{
std::vector<uint8_t> data = { 0x55, 0xc1, 0x6f, 0xbb, 0xb0, 0xf3, 0xd3, 0xa6, 0xb7, 0xe6, 0x12, 0xb4,
0x49, 0x86, 0x64, 0x07, 0x82, 0xbb, 0x45, 0x46, 0x70, 0xae, 0x64, 0x5f };
std::vector<uint8_t> key = { 0x44, 0x0a, 0xb1, 0x6d, 0xe0, 0x67, 0x28, 0x9d };
std::vector<uint8_t> expected = { 0x4d, 0x0d, 0xfb, 0x59, 0xe6, 0xc4, 0xf2, 0xe7,
0xab, 0x46, 0x4f, 0x59, 0x48, 0x11, 0x1b, 0x4f };
ASSERT_EQ(expected, ecbDec(data, key));
}
{
std::vector<uint8_t> data = { 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 };
std::vector<uint8_t> key = { 0xe4, 0x60, 0xb9, 0xfa, 0x4b, 0x79, 0x6a, 0xf3 };
std::vector<uint8_t> expected = { 0x8a, 0xe3, 0x85, 0xe0, 0x59, 0xe7, 0xee, 0xce, 0xc0, 0xbf, 0x50, 0x53,
0x95, 0xbf, 0x4b, 0xdd, 0x3b, 0x02, 0x2f, 0xcb, 0xd0, 0xd3, 0x62, 0x60 };
ASSERT_EQ(expected, ecbDec(data, key));
}
{
std::vector<uint8_t> data = { 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 };
std::vector<uint8_t> key = { 0xe9, 0xeb, 0x1f, 0xfa, 0x39, 0x95, 0xf0, 0xb9 };
std::vector<uint8_t> expected = { 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 };
ASSERT_EQ(expected, ecbDec(data, key));
}
{
std::vector<uint8_t> data = { 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 };
std::vector<uint8_t> key = { 0x3b, 0x4d, 0xdd, 0x0a, 0x28, 0xf5, 0x96, 0x36 };
std::vector<uint8_t> expected = { 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 };
ASSERT_EQ(expected, ecbDec(data, key));
}
{
std::vector<uint8_t> data = { 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 };
std::vector<uint8_t> key = { 0x3c, 0xd0, 0xab, 0xd3, 0xb4, 0xc3, 0xac, 0x53 };
std::vector<uint8_t> expected = { 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 };
ASSERT_EQ(expected, ecbDec(data, key));
}
}
TEST(EcbModeTests, DecryptManyUpdatesTest)
{
struct Helper
{
Helper(const std::vector<uint8_t> & key)
: Key_(key.begin(), key.end())
, Dec_(Key_)
, Written_(0)
{ }
void Update(const std::vector<uint8_t> & data)
{
Out_.resize(Out_.size() + Dec_.PredictMaxUpdateOutput(data.size()));
Written_ += Dec_.Update(Out_.begin() + Written_, Out_.end(),
data.begin(), data.end());
}
std::vector<uint8_t> Finish()
{
Out_.resize(Out_.size() + Dec_.PredictMaxFinishOutput());
Written_ += Dec_.Finish(Out_.begin() + Written_, Out_.end());
Out_.resize(Written_);
return Out_;
}
Des::DesCrypt::Key Key_;
EcbMode<Des::DesCrypt, PadderPkcs7>::Decryptor Dec_;
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 = { 0xf4, 0xde, 0x9a, 0xe4, 0x78, 0x8c, 0xf2, 0xe6, 0x75 };
std::vector<uint8_t> data2 = { 0x34, 0xe7, 0x9f, 0xe8, 0x16, 0x09, 0x12, 0x55 };
std::vector<uint8_t> data3 = { 0x75, 0x5b, 0xe4, 0x21, 0x65,
0x98, 0x0e, 0xc6, 0x7a, 0x62, 0xf6, 0x88, 0xcd, 0x3e, 0x83 };
std::vector<uint8_t> expected = { 0x97, 0x65, 0xe2, 0xb1, 0xae, 0x3e, 0x55, 0xff, 0x1d,
0xac, 0x8a, 0xa6, 0xac, 0xa5, 0x9a, 0xf9, 0xb6,
0x3c, 0xba, 0x95, 0x5e, 0x78, 0x29, 0x22, 0x7c, 0x12, 0x7e, 0x4c };
Helper ecbDec(key);
ecbDec.Update(data1);
ecbDec.Update(data2);
ecbDec.Update(data3);
ASSERT_EQ(expected, ecbDec.Finish());
}
{
std::vector<uint8_t> key = { 0xd5, 0xc1, 0x3a, 0xcc, 0x6f, 0x86, 0x1d, 0x4b };
std::vector<uint8_t> data1 = { 0x5f, 0x18, 0x61, 0x15, 0x72, 0x75, 0x73, 0xb3 };
std::vector<uint8_t> data2 = { 0x44, 0xa5, 0xf0, 0x46, 0xa9, 0x4b, 0x7e, 0x5e };
std::vector<uint8_t> data3 = { 0x5b, 0x88, 0x46, 0x07, 0x5e, 0x33, 0x16, 0x01 };
std::vector<uint8_t> data4 = { 0x69, 0x13, 0x76, 0x8b, 0x7f, 0xbc, 0xf2, 0x75 };
std::vector<uint8_t> expected = { 0x43, 0x73, 0x1b, 0xde, 0xbd, 0xfe, 0x16, 0x58,
0x8c, 0x16, 0x13, 0x53, 0x22, 0x66, 0xb4, 0xf2,
0x25, 0xcf, 0xcd, 0xab, 0x7d, 0x29, 0x9a, 0xd7 };
Helper ecbDec(key);
ecbDec.Update(data1);
ecbDec.Update(data2);
ecbDec.Update(data3);
ecbDec.Update(data4);
ASSERT_EQ(expected, ecbDec.Finish());
}
{
std::vector<uint8_t> key = { 0xc0, 0x98, 0x56, 0xb3, 0x27, 0xc9, 0x78, 0x89 };
std::vector<uint8_t> data1 = { 0xf9 };
std::vector<uint8_t> data2 = { 0x79 };
std::vector<uint8_t> data3 = { 0x6d, 0x46, 0xb6, 0x4a, 0xa8, 0xaa, 0x82, 0xb9 };
std::vector<uint8_t> data4 = { 0xca };
std::vector<uint8_t> data5 = { 0x65, 0xe9, 0x3c, 0xa8, 0xdc };
std::vector<uint8_t> expected = { 0xb7, 0xb3, 0x95, 0x56, 0xc0, 0x9f, 0x74, 0xd0,
0x49, 0xb5, 0x15 };
Helper ecbDec(key);
ecbDec.Update(data1);
ecbDec.Update(data2);
ecbDec.Update(data3);
ecbDec.Update(data4);
ecbDec.Update(data5);
ASSERT_EQ(expected, ecbDec.Finish());
}
{
std::vector<uint8_t> key = { 0xcb, 0x22, 0x90, 0x27, 0x87, 0x42, 0xd1, 0x58 };
std::vector<uint8_t> data1 = { 0xfb, 0x9d, 0x21, 0x86, 0x9b, 0xf1, 0x79 };
std::vector<uint8_t> data2 = { 0xeb, 0x6c, 0xcf, 0xcc, 0xe7, 0x55, 0x48, 0x05, 0x42, 0x37, 0x23 };
std::vector<uint8_t> data3 = { 0xcc, 0xda, 0x82, 0x8f, 0xe7, 0x5a, 0x08, 0x85,
0x00, 0x0b, 0x38, 0x5f, 0x54, 0x53, 0xf9, 0x40 };
std::vector<uint8_t> data4 = { 0x59, 0xc7, 0xf3 };
std::vector<uint8_t> data5 = { 0x06, 0x10, 0x10, 0xed, 0xfc };
std::vector<uint8_t> data6 = { 0x76, 0xff, 0xe6, 0x55, 0xb0, 0xe1 };
std::vector<uint8_t> expected = { 0xca, 0x40, 0xeb, 0x08, 0x75, 0x62, 0x41, 0x16,
0xa4, 0x12, 0x21, 0x3d, 0x5a, 0xf2, 0xb4, 0xa7,
0xb5, 0xa7, 0x59, 0x51, 0xd0, 0x41, 0x21, 0xf0,
0x2b, 0x76, 0xd8, 0xe4, 0x60, 0xb5, 0xab, 0xd8,
0x25, 0x29, 0x94, 0x9f, 0xed, 0x1a, 0xa1, 0x57,
0x35, 0x2c };
Helper ecbDec(key);
ecbDec.Update(data1);
ecbDec.Update(data2);
ecbDec.Update(data3);
ecbDec.Update(data4);
ecbDec.Update(data5);
ecbDec.Update(data6);
ASSERT_EQ(expected, ecbDec.Finish());
}
}
TEST(EcbModeTests, DecryptLongInputTest)
{
struct Helper
{
Helper(const std::vector<uint8_t> & key)
: Key_(key.begin(), key.end())
, Dec_(Key_)
, Written_(0)
{ }
void Update(const std::vector<uint8_t> & data)
{
Out_.resize(Out_.size() + Dec_.PredictMaxUpdateOutput(data.size()));
Written_ += Dec_.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() + Dec_.PredictMaxUpdateOutput(std::distance(begin, end)));
Written_ += Dec_.Update(Out_.begin() + Written_, Out_.end(), begin, end);
}
std::vector<uint8_t> Finish()
{
Out_.resize(Out_.size() + Dec_.PredictMaxFinishOutput());
Written_ += Dec_.Finish(Out_.begin() + Written_, Out_.end());
Out_.resize(Written_);
return Out_;
}
Des::DesCrypt::Key Key_;
EcbMode<Des::DesCrypt, PadderPkcs7>::Decryptor Dec_;
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;
{
std::vector<uint8_t> block = { 0x90, 0xf0, 0x72, 0xae, 0xcc, 0x98, 0x93, 0x8d };
for (int i = 0; i < 256; ++i)
{
data.insert(data.end(), block.begin(), block.end());
}
std::vector<uint8_t> lastBlock = { 0xce, 0x7c, 0x12, 0x8e, 0x8c, 0xc0, 0x88, 0x02 };
data.insert(data.end(), lastBlock.begin(), lastBlock.end());
}
std::vector<uint8_t> expected(2048, 0xab);
Helper ecbDec(key);
ecbDec.Update(data);
ASSERT_EQ(expected, ecbDec.Finish());
}
{
std::vector<uint8_t> key = { 0xcc, 0xc5, 0x08, 0x98, 0xe9, 0x8c, 0xeb, 0x23 };
std::vector<uint8_t> data;
{
std::vector<uint8_t> block = { 0xb7, 0x9f, 0x07, 0x2c, 0xe9, 0x54, 0x83, 0xc5 };
for (int i = 0; i < 2048; ++i)
{
data.insert(data.end(), block.begin(), block.end());
}
std::vector<uint8_t> lastBlock = { 0x14, 0x59, 0x29, 0x72, 0x45, 0xb3, 0x03, 0xb3 };
data.insert(data.end(), lastBlock.begin(), lastBlock.end());
}
std::vector<uint8_t> expected(16384, 0x7a);
Helper ecbDec(key);
ecbDec.Update(data.begin(), data.begin() + 2340);
ecbDec.Update(data.begin() + 2340, data.begin() + 3511);
ecbDec.Update(data.begin() + 3511, data.begin() + 6436);
ecbDec.Update(data.begin() + 6436, data.begin() + 6437);
ecbDec.Update(data.begin() + 6437, data.begin() + 10532);
ecbDec.Update(data.begin() + 10532, data.begin() + 11118);
ecbDec.Update(data.begin() + 11118, data.begin() + 14628);
ecbDec.Update(data.begin() + 14628, data.begin() + 16384);
ecbDec.Update(data.begin() + 16384, data.end());
ASSERT_EQ(expected, ecbDec.Finish());
}
}
TEST(EcbModeTests, DecryptInsufficientBufferTest)
{
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 = { 0x65, 0x35, 0xa5, 0x01, 0x29, 0xf4, 0xfd, 0xea,
0x14, 0x59, 0x29, 0x72, 0x45, 0xb3, 0x03, 0xb3 };
std::vector<uint8_t> out;
EcbMode<Des::DesCrypt, PadderPkcs7>::Decryptor dec(desKey);
ASSERT_THROW(dec.Update(out.begin(), out.end(), data.begin(), data.end()),
Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data = { 0x65, 0x35, 0xa5, 0x01, 0x29, 0xf4, 0xfd, 0xea,
0x65, 0x35, 0xa5, 0x01, 0x29, 0xf4, 0xfd, 0xea,
0x14, 0x59, 0x29, 0x72, 0x45, 0xb3, 0x03, 0xb3 };
std::vector<uint8_t> out(8, 0);
EcbMode<Des::DesCrypt, PadderPkcs7>::Decryptor dec(desKey);
ASSERT_THROW(dec.Update(out.begin(), out.end(), data.begin(), data.end()),
Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data1 = { 0x65, 0x35, 0xa5, 0x01, 0x29, 0xf4, 0xfd, 0xea,
0x65, 0x35, 0xa5, 0x01, 0x29, 0xf4, 0xfd, 0xea,
0x65, 0x35, 0xa5, 0x01, 0x29, 0xf4, 0xfd, 0xea };
std::vector<uint8_t> data2 = { 0x14, 0x59, 0x29, 0x72, 0x45, 0xb3, 0x03, 0xb3 };
std::vector<uint8_t> out1(16, 0);
std::vector<uint8_t> out2(2, 0);
EcbMode<Des::DesCrypt, PadderPkcs7>::Decryptor dec(desKey);
dec.Update(out1.begin(), out1.end(), data1.begin(), data1.end());
ASSERT_THROW(dec.Update(out2.begin(), out2.end(), data2.begin(), data2.end()),
Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data = { 0x65, 0x35, 0xa5, 0x01, 0x29, 0xf4, 0xfd, 0xea,
0x62, 0x3a, 0x45, 0xdd, 0x62, 0xda, 0xb0, 0x45 };
std::vector<uint8_t> out(8, 0);
EcbMode<Des::DesCrypt, PadderPkcs7>::Decryptor dec(desKey);
uint64_t written = dec.Update(out.begin(), out.end(), data.begin(), data.end());
ASSERT_THROW(written += dec.Finish(out.begin() + written, out.end()),
Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data = { 0x65, 0x35, 0xa5, 0x01, 0x29, 0xf4, 0xfd, 0xea,
0x65, 0x35, 0xa5, 0x01, 0x29, 0xf4, 0xfd, 0xea,
0xbd, 0x11, 0x44, 0xf4, 0x99, 0x95, 0x53, 0xd3 };
std::vector<uint8_t> out(17, 0);
EcbMode<Des::DesCrypt, PadderPkcs7>::Decryptor dec(desKey);
uint64_t written = dec.Update(out.begin(), out.end(), data.begin(), data.end());
ASSERT_THROW(written += dec.Finish(out.begin() + written, out.end()),
Chaos::Service::ChaosException);
}
}
TEST(EcbModeTests, DecryptNotRoundSizeTest)
{
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>::Decryptor dec(desKey);
std::vector<uint8_t> out;
out.resize(dec.PredictMaxUpdateOutput(data.size()) +
dec.PredictMaxFinishOutput());
uint64_t written = dec.Update(out.begin(), out.end(), data.begin(), data.end());
written += dec.Finish(out.begin() + written, out.end());
out.resize(written);
return out;
}
};
Helper ecbDec;
{
std::vector<uint8_t> data = { 0x64, 0x32, 0x13, 0xe7, 0x31, 0x06, 0xc6 };
std::vector<uint8_t> key = { 0x28, 0x1c, 0xf3, 0x11, 0xce, 0xc6, 0xc2, 0x38 };
ASSERT_THROW(ecbDec(data, key), Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data = { 0xee, 0x5b, 0x16, 0x33, 0xad, 0x2d, 0x32, 0x42,
0xdc, 0x0d, 0x9f, 0x87 };
std::vector<uint8_t> key = { 0x28, 0x1c, 0xf3, 0x11, 0xce, 0xc6, 0xc2, 0x38 };
ASSERT_THROW(ecbDec(data, key), Chaos::Service::ChaosException);
}
}
TEST(EcbModeTests, DecryptInvalidPaddingTest)
{
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>::Decryptor dec(desKey);
std::vector<uint8_t> out;
out.resize(dec.PredictMaxUpdateOutput(data.size()) +
dec.PredictMaxFinishOutput());
uint64_t written = dec.Update(out.begin(), out.end(), data.begin(), data.end());
written += dec.Finish(out.begin() + written, out.end());
out.resize(written);
return out;
}
};
Helper ecbDec;
{
std::vector<uint8_t> data = { 0x60, 0xa7, 0x4b, 0x8c, 0x68, 0x03, 0x70, 0x0c };
std::vector<uint8_t> key = { 0xaa, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf1, 0x12 };
ASSERT_THROW(ecbDec(data, key), Chaos::Service::ChaosException);
}
{
std::vector<uint8_t> data = { 0xf6, 0x61, 0x7a, 0x71, 0xe8, 0xa4, 0xb9, 0x70,
0xbe, 0xd2, 0x51, 0x7e, 0x4f, 0x39, 0xfe, 0xa2 };
std::vector<uint8_t> key = { 0xaa, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf1, 0x12 };
ASSERT_THROW(ecbDec(data, key), Chaos::Service::ChaosException);
}
}