diff options
Diffstat (limited to 'src/shared/Auth')
-rw-r--r-- | src/shared/Auth/AuthCrypt.cpp | 17 | ||||
-rw-r--r-- | src/shared/Auth/AuthCrypt.h | 7 | ||||
-rw-r--r-- | src/shared/Auth/BigNumber.cpp | 39 | ||||
-rw-r--r-- | src/shared/Auth/BigNumber.h | 15 | ||||
-rw-r--r-- | src/shared/Auth/Hmac.cpp | 8 | ||||
-rw-r--r-- | src/shared/Auth/Hmac.h | 5 | ||||
-rw-r--r-- | src/shared/Auth/SARC4.cpp | 6 | ||||
-rw-r--r-- | src/shared/Auth/SARC4.h | 3 | ||||
-rw-r--r-- | src/shared/Auth/Sha1.cpp | 9 | ||||
-rw-r--r-- | src/shared/Auth/Sha1.h | 9 |
10 files changed, 118 insertions, 0 deletions
diff --git a/src/shared/Auth/AuthCrypt.cpp b/src/shared/Auth/AuthCrypt.cpp index 56d70d4c14e..e8126ad9f73 100644 --- a/src/shared/Auth/AuthCrypt.cpp +++ b/src/shared/Auth/AuthCrypt.cpp @@ -17,48 +17,65 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #include "AuthCrypt.h" #include "Hmac.h" #include "Log.h" #include "BigNumber.h" + AuthCrypt::AuthCrypt() { _initialized = false; } + AuthCrypt::~AuthCrypt() { + } + void AuthCrypt::Init(BigNumber *K) { uint8 ServerEncryptionKey[SEED_KEY_SIZE] = { 0x22, 0xBE, 0xE5, 0xCF, 0xBB, 0x07, 0x64, 0xD9, 0x00, 0x45, 0x1B, 0xD0, 0x24, 0xB8, 0xD5, 0x45 }; HmacHash serverEncryptHmac(SEED_KEY_SIZE, (uint8*)ServerEncryptionKey); uint8 *encryptHash = serverEncryptHmac.ComputeHash(K); + uint8 ServerDecryptionKey[SEED_KEY_SIZE] = { 0xF4, 0x66, 0x31, 0x59, 0xFC, 0x83, 0x6E, 0x31, 0x31, 0x02, 0x51, 0xD5, 0x44, 0x31, 0x67, 0x98 }; HmacHash clientDecryptHmac(SEED_KEY_SIZE, (uint8*)ServerDecryptionKey); uint8 *decryptHash = clientDecryptHmac.ComputeHash(K); + //SARC4 _serverDecrypt(encryptHash); _clientDecrypt.Init(decryptHash); _serverEncrypt.Init(encryptHash); //SARC4 _clientEncrypt(decryptHash); + uint8 syncBuf[1024]; + memset(syncBuf, 0, 1024); + _serverEncrypt.UpdateData(1024, syncBuf); //_clientEncrypt.UpdateData(1024, syncBuf); + memset(syncBuf, 0, 1024); + //_serverDecrypt.UpdateData(1024, syncBuf); _clientDecrypt.UpdateData(1024, syncBuf); + _initialized = true; } + void AuthCrypt::DecryptRecv(uint8 *data, size_t len) { if (!_initialized) return; + _clientDecrypt.UpdateData(len, data); } + void AuthCrypt::EncryptSend(uint8 *data, size_t len) { if (!_initialized) return; + _serverEncrypt.UpdateData(len, data); } diff --git a/src/shared/Auth/AuthCrypt.h b/src/shared/Auth/AuthCrypt.h index 7f885e563ab..226fde018ae 100644 --- a/src/shared/Auth/AuthCrypt.h +++ b/src/shared/Auth/AuthCrypt.h @@ -17,20 +17,27 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #ifndef _AUTHCRYPT_H #define _AUTHCRYPT_H + #include <Common.h> #include "SARC4.h" + class BigNumber; + class AuthCrypt { public: AuthCrypt(); ~AuthCrypt(); + void Init(BigNumber *K); void DecryptRecv(uint8 *, size_t); void EncryptSend(uint8 *, size_t); + bool IsInitialized() { return _initialized; } + private: SARC4 _clientDecrypt; SARC4 _serverEncrypt; diff --git a/src/shared/Auth/BigNumber.cpp b/src/shared/Auth/BigNumber.cpp index 837b75804df..303687c266c 100644 --- a/src/shared/Auth/BigNumber.cpp +++ b/src/shared/Auth/BigNumber.cpp @@ -17,143 +17,182 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #include "Auth/BigNumber.h" #include <openssl/bn.h> #include <algorithm> + BigNumber::BigNumber() { _bn = BN_new(); _array = NULL; } + BigNumber::BigNumber(const BigNumber &bn) { _bn = BN_dup(bn._bn); _array = NULL; } + BigNumber::BigNumber(uint32 val) { _bn = BN_new(); BN_set_word(_bn, val); _array = NULL; } + BigNumber::~BigNumber() { BN_free(_bn); if(_array) delete[] _array; } + void BigNumber::SetDword(uint32 val) { BN_set_word(_bn, val); } + void BigNumber::SetQword(uint64 val) { BN_add_word(_bn, (uint32)(val >> 32)); BN_lshift(_bn, _bn, 32); BN_add_word(_bn, (uint32)(val & 0xFFFFFFFF)); } + void BigNumber::SetBinary(const uint8 *bytes, int len) { uint8 t[1000]; for (int i = 0; i < len; i++) t[i] = bytes[len - 1 - i]; BN_bin2bn(t, len, _bn); } + void BigNumber::SetHexStr(const char *str) { BN_hex2bn(&_bn, str); } + void BigNumber::SetRand(int numbits) { BN_rand(_bn, numbits, 0, 1); } + BigNumber BigNumber::operator=(const BigNumber &bn) { BN_copy(_bn, bn._bn); return *this; } + BigNumber BigNumber::operator+=(const BigNumber &bn) { BN_add(_bn, _bn, bn._bn); return *this; } + BigNumber BigNumber::operator-=(const BigNumber &bn) { BN_sub(_bn, _bn, bn._bn); return *this; } + BigNumber BigNumber::operator*=(const BigNumber &bn) { BN_CTX *bnctx; + bnctx = BN_CTX_new(); BN_mul(_bn, _bn, bn._bn, bnctx); BN_CTX_free(bnctx); + return *this; } + BigNumber BigNumber::operator/=(const BigNumber &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%=(const BigNumber &bn) { BN_CTX *bnctx; + bnctx = BN_CTX_new(); BN_mod(_bn, _bn, bn._bn, bnctx); BN_CTX_free(bnctx); + return *this; } + BigNumber BigNumber::Exp(const BigNumber &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(const BigNumber &bn1, const BigNumber &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; } + int 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)!=0; } + uint8 *BigNumber::AsByteArray(int minSize) { int length = (minSize >= GetNumBytes()) ? minSize : GetNumBytes(); + if (_array) { delete[] _array; _array = NULL; } _array = new uint8[length]; + // If we need more bytes than length of BigNumber set the rest to 0 if (length > GetNumBytes()) memset((void*)_array, 0, length); + BN_bn2bin(_bn, (unsigned char *)_array); + std::reverse(_array, _array + length); + return _array; } + const char *BigNumber::AsHexStr() { return BN_bn2hex(_bn); } + const char *BigNumber::AsDecStr() { return BN_bn2dec(_bn); diff --git a/src/shared/Auth/BigNumber.h b/src/shared/Auth/BigNumber.h index 898f53c2a05..f1b3a0beda2 100644 --- a/src/shared/Auth/BigNumber.h +++ b/src/shared/Auth/BigNumber.h @@ -17,10 +17,14 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #ifndef _AUTH_BIGNUMBER_H #define _AUTH_BIGNUMBER_H + #include "Common.h" + struct bignum_st; + class BigNumber { public: @@ -28,12 +32,16 @@ class BigNumber BigNumber(const BigNumber &bn); BigNumber(uint32); ~BigNumber(); + void SetDword(uint32); void SetQword(uint64); void SetBinary(const uint8 *bytes, int len); void SetHexStr(const char *str); + void SetRand(int numbits); + BigNumber operator=(const BigNumber &bn); + BigNumber operator+=(const BigNumber &bn); BigNumber operator+(const BigNumber &bn) { @@ -64,15 +72,22 @@ class BigNumber BigNumber t(*this); return t %= bn; } + bool isZero() const; + BigNumber ModExp(const BigNumber &bn1, const BigNumber &bn2); BigNumber Exp(const BigNumber &); + int GetNumBytes(void); + struct bignum_st *BN() { return _bn; } + uint32 AsDword(); uint8* AsByteArray(int minSize = 0); + const char *AsHexStr(); const char *AsDecStr(); + private: struct bignum_st *_bn; uint8 *_array; diff --git a/src/shared/Auth/Hmac.cpp b/src/shared/Auth/Hmac.cpp index ed68ce8c5df..985b4fb9a56 100644 --- a/src/shared/Auth/Hmac.cpp +++ b/src/shared/Auth/Hmac.cpp @@ -17,32 +17,40 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #include "Auth/Hmac.h" #include "BigNumber.h" + HmacHash::HmacHash(uint32 len, uint8 *seed) { ASSERT(len == SEED_KEY_SIZE); + HMAC_CTX_init(&m_ctx); HMAC_Init_ex(&m_ctx, seed, SEED_KEY_SIZE, EVP_sha1(), NULL); } + HmacHash::~HmacHash() { HMAC_CTX_cleanup(&m_ctx); } + void HmacHash::UpdateBigNumber(BigNumber *bn) { UpdateData(bn->AsByteArray(), bn->GetNumBytes()); } + void HmacHash::UpdateData(const uint8 *data, int length) { HMAC_Update(&m_ctx, data, length); } + void HmacHash::Finalize() { uint32 length = 0; HMAC_Final(&m_ctx, (uint8*)m_digest, &length); ASSERT(length == SHA_DIGEST_LENGTH) } + uint8 *HmacHash::ComputeHash(BigNumber *bn) { HMAC_Update(&m_ctx, bn->AsByteArray(), bn->GetNumBytes()); diff --git a/src/shared/Auth/Hmac.h b/src/shared/Auth/Hmac.h index a039617f801..76a302d68de 100644 --- a/src/shared/Auth/Hmac.h +++ b/src/shared/Auth/Hmac.h @@ -17,13 +17,18 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #ifndef _AUTH_HMAC_H #define _AUTH_HMAC_H + #include "Common.h" #include <openssl/hmac.h> #include <openssl/sha.h> + class BigNumber; + #define SEED_KEY_SIZE 16 + class HmacHash { public: diff --git a/src/shared/Auth/SARC4.cpp b/src/shared/Auth/SARC4.cpp index 5687deece59..f59bb7f0c53 100644 --- a/src/shared/Auth/SARC4.cpp +++ b/src/shared/Auth/SARC4.cpp @@ -15,14 +15,17 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #include "Auth/SARC4.h" #include <openssl/sha.h> + SARC4::SARC4() { EVP_CIPHER_CTX_init(&m_ctx); EVP_EncryptInit_ex(&m_ctx, EVP_rc4(), NULL, NULL, NULL); EVP_CIPHER_CTX_set_key_length(&m_ctx, SHA_DIGEST_LENGTH); } + SARC4::SARC4(uint8 *seed) { EVP_CIPHER_CTX_init(&m_ctx); @@ -30,14 +33,17 @@ SARC4::SARC4(uint8 *seed) EVP_CIPHER_CTX_set_key_length(&m_ctx, SHA_DIGEST_LENGTH); EVP_EncryptInit_ex(&m_ctx, NULL, NULL, seed, NULL); } + SARC4::~SARC4() { EVP_CIPHER_CTX_cleanup(&m_ctx); } + void SARC4::Init(uint8 *seed) { EVP_EncryptInit_ex(&m_ctx, NULL, NULL, seed, NULL); } + void SARC4::UpdateData(int len, uint8 *data) { int outlen = 0; diff --git a/src/shared/Auth/SARC4.h b/src/shared/Auth/SARC4.h index 350b4ad51e6..3f15328d6cb 100644 --- a/src/shared/Auth/SARC4.h +++ b/src/shared/Auth/SARC4.h @@ -15,10 +15,13 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #ifndef _AUTH_SARC4_H #define _AUTH_SARC4_H + #include "Common.h" #include <openssl/evp.h> + class SARC4 { public: diff --git a/src/shared/Auth/Sha1.cpp b/src/shared/Auth/Sha1.cpp index 1cb1ea5fb40..802f1bbcdff 100644 --- a/src/shared/Auth/Sha1.cpp +++ b/src/shared/Auth/Sha1.cpp @@ -17,29 +17,36 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #include "Auth/Sha1.h" #include "Auth/BigNumber.h" #include <stdarg.h> + Sha1Hash::Sha1Hash() { SHA1_Init(&mC); } + 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) @@ -49,10 +56,12 @@ void Sha1Hash::UpdateBigNumbers(BigNumber *bn0, ...) } va_end(v); } + void Sha1Hash::Initialize() { SHA1_Init(&mC); } + void Sha1Hash::Finalize(void) { SHA1_Final(mDigest, &mC); diff --git a/src/shared/Auth/Sha1.h b/src/shared/Auth/Sha1.h index 9b6dad84234..bd2b1afa876 100644 --- a/src/shared/Auth/Sha1.h +++ b/src/shared/Auth/Sha1.h @@ -17,25 +17,34 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ + #ifndef _AUTH_SHA1_H #define _AUTH_SHA1_H + #include "Common.h" #include <openssl/sha.h> #include <openssl/crypto.h> + class BigNumber; + class Sha1Hash { public: Sha1Hash(); ~Sha1Hash(); + void UpdateFinalizeBigNumbers(BigNumber *bn0, ...); 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) { return SHA_DIGEST_LENGTH; }; + private: SHA_CTX mC; uint8 mDigest[SHA_DIGEST_LENGTH]; |