From 8ae01f81c925a2296bde23fc49e443ae78f345cf Mon Sep 17 00:00:00 2001 From: devgianlu Date: Sat, 15 Feb 2025 14:18:35 +0100 Subject: [PATCH] LibCrypto: Remove unused `MGF` class --- Libraries/LibCrypto/Hash/MGF.h | 57 ------------------ Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp | 1 - Tests/LibCrypto/CMakeLists.txt | 1 - Tests/LibCrypto/TestMGF.cpp | 63 -------------------- 4 files changed, 122 deletions(-) delete mode 100644 Libraries/LibCrypto/Hash/MGF.h delete mode 100644 Tests/LibCrypto/TestMGF.cpp diff --git a/Libraries/LibCrypto/Hash/MGF.h b/Libraries/LibCrypto/Hash/MGF.h deleted file mode 100644 index 43764c84022..00000000000 --- a/Libraries/LibCrypto/Hash/MGF.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2024, stelar7 - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include -#include - -namespace Crypto::Hash { - -class MGF { -public: - // https://datatracker.ietf.org/doc/html/rfc2437#section-10.2.1 - template - static ErrorOr mgf1(ReadonlyBytes seed, size_t length) - requires requires { HashFunction::digest_size(); } - { - auto hash = HashFunction::create(); - - size_t h_len = hash->digest_size(); - - // 1. If length > 2^32(hLen), output "mask too long" and stop. - if constexpr (sizeof(size_t) > 32) { - if (length > (h_len << 32)) - return Error::from_string_literal("mask too long"); - } - - // 2. Let T be the empty octet string. - auto t = TRY(ByteBuffer::create_uninitialized(0)); - - // 3. For counter from 0 to ceil(length / hLen) - 1, do the following: - auto counter = 0u; - auto iterations = AK::ceil_div(length, h_len) - 1; - - auto c = TRY(ByteBuffer::create_uninitialized(4)); - for (; counter <= iterations; ++counter) { - // a. Convert counter to an octet string C of length 4 with the primitive I2OSP: C = I2OSP(counter, 4) - ByteReader::store(static_cast(c.data()), AK::convert_between_host_and_big_endian(static_cast(counter))); - - // b. Concatenate the hash of the seed Z and C to the octet string T: T = T || Hash (Z || C) - hash->update(seed); - hash->update(c); - auto digest = hash->digest(); - - TRY(t.try_append(digest.bytes())); - } - - // 4. Output the leading l octets of T as the octet string mask. - return t.slice(0, length); - } -}; - -} diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp index 981a147a0f7..07d0c034296 100644 --- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp +++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include diff --git a/Tests/LibCrypto/CMakeLists.txt b/Tests/LibCrypto/CMakeLists.txt index 946bf2c4989..ab65c793d84 100644 --- a/Tests/LibCrypto/CMakeLists.txt +++ b/Tests/LibCrypto/CMakeLists.txt @@ -9,7 +9,6 @@ set(TEST_SOURCES TestHash.cpp TestHKDF.cpp TestHMAC.cpp - TestMGF.cpp TestOAEP.cpp TestPBKDF2.cpp TestPSS.cpp diff --git a/Tests/LibCrypto/TestMGF.cpp b/Tests/LibCrypto/TestMGF.cpp deleted file mode 100644 index d6a834936aa..00000000000 --- a/Tests/LibCrypto/TestMGF.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2024, stelar7 - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include -#include - -static ByteBuffer operator""_b(char const* string, size_t length) -{ - return ByteBuffer::copy(string, length).release_value(); -} - -TEST_CASE(test_mgf1_short) -{ - u8 expected_result[3] { - 0x1a, 0xc9, 0x07 - }; - auto expected = ReadonlyBytes { expected_result, 3 }; - - ByteBuffer seed = { "foo"_b }; - auto length = 3; - ByteBuffer result = MUST(Crypto::Hash::MGF::mgf1(seed, length)); - - EXPECT_EQ(expected, result); -} - -TEST_CASE(test_mgf1_long) -{ - u8 expected_result[50] { - 0xbc, 0x0c, 0x65, 0x5e, 0x01, 0x6b, 0xc2, 0x93, 0x1d, 0x85, 0xa2, 0xe6, 0x75, 0x18, 0x1a, 0xdc, - 0xef, 0x7f, 0x58, 0x1f, 0x76, 0xdf, 0x27, 0x39, 0xda, 0x74, 0xfa, 0xac, 0x41, 0x62, 0x7b, 0xe2, - 0xf7, 0xf4, 0x15, 0xc8, 0x9e, 0x98, 0x3f, 0xd0, 0xce, 0x80, 0xce, 0xd9, 0x87, 0x86, 0x41, 0xcb, - 0x48, 0x76 - }; - auto expected = ReadonlyBytes { expected_result, 50 }; - - ByteBuffer seed = { "bar"_b }; - auto length = 50; - ByteBuffer result = MUST(Crypto::Hash::MGF::mgf1(seed, length)); - - EXPECT_EQ(expected, result); -} - -TEST_CASE(test_mgf1_long_sha256) -{ - u8 expected_result[50] { - 0x38, 0x25, 0x76, 0xa7, 0x84, 0x10, 0x21, 0xcc, 0x28, 0xfc, 0x4c, 0x09, 0x48, 0x75, 0x3f, 0xb8, - 0x31, 0x20, 0x90, 0xce, 0xa9, 0x42, 0xea, 0x4c, 0x4e, 0x73, 0x5d, 0x10, 0xdc, 0x72, 0x4b, 0x15, - 0x5f, 0x9f, 0x60, 0x69, 0xf2, 0x89, 0xd6, 0x1d, 0xac, 0xa0, 0xcb, 0x81, 0x45, 0x02, 0xef, 0x04, - 0xea, 0xe1 - }; - auto expected = ReadonlyBytes { expected_result, 50 }; - - ByteBuffer seed = { "bar"_b }; - auto length = 50; - ByteBuffer result = MUST(Crypto::Hash::MGF::mgf1(seed, length)); - - EXPECT_EQ(expected, result); -}