mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 10:01:13 +09:00
LibWeb: Implement ED25519 sign for SubtleCrypto
This commit is contained in:
parent
ec015034bd
commit
9ad10566b2
Notes:
sideshowbarker
2024-07-17 12:02:22 +09:00
Author: https://github.com/stelar7
Commit: 9ad10566b2
Pull-request: https://github.com/SerenityOS/serenity/pull/23737
Reviewed-by: https://github.com/ADKaster ✅
Reviewed-by: https://github.com/trflynn89
5 changed files with 46 additions and 14 deletions
|
@ -1278,4 +1278,38 @@ WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<Crypto
|
|||
return Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>> { CryptoKeyPair::create(m_realm, public_key, private_key) };
|
||||
}
|
||||
|
||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> ED25519::sign([[maybe_unused]] AlgorithmParams const& params, JS::NonnullGCPtr<CryptoKey> key, ByteBuffer const& message)
|
||||
{
|
||||
auto& realm = m_realm;
|
||||
auto& vm = realm.vm();
|
||||
|
||||
// 1. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.
|
||||
if (key->type() != Bindings::KeyType::Private)
|
||||
return WebIDL::InvalidAccessError::create(realm, "Key is not a private key"_fly_string);
|
||||
|
||||
// 2. Perform the Ed25519 signing process, as specified in [RFC8032], Section 5.1.6,
|
||||
// with message as M, using the Ed25519 private key associated with key.
|
||||
auto private_key = key->handle().visit(
|
||||
[](ByteBuffer data) -> ByteBuffer {
|
||||
return data;
|
||||
},
|
||||
[](auto) -> ByteBuffer { VERIFY_NOT_REACHED(); });
|
||||
|
||||
::Crypto::Curves::Ed25519 curve;
|
||||
auto maybe_public_key = curve.generate_public_key(private_key);
|
||||
if (maybe_public_key.is_error())
|
||||
return WebIDL::OperationError::create(realm, "Failed to generate public key"_fly_string);
|
||||
auto public_key = maybe_public_key.release_value();
|
||||
|
||||
auto maybe_signature = curve.sign(public_key, private_key, message);
|
||||
if (maybe_signature.is_error())
|
||||
return WebIDL::OperationError::create(realm, "Failed to sign message"_fly_string);
|
||||
auto signature = maybe_signature.release_value();
|
||||
|
||||
// 3. Return a new ArrayBuffer associated with the relevant global object of this [HTML],
|
||||
// and containing the bytes of the signature resulting from performing the Ed25519 signing process.
|
||||
auto result = TRY_OR_THROW_OOM(vm, ByteBuffer::copy(signature));
|
||||
return JS::ArrayBuffer::create(realm, move(result));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue