mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 17:10:23 +00:00
LibWeb: Implement AES-GCM.generateKey
This commit is contained in:
parent
196d99352a
commit
171af8de33
Notes:
github-actions[bot]
2024-10-31 22:34:48 +00:00
Author: https://github.com/stelar7 Commit: https://github.com/LadybirdBrowser/ladybird/commit/171af8de333 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2085
3 changed files with 50 additions and 0 deletions
|
@ -2107,6 +2107,54 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> AesGcm::decrypt(Algorithm
|
||||||
return JS::ArrayBuffer::create(m_realm, plaintext);
|
return JS::ArrayBuffer::create(m_realm, plaintext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
WebIDL::ExceptionOr<Variant<JS::NonnullGCPtr<CryptoKey>, JS::NonnullGCPtr<CryptoKeyPair>>> AesGcm::generate_key(AlgorithmParams const& params, bool extractable, Vector<Bindings::KeyUsage> const& key_usages)
|
||||||
|
{
|
||||||
|
// 1. If usages contains any entry which is not one of "encrypt", "decrypt", "wrapKey" or "unwrapKey", then throw a SyntaxError.
|
||||||
|
for (auto const& usage : key_usages) {
|
||||||
|
if (usage != Bindings::KeyUsage::Encrypt && usage != Bindings::KeyUsage::Decrypt && usage != Bindings::KeyUsage::Wrapkey && usage != Bindings::KeyUsage::Unwrapkey) {
|
||||||
|
return WebIDL::SyntaxError::create(m_realm, MUST(String::formatted("Invalid key usage '{}'", idl_enum_to_string(usage))));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.
|
||||||
|
auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);
|
||||||
|
auto length = normalized_algorithm.length;
|
||||||
|
if (length != 128 && length != 192 && length != 256) {
|
||||||
|
return WebIDL::OperationError::create(m_realm, MUST(String::formatted("Cannot create AES-CTR key with unusual amount of {} bits", length)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.
|
||||||
|
// 4. If the key generation step fails, then throw an OperationError.
|
||||||
|
auto key_buffer = TRY(generate_aes_key(m_realm->vm(), length / 8));
|
||||||
|
|
||||||
|
// 5. Let key be a new CryptoKey object representing the generated AES key.
|
||||||
|
auto key = CryptoKey::create(m_realm, CryptoKey::InternalKeyData { key_buffer });
|
||||||
|
|
||||||
|
// 6. Let algorithm be a new AesKeyAlgorithm.
|
||||||
|
auto algorithm = AesKeyAlgorithm::create(m_realm);
|
||||||
|
|
||||||
|
// 7. Set the name attribute of algorithm to "AES-GCM".
|
||||||
|
algorithm->set_name("AES-GCM"_string);
|
||||||
|
|
||||||
|
// 8. Set the length attribute of algorithm to equal the length member of normalizedAlgorithm.
|
||||||
|
algorithm->set_length(length);
|
||||||
|
|
||||||
|
// 9. Set the [[type]] internal slot of key to "secret".
|
||||||
|
key->set_type(Bindings::KeyType::Secret);
|
||||||
|
|
||||||
|
// 10. Set the [[algorithm]] internal slot of key to algorithm.
|
||||||
|
key->set_algorithm(algorithm);
|
||||||
|
|
||||||
|
// 11. Set the [[extractable]] internal slot of key to be extractable.
|
||||||
|
key->set_extractable(extractable);
|
||||||
|
|
||||||
|
// 12. Set the [[usages]] internal slot of key to be usages.
|
||||||
|
key->set_usages(key_usages);
|
||||||
|
|
||||||
|
// 13. Return key.
|
||||||
|
return { key };
|
||||||
|
}
|
||||||
|
|
||||||
// https://w3c.github.io/webcrypto/#hkdf-operations
|
// https://w3c.github.io/webcrypto/#hkdf-operations
|
||||||
WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> HKDF::import_key(AlgorithmParams const&, Bindings::KeyFormat format, CryptoKey::InternalKeyData key_data, bool extractable, Vector<Bindings::KeyUsage> const& key_usages)
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> HKDF::import_key(AlgorithmParams const&, Bindings::KeyFormat format, CryptoKey::InternalKeyData key_data, bool extractable, Vector<Bindings::KeyUsage> const& key_usages)
|
||||||
{
|
{
|
||||||
|
|
|
@ -387,6 +387,7 @@ public:
|
||||||
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
|
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::Object>> export_key(Bindings::KeyFormat, JS::NonnullGCPtr<CryptoKey>) override;
|
||||||
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
|
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> encrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
|
||||||
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) override;
|
virtual WebIDL::ExceptionOr<JS::NonnullGCPtr<JS::ArrayBuffer>> decrypt(AlgorithmParams const&, JS::NonnullGCPtr<CryptoKey>, ByteBuffer const&) 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 AesGcm(realm)); }
|
static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AesGcm(realm)); }
|
||||||
|
|
||||||
|
|
|
@ -788,6 +788,7 @@ SupportedAlgorithmsMap supported_algorithms()
|
||||||
define_an_algorithm<AesGcm, AesDerivedKeyParams>("get key length"_string, "AES-GCM"_string);
|
define_an_algorithm<AesGcm, AesDerivedKeyParams>("get key length"_string, "AES-GCM"_string);
|
||||||
define_an_algorithm<AesGcm>("importKey"_string, "AES-GCM"_string);
|
define_an_algorithm<AesGcm>("importKey"_string, "AES-GCM"_string);
|
||||||
define_an_algorithm<AesGcm>("exportKey"_string, "AES-GCM"_string);
|
define_an_algorithm<AesGcm>("exportKey"_string, "AES-GCM"_string);
|
||||||
|
define_an_algorithm<AesGcm, AesKeyGenParams>("generateKey"_string, "AES-GCM"_string);
|
||||||
define_an_algorithm<AesGcm, AesGcmParams>("encrypt"_string, "AES-GCM"_string);
|
define_an_algorithm<AesGcm, AesGcmParams>("encrypt"_string, "AES-GCM"_string);
|
||||||
define_an_algorithm<AesGcm, AesGcmParams>("decrypt"_string, "AES-GCM"_string);
|
define_an_algorithm<AesGcm, AesGcmParams>("decrypt"_string, "AES-GCM"_string);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue