Skip to content

Commit

Permalink
Use correct names for encryption algos.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoikas committed Aug 29, 2023
1 parent 1348f46 commit 99cbdcd
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion AuthServ/AuthServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ void DS::AuthServer_Init(bool restrictLogins)

DS::BufferStream stream;
DS::EncryptedStream encStream(&stream, DS::EncryptedStream::Mode::e_write,
DS::EncryptedStream::Type::e_btea, DS::Settings::DroidKey());
DS::EncryptedStream::Type::e_xxtea, DS::Settings::DroidKey());
SDL::DescriptorDb::WriteDescriptors(&encStream);
s_minifiedSdl = DS::Blob(stream.buffer(), stream.size());
}
Expand Down
2 changes: 1 addition & 1 deletion SDL/SdlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool SDL::Parser::open(const char* filename)
close();
return false;
}
if (m_encStream->getEncType() == DS::EncryptedStream::Type::e_btea)
if (m_encStream->getEncType() == DS::EncryptedStream::Type::e_xxtea)
m_encStream->setKeys(DS::Settings::DroidKey());
}

Expand Down
18 changes: 9 additions & 9 deletions Tests/Test_EncryptedStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ TEST_CASE("EncryptedStream known values", "[streams]")
const char result[] = "Hello, world!";
constexpr size_t resultsz = sizeof(result) - 1;

