mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-09 01:29:17 +00:00
LibWeb: Implement X25519.generateKey
This commit is contained in:
parent
8c5e5adc8a
commit
944f00c489
Notes:
github-actions[bot]
2024-10-31 08:53:45 +00:00
Author: https://github.com/stelar7
Commit: 944f00c489
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1983
Reviewed-by: https://github.com/BenWiederhake
3 changed files with 74 additions and 1 deletions
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
|
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
|
||||||
|
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -2394,4 +2395,73 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> X25519::derive_bits(Algor
|
||||||
return JS::ArrayBuffer::create(realm, move(result));
|
return JS::ArrayBuffer::create(realm, move(result));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> X25519::generate_key([[maybe_unused]] AlgorithmParams const& params, bool extractable, Vector<Bindings::KeyUsage> const& key_usages)
|
||||||
|
{
|
||||||
|
// 1. If usages contains an entry which is not "deriveKey" or "deriveBits" then throw a SyntaxError.
|
||||||
|
for (auto const& usage : key_usages) {
|
||||||
|
if (usage != Bindings::KeyUsage::Derivekey && usage != Bindings::KeyUsage::Derivebits) {
|
||||||
|
return WebIDL::SyntaxError::create(m_realm, MUST(String::formatted("Invalid key usage '{}'", idl_enum_to_string(usage))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Generate an X25519 key pair, with the private key being 32 random bytes,
|
||||||
|
// and the public key being X25519(a, 9), as defined in [RFC7748], section 6.1.
|
||||||
|
::Crypto::Curves::X25519 curve;
|
||||||
|
auto maybe_private_key = curve.generate_private_key();
|
||||||
|
if (maybe_private_key.is_error())
|
||||||
|
return WebIDL::OperationError::create(m_realm, "Failed to generate private key"_string);
|
||||||
|
|
||||||
|
auto private_key_data = maybe_private_key.release_value();
|
||||||
|
|
||||||
|
auto maybe_public_key = curve.generate_public_key(private_key_data);
|
||||||
|
if (maybe_public_key.is_error())
|
||||||
|
return WebIDL::OperationError::create(m_realm, "Failed to generate public key"_string);
|
||||||
|
|
||||||
|
auto public_key_data = maybe_public_key.release_value();
|
||||||
|
|
||||||
|
// 3. Let algorithm be a new KeyAlgorithm object.
|
||||||
|
auto algorithm = KeyAlgorithm::create(m_realm);
|
||||||
|
|
||||||
|
// 4. Set the name attribute of algorithm to "X25519".
|
||||||
|
algorithm->set_name("X25519"_string);
|
||||||
|
|
||||||
|
// 5. Let publicKey be a new CryptoKey associated with the relevant global object of this [HTML],
|
||||||
|
// and representing the public key of the generated key pair.
|
||||||
|
auto public_key = CryptoKey::create(m_realm, CryptoKey::InternalKeyData { public_key_data });
|
||||||
|
|
||||||
|
// 6. Set the [[type]] internal slot of publicKey to "public"
|
||||||
|
public_key->set_type(Bindings::KeyType::Public);
|
||||||
|
|
||||||
|
// 7. Set the [[algorithm]] internal slot of publicKey to algorithm.
|
||||||
|
public_key->set_algorithm(algorithm);
|
||||||
|
|
||||||
|
// 8. Set the [[extractable]] internal slot of publicKey to true.
|
||||||
|
public_key->set_extractable(true);
|
||||||
|
|
||||||
|
// 9. Set the [[usages]] internal slot of publicKey to be the empty list.
|
||||||
|
public_key->set_usages({});
|
||||||
|
|
||||||
|
// 10. Let privateKey be a new CryptoKey associated with the relevant global object of this [HTML],
|
||||||
|
// and representing the private key of the generated key pair.
|
||||||
|
auto private_key = CryptoKey::create(m_realm, CryptoKey::InternalKeyData { private_key_data });
|
||||||
|
|
||||||
|
// 11. Set the [[type]] internal slot of privateKey to "private"
|
||||||
|
private_key->set_type(Bindings::KeyType::Private);
|
||||||
|
|
||||||
|
// 12. Set the [[algorithm]] internal slot of privateKey to algorithm.
|
||||||
|
private_key->set_algorithm(algorithm);
|
||||||
|
|
||||||
|
// 13. Set the [[extractable]] internal slot of privateKey to extractable.
|
||||||
|
private_key->set_extractable(extractable);
|
||||||
|
|
||||||
|
// 14. Set the [[usages]] internal slot of privateKey to be the usage intersection of usages and [ "deriveKey", "deriveBits" ].
|
||||||
|
private_key->set_usages(usage_intersection(key_usages, { { Bindings::KeyUsage::Derivekey, Bindings::KeyUsage::Derivebits } }));
|
||||||
|
|
||||||
|
// 15. Let result be a new CryptoKeyPair dictionary.
|
||||||
|
// 16. Set the publicKey attribute of result to be publicKey.
|
||||||
|
// 17. Set the privateKey attribute of result to be privateKey.
|
||||||
|
// 18. Return the result of converting result to an ECMAScript Object, as defined by [WebIDL].
|
||||||
|
return Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>> { CryptoKeyPair::create(m_realm, public_key, private_key) };
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
|
* Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
|
||||||
|
* Copyright (c) 2024, stelar7 <dudedbz@gmail.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -439,6 +440,7 @@ private:
|
||||||
class X25519 : public AlgorithmMethods {
|
class X25519 : public AlgorithmMethods {
|
||||||
public:
|
public:
|
||||||
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>) override;
|
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> derive_bits(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, Optional<u32>) override;
|
||||||
|
virtual WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> generate_key(AlgorithmParams const&, bool, Vector<Bindings::KeyUsage> const&) override;
|
||||||
|
|
||||||
static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new X25519(realm)); }
|
static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new X25519(realm)); }
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
||||||
* Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
|
* Copyright (c) 2023-2024, stelar7 <dudedbz@gmail.com>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -813,6 +813,7 @@ SupportedAlgorithmsMap supported_algorithms()
|
||||||
|
|
||||||
// https://wicg.github.io/webcrypto-secure-curves/#x25519
|
// https://wicg.github.io/webcrypto-secure-curves/#x25519
|
||||||
define_an_algorithm<X25519, EcdhKeyDerivePrams>("deriveBits"_string, "X25519"_string);
|
define_an_algorithm<X25519, EcdhKeyDerivePrams>("deriveBits"_string, "X25519"_string);
|
||||||
|
define_an_algorithm<X25519>("generateKey"_string, "X25519"_string);
|
||||||
|
|
||||||
return internal_object;
|
return internal_object;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue