forked from TLeonardUK/ds3os
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a super janky reliable-udp implementation, it only implements the incoming connection side, as we are only worried about the server right now. Its enough now to successfully handshake a negotiate a connection. It falls over once the first DAT message is received as we don't handle them yet. Message protocol needs implementing next so we can handle the DAT messages and actually do something useful.
- Loading branch information
1 parent
9e085bb
commit fb33123
Showing
18 changed files
with
1,101 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Dark Souls 3 - Open Server | ||
|
||
#include "Core/Crypto/CWCServerUDPCipher.h" | ||
|
||
#include "Core/Utils/Logging.h" | ||
#include "Core/Utils/Random.h" | ||
#include "Core/Utils/Endian.h" | ||
#include "Core/Utils/Strings.h" | ||
|
||
// Basically the same as CWCCipher except for different header verification. | ||
|
||
CWCServerUDPCipher::CWCServerUDPCipher(const std::vector<uint8_t>& InKey, uint64_t InAuthToken) | ||
: Key(InKey) | ||
, AuthToken(InAuthToken) | ||
{ | ||
cwc_init_and_key(InKey.data(), InKey.size(), &CwcContext); | ||
|
||
// Auth token bytes to encode in the header are the reversed auth token. | ||
uint8_t* InAuthTokenBytes = reinterpret_cast<uint8_t*>(&InAuthToken); | ||
AuthTokenHeaderBytes.assign(InAuthTokenBytes, InAuthTokenBytes + 8); | ||
} | ||
|
||
bool CWCServerUDPCipher::Encrypt(const std::vector<uint8_t>& Input, std::vector<uint8_t>& Output) | ||
{ | ||
std::vector<uint8_t> IV(11, 0); | ||
std::vector<uint8_t> Tag(16, 0); | ||
std::vector<uint8_t> Payload = Input; | ||
|
||
FillRandomBytes(IV); | ||
|
||
std::vector<uint8_t> Header; | ||
Header.resize(11); | ||
memcpy(Header.data(), IV.data(), 11); | ||
|
||
if (cwc_encrypt_message(IV.data(), 11, Header.data(), Header.size(), (unsigned char*)Payload.data(), Payload.size(), Tag.data(), 16, &CwcContext) == RETURN_ERROR) | ||
{ | ||
return false; | ||
} | ||
|
||
Output.resize(Payload.size() + 11 + 16); | ||
|
||
memcpy(Output.data(), IV.data(), 11); | ||
memcpy(Output.data() + 11, Tag.data(), 16); | ||
memcpy(Output.data() + 11 + 16, Payload.data(), Payload.size()); | ||
|
||
//Log("EncryptServer: PayloadSize=%i IV=%s Tag=%s Header=%s", Payload.size(), BytesToHex(IV).c_str(), BytesToHex(Tag).c_str(), BytesToHex(Header).c_str()); | ||
|
||
return true; | ||
} | ||
|
||
bool CWCServerUDPCipher::Decrypt(const std::vector<uint8_t>& Input, std::vector<uint8_t>& Output) | ||
{ | ||
std::vector<uint8_t> IV(11); | ||
std::vector<uint8_t> Tag(16); | ||
|
||
// Actually enough data for any data? | ||
if (Input.size() < 11 + 16 + 1) | ||
{ | ||
return false; | ||
} | ||
|
||
Output.resize(Input.size() - 11 - 16); | ||
|
||
memcpy(IV.data(), Input.data(), 11); | ||
memcpy(Tag.data(), Input.data() + 11, 16); | ||
memcpy(Output.data(), Input.data() + 11 + 16, Output.size()); | ||
|
||
std::vector<uint8_t> Header; | ||
Header.resize(11); | ||
memcpy(Header.data(), IV.data(), 11); | ||
|
||
//Log("DecryptServer: PayloadSize=%i IV=%s Tag=%s Header=%s", Output.size(), BytesToHex(IV).c_str(), BytesToHex(Tag).c_str(), BytesToHex(Header).c_str()); | ||
|
||
if (cwc_decrypt_message(IV.data(), 11, Header.data(), Header.size(), (unsigned char*)Output.data(), Output.size(), Tag.data(), 16, &CwcContext) == RETURN_ERROR) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Dark Souls 3 - Open Server | ||
|
||
#pragma once | ||
|
||
#include "Core/Crypto/Cipher.h" | ||
|
||
#include "cwc.h" | ||
|
||
#include <vector> | ||
|
||
class CWCServerUDPCipher | ||
: public Cipher | ||
{ | ||
public: | ||
|
||
CWCServerUDPCipher(const std::vector<uint8_t>& key, uint64_t AuthToken); | ||
|
||
bool Encrypt(const std::vector<uint8_t>& input, std::vector<uint8_t>& Output) override; | ||
bool Decrypt(const std::vector<uint8_t>& input, std::vector<uint8_t>& Output) override; | ||
|
||
private: | ||
std::vector<uint8_t> Key; | ||
|
||
cwc_ctx CwcContext; | ||
|
||
uint64_t AuthToken; | ||
std::vector<uint8_t> AuthTokenHeaderBytes; | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.