mirror of
https://github.com/edubart/otclient.git
synced 2025-10-16 20:43:26 +02:00
add lua flexibility for protocol
* use shared_ptr for InputMessage and OutputMessage and bind them * allow sending network messages from lua * implement extended opcode * use own OS type for otclient to allow server side detection * fixes in input event bot protection * move RSA to input/output network messages * allow to capture opcodes before GameProtocol parsing with the event GameProtocol.onOpcode * fixes in lua std::string pop/push to allow byte buffering
This commit is contained in:
@@ -221,8 +221,10 @@ void Application::poll()
|
||||
|
||||
void Application::close()
|
||||
{
|
||||
m_onInputEvent = true;
|
||||
if(!g_lua.callGlobalField<bool>("g_app", "onClose"))
|
||||
exit();
|
||||
m_onInputEvent = false;
|
||||
}
|
||||
|
||||
void Application::render()
|
||||
@@ -235,11 +237,15 @@ void Application::render()
|
||||
|
||||
void Application::resize(const Size& size)
|
||||
{
|
||||
m_onInputEvent = true;
|
||||
g_graphics.resize(size);
|
||||
g_ui.resize(size);
|
||||
m_onInputEvent = false;
|
||||
}
|
||||
|
||||
void Application::inputEvent(const InputEvent& event)
|
||||
{
|
||||
m_onInputEvent = true;
|
||||
g_ui.inputEvent(event);
|
||||
m_onInputEvent = false;
|
||||
}
|
||||
|
@@ -47,6 +47,7 @@ public:
|
||||
|
||||
bool isRunning() { return m_running; }
|
||||
bool isStopping() { return m_stopping; }
|
||||
bool isOnInputEvent() { return m_onInputEvent; }
|
||||
int getFrameSleep() { return m_frameSleep; }
|
||||
const std::string& getName() { return m_appName; }
|
||||
const std::string& getVersion() { return m_appVersion; }
|
||||
@@ -68,6 +69,7 @@ protected:
|
||||
Boolean<false> m_initialized;
|
||||
Boolean<false> m_running;
|
||||
Boolean<false> m_stopping;
|
||||
Boolean<false> m_onInputEvent;
|
||||
};
|
||||
|
||||
extern Application *g_app;
|
||||
|
@@ -397,6 +397,11 @@ void Application::registerLuaFunctions()
|
||||
|
||||
// Protocol
|
||||
g_lua.registerClass<Protocol>();
|
||||
//g_lua.bindClassMemberFunction<Protocol>("connect", &Protocol::connect);
|
||||
g_lua.bindClassMemberFunction<Protocol>("disconnect", &Protocol::disconnect);
|
||||
g_lua.bindClassMemberFunction<Protocol>("isConnected", &Protocol::isConnected);
|
||||
g_lua.bindClassMemberFunction<Protocol>("isConnecting", &Protocol::isConnecting);
|
||||
g_lua.bindClassMemberFunction<Protocol>("send", &Protocol::send);
|
||||
|
||||
// Module
|
||||
g_lua.registerClass<Module>();
|
||||
@@ -415,20 +420,31 @@ void Application::registerLuaFunctions()
|
||||
g_lua.bindClassMemberFunction<Module>("isAutoLoad", &Module::isAutoLoad);
|
||||
g_lua.bindClassMemberFunction<Module>("getAutoLoadPriority", &Module::getAutoLoadPriority);
|
||||
|
||||
// network manipulation via lua is disabled for a while
|
||||
/*
|
||||
// InputMessage
|
||||
g_lua.registerClass<InputMessage>();
|
||||
g_lua.bindClassStaticFunction<InputMessage>("create", []{ return InputMessagePtr(new InputMessage); });
|
||||
g_lua.bindClassMemberFunction<InputMessage>("skipBytes", &InputMessage::skipBytes);
|
||||
g_lua.bindClassMemberFunction<InputMessage>("getU8", &InputMessage::getU8);
|
||||
g_lua.bindClassMemberFunction<InputMessage>("getU16", &InputMessage::getU16);
|
||||
g_lua.bindClassMemberFunction<InputMessage>("getU32", &InputMessage::getU32);
|
||||
g_lua.bindClassMemberFunction<InputMessage>("getU64", &InputMessage::getU64);
|
||||
g_lua.bindClassMemberFunction<InputMessage>("getString", &InputMessage::getString);
|
||||
g_lua.bindClassMemberFunction<InputMessage>("decryptRSA", &InputMessage::decryptRSA);
|
||||
g_lua.bindClassMemberFunction<InputMessage>("getReadSize", &InputMessage::getReadSize);
|
||||
g_lua.bindClassMemberFunction<InputMessage>("getUnreadSize", &InputMessage::getUnreadSize);
|
||||
g_lua.bindClassMemberFunction<InputMessage>("eof", &InputMessage::eof);
|
||||
|
||||
// OutputMessage
|
||||
g_lua.registerClass<OutputMessage>();
|
||||
g_lua.bindClassStaticFunction<OutputMessage>("new", []{ return OutputMessagePtr(new OutputMessage); });
|
||||
g_lua.bindClassStaticFunction<OutputMessage>("create", []{ return OutputMessagePtr(new OutputMessage); });
|
||||
g_lua.bindClassMemberFunction<OutputMessage>("reset", &OutputMessage::reset);
|
||||
g_lua.bindClassMemberFunction<OutputMessage>("addU8", &OutputMessage::addU8);
|
||||
g_lua.bindClassMemberFunction<OutputMessage>("addU16", &OutputMessage::addU16);
|
||||
g_lua.bindClassMemberFunction<OutputMessage>("addU32", &OutputMessage::addU32);
|
||||
g_lua.bindClassMemberFunction<OutputMessage>("addU64", &OutputMessage::addU64);
|
||||
g_lua.bindClassMemberFunction<OutputMessage>("addString", (void(OutputMessage::*)(const std::string&))&OutputMessage::addString);
|
||||
|
||||
g_lua.bindClassStaticFunction<Protocol>("send", [](const ProtocolPtr proto, OutputMessagePtr msg) { proto->send(*msg.get()); });
|
||||
*/
|
||||
g_lua.bindClassMemberFunction<OutputMessage>("addString", &OutputMessage::addString);
|
||||
g_lua.bindClassMemberFunction<OutputMessage>("addPaddingBytes", &OutputMessage::addPaddingBytes);
|
||||
g_lua.bindClassMemberFunction<OutputMessage>("encryptRSA", &OutputMessage::encryptRSA);
|
||||
|
||||
// Application
|
||||
g_lua.registerStaticClass("g_app");
|
||||
|
@@ -610,7 +610,7 @@ void LuaInterface::createLuaState()
|
||||
|
||||
// load lua standard libraries
|
||||
luaL_openlibs(L);
|
||||
|
||||
|
||||
// load bit32 lib for bitwise operations
|
||||
luaopen_bit32(L);
|
||||
|
||||
@@ -998,6 +998,11 @@ void LuaInterface::pushCString(const char* v)
|
||||
lua_pushstring(L, v);
|
||||
}
|
||||
|
||||
void LuaInterface::pushString(const std::string& v)
|
||||
{
|
||||
lua_pushlstring(L, v.c_str(), v.length());
|
||||
}
|
||||
|
||||
void LuaInterface::pushLightUserdata(void* p)
|
||||
{
|
||||
lua_pushlightuserdata(L, p);
|
||||
@@ -1121,9 +1126,10 @@ std::string LuaInterface::toString(int index)
|
||||
{
|
||||
assert(hasIndex(index));
|
||||
std::string str;
|
||||
const char *c_str = lua_tostring(L, index);
|
||||
if(c_str)
|
||||
str = c_str;
|
||||
size_t len;
|
||||
const char *c_str = lua_tolstring(L, index, &len);
|
||||
if(c_str && len > 0)
|
||||
str.assign(c_str, len);
|
||||
return str;
|
||||
}
|
||||
|
||||
|
@@ -269,7 +269,7 @@ public:
|
||||
void pushNumber(double v);
|
||||
void pushBoolean(bool v);
|
||||
void pushCString(const char* v);
|
||||
void pushString(const std::string& v) { pushCString(v.c_str()); }
|
||||
void pushString(const std::string& v);
|
||||
void pushLightUserdata(void* p);
|
||||
void pushThread();
|
||||
void pushValue(int index = -1);
|
||||
|
@@ -28,12 +28,14 @@
|
||||
|
||||
namespace asio = boost::asio;
|
||||
|
||||
class Connection;
|
||||
class InputMessage;
|
||||
class OutputMessage;
|
||||
class Connection;
|
||||
class Protocol;
|
||||
class Server;
|
||||
|
||||
typedef std::shared_ptr<InputMessage> InputMessagePtr;
|
||||
typedef std::shared_ptr<OutputMessage> OutputMessagePtr;
|
||||
typedef std::shared_ptr<Connection> ConnectionPtr;
|
||||
typedef std::weak_ptr<Connection> ConnectionWeakPtr;
|
||||
typedef std::shared_ptr<Protocol> ProtocolPtr;
|
||||
|
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
#include "inputmessage.h"
|
||||
#include "rsa.h"
|
||||
|
||||
InputMessage::InputMessage()
|
||||
{
|
||||
@@ -106,6 +107,11 @@ std::string InputMessage::getString()
|
||||
return std::string(v, stringLength);
|
||||
}
|
||||
|
||||
void InputMessage::decryptRSA(int size, const std::string& p, const std::string& q, const std::string& d)
|
||||
{
|
||||
RSA::decrypt((char*)m_buffer + m_readPos, size, p.c_str(), q.c_str(), d.c_str());
|
||||
}
|
||||
|
||||
bool InputMessage::canRead(int bytes)
|
||||
{
|
||||
if((m_readPos - m_headerPos + bytes > m_messageSize) || (m_readPos + bytes > BUFFER_MAXSIZE))
|
||||
|
@@ -25,8 +25,9 @@
|
||||
|
||||
#include "declarations.h"
|
||||
#include "networkexception.h"
|
||||
#include <framework/luascript/luaobject.h>
|
||||
|
||||
class InputMessage
|
||||
class InputMessage : public LuaObject
|
||||
{
|
||||
public:
|
||||
enum {
|
||||
@@ -36,32 +37,38 @@ public:
|
||||
|
||||
InputMessage();
|
||||
|
||||
void reset();
|
||||
|
||||
void fillBuffer(uint8 *buffer, uint16 size);
|
||||
uint16 readSize() { return getU16(); }
|
||||
bool readChecksum();
|
||||
|
||||
void setHeaderSize(uint16 size);
|
||||
void setMessageSize(uint16 size) { m_messageSize = size; }
|
||||
|
||||
void skipBytes(uint16 bytes) { m_readPos += bytes; }
|
||||
|
||||
uint8 getU8(bool peek = false);
|
||||
uint16 getU16(bool peek = false);
|
||||
uint32 getU32(bool peek = false);
|
||||
uint64 getU64(bool peek = false);
|
||||
std::string getString();
|
||||
|
||||
void decryptRSA(int size, const std::string& p, const std::string& q, const std::string& d);
|
||||
|
||||
int getReadSize() { return m_readPos - m_headerPos; }
|
||||
int getUnreadSize() { return m_messageSize - (m_readPos - m_headerPos); }
|
||||
|
||||
bool eof() { return (m_readPos - m_headerPos) >= m_messageSize; }
|
||||
|
||||
protected:
|
||||
void reset();
|
||||
|
||||
void fillBuffer(uint8 *buffer, uint16 size);
|
||||
|
||||
void setHeaderSize(uint16 size);
|
||||
void setMessageSize(uint16 size) { m_messageSize = size; }
|
||||
|
||||
uint8* getReadBuffer() { return m_buffer + m_readPos; }
|
||||
uint8* getHeaderBuffer() { return m_buffer + m_headerPos; }
|
||||
uint8* getDataBuffer() { return m_buffer + MAX_HEADER_SIZE; }
|
||||
uint16 getHeaderSize() { return (MAX_HEADER_SIZE - m_headerPos); }
|
||||
uint16 getMessageSize() { return m_messageSize; }
|
||||
int getReadSize() { return m_readPos - m_headerPos; }
|
||||
int getUnreadSize() { return m_messageSize - (m_readPos - m_headerPos); }
|
||||
|
||||
bool eof() { return (m_readPos - m_headerPos) >= m_messageSize; }
|
||||
uint16 readSize() { return getU16(); }
|
||||
bool readChecksum();
|
||||
|
||||
friend class Protocol;
|
||||
|
||||
private:
|
||||
bool canRead(int bytes);
|
||||
|
@@ -21,6 +21,7 @@
|
||||
*/
|
||||
|
||||
#include <framework/net/outputmessage.h>
|
||||
#include "rsa.h"
|
||||
|
||||
OutputMessage::OutputMessage()
|
||||
{
|
||||
@@ -66,20 +67,16 @@ void OutputMessage::addU64(uint64 value)
|
||||
m_messageSize += 8;
|
||||
}
|
||||
|
||||
void OutputMessage::addString(const char* value, int length)
|
||||
void OutputMessage::addString(const std::string& buffer)
|
||||
{
|
||||
if(length > 65535)
|
||||
throw NetworkException("[OutputMessage::addString] string length > 65535");
|
||||
checkWrite(length + 2);
|
||||
addU16(length);
|
||||
memcpy((char*)(m_buffer + m_writePos), value, length);
|
||||
m_writePos += length;
|
||||
m_messageSize += length;
|
||||
}
|
||||
|
||||
void OutputMessage::addString(const std::string& value)
|
||||
{
|
||||
addString(value.c_str(), value.length());
|
||||
int len = buffer.length();
|
||||
if(len > MAX_STRING_LENGTH)
|
||||
throw NetworkException("string length > MAX_STRING_LENGTH");
|
||||
checkWrite(len + 2);
|
||||
addU16(len);
|
||||
memcpy((char*)(m_buffer + m_writePos), buffer.c_str(), len);
|
||||
m_writePos += len;
|
||||
m_messageSize += len;
|
||||
}
|
||||
|
||||
void OutputMessage::addPaddingBytes(int bytes, uint8 byte)
|
||||
@@ -92,6 +89,11 @@ void OutputMessage::addPaddingBytes(int bytes, uint8 byte)
|
||||
m_messageSize += bytes;
|
||||
}
|
||||
|
||||
void OutputMessage::encryptRSA(int size, const std::string& key)
|
||||
{
|
||||
RSA::encrypt((char*)m_buffer + m_writePos - size, size, key.c_str());
|
||||
}
|
||||
|
||||
void OutputMessage::writeChecksum()
|
||||
{
|
||||
uint32 checksum = Fw::getAdlerChecksum(m_buffer + m_headerPos, m_messageSize);
|
||||
|
@@ -27,13 +27,12 @@
|
||||
#include "networkexception.h"
|
||||
#include <framework/luascript/luaobject.h>
|
||||
|
||||
typedef std::shared_ptr<OutputMessage> OutputMessagePtr;
|
||||
|
||||
class OutputMessage : public LuaObject
|
||||
{
|
||||
public:
|
||||
enum {
|
||||
BUFFER_MAXSIZE = 1024,
|
||||
MAX_STRING_LENGTH = 65536,
|
||||
MAX_HEADER_SIZE = 8
|
||||
};
|
||||
|
||||
@@ -45,10 +44,12 @@ public:
|
||||
void addU16(uint16 value);
|
||||
void addU32(uint32 value);
|
||||
void addU64(uint64 value);
|
||||
void addString(const char* value, int length);
|
||||
void addString(const std::string& value);
|
||||
void addString(const std::string& buffer);
|
||||
void addPaddingBytes(int bytes, uint8 byte = 0);
|
||||
|
||||
void encryptRSA(int size, const std::string& key);
|
||||
|
||||
protected:
|
||||
uint8* getWriteBuffer() { return m_buffer + m_writePos; }
|
||||
uint8* getHeaderBuffer() { return m_buffer + m_headerPos; }
|
||||
uint8* getDataBuffer() { return m_buffer + MAX_HEADER_SIZE; }
|
||||
@@ -57,6 +58,8 @@ public:
|
||||
void writeChecksum();
|
||||
void writeMessageSize();
|
||||
|
||||
friend class Protocol;
|
||||
|
||||
private:
|
||||
bool canWrite(int bytes);
|
||||
void checkWrite(int bytes);
|
||||
|
@@ -27,6 +27,7 @@ Protocol::Protocol()
|
||||
{
|
||||
m_xteaEncryptionEnabled = false;
|
||||
m_checksumEnabled = false;
|
||||
m_inputMessage = InputMessagePtr(new InputMessage);
|
||||
}
|
||||
|
||||
Protocol::~Protocol()
|
||||
@@ -63,7 +64,7 @@ bool Protocol::isConnecting()
|
||||
return false;
|
||||
}
|
||||
|
||||
void Protocol::send(OutputMessage& outputMessage)
|
||||
void Protocol::send(const OutputMessagePtr& outputMessage)
|
||||
{
|
||||
// encrypt
|
||||
if(m_xteaEncryptionEnabled)
|
||||
@@ -71,19 +72,19 @@ void Protocol::send(OutputMessage& outputMessage)
|
||||
|
||||
// write checksum
|
||||
if(m_checksumEnabled)
|
||||
outputMessage.writeChecksum();
|
||||
outputMessage->writeChecksum();
|
||||
|
||||
// wirte message size
|
||||
outputMessage.writeMessageSize();
|
||||
outputMessage->writeMessageSize();
|
||||
|
||||
// send
|
||||
if(m_connection)
|
||||
m_connection->write(outputMessage.getHeaderBuffer(), outputMessage.getMessageSize());
|
||||
m_connection->write(outputMessage->getHeaderBuffer(), outputMessage->getMessageSize());
|
||||
}
|
||||
|
||||
void Protocol::recv()
|
||||
{
|
||||
m_inputMessage.reset();
|
||||
m_inputMessage->reset();
|
||||
|
||||
// first update message header size
|
||||
int headerSize = 2; // 2 bytes for message size
|
||||
@@ -91,7 +92,7 @@ void Protocol::recv()
|
||||
headerSize += 4; // 4 bytes for checksum
|
||||
if(m_xteaEncryptionEnabled)
|
||||
headerSize += 2; // 2 bytes for XTEA encrypted message size
|
||||
m_inputMessage.setHeaderSize(headerSize);
|
||||
m_inputMessage->setHeaderSize(headerSize);
|
||||
|
||||
// read the first 2 bytes which contain the message size
|
||||
if(m_connection)
|
||||
@@ -101,8 +102,8 @@ void Protocol::recv()
|
||||
void Protocol::internalRecvHeader(uint8* buffer, uint16 size)
|
||||
{
|
||||
// read message size
|
||||
m_inputMessage.fillBuffer(buffer, size);
|
||||
uint16 remainingSize = m_inputMessage.readSize();
|
||||
m_inputMessage->fillBuffer(buffer, size);
|
||||
uint16 remainingSize = m_inputMessage->readSize();
|
||||
|
||||
// read remaining message data
|
||||
if(m_connection)
|
||||
@@ -117,9 +118,9 @@ void Protocol::internalRecvData(uint8* buffer, uint16 size)
|
||||
return;
|
||||
}
|
||||
|
||||
m_inputMessage.fillBuffer(buffer, size);
|
||||
m_inputMessage->fillBuffer(buffer, size);
|
||||
|
||||
if(m_checksumEnabled && !m_inputMessage.readChecksum()) {
|
||||
if(m_checksumEnabled && !m_inputMessage->readChecksum()) {
|
||||
logTraceError("got a network message with invalid checksum");
|
||||
return;
|
||||
}
|
||||
@@ -130,8 +131,8 @@ void Protocol::internalRecvData(uint8* buffer, uint16 size)
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
int size = m_inputMessage.getU16();
|
||||
if(size != m_inputMessage.getUnreadSize()) {
|
||||
int size = m_inputMessage->getU16();
|
||||
if(size != m_inputMessage->getUnreadSize()) {
|
||||
logTraceError("invalid message size");
|
||||
return;
|
||||
}
|
||||
@@ -150,15 +151,15 @@ void Protocol::generateXteaKey()
|
||||
m_xteaKey[3] = unif(eng);
|
||||
}
|
||||
|
||||
bool Protocol::xteaDecrypt(InputMessage& inputMessage)
|
||||
bool Protocol::xteaDecrypt(const InputMessagePtr& inputMessage)
|
||||
{
|
||||
uint16 encryptedSize = inputMessage.getUnreadSize();
|
||||
uint16 encryptedSize = inputMessage->getUnreadSize();
|
||||
if(encryptedSize % 8 != 0) {
|
||||
logTraceError("invalid encrypted network message");
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32 *buffer = (uint32*)(inputMessage.getReadBuffer());
|
||||
uint32 *buffer = (uint32*)(inputMessage->getReadBuffer());
|
||||
int readPos = 0;
|
||||
|
||||
while(readPos < encryptedSize/4) {
|
||||
@@ -175,31 +176,31 @@ bool Protocol::xteaDecrypt(InputMessage& inputMessage)
|
||||
readPos = readPos + 2;
|
||||
}
|
||||
|
||||
uint16 decryptedSize = inputMessage.getU16() + 2;
|
||||
uint16 decryptedSize = inputMessage->getU16() + 2;
|
||||
int sizeDelta = decryptedSize - encryptedSize;
|
||||
if(sizeDelta > 0 || -sizeDelta > encryptedSize) {
|
||||
logTraceError("invalid decrypted a network message");
|
||||
return false;
|
||||
}
|
||||
|
||||
inputMessage.setMessageSize(inputMessage.getMessageSize() + sizeDelta);
|
||||
inputMessage->setMessageSize(inputMessage->getMessageSize() + sizeDelta);
|
||||
return true;
|
||||
}
|
||||
|
||||
void Protocol::xteaEncrypt(OutputMessage& outputMessage)
|
||||
void Protocol::xteaEncrypt(const OutputMessagePtr& outputMessage)
|
||||
{
|
||||
outputMessage.writeMessageSize();
|
||||
uint16 encryptedSize = outputMessage.getMessageSize();
|
||||
outputMessage->writeMessageSize();
|
||||
uint16 encryptedSize = outputMessage->getMessageSize();
|
||||
|
||||
//add bytes until reach 8 multiple
|
||||
if((encryptedSize % 8) != 0) {
|
||||
uint16 n = 8 - (encryptedSize % 8);
|
||||
outputMessage.addPaddingBytes(n);
|
||||
outputMessage->addPaddingBytes(n);
|
||||
encryptedSize += n;
|
||||
}
|
||||
|
||||
int readPos = 0;
|
||||
uint32 *buffer = (uint32*)(outputMessage.getDataBuffer() - 2);
|
||||
uint32 *buffer = (uint32*)(outputMessage->getDataBuffer() - 2);
|
||||
while(readPos < encryptedSize / 4) {
|
||||
uint32 v0 = buffer[readPos], v1 = buffer[readPos + 1];
|
||||
uint32 delta = 0x61C88647;
|
||||
|
@@ -41,33 +41,34 @@ public:
|
||||
bool isConnected();
|
||||
bool isConnecting();
|
||||
|
||||
void send(OutputMessage& outputMessage);
|
||||
void recv();
|
||||
|
||||
void internalRecvHeader(uint8* buffer, uint16 size);
|
||||
void internalRecvData(uint8* buffer, uint16 size);
|
||||
|
||||
virtual void onConnect() = 0;
|
||||
virtual void onRecv(InputMessage& inputMessage) = 0;
|
||||
virtual void onError(const boost::system::error_code& err) = 0;
|
||||
virtual void send(const OutputMessagePtr& outputMessage);
|
||||
|
||||
ProtocolPtr asProtocol() { return std::static_pointer_cast<Protocol>(shared_from_this()); }
|
||||
|
||||
protected:
|
||||
void recv();
|
||||
|
||||
virtual void onConnect() = 0;
|
||||
virtual void onRecv(const InputMessagePtr& inputMessage) = 0;
|
||||
virtual void onError(const boost::system::error_code& err) = 0;
|
||||
|
||||
void enableChecksum() { m_checksumEnabled = true; }
|
||||
void enableXteaEncryption() { m_xteaEncryptionEnabled = true; }
|
||||
void generateXteaKey();
|
||||
|
||||
uint32 m_xteaKey[4];
|
||||
InputMessage m_inputMessage;
|
||||
|
||||
private:
|
||||
bool xteaDecrypt(InputMessage& inputMessage);
|
||||
void xteaEncrypt(OutputMessage& outputMessage);
|
||||
void internalRecvHeader(uint8* buffer, uint16 size);
|
||||
void internalRecvData(uint8* buffer, uint16 size);
|
||||
|
||||
bool xteaDecrypt(const InputMessagePtr& inputMessage);
|
||||
void xteaEncrypt(const OutputMessagePtr& outputMessage);
|
||||
|
||||
bool m_checksumEnabled;
|
||||
bool m_xteaEncryptionEnabled;
|
||||
ConnectionPtr m_connection;
|
||||
InputMessagePtr m_inputMessage;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@@ -21,56 +21,12 @@
|
||||
*/
|
||||
|
||||
#include "rsa.h"
|
||||
#include <gmp.h>
|
||||
|
||||
Rsa::Rsa()
|
||||
void RSA::encrypt(char *msg, int size, const char* key)
|
||||
{
|
||||
m_keySet = false;
|
||||
mpz_init2(m_p, 1024);
|
||||
mpz_init2(m_q, 1024);
|
||||
mpz_init2(m_d, 1024);
|
||||
mpz_init2(m_u, 1024);
|
||||
mpz_init2(m_dp, 1024);
|
||||
mpz_init2(m_dq, 1024);
|
||||
mpz_init2(m_mod, 1024);
|
||||
}
|
||||
assert(size <= 128);
|
||||
|
||||
Rsa::~Rsa()
|
||||
{
|
||||
mpz_clear(m_p);
|
||||
mpz_clear(m_q);
|
||||
mpz_clear(m_d);
|
||||
mpz_clear(m_u);
|
||||
mpz_clear(m_dp);
|
||||
mpz_clear(m_dq);
|
||||
mpz_clear(m_mod);
|
||||
}
|
||||
|
||||
void Rsa::setKey(const char* p, const char* q, const char* d)
|
||||
{
|
||||
mpz_set_str(m_p, p, 10);
|
||||
mpz_set_str(m_q, q, 10);
|
||||
mpz_set_str(m_d, d, 10);
|
||||
|
||||
mpz_t pm1,qm1;
|
||||
mpz_init2(pm1,520);
|
||||
mpz_init2(qm1,520);
|
||||
|
||||
mpz_sub_ui(pm1, m_p, 1);
|
||||
mpz_sub_ui(qm1, m_q, 1);
|
||||
mpz_invert(m_u, m_p, m_q);
|
||||
mpz_mod(m_dp, m_d, pm1);
|
||||
mpz_mod(m_dq, m_d, qm1);
|
||||
|
||||
mpz_mul(m_mod, m_p, m_q);
|
||||
|
||||
mpz_clear(pm1);
|
||||
mpz_clear(qm1);
|
||||
|
||||
m_keySet = true;
|
||||
}
|
||||
|
||||
void Rsa::encrypt(char* msg, int32_t size, const char* key)
|
||||
{
|
||||
mpz_t plain, c;
|
||||
mpz_init2(plain, 1024);
|
||||
mpz_init2(c, 1024);
|
||||
@@ -83,12 +39,12 @@ void Rsa::encrypt(char* msg, int32_t size, const char* key)
|
||||
mpz_init2(mod, 1024);
|
||||
mpz_set_str(mod, key, 10);
|
||||
|
||||
mpz_import(plain, 128, 1, 1, 0, 0, msg);
|
||||
mpz_import(plain, size, 1, 1, 0, 0, msg);
|
||||
mpz_powm(c, plain, e, mod);
|
||||
|
||||
size_t count = (mpz_sizeinbase(c, 2) + 7)/8;
|
||||
memset(msg, 0, 128 - count);
|
||||
mpz_export(&msg[128 - count], NULL, 1, 1, 0, 0, c);
|
||||
memset(msg, 0, size - count);
|
||||
mpz_export(&msg[size - count], NULL, 1, 1, 0, 0, c);
|
||||
|
||||
mpz_clear(c);
|
||||
mpz_clear(plain);
|
||||
@@ -96,43 +52,73 @@ void Rsa::encrypt(char* msg, int32_t size, const char* key)
|
||||
mpz_clear(mod);
|
||||
}
|
||||
|
||||
bool Rsa::decrypt(char* msg, int32_t size)
|
||||
void RSA::decrypt(char *msg, int size, const char *p, const char *q, const char *d)
|
||||
{
|
||||
if(!m_keySet)
|
||||
return false;
|
||||
assert(size <= 128);
|
||||
|
||||
mpz_t c,v1,v2,u2,tmp;
|
||||
mpz_t mp, mq, md, u, dp, dq, mod, c, v1, v2, u2, tmp;
|
||||
mpz_init2(mp, 1024);
|
||||
mpz_init2(mq, 1024);
|
||||
mpz_init2(md, 1024);
|
||||
mpz_init2(u, 1024);
|
||||
mpz_init2(dp, 1024);
|
||||
mpz_init2(dq, 1024);
|
||||
mpz_init2(mod, 1024);
|
||||
mpz_init2(c, 1024);
|
||||
mpz_init2(v1, 1024);
|
||||
mpz_init2(v2, 1024);
|
||||
mpz_init2(u2, 1024);
|
||||
mpz_init2(tmp, 1024);
|
||||
|
||||
mpz_import(c, 128, 1, 1, 0, 0, msg);
|
||||
mpz_set_str(mp, p, 10);
|
||||
mpz_set_str(mq, q, 10);
|
||||
mpz_set_str(md, d, 10);
|
||||
|
||||
mpz_mod(tmp, c, m_p);
|
||||
mpz_powm(v1, tmp, m_dp, m_p);
|
||||
mpz_mod(tmp, c, m_q);
|
||||
mpz_powm(v2, tmp, m_dq, m_q);
|
||||
mpz_t pm1,qm1;
|
||||
mpz_init2(pm1, 520);
|
||||
mpz_init2(qm1, 520);
|
||||
|
||||
mpz_sub_ui(pm1, mp, 1);
|
||||
mpz_sub_ui(qm1, mq, 1);
|
||||
mpz_invert(u, mp, mq);
|
||||
mpz_mod(dp, md, pm1);
|
||||
mpz_mod(dq, md, qm1);
|
||||
|
||||
mpz_mul(mod, mp, mq);
|
||||
|
||||
mpz_import(c, size, 1, 1, 0, 0, msg);
|
||||
|
||||
mpz_mod(tmp, c, mp);
|
||||
mpz_powm(v1, tmp, dp, mp);
|
||||
mpz_mod(tmp, c, mq);
|
||||
mpz_powm(v2, tmp, dq, mq);
|
||||
mpz_sub(u2, v2, v1);
|
||||
mpz_mul(tmp, u2, m_u);
|
||||
mpz_mod(u2, tmp, m_q);
|
||||
if(mpz_cmp_si(u2, 0) < 0){
|
||||
mpz_add(tmp, u2, m_q);
|
||||
mpz_set(u2, tmp);
|
||||
mpz_mul(tmp, u2, u);
|
||||
mpz_mod(u2, tmp, mq);
|
||||
if(mpz_cmp_si(u2, 0) < 0) {
|
||||
mpz_add(tmp, u2, mq);
|
||||
mpz_set(u2, tmp);
|
||||
}
|
||||
mpz_mul(tmp, u2, m_p);
|
||||
mpz_mul(tmp, u2, mp);
|
||||
mpz_set_ui(c, 0);
|
||||
mpz_add(c, v1, tmp);
|
||||
|
||||
size_t count = (mpz_sizeinbase(c, 2) + 7)/8;
|
||||
memset(msg, 0, 128 - count);
|
||||
mpz_export(&msg[128 - count], NULL, 1, 1, 0, 0, c);
|
||||
memset(msg, 0, size - count);
|
||||
mpz_export(&msg[size - count], NULL, 1, 1, 0, 0, c);
|
||||
|
||||
mpz_clear(c);
|
||||
mpz_clear(v1);
|
||||
mpz_clear(v2);
|
||||
mpz_clear(u2);
|
||||
mpz_clear(tmp);
|
||||
return true;
|
||||
mpz_clear(pm1);
|
||||
mpz_clear(qm1);
|
||||
mpz_clear(mp);
|
||||
mpz_clear(mq);
|
||||
mpz_clear(md);
|
||||
mpz_clear(u);
|
||||
mpz_clear(dp);
|
||||
mpz_clear(dq);
|
||||
mpz_clear(mod);
|
||||
}
|
||||
|
@@ -24,22 +24,11 @@
|
||||
#define RSA_H
|
||||
|
||||
#include <framework/global.h>
|
||||
#include <gmp.h>
|
||||
|
||||
class Rsa
|
||||
namespace RSA
|
||||
{
|
||||
public:
|
||||
Rsa();
|
||||
~Rsa();
|
||||
|
||||
void setKey(const char* p, const char* q, const char* d);
|
||||
bool decrypt(char* msg, int32_t size);
|
||||
static void encrypt(char* msg, int32_t size, const char* key);
|
||||
|
||||
protected:
|
||||
bool m_keySet;
|
||||
|
||||
mpz_t m_p, m_q, m_u, m_d, m_dp, m_dq, m_mod;
|
||||
void encrypt(char *msg, int size, const char *key);
|
||||
void decrypt(char *msg, int size, const char *p, const char *q, const char *d);
|
||||
};
|
||||
|
||||
#endif
|
@@ -63,7 +63,6 @@ void UIManager::resize(const Size& size)
|
||||
|
||||
void UIManager::inputEvent(const InputEvent& event)
|
||||
{
|
||||
m_isOnInputEvent = true;
|
||||
UIWidgetList widgetList;
|
||||
switch(event.type) {
|
||||
case Fw::KeyTextInputEvent:
|
||||
@@ -155,7 +154,6 @@ void UIManager::inputEvent(const InputEvent& event)
|
||||
default:
|
||||
break;
|
||||
};
|
||||
m_isOnInputEvent = false;
|
||||
}
|
||||
|
||||
void UIManager::updatePressedWidget(const UIWidgetPtr& newPressedWidget, const Point& clickedPos)
|
||||
|
@@ -62,7 +62,6 @@ public:
|
||||
UIWidgetPtr getPressedWidget() { return m_pressedWidget; }
|
||||
UIWidgetPtr getRootWidget() { return m_rootWidget; }
|
||||
|
||||
bool isOnInputEvent() { return m_isOnInputEvent; }
|
||||
bool isDrawingDebugBoxes() { return m_drawDebugBoxes; }
|
||||
|
||||
protected:
|
||||
@@ -80,7 +79,6 @@ private:
|
||||
UIWidgetPtr m_hoveredWidget;
|
||||
UIWidgetPtr m_pressedWidget;
|
||||
Boolean<false> m_hoverUpdateScheduled;
|
||||
bool m_isOnInputEvent;
|
||||
Boolean<false> m_drawDebugBoxes;
|
||||
std::unordered_map<std::string, OTMLNodePtr> m_styles;
|
||||
};
|
||||
|
Reference in New Issue
Block a user