Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
Winfidonarleyan committed May 5, 2021
1 parent 880232b commit 3e409cf
Show file tree
Hide file tree
Showing 28 changed files with 241 additions and 423 deletions.
24 changes: 6 additions & 18 deletions src/common/Cryptography/AES.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,31 @@
/*
* This file is part of the WarheadCore Project. See AUTHORS file for Copyright information
*
* 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/>.
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#include "AES.h"
#include "Errors.h"
#include <limits>

Warhead::Crypto::AES::AES(bool encrypting) : _ctx(EVP_CIPHER_CTX_new()), _encrypting(encrypting)
acore::Crypto::AES::AES(bool encrypting) : _ctx(EVP_CIPHER_CTX_new()), _encrypting(encrypting)
{
EVP_CIPHER_CTX_init(_ctx);
int status = EVP_CipherInit_ex(_ctx, EVP_aes_128_gcm(), nullptr, nullptr, nullptr, _encrypting ? 1 : 0);
ASSERT(status);
}

Warhead::Crypto::AES::~AES()
acore::Crypto::AES::~AES()
{
EVP_CIPHER_CTX_free(_ctx);
}

void Warhead::Crypto::AES::Init(Key const& key)
void acore::Crypto::AES::Init(Key const& key)
{
int status = EVP_CipherInit_ex(_ctx, nullptr, nullptr, key.data(), nullptr, -1);
ASSERT(status);
}

bool Warhead::Crypto::AES::Process(IV const& iv, uint8* data, size_t length, Tag& tag)
bool acore::Crypto::AES::Process(IV const& iv, uint8* data, size_t length, Tag& tag)
{
ASSERT(length <= static_cast<size_t>(std::numeric_limits<int>::max()));
int len = static_cast<int>(length);
Expand Down
20 changes: 4 additions & 16 deletions src/common/Cryptography/AES.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
/*
* This file is part of the WarheadCore Project. See AUTHORS file for Copyright information
*
* 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/>.
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#ifndef Warhead_AES_h__
Expand All @@ -22,9 +10,9 @@
#include <array>
#include <openssl/evp.h>

namespace Warhead::Crypto
namespace acore::Crypto
{
class WH_COMMON_API AES
class AC_COMMON_API AES
{
public:
static constexpr size_t IV_SIZE_BYTES = 12;
Expand Down
23 changes: 11 additions & 12 deletions src/common/Cryptography/ARC4.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,26 @@
#define _AUTH_SARC4_H

#include "Define.h"

#include <array>
#include <openssl/evp.h>

namespace acore::Crypto
{
class ARC4
{
public:
ARC4();
~ARC4();
public:
ARC4();
~ARC4();

void Init(uint8 const* seed, size_t len);
template <typename Container>
void Init(Container const& c) { Init(std::data(c), std::size(c)); }
void Init(uint8 const* seed, size_t len);
template <typename Container>
void Init(Container const& c) { Init(std::data(c), std::size(c)); }

void UpdateData(uint8* data, size_t len);
template <typename Container>
void UpdateData(Container& c) { UpdateData(std::data(c), std::size(c)); }
private:
EVP_CIPHER_CTX* _ctx;
void UpdateData(uint8* data, size_t len);
template <typename Container>
void UpdateData(Container& c) { UpdateData(std::data(c), std::size(c)); }
private:
EVP_CIPHER_CTX* _ctx;
};
}

Expand Down
20 changes: 4 additions & 16 deletions src/common/Cryptography/Argon2.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
/*
* This file is part of the WarheadCore Project. See AUTHORS file for Copyright information
*
* 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/>.
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#include "Argon2.h"
#include <argon2/argon2.h>

/*static*/ Optional<std::string> Warhead::Crypto::Argon2::Hash(std::string const& password, BigNumber const& salt, uint32 nIterations, uint32 kibMemoryCost)
/*static*/ Optional<std::string> acore::Crypto::Argon2::Hash(std::string const& password, BigNumber const& salt, uint32 nIterations, uint32 kibMemoryCost)
{
char buf[ENCODED_HASH_LEN];
std::vector<uint8> saltBytes = salt.ToByteVector();
Expand All @@ -37,7 +25,7 @@
return {};
}

