mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-07 08:39:22 +00:00
LibCrypto: Remove unused GHash
class
This commit is contained in:
parent
05f3b1f361
commit
40c71ff3c0
Notes:
github-actions[bot]
2025-03-02 14:12:37 +00:00
Author: https://github.com/devgianlu
Commit: 40c71ff3c0
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/3749
Reviewed-by: https://github.com/konradekk
4 changed files with 0 additions and 209 deletions
|
@ -1,123 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <AK/ByteReader.h>
|
|
||||||
#include <AK/Debug.h>
|
|
||||||
#include <AK/Types.h>
|
|
||||||
#include <LibCrypto/Authentication/GHash.h>
|
|
||||||
|
|
||||||
namespace {
|
|
||||||
|
|
||||||
static u32 to_u32(u8 const* b)
|
|
||||||
{
|
|
||||||
return AK::convert_between_host_and_big_endian(ByteReader::load32(b));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void to_u8s(u8* b, u32 const* w)
|
|
||||||
{
|
|
||||||
for (auto i = 0; i < 4; ++i) {
|
|
||||||
ByteReader::store(b + i * 4, AK::convert_between_host_and_big_endian(w[i]));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace Crypto::Authentication {
|
|
||||||
|
|
||||||
void GHash::process_one(u32 (&tag)[4], ReadonlyBytes buf) const
|
|
||||||
{
|
|
||||||
size_t i = 0;
|
|
||||||
for (; i < buf.size(); i += 16) {
|
|
||||||
if (i + 16 <= buf.size()) {
|
|
||||||
for (auto j = 0; j < 4; ++j) {
|
|
||||||
tag[j] ^= to_u32(buf.offset(i + j * 4));
|
|
||||||
}
|
|
||||||
galois_multiply(tag, m_key, tag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (i > buf.size()) {
|
|
||||||
u8 buffer[16] = {};
|
|
||||||
Bytes buffer_bytes { buffer, 16 };
|
|
||||||
(void)buf.slice(i - 16).copy_to(buffer_bytes);
|
|
||||||
|
|
||||||
for (auto j = 0; j < 4; ++j) {
|
|
||||||
tag[j] ^= to_u32(buffer_bytes.offset(j * 4));
|
|
||||||
}
|
|
||||||
galois_multiply(tag, m_key, tag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GHash::TagType GHash::process(ReadonlyBytes aad, ReadonlyBytes cipher)
|
|
||||||
{
|
|
||||||
u32 tag[4] { 0, 0, 0, 0 };
|
|
||||||
|
|
||||||
process_one(tag, aad);
|
|
||||||
process_one(tag, cipher);
|
|
||||||
|
|
||||||
auto aad_bits = 8 * (u64)aad.size();
|
|
||||||
auto cipher_bits = 8 * (u64)cipher.size();
|
|
||||||
|
|
||||||
auto high = [](u64 value) -> u32 { return value >> 32; };
|
|
||||||
auto low = [](u64 value) -> u32 { return value & 0xffffffff; };
|
|
||||||
|
|
||||||
if constexpr (GHASH_PROCESS_DEBUG) {
|
|
||||||
dbgln("AAD bits: {} : {}", high(aad_bits), low(aad_bits));
|
|
||||||
dbgln("Cipher bits: {} : {}", high(cipher_bits), low(cipher_bits));
|
|
||||||
dbgln("Tag bits: {} : {} : {} : {}", tag[0], tag[1], tag[2], tag[3]);
|
|
||||||
}
|
|
||||||
|
|
||||||
tag[0] ^= high(aad_bits);
|
|
||||||
tag[1] ^= low(aad_bits);
|
|
||||||
tag[2] ^= high(cipher_bits);
|
|
||||||
tag[3] ^= low(cipher_bits);
|
|
||||||
|
|
||||||
dbgln_if(GHASH_PROCESS_DEBUG, "Tag bits: {} : {} : {} : {}", tag[0], tag[1], tag[2], tag[3]);
|
|
||||||
|
|
||||||
galois_multiply(tag, m_key, tag);
|
|
||||||
|
|
||||||
TagType digest;
|
|
||||||
to_u8s(digest.data, tag);
|
|
||||||
|
|
||||||
return digest;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Galois Field multiplication using <x^127 + x^7 + x^2 + x + 1>.
|
|
||||||
/// Note that x, y, and z are strictly BE.
|
|
||||||
void galois_multiply(u32 (&_z)[4], u32 const (&_x)[4], u32 const (&_y)[4])
|
|
||||||
{
|
|
||||||
// Note: Copied upfront to stack to avoid memory access in the loop.
|
|
||||||
u32 x[4] { _x[0], _x[1], _x[2], _x[3] };
|
|
||||||
u32 const y[4] { _y[0], _y[1], _y[2], _y[3] };
|
|
||||||
u32 z[4] { 0, 0, 0, 0 };
|
|
||||||
|
|
||||||
// Unrolled by 32, the access in y[3-(i/32)] can be cached throughout the loop.
|
|
||||||
#pragma GCC unroll 32
|
|
||||||
for (ssize_t i = 127; i > -1; --i) {
|
|
||||||
auto r = -((y[3 - (i / 32)] >> (i % 32)) & 1);
|
|
||||||
z[0] ^= x[0] & r;
|
|
||||||
z[1] ^= x[1] & r;
|
|
||||||
z[2] ^= x[2] & r;
|
|
||||||
z[3] ^= x[3] & r;
|
|
||||||
auto a0 = x[0] & 1;
|
|
||||||
x[0] >>= 1;
|
|
||||||
auto a1 = x[1] & 1;
|
|
||||||
x[1] >>= 1;
|
|
||||||
x[1] |= a0 << 31;
|
|
||||||
auto a2 = x[2] & 1;
|
|
||||||
x[2] >>= 1;
|
|
||||||
x[2] |= a1 << 31;
|
|
||||||
auto a3 = x[3] & 1;
|
|
||||||
x[3] >>= 1;
|
|
||||||
x[3] |= a2 << 31;
|
|
||||||
|
|
||||||
x[0] ^= 0xe1000000 & -a3;
|
|
||||||
}
|
|
||||||
|
|
||||||
memcpy(_z, z, sizeof(z));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,58 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <AK/ByteReader.h>
|
|
||||||
#include <AK/ByteString.h>
|
|
||||||
#include <AK/Endian.h>
|
|
||||||
#include <AK/Types.h>
|
|
||||||
|
|
||||||
namespace Crypto::Authentication {
|
|
||||||
|
|
||||||
void galois_multiply(u32 (&z)[4], u32 const (&x)[4], u32 const (&y)[4]);
|
|
||||||
|
|
||||||
struct GHashDigest {
|
|
||||||
constexpr static size_t Size = 16;
|
|
||||||
u8 data[Size];
|
|
||||||
|
|
||||||
u8 const* immutable_data() const { return data; }
|
|
||||||
size_t data_length() { return Size; }
|
|
||||||
};
|
|
||||||
|
|
||||||
class GHash final {
|
|
||||||
public:
|
|
||||||
using TagType = GHashDigest;
|
|
||||||
|
|
||||||
template<size_t N>
|
|
||||||
explicit GHash(char const (&key)[N])
|
|
||||||
: GHash({ key, N })
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
explicit GHash(ReadonlyBytes key)
|
|
||||||
{
|
|
||||||
VERIFY(key.size() >= 16);
|
|
||||||
for (size_t i = 0; i < 16; i += 4) {
|
|
||||||
m_key[i / 4] = AK::convert_between_host_and_big_endian(ByteReader::load32(key.offset(i)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
constexpr static size_t digest_size() { return TagType::Size; }
|
|
||||||
|
|
||||||
ByteString class_name() const
|
|
||||||
{
|
|
||||||
return "GHash";
|
|
||||||
}
|
|
||||||
|
|
||||||
void process_one(u32 (&tag)[4], ReadonlyBytes buf) const;
|
|
||||||
TagType process(ReadonlyBytes aad, ReadonlyBytes cipher);
|
|
||||||
|
|
||||||
private:
|
|
||||||
u32 m_key[4];
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
|
@ -5,7 +5,6 @@ set(SOURCES
|
||||||
ASN1/ASN1.cpp
|
ASN1/ASN1.cpp
|
||||||
ASN1/DER.cpp
|
ASN1/DER.cpp
|
||||||
ASN1/PEM.cpp
|
ASN1/PEM.cpp
|
||||||
Authentication/GHash.cpp
|
|
||||||
Authentication/HMAC.cpp
|
Authentication/HMAC.cpp
|
||||||
BigFraction/BigFraction.cpp
|
BigFraction/BigFraction.cpp
|
||||||
BigInt/Algorithms/BitwiseOperations.cpp
|
BigInt/Algorithms/BitwiseOperations.cpp
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <LibCrypto/Authentication/GHash.h>
|
|
||||||
#include <LibCrypto/Authentication/HMAC.h>
|
#include <LibCrypto/Authentication/HMAC.h>
|
||||||
#include <LibCrypto/Hash/BLAKE2b.h>
|
#include <LibCrypto/Hash/BLAKE2b.h>
|
||||||
#include <LibCrypto/Hash/MD5.h>
|
#include <LibCrypto/Hash/MD5.h>
|
||||||
|
@ -299,29 +298,3 @@ TEST_CASE(test_SHA512_hash_empty_string)
|
||||||
auto digest = Crypto::Hash::SHA512::hash(""sv);
|
auto digest = Crypto::Hash::SHA512::hash(""sv);
|
||||||
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA512::digest_size()) == 0);
|
EXPECT(memcmp(result, digest.data, Crypto::Hash::SHA512::digest_size()) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE(test_ghash_test_name)
|
|
||||||
{
|
|
||||||
Crypto::Authentication::GHash ghash("WellHelloFriends");
|
|
||||||
EXPECT_EQ(ghash.class_name(), "GHash");
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE(test_ghash_galois_field_multiply)
|
|
||||||
{
|
|
||||||
u32 x[4] { 0x42831ec2, 0x21777424, 0x4b7221b7, 0x84d0d49c },
|
|
||||||
y[4] { 0xb83b5337, 0x08bf535d, 0x0aa6e529, 0x80d53b78 }, z[4] { 0, 0, 0, 0 };
|
|
||||||
static constexpr u32 result[4] { 0x59ed3f2b, 0xb1a0aaa0, 0x7c9f56c6, 0xa504647b };
|
|
||||||
|
|
||||||
Crypto::Authentication::galois_multiply(z, x, y);
|
|
||||||
EXPECT(memcmp(result, z, 4 * sizeof(u32)) == 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
TEST_CASE(test_ghash_galois_field_multiply2)
|
|
||||||
{
|
|
||||||
u32 x[4] { 59300558, 1622582162, 4079534777, 1907555960 },
|
|
||||||
y[4] { 1726565332, 4018809915, 2286746201, 3392416558 }, z[4];
|
|
||||||
constexpr static u32 result[4] { 1580123974, 2440061576, 746958952, 1398005431 };
|
|
||||||
|
|
||||||
Crypto::Authentication::galois_multiply(z, x, y);
|
|
||||||
EXPECT(memcmp(result, z, 4 * sizeof(u32)) == 0);
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue