Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LibWeb+LibCrypto: Add Ed448 support in WebCryptoAPI #3009

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Libraries/LibCrypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ set(SOURCES
Cipher/ChaCha20.cpp
Curves/Curve25519.cpp
Curves/Ed25519.cpp
Curves/Ed448.cpp
Curves/X25519.cpp
Curves/X448.cpp
NumberTheory/ModularFunctions.cpp
Expand Down
3 changes: 2 additions & 1 deletion Libraries/LibCrypto/Certificate/Certificate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ static ErrorOr<AlgorithmIdentifier> parse_algorithm_identifier(Crypto::ASN1::Dec
READ_OBJECT(ObjectIdentifier, Vector<int>, algorithm);
POP_SCOPE();

constexpr static Array<Span<int const>, 12> known_algorithm_identifiers {
constexpr static Array<Span<int const>, 13> known_algorithm_identifiers {
ASN1::rsa_encryption_oid,
ASN1::rsa_md5_encryption_oid,
ASN1::rsa_sha1_encryption_oid,
Expand All @@ -95,6 +95,7 @@ static ErrorOr<AlgorithmIdentifier> parse_algorithm_identifier(Crypto::ASN1::Dec
ASN1::x25519_oid,
ASN1::ed25519_oid,
ASN1::x448_oid,
ASN1::ed448_oid,
};

bool is_known_algorithm = false;
Expand Down
82 changes: 82 additions & 0 deletions Libraries/LibCrypto/Curves/Ed448.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2024, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/ScopeGuard.h>
#include <LibCrypto/Curves/Ed448.h>
#include <LibCrypto/OpenSSL.h>

#include <openssl/core_names.h>
#include <openssl/evp.h>

namespace Crypto::Curves {

ErrorOr<ByteBuffer> Ed448::generate_private_key()
Copy link
Contributor

@rmg-x rmg-x Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-maintainer take: I think we should avoid fallible returns for things that most likely won't fail. Only because if they do fail, it's either us using the API incorrectly or something completely out of our control like a memory allocation failure. Failing fast in those scenarios seems better than bubbling it up to callers.

Slight bonus is that call sites of these functions become a little less noisy. Anyway, this could just return ByteBuffer and MUST(...) any fallible calls it makes IMO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I am of the opposite idea: since error handling comes at a little cost we should try to cover every case to preserve uniformity and future proofing.

{
auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_Q_keygen(nullptr, nullptr, "ED448")));

size_t key_size = EVP_PKEY_get_size(key.ptr());
auto buf = TRY(ByteBuffer::create_uninitialized(key_size));

OPENSSL_TRY(EVP_PKEY_get_raw_private_key(key.ptr(), buf.data(), &key_size));

return buf.slice(0, key_size);
}

ErrorOr<ByteBuffer> Ed448::generate_public_key(ReadonlyBytes private_key)
{
auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_new_raw_private_key(EVP_PKEY_ED448, nullptr, private_key.data(), private_key.size())));

size_t key_size = EVP_PKEY_get_size(key.ptr());
auto buf = TRY(ByteBuffer::create_uninitialized(key_size));

OPENSSL_TRY(EVP_PKEY_get_raw_public_key(key.ptr(), buf.data(), &key_size));

return buf.slice(0, key_size);
}

ErrorOr<ByteBuffer> Ed448::sign(ReadonlyBytes private_key, ReadonlyBytes message, ReadonlyBytes context)
{
auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_new_raw_private_key_ex(nullptr, "ED448", nullptr, private_key.data(), private_key.size())));

auto ctx = TRY(OpenSSL_MD_CTX::create());

OSSL_PARAM params[] = {
OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, const_cast<u8*>(context.data()), context.size()),
OSSL_PARAM_END
};

OPENSSL_TRY(EVP_DigestSignInit_ex(ctx.ptr(), nullptr, nullptr, nullptr, nullptr, key.ptr(), params));

size_t sig_len = signature_size();
auto sig = TRY(ByteBuffer::create_uninitialized(sig_len));

OPENSSL_TRY(EVP_DigestSign(ctx.ptr(), sig.data(), &sig_len, message.data(), message.size()));

return sig.slice(0, sig_len);
}

ErrorOr<bool> Ed448::verify(ReadonlyBytes public_key, ReadonlyBytes signature, ReadonlyBytes message, ReadonlyBytes context)
{
auto key = TRY(OpenSSL_PKEY::wrap(EVP_PKEY_new_raw_public_key_ex(nullptr, "ED448", nullptr, public_key.data(), public_key.size())));

auto ctx = TRY(OpenSSL_MD_CTX::create());

OSSL_PARAM params[] = {
OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_CONTEXT_STRING, const_cast<u8*>(context.data()), context.size()),
OSSL_PARAM_END
};

OPENSSL_TRY(EVP_DigestVerifyInit_ex(ctx.ptr(), nullptr, nullptr, nullptr, nullptr, key.ptr(), params));