/*static*/ bool Warhead::Crypto::Argon2::Verify(std::string const& password, std::string const& hash)
/*static*/ bool acore::Crypto::Argon2::Verify(std::string const& password, std::string const& hash)
{
int status = argon2id_verify(hash.c_str(), password.c_str(), password.length());
return (status == ARGON2_OK);
Expand Down
20 changes: 4 additions & 16 deletions src/common/Cryptography/Argon2.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
/*
* This file is part of the WarheadCore Project. See AUTHORS file for Copyright information
*
* 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/>.
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#ifndef WARHEAD_ARGON2_H
Expand All @@ -23,9 +11,9 @@
#include "Optional.h"
#include <string>

namespace Warhead::Crypto
namespace acore::Crypto
{
struct WH_COMMON_API Argon2
struct AC_COMMON_API Argon2
{
static constexpr uint32 HASH_LEN = 16; // 128 bits, in bytes
static constexpr uint32 ENCODED_HASH_LEN = 100; // in chars
Expand Down
2 changes: 1 addition & 1 deletion src/common/Cryptography/Authentication/SRP6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ std::optional<SessionKey> SRP6::VerifyChallengeResponse(EphemeralKey const& A, S
_used = true;

BigNumber const _A(A);
if ((_A % _N).isZero())
if ((_A % _N).IsZero())
return std::nullopt;

BigNumber const u(SHA1::GetDigestOf(A, B));
Expand Down
16 changes: 2 additions & 14 deletions src/common/Cryptography/BigNumber.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
/*
* This file is part of the WarheadCore Project. See AUTHORS file for Copyright information
*
* 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/>.
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#include "Cryptography/BigNumber.h"
Expand Down
21 changes: 6 additions & 15 deletions src/common/Cryptography/BigNumber.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
/*
* This file is part of the WarheadCore Project. See AUTHORS file for Copyright information
*
* 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/>.
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU GPL v2 license, you may redistribute it and/or modify it under version 2 of the License, or (at your option), any later version.
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#ifndef _AUTH_BIGNUMBER_H
Expand All @@ -26,14 +14,15 @@

struct bignum_st;

class WH_COMMON_API BigNumber
class AC_COMMON_API BigNumber
{
public:
BigNumber();
BigNumber(BigNumber const& bn);
BigNumber(uint32 v) : BigNumber() { SetDword(v); }
BigNumber(int32 v) : BigNumber() { SetDword(v); }
BigNumber(std::string const& v) : BigNumber() { SetHexStr(v); }

template <size_t Size>
BigNumber(std::array<uint8, Size> const& v, bool littleEndian = true) : BigNumber() { SetBinary(v.data(), Size, littleEndian); }

Expand All @@ -43,8 +32,10 @@ class WH_COMMON_API BigNumber
void SetDword(uint32);
void SetQword(uint64);
void SetBinary(uint8 const* bytes, int32 len, bool littleEndian = true);

template <typename Container>
auto SetBinary(Container const& c, bool littleEndian = true) -> std::enable_if_t<!std::is_pointer_v<std::decay_t<Container>>> { SetBinary(std::data(c), std::size(c), littleEndian); }

bool SetHexStr(char const* str);
bool SetHexStr(std::string const& str) { return SetHexStr(str.c_str()); }

Expand Down
25 changes: 8 additions & 17 deletions src/common/Cryptography/TOTP.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
/*
* This file is part of the WarheadCore Project. See AUTHORS file for Copyright information
*
* 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/>.
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#include "TOTP.h"
#include <cstring>
#include <openssl/evp.h>
#include <openssl/hmac.h>

constexpr std::size_t Warhead::Crypto::TOTP::RECOMMENDED_SECRET_LENGTH;
constexpr std::size_t acore::Crypto::TOTP::RECOMMENDED_SECRET_LENGTH;
static constexpr uint32 TOTP_INTERVAL = 30;
static constexpr uint32 HMAC_RESULT_SIZE = 20;
/*static*/ uint32 Warhead::Crypto::TOTP::GenerateToken(Secret const& secret, time_t timestamp)

/*static*/ uint32 acore::Crypto::TOTP::GenerateToken(Secret const& secret, time_t timestamp)
{
timestamp /= TOTP_INTERVAL;
unsigned char challenge[8];

for (int i = 8; i--; timestamp >>= 8)
challenge[i] = timestamp;

Expand All @@ -37,10 +27,11 @@ static constexpr uint32 HMAC_RESULT_SIZE = 20;
uint32 offset = digest[19] & 0xF;
uint32 truncated = (digest[offset] << 24) | (digest[offset + 1] << 16) | (digest[offset + 2] << 8) | (digest[offset + 3]);
truncated &= 0x7FFFFFFF;

return (truncated % 1000000);
}

/*static*/ bool Warhead::Crypto::TOTP::ValidateToken(Secret const& secret, uint32 token)
/*static*/ bool acore::Crypto::TOTP::ValidateToken(Secret const& secret, uint32 token)
{
time_t now = time(nullptr);
return (
Expand Down
20 changes: 4 additions & 16 deletions src/common/Cryptography/TOTP.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
/*
* This file is part of the WarheadCore Project. See AUTHORS file for Copyright information
*
* 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/>.
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#ifndef WARHEAD_TOTP_H
Expand All @@ -22,9 +10,9 @@
#include <ctime>
#include <vector>

namespace Warhead::Crypto
namespace acore::Crypto
{
struct WH_COMMON_API TOTP
struct AC_COMMON_API TOTP
{
static constexpr size_t RECOMMENDED_SECRET_LENGTH = 20;
using Secret = std::vector<uint8>;
Expand Down
24 changes: 6 additions & 18 deletions src/common/Encoding/Base32.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
/*
* This file is part of the WarheadCore Project. See AUTHORS file for Copyright information
*
* 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/>.
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license: https://github.com/azerothcore/azerothcore-wotlk/blob/master/LICENSE-AGPL3
* Copyright (C) 2021+ WarheadCore <https://github.com/WarheadCore>
*/

#include "Base32.h"
Expand Down Expand Up @@ -44,12 +32,12 @@ struct B32Impl
}
};

/*static*/ std::string Warhead::Encoding::Base32::Encode(std::vector<uint8> const& data)
/*static*/ std::string acore::Encoding::Base32::Encode(std::vector<uint8> const& data)
{
return Warhead::Impl::GenericBaseEncoding<B32Impl>::Encode(data);
return acore::Impl::GenericBaseEncoding<B32Impl>::Encode(data);
}

/*static*/ Optional<std::vector<uint8>> Warhead::Encoding::Base32::Decode(std::string const& data)
/*static*/ Optional<std::vector<uint8>> acore::Encoding::Base32::Decode(std::string const& data)
{
return Warhead::Impl::GenericBaseEncoding<B32Impl>::Decode(data);
return acore::Impl::GenericBaseEncoding<B32Impl>::Decode(data);
}
Loading

0 comments on commit 3e409cf

Please sign in to comment.