Sync source and test dirs structure.
This commit is contained in:
@@ -0,0 +1,230 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "TestHelpers/AssertThrowEx.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
#include "Cipher/Arc4/Arc4Crypt.hpp"
|
||||
#include "Service/ChaosException.hpp"
|
||||
|
||||
using namespace Chaos::Cipher::Arc4;
|
||||
|
||||
static std::vector<uint8_t> StrToU8Vec(const char * str)
|
||||
{
|
||||
std::vector<uint8_t> result;
|
||||
result.reserve(strlen(str));
|
||||
|
||||
for (; *str != '\0'; ++str)
|
||||
{
|
||||
result.push_back(static_cast<uint8_t>(*str));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
TEST(Arc4CryptTests, BasicTest)
|
||||
{
|
||||
const std::vector<uint8_t> key = StrToU8Vec("Secret");
|
||||
const std::vector<uint8_t> data = StrToU8Vec("Attack at dawn");
|
||||
|
||||
std::vector<uint8_t> ciphertext;
|
||||
ciphertext.resize(data.size());
|
||||
|
||||
{
|
||||
Arc4Crypt arc4;
|
||||
arc4.Rekey(key.begin(), key.end());
|
||||
|
||||
arc4.Encrypt(ciphertext.begin(), data.begin(), data.size());
|
||||
}
|
||||
|
||||
ASSERT_EQ(std::vector<uint8_t>({ 0x45, 0xA0, 0x1F, 0x64, 0x5F, 0xC3, 0x5B,
|
||||
0x38, 0x35, 0x52, 0x54, 0x4B, 0x9B, 0xF5 }),
|
||||
ciphertext);
|
||||
|
||||
std::vector<uint8_t> recoveredData;
|
||||
recoveredData.resize(data.size());
|
||||
|
||||
{
|
||||
Arc4Crypt arc4;
|
||||
arc4.Rekey(key.begin(), key.end());
|
||||
|
||||
arc4.Decrypt(recoveredData.begin(), ciphertext.begin(), ciphertext.size());
|
||||
}
|
||||
|
||||
ASSERT_EQ(data, recoveredData);
|
||||
}
|
||||
|
||||
TEST(Arc4CryptTests, UninitializedArc4CryptTest)
|
||||
{
|
||||
Arc4Crypt arc4;
|
||||
|
||||
{
|
||||
std::array<uint8_t, 10> in = {};
|
||||
std::array<uint8_t, 10> out = {};
|
||||
|
||||
ASSERT_THROW_EX(arc4.Encrypt(out.begin(), in.begin(), in.size()),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Crypt: not initialized", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(arc4.Decrypt(out.begin(), in.begin(), in.size()),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Crypt: not initialized", ex.GetMessage());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Arc4CryptTests, RekeyTest)
|
||||
{
|
||||
Arc4Crypt arc4;
|
||||
|
||||
const std::vector<uint8_t> data = StrToU8Vec("The quick brown fox jumps over the lazy dog.");
|
||||
|
||||
{
|
||||
const std::vector<uint8_t> key = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
|
||||
std::vector<uint8_t> ciphertext;
|
||||
ciphertext.resize(data.size());
|
||||
|
||||
arc4.Rekey(key.begin(), key.end());
|
||||
arc4.Encrypt(ciphertext.begin(), data.begin(), data.size());
|
||||
|
||||
ASSERT_EQ(std::vector<uint8_t>({ 0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30,
|
||||
0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89,
|
||||
0xef, 0xa5, 0xfa, 0xe4, 0x6c, 0xd2, 0x1f, 0x7f, 0x29, 0x6d, 0xde,
|
||||
0xea, 0x58, 0xae, 0xec, 0x6f, 0xa1, 0x02, 0x47, 0x23, 0x0b, 0x96 }),
|
||||
ciphertext);
|
||||
}
|
||||
|
||||
{
|
||||
const std::vector<uint8_t> key = { 0x05, 0x04, 0x03, 0x02, 0x01 };
|
||||
|
||||
std::vector<uint8_t> ciphertext;
|
||||
ciphertext.resize(data.size());
|
||||
|
||||
arc4.Rekey(key.begin(), key.end());
|
||||
arc4.Encrypt(ciphertext.begin(), data.begin(), data.size());
|
||||
|
||||
ASSERT_EQ(std::vector<uint8_t>({ 0x18, 0x4a, 0x2e, 0x05, 0xc2, 0x8e, 0x5b, 0x26, 0xfa, 0x47, 0x44,
|
||||
0x0d, 0x12, 0x28, 0xb4, 0x45, 0x06, 0xc3, 0x5a, 0x17, 0xeb, 0xad,
|
||||
0x60, 0xb2, 0x16, 0x17, 0x29, 0x4d, 0xaa, 0xcb, 0x27, 0xd1, 0x45,
|
||||
0xa8, 0xb9, 0xc0, 0x02, 0xd3, 0x4a, 0x9d, 0xe8, 0xde, 0x63, 0x30 }),
|
||||
ciphertext);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Arc4CryptTests, EncryptOutIteratorUsageTest)
|
||||
{
|
||||
const std::vector<uint8_t> data = StrToU8Vec("The quick brown fox jumps over the lazy dog.");
|
||||
|
||||
{
|
||||
std::array<uint8_t, 5> key = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 47> out = {};
|
||||
|
||||
std::array<uint8_t, 47> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30,
|
||||
0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Encrypt(out.begin() + 3, data.begin(), 22);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 5> key = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 47> out = {};
|
||||
|
||||
std::array<uint8_t, 47> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xe6, 0x51, 0x06, 0x25, 0x81, 0x48, 0xa9, 0x44, 0xa7, 0xe3, 0x30,
|
||||
0x38, 0x65, 0x66, 0x76, 0x88, 0x0f, 0xed, 0xec, 0x6f, 0x72, 0x89,
|
||||
0xef, 0xa5, 0xfa, 0xe4, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Encrypt(out.begin() + 3, data.begin(), 27);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
std::array<uint8_t, 5> key = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 44> out = {};
|
||||
std::array<uint8_t, 44> expected = {};
|
||||
|
||||
crypt.Encrypt(out.begin() + 3, data.begin(), 0);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Arc4CryptTests, DecryptOutIteratorUsageTest)
|
||||
{
|
||||
const std::array<uint8_t, 14> data = { 0x45, 0xA0, 0x1F, 0x64, 0x5F, 0xC3, 0x5B,
|
||||
0x38, 0x35, 0x52, 0x54, 0x4B, 0x9B, 0xF5 };
|
||||
const std::vector<uint8_t> key = StrToU8Vec("Secret");
|
||||
|
||||
{
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 17> out = {};
|
||||
|
||||
std::array<uint8_t, 17> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
'A', 't', 't', 'a', 'c', 'k', ' ', 'a', 't', ' ', 'd', 'a',
|
||||
0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Decrypt(out.begin() + 3, data.begin(), 12);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 17> out = {};
|
||||
|
||||
std::array<uint8_t, 17> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
'A', 't', 't', 'a', 'c', 'k', ' ',
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
crypt.Decrypt(out.begin() + 3, data.begin(), 7);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
Arc4Crypt crypt(key.begin(), key.end());
|
||||
|
||||
std::array<uint8_t, 14> out = {};
|
||||
std::array<uint8_t, 14> expected = {};
|
||||
|
||||
crypt.Decrypt(out.begin() + 3, data.begin(), 0);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,473 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "TestHelpers/AssertThrowEx.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <array>
|
||||
#include <iterator>
|
||||
#include <cstring>
|
||||
|
||||
#include "Cipher/Arc4/Arc4Gen.hpp"
|
||||
#include "Service/ChaosException.hpp"
|
||||
|
||||
using namespace Chaos::Cipher::Arc4;
|
||||
|
||||
TEST(Arc4GenTests, RFCTest)
|
||||
{
|
||||
Arc4Gen gen;
|
||||
|
||||
// Key: 0x0102030405 (40 bits)
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27,
|
||||
0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8,
|
||||
0x69, 0x82, 0x94, 0x4f, 0x18, 0xfc, 0x82, 0xd5,
|
||||
0x89, 0xc4, 0x03, 0xa4, 0x7a, 0x0d, 0x09, 0x19
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0x28, 0xcb, 0x11, 0x32, 0xc9, 0x6c, 0xe2, 0x86,
|
||||
0x42, 0x1d, 0xca, 0xad, 0xb8, 0xb6, 0x9e, 0xae,
|
||||
0x1c, 0xfc, 0xf6, 0x2b, 0x03, 0xed, 0xdb, 0x64,
|
||||
0x1d, 0x77, 0xdf, 0xcf, 0x7f, 0x8d, 0x8c, 0x93
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Drop(240);
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0x42, 0xb7, 0xd0, 0xcd, 0xd9, 0x18, 0xa8, 0xa3,
|
||||
0x3d, 0xd5, 0x17, 0x81, 0xc8, 0x1f, 0x40, 0x41,
|
||||
0x64, 0x59, 0x84, 0x44, 0x32, 0xa7, 0xda, 0x92,
|
||||
0x3c, 0xfb, 0x3e, 0xb4, 0x98, 0x06, 0x61, 0xf6
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Drop(496);
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0x45, 0x12, 0x90, 0x48, 0xe6, 0xa0, 0xed, 0x0b,
|
||||
0x56, 0xb4, 0x90, 0x33, 0x8f, 0x07, 0x8d, 0xa5,
|
||||
0x30, 0xab, 0xbc, 0xc7, 0xc2, 0x0b, 0x01, 0x60,
|
||||
0x9f, 0x23, 0xee, 0x2d, 0x5f, 0x6b, 0xb7, 0xdf
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Drop(1008);
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0x1e, 0xb1, 0x4a, 0x0c, 0x13, 0xb3, 0xbf, 0x47,
|
||||
0xfa, 0x2a, 0x0b, 0xa9, 0x3a, 0xd4, 0x5b, 0x8b,
|
||||
0xcc, 0x58, 0x2f, 0x8b, 0xa9, 0xf2, 0x65, 0xe2,
|
||||
0xb1, 0xbe, 0x91, 0x12, 0xe9, 0x75, 0xd2, 0xd7
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Drop(2032);
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0x06, 0x83, 0x26, 0xa2, 0x11, 0x84, 0x16, 0xd2,
|
||||
0x1f, 0x9d, 0x04, 0xb2, 0xcd, 0x1c, 0xa0, 0x50,
|
||||
0xff, 0x25, 0xb5, 0x89, 0x95, 0x99, 0x67, 0x07,
|
||||
0xe5, 0x1f, 0xbd, 0xf0, 0x8b, 0x34, 0xd8, 0x75
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Drop(4080);
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0x06, 0x83, 0x26, 0xa2, 0x11, 0x84, 0x16, 0xd2,
|
||||
0x1f, 0x9d, 0x04, 0xb2, 0xcd, 0x1c, 0xa0, 0x50,
|
||||
0xff, 0x25, 0xb5, 0x89, 0x95, 0x99, 0x67, 0x07,
|
||||
0xe5, 0x1f, 0xbd, 0xf0, 0x8b, 0x34, 0xd8, 0x75
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Drop(4080);
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
// Key: 0x0102030405060708090a (80 bits)
|
||||
|
||||
{
|
||||
uint8_t key[] =
|
||||
{
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a
|
||||
};
|
||||
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0xed, 0xe3, 0xb0, 0x46, 0x43, 0xe5, 0x86, 0xcc,
|
||||
0x90, 0x7d, 0xc2, 0x18, 0x51, 0x70, 0x99, 0x02,
|
||||
0x03, 0x51, 0x6b, 0xa7, 0x8f, 0x41, 0x3b, 0xeb,
|
||||
0x22, 0x3a, 0xa5, 0xd4, 0xd2, 0xdf, 0x67, 0x11
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] =
|
||||
{
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a
|
||||
};
|
||||
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0xb5, 0x5a, 0x0a, 0x5b, 0x51, 0xa1, 0x2a, 0x8b,
|
||||
0xe3, 0x48, 0x99, 0xc3, 0xe0, 0x47, 0x51, 0x1a,
|
||||
0xd9, 0xa0, 0x9c, 0xea, 0x3c, 0xe7, 0x5f, 0xe3,
|
||||
0x96, 0x98, 0x07, 0x03, 0x17, 0xa7, 0x13, 0x39
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Drop(2032);
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
// Key: 0x0102030405060708090a0b0c0d0e0f10 (128 bits)
|
||||
|
||||
{
|
||||
uint8_t key[] =
|
||||
{
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
|
||||
0x0f, 0x10
|
||||
};
|
||||
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0x9a, 0xc7, 0xcc, 0x9a, 0x60, 0x9d, 0x1e, 0xf7,
|
||||
0xb2, 0x93, 0x28, 0x99, 0xcd, 0xe4, 0x1b, 0x97,
|
||||
0x52, 0x48, 0xc4, 0x95, 0x90, 0x14, 0x12, 0x6a,
|
||||
0x6e, 0x8a, 0x84, 0xf1, 0x1d, 0x1a, 0x9e, 0x1c
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] =
|
||||
{
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
|
||||
0x0f, 0x10
|
||||
};
|
||||
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0xd0, 0x3d, 0x1b, 0xc0, 0x3c, 0xd3, 0x3d, 0x70,
|
||||
0xdf, 0xf9, 0xfa, 0x5d, 0x71, 0x96, 0x3e, 0xbd,
|
||||
0x8a, 0x44, 0x12, 0x64, 0x11, 0xea, 0xa7, 0x8b,
|
||||
0xd5, 0x1e, 0x8d, 0x87, 0xa8, 0x87, 0x9b, 0xf5
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Drop(2032);
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
// Key: 0x0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20 (256 bits)
|
||||
|
||||
{
|
||||
uint8_t key[] =
|
||||
{
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
|
||||
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
|
||||
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
|
||||
0x1d, 0x1e, 0x1f, 0x20
|
||||
};
|
||||
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0xea, 0xa6, 0xbd, 0x25, 0x88, 0x0b, 0xf9, 0x3d,
|
||||
0x3f, 0x5d, 0x1e, 0x4c, 0xa2, 0x61, 0x1d, 0x91,
|
||||
0xcf, 0xa4, 0x5c, 0x9f, 0x7e, 0x71, 0x4b, 0x54,
|
||||
0xbd, 0xfa, 0x80, 0x02, 0x7c, 0xb1, 0x43, 0x80
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] =
|
||||
{
|
||||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
|
||||
0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
|
||||
0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c,
|
||||
0x1d, 0x1e, 0x1f, 0x20
|
||||
};
|
||||
|
||||
gen.Rekey(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 32> expected =
|
||||
{
|
||||
0xf6, 0x56, 0xab, 0xcc, 0xf8, 0x8d, 0xd8, 0x27,
|
||||
0x02, 0x7b, 0x2c, 0xe9, 0x17, 0xd4, 0x64, 0xec,
|
||||
0x18, 0xb6, 0x25, 0x03, 0xbf, 0xbc, 0x07, 0x7f,
|
||||
0xba, 0xbb, 0x98, 0xf2, 0x0d, 0x98, 0xab, 0x34
|
||||
};
|
||||
|
||||
std::array<uint8_t, 32> fact;
|
||||
|
||||
gen.Drop(2032);
|
||||
gen.Generate(fact.begin(), fact.size());
|
||||
|
||||
ASSERT_EQ(expected, fact);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Arc4GenTests, TooSmallKeyTest)
|
||||
{
|
||||
{
|
||||
const char * key = "smal";
|
||||
|
||||
Arc4Gen gen;
|
||||
ASSERT_THROW_EX(gen.Rekey(key, key + strlen(key)),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: key is too small", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(Arc4Gen(key, key + strlen(key)),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: key is too small", ex.GetMessage());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Arc4GenTests, UninitializedGenTest)
|
||||
{
|
||||
std::array<uint8_t, 10> out;
|
||||
|
||||
{
|
||||
Arc4Gen gen;
|
||||
|
||||
ASSERT_THROW_EX(gen.Generate(out.begin(), out.size()),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(gen.Drop(0),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(gen.Drop(1),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(gen.Drop(20),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(gen.Drop(256),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
}
|
||||
|
||||
{
|
||||
const char * key = "smal";
|
||||
|
||||
Arc4Gen gen;
|
||||
|
||||
ASSERT_THROW_EX(gen.Rekey(key, key + strlen(key)),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: key is too small", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(gen.Generate(out.begin(), out.size()),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(gen.Drop(0),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(gen.Drop(1),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(gen.Drop(20),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
|
||||
ASSERT_THROW_EX(gen.Drop(256),
|
||||
Chaos::Service::ChaosException,
|
||||
{
|
||||
ASSERT_EQ("Arc4Gen: not initialized", ex.GetMessage());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Arc4GenTests, GenerateOutIteratorUsageTest)
|
||||
{
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
Arc4Gen gen(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 23> out = {};
|
||||
|
||||
std::array<uint8_t, 23> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27,
|
||||
0xcc, 0xc3, 0x52, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
gen.Generate(out.begin() + 3, 11);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
Arc4Gen gen(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 23> out = {};
|
||||
|
||||
std::array<uint8_t, 23> expected =
|
||||
{
|
||||
0x00, 0x00, 0x00,
|
||||
0xb2, 0x39, 0x63, 0x05, 0xf0, 0x3d, 0xc0, 0x27,
|
||||
0xcc, 0xc3, 0x52, 0x4a, 0x0a, 0x11, 0x18, 0xa8,
|
||||
0x69, 0x82, 0x00, 0x00
|
||||
};
|
||||
|
||||
gen.Generate(out.begin() + 3, 18);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
|
||||
{
|
||||
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
|
||||
Arc4Gen gen(key, key + std::size(key));
|
||||
|
||||
std::array<uint8_t, 20> out = {};
|
||||
std::array<uint8_t, 20> expected = {};
|
||||
|
||||
gen.Generate(out.begin() + 3, 0);
|
||||
|
||||
ASSERT_EQ(expected, out);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user