Core/Crypto: switched away from deprecated openSSL functions in HmacHash
This commit is contained in:
1 parent
1539875440
commit
d20c2f2c34
2 files changed
+13
-24
No files matched your search
@@ -20,58 +20,46 @@
|
||||
#include "Errors.h"
|
||||
#include <cstring>
|
||||
|
||||
#if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER < 0x10100000L
|
||||
HMAC_CTX* HMAC_CTX_new()
|
||||
{
|
||||
HMAC_CTX *ctx = new HMAC_CTX();
|
||||
HMAC_CTX_init(ctx);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void HMAC_CTX_free(HMAC_CTX* ctx)
|
||||
{
|
||||
HMAC_CTX_cleanup(ctx);
|
||||
delete ctx;
|
||||
}
|
||||
#endif
|
||||
|
||||
template<HashCreateFn HashCreator, uint32 DigestLength>
|
||||
HmacHash<HashCreator, DigestLength>::HmacHash(uint32 len, uint8 const* seed) : _ctx(HMAC_CTX_new())
|
||||
HmacHash<HashCreator, DigestLength>::HmacHash(uint32 len, uint8 const* seed) : _ctx(EVP_MD_CTX_create()), _key(EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, nullptr, seed, len))
|
||||
{
|
||||
HMAC_Init_ex(_ctx, seed, len, HashCreator(), nullptr);
|
||||
EVP_DigestSignInit(_ctx, nullptr, HashCreator(), nullptr, _key);
|
||||
memset(_digest, 0, DigestLength);
|
||||
}
|
||||
|
||||
template<HashCreateFn HashCreator, uint32 DigestLength>
|
||||
HmacHash<HashCreator, DigestLength>::~HmacHash()
|
||||
{
|
||||
HMAC_CTX_free(_ctx);
|
||||
EVP_MD_CTX_destroy(_ctx);
|
||||
_ctx = nullptr;
|
||||
EVP_PKEY_free(_key);
|
||||
_key = nullptr;
|
||||
}
|
||||
|
||||
template<HashCreateFn HashCreator, uint32 DigestLength>
|
||||
void HmacHash<HashCreator, DigestLength>::UpdateData(std::string const& str)
|
||||
{
|
||||
HMAC_Update(_ctx, reinterpret_cast<uint8 const*>(str.c_str()), str.length());
|
||||
EVP_DigestSignUpdate(_ctx, reinterpret_cast<uint8 const*>(str.c_str()), str.length());
|
||||
}
|
||||
|
||||
template<HashCreateFn HashCreator, uint32 DigestLength>
|
||||
void HmacHash<HashCreator, DigestLength>::UpdateData(uint8 const* data, size_t len)
|
||||
{
|
||||
HMAC_Update(_ctx, data, len);
|
||||
EVP_DigestSignUpdate(_ctx, data, len);
|
||||
}
|
||||
|
||||
template<HashCreateFn HashCreator, uint32 DigestLength>
|
||||
void HmacHash<HashCreator, DigestLength>::Finalize()
|
||||
{
|
||||
uint32 length = 0;
|
||||
HMAC_Final(_ctx, _digest, &length);
|
||||
size_t length = DigestLength;
|
||||
EVP_DigestSignFinal(_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());
|
||||
EVP_DigestSignUpdate(_ctx, bn->AsByteArray().get(), bn->GetNumBytes());
|
||||
Finalize();
|
||||
return _digest;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,8 @@ class TC_COMMON_API HmacHash
|
||||
uint8* GetDigest() { return _digest; }
|
||||
uint32 GetLength() const { return DigestLength; }
|
||||
private:
|
||||
HMAC_CTX* _ctx;
|
||||
EVP_MD_CTX* _ctx;
|
||||
EVP_PKEY* _key;
|
||||
uint8 _digest[DigestLength];
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user