From 680c12a4bfc3dfb181ee8751365abf203fcbb56d Mon Sep 17 00:00:00 2001 From: Shauren Date: Sat, 19 Mar 2016 23:15:54 +0100 Subject: [PATCH] Core/Crypto: Renamed SHA1Randx/WardenKeyGeneration and made it a template class --- src/common/Cryptography/SHA1.h | 3 ++ src/common/Cryptography/SHA256.h | 3 ++ ...KeyGeneration.h => SessionKeyGeneration.h} | 37 ++++++++++--------- src/server/game/Warden/WardenMac.cpp | 4 +- src/server/game/Warden/WardenWin.cpp | 4 +- 5 files changed, 29 insertions(+), 22 deletions(-) rename src/common/Cryptography/{WardenKeyGeneration.h => SessionKeyGeneration.h} (63%) diff --git a/src/common/Cryptography/SHA1.h b/src/common/Cryptography/SHA1.h index ffa02176a2d..944350265f4 100644 --- a/src/common/Cryptography/SHA1.h +++ b/src/common/Cryptography/SHA1.h @@ -21,6 +21,7 @@ #include "Define.h" #include +#include #include class BigNumber; @@ -28,6 +29,8 @@ class BigNumber; class SHA1Hash { public: + typedef std::integral_constant DigestLength; + SHA1Hash(); ~SHA1Hash(); diff --git a/src/common/Cryptography/SHA256.h b/src/common/Cryptography/SHA256.h index d31c24ed93b..33f23f639a4 100644 --- a/src/common/Cryptography/SHA256.h +++ b/src/common/Cryptography/SHA256.h @@ -20,6 +20,7 @@ #include "Define.h" #include +#include #include class BigNumber; @@ -27,6 +28,8 @@ class BigNumber; class SHA256Hash { public: + typedef std::integral_constant DigestLength; + SHA256Hash(); ~SHA256Hash(); diff --git a/src/common/Cryptography/WardenKeyGeneration.h b/src/common/Cryptography/SessionKeyGeneration.h similarity index 63% rename from src/common/Cryptography/WardenKeyGeneration.h rename to src/common/Cryptography/SessionKeyGeneration.h index 18118ece649..0ec4479b765 100644 --- a/src/common/Cryptography/WardenKeyGeneration.h +++ b/src/common/Cryptography/SessionKeyGeneration.h @@ -16,17 +16,16 @@ * with this program. If not, see . */ -#include "SHA1.h" +#ifndef SessionKeyGeneration_h__ +#define SessionKeyGeneration_h__ -#include +#include "Common.h" -#ifndef _WARDEN_KEY_GENERATION_H -#define _WARDEN_KEY_GENERATION_H - -class SHA1Randx +template +class SessionKeyGenerator { public: - SHA1Randx(uint8* buff, uint32 size) + SessionKeyGenerator(uint8* buff, uint32 size) { uint32 halfSize = size / 2; @@ -34,15 +33,15 @@ public: sh.UpdateData(buff, halfSize); sh.Finalize(); - memcpy(o1, sh.GetDigest(), 20); + memcpy(o1, sh.GetDigest(), Hash::DigestLength::value); sh.Initialize(); sh.UpdateData(buff + halfSize, size - halfSize); sh.Finalize(); - memcpy(o2, sh.GetDigest(), 20); + memcpy(o2, sh.GetDigest(), Hash::DigestLength::value); - memset(o0, 0x00, 20); + memset(o0, 0x00, Hash::DigestLength::value); FillUp(); } @@ -51,7 +50,7 @@ public: { for (uint32 i = 0; i < sz; ++i) { - if (taken == 20) + if (taken == Hash::DigestLength::value) FillUp(); buf[i] = o0[taken]; @@ -63,19 +62,21 @@ private: void FillUp() { sh.Initialize(); - sh.UpdateData(o1, 20); - sh.UpdateData(o0, 20); - sh.UpdateData(o2, 20); + sh.UpdateData(o1, Hash::DigestLength::value); + sh.UpdateData(o0, Hash::DigestLength::value); + sh.UpdateData(o2, Hash::DigestLength::value); sh.Finalize(); - memcpy(o0, sh.GetDigest(), 20); + memcpy(o0, sh.GetDigest(), Hash::DigestLength::value); taken = 0; } - SHA1Hash sh; + Hash sh; uint32 taken; - uint8 o0[20], o1[20], o2[20]; + uint8 o0[Hash::DigestLength::value]; + uint8 o1[Hash::DigestLength::value]; + uint8 o2[Hash::DigestLength::value]; }; -#endif +#endif // SessionKeyGeneration_h__ diff --git a/src/server/game/Warden/WardenMac.cpp b/src/server/game/Warden/WardenMac.cpp index 6df1dc7561c..3c89792745b 100644 --- a/src/server/game/Warden/WardenMac.cpp +++ b/src/server/game/Warden/WardenMac.cpp @@ -16,7 +16,7 @@ * with this program. If not, see . */ -#include "Cryptography/WardenKeyGeneration.h" +#include "Cryptography/SessionKeyGeneration.h" #include "Common.h" #include "WorldPacket.h" #include "WorldSession.h" @@ -39,7 +39,7 @@ void WardenMac::Init(WorldSession* pClient, BigNumber* K) { _session = pClient; // Generate Warden Key - SHA1Randx WK(K->AsByteArray().get(), K->GetNumBytes()); + SessionKeyGenerator WK(K->AsByteArray().get(), K->GetNumBytes()); WK.Generate(_inputKey, 16); WK.Generate(_outputKey, 16); /* diff --git a/src/server/game/Warden/WardenWin.cpp b/src/server/game/Warden/WardenWin.cpp index b99a6cd3ac9..96857dd26d3 100644 --- a/src/server/game/Warden/WardenWin.cpp +++ b/src/server/game/Warden/WardenWin.cpp @@ -17,7 +17,7 @@ */ #include "Cryptography/HmacHash.h" -#include "Cryptography/WardenKeyGeneration.h" +#include "Cryptography/SessionKeyGeneration.h" #include "Common.h" #include "WorldPacket.h" #include "WorldSession.h" @@ -42,7 +42,7 @@ void WardenWin::Init(WorldSession* session, BigNumber* k) { _session = session; // Generate Warden Key - SHA1Randx WK(k->AsByteArray().get(), k->GetNumBytes()); + SessionKeyGenerator WK(k->AsByteArray().get(), k->GetNumBytes()); WK.Generate(_inputKey, 16); WK.Generate(_outputKey, 16);