aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShauren <shauren.trinity@gmail.com>2022-01-23 23:49:34 +0100
committerShauren <shauren.trinity@gmail.com>2022-01-23 23:49:34 +0100
commitcacdb57c9cd302e5d746e96cc37aa395564279c6 (patch)
treeb734f2dd66916dc1841f86e432c22499507a5626
parent5f5d32888ac97b24b46a9c2d08f028583faaa911 (diff)
Core/Crypto: c++17-ify crypto code cherry picked earlier that was downgraded to c++14
-rw-r--r--src/common/Cryptography/AES.h5
-rw-r--r--src/common/Cryptography/ARC4.h5
-rw-r--r--src/common/Cryptography/Argon2.h25
-rw-r--r--src/common/Cryptography/CryptoGenerics.h10
-rw-r--r--src/common/Cryptography/TOTP.h19
-rw-r--r--src/server/scripts/Commands/cs_account.cpp4
6 files changed, 25 insertions, 43 deletions
diff --git a/src/common/Cryptography/AES.h b/src/common/Cryptography/AES.h
index e559be75a16..b7ff64c11d0 100644
--- a/src/common/Cryptography/AES.h
+++ b/src/common/Cryptography/AES.h
@@ -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__
diff --git a/src/common/Cryptography/ARC4.h b/src/common/Cryptography/ARC4.h
index 8d5b89787e9..5ce2db5aa3b 100644
--- a/src/common/Cryptography/ARC4.h
+++ b/src/common/Cryptography/ARC4.h
@@ -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
diff --git a/src/common/Cryptography/Argon2.h b/src/common/Cryptography/Argon2.h
index 06f1c6398aa..2c1d44d10d7 100644
--- a/src/common/Cryptography/Argon2.h
+++ b/src/common/Cryptography/Argon2.h
@@ -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
diff --git a/src/common/Cryptography/CryptoGenerics.h b/src/common/Cryptography/CryptoGenerics.h
index affa11bf79d..75ad443b32a 100644
--- a/src/common/Cryptography/CryptoGenerics.h
+++ b/src/common/Cryptography/CryptoGenerics.h
@@ -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
diff --git a/src/common/Cryptography/TOTP.h b/src/common/Cryptography/TOTP.h
index 02e5b9adffc..0aba8ff867e 100644
--- a/src/common/Cryptography/TOTP.h
+++ b/src/common/Cryptography/TOTP.h
@@ -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
diff --git a/src/server/scripts/Commands/cs_account.cpp b/src/server/scripts/Commands/cs_account.cpp
index 29cc59266d6..7ae7baa75ff 100644
--- a/src/server/scripts/Commands/cs_account.cpp
+++ b/src/server/scripts/Commands/cs_account.cpp
@@ -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
{