SECTION("bTEA") {
SECTION("xxtea") {
uint32_t keys[] = { 0x31415926, 0x53589793, 0x23846264, 0x33832795 };
uint8_t buff[] = {
0x6E, 0x6F, 0x74, 0x74, 0x68, 0x65, 0x64, 0x72, 0x6F, 0x69, 0x64, 0x73,
Expand All @@ -35,7 +35,7 @@ TEST_CASE("EncryptedStream known values", "[streams]")

DS::BufferStream base(buff, sizeof(buff));
DS::EncryptedStream stream(&base, DS::EncryptedStream::Mode::e_read, std::nullopt, keys);
REQUIRE(stream.getEncType() == DS::EncryptedStream::Type::e_btea);
REQUIRE(stream.getEncType() == DS::EncryptedStream::Type::e_xxtea);
REQUIRE(stream.size() == resultsz);

char test[sizeof(result)];
Expand All @@ -47,7 +47,7 @@ TEST_CASE("EncryptedStream known values", "[streams]")
REQUIRE(stream.atEof());
}

SECTION("xTEA") {
SECTION("tea") {
uint8_t buff[] = {
0x77, 0x68, 0x61, 0x74, 0x64, 0x6F, 0x79, 0x6F, 0x75, 0x73, 0x65, 0x65,
0x0D, 0x00, 0x00, 0x00, 0xAC, 0xC1, 0xA6, 0xB6, 0xDC, 0x33, 0x95, 0x0E,
Expand All @@ -56,7 +56,7 @@ TEST_CASE("EncryptedStream known values", "[streams]")

DS::BufferStream base(buff, sizeof(buff));
DS::EncryptedStream stream(&base, DS::EncryptedStream::Mode::e_read);
REQUIRE(stream.getEncType() == DS::EncryptedStream::Type::e_xtea);
REQUIRE(stream.getEncType() == DS::EncryptedStream::Type::e_tea);
REQUIRE(stream.size() == resultsz);

char test[sizeof(result)];
Expand All @@ -82,8 +82,8 @@ TEST_CASE("EncryptedStream known values", "[streams]")

TEST_CASE("EncryptedStream round-trip", "[streams]",) {
auto type = GENERATE(
DS::EncryptedStream::Type::e_btea,
DS::EncryptedStream::Type::e_xtea
DS::EncryptedStream::Type::e_xxtea,
DS::EncryptedStream::Type::e_tea
);

DS::BufferStream base;
Expand Down Expand Up @@ -114,18 +114,18 @@ TEST_CASE("EncryptedStream Magic Strings", "[streams]")
SECTION("whatdoyousee") {
const char buffer[] { "whatdoyousee\x0\x0\x0\x0" };
DS::BufferStream base(buffer, sizeof(buffer));
REQUIRE(DS::EncryptedStream::CheckEncryption(&base) == DS::EncryptedStream::Type::e_xtea);
REQUIRE(DS::EncryptedStream::CheckEncryption(&base) == DS::EncryptedStream::Type::e_tea);
}

SECTION("BriceIsSmart") {
const char buffer[] { "BriceIsSmart\x0\x0\x0\x0" };
DS::BufferStream base(buffer, sizeof(buffer));
REQUIRE(DS::EncryptedStream::CheckEncryption(&base) == DS::EncryptedStream::Type::e_xtea);
REQUIRE(DS::EncryptedStream::CheckEncryption(&base) == DS::EncryptedStream::Type::e_tea);
}

SECTION("notthedroids") {
const char buffer[] { "notthedroids\x0\x0\x0\x0" };
DS::BufferStream base(buffer, sizeof(buffer));
REQUIRE(DS::EncryptedStream::CheckEncryption(&base) == DS::EncryptedStream::Type::e_btea);
REQUIRE(DS::EncryptedStream::CheckEncryption(&base) == DS::EncryptedStream::Type::e_xxtea);
}
}
48 changes: 24 additions & 24 deletions streams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,13 +305,13 @@ DS::EncryptedStream::EncryptedStream(
DS::Stream* base, DS::EncryptedStream::Mode mode,
std::optional<DS::EncryptedStream::Type> type, const uint32_t* keys
) : m_base(base), m_buffer(), m_key(), m_pos(), m_size(),
m_type(type.has_value() ? type.value() : DS::EncryptedStream::Type::e_xtea),
m_type(type.has_value() ? type.value() : DS::EncryptedStream::Type::e_tea),
m_mode(mode)
{
DS_ASSERT(base != nullptr);
DS_ASSERT(base->tell() == 0);

static constexpr uint32_t kXteaKey[] {
static constexpr uint32_t kTeaKey[] {
0x6c0a5452,
0x03827d0f,
0x3a170b92,
Expand All @@ -320,8 +320,8 @@ DS::EncryptedStream::EncryptedStream(
if (keys) {
memcpy(m_key, keys, sizeof(m_key));
} else {
static_assert(sizeof(kXteaKey) == sizeof(m_key));
memcpy(m_key, kXteaKey, sizeof(m_key));
static_assert(sizeof(kTeaKey) == sizeof(m_key));
memcpy(m_key, kTeaKey, sizeof(m_key));
}

uint8_t header[16]{};
Expand All @@ -330,11 +330,11 @@ DS::EncryptedStream::EncryptedStream(
case Mode::e_read:
base->readBytes(header, sizeof(header));
if (memcmp(header, "whatdoyousee", 12) == 0)
m_type = DS::EncryptedStream::Type::e_xtea;
m_type = DS::EncryptedStream::Type::e_tea;
else if (memcmp(header, "BriceIsSmart", 12) == 0)
m_type = DS::EncryptedStream::Type::e_xtea;
m_type = DS::EncryptedStream::Type::e_tea;
else if (memcmp(header, "notthedroids", 12) == 0)
m_type = DS::EncryptedStream::Type::e_btea;
m_type = DS::EncryptedStream::Type::e_xxtea;
else
throw DS::FileIOException("Unknown EncryptedString magic");
DS_ASSERT(!type.has_value() || type.value() == m_type);
Expand All @@ -356,10 +356,10 @@ DS::EncryptedStream::~EncryptedStream()
cryptFlush();
m_base->seek(0, SEEK_SET);
switch (m_type) {
case Type::e_btea:
case Type::e_xxtea:
m_base->writeBytes("notthedroids", 12);
break;
case Type::e_xtea:
case Type::e_tea:
m_base->writeBytes("whatdoyousee", 12);
break;
}
Expand All @@ -384,11 +384,11 @@ std::optional<DS::EncryptedStream::Type> DS::EncryptedStream::CheckEncryption(DS
stream->seek(pos, SEEK_SET);

if (memcmp(header, "whatdoyousee", sizeof(header)) == 0)
return DS::EncryptedStream::Type::e_xtea;
return DS::EncryptedStream::Type::e_tea;
if (memcmp(header, "BriceIsSmart", sizeof(header)) == 0)
return DS::EncryptedStream::Type::e_xtea;
return DS::EncryptedStream::Type::e_tea;
if (memcmp(header, "notthedroids", sizeof(header)) == 0)
return DS::EncryptedStream::Type::e_btea;
return DS::EncryptedStream::Type::e_xxtea;
return std::nullopt;
}

Expand All @@ -397,7 +397,7 @@ void DS::EncryptedStream::setKeys(const uint32_t* keys)
memcpy(m_key, keys, sizeof(m_key));
}

void DS::EncryptedStream::bteaDecipher(uint32_t* buf, uint32_t num) const
void DS::EncryptedStream::xxteaDecipher(uint32_t* buf, uint32_t num) const
{
uint32_t key = ((52 / num) + 6) * 0x9E3779B9;
while (key != 0) {
Expand All @@ -420,7 +420,7 @@ void DS::EncryptedStream::bteaDecipher(uint32_t* buf, uint32_t num) const
}
}

void DS::EncryptedStream::bteaEncipher(uint32_t* buf, uint32_t num) const
void DS::EncryptedStream::xxteaEncipher(uint32_t* buf, uint32_t num) const
{
uint32_t key = 0;
uint32_t count = (52 / num) + 6;
Expand All @@ -445,7 +445,7 @@ void DS::EncryptedStream::bteaEncipher(uint32_t* buf, uint32_t num) const
}
}

void DS::EncryptedStream::xteaDecipher(uint32_t* buf) const
void DS::EncryptedStream::teaDecipher(uint32_t* buf) const
{
uint32_t second = buf[1], first = buf[0], key = 0xC6EF3720;

Expand All @@ -460,7 +460,7 @@ void DS::EncryptedStream::xteaDecipher(uint32_t* buf) const
buf[1] = second;
}

void DS::EncryptedStream::xteaEncipher(uint32_t* buf) const
void DS::EncryptedStream::teaEncipher(uint32_t* buf) const
{
uint32_t first = buf[0], second = buf[1], key = 0;

Expand All @@ -478,11 +478,11 @@ void DS::EncryptedStream::xteaEncipher(uint32_t* buf) const
void DS::EncryptedStream::cryptFlush()
{
switch (m_type) {
case Type::e_btea:
bteaEncipher(reinterpret_cast<uint32_t*>(m_buffer), 2);
case Type::e_xxtea:
xxteaEncipher(reinterpret_cast<uint32_t*>(m_buffer), 2);
break;
case Type::e_xtea:
xteaEncipher(reinterpret_cast<uint32_t*>(m_buffer));
case Type::e_tea:
teaEncipher(reinterpret_cast<uint32_t*>(m_buffer));
break;
}
m_base->writeBytes(m_buffer, sizeof(m_buffer));
Expand All @@ -500,11 +500,11 @@ ssize_t DS::EncryptedStream::readBytes(void* buffer, size_t count)
if (lp == 0) {
m_base->readBytes(m_buffer, sizeof(m_buffer));
switch (m_type) {
case Type::e_btea:
bteaDecipher(reinterpret_cast<uint32_t*>(m_buffer), 2);
case Type::e_xxtea:
xxteaDecipher(reinterpret_cast<uint32_t*>(m_buffer), 2);
break;
case Type::e_xtea:
xteaDecipher(reinterpret_cast<uint32_t*>(m_buffer));
case Type::e_tea:
teaDecipher(reinterpret_cast<uint32_t*>(m_buffer));
break;
}
}
Expand Down
12 changes: 6 additions & 6 deletions streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ namespace DS

enum class Type
{
e_btea,
e_xtea,
e_xxtea,
e_tea,
};

protected:
Expand All @@ -302,10 +302,10 @@ namespace DS
Type m_type;
Mode m_mode;

void bteaDecipher(uint32_t* buf, uint32_t num) const;
void bteaEncipher(uint32_t* buf, uint32_t num) const;
void xteaDecipher(uint32_t* buf) const;
void xteaEncipher(uint32_t* buf) const;
void xxteaDecipher(uint32_t* buf, uint32_t num) const;
void xxteaEncipher(uint32_t* buf, uint32_t num) const;
void teaDecipher(uint32_t* buf) const;
void teaEncipher(uint32_t* buf) const;
void cryptFlush();

public:
Expand Down

0 comments on commit 99cbdcd

Please sign in to comment.