Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
etorth committed Jan 14, 2025
1 parent 86b732a commit 2e6a823
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 22 deletions.
65 changes: 47 additions & 18 deletions common/src/aesf.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,56 @@
#include <type_traits>
#include <utility>

#include "aesf.hpp"
#include "strf.hpp"
#include "totype.hpp"
#include "base64f.hpp"
#include "fflerror.hpp"

#define ECB 0
#define CTR 0
#define CBC 1
#include "aes.hpp"

static_assert(AES_KEYLEN == 16);
static_assert(AES_BLOCKLEN == 16);

#define AES_ctx_ptr(p) std::launder(reinterpret_cast<AES_ctx *>(p))

aesf::AES::AES(const char *key, uint64_t iv1, uint64_t iv2)
{
static_assert(AES_storage_alignment >= alignof(AES_ctx));
static_assert(AES_storage_size >= sizeof(AES_ctx));

const auto keysv = to_sv(str_haschar(key) ? key : "0299dc69a0a0f4de");
uint8_t keybuf[AES_KEYLEN];

for(size_t done = 0; done < AES_KEYLEN;){
const size_t copied = std::min<size_t>(AES_KEYLEN - done, keysv.size());
std::memcpy(keybuf + done, keysv.data(), copied);
done += copied;
}

std::construct_at<AES_ctx>(reinterpret_cast<AES_ctx *>(storage));

uint8_t ivbuf[16];
std::memcpy(ivbuf , &iv1, 8);
std::memcpy(ivbuf + 8, &iv2, 8);

AES_init_ctx_iv(AES_ctx_ptr(storage), keybuf, ivbuf);
}

std::string aesf::encrypt(const char *orig, const char *password, uint64_t r)
void aesf::AES::encrypt(void *data, size_t size)
{
fflassert(orig, to_cstr(orig));
fflassert(password, to_cstr(password));

std::string result;
result.append(orig);
result.append(password);
result.append(std::to_string(r));
result.append(str_printf("%04zu", to_sv(orig).size()));
return base64f::encode(result);
fflassert(data);
fflassert(size);
fflassert(size % 16 == 0, size);
AES_CBC_encrypt_buffer(AES_ctx_ptr(storage), reinterpret_cast<uint8_t *>(data), size);
}

std::string aesf::decrypt(const char *encoded, const char *, uint64_t)
void aesf::AES::decrypt(void *data, size_t size)
{
const auto s = base64f::decode(encoded, to_sv(encoded).size());
const size_t size = (s.rbegin()[3] - '0') * 1000
+ (s.rbegin()[2] - '0') * 100
+ (s.rbegin()[1] - '0') * 10
+ (s.rbegin()[0] - '0') * 1;
return s.substr(0, size);
fflassert(data);
fflassert(size);
fflassert(size % 16 == 0, size);
AES_CBC_decrypt_buffer(AES_ctx_ptr(storage), reinterpret_cast<uint8_t *>(data), size);
}
21 changes: 19 additions & 2 deletions common/src/aesf.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
#pragma once
#include <string>
#include <cstdint>
#include <cstddef>

