-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
devgianlu
wants to merge
4
commits into
LadybirdBrowser:master
Choose a base branch
from
devgianlu:ed448
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,769
−8
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9aa8b31
LibCrypto: Add useful macros and classes for working with OpenSSL
devgianlu 89dfa98
LibCrypto: Implement Ed448
devgianlu c5a8930
LibCrypto: Add Ed448 OID to know algorithm identifiers
devgianlu 6d77b02
LibWeb: Add Ed448 support in WebCryptoAPI
devgianlu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = {}); | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
}; | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andMUST(...)
any fallible calls it makes IMO.There was a problem hiding this comment.
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.