aboutsummaryrefslogtreecommitdiff
path: root/src/common/Cryptography
diff options
context:
space:
mode:
authorStormBytePP <stormbyte@gmail.com>2015-08-19 19:02:10 +0200
committerStormBytePP <stormbyte@gmail.com>2015-08-21 17:52:42 +0200
commit1d2aafd39bcb79a67357d198ce9b2345642fdd39 (patch)
treec32cf1c3717625c60da59c82ba5a4fca2530119a /src/common/Cryptography
parent172293acee1607727ebd8070ab3e1390590d02a8 (diff)
Core/Build: Merge common library and move database out of shared
Diffstat (limited to 'src/common/Cryptography')
-rw-r--r--src/common/Cryptography/ARC4.cpp52
-rw-r--r--src/common/Cryptography/ARC4.h37
-rw-r--r--src/common/Cryptography/BigNumber.cpp213
-rw-r--r--src/common/Cryptography/BigNumber.h102
-rw-r--r--src/common/Cryptography/HmacHash.cpp66
-rw-r--r--src/common/Cryptography/HmacHash.h53
-rw-r--r--src/common/Cryptography/OpenSSLCrypto.cpp59
-rw-r--r--src/common/Cryptography/OpenSSLCrypto.h33
-rw-r--r--src/common/Cryptography/SHA1.cpp69
-rw-r--r--src/common/Cryptography/SHA1.h50
-rw-r--r--src/common/Cryptography/SHA256.cpp67
-rw-r--r--src/common/Cryptography/SHA256.h49
-rw-r--r--src/common/Cryptography/WardenKeyGeneration.h81
13 files changed, 931 insertions, 0 deletions
diff --git a/src/common/Cryptography/ARC4.cpp b/src/common/Cryptography/ARC4.cpp
new file mode 100644
index 00000000000..4915383f8dc
--- /dev/null
+++ b/src/common/Cryptography/ARC4.cpp
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "ARC4.h"
+#include <openssl/sha.h>
+
+ARC4::ARC4(uint32 len) : m_ctx()
+{
+ EVP_CIPHER_CTX_init(&m_ctx);
+ EVP_EncryptInit_ex(&m_ctx, EVP_rc4(), NULL, NULL, NULL);
+ EVP_CIPHER_CTX_set_key_length(&m_ctx, len);
+}
+
+ARC4::ARC4(uint8 *seed, uint32 len) : m_ctx()
+{
+ EVP_CIPHER_CTX_init(&m_ctx);
+ EVP_EncryptInit_ex(&m_ctx, EVP_rc4(), NULL, NULL, NULL);
+ EVP_CIPHER_CTX_set_key_length(&m_ctx, len);
+ EVP_EncryptInit_ex(&m_ctx, NULL, NULL, seed, NULL);
+}
+
+ARC4::~ARC4()
+{
+ EVP_CIPHER_CTX_cleanup(&m_ctx);
+}
+
+void ARC4::Init(uint8 *seed)
+{
+ EVP_EncryptInit_ex(&m_ctx, NULL, NULL, seed, NULL);
+}
+
+void ARC4::UpdateData(int len, uint8 *data)
+{
+ int outlen = 0;
+ EVP_EncryptUpdate(&m_ctx, data, &outlen, data, len);
+ EVP_EncryptFinal_ex(&m_ctx, data, &outlen);
+}
diff --git a/src/common/Cryptography/ARC4.h b/src/common/Cryptography/ARC4.h
new file mode 100644
index 00000000000..aa08901f456
--- /dev/null
+++ b/src/common/Cryptography/ARC4.h
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _AUTH_SARC4_H
+#define _AUTH_SARC4_H
+
+#include <openssl/evp.h>
+#include "Define.h"
+
+class ARC4
+{
+ public:
+ ARC4(uint32 len);
+ ARC4(uint8 *seed, uint32 len);
+ ~ARC4();
+ void Init(uint8 *seed);
+ void UpdateData(int len, uint8 *data);
+ private:
+ EVP_CIPHER_CTX m_ctx;
+};
+
+#endif
diff --git a/src/common/Cryptography/BigNumber.cpp b/src/common/Cryptography/BigNumber.cpp
new file mode 100644
index 00000000000..5be425a3d16
--- /dev/null
+++ b/src/common/Cryptography/BigNumber.cpp
@@ -0,0 +1,213 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "Cryptography/BigNumber.h"
+#include <openssl/bn.h>
+#include <openssl/crypto.h>
+#include <cstring>
+#include <algorithm>
+#include <memory>
+
+BigNumber::BigNumber()
+ : _bn(BN_new())
+{ }
+
+BigNumber::BigNumber(BigNumber const& bn)
+ : _bn(BN_dup(bn._bn))
+{ }
+
+BigNumber::BigNumber(uint32 val)
+ : _bn(BN_new())
+{
+ BN_set_word(_bn, val);
+}
+
+BigNumber::~BigNumber()
+{
+ BN_free(_bn);
+}
+
+void BigNumber::SetDword(uint32 val)
+{
+ BN_set_word(_bn, val);
+}
+
+void BigNumber::SetQword(uint64 val)
+{
+ BN_set_word(_bn, (uint32)(val >> 32));
+ BN_lshift(_bn, _bn, 32);
+ BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF));
+}
+
+void BigNumber::SetBinary(uint8 const* bytes, int32 len)
+{
+ uint8* array = new uint8[len];
+
+ for (int i = 0; i < len; i++)
+ array[i] = bytes[len - 1 - i];
+
+ BN_bin2bn(array, len, _bn);
+
+ delete[] array;
+}
+
+void BigNumber::SetHexStr(char const* str)
+{
+ BN_hex2bn(&_bn, str);
+}
+
+void BigNumber::SetRand(int32 numbits)
+{
+ BN_rand(_bn, numbits, 0, 1);
+}
+
+BigNumber& BigNumber::operator=(BigNumber const& bn)
+{
+ if (this == &bn)
+ return *this;
+
+ BN_copy(_bn, bn._bn);
+ return *this;
+}
+
+BigNumber BigNumber::operator+=(BigNumber const& bn)
+{
+ BN_add(_bn, _bn, bn._bn);
+ return *this;
+}
+
+BigNumber BigNumber::operator-=(BigNumber const& bn)
+{
+ BN_sub(_bn, _bn, bn._bn);
+ return *this;
+}
+
+BigNumber BigNumber::operator*=(BigNumber const& bn)
+{
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_mul(_bn, _bn, bn._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return *this;
+}
+
+BigNumber BigNumber::operator/=(BigNumber const& bn)
+{
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_div(_bn, NULL, _bn, bn._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return *this;
+}
+
+BigNumber BigNumber::operator%=(BigNumber const& bn)
+{
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_mod(_bn, _bn, bn._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return *this;
+}
+
+BigNumber BigNumber::Exp(BigNumber const& bn)
+{
+ BigNumber ret;
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_exp(ret._bn, _bn, bn._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return ret;
+}
+
+BigNumber BigNumber::ModExp(BigNumber const& bn1, BigNumber const& bn2)
+{
+ BigNumber ret;
+ BN_CTX *bnctx;
+
+ bnctx = BN_CTX_new();
+ BN_mod_exp(ret._bn, _bn, bn1._bn, bn2._bn, bnctx);
+ BN_CTX_free(bnctx);
+
+ return ret;
+}
+
+int32 BigNumber::GetNumBytes(void)
+{
+ return BN_num_bytes(_bn);
+}
+
+uint32 BigNumber::AsDword()
+{
+ return (uint32)BN_get_word(_bn);
+}
+
+bool BigNumber::IsZero() const
+{
+ return BN_is_zero(_bn);
+}
+
+bool BigNumber::IsNegative() const
+{
+ return BN_is_negative(_bn);
+}
+
+std::unique_ptr<uint8[]> BigNumber::AsByteArray(int32 minSize, bool littleEndian)
+{
+ int numBytes = GetNumBytes();
+ int length = (minSize >= numBytes) ? minSize : numBytes;
+
+ uint8* array = new uint8[length];
+
+ // If we need more bytes than length of BigNumber set the rest to 0
+ if (length > numBytes)
+ memset((void*)array, 0, length);
+
+ BN_bn2bin(_bn, (unsigned char *)array);
+
+ // openssl's BN stores data internally in big endian format, reverse if little endian desired
+ if (littleEndian)
+ std::reverse(array, array + numBytes);
+
+ std::unique_ptr<uint8[]> ret(array);
+ return ret;
+}
+
+std::string BigNumber::AsHexStr() const
+{
+ char* ch = BN_bn2hex(_bn);
+ std::string ret = ch;
+ OPENSSL_free(ch);
+ return ret;
+}
+
+std::string BigNumber::AsDecStr() const
+{
+ char* ch = BN_bn2dec(_bn);
+ std::string ret = ch;
+ OPENSSL_free(ch);
+ return ret;
+}
+
diff --git a/src/common/Cryptography/BigNumber.h b/src/common/Cryptography/BigNumber.h
new file mode 100644
index 00000000000..f0feebeafb2
--- /dev/null
+++ b/src/common/Cryptography/BigNumber.h
@@ -0,0 +1,102 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _AUTH_BIGNUMBER_H
+#define _AUTH_BIGNUMBER_H
+
+#include <memory>
+#include "Define.h"
+#include <string>
+
+struct bignum_st;
+
+class BigNumber
+{
+ public:
+ BigNumber();
+ BigNumber(BigNumber const& bn);
+ BigNumber(uint32);
+ ~BigNumber();
+
+ void SetDword(uint32);
+ void SetQword(uint64);
+ void SetBinary(uint8 const* bytes, int32 len);
+ void SetHexStr(char const* str);
+
+ void SetRand(int32 numbits);
+
+ BigNumber& operator=(BigNumber const& bn);
+
+ BigNumber operator+=(BigNumber const& bn);
+ BigNumber operator+(BigNumber const& bn)
+ {
+ BigNumber t(*this);
+ return t += bn;
+ }
+
+ BigNumber operator-=(BigNumber const& bn);
+ BigNumber operator-(BigNumber const& bn)
+ {
+ BigNumber t(*this);
+ return t -= bn;
+ }
+
+ BigNumber operator*=(BigNumber const& bn);
+ BigNumber operator*(BigNumber const& bn)
+ {
+ BigNumber t(*this);
+ return t *= bn;
+ }
+
+ BigNumber operator/=(BigNumber const& bn);
+ BigNumber operator/(BigNumber const& bn)
+ {
+ BigNumber t(*this);
+ return t /= bn;
+ }
+
+ BigNumber operator%=(BigNumber const& bn);
+ BigNumber operator%(BigNumber const& bn)
+ {
+ BigNumber t(*this);
+ return t %= bn;
+ }
+
+ bool IsZero() const;
+ bool IsNegative() const;
+
+ BigNumber ModExp(BigNumber const& bn1, BigNumber const& bn2);
+ BigNumber Exp(BigNumber const&);
+
+ int32 GetNumBytes(void);
+
+ struct bignum_st *BN() { return _bn; }
+
+ uint32 AsDword();
+
+ std::unique_ptr<uint8[]> AsByteArray(int32 minSize = 0, bool littleEndian = true);
+
+ std::string AsHexStr() const;
+ std::string AsDecStr() const;
+
+ private:
+ struct bignum_st *_bn;
+
+};
+#endif
+
diff --git a/src/common/Cryptography/HmacHash.cpp b/src/common/Cryptography/HmacHash.cpp
new file mode 100644
index 00000000000..3c16ec3a72a
--- /dev/null
+++ b/src/common/Cryptography/HmacHash.cpp
@@ -0,0 +1,66 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "HmacHash.h"
+#include "BigNumber.h"
+#include "Common.h"
+
+template<HashCreateFn HashCreator, uint32 DigestLength>
+HmacHash<HashCreator, DigestLength>::HmacHash(uint32 len, uint8 const* seed)
+{
+ HMAC_CTX_init(&_ctx);
+ HMAC_Init_ex(&_ctx, seed, len, HashCreator(), NULL);
+ memset(_digest, 0, DigestLength);
+}
+
+template<HashCreateFn HashCreator, uint32 DigestLength>
+HmacHash<HashCreator, DigestLength>::~HmacHash()
+{
+ HMAC_CTX_cleanup(&_ctx);
+}
+
+template<HashCreateFn HashCreator, uint32 DigestLength>
+void HmacHash<HashCreator, DigestLength>::UpdateData(const std::string &str)
+{
+ HMAC_Update(&_ctx, (uint8 const*)str.c_str(), str.length());
+}
+
+template<HashCreateFn HashCreator, uint32 DigestLength>
+void HmacHash<HashCreator, DigestLength>::UpdateData(const uint8* data, size_t len)
+{
+ HMAC_Update(&_ctx, data, len);
+}
+
+template<HashCreateFn HashCreator, uint32 DigestLength>
+void HmacHash<HashCreator, DigestLength>::Finalize()
+{
+ uint32 length = 0;
+ HMAC_Final(&_ctx, _digest, &length);
+ ASSERT(length == DigestLength);
+}
+
+template<HashCreateFn HashCreator, uint32 DigestLength>
+uint8* HmacHash<HashCreator, DigestLength>::ComputeHash(BigNumber* bn)
+{
+ HMAC_Update(&_ctx, bn->AsByteArray().get(), bn->GetNumBytes());
+ Finalize();
+ return _digest;
+}
+
+template class HmacHash<EVP_sha1, SHA_DIGEST_LENGTH>;
+template class HmacHash<EVP_sha256, SHA256_DIGEST_LENGTH>;
diff --git a/src/common/Cryptography/HmacHash.h b/src/common/Cryptography/HmacHash.h
new file mode 100644
index 00000000000..7ff1de9ba05
--- /dev/null
+++ b/src/common/Cryptography/HmacHash.h
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _AUTH_HMAC_H
+#define _AUTH_HMAC_H
+
+#include "Define.h"
+#include <string>
+#include <openssl/hmac.h>
+#include <openssl/sha.h>
+
+class BigNumber;
+
+#define SEED_KEY_SIZE 16
+
+typedef EVP_MD const* (*HashCreateFn)();
+
+template<HashCreateFn HashCreator, uint32 DigestLength>
+class HmacHash
+{
+ public:
+ HmacHash(uint32 len, uint8 const* seed);
+ ~HmacHash();
+ 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; }
+ private:
+ HMAC_CTX _ctx;
+ uint8 _digest[DigestLength];
+};
+
+typedef HmacHash<EVP_sha1, SHA_DIGEST_LENGTH> HmacSha1;
+typedef HmacHash<EVP_sha256, SHA256_DIGEST_LENGTH> HmacSha256;
+
+#endif
diff --git a/src/common/Cryptography/OpenSSLCrypto.cpp b/src/common/Cryptography/OpenSSLCrypto.cpp
new file mode 100644
index 00000000000..f122888292f
--- /dev/null
+++ b/src/common/Cryptography/OpenSSLCrypto.cpp
@@ -0,0 +1,59 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <OpenSSLCrypto.h>
+#include <openssl/crypto.h>
+#include <vector>
+#include <thread>
+#include <mutex>
+
+std::vector<std::mutex*> cryptoLocks;
+
+static void lockingCallback(int mode, int type, const char* /*file*/, int /*line*/)
+{
+ if (mode & CRYPTO_LOCK)
+ cryptoLocks[type]->lock();
+ else
+ cryptoLocks[type]->unlock();
+}
+
+static void threadIdCallback(CRYPTO_THREADID * id)
+{
+ CRYPTO_THREADID_set_numeric(id, std::hash<std::thread::id>()(std::this_thread::get_id()));
+}
+
+void OpenSSLCrypto::threadsSetup()
+{
+ cryptoLocks.resize(CRYPTO_num_locks());
+ for(int i = 0 ; i < CRYPTO_num_locks(); ++i)
+ {
+ cryptoLocks[i] = new std::mutex;
+ }
+ CRYPTO_THREADID_set_callback(threadIdCallback);
+ CRYPTO_set_locking_callback(lockingCallback);
+}
+
+void OpenSSLCrypto::threadsCleanup()
+{
+ CRYPTO_set_locking_callback(NULL);
+ CRYPTO_THREADID_set_callback(NULL);
+ for(int i = 0 ; i < CRYPTO_num_locks(); ++i)
+ {
+ delete cryptoLocks[i];
+ }
+ cryptoLocks.resize(0);
+}
diff --git a/src/common/Cryptography/OpenSSLCrypto.h b/src/common/Cryptography/OpenSSLCrypto.h
new file mode 100644
index 00000000000..0daa20c4780
--- /dev/null
+++ b/src/common/Cryptography/OpenSSLCrypto.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef OPENSSL_CRYPTO_H
+#define OPENSSL_CRYPTO_H
+
+/**
+* A group of functions which setup openssl crypto module to work properly in multithreaded enviroment
+* If not setup properly - it will crash
+*/
+namespace OpenSSLCrypto
+{
+ /// Needs to be called before threads using openssl are spawned
+ void threadsSetup();
+ /// Needs to be called after threads using openssl are despawned
+ void threadsCleanup();
+}
+
+#endif \ No newline at end of file
diff --git a/src/common/Cryptography/SHA1.cpp b/src/common/Cryptography/SHA1.cpp
new file mode 100644
index 00000000000..bd7101075de
--- /dev/null
+++ b/src/common/Cryptography/SHA1.cpp
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "SHA1.h"
+#include "BigNumber.h"
+#include <cstring>
+#include <stdarg.h>
+
+SHA1Hash::SHA1Hash()
+{
+ SHA1_Init(&mC);
+ memset(mDigest, 0, SHA_DIGEST_LENGTH * sizeof(uint8));
+}
+
+SHA1Hash::~SHA1Hash()
+{
+ SHA1_Init(&mC);
+}
+
+void SHA1Hash::UpdateData(const uint8 *dta, int len)
+{
+ SHA1_Update(&mC, dta, len);
+}
+
+void SHA1Hash::UpdateData(const std::string &str)
+{
+ UpdateData((uint8 const*)str.c_str(), str.length());
+}
+
+void SHA1Hash::UpdateBigNumbers(BigNumber* bn0, ...)
+{
+ va_list v;
+ BigNumber* bn;
+
+ va_start(v, bn0);
+ bn = bn0;
+ while (bn)
+ {
+ UpdateData(bn->AsByteArray().get(), bn->GetNumBytes());
+ bn = va_arg(v, BigNumber*);
+ }
+ va_end(v);
+}
+
+void SHA1Hash::Initialize()
+{
+ SHA1_Init(&mC);
+}
+
+void SHA1Hash::Finalize(void)
+{
+ SHA1_Final(mDigest, &mC);
+}
+
diff --git a/src/common/Cryptography/SHA1.h b/src/common/Cryptography/SHA1.h
new file mode 100644
index 00000000000..f59bdc25556
--- /dev/null
+++ b/src/common/Cryptography/SHA1.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _AUTH_SHA1_H
+#define _AUTH_SHA1_H
+
+#include "Define.h"
+#include <string>
+#include <openssl/sha.h>
+
+class BigNumber;
+
+class SHA1Hash
+{
+ public:
+ SHA1Hash();
+ ~SHA1Hash();
+
+ void UpdateBigNumbers(BigNumber* bn0, ...);
+
+ void UpdateData(const uint8 *dta, int len);
+ void UpdateData(const std::string &str);
+
+ void Initialize();
+ void Finalize();
+
+ uint8 *GetDigest(void) { return mDigest; }
+ int GetLength(void) const { return SHA_DIGEST_LENGTH; }
+
+ private:
+ SHA_CTX mC;
+ uint8 mDigest[SHA_DIGEST_LENGTH];
+};
+#endif
+
diff --git a/src/common/Cryptography/SHA256.cpp b/src/common/Cryptography/SHA256.cpp
new file mode 100644
index 00000000000..2a93aeeab72
--- /dev/null
+++ b/src/common/Cryptography/SHA256.cpp
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "SHA256.h"
+#include "BigNumber.h"
+#include <cstring>
+#include <stdarg.h>
+
+SHA256Hash::SHA256Hash()
+{
+ SHA256_Init(&mC);
+ memset(mDigest, 0, SHA256_DIGEST_LENGTH * sizeof(uint8));
+}
+
+SHA256Hash::~SHA256Hash()
+{
+ SHA256_Init(&mC);
+}
+
+void SHA256Hash::UpdateData(const uint8 *dta, int len)
+{
+ SHA256_Update(&mC, dta, len);
+}
+
+void SHA256Hash::UpdateData(const std::string &str)
+{
+ UpdateData((uint8 const*)str.c_str(), str.length());
+}
+
+void SHA256Hash::UpdateBigNumbers(BigNumber* bn0, ...)
+{
+ va_list v;
+ BigNumber* bn;
+
+ va_start(v, bn0);
+ bn = bn0;
+ while (bn)
+ {
+ UpdateData(bn->AsByteArray().get(), bn->GetNumBytes());
+ bn = va_arg(v, BigNumber*);
+ }
+ va_end(v);
+}
+
+void SHA256Hash::Initialize()
+{
+ SHA256_Init(&mC);
+}
+
+void SHA256Hash::Finalize(void)
+{
+ SHA256_Final(mDigest, &mC);
+}
diff --git a/src/common/Cryptography/SHA256.h b/src/common/Cryptography/SHA256.h
new file mode 100644
index 00000000000..1d85545b2e0
--- /dev/null
+++ b/src/common/Cryptography/SHA256.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SHA256_h__
+#define SHA256_h__
+
+#include "Define.h"
+#include <string>
+#include <openssl/sha.h>
+
+class BigNumber;
+
+class SHA256Hash
+{
+ public:
+ SHA256Hash();
+ ~SHA256Hash();
+
+ void UpdateBigNumbers(BigNumber* bn0, ...);
+
+ void UpdateData(const uint8 *dta, int len);
+ void UpdateData(const std::string &str);
+
+ void Initialize();
+ void Finalize();
+
+ uint8 *GetDigest(void) { return mDigest; }
+ int GetLength(void) const { return SHA256_DIGEST_LENGTH; }
+
+ private:
+ SHA256_CTX mC;
+ uint8 mDigest[SHA256_DIGEST_LENGTH];
+};
+
+#endif // SHA256_h__
diff --git a/src/common/Cryptography/WardenKeyGeneration.h b/src/common/Cryptography/WardenKeyGeneration.h
new file mode 100644
index 00000000000..bfa0337d347
--- /dev/null
+++ b/src/common/Cryptography/WardenKeyGeneration.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2008-2015 TrinityCore <http://www.trinitycore.org/>
+ * Copyright (C) 2005-2011 MaNGOS <http://getmangos.com/>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include "SHA1.h"
+
+#include <cstring>
+
+#ifndef _WARDEN_KEY_GENERATION_H
+#define _WARDEN_KEY_GENERATION_H
+
+class SHA1Randx
+{
+public:
+ SHA1Randx(uint8* buff, uint32 size)
+ {
+ uint32 halfSize = size / 2;
+
+ sh.Initialize();
+ sh.UpdateData(buff, halfSize);
+ sh.Finalize();
+
+ memcpy(o1, sh.GetDigest(), 20);
+
+ sh.Initialize();
+ sh.UpdateData(buff + halfSize, size - halfSize);
+ sh.Finalize();
+
+ memcpy(o2, sh.GetDigest(), 20);
+
+ memset(o0, 0x00, 20);
+
+ FillUp();
+ }
+
+ void Generate(uint8* buf, uint32 sz)
+ {
+ for (uint32 i = 0; i < sz; ++i)
+ {
+ if (taken == 20)
+ FillUp();
+
+ buf[i] = o0[taken];
+ taken++;
+ }
+ }
+
+private:
+ void FillUp()
+ {
+ sh.Initialize();
+ sh.UpdateData(o1, 20);
+ sh.UpdateData(o0, 20);
+ sh.UpdateData(o2, 20);
+ sh.Finalize();
+
+ memcpy(o0, sh.GetDigest(), 20);
+
+ taken = 0;
+ }
+
+ SHA1Hash sh;
+ uint32 taken;
+ uint8 o0[20], o1[20], o2[20];
+};
+
+#endif