LibCrypto: Exclude class_name() methods from the Kernel

These are only used by Userland and contain infallible String
allocations, so let's just ifdef them out of the Kernel.
This commit is contained in:
Idan Horowitz 2022-02-15 21:36:46 +02:00 committed by Andreas Kling
parent 6959e7f778
commit c8db8d6152
Notes: sideshowbarker 2024-07-17 18:40:56 +09:00
16 changed files with 119 additions and 21 deletions

View file

@ -9,7 +9,6 @@
#include <AK/MemoryStream.h>
#include <AK/Types.h>
#include <LibCrypto/Authentication/GHash.h>
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
namespace {

View file

@ -7,10 +7,14 @@
#pragma once
#include <AK/ByteReader.h>
#include <AK/String.h>
#include <AK/Endian.h>
#include <AK/Types.h>
#include <LibCrypto/Hash/HashFunction.h>
#ifndef KERNEL
# include <AK/String.h>
#endif
namespace Crypto {
namespace Authentication {
@ -44,7 +48,12 @@ public:
constexpr static size_t digest_size() { return TagType::Size; }
String class_name() const { return "GHash"; }
#ifndef KERNEL
String class_name() const
{
return "GHash";
}
#endif
TagType process(ReadonlyBytes aad, ReadonlyBytes cipher);

View file

@ -7,12 +7,15 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#ifndef KERNEL
# include <AK/String.h>
#endif
constexpr static auto IPAD = 0x36;
constexpr static auto OPAD = 0x5c;
@ -70,6 +73,7 @@ public:
m_outer_hasher.update(m_key_data + m_inner_hasher.block_size(), m_outer_hasher.block_size());
}
#ifndef KERNEL
String class_name() const
{
StringBuilder builder;
@ -77,6 +81,7 @@ public:
builder.append(m_inner_hasher.class_name());
return builder.build();
}
#endif
private:
void derive_key(const u8* key, size_t length)

View file

@ -119,7 +119,12 @@ public:
virtual void encrypt_block(const BlockType& in, BlockType& out) override;
virtual void decrypt_block(const BlockType& in, BlockType& out) override;
virtual String class_name() const override { return "AES"; }
#ifndef KERNEL
virtual String class_name() const override
{
return "AES";
}
#endif
protected:
AESCipherKey m_key;

View file

@ -111,7 +111,9 @@ public:
virtual void encrypt_block(const BlockType& in, BlockType& out) = 0;
virtual void decrypt_block(const BlockType& in, BlockType& out) = 0;
#ifndef KERNEL
virtual String class_name() const = 0;
#endif
protected:
virtual ~Cipher() = default;

View file

@ -6,11 +6,14 @@
#pragma once
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <LibCrypto/Cipher/Mode/Mode.h>
#ifndef KERNEL
# include <AK/String.h>
#endif
namespace Crypto {
namespace Cipher {
@ -26,6 +29,7 @@ public:
{
}
#ifndef KERNEL
virtual String class_name() const override
{
StringBuilder builder;
@ -33,8 +37,12 @@ public:
builder.append("_CBC");
return builder.build();
}
#endif
virtual size_t IV_length() const override { return IVSizeInBits / 8; }
virtual size_t IV_length() const override
{
return IVSizeInBits / 8;
}
virtual void encrypt(ReadonlyBytes in, Bytes& out, ReadonlyBytes ivec = {}, Bytes* ivec_out = nullptr) override
{

View file

@ -6,11 +6,14 @@
#pragma once
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <LibCrypto/Cipher/Mode/Mode.h>
#ifndef KERNEL
# include <AK/String.h>
#endif
namespace Crypto {
namespace Cipher {
@ -101,6 +104,7 @@ public:
{
}
#ifndef KERNEL
virtual String class_name() const override
{
StringBuilder builder;
@ -108,8 +112,12 @@ public:
builder.append("_CTR");
return builder.build();
}
#endif
virtual size_t IV_length() const override { return IVSizeInBits / 8; }
virtual size_t IV_length() const override
{
return IVSizeInBits / 8;
}
virtual void encrypt(ReadonlyBytes in, Bytes& out, ReadonlyBytes ivec = {}, Bytes* ivec_out = nullptr) override
{

View file

@ -7,13 +7,16 @@
#pragma once
#include <AK/OwnPtr.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <LibCrypto/Authentication/GHash.h>
#include <LibCrypto/Cipher/Mode/CTR.h>
#include <LibCrypto/Verification.h>
#ifndef KERNEL
# include <AK/String.h>
#endif
namespace Crypto {
namespace Cipher {
@ -40,6 +43,7 @@ public:
m_ghash = Authentication::GHash(m_auth_key);
}
#ifndef KERNEL
virtual String class_name() const override
{
StringBuilder builder;
@ -47,8 +51,12 @@ public:
builder.append("_GCM");
return builder.build();
}
#endif
virtual size_t IV_length() const override { return IVSizeInBits / 8; }
virtual size_t IV_length() const override
{
return IVSizeInBits / 8;
}
// FIXME: This overload throws away the auth stuff, think up a better way to return more than a single bytebuffer.
virtual void encrypt(ReadonlyBytes in, Bytes& out, ReadonlyBytes ivec = {}, Bytes* = nullptr) override

View file

@ -35,8 +35,14 @@ public:
return ByteBuffer::create_uninitialized(input_size + T::block_size() - remainder);
}
#ifndef KERNEL
virtual String class_name() const = 0;
T& cipher() { return m_cipher; }
#endif
T& cipher()
{
return m_cipher;
}
protected:
virtual void prune_padding(Bytes& data)

View file

@ -51,7 +51,9 @@ public:
virtual void reset() = 0;
#ifndef KERNEL
virtual String class_name() const = 0;
#endif
protected:
virtual ~HashFunction() = default;

View file

@ -191,12 +191,14 @@ public:
[&](auto& hash) { hash.reset(); });
}
#ifndef KERNEL
virtual String class_name() const override
{
return m_algorithm.visit(
[&](const Empty&) -> String { return "UninitializedHashManager"; },
[&](const auto& hash) { return hash.class_name(); });
}
#endif
inline bool is(HashKind kind) const
{

View file

@ -6,10 +6,13 @@
#pragma once
#include <AK/String.h>
#include <AK/Types.h>
#include <LibCrypto/Hash/HashFunction.h>
#ifndef KERNEL
# include <AK/String.h>
#endif
namespace Crypto {
namespace Hash {
@ -53,7 +56,12 @@ public:
virtual DigestType digest() override;
virtual DigestType peek() override;
virtual String class_name() const override { return "MD5"; }
#ifndef KERNEL
virtual String class_name() const override
{
return "MD5";
}
#endif
inline static DigestType hash(const u8* data, size_t length)
{

View file

@ -6,9 +6,12 @@
#pragma once
#include <AK/String.h>
#include <LibCrypto/Hash/HashFunction.h>
#ifndef KERNEL
# include <AK/String.h>
#endif
namespace Crypto {
namespace Hash {
@ -49,10 +52,13 @@ public:
inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
inline static DigestType hash(StringView buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
#ifndef KERNEL
virtual String class_name() const override
{
return "SHA1";
}
#endif
inline virtual void reset() override
{
m_data_length = 0;

View file

@ -6,10 +6,13 @@
#pragma once
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibCrypto/Hash/HashFunction.h>
#ifndef KERNEL
# include <AK/String.h>
#endif
namespace Crypto {
namespace Hash {
@ -97,10 +100,12 @@ public:
inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
inline static DigestType hash(StringView buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
#ifndef KERNEL
virtual String class_name() const override
{
return String::formatted("SHA{}", DigestSize * 8);
}
#endif
inline virtual void reset() override
{
@ -147,10 +152,12 @@ public:
inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
inline static DigestType hash(StringView buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
#ifndef KERNEL
virtual String class_name() const override
{
return String::formatted("SHA{}", DigestSize * 8);
}
#endif
inline virtual void reset() override
{
@ -197,10 +204,12 @@ public:
inline static DigestType hash(const ByteBuffer& buffer) { return hash(buffer.data(), buffer.size()); }
inline static DigestType hash(StringView buffer) { return hash((const u8*)buffer.characters_without_null_termination(), buffer.length()); }
#ifndef KERNEL
virtual String class_name() const override
{
return String::formatted("SHA{}", DigestSize * 8);
}
#endif
inline virtual void reset() override
{

View file

@ -7,7 +7,10 @@
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/String.h>
#ifndef KERNEL
# include <AK/String.h>
#endif
namespace Crypto {
namespace PK {
@ -33,7 +36,9 @@ public:
virtual void sign(ReadonlyBytes in, Bytes& out) = 0;
virtual void verify(ReadonlyBytes in, Bytes& out) = 0;
#ifndef KERNEL
virtual String class_name() const = 0;
#endif
virtual size_t output_size() const = 0;

View file

@ -160,9 +160,17 @@ public:
virtual void sign(ReadonlyBytes in, Bytes& out) override;
virtual void verify(ReadonlyBytes in, Bytes& out) override;
virtual String class_name() const override { return "RSA"; }
#ifndef KERNEL
virtual String class_name() const override
{
return "RSA";
}
#endif
virtual size_t output_size() const override { return m_public_key.length(); }
virtual size_t output_size() const override
{
return m_public_key.length();
}
void import_public_key(ReadonlyBytes, bool pem = true);
void import_private_key(ReadonlyBytes, bool pem = true);
@ -204,8 +212,16 @@ public:
virtual void sign(ReadonlyBytes, Bytes&) override;
virtual void verify(ReadonlyBytes, Bytes&) override;
virtual String class_name() const override { return "RSA_PKCS1-EME"; }
virtual size_t output_size() const override { return m_public_key.length(); }
#ifndef KERNEL
virtual String class_name() const override
{
return "RSA_PKCS1-EME";
}
#endif
virtual size_t output_size() const override
{
return m_public_key.length();
}
};
}
}