namespace aesf
{
std::string encrypt(const char *, const char *, uint64_t);
std::string decrypt(const char *, const char *, uint64_t);
class AES
{
private:
#define AES_storage_alignment 16
#define AES_storage_size 192
alignas(AES_storage_alignment) std::byte storage[AES_storage_size];

public:
AES(const char *, uint64_t, uint64_t);

public:
void encrypt(void *, size_t);
void decrypt(void *, size_t);

public:
template<typename T> void encrypt(T &t) { encrypt(std::data(t), std::size(t)); }
template<typename T> void decrypt(T &t) { decrypt(std::data(t), std::size(t)); }
};
}
1 change: 1 addition & 0 deletions server/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ TARGET_INCLUDE_DIRECTORIES(monoserver PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
TARGET_INCLUDE_DIRECTORIES(monoserver PRIVATE ${CMAKE_CURRENT_LIST_DIR})

TARGET_LINK_LIBRARIES(monoserver common )
TARGET_LINK_LIBRARIES(monoserver ${TINYAES_LIBRARIES} )
TARGET_LINK_LIBRARIES(monoserver ${TINYXML2_LIBRARIES} )
TARGET_LINK_LIBRARIES(monoserver ${LUA_LIBRARIES} )
TARGET_LINK_LIBRARIES(monoserver ${FLTK_LIBRARIES} )
Expand Down
28 changes: 26 additions & 2 deletions server/src/npchar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "npchar.hpp"
#include "colorf.hpp"
#include "totype.hpp"
#include "base64f.hpp"
#include "filesys.hpp"
#include "dbcomid.hpp"
#include "cerealf.hpp"
Expand All @@ -23,6 +24,28 @@ extern MonoServer *g_monoServer;
extern ServerPasswordWindow *g_serverPasswordWindow;
extern ServerConfigureWindow *g_serverConfigureWindow;

NPChar::AESHelper::AESHelper(const NPChar *npc, uint64_t uid)
: aesf::AES(g_serverPasswordWindow->getPassword(), reinterpret_cast<uintptr_t>(to_cvptr(npc)) ^ ~uid, npc->getXMLSeqID(uid).value())
{}

std::string NPChar::AESHelper::encode(const char *s)
{
fflassert(str_haschar(s), s);
std::string buf(s);

buf.resize((buf.size() + 15) / 16 * 16);
encrypt(buf);

return base64f::encode(buf);
}

std::string NPChar::AESHelper::decode(const char *s)
{
auto buf = base64f::decode(to_sv(s));
decrypt(buf);
return buf;
}

NPChar::LuaThreadRunner::LuaThreadRunner(NPChar *npc)
: CharObject::LuaThreadRunner(npc)
{
Expand Down Expand Up @@ -444,15 +467,15 @@ void NPChar::postXMLLayout(uint64_t uid, std::string path, std::string xmlString
fflassert(xmlDoc.RootElement(), xmlString);
fflassert(str_tolower(xmlDoc.RootElement()->Name()) == "layout", xmlString);

auto fnXMLTran = [xmlSeqID = rollXMLSeqID(uid)](this auto &&self, tinyxml2::XMLNode *node)
auto fnXMLTran = [uid, npc = this](this auto &&self, tinyxml2::XMLNode *node)
{
if(!node){
return;
}
if(auto elem = node->ToElement()){
for(auto attr = elem->FirstAttribute(); attr; attr = attr->Next()){
if(str_tolower(attr->Name()) == "id"){
const_cast<tinyxml2::XMLAttribute *>(attr)->SetAttribute(aesf::encrypt(attr->Value(), g_serverPasswordWindow->getPassword(), xmlSeqID).c_str());
const_cast<tinyxml2::XMLAttribute *>(attr)->SetAttribute(AESHelper(npc, uid).encode(attr->Value()).c_str());
break;
}
}
Expand All @@ -463,6 +486,7 @@ void NPChar::postXMLLayout(uint64_t uid, std::string path, std::string xmlString
}
};

rollXMLSeqID(uid);
fnXMLTran(xmlDoc.RootElement());
forwardNetPackage(uid, SM_NPCXMLLAYOUT, cerealf::serialize(SDNPCXMLLayout
{
Expand Down
10 changes: 10 additions & 0 deletions server/src/npchar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <memory>
#include <cstdint>
#include <optional>
#include "aesf.hpp"
#include "servicecore.hpp"
#include "servermap.hpp"
#include "charobject.hpp"
Expand All @@ -16,6 +17,15 @@ class NPChar final: public CharObject
std::vector<SDCostItem> costList;
};

struct AESHelper: protected aesf::AES
{
/**/ AESHelper(const NPChar *, uint64_t);
/**/ ~AESHelper() = default;

std::string encode(const char *);
std::string decode(const char *);
};

protected:
class LuaThreadRunner: public CharObject::LuaThreadRunner
{
Expand Down

0 comments on commit 2e6a823

Please sign in to comment.