Truly independent web browser
LibCrypto+LibWeb: Check WebCrypto parameters in methods of their own
Jelle Raaijmakers88052cf4d2a3
diff --git a/Libraries/LibCrypto/Authentication/KMAC.cpp b/Libraries/LibCrypto/Authentication/KMAC.cppindex ea20ff36c27..d0dea617420 100644--- a/Libraries/LibCrypto/Authentication/KMAC.cpp+++ b/Libraries/LibCrypto/Authentication/KMAC.cpp@@ -28,15 +28,32 @@ KMAC::KMAC(KMACKind kind){}-// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-sign-ErrorOr<ByteBuffer> KMAC::sign(ReadonlyBytes key, ReadonlyBytes message, u32 output_length_bits, Optional<ReadonlyBytes> customization) const+ErrorOr<void> KMAC::validate_parameters(u32 output_length_bits, Optional<ReadonlyBytes> customization){+ // OpenSSL's KMAC implementation rejects longer outputs and customization strings.+ static constexpr size_t maximum_output_length = 0xFFFFFF / 8;+ static constexpr size_t maximum_customization_length = 512;+if (output_length_bits == 0)return Error::from_string_literal("KMAC output length must be greater than zero");if ((output_length_bits % 8) != 0)return Error::from_string_literal("KMAC output length must be a multiple of 8");+ if (output_length_bits / 8 > maximum_output_length)+ return Error::from_string_literal("KMAC output length is too large");++ if (customization.has_value() && customization->size() > maximum_customization_length)+ return Error::from_string_literal("KMAC customization is too long");++ return {};+}++// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-sign+ErrorOr<ByteBuffer> KMAC::sign(ReadonlyBytes key, ReadonlyBytes message, u32 output_length_bits, Optional<ReadonlyBytes> customization) const+{+ TRY(validate_parameters(output_length_bits, customization));+auto const* mac_name = (m_kind == KMACKind::KMAC128) ? OSSL_MAC_NAME_KMAC128 : OSSL_MAC_NAME_KMAC256;auto mac = TRY(OpenSSL_MAC::wrap(EVP_MAC_fetch(nullptr, mac_name, nullptr)));diff --git a/Libraries/LibCrypto/Authentication/KMAC.h b/Libraries/LibCrypto/Authentication/KMAC.hindex ccfd4351956..ce29fe0e18b 100644--- a/Libraries/LibCrypto/Authentication/KMAC.h+++ b/Libraries/LibCrypto/Authentication/KMAC.h@@ -27,6 +27,7 @@ public:~KMAC() = default;static u32 default_key_length(KMACKind);+ static ErrorOr<void> validate_parameters(u32 output_length_bits, Optional<ReadonlyBytes> customization);ErrorOr<ByteBuffer> sign(ReadonlyBytes key,diff --git a/Libraries/LibCrypto/Hash/Argon2.cpp b/Libraries/LibCrypto/Hash/Argon2.cppindex 6a04bfb76f9..ebae41d3a11 100644--- a/Libraries/LibCrypto/Hash/Argon2.cpp+++ b/Libraries/LibCrypto/Hash/Argon2.cpp@@ -40,6 +40,17 @@ Argon2::~Argon2()EVP_KDF_free(m_kdf);}+ErrorOr<void> Argon2::validate_parameters(ReadonlyBytes nonce)+{+ // OpenSSL's Argon2 implementation rejects shorter salts.+ static constexpr size_t minimum_nonce_length = 8;++ if (nonce.size() < minimum_nonce_length)+ return Error::from_string_literal("Argon2 nonce must be at least 8 bytes");++ return {};+}+ErrorOr<ByteBuffer> Argon2::derive_key(ReadonlyBytes message,ReadonlyBytes nonce,@@ -51,6 +62,8 @@ ErrorOr<ByteBuffer> Argon2::derive_key(Optional<ReadonlyBytes> associated_data,u32 tag_length) const{+ TRY(validate_parameters(nonce));+auto ctx = TRY(OpenSSL_KDF_CTX::wrap(EVP_KDF_CTX_new(m_kdf)));auto threads = min(OSSL_get_max_threads(nullptr), parallelism);diff --git a/Libraries/LibCrypto/Hash/Argon2.h b/Libraries/LibCrypto/Hash/Argon2.hindex bf88f512c63..2cea4e9b408 100644--- a/Libraries/LibCrypto/Hash/Argon2.h+++ b/Libraries/LibCrypto/Hash/Argon2.h@@ -25,6 +25,8 @@ public:~Argon2();+ static ErrorOr<void> validate_parameters(ReadonlyBytes nonce);+ErrorOr<ByteBuffer> derive_key(ReadonlyBytes message,ReadonlyBytes nonce,diff --git a/Libraries/LibCrypto/Hash/SHAKE.cpp b/Libraries/LibCrypto/Hash/SHAKE.cppindex 3fe7668a315..afd4844b24e 100644--- a/Libraries/LibCrypto/Hash/SHAKE.cpp+++ b/Libraries/LibCrypto/Hash/SHAKE.cpp@@ -16,11 +16,10 @@ SHAKE::SHAKE(SHAKEKind kind)m_md = (kind == SHAKEKind::CSHAKE128) ? EVP_shake128() : EVP_shake256();}-ErrorOr<ByteBuffer> SHAKE::digest(- ReadonlyBytes data,+ErrorOr<void> SHAKE::validate_parameters(u32 length,Optional<ReadonlyBytes> customization,- Optional<ReadonlyBytes> function_name) const+ Optional<ReadonlyBytes> function_name){bool wants_cshake = (customization.has_value() && !customization->is_empty()) || (function_name.has_value() && !function_name->is_empty());@@ -33,6 +32,17 @@ ErrorOr<ByteBuffer> SHAKE::digest(return Error::from_string_literal("SHAKE output length must be a multiple of 8 bits");}+ return {};+}++ErrorOr<ByteBuffer> SHAKE::digest(+ ReadonlyBytes data,+ u32 length,+ Optional<ReadonlyBytes> customization,+ Optional<ReadonlyBytes> function_name) const+{+ TRY(validate_parameters(length, customization, function_name));+size_t output_bytes = length / 8;auto buf = TRY(ByteBuffer::create_uninitialized(output_bytes));diff --git a/Libraries/LibCrypto/Hash/SHAKE.h b/Libraries/LibCrypto/Hash/SHAKE.hindex 14375b07554..7c94c1ff1d9 100644--- a/Libraries/LibCrypto/Hash/SHAKE.h+++ b/Libraries/LibCrypto/Hash/SHAKE.h@@ -26,6 +26,11 @@ public:~SHAKE() = default;+ static ErrorOr<void> validate_parameters(+ u32 length,+ Optional<ReadonlyBytes> customization,+ Optional<ReadonlyBytes> function_name);+ErrorOr<ByteBuffer> digest(ReadonlyBytes data,u32 length,diff --git a/Libraries/LibCrypto/PK/RSA.cpp b/Libraries/LibCrypto/PK/RSA.cppindex 06a1f5a0911..fc275fc1bbe 100644--- a/Libraries/LibCrypto/PK/RSA.cpp+++ b/Libraries/LibCrypto/PK/RSA.cpp@@ -118,8 +118,21 @@ ErrorOr<RSA::KeyPairType> RSA::parse_rsa_key(ReadonlyBytes der, bool is_private,}}+ErrorOr<void> RSA::validate_key_generation_parameters(size_t bits)+{+ // OpenSSL refuses to generate keys with shorter moduli.+ static constexpr size_t minimum_modulus_length = 512;++ if (bits < minimum_modulus_length)+ return Error::from_string_literal("RSA modulus length must be at least 512 bits");++ return {};+}+ErrorOr<RSA::KeyPairType> RSA::generate_key_pair(size_t bits, UnsignedBigInteger e){+ TRY(validate_key_generation_parameters(bits));+auto ctx = TRY(OpenSSL_PKEY_CTX::wrap(EVP_PKEY_CTX_new_from_name(nullptr, "RSA", nullptr)));OPENSSL_TRY(EVP_PKEY_keygen_init(ctx.ptr()));diff --git a/Libraries/LibCrypto/PK/RSA.h b/Libraries/LibCrypto/PK/RSA.hindex 567a0906882..ef98c9d679e 100644--- a/Libraries/LibCrypto/PK/RSA.h+++ b/Libraries/LibCrypto/PK/RSA.h@@ -138,6 +138,7 @@ public:using KeyPairType = RSAKeyPair<PublicKeyType, PrivateKeyType>;static ErrorOr<KeyPairType> parse_rsa_key(ReadonlyBytes der, bool is_private, Vector<StringView> current_scope);+ static ErrorOr<void> validate_key_generation_parameters(size_t bits);static ErrorOr<KeyPairType> generate_key_pair(size_t bits, UnsignedBigInteger e = 65537);RSA(KeyPairType const& pair)diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cppindex a00e7763e53..4922ca0f0d7 100644--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp@@ -69,6 +69,19 @@ static JS::ThrowCompletionOr<HashAlgorithmIdentifier> hash_algorithm_identifier_return HashAlgorithmIdentifier { maybe_normalized_algorithm.value().parameter->name };}+static WebIDL::ExceptionOr<::Crypto::Hash::HashKind> sha_hash_kind(Utf16String const& hash_algorithm)+{+ if (hash_algorithm == "SHA-1"sv)+ return ::Crypto::Hash::HashKind::SHA1;+ if (hash_algorithm == "SHA-256"sv)+ return ::Crypto::Hash::HashKind::SHA256;+ if (hash_algorithm == "SHA-384"sv)+ return ::Crypto::Hash::HashKind::SHA384;+ if (hash_algorithm == "SHA-512"sv)+ return ::Crypto::Hash::HashKind::SHA512;+ return WebIDL::NotSupportedError::create(Utf16String::formatted("Invalid hash function '{}'", hash_algorithm));+}+static WebIDL::ExceptionOr<ByteBuffer> export_public_exponent(JS::VM& vm, ::Crypto::UnsignedBigInteger const& exponent){auto bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::create_uninitialized(exponent.byte_length()));@@ -113,6 +126,10 @@ static WebIDL::ExceptionOr<void> validate_rsa_key_generation_parameters(RsaKeyGe|| public_exponent.plus(1).one_based_index_of_highest_set_bit() > modulus_length)return WebIDL::OperationError::create("Invalid RSA key generation parameters"_utf16);+ // NB: Generating the key pair fails for the moduli that our RSA does not support.+ if (::Crypto::PK::RSA::validate_key_generation_parameters(modulus_length).is_error())+ return WebIDL::OperationError::create("RSA modulus length must be at least 512 bits"_utf16);+return {};}@@ -966,6 +983,13 @@ WebIDL::ExceptionOr<ByteBuffer> RSAOAEP::decrypt(JS::Realm&, AlgorithmParams conreturn maybe_plaintext.release_value();}+// https://w3c.github.io/webcrypto/#rsa-oaep-operations-generate-key+WebIDL::ExceptionOr<void> RSAOAEP::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // 2. Perform the validate RSA key generation parameters algorithm with normalizedAlgorithm.+ return validate_rsa_key_generation_parameters(static_cast<RsaHashedKeyGenParams const&>(params));+}+// https://w3c.github.io/webcrypto/#rsa-oaep-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> RSAOAEP::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -1473,6 +1497,13 @@ Optional<ReadonlySpan<KeyUsage>> RSAOAEP::public_key_usages() constreturn usages.span();}+// https://w3c.github.io/webcrypto/#rsa-pss-operations-generate-key+WebIDL::ExceptionOr<void> RSAPSS::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // 2. Perform the validate RSA key generation parameters algorithm with normalizedAlgorithm.+ return validate_rsa_key_generation_parameters(static_cast<RsaHashedKeyGenParams const&>(params));+}+// https://w3c.github.io/webcrypto/#rsa-pss-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> RSAPSS::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -2060,6 +2091,13 @@ Optional<ReadonlySpan<KeyUsage>> RSAPSS::public_key_usages() constreturn usages.span();}+// https://w3c.github.io/webcrypto/#rsassa-pkcs1-operations-generate-key+WebIDL::ExceptionOr<void> RSASSAPKCS1::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // 2. Perform the validate RSA key generation parameters algorithm with normalizedAlgorithm.+ return validate_rsa_key_generation_parameters(static_cast<RsaHashedKeyGenParams const&>(params));+}+// https://w3c.github.io/webcrypto/#rsassa-pkcs1-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> RSASSAPKCS1::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -2642,7 +2680,7 @@ Optional<ReadonlySpan<KeyUsage>> RSASSAPKCS1::public_key_usages() const}// https://w3c.github.io/webcrypto/#aes-cbc-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> AesCbc::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+WebIDL::ExceptionOr<void> AesCbc::validate_encrypt_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<AesCbcParams const&>(params);@@ -2650,6 +2688,17 @@ WebIDL::ExceptionOr<ByteBuffer> AesCbc::encrypt(JS::Realm&, AlgorithmParams consif (normalized_algorithm.iv.size() != 16)return WebIDL::OperationError::create("IV to AES-CBC must be exactly 16 bytes"_utf16);+ return {};+}++// https://w3c.github.io/webcrypto/#aes-cbc-operations-encrypt+WebIDL::ExceptionOr<ByteBuffer> AesCbc::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+{+ auto const& normalized_algorithm = static_cast<AesCbcParams const&>(params);++ // NB: Step 1 is performed by validate_encrypt_parameters().+ TRY(validate_encrypt_parameters(params));+// 2. Let paddedPlaintext be the result of adding padding octets to the contents of plaintext according to the procedure defined in Section 10.3 of [RFC2315], step 2, with a value of k of 16.// 3. Let ciphertext be the result of performing the CBC Encryption operation described in Section 6.2 of [NIST-SP800-38A] using AES as the block cipher, the contents of the iv member of normalizedAlgorithm as the IV input parameter and paddedPlaintext as the input plaintext.auto key_bytes = key->handle().get<ByteBuffer>();@@ -2664,7 +2713,7 @@ WebIDL::ExceptionOr<ByteBuffer> AesCbc::encrypt(JS::Realm&, AlgorithmParams cons}// https://w3c.github.io/webcrypto/#aes-cbc-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> AesCbc::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+WebIDL::ExceptionOr<void> AesCbc::validate_decrypt_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<AesCbcParams const&>(params);@@ -2672,6 +2721,17 @@ WebIDL::ExceptionOr<ByteBuffer> AesCbc::decrypt(JS::Realm&, AlgorithmParams consif (normalized_algorithm.iv.size() != 16)return WebIDL::OperationError::create("IV to AES-CBC must be exactly 16 bytes"_utf16);+ return {};+}++// https://w3c.github.io/webcrypto/#aes-cbc-operations-decrypt+WebIDL::ExceptionOr<ByteBuffer> AesCbc::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+{+ auto const& normalized_algorithm = static_cast<AesCbcParams const&>(params);++ // NB: Step 1 is performed by validate_decrypt_parameters().+ TRY(validate_decrypt_parameters(params));+// 2. If the length of ciphertext is zero or is not a multiple of 16 bytes, then throw an OperationError.if (ciphertext.is_empty() || ciphertext.size() % 16 != 0)return WebIDL::OperationError::create("Ciphertext length must be a multiple of 16 bytes"_utf16);@@ -2794,6 +2854,17 @@ WebIDL::ExceptionOr<GC::Ref<CryptoKey>> AesCbc::import_key(JS::Realm& realm, Algreturn key;}+// https://w3c.github.io/webcrypto/#aes-cbc-operations-generate-key+WebIDL::ExceptionOr<void> AesCbc::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ auto const bits = static_cast<AesKeyGenParams const&>(params).length;+ if (bits != 128 && bits != 192 && bits != 256)+ return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-CBC key with unusual amount of {} bits", bits));++ return {};+}+// https://w3c.github.io/webcrypto/#aes-cbc-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesCbc::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -2806,11 +2877,9 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesCbc::generateauto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);- // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ // NB: Step 2 is performed by validate_generate_key_parameters().+ TRY(validate_generate_key_parameters(params));auto const bits = normalized_algorithm.length;- if (bits != 128 && bits != 192 && bits != 256) {- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-CBC key with unusual amount of {} bits", bits));- }// 3. Generate an AES key of length equal to the length member of normalizedAlgorithm.auto key_buffer = TRY(generate_random_key(realm.vm(), bits));@@ -3122,6 +3191,17 @@ WebIDL::ExceptionOr<Optional<u32>> AesCtr::get_key_length(JS::Realm&, AlgorithmPreturn length;}+// https://w3c.github.io/webcrypto/#aes-ctr-operations-generate-key+WebIDL::ExceptionOr<void> AesCtr::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ auto const bits = static_cast<AesKeyGenParams const&>(params).length;+ if (bits != 128 && bits != 192 && bits != 256)+ return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-CTR key with unusual amount of {} bits", bits));++ return {};+}+// https://w3c.github.io/webcrypto/#aes-ctr-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesCtr::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -3132,12 +3212,10 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesCtr::generate}}- // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ // NB: Step 2 is performed by validate_generate_key_parameters().+ TRY(validate_generate_key_parameters(params));auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);auto const bits = normalized_algorithm.length;- if (bits != 128 && bits != 192 && bits != 256) {- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-CTR key with unusual amount of {} bits", bits));- }// 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.@@ -3166,19 +3244,28 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesCtr::generate}// https://w3c.github.io/webcrypto/#aes-ctr-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> AesCtr::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+WebIDL::ExceptionOr<void> AesCtr::validate_encrypt_parameters(AlgorithmParams const& params) const{- // 1. If the counter member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.auto const& normalized_algorithm = static_cast<AesCtrParams const&>(params);- auto const& counter = normalized_algorithm.counter;- if (counter.size() != 16)++ // 1. If the counter member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.+ if (normalized_algorithm.counter.size() != 16)return WebIDL::OperationError::create("Invalid counter length"_utf16);// 2. If the length member of normalizedAlgorithm is zero or is greater than 128, then throw an OperationError.- auto const& length = normalized_algorithm.length;- if (length == 0 || length > 128)+ if (normalized_algorithm.length == 0 || normalized_algorithm.length > 128)return WebIDL::OperationError::create("Invalid length"_utf16);+ return {};+}++// https://w3c.github.io/webcrypto/#aes-ctr-operations-encrypt+WebIDL::ExceptionOr<ByteBuffer> AesCtr::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+{+ // NB: Steps 1 and 2 are performed by validate_encrypt_parameters().+ TRY(validate_encrypt_parameters(params));+ auto const& counter = static_cast<AesCtrParams const&>(params).counter;+// 3. Let ciphertext be the result of performing the CTR Encryption operation described in Section 6.5 of [NIST-SP800-38A] using// AES as the block cipher,// the contents of the counter member of normalizedAlgorithm as the initial value of the counter block,@@ -3194,19 +3281,28 @@ WebIDL::ExceptionOr<ByteBuffer> AesCtr::encrypt(JS::Realm&, AlgorithmParams cons}// https://w3c.github.io/webcrypto/#aes-ctr-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> AesCtr::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+WebIDL::ExceptionOr<void> AesCtr::validate_decrypt_parameters(AlgorithmParams const& params) const{- // 1. If the counter member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.auto const& normalized_algorithm = static_cast<AesCtrParams const&>(params);- auto const& counter = normalized_algorithm.counter;- if (counter.size() != 16)++ // 1. If the counter member of normalizedAlgorithm does not have length 16 bytes, then throw an OperationError.+ if (normalized_algorithm.counter.size() != 16)return WebIDL::OperationError::create("Invalid counter length"_utf16);// 2. If the length member of normalizedAlgorithm is zero or is greater than 128, then throw an OperationError.- auto const& length = normalized_algorithm.length;- if (length == 0 || length > 128)+ if (normalized_algorithm.length == 0 || normalized_algorithm.length > 128)return WebIDL::OperationError::create("Invalid length"_utf16);+ return {};+}++// https://w3c.github.io/webcrypto/#aes-ctr-operations-decrypt+WebIDL::ExceptionOr<ByteBuffer> AesCtr::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+{+ // NB: Steps 1 and 2 are performed by validate_decrypt_parameters().+ TRY(validate_decrypt_parameters(params));+ auto const& counter = static_cast<AesCtrParams const&>(params).counter;+// 3. Let plaintext be the result of performing the CTR Decryption operation described in Section 6.5 of [NIST-SP800-38A] using// AES as the block cipher,// the contents of the counter member of normalizedAlgorithm as the initial value of the counter block,@@ -3420,10 +3516,14 @@ WebIDL::ExceptionOr<ExportKeyResult> AesGcm::export_key(JS::Realm& realm, KeyFor}// https://w3c.github.io/webcrypto/#aes-gcm-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> AesGcm::encrypt(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+WebIDL::ExceptionOr<void> AesGcm::validate_encrypt_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<AesGcmParams const&>(params);+ // NB: Our AES-GCM rejects an empty IV.+ if (normalized_algorithm.iv.is_empty())+ return WebIDL::OperationError::create("IV must not be empty"_utf16);+// 1. If the iv member of normalizedAlgorithm has a length greater than 2^64 - 1 bytes, then throw an OperationError.// NOTE: This is not possible@@ -3431,18 +3531,24 @@ WebIDL::ExceptionOr<ByteBuffer> AesGcm::encrypt(JS::Realm& realm, AlgorithmParam// NOTE: This is not possible// 3. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.- auto tag_length = 0;+ // If the tagLength member of normalizedAlgorithm is one of 32, 64, 96, 104, 112, 120 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm+ // Otherwise: throw an OperationError.auto constexpr valid_tag_lengths = Array { 32, 64, 96, 104, 112, 120, 128 };- if (!normalized_algorithm.tag_length.has_value())- tag_length = 128;+ if (normalized_algorithm.tag_length.has_value() && !valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))+ return WebIDL::OperationError::create("Invalid tag length"_utf16);- // If the tagLength member of normalizedAlgorithm is one of 32, 64, 96, 104, 112, 120 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm- else if (valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))- tag_length = normalized_algorithm.tag_length.value();+ return {};+}- // Otherwise: throw an OperationError.- else- return WebIDL::OperationError::create("Invalid tag length"_utf16);+// https://w3c.github.io/webcrypto/#aes-gcm-operations-encrypt+WebIDL::ExceptionOr<ByteBuffer> AesGcm::encrypt(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+{+ auto const& normalized_algorithm = static_cast<AesGcmParams const&>(params);++ // NB: Steps 1 to 3 are performed by validate_encrypt_parameters(), which leaves tagLength as the tagLength member+ // of normalizedAlgorithm if present, or 128 otherwise.+ TRY(validate_encrypt_parameters(params));+ auto tag_length = normalized_algorithm.tag_length.value_or(128);// 4. If plaintext has a length greater than 2^39 - 256 bytes, then throw an OperationError.if (plaintext.size() > (1ULL << 39) - 256)@@ -3473,22 +3579,19 @@ WebIDL::ExceptionOr<ByteBuffer> AesGcm::encrypt(JS::Realm& realm, AlgorithmParam}// https://w3c.github.io/webcrypto/#aes-gcm-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> AesGcm::decrypt(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+WebIDL::ExceptionOr<void> AesGcm::validate_decrypt_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<AesGcmParams const&>(params);+ // NB: Our AES-GCM rejects an empty IV.+ if (normalized_algorithm.iv.is_empty())+ return WebIDL::OperationError::create("IV must not be empty"_utf16);+// 1. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.- u32 tag_length = 0;+ // If the tagLength member of normalizedAlgorithm is one of 32, 64, 96, 104, 112, 120 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm+ // Otherwise: throw an OperationError.auto constexpr valid_tag_lengths = Array { 32, 64, 96, 104, 112, 120, 128 };- if (!normalized_algorithm.tag_length.has_value())- tag_length = 128;-- // If the tagLength member of normalizedAlgorithm is one of 32, 64, 96, 104, 112, 120 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm- else if (valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))- tag_length = normalized_algorithm.tag_length.value();-- // Otherwise: throw an OperationError.- else+ if (normalized_algorithm.tag_length.has_value() && !valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))return WebIDL::OperationError::create("Invalid tag length"_utf16);// 2. If the iv member of normalizedAlgorithm has a length greater than 2^64 - 1 bytes, then throw an OperationError.@@ -3497,6 +3600,19 @@ WebIDL::ExceptionOr<ByteBuffer> AesGcm::decrypt(JS::Realm& realm, AlgorithmParam// 3. If the additionalData member of normalizedAlgorithm is present and has a length greater than 2^64 - 1 bytes, then throw an OperationError.// NOTE: This is not possible+ return {};+}++// https://w3c.github.io/webcrypto/#aes-gcm-operations-decrypt+WebIDL::ExceptionOr<ByteBuffer> AesGcm::decrypt(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+{+ auto const& normalized_algorithm = static_cast<AesGcmParams const&>(params);++ // NB: Steps 1 to 3 are performed by validate_decrypt_parameters(), which leaves tagLength as the tagLength member+ // of normalizedAlgorithm if present, or 128 otherwise.+ TRY(validate_decrypt_parameters(params));+ u32 tag_length = normalized_algorithm.tag_length.value_or(128);+// 4. If ciphertext has a length in bits less than tagLength, then throw an OperationError.if (ciphertext.size() < tag_length / 8)return WebIDL::OperationError::create("Invalid ciphertext length"_utf16);@@ -3531,6 +3647,17 @@ WebIDL::ExceptionOr<ByteBuffer> AesGcm::decrypt(JS::Realm& realm, AlgorithmParamreturn maybe_plaintext.release_value();}+// https://w3c.github.io/webcrypto/#aes-gcm-operations-generate-key+WebIDL::ExceptionOr<void> AesGcm::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ auto const bits = static_cast<AesKeyGenParams const&>(params).length;+ if (bits != 128 && bits != 192 && bits != 256)+ return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-GCM key with unusual amount of {} bits", bits));++ return {};+}+// https://w3c.github.io/webcrypto/#aes-gcm-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesGcm::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -3541,12 +3668,10 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesGcm::generate}}- // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ // NB: Step 2 is performed by validate_generate_key_parameters().+ TRY(validate_generate_key_parameters(params));auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);auto const bits = normalized_algorithm.length;- if (bits != 128 && bits != 192 && bits != 256) {- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-GCM key with unusual amount of {} bits", bits));- }// 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.@@ -3770,6 +3895,17 @@ WebIDL::ExceptionOr<Optional<u32>> AesKw::get_key_length(JS::Realm&, AlgorithmPareturn length;}+// https://w3c.github.io/webcrypto/#aes-kw-operations-generate-key+WebIDL::ExceptionOr<void> AesKw::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // 2. If the length property of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ auto const bits = static_cast<AesKeyGenParams const&>(params).length;+ if (bits != 128 && bits != 192 && bits != 256)+ return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-KW key with unusual amount of {} bits", bits));++ return {};+}+// https://w3c.github.io/webcrypto/#aes-kw-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesKw::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -3780,12 +3916,10 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesKw::generate_}}- // 2. If the length property of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ // NB: Step 2 is performed by validate_generate_key_parameters().+ TRY(validate_generate_key_parameters(params));auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);auto const bits = normalized_algorithm.length;- if (bits != 128 && bits != 192 && bits != 256) {- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-KW key with unusual amount of {} bits", bits));- }// 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.@@ -3926,6 +4060,18 @@ WebIDL::ExceptionOr<ByteBuffer> SHA::digest(JS::Realm&, AlgorithmParams const& areturn result_buffer.release_value();}+// https://w3c.github.io/webcrypto/#ecdsa-operations-generate-key+WebIDL::ExceptionOr<void> ECDSA::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // NB: Step 2 throws a NotSupportedError when the namedCurve member of normalizedAlgorithm is not "P-256", "P-384" or+ // "P-521".+ auto const& normalized_algorithm = static_cast<EcKeyGenParams const&>(params);+ if (!normalized_algorithm.named_curve.is_one_of("P-256"_utf16, "P-384"_utf16, "P-521"_utf16))+ return WebIDL::NotSupportedError::create("Only 'P-256', 'P-384' and 'P-521' is supported"_utf16);++ return {};+}+// https://w3c.github.io/webcrypto/#ecdsa-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> ECDSA::generate_key(JS::Realm&, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -4022,6 +4168,15 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> ECDSA::generate_return Variant<GC::Ref<CryptoKey>, CryptoKeyPair> { CryptoKeyPair { public_key, private_key } };}+// https://w3c.github.io/webcrypto/#ecdsa-operations-sign+WebIDL::ExceptionOr<void> ECDSA::validate_sign_parameters(AlgorithmParams const& params) const+{+ // NB: Step 3 fails for the hash functions that our ECDSA does not support.+ TRY(sha_hash_kind(static_cast<EcdsaParams const&>(params).hash.name));++ return {};+}+// https://w3c.github.io/webcrypto/#ecdsa-operations-signWebIDL::ExceptionOr<ByteBuffer> ECDSA::sign(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& message){@@ -4036,18 +4191,7 @@ WebIDL::ExceptionOr<ByteBuffer> ECDSA::sign(JS::Realm& realm, AlgorithmParams coauto const& hash_algorithm = normalized_algorithm.hash.name;// 3. Let M be the result of performing the digest operation specified by hashAlgorithm using message.- ::Crypto::Hash::HashKind hash_kind;- if (hash_algorithm == "SHA-1"sv) {- hash_kind = ::Crypto::Hash::HashKind::SHA1;- } else if (hash_algorithm == "SHA-256"sv) {- hash_kind = ::Crypto::Hash::HashKind::SHA256;- } else if (hash_algorithm == "SHA-384"sv) {- hash_kind = ::Crypto::Hash::HashKind::SHA384;- } else if (hash_algorithm == "SHA-512"sv) {- hash_kind = ::Crypto::Hash::HashKind::SHA512;- } else {- return WebIDL::NotSupportedError::create(Utf16String::formatted("Invalid hash function '{}'", hash_algorithm));- }+ auto hash_kind = TRY(sha_hash_kind(hash_algorithm));::Crypto::Hash::Manager hash { hash_kind };hash.update(message);auto digest = hash.digest();@@ -4117,6 +4261,15 @@ WebIDL::ExceptionOr<ByteBuffer> ECDSA::sign(JS::Realm& realm, AlgorithmParams coreturn result;}+// https://w3c.github.io/webcrypto/#ecdsa-operations-verify+WebIDL::ExceptionOr<void> ECDSA::validate_verify_parameters(AlgorithmParams const& params) const+{+ // NB: Step 3 fails for the hash functions that our ECDSA does not support.+ TRY(sha_hash_kind(static_cast<EcdsaParams const&>(params).hash.name));++ return {};+}+// https://w3c.github.io/webcrypto/#ecdsa-operations-verifyWebIDL::ExceptionOr<bool> ECDSA::verify(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& signature, ByteBuffer const& message){@@ -4127,21 +4280,10 @@ WebIDL::ExceptionOr<bool> ECDSA::verify(JS::Realm& realm, AlgorithmParams const&return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);// 2. Let hashAlgorithm be the hash member of normalizedAlgorithm.- [[maybe_unused]] auto const& hash_algorithm = normalized_algorithm.hash.name;+ auto const& hash_algorithm = normalized_algorithm.hash.name;// 3. Let M be the result of performing the digest operation specified by hashAlgorithm using message.- ::Crypto::Hash::HashKind hash_kind;- if (hash_algorithm == "SHA-1"sv) {- hash_kind = ::Crypto::Hash::HashKind::SHA1;- } else if (hash_algorithm == "SHA-256"sv) {- hash_kind = ::Crypto::Hash::HashKind::SHA256;- } else if (hash_algorithm == "SHA-384"sv) {- hash_kind = ::Crypto::Hash::HashKind::SHA384;- } else if (hash_algorithm == "SHA-512"sv) {- hash_kind = ::Crypto::Hash::HashKind::SHA512;- } else {- return WebIDL::NotSupportedError::create(Utf16String::formatted("Invalid hash function '{}'", hash_algorithm));- }+ auto hash_kind = TRY(sha_hash_kind(hash_algorithm));::Crypto::Hash::Manager hash { hash_kind };hash.update(message);auto digest = hash.digest();@@ -4206,7 +4348,7 @@ WebIDL::ExceptionOr<bool> ECDSA::verify(JS::Realm& realm, AlgorithmParams const&}// https://w3c.github.io/webcrypto/#ecdsa-operations-import-key-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> ECDSA::import_key(JS::Realm& realm, AlgorithmParams const& params, KeyFormat key_format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& usages)+WebIDL::ExceptionOr<void> ECDSA::validate_import_key_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<EcKeyImportParams const&>(params);@@ -4216,6 +4358,17 @@ WebIDL::ExceptionOr<GC::Ref<CryptoKey>> ECDSA::import_key(JS::Realm& realm, Algoif (!normalized_algorithm.named_curve.is_one_of("P-256"_utf16, "P-384"_utf16, "P-521"_utf16))return WebIDL::NotSupportedError::create(Utf16String::formatted("Unsupported named curve '{}'", normalized_algorithm.named_curve));+ return {};+}++// https://w3c.github.io/webcrypto/#ecdsa-operations-import-key+WebIDL::ExceptionOr<GC::Ref<CryptoKey>> ECDSA::import_key(JS::Realm& realm, AlgorithmParams const& params, KeyFormat key_format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& usages)+{+ auto const& normalized_algorithm = static_cast<EcKeyImportParams const&>(params);++ // NB: Step 1 is performed by validate_import_key_parameters().+ TRY(validate_import_key_parameters(params));+// NOTE: This is a parameter to the function// 2. Let keyData be the key data to be imported.@@ -4984,6 +5137,18 @@ Optional<ReadonlySpan<KeyUsage>> ECDSA::public_key_usages() constreturn usages.span();}+// https://w3c.github.io/webcrypto/#ecdh-operations-generate-key+WebIDL::ExceptionOr<void> ECDH::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // NB: Step 2 throws a NotSupportedError when the namedCurve member of normalizedAlgorithm is not "P-256", "P-384" or+ // "P-521".+ auto const& normalized_algorithm = static_cast<EcKeyGenParams const&>(params);+ if (!normalized_algorithm.named_curve.is_one_of("P-256"_utf16, "P-384"_utf16, "P-521"_utf16))+ return WebIDL::NotSupportedError::create("Only 'P-256', 'P-384' and 'P-521' is supported"_utf16);++ return {};+}+// https://w3c.github.io/webcrypto/#ecdh-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> ECDH::generate_key(JS::Realm&, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -5081,7 +5246,7 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> ECDH::generate_k}// https://w3c.github.io/webcrypto/#ecdh-operations-derive-bits-WebIDL::ExceptionOr<ByteBuffer> ECDH::derive_bits(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length_optional)+WebIDL::ExceptionOr<void> ECDH::validate_derive_bits_parameters(AlgorithmParams const& params, Optional<u32> length_optional) const{auto const& normalized_algorithm = static_cast<EcdhKeyDeriveParams const&>(params);@@ -5105,6 +5270,18 @@ WebIDL::ExceptionOr<ByteBuffer> ECDH::derive_bits(JS::Realm& realm, AlgorithmParif (length_optional.has_value() && *length_optional > maximum_length)return WebIDL::OperationError::create("Length is too large"_utf16);+ return {};+}++// https://w3c.github.io/webcrypto/#ecdh-operations-derive-bits+WebIDL::ExceptionOr<ByteBuffer> ECDH::derive_bits(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length_optional)+{+ auto const& normalized_algorithm = static_cast<EcdhKeyDeriveParams const&>(params);++ // NB: Steps 1 to 5 are performed by validate_derive_bits_parameters().+ TRY(validate_derive_bits_parameters(params, length_optional));+ auto& public_key = normalized_algorithm.public_key;+// 6. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.if (key->type() != KeyType::Private)return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);@@ -5193,7 +5370,7 @@ WebIDL::ExceptionOr<ByteBuffer> ECDH::derive_bits(JS::Realm& realm, AlgorithmPar}// https://w3c.github.io/webcrypto/#ecdh-operations-import-key-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> ECDH::import_key(JS::Realm& realm, AlgorithmParams const& params, KeyFormat key_format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& usages)+WebIDL::ExceptionOr<void> ECDH::validate_import_key_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<EcKeyImportParams const&>(params);@@ -5203,6 +5380,17 @@ WebIDL::ExceptionOr<GC::Ref<CryptoKey>> ECDH::import_key(JS::Realm& realm, Algorif (!normalized_algorithm.named_curve.is_one_of("P-256"_utf16, "P-384"_utf16, "P-521"_utf16))return WebIDL::NotSupportedError::create(Utf16String::formatted("Unsupported named curve '{}'", normalized_algorithm.named_curve));+ return {};+}++// https://w3c.github.io/webcrypto/#ecdh-operations-import-key+WebIDL::ExceptionOr<GC::Ref<CryptoKey>> ECDH::import_key(JS::Realm& realm, AlgorithmParams const& params, KeyFormat key_format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& usages)+{+ auto const& normalized_algorithm = static_cast<EcKeyImportParams const&>(params);++ // NB: Step 1 is performed by validate_import_key_parameters().+ TRY(validate_import_key_parameters(params));+// NOTE: This is a parameter to the function// 2. Let keyData be the key data to be imported.@@ -6919,7 +7107,7 @@ WebIDL::ExceptionOr<bool> ED448::verify(JS::Realm&, AlgorithmParams const& param}// https://w3c.github.io/webcrypto/#hkdf-operations-derive-bits-WebIDL::ExceptionOr<ByteBuffer> HKDF::derive_bits(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length_optional)+WebIDL::ExceptionOr<void> HKDF::validate_derive_bits_parameters(AlgorithmParams const& params, Optional<u32> length_optional) const{auto const& normalized_algorithm = static_cast<HKDFParams const&>(params);@@ -6929,24 +7117,25 @@ WebIDL::ExceptionOr<ByteBuffer> HKDF::derive_bits(JS::Realm&, AlgorithmParams co// 2. Let hashLength be the length in bits of the output of the hash function identified by the hash member of// normalizedAlgorithm.- auto const& hash_algorithm = normalized_algorithm.hash.name;- auto hash_kind = TRY([&] -> WebIDL::ExceptionOr<::Crypto::Hash::HashKind> {- if (hash_algorithm == "SHA-1"sv)- return ::Crypto::Hash::HashKind::SHA1;- if (hash_algorithm == "SHA-256"sv)- return ::Crypto::Hash::HashKind::SHA256;- if (hash_algorithm == "SHA-384"sv)- return ::Crypto::Hash::HashKind::SHA384;- if (hash_algorithm == "SHA-512"sv)- return ::Crypto::Hash::HashKind::SHA512;- return WebIDL::NotSupportedError::create(Utf16String::formatted("Invalid hash function '{}'", hash_algorithm));- }());+ auto hash_kind = TRY(sha_hash_kind(normalized_algorithm.hash.name));auto hash_length = ::Crypto::Hash::Manager { hash_kind }.digest_size() * 8;// 3. If length is greater than 255 * hashLength, then throw an OperationError.if (*length_optional > 255 * hash_length)return WebIDL::OperationError::create("Length is too large for the hash function"_utf16);+ return {};+}++// https://w3c.github.io/webcrypto/#hkdf-operations-derive-bits+WebIDL::ExceptionOr<ByteBuffer> HKDF::derive_bits(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length_optional)+{+ auto const& normalized_algorithm = static_cast<HKDFParams const&>(params);++ // NB: Steps 1 to 3 are performed by validate_derive_bits_parameters().+ TRY(validate_derive_bits_parameters(params, length_optional));+ auto hash_kind = TRY(sha_hash_kind(normalized_algorithm.hash.name));+// 4. Let keyDerivationKey be the secret represented by [[handle]] internal slot of key as the message.auto key_derivation_key = key->handle().get<ByteBuffer>();@@ -6985,7 +7174,7 @@ WebIDL::ExceptionOr<Optional<u32>> HKDF::get_key_length(JS::Realm&, AlgorithmPar}// https://w3c.github.io/webcrypto/#pbkdf2-operations-derive-bits-WebIDL::ExceptionOr<ByteBuffer> PBKDF2::derive_bits(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length_optional)+WebIDL::ExceptionOr<void> PBKDF2::validate_derive_bits_parameters(AlgorithmParams const& params, Optional<u32> length_optional) const{auto const& normalized_algorithm = static_cast<PBKDF2Params const&>(params);@@ -6997,6 +7186,20 @@ WebIDL::ExceptionOr<ByteBuffer> PBKDF2::derive_bits(JS::Realm&, AlgorithmParamsif (normalized_algorithm.iterations == 0)return WebIDL::OperationError::create("Iterations must be greater than 0"_utf16);+ // NB: Step 4 fails for the hash functions that our PBKDF2 does not support.+ TRY(sha_hash_kind(normalized_algorithm.hash.name));++ return {};+}++// https://w3c.github.io/webcrypto/#pbkdf2-operations-derive-bits+WebIDL::ExceptionOr<ByteBuffer> PBKDF2::derive_bits(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length_optional)+{+ auto const& normalized_algorithm = static_cast<PBKDF2Params const&>(params);++ // NB: Steps 1 and 2 are performed by validate_derive_bits_parameters().+ TRY(validate_derive_bits_parameters(params, length_optional));+// 3. Let prf be the MAC Generation function described in Section 4 of [FIPS-198-1] using the hash function described by the hash member of normalizedAlgorithm.auto const& hash_algorithm = normalized_algorithm.hash.name;@@ -7011,17 +7214,7 @@ WebIDL::ExceptionOr<ByteBuffer> PBKDF2::derive_bits(JS::Realm&, AlgorithmParamsauto iterations = normalized_algorithm.iterations;auto derived_key_length_bytes = *length_optional / 8;- auto hash_kind = TRY([&] -> WebIDL::ExceptionOr<::Crypto::Hash::HashKind> {- if (hash_algorithm == "SHA-1"sv)- return ::Crypto::Hash::HashKind::SHA1;- if (hash_algorithm == "SHA-256"sv)- return ::Crypto::Hash::HashKind::SHA256;- if (hash_algorithm == "SHA-384"sv)- return ::Crypto::Hash::HashKind::SHA384;- if (hash_algorithm == "SHA-512"sv)- return ::Crypto::Hash::HashKind::SHA512;- return WebIDL::NotSupportedError::create(Utf16String::formatted("Invalid hash function '{}'", hash_algorithm));- }());+ auto hash_kind = TRY(sha_hash_kind(hash_algorithm));::Crypto::Hash::PBKDF2 pbkdf2(hash_kind);auto maybe_result = pbkdf2.derive_key(password, salt, iterations, derived_key_length_bytes);@@ -7076,7 +7269,7 @@ WebIDL::ExceptionOr<GC::Ref<CryptoKey>> PBKDF2::import_key(JS::Realm&, Algorithm}// https://w3c.github.io/webcrypto/#x25519-operations-derive-bits-WebIDL::ExceptionOr<ByteBuffer> X25519::derive_bits(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length_optional)+WebIDL::ExceptionOr<void> X25519::validate_derive_bits_parameters(AlgorithmParams const& params, Optional<u32> length_optional) const{auto const& normalized_algorithm = static_cast<EcdhKeyDeriveParams const&>(params);@@ -7096,6 +7289,18 @@ WebIDL::ExceptionOr<ByteBuffer> X25519::derive_bits(JS::Realm& realm, AlgorithmPif (length_optional.has_value() && *length_optional > 256)return WebIDL::OperationError::create("Length is too large"_utf16);+ return {};+}++// https://w3c.github.io/webcrypto/#x25519-operations-derive-bits+WebIDL::ExceptionOr<ByteBuffer> X25519::derive_bits(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length_optional)+{+ auto const& normalized_algorithm = static_cast<EcdhKeyDeriveParams const&>(params);++ // NB: Steps 1 to 4 are performed by validate_derive_bits_parameters().+ TRY(validate_derive_bits_parameters(params, length_optional));+ auto& public_key = normalized_algorithm.public_key;+// 5. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.if (key->type() != KeyType::Private)return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);@@ -8125,6 +8330,21 @@ WebIDL::ExceptionOr<bool> HMAC::verify(JS::Realm& realm, AlgorithmParams const&,return mac.size() == signature.size() && timing_safe_compare(mac.data(), signature.data(), mac.size());}+// https://w3c.github.io/webcrypto/#hmac-operations-generate-key+WebIDL::ExceptionOr<void> HMAC::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ auto const& normalized_algorithm = static_cast<HmacKeyGenParams const&>(params);++ // NB: Step 2 throws an error when the length member of normalizedAlgorithm is not present and we do not know the+ // block size of the hash function, or when the length member is zero.+ if (!normalized_algorithm.length.has_value())+ TRY(hmac_hash_block_size(normalized_algorithm.hash));+ else if (normalized_algorithm.length.value() == 0)+ return WebIDL::OperationError::create("Invalid length"_utf16);++ return {};+}+// https://w3c.github.io/webcrypto/#hmac-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> HMAC::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& usages){@@ -8190,7 +8410,7 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> HMAC::generate_k}// https://w3c.github.io/webcrypto/#hmac-operations-import-key-WebIDL::ExceptionOr<GC::Ref<CryptoKey>> HMAC::import_key(JS::Realm& realm, Web::Crypto::AlgorithmParams const& params, KeyFormat key_format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& usages)+WebIDL::ExceptionOr<void> HMAC::validate_import_key_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<HmacImportParams const&>(params);@@ -8198,6 +8418,17 @@ WebIDL::ExceptionOr<GC::Ref<CryptoKey>> HMAC::import_key(JS::Realm& realm, Web::if (normalized_algorithm.length.has_value() && *normalized_algorithm.length == 0)return WebIDL::DataError::create("Invalid key length"_utf16);+ return {};+}++// https://w3c.github.io/webcrypto/#hmac-operations-import-key+WebIDL::ExceptionOr<GC::Ref<CryptoKey>> HMAC::import_key(JS::Realm& realm, Web::Crypto::AlgorithmParams const& params, KeyFormat key_format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& usages)+{+ auto const& normalized_algorithm = static_cast<HmacImportParams const&>(params);++ // NB: Step 1 is performed by validate_import_key_parameters().+ TRY(validate_import_key_parameters(params));+// 2. Let keyData be the key data to be imported.// 3. If usages contains an entry which is not "sign" or "verify", then throw a SyntaxError.for (auto const& usage : usages) {@@ -8464,7 +8695,7 @@ WebIDL::ExceptionOr<Optional<u32>> HMAC::get_key_length(JS::Realm&, AlgorithmPar}// https://wicg.github.io/webcrypto-modern-algos/#ml-dsa-operations-sign-WebIDL::ExceptionOr<ByteBuffer> MLDSA::sign(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& message)+WebIDL::ExceptionOr<void> MLDSA::validate_sign_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<ContextParams const&>(params);@@ -8473,6 +8704,17 @@ WebIDL::ExceptionOr<ByteBuffer> MLDSA::sign(JS::Realm&, AlgorithmParams const& pif (normalized_algorithm.context.has_value() && normalized_algorithm.context->size() > 255)return WebIDL::OperationError::create("Context is too long"_utf16);+ return {};+}++// https://wicg.github.io/webcrypto-modern-algos/#ml-dsa-operations-sign+WebIDL::ExceptionOr<ByteBuffer> MLDSA::sign(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& message)+{+ auto const& normalized_algorithm = static_cast<ContextParams const&>(params);++ // NB: Step 1 is performed by validate_sign_parameters().+ TRY(validate_sign_parameters(params));+// 2. If the [[type]] internal slot of key is not "private", then throw an InvalidAccessError.if (key->type() != KeyType::Private)return WebIDL::InvalidAccessError::create("Key is not a private key"_utf16);@@ -8505,7 +8747,7 @@ WebIDL::ExceptionOr<ByteBuffer> MLDSA::sign(JS::Realm&, AlgorithmParams const& p}// https://wicg.github.io/webcrypto-modern-algos/#ml-dsa-operations-verify-WebIDL::ExceptionOr<bool> MLDSA::verify(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& signature, ByteBuffer const& message)+WebIDL::ExceptionOr<void> MLDSA::validate_verify_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<ContextParams const&>(params);@@ -8514,6 +8756,17 @@ WebIDL::ExceptionOr<bool> MLDSA::verify(JS::Realm&, AlgorithmParams const& paramif (normalized_algorithm.context.has_value() && normalized_algorithm.context->size() > 255)return WebIDL::OperationError::create("Context is too long"_utf16);+ return {};+}++// https://wicg.github.io/webcrypto-modern-algos/#ml-dsa-operations-verify+WebIDL::ExceptionOr<bool> MLDSA::verify(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& signature, ByteBuffer const& message)+{+ auto const& normalized_algorithm = static_cast<ContextParams const&>(params);++ // NB: Step 1 is performed by validate_verify_parameters().+ TRY(validate_verify_parameters(params));+// 2. If the [[type]] internal slot of key is not "public", then throw an InvalidAccessError.if (key->type() != KeyType::Public)return WebIDL::InvalidAccessError::create("Key is not a public key"_utf16);@@ -9634,9 +9887,10 @@ WebIDL::ExceptionOr<GC::Ref<CryptoKey>> Argon2::import_key(JS::Realm&, Algorithm}// https://wicg.github.io/webcrypto-modern-algos/#argon2-operations-derive-bits-WebIDL::ExceptionOr<ByteBuffer> Argon2::derive_bits(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length)+WebIDL::ExceptionOr<void> Argon2::validate_derive_bits_parameters(AlgorithmParams const& params, Optional<u32> length) const{auto const& normalized_algorithm = static_cast<Argon2Params const&>(params);+// 1. If length is null, or is less than 32 (4*8), or is not a multiple of 8, then throw an OperationError.if (!length.has_value() || length.value() < 32u || length.value() % 8 != 0)return WebIDL::OperationError::create("Length must be equal or greater than 32"_utf16);@@ -9657,6 +9911,20 @@ WebIDL::ExceptionOr<ByteBuffer> Argon2::derive_bits(JS::Realm&, AlgorithmParamsif (normalized_algorithm.passes == 0)return WebIDL::OperationError::create("Invalid passes"_utf16);+ // NB: Our Argon2 rejects short nonces.+ if (auto result = ::Crypto::Hash::Argon2::validate_parameters(normalized_algorithm.nonce); result.is_error())+ return WebIDL::OperationError::create(Utf16String::formatted("Hashing function failed: {}", result.error()));++ return {};+}++// https://wicg.github.io/webcrypto-modern-algos/#argon2-operations-derive-bits+WebIDL::ExceptionOr<ByteBuffer> Argon2::derive_bits(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, Optional<u32> length)+{+ auto const& normalized_algorithm = static_cast<Argon2Params const&>(params);+ // NB: Steps 1 to 5 are performed by validate_derive_bits_parameters().+ TRY(validate_derive_bits_parameters(params, length));+auto const algorithm = [&]() {// 6 => If the name member of normalizedAlgorithm is a case-sensitive string match for "Argon2d":// Let type be 0.@@ -9713,6 +9981,21 @@ WebIDL::ExceptionOr<Optional<u32>> Argon2::get_key_length(JS::Realm&, AlgorithmPreturn Optional<u32> {};}+// https://wicg.github.io/webcrypto-modern-algos/#cshake-operations-digest+WebIDL::ExceptionOr<void> CShake::validate_digest_parameters(AlgorithmParams const& params) const+{+ // NB: Step 5 throws an OperationError for the parameters that our cSHAKE does not support.+ auto const& normalized_algorithm = static_cast<CShakeParams const&>(params);+ auto result = ::Crypto::Hash::SHAKE::validate_parameters(+ normalized_algorithm.output_length,+ normalized_algorithm.customization.map([](auto const& value) { return value.span(); }),+ normalized_algorithm.function_name.map([](auto const& value) { return value.span(); }));+ if (result.is_error())+ return WebIDL::OperationError::create(Utf16String::formatted("Hash function failed: {}", result.error()));++ return {};+}+// https://wicg.github.io/webcrypto-modern-algos/#cshake-operations-digestWebIDL::ExceptionOr<ByteBuffer> CShake::digest(JS::Realm&, AlgorithmParams const& params, ByteBuffer const& data){@@ -9786,7 +10069,7 @@ JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> AeadParams::from_value(JS:}// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> ChaCha20Poly1305::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+WebIDL::ExceptionOr<void> ChaCha20Poly1305::validate_encrypt_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<AeadParams const&>(params);@@ -9798,6 +10081,17 @@ WebIDL::ExceptionOr<ByteBuffer> ChaCha20Poly1305::encrypt(JS::Realm&, AlgorithmPif (normalized_algorithm.tag_length.has_value() && normalized_algorithm.tag_length.value() != 128)return WebIDL::OperationError::create("tagLength must be 128"_utf16);+ return {};+}++// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-encrypt+WebIDL::ExceptionOr<ByteBuffer> ChaCha20Poly1305::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+{+ auto const& normalized_algorithm = static_cast<AeadParams const&>(params);++ // NB: Steps 1 and 2 are performed by validate_encrypt_parameters().+ TRY(validate_encrypt_parameters(params));+// 3. Let additionalData be the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.auto const& additional_data = normalized_algorithm.additional_data.value_or(ByteBuffer {});@@ -9820,7 +10114,7 @@ WebIDL::ExceptionOr<ByteBuffer> ChaCha20Poly1305::encrypt(JS::Realm&, AlgorithmP}// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> ChaCha20Poly1305::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+WebIDL::ExceptionOr<void> ChaCha20Poly1305::validate_decrypt_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<AeadParams const&>(params);@@ -9832,6 +10126,17 @@ WebIDL::ExceptionOr<ByteBuffer> ChaCha20Poly1305::decrypt(JS::Realm&, AlgorithmPif (normalized_algorithm.tag_length.has_value() && normalized_algorithm.tag_length.value() != 128)return WebIDL::OperationError::create("tagLength must be 128"_utf16);+ return {};+}++// https://wicg.github.io/webcrypto-modern-algos/#chacha20-poly1305-operations-decrypt+WebIDL::ExceptionOr<ByteBuffer> ChaCha20Poly1305::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+{+ auto const& normalized_algorithm = static_cast<AeadParams const&>(params);++ // NB: Steps 1 and 2 are performed by validate_decrypt_parameters().+ TRY(validate_decrypt_parameters(params));+// 3. If ciphertext has a length less than 128 bits, then throw an OperationError.if (ciphertext.size() < 16)return WebIDL::OperationError::create("Ciphertext must be at least 128 bits"_utf16);@@ -10044,7 +10349,7 @@ WebIDL::ExceptionOr<Optional<u32>> ChaCha20Poly1305::get_key_length(JS::Realm&,}// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-encrypt-WebIDL::ExceptionOr<ByteBuffer> AesOcb::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+WebIDL::ExceptionOr<void> AesOcb::validate_encrypt_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<AeadParams const&>(params);@@ -10052,20 +10357,29 @@ WebIDL::ExceptionOr<ByteBuffer> AesOcb::encrypt(JS::Realm&, AlgorithmParams consif (normalized_algorithm.iv.size() > 15)return WebIDL::OperationError::create("IV must have a length of 15 bytes"_utf16);- size_t tag_length = 0;- auto constexpr valid_tag_lengths = Array { 64, 96, 128 };+ // NB: Our AES-OCB rejects an empty IV.+ if (normalized_algorithm.iv.is_empty())+ return WebIDL::OperationError::create("IV must not be empty"_utf16);// 2. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.- if (!normalized_algorithm.tag_length.has_value())- tag_length = 128;+ // If the tagLength member of normalizedAlgorithm is one of 64, 96 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm+ // Otherwise: throw an OperationError.+ auto constexpr valid_tag_lengths = Array { 64, 96, 128 };+ if (normalized_algorithm.tag_length.has_value() && !valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))+ return WebIDL::OperationError::create("Invalid tag length"_utf16);- // 2. If the tagLength member of normalizedAlgorithm is one of 64, 96 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm- else if (valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))- tag_length = normalized_algorithm.tag_length.value();+ return {};+}- // 2. Otherwise: throw an OperationError.- else- return WebIDL::OperationError::create("Invalid tag length"_utf16);+// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-encrypt+WebIDL::ExceptionOr<ByteBuffer> AesOcb::encrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& plaintext)+{+ auto const& normalized_algorithm = static_cast<AeadParams const&>(params);++ // NB: Steps 1 and 2 are performed by validate_encrypt_parameters(), which leaves tagLength as the tagLength member+ // of normalizedAlgorithm if present, or 128 otherwise.+ TRY(validate_encrypt_parameters(params));+ size_t tag_length = normalized_algorithm.tag_length.value_or(128);// 3. Let additionalData be the additionalData member of normalizedAlgorithm if present or the empty octet string otherwise.auto const& additional_data = normalized_algorithm.additional_data.value_or(ByteBuffer {});@@ -10093,7 +10407,7 @@ WebIDL::ExceptionOr<ByteBuffer> AesOcb::encrypt(JS::Realm&, AlgorithmParams cons}// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-decrypt-WebIDL::ExceptionOr<ByteBuffer> AesOcb::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+WebIDL::ExceptionOr<void> AesOcb::validate_decrypt_parameters(AlgorithmParams const& params) const{auto const& normalized_algorithm = static_cast<AeadParams const&>(params);@@ -10101,20 +10415,29 @@ WebIDL::ExceptionOr<ByteBuffer> AesOcb::decrypt(JS::Realm&, AlgorithmParams consif (normalized_algorithm.iv.size() > 15)return WebIDL::OperationError::create("IV must have a length of 15 bytes"_utf16);- size_t tag_length = 0;- auto constexpr valid_tag_lengths = Array { 64, 96, 128 };+ // NB: Our AES-OCB rejects an empty IV.+ if (normalized_algorithm.iv.is_empty())+ return WebIDL::OperationError::create("IV must not be empty"_utf16);// 2. If the tagLength member of normalizedAlgorithm is not present: Let tagLength be 128.- if (!normalized_algorithm.tag_length.has_value())- tag_length = 128;+ // If the tagLength member of normalizedAlgorithm is one of 64, 96 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm+ // Otherwise: throw an OperationError.+ auto constexpr valid_tag_lengths = Array { 64, 96, 128 };+ if (normalized_algorithm.tag_length.has_value() && !valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))+ return WebIDL::OperationError::create("Invalid tag length"_utf16);- // 2. If the tagLength member of normalizedAlgorithm is one of 64, 96 or 128: Let tagLength be equal to the tagLength member of normalizedAlgorithm- else if (valid_tag_lengths.contains_slow(normalized_algorithm.tag_length.value()))- tag_length = normalized_algorithm.tag_length.value();+ return {};+}- // 2. Otherwise: throw an OperationError.- else- return WebIDL::OperationError::create("Invalid tag length"_utf16);+// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-decrypt+WebIDL::ExceptionOr<ByteBuffer> AesOcb::decrypt(JS::Realm&, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& ciphertext)+{+ auto const& normalized_algorithm = static_cast<AeadParams const&>(params);++ // NB: Steps 1 and 2 are performed by validate_decrypt_parameters(), which leaves tagLength as the tagLength member+ // of normalizedAlgorithm if present, or 128 otherwise.+ TRY(validate_decrypt_parameters(params));+ size_t tag_length = normalized_algorithm.tag_length.value_or(128);// 3. If ciphertext has a length less than tagLength bits, then throw an OperationError.if (ciphertext.size() < tag_length / 8)@@ -10149,6 +10472,17 @@ WebIDL::ExceptionOr<ByteBuffer> AesOcb::decrypt(JS::Realm&, AlgorithmParams consreturn maybe_plaintext.value();}+// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-generate-key+WebIDL::ExceptionOr<void> AesOcb::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ auto const bits = static_cast<AesKeyGenParams const&>(params).length;+ if (bits != 128 && bits != 192 && bits != 256)+ return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-OCB key with unusual amount of {} bits", bits));++ return {};+}+// https://wicg.github.io/webcrypto-modern-algos/#aes-ocb-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesOcb::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& key_usages){@@ -10159,12 +10493,10 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> AesOcb::generate}}- // 2. If the length member of normalizedAlgorithm is not equal to one of 128, 192 or 256, then throw an OperationError.+ // NB: Step 2 is performed by validate_generate_key_parameters().+ TRY(validate_generate_key_parameters(params));auto const& normalized_algorithm = static_cast<AesKeyGenParams const&>(params);auto const bits = normalized_algorithm.length;- if (bits != 128 && bits != 192 && bits != 256) {- return WebIDL::OperationError::create(Utf16String::formatted("Cannot create AES-OCB key with unusual amount of {} bits", bits));- }// 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.@@ -10411,6 +10743,19 @@ static WebIDL::ExceptionOr<ByteBuffer> kmac_calculate_mac(return maybe_result.release_value();}+// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-sign+WebIDL::ExceptionOr<void> KMAC::validate_sign_parameters(AlgorithmParams const& params) const+{+ // NB: Step 2 fails for the output lengths and customizations that our KMAC does not support.+ auto const& normalized_algorithm = static_cast<KmacParams const&>(params);+ auto result = ::Crypto::Authentication::KMAC::validate_parameters(normalized_algorithm.output_length,+ normalized_algorithm.customization.map([](auto const& value) { return value.span(); }));+ if (result.is_error())+ return WebIDL::OperationError::create("KMAC operation failed"_utf16);++ return {};+}+// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-signWebIDL::ExceptionOr<ByteBuffer> KMAC::sign(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& message){@@ -10444,6 +10789,19 @@ WebIDL::ExceptionOr<ByteBuffer> KMAC::sign(JS::Realm& realm, AlgorithmParams conreturn mac;}+// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-verify+WebIDL::ExceptionOr<void> KMAC::validate_verify_parameters(AlgorithmParams const& params) const+{+ // NB: Step 2 fails for the output lengths and customizations that our KMAC does not support.+ auto const& normalized_algorithm = static_cast<KmacParams const&>(params);+ auto result = ::Crypto::Authentication::KMAC::validate_parameters(normalized_algorithm.output_length,+ normalized_algorithm.customization.map([](auto const& value) { return value.span(); }));+ if (result.is_error())+ return WebIDL::OperationError::create("KMAC operation failed"_utf16);++ return {};+}+// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-verifyWebIDL::ExceptionOr<bool> KMAC::verify(JS::Realm& realm, AlgorithmParams const& params, GC::Ref<CryptoKey> key, ByteBuffer const& signature, ByteBuffer const& message){@@ -10477,6 +10835,17 @@ WebIDL::ExceptionOr<bool> KMAC::verify(JS::Realm& realm, AlgorithmParams const&return mac.size() == signature.size() && timing_safe_compare(mac.data(), signature.data(), mac.size());}+// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-generate-key+WebIDL::ExceptionOr<void> KMAC::validate_generate_key_parameters(AlgorithmParams const& params) const+{+ // NB: Generating a key of a length that our KMAC does not support fails.+ auto const& normalized_algorithm = static_cast<KmacKeyGenParams const&>(params);+ if (normalized_algorithm.length.has_value())+ TRY(validate_kmac_key_length(normalized_algorithm.length.value()));++ return {};+}+// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-generate-keyWebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> KMAC::generate_key(JS::Realm& realm, AlgorithmParams const& params, bool extractable, Vector<KeyUsage> const& usages){@@ -10535,6 +10904,17 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> KMAC::generate_kreturn Variant<GC::Ref<CryptoKey>, CryptoKeyPair> { key };}+// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-import-key+WebIDL::ExceptionOr<void> KMAC::validate_import_key_parameters(AlgorithmParams const& params) const+{+ // NB: Importing a key of a length that our KMAC does not support fails.+ auto const& normalized_algorithm = static_cast<KmacImportParams const&>(params);+ if (normalized_algorithm.length.has_value())+ TRY(validate_kmac_key_length(normalized_algorithm.length.value()));++ return {};+}+// https://wicg.github.io/webcrypto-modern-algos/#kmac-operations-import-keyWebIDL::ExceptionOr<GC::Ref<CryptoKey>> KMAC::import_key(JS::Realm& realm, AlgorithmParams const& params, KeyFormat format, CryptoKey::ImportKeyData key_data, bool extractable, Vector<KeyUsage> const& usages){diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h b/Libraries/LibWeb/Crypto/CryptoAlgorithms.hindex abaec19bcae..f7e01b23c93 100644--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.h+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.h@@ -376,6 +376,15 @@ public:return WebIDL::NotSupportedError::create("decalpsulate is not supported"_utf16);}+ virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const { return {}; }+ virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const { return {}; }+ virtual WebIDL::ExceptionOr<void> validate_sign_parameters(AlgorithmParams const&) const { return {}; }+ virtual WebIDL::ExceptionOr<void> validate_verify_parameters(AlgorithmParams const&) const { return {}; }+ virtual WebIDL::ExceptionOr<void> validate_digest_parameters(AlgorithmParams const&) const { return {}; }+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const { return {}; }+ virtual WebIDL::ExceptionOr<void> validate_import_key_parameters(AlgorithmParams const&) const { return {}; }+ virtual WebIDL::ExceptionOr<void> validate_derive_bits_parameters(AlgorithmParams const&, Optional<u32>) const { return {}; }+virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const { return {}; }static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AlgorithmMethods); }@@ -394,6 +403,7 @@ public:virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new RSAOAEP); }@@ -412,6 +422,7 @@ public:virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new RSAPSS); }@@ -430,6 +441,7 @@ public:virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new RSASSAPKCS1); }@@ -447,6 +459,10 @@ public:virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;+ virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesCbc); }private:@@ -462,6 +478,10 @@ public:virtual WebIDL::ExceptionOr<ByteBuffer> encrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;virtual WebIDL::ExceptionOr<ByteBuffer> decrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;+ virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesCtr); }private:@@ -477,6 +497,10 @@ public:virtual WebIDL::ExceptionOr<ByteBuffer> decrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;+ virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesGcm); }private:@@ -492,6 +516,8 @@ public:virtual WebIDL::ExceptionOr<ByteBuffer> wrap_key(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;virtual WebIDL::ExceptionOr<ByteBuffer> unwrap_key(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesKw); }private:@@ -504,6 +530,8 @@ public:virtual WebIDL::ExceptionOr<ByteBuffer> derive_bits(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, Optional<u32>) override;virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;+ virtual WebIDL::ExceptionOr<void> validate_derive_bits_parameters(AlgorithmParams const&, Optional<u32>) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new HKDF); }private:@@ -516,6 +544,8 @@ public:virtual WebIDL::ExceptionOr<ByteBuffer> derive_bits(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, Optional<u32>) override;virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;+ virtual WebIDL::ExceptionOr<void> validate_derive_bits_parameters(AlgorithmParams const&, Optional<u32>) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new PBKDF2); }private:@@ -541,6 +571,10 @@ public:virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_import_key_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_sign_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_verify_parameters(AlgorithmParams const&) const override;virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ECDSA); }@@ -556,6 +590,9 @@ public:virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;virtual WebIDL::ExceptionOr<ByteBuffer> derive_bits(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, Optional<u32>) override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_import_key_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_derive_bits_parameters(AlgorithmParams const&, Optional<u32>) const override;virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ECDH); }@@ -605,6 +642,7 @@ public:virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;+ virtual WebIDL::ExceptionOr<void> validate_derive_bits_parameters(AlgorithmParams const&, Optional<u32>) const override;virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new X25519); }@@ -637,6 +675,9 @@ public:virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_import_key_parameters(AlgorithmParams const&) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new HMAC); }private:@@ -651,6 +692,8 @@ public:virtual WebIDL::ExceptionOr<GC::Ref<CryptoKey>> import_key(JS::Realm& realm, AlgorithmParams const&, KeyFormat, CryptoKey::ImportKeyData, bool, Vector<KeyUsage> const&) override;virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;+ virtual WebIDL::ExceptionOr<void> validate_sign_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_verify_parameters(AlgorithmParams const&) const override;virtual Optional<ReadonlySpan<KeyUsage>> public_key_usages() const override;static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new MLDSA); }@@ -681,6 +724,8 @@ public:virtual WebIDL::ExceptionOr<ByteBuffer> derive_bits(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, Optional<u32>) override;virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;+ virtual WebIDL::ExceptionOr<void> validate_derive_bits_parameters(AlgorithmParams const&, Optional<u32>) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new Argon2); }private:@@ -690,6 +735,9 @@ private:class CShake : public AlgorithmMethods {public:virtual WebIDL::ExceptionOr<ByteBuffer> digest(JS::Realm& realm, AlgorithmParams const&, ByteBuffer const&) override;++ virtual WebIDL::ExceptionOr<void> validate_digest_parameters(AlgorithmParams const&) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new CShake); }private:@@ -837,6 +885,11 @@ public:virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_import_key_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_sign_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_verify_parameters(AlgorithmParams const&) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new KMAC); }private:@@ -870,6 +923,9 @@ public:virtual WebIDL::ExceptionOr<ExportKeyResult> export_key(JS::Realm& realm, KeyFormat, GC::Ref<CryptoKey>) override;virtual WebIDL::ExceptionOr<Optional<u32>> get_key_length(JS::Realm& realm, AlgorithmParams const&) override;+ virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new ChaCha20Poly1305); }private:@@ -885,6 +941,10 @@ public:virtual WebIDL::ExceptionOr<ByteBuffer> decrypt(JS::Realm& realm, AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&) override;virtual WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> generate_key(JS::Realm& realm, AlgorithmParams const&, bool, Vector<KeyUsage> const&) override;+ virtual WebIDL::ExceptionOr<void> validate_encrypt_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_decrypt_parameters(AlgorithmParams const&) const override;+ virtual WebIDL::ExceptionOr<void> validate_generate_key_parameters(AlgorithmParams const&) const override;+static NonnullOwnPtr<AlgorithmMethods> create() { return adopt_own(*new AesOcb); }private: