mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09:00
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:
parent
2d799727e8
commit
89061dd3c4
Notes:
github-actions[bot]
2024-12-22 17:54:42 +00:00
Author: https://github.com/devgianlu
Commit: 89061dd3c4
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2989
Reviewed-by: https://github.com/ADKaster
Reviewed-by: https://github.com/alimpfard ✅
Reviewed-by: https://github.com/rmg-x
16 changed files with 164 additions and 1350 deletions
|
@ -24,12 +24,12 @@ public:
|
|||
using HashType = HashT;
|
||||
using TagType = typename HashType::DigestType;
|
||||
|
||||
constexpr size_t digest_size() const { return m_inner_hasher.digest_size(); }
|
||||
size_t digest_size() const { return m_inner_hasher->digest_size(); }
|
||||
|
||||
template<typename KeyBufferType, typename... Args>
|
||||
HMAC(KeyBufferType key, Args... args)
|
||||
: m_inner_hasher(args...)
|
||||
, m_outer_hasher(args...)
|
||||
: m_inner_hasher(move(HashT::create(args...)))
|
||||
, m_outer_hasher(move(HashT::create(args...)))
|
||||
{
|
||||
derive_key(key);
|
||||
reset();
|
||||
|
@ -44,7 +44,7 @@ public:
|
|||
|
||||
void update(u8 const* message, size_t length)
|
||||
{
|
||||
m_inner_hasher.update(message, length);
|
||||
m_inner_hasher->update(message, length);
|
||||
}
|
||||
|
||||
TagType process(ReadonlyBytes span) { return process(span.data(), span.size()); }
|
||||
|
@ -55,32 +55,32 @@ public:
|
|||
|
||||
TagType digest()
|
||||
{
|
||||
m_outer_hasher.update(m_inner_hasher.digest().immutable_data(), m_inner_hasher.digest_size());
|
||||
auto result = m_outer_hasher.digest();
|
||||
m_outer_hasher->update(m_inner_hasher->digest().immutable_data(), m_inner_hasher->digest_size());
|
||||
auto result = m_outer_hasher->digest();
|
||||
reset();
|
||||
return result;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_inner_hasher.reset();
|
||||
m_outer_hasher.reset();
|
||||
m_inner_hasher.update(m_key_data, m_inner_hasher.block_size());
|
||||
m_outer_hasher.update(m_key_data + m_inner_hasher.block_size(), m_outer_hasher.block_size());
|
||||
m_inner_hasher->reset();
|
||||
m_outer_hasher->reset();
|
||||
m_inner_hasher->update(m_key_data, m_inner_hasher->block_size());
|
||||
m_outer_hasher->update(m_key_data + m_inner_hasher->block_size(), m_outer_hasher->block_size());
|
||||
}
|
||||
|
||||
ByteString class_name() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.append("HMAC-"sv);
|
||||
builder.append(m_inner_hasher.class_name());
|
||||
builder.append(m_inner_hasher->class_name());
|
||||
return builder.to_byte_string();
|
||||
}
|
||||
|
||||
private:
|
||||
void derive_key(u8 const* key, size_t length)
|
||||
{
|
||||
auto block_size = m_inner_hasher.block_size();
|
||||
auto block_size = m_inner_hasher->block_size();
|
||||
// Note: The block size of all the current hash functions is 512 bits.
|
||||
Vector<u8, 64> v_key;
|
||||
v_key.resize(block_size);
|
||||
|
@ -89,10 +89,10 @@ private:
|
|||
// the first few bytes leaves the rest zero, which
|
||||
// is exactly what we want (zero padding)
|
||||
if (length > block_size) {
|
||||
m_inner_hasher.update(key, length);
|
||||
auto digest = m_inner_hasher.digest();
|
||||
m_inner_hasher->update(key, length);
|
||||
auto digest = m_inner_hasher->digest();
|
||||
// FIXME: should we check if the hash function creates more data than its block size?
|
||||
key_buffer.overwrite(0, digest.immutable_data(), m_inner_hasher.digest_size());
|
||||
key_buffer.overwrite(0, digest.immutable_data(), m_inner_hasher->digest_size());
|
||||
} else if (length > 0) {
|
||||
key_buffer.overwrite(0, key, length);
|
||||
}
|
||||
|
@ -110,7 +110,7 @@ private:
|
|||
void derive_key(ReadonlyBytes key) { derive_key(key.data(), key.size()); }
|
||||
void derive_key(StringView key) { derive_key(key.bytes()); }
|
||||
|
||||
HashType m_inner_hasher, m_outer_hasher;
|
||||
NonnullOwnPtr<HashType> m_inner_hasher, m_outer_hasher;
|
||||
u8 m_key_data[2048];
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue