LibCrypto: Replace all hashes implementation with OpenSSL

This required multiple changes:
- Make hashes non-copiable because they contain a heap allocated pointer
- Reference classes via `NonnullOwnPtr` only (they are non-copiable)
- Drop all existing hashes implementations
- Use the `OpenSSLHashFunction` base class to implement the same hashes

I was not able to come up with a way to divide this commit into multiple
without increasing the amount of changes.

Nothing breaks with this commit!
This commit is contained in:
devgianlu 2024-12-20 09:11:10 +01:00 committed by Ali Mohammad Pur
commit 89061dd3c4
Notes: github-actions[bot] 2024-12-22 17:54:42 +00:00
16 changed files with 164 additions and 1350 deletions

View file

@ -7,71 +7,23 @@
#pragma once
#include <AK/ByteString.h>
#include <LibCrypto/Hash/HashFunction.h>
#include <LibCrypto/Hash/OpenSSLHashFunction.h>
namespace Crypto::Hash {
namespace SHA1Constants {
class SHA1 final : public OpenSSLHashFunction<SHA1, 512, 160> {
AK_MAKE_NONCOPYABLE(SHA1);
constexpr static u32 InitializationHashes[5] { 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0 };
constexpr static u32 RoundConstants[4] {
0X5a827999,
0X6ed9eba1,
0X8f1bbcdc,
0Xca62c1d6,
};
}
class SHA1 final : public HashFunction<512, 160> {
public:
using HashFunction::update;
SHA1()
explicit SHA1(EVP_MD_CTX* context)
: OpenSSLHashFunction(EVP_sha1(), context)
{
reset();
}
virtual void update(u8 const*, size_t) override;
virtual DigestType digest() override;
virtual DigestType peek() override;
static DigestType hash(u8 const* data, size_t length)
{
SHA1 sha;
sha.update(data, length);
return sha.digest();
}
static DigestType hash(ByteBuffer const& buffer) { return hash(buffer.data(), buffer.size()); }
static DigestType hash(StringView buffer) { return hash((u8 const*)buffer.characters_without_null_termination(), buffer.length()); }
virtual ByteString class_name() const override
{
return "SHA1";
}
virtual void reset() override
{
m_data_length = 0;
m_bit_length = 0;
for (auto i = 0; i < 5; ++i)
m_state[i] = SHA1Constants::InitializationHashes[i];
}
private:
inline void transform(u8 const*);
u8 m_data_buffer[BlockSize] {};
size_t m_data_length { 0 };
u64 m_bit_length { 0 };
u32 m_state[5];
constexpr static auto FinalBlockDataSize = BlockSize - 8;
constexpr static auto Rounds = 80;
};
}