auto res = EVP_DigestVerify(ctx.ptr(), signature.data(), signature.size(), message.data(), message.size());
if (res == 1)
return true;
if (res == 0)
return false;
OPENSSL_TRY(res);
VERIFY_NOT_REACHED();
}
}
24 changes: 24 additions & 0 deletions Libraries/LibCrypto/Curves/Ed448.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/ByteBuffer.h>

namespace Crypto::Curves {

class Ed448 {
public:
constexpr size_t key_size() const { return 57; }
constexpr size_t signature_size() const { return 114; }
ErrorOr<ByteBuffer> generate_private_key();
ErrorOr<ByteBuffer> generate_public_key(ReadonlyBytes private_key);

ErrorOr<ByteBuffer> sign(ReadonlyBytes private_key, ReadonlyBytes message, ReadonlyBytes context = {});
ErrorOr<bool> verify(ReadonlyBytes public_key, ReadonlyBytes signature, ReadonlyBytes message, ReadonlyBytes context = {});
};

}
139 changes: 139 additions & 0 deletions Libraries/LibCrypto/OpenSSL.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2024, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/Error.h>
#include <AK/Format.h>

#include <openssl/err.h>
#include <openssl/evp.h>

namespace Crypto {

#define OPENSSL_TRY_PTR(...) \
({ \
auto* _temporary_result = (__VA_ARGS__); \
if (!_temporary_result) [[unlikely]] { \
auto err = ERR_get_error(); \
VERIFY(err); \
auto* err_message = ERR_error_string(err, nullptr); \
dbgln("OpenSSL error: {}", err_message); \
return Error::from_string_literal(#__VA_ARGS__ " failed"); \
} \
_temporary_result; \
})

#define OPENSSL_TRY(...) \
({ \
auto _temporary_result = (__VA_ARGS__); \
if (_temporary_result != 1) [[unlikely]] { \
auto err = ERR_get_error(); \
VERIFY(err); \
auto* err_message = ERR_error_string(err, nullptr); \
dbgln("OpenSSL error: {}", err_message); \
return Error::from_string_literal(#__VA_ARGS__ " failed"); \
} \
_temporary_result; \
})

class OpenSSL_PKEY {
AK_MAKE_NONCOPYABLE(OpenSSL_PKEY);

public:
static ErrorOr<OpenSSL_PKEY> wrap(EVP_PKEY* ptr)
{
return OpenSSL_PKEY(OPENSSL_TRY_PTR(ptr));
}

static ErrorOr<OpenSSL_PKEY> create()
{
return OpenSSL_PKEY(OPENSSL_TRY_PTR(EVP_PKEY_new()));
}

~OpenSSL_PKEY()
{
EVP_PKEY_free(m_ptr);
}

OpenSSL_PKEY(OpenSSL_PKEY&& other)
: m_ptr(other.leak_ptr())
{
}

OpenSSL_PKEY& operator=(OpenSSL_PKEY&& other)
{
OpenSSL_PKEY ptr(move(other));
swap(m_ptr, ptr.m_ptr);
return *this;
}

EVP_PKEY const* ptr() const { return m_ptr; }
EVP_PKEY* ptr() { return m_ptr; }

private:
[[nodiscard]] EVP_PKEY* leak_ptr()
{
return exchange(m_ptr, nullptr);
}

explicit OpenSSL_PKEY(EVP_PKEY* ptr)
: m_ptr(ptr)
{
}

EVP_PKEY* m_ptr { nullptr };
};

class OpenSSL_MD_CTX {
AK_MAKE_NONCOPYABLE(OpenSSL_MD_CTX);

public:
static ErrorOr<OpenSSL_MD_CTX> wrap(EVP_MD_CTX* ptr)
{
return OpenSSL_MD_CTX(OPENSSL_TRY_PTR(ptr));
}

static ErrorOr<OpenSSL_MD_CTX> create()
{
return OpenSSL_MD_CTX(OPENSSL_TRY_PTR(EVP_MD_CTX_new()));
}

OpenSSL_MD_CTX(OpenSSL_MD_CTX&& other)
: m_ptr(other.leak_ptr())
{
}

OpenSSL_MD_CTX& operator=(OpenSSL_MD_CTX&& other)
{
OpenSSL_MD_CTX ptr(move(other));
swap(m_ptr, ptr.m_ptr);
return *this;
}

~OpenSSL_MD_CTX()
{
EVP_MD_CTX_free(m_ptr);
}

EVP_MD_CTX const* ptr() const { return m_ptr; }
EVP_MD_CTX* ptr() { return m_ptr; }

private:
[[nodiscard]] EVP_MD_CTX* leak_ptr()
{
return exchange(m_ptr, nullptr);
}

explicit OpenSSL_MD_CTX(EVP_MD_CTX* ptr)
: m_ptr(ptr)
{
}

EVP_MD_CTX* m_ptr { nullptr };
};

}
Loading
Loading