Skip to content

Truly independent web browser

LibWeb: Allow KMAC keys with a length of zero

Shannon Booth6d83bdf457ed
diff --git a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
index 615fdda87be..e6f8bee548b 100644
--- a/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
+++ b/Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
@@ -10397,10 +10397,8 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, CryptoKeyPair>> KMAC::generate_k
if (!kind.has_value())
return WebIDL::OperationError::create("Unsupported KMAC algorithm"_utf16);
length = ::Crypto::Authentication::KMAC::default_key_length(*kind);
- } else if (normalized_algorithm.length.value() != 0) {
- length = normalized_algorithm.length.value();
} else {
- return WebIDL::OperationError::create("Invalid key length"_utf16);
+ length = normalized_algorithm.length.value();
}
// 3. Generate a key of length bits.
@@ -10513,7 +10511,7 @@ WebIDL::ExceptionOr<GC::Ref<CryptoKey>> KMAC::import_key(JS::Realm& realm, Algor
return WebIDL::DataError::create("Invalid data size"_utf16);
// 2. If the length member of normalizedAlgorithm is less than or equal to length minus eight, then throw a DataError.
- if (requested_length <= length - 8)
+ if (requested_length + 8 <= length)
return WebIDL::DataError::create("Invalid data size"_utf16);
// 3. Otherwise:
diff --git a/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-KMAC-zero-length.txt b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-KMAC-zero-length.txt
new file mode 100644
index 00000000000..9ed76c1b202
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/Crypto/SubtleCrypto-KMAC-zero-length.txt
@@ -0,0 +1,2 @@
+generateKey: ok
+importKey: ok
diff --git a/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-KMAC-zero-length.html b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-KMAC-zero-length.html
new file mode 100644
index 00000000000..0d2b5fc26f4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/Crypto/SubtleCrypto-KMAC-zero-length.html
@@ -0,0 +1,17 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ async function check(label, promise) {
+ try {
+ await promise;
+ println(`${label}: ok`);
+ } catch (error) {
+ println(`${label}: ${error.name}`);
+ }
+ }
+
+ await check("generateKey", crypto.subtle.generateKey({ name: "KMAC128", length: 0 }, false, ["sign"]));
+ await check("importKey", crypto.subtle.importKey("raw-secret", new Uint8Array(0), { name: "KMAC128", length: 0 }, false, ["sign"]));
+ });
+</script>