From a142eb9f7a9683f98fe1e9153f6958f80d374c9d Mon Sep 17 00:00:00 2001 From: Shauren Date: Wed, 30 Apr 2014 20:16:08 +0200 Subject: Core/Auth: Battle.net stuff --- src/server/shared/Cryptography/HmacHash.cpp | 59 +++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/server/shared/Cryptography/HmacHash.cpp (limited to 'src/server/shared/Cryptography/HmacHash.cpp') diff --git a/src/server/shared/Cryptography/HmacHash.cpp b/src/server/shared/Cryptography/HmacHash.cpp new file mode 100644 index 00000000000..7a365ade457 --- /dev/null +++ b/src/server/shared/Cryptography/HmacHash.cpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2008-2014 TrinityCore + * Copyright (C) 2005-2009 MaNGOS + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + */ + +#include "HmacHash.h" +#include "BigNumber.h" +#include "Common.h" + +HmacHash::HmacHash(uint32 len, uint8 *seed, EVP_MD const* hasher, uint32 digestLength) +{ + HMAC_CTX_init(&_ctx); + HMAC_Init_ex(&_ctx, seed, len, hasher, NULL); + _digest = new uint8[digestLength]; + memset(_digest, 0, digestLength); +} + +HmacHash::~HmacHash() +{ + HMAC_CTX_cleanup(&_ctx); + delete[] _digest; +} + +void HmacHash::UpdateData(const std::string &str) +{ + HMAC_Update(&_ctx, (uint8 const*)str.c_str(), str.length()); +} + +void HmacHash::UpdateData(const uint8* data, size_t len) +{ + HMAC_Update(&_ctx, data, len); +} + +void HmacHash::Finalize() +{ + uint32 length = 0; + HMAC_Final(&_ctx, _digest, &length); + ASSERT(length == _digestLength); +} + +uint8* HmacHash::ComputeHash(BigNumber* bn) +{ + HMAC_Update(&_ctx, bn->AsByteArray().get(), bn->GetNumBytes()); + Finalize(); + return _digest; +} -- cgit v1.2.3 From f0d6f87138a915825f9986fae80ccd1fb72c154c Mon Sep 17 00:00:00 2001 From: Shauren Date: Sun, 4 May 2014 11:49:32 +0200 Subject: Core/Battle.net: Fixed encryption --- src/server/authserver/Server/BattlenetPacketCrypt.cpp | 17 ++++++++++------- src/server/shared/Cryptography/HmacHash.cpp | 2 +- 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'src/server/shared/Cryptography/HmacHash.cpp') diff --git a/src/server/authserver/Server/BattlenetPacketCrypt.cpp b/src/server/authserver/Server/BattlenetPacketCrypt.cpp index 10aa684e10a..31fcfdd930a 100644 --- a/src/server/authserver/Server/BattlenetPacketCrypt.cpp +++ b/src/server/authserver/Server/BattlenetPacketCrypt.cpp @@ -26,14 +26,17 @@ Battlenet::PacketCrypt::PacketCrypt() : ::PacketCrypt(SHA256_DIGEST_LENGTH) void Battlenet::PacketCrypt::Init(BigNumber* K) { uint8 ServerEncryptionKey[SEED_KEY_SIZE] = { 0x68, 0xE0, 0xC7, 0x2E, 0xDD, 0xD6, 0xD2, 0xF3, 0x1E, 0x5A, 0xB1, 0x55, 0xB1, 0x8B, 0x63, 0x1E }; - HmacHash serverEncryptHmac(SEED_KEY_SIZE, ServerEncryptionKey, EVP_sha256(), SHA256_DIGEST_LENGTH); - uint8 *encryptHash = serverEncryptHmac.ComputeHash(K); - uint8 ClientDecryptionKey[SEED_KEY_SIZE] = { 0xDE, 0xA9, 0x65, 0xAE, 0x54, 0x3A, 0x1E, 0x93, 0x9E, 0x69, 0x0C, 0xAA, 0x68, 0xDE, 0x78, 0x39 }; - HmacHash clientDecryptHmac(SEED_KEY_SIZE, ClientDecryptionKey, EVP_sha256(), SHA256_DIGEST_LENGTH); - uint8 *decryptHash = clientDecryptHmac.ComputeHash(K); - _clientDecrypt.Init(decryptHash); - _serverEncrypt.Init(encryptHash); + HmacHash serverEncryptHmac(K->GetNumBytes(), K->AsByteArray().get(), EVP_sha256(), SHA256_DIGEST_LENGTH); + serverEncryptHmac.UpdateData(ServerEncryptionKey, SEED_KEY_SIZE); + serverEncryptHmac.Finalize(); + + HmacHash clientDecryptHmac(K->GetNumBytes(), K->AsByteArray().get(), EVP_sha256(), SHA256_DIGEST_LENGTH); + clientDecryptHmac.UpdateData(ClientDecryptionKey, SEED_KEY_SIZE); + clientDecryptHmac.Finalize(); + + _clientDecrypt.Init(clientDecryptHmac.GetDigest()); + _serverEncrypt.Init(serverEncryptHmac.GetDigest()); _initialized = true; } diff --git a/src/server/shared/Cryptography/HmacHash.cpp b/src/server/shared/Cryptography/HmacHash.cpp index 7a365ade457..71fc362ae5d 100644 --- a/src/server/shared/Cryptography/HmacHash.cpp +++ b/src/server/shared/Cryptography/HmacHash.cpp @@ -20,7 +20,7 @@ #include "BigNumber.h" #include "Common.h" -HmacHash::HmacHash(uint32 len, uint8 *seed, EVP_MD const* hasher, uint32 digestLength) +HmacHash::HmacHash(uint32 len, uint8 *seed, EVP_MD const* hasher, uint32 digestLength) : _digestLength(digestLength) { HMAC_CTX_init(&_ctx); HMAC_Init_ex(&_ctx, seed, len, hasher, NULL); -- cgit v1.2.3 From 7a27492071d79b036343b90ecdf1678548f3c550 Mon Sep 17 00:00:00 2001 From: Shauren Date: Tue, 3 Jun 2014 18:15:35 +0200 Subject: Core/Crypto: Refactored HmacHash to make it easier to use with different hash algorithms --- .../authserver/Server/BattlenetPacketCrypt.cpp | 4 +-- src/server/authserver/Server/BattlenetSocket.cpp | 12 ++++----- src/server/game/Warden/WardenWin.cpp | 2 +- .../Authentication/WorldPacketCrypt.cpp | 4 +-- src/server/shared/Cryptography/HmacHash.cpp | 29 ++++++++++++++-------- src/server/shared/Cryptography/HmacHash.h | 17 ++++++++----- 6 files changed, 40 insertions(+), 28 deletions(-) (limited to 'src/server/shared/Cryptography/HmacHash.cpp') diff --git a/src/server/authserver/Server/BattlenetPacketCrypt.cpp b/src/server/authserver/Server/BattlenetPacketCrypt.cpp index 31fcfdd930a..de4cf73f71c 100644 --- a/src/server/authserver/Server/BattlenetPacketCrypt.cpp +++ b/src/server/authserver/Server/BattlenetPacketCrypt.cpp @@ -28,11 +28,11 @@ void Battlenet::PacketCrypt::Init(BigNumber* K) uint8 ServerEncryptionKey[SEED_KEY_SIZE] = { 0x68, 0xE0, 0xC7, 0x2E, 0xDD, 0xD6, 0xD2, 0xF3, 0x1E, 0x5A, 0xB1, 0x55, 0xB1, 0x8B, 0x63, 0x1E }; uint8 ClientDecryptionKey[SEED_KEY_SIZE] = { 0xDE, 0xA9, 0x65, 0xAE, 0x54, 0x3A, 0x1E, 0x93, 0x9E, 0x69, 0x0C, 0xAA, 0x68, 0xDE, 0x78, 0x39 }; - HmacHash serverEncryptHmac(K->GetNumBytes(), K->AsByteArray().get(), EVP_sha256(), SHA256_DIGEST_LENGTH); + HmacSha256 serverEncryptHmac(K->GetNumBytes(), K->AsByteArray().get()); serverEncryptHmac.UpdateData(ServerEncryptionKey, SEED_KEY_SIZE); serverEncryptHmac.Finalize(); - HmacHash clientDecryptHmac(K->GetNumBytes(), K->AsByteArray().get(), EVP_sha256(), SHA256_DIGEST_LENGTH); + HmacSha256 clientDecryptHmac(K->GetNumBytes(), K->AsByteArray().get()); clientDecryptHmac.UpdateData(ClientDecryptionKey, SEED_KEY_SIZE); clientDecryptHmac.Finalize(); diff --git a/src/server/authserver/Server/BattlenetSocket.cpp b/src/server/authserver/Server/BattlenetSocket.cpp index db72cbf5e07..1cce8f2f94d 100644 --- a/src/server/authserver/Server/BattlenetSocket.cpp +++ b/src/server/authserver/Server/BattlenetSocket.cpp @@ -503,7 +503,7 @@ bool Battlenet::Socket::HandleRealmJoinRequest(PacketHeader& header, BitStream& result.ServerSeed = uint32(rand32()); uint8 sessionKey[40]; - HmacHash hmac(K.GetNumBytes(), K.AsByteArray().get(), EVP_sha1(), SHA_DIGEST_LENGTH); + HmacSha1 hmac(K.GetNumBytes(), K.AsByteArray().get()); hmac.UpdateData((uint8*)"WoW\0", 4); hmac.UpdateData((uint8*)&join.ClientSeed, 4); hmac.UpdateData((uint8*)&result.ServerSeed, 4); @@ -511,7 +511,7 @@ bool Battlenet::Socket::HandleRealmJoinRequest(PacketHeader& header, BitStream& memcpy(sessionKey, hmac.GetDigest(), hmac.GetLength()); - HmacHash hmac2(K.GetNumBytes(), K.AsByteArray().get(), EVP_sha1(), SHA_DIGEST_LENGTH); + HmacSha1 hmac2(K.GetNumBytes(), K.AsByteArray().get()); hmac2.UpdateData((uint8*)"WoW\0", 4); hmac2.UpdateData((uint8*)&result.ServerSeed, 4); hmac2.UpdateData((uint8*)&join.ClientSeed, 4); @@ -915,13 +915,13 @@ bool Battlenet::Socket::HandleResumeModule(BitStream* dataStream, ServerPacket** ACE_Auto_Array_Ptr&& serverChallenge = _reconnectProof.AsByteArray(); ACE_Auto_Array_Ptr&& sessionKey = K.AsByteArray(); - HmacHash clientPart(64, sessionKey.get(), EVP_sha256(), SHA256_DIGEST_LENGTH); + HmacSha256 clientPart(64, sessionKey.get()); clientPart.UpdateData(&ResumeClient, 1); clientPart.UpdateData(clientChallenge.get(), 16); clientPart.UpdateData(serverChallenge.get(), 16); clientPart.Finalize(); - HmacHash serverPart(64, sessionKey.get(), EVP_sha256(), SHA256_DIGEST_LENGTH); + HmacSha256 serverPart(64, sessionKey.get()); serverPart.UpdateData(&ResumeServer, 1); serverPart.UpdateData(serverChallenge.get(), 16); serverPart.UpdateData(clientChallenge.get(), 16); @@ -933,7 +933,7 @@ bool Battlenet::Socket::HandleResumeModule(BitStream* dataStream, ServerPacket** K.SetBinary(newSessionKey, 64); - HmacHash proof(64, newSessionKey, EVP_sha256(), SHA256_DIGEST_LENGTH); + HmacSha256 proof(64, newSessionKey); proof.UpdateData(&ResumeClient, 1); proof.UpdateData(clientChallenge.get(), 16); proof.UpdateData(serverChallenge.get(), 16); @@ -953,7 +953,7 @@ bool Battlenet::Socket::HandleResumeModule(BitStream* dataStream, ServerPacket** stmt->setUInt32(1, _accountId); LoginDatabase.Execute(stmt); - HmacHash serverProof(64, newSessionKey, EVP_sha256(), SHA256_DIGEST_LENGTH); + HmacSha256 serverProof(64, newSessionKey); serverProof.UpdateData(&ResumeServer, 1); serverProof.UpdateData(serverChallenge.get(), 16); serverProof.UpdateData(clientChallenge.get(), 16); diff --git a/src/server/game/Warden/WardenWin.cpp b/src/server/game/Warden/WardenWin.cpp index 3014fcfb993..3428708ed69 100644 --- a/src/server/game/Warden/WardenWin.cpp +++ b/src/server/game/Warden/WardenWin.cpp @@ -283,7 +283,7 @@ void WardenWin::RequestData() { uint32 seed = static_cast(rand32()); buff << uint32(seed); - HmacHash hmac(4, (uint8*)&seed, EVP_sha1(), SHA_DIGEST_LENGTH); + HmacSha1 hmac(4, (uint8*)&seed); hmac.UpdateData(wd->Str); hmac.Finalize(); buff.append(hmac.GetDigest(), hmac.GetLength()); diff --git a/src/server/shared/Cryptography/Authentication/WorldPacketCrypt.cpp b/src/server/shared/Cryptography/Authentication/WorldPacketCrypt.cpp index c6b283d9961..10403b84a1f 100644 --- a/src/server/shared/Cryptography/Authentication/WorldPacketCrypt.cpp +++ b/src/server/shared/Cryptography/Authentication/WorldPacketCrypt.cpp @@ -27,11 +27,11 @@ WorldPacketCrypt::WorldPacketCrypt() : PacketCrypt(SHA_DIGEST_LENGTH) void WorldPacketCrypt::Init(BigNumber* K) { uint8 ServerEncryptionKey[SEED_KEY_SIZE] = { 0xCC, 0x98, 0xAE, 0x04, 0xE8, 0x97, 0xEA, 0xCA, 0x12, 0xDD, 0xC0, 0x93, 0x42, 0x91, 0x53, 0x57 }; - HmacHash serverEncryptHmac(SEED_KEY_SIZE, (uint8*)ServerEncryptionKey, EVP_sha1(), SHA_DIGEST_LENGTH); + HmacSha1 serverEncryptHmac(SEED_KEY_SIZE, (uint8*)ServerEncryptionKey); uint8 *encryptHash = serverEncryptHmac.ComputeHash(K); uint8 ServerDecryptionKey[SEED_KEY_SIZE] = { 0xC2, 0xB3, 0x72, 0x3C, 0xC6, 0xAE, 0xD9, 0xB5, 0x34, 0x3C, 0x53, 0xEE, 0x2F, 0x43, 0x67, 0xCE }; - HmacHash clientDecryptHmac(SEED_KEY_SIZE, (uint8*)ServerDecryptionKey, EVP_sha1(), SHA_DIGEST_LENGTH); + HmacSha1 clientDecryptHmac(SEED_KEY_SIZE, (uint8*)ServerDecryptionKey); uint8 *decryptHash = clientDecryptHmac.ComputeHash(K); _clientDecrypt.Init(decryptHash); diff --git a/src/server/shared/Cryptography/HmacHash.cpp b/src/server/shared/Cryptography/HmacHash.cpp index 71fc362ae5d..2913b9fa79a 100644 --- a/src/server/shared/Cryptography/HmacHash.cpp +++ b/src/server/shared/Cryptography/HmacHash.cpp @@ -20,40 +20,47 @@ #include "BigNumber.h" #include "Common.h" -HmacHash::HmacHash(uint32 len, uint8 *seed, EVP_MD const* hasher, uint32 digestLength) : _digestLength(digestLength) +template +HmacHash::HmacHash(uint32 len, uint8 *seed) { HMAC_CTX_init(&_ctx); - HMAC_Init_ex(&_ctx, seed, len, hasher, NULL); - _digest = new uint8[digestLength]; - memset(_digest, 0, digestLength); + HMAC_Init_ex(&_ctx, seed, len, HashCreator(), NULL); + memset(_digest, 0, DigestLength); } -HmacHash::~HmacHash() +template +HmacHash::~HmacHash() { HMAC_CTX_cleanup(&_ctx); - delete[] _digest; } -void HmacHash::UpdateData(const std::string &str) +template +void HmacHash::UpdateData(const std::string &str) { HMAC_Update(&_ctx, (uint8 const*)str.c_str(), str.length()); } -void HmacHash::UpdateData(const uint8* data, size_t len) +template +void HmacHash::UpdateData(const uint8* data, size_t len) { HMAC_Update(&_ctx, data, len); } -void HmacHash::Finalize() +template +void HmacHash::Finalize() { uint32 length = 0; HMAC_Final(&_ctx, _digest, &length); - ASSERT(length == _digestLength); + ASSERT(length == DigestLength); } -uint8* HmacHash::ComputeHash(BigNumber* bn) +template +uint8* HmacHash::ComputeHash(BigNumber* bn) { HMAC_Update(&_ctx, bn->AsByteArray().get(), bn->GetNumBytes()); Finalize(); return _digest; } + +template class HmacHash; +template class HmacHash; diff --git a/src/server/shared/Cryptography/HmacHash.h b/src/server/shared/Cryptography/HmacHash.h index bf82e39a526..56ee55edda2 100644 --- a/src/server/shared/Cryptography/HmacHash.h +++ b/src/server/shared/Cryptography/HmacHash.h @@ -28,21 +28,26 @@ class BigNumber; #define SEED_KEY_SIZE 16 +typedef EVP_MD const* (*HashCreateFn)(); + +template class HmacHash { public: - HmacHash(uint32 len, uint8 *seed, EVP_MD const* hasher, uint32 digestLength); + HmacHash(uint32 len, uint8 *seed); ~HmacHash(); - void UpdateData(const std::string &str); - void UpdateData(const uint8* data, size_t len); + void UpdateData(std::string const& str); + void UpdateData(uint8 const* data, size_t len); void Finalize(); uint8* ComputeHash(BigNumber* bn); uint8* GetDigest() { return _digest; } - uint32 GetLength() const { return _digestLength; } + uint32 GetLength() const { return DigestLength; } private: HMAC_CTX _ctx; - uint8* _digest; - uint32 _digestLength; + uint8 _digest[DigestLength]; }; +typedef HmacHash HmacSha1; +typedef HmacHash HmacSha256; + #endif -- cgit v1.2.3