TrinityCore
WorldPacketCrypt.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "WorldPacketCrypt.h"
19#include <array>
20#include <cstring>
21
22WorldPacketCrypt::WorldPacketCrypt() : _clientDecrypt(false), _serverEncrypt(true), _clientCounter(0), _serverCounter(0), _initialized(false)
23{
24}
25
27{
30 _initialized = true;
31}
32
34{
36 {
37 memcpy(Value.data(), &counter, sizeof(uint64));
38 memcpy(Value.data() + sizeof(uint64), &magic, sizeof(uint32));
39 }
40
41 std::array<uint8, 12> Value;
42};
43
44bool WorldPacketCrypt::PeekDecryptRecv(uint8* data, size_t length)
45{
46 if (_initialized)
47 {
48 WorldPacketCryptIV iv{ _clientCounter, 0x544E4C43 };
49 if (!_clientDecrypt.ProcessNoIntegrityCheck(iv.Value, data, length))
50 return false;
51 }
52
53 return true;
54}
55
57{
58 if (_initialized)
59 {
60 WorldPacketCryptIV iv{ _clientCounter, 0x544E4C43 };
61 if (!_clientDecrypt.Process(iv.Value, data, length, tag))
62 return false;
63 }
64 else
65 memset(tag, 0, sizeof(tag));
66
68 return true;
69}
70
72{
73 if (_initialized)
74 {
75 WorldPacketCryptIV iv{ _serverCounter, 0x52565253 };
76 if (!_serverEncrypt.Process(iv.Value, data, length, tag))
77 return false;
78 }
79 else
80 memset(tag, 0, sizeof(tag));
81
83 return true;
84}
uint8_t uint8
Definition: Define.h:144
uint64_t uint64
Definition: Define.h:141
uint32_t uint32
Definition: Define.h:142
uint8[TAG_SIZE_BYTES] Tag
Definition: AES.h:36
bool ProcessNoIntegrityCheck(IV const &iv, uint8 *data, size_t partialLength)
Definition: AES.cpp:67
std::array< uint8, KEY_SIZE_BYTES > Key
Definition: AES.h:35
bool Process(IV const &iv, uint8 *data, size_t length, Tag &tag)
Definition: AES.cpp:40
void Init(Key const &key)
Definition: AES.cpp:34
Trinity::Crypto::AES _clientDecrypt
bool PeekDecryptRecv(uint8 *data, size_t length)
void Init(Trinity::Crypto::AES::Key const &key)
Trinity::Crypto::AES _serverEncrypt
bool EncryptSend(uint8 *data, size_t length, Trinity::Crypto::AES::Tag &tag)
bool DecryptRecv(uint8 *data, size_t length, Trinity::Crypto::AES::Tag &tag)
std::array< uint8, 12 > Value
WorldPacketCryptIV(uint64 counter, uint32 magic)