Core/Crypto: c++17-ify crypto code cherry picked earlier that was downgraded to c++14

This commit is contained in:
Shauren
2022-01-23 23:49:34 +01:00
parent 5f5d32888a
commit cacdb57c9c
6 changed files with 25 additions and 43 deletions

View File

@@ -22,9 +22,7 @@
#include <array>
#include <openssl/evp.h>
namespace Trinity
{
namespace Crypto
namespace Trinity::Crypto
{
class TC_COMMON_API AES
{
@@ -50,6 +48,5 @@ namespace Crypto
bool _encrypting;
};
}
}
#endif // Trinity_AES_h__

View File

@@ -22,9 +22,7 @@
#include <array>
#include <openssl/evp.h>
namespace Trinity
{
namespace Crypto
namespace Trinity::Crypto
{
class TC_COMMON_API ARC4
{
@@ -43,6 +41,5 @@ namespace Crypto
EVP_CIPHER_CTX* _ctx;
};
}
}
#endif

View File

@@ -23,22 +23,19 @@
#include "Optional.h"
#include <string>
namespace Trinity
namespace Trinity::Crypto
{
namespace Crypto
{
struct TC_COMMON_API Argon2
{
static constexpr uint32 HASH_LEN = 16; // 128 bits, in bytes
static constexpr uint32 ENCODED_HASH_LEN = 100; // in chars
static constexpr uint32 DEFAULT_ITERATIONS = 10; // determined by dice roll, guaranteed to be secure (not really)
static constexpr uint32 DEFAULT_MEMORY_COST = (1u << 17); // 2^17 kibibytes is 2^7 mebibytes is ~100MB
static constexpr uint32 PARALLELISM = 1; // we don't support threaded hashing
struct TC_COMMON_API Argon2
{
static constexpr uint32 HASH_LEN = 16; // 128 bits, in bytes
static constexpr uint32 ENCODED_HASH_LEN = 100; // in chars
static constexpr uint32 DEFAULT_ITERATIONS = 10; // determined by dice roll, guaranteed to be secure (not really)
static constexpr uint32 DEFAULT_MEMORY_COST = (1u << 17); // 2^17 kibibytes is 2^7 mebibytes is ~100MB
static constexpr uint32 PARALLELISM = 1; // we don't support threaded hashing
static Optional<std::string> Hash(std::string const& password, BigNumber const& salt, uint32 nIterations = DEFAULT_ITERATIONS, uint32 kibMemoryCost = DEFAULT_MEMORY_COST);
static bool Verify(std::string const& password, std::string const& hash);
};
}
static Optional<std::string> Hash(std::string const& password, BigNumber const& salt, uint32 nIterations = DEFAULT_ITERATIONS, uint32 kibMemoryCost = DEFAULT_MEMORY_COST);
static bool Verify(std::string const& password, std::string const& hash);
};
}
#endif

View File

@@ -25,9 +25,7 @@
#include <iterator>
#include <vector>
namespace Trinity
{
namespace Impl
namespace Trinity::Impl
{
struct CryptoGenericsImpl
{
@@ -57,11 +55,8 @@ namespace Impl
}
};
}
}
namespace Trinity
{
namespace Crypto
namespace Trinity::Crypto
{
template <typename Cipher>
void AEEncryptWithRandomIV(std::vector<uint8>& data, typename Cipher::Key const& key)
@@ -112,6 +107,5 @@ namespace Crypto
return AEDecrypt<Cipher>(data, key.ToByteArray<Cipher::KEY_SIZE_BYTES>());
}
}
}
#endif

View File

@@ -22,19 +22,16 @@
#include <ctime>
#include <vector>
namespace Trinity
namespace Trinity::Crypto
{
namespace Crypto
{
struct TC_COMMON_API TOTP
{
static constexpr std::size_t RECOMMENDED_SECRET_LENGTH = 20;
using Secret = std::vector<uint8>;
struct TC_COMMON_API TOTP
{
static constexpr size_t RECOMMENDED_SECRET_LENGTH = 20;
using Secret = std::vector<uint8>;
static uint32 GenerateToken(Secret const& key, time_t timestamp);
static bool ValidateToken(Secret const& key, uint32 token);
};
}
static uint32 GenerateToken(Secret const& key, time_t timestamp);
static bool ValidateToken(Secret const& key, uint32 token);
};
}
#endif

View File

@@ -27,6 +27,7 @@ EndScriptData */
#include "Base32.h"
#include "Chat.h"
#include "CryptoGenerics.h"
#include "CryptoRandom.h"
#include "DatabaseEnv.h"
#include "IpAddress.h"
#include "IPLocation.h"
@@ -39,7 +40,6 @@ EndScriptData */
#include "World.h"
#include "WorldSession.h"
#include <unordered_map>
#include <openssl/rand.h>
using namespace Trinity::ChatCommands;
@@ -131,7 +131,7 @@ public:
static std::unordered_map<uint32, Trinity::Crypto::TOTP::Secret> suggestions;
auto pair = suggestions.emplace(std::piecewise_construct, std::make_tuple(accountId), std::make_tuple(Trinity::Crypto::TOTP::RECOMMENDED_SECRET_LENGTH)); // std::vector 1-argument size_t constructor invokes resize
if (pair.second) // no suggestion yet, generate random secret
RAND_bytes(pair.first->second.data(), pair.first->second.size());
Trinity::Crypto::GetRandomBytes(pair.first->second);
if (!pair.second && token) // suggestion already existed and token specified - validate
{