Skip to content

Commit

Permalink
LibWeb: Set algorithm length for HMAC key generation
Browse files Browse the repository at this point in the history
This assignment was missing from the spec, but has been fixed since
w3c/webcrypto#394.

Also add relevant WPT tests.
  • Loading branch information
devgianlu authored and awesomekling committed Jan 2, 2025
1 parent ac0036d commit f5d3d6a
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 5 deletions.
13 changes: 8 additions & 5 deletions Libraries/LibWeb/Crypto/CryptoAlgorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6205,19 +6205,22 @@ WebIDL::ExceptionOr<Variant<GC::Ref<CryptoKey>, GC::Ref<CryptoKeyPair>>> HMAC::g
// 10. Set the hash attribute of algorithm to hash.
algorithm->set_hash(hash);

// 11. Set the [[type]] internal slot of key to "secret".
// 11. Set the length attribute of algorithm to length.
algorithm->set_length(length);

// 12. Set the [[type]] internal slot of key to "secret".
key->set_type(Bindings::KeyType::Secret);

// 12. Set the [[algorithm]] internal slot of key to algorithm.
// 13. Set the [[algorithm]] internal slot of key to algorithm.
key->set_algorithm(algorithm);

// 13. Set the [[extractable]] internal slot of key to be extractable.
// 14. Set the [[extractable]] internal slot of key to be extractable.
key->set_extractable(extractable);

// 14. Set the [[usages]] internal slot of key to be usages.
// 15. Set the [[usages]] internal slot of key to be usages.
key->set_usages(usages);

// 15. Return key.
// 16. Return key.
return Variant<GC::Ref<CryptoKey>, GC::Ref<CryptoKeyPair>> { key };
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
Harness status: OK

Found 192 tests

96 Pass
96 Fail
Pass Success: generateKey({hash: SHA-1, length: 160, name: HMAC}, false, [sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: HMAC}, true, [sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: HMAC}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: HMAC}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: HMAC}, false, [verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: HMAC}, true, [verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: HMAC}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: HMAC}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: HMAC}, false, [sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: HMAC}, true, [sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: HMAC}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: HMAC}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: HMAC}, false, [verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: HMAC}, true, [verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: HMAC}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: HMAC}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: HMAC}, false, [sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: HMAC}, true, [sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: HMAC}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: HMAC}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: HMAC}, false, [verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: HMAC}, true, [verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: HMAC}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: HMAC}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: HMAC}, false, [sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: HMAC}, true, [sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: HMAC}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: HMAC}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: HMAC}, false, [verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: HMAC}, true, [verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: HMAC}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: HMAC}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-1, name: HMAC}, false, [sign])
Fail Success: generateKey({hash: SHA-1, name: HMAC}, true, [sign])
Fail Success: generateKey({hash: SHA-1, name: HMAC}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-1, name: HMAC}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-1, name: HMAC}, false, [verify])
Fail Success: generateKey({hash: SHA-1, name: HMAC}, true, [verify])
Fail Success: generateKey({hash: SHA-1, name: HMAC}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-1, name: HMAC}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-256, name: HMAC}, false, [sign])
Fail Success: generateKey({hash: SHA-256, name: HMAC}, true, [sign])
Fail Success: generateKey({hash: SHA-256, name: HMAC}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-256, name: HMAC}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-256, name: HMAC}, false, [verify])
Fail Success: generateKey({hash: SHA-256, name: HMAC}, true, [verify])
Fail Success: generateKey({hash: SHA-256, name: HMAC}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-256, name: HMAC}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-384, name: HMAC}, false, [sign])
Fail Success: generateKey({hash: SHA-384, name: HMAC}, true, [sign])
Fail Success: generateKey({hash: SHA-384, name: HMAC}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-384, name: HMAC}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-384, name: HMAC}, false, [verify])
Fail Success: generateKey({hash: SHA-384, name: HMAC}, true, [verify])
Fail Success: generateKey({hash: SHA-384, name: HMAC}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-384, name: HMAC}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-512, name: HMAC}, false, [sign])
Fail Success: generateKey({hash: SHA-512, name: HMAC}, true, [sign])
Fail Success: generateKey({hash: SHA-512, name: HMAC}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-512, name: HMAC}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-512, name: HMAC}, false, [verify])
Fail Success: generateKey({hash: SHA-512, name: HMAC}, true, [verify])
Fail Success: generateKey({hash: SHA-512, name: HMAC}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-512, name: HMAC}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: hmac}, false, [sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: hmac}, true, [sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: hmac}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: hmac}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: hmac}, false, [verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: hmac}, true, [verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: hmac}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: hmac}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: hmac}, false, [sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: hmac}, true, [sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: hmac}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: hmac}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: hmac}, false, [verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: hmac}, true, [verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: hmac}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: hmac}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: hmac}, false, [sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: hmac}, true, [sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: hmac}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: hmac}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: hmac}, false, [verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: hmac}, true, [verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: hmac}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: hmac}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: hmac}, false, [sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: hmac}, true, [sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: hmac}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: hmac}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: hmac}, false, [verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: hmac}, true, [verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: hmac}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: hmac}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-1, name: hmac}, false, [sign])
Fail Success: generateKey({hash: SHA-1, name: hmac}, true, [sign])
Fail Success: generateKey({hash: SHA-1, name: hmac}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-1, name: hmac}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-1, name: hmac}, false, [verify])
Fail Success: generateKey({hash: SHA-1, name: hmac}, true, [verify])
Fail Success: generateKey({hash: SHA-1, name: hmac}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-1, name: hmac}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-256, name: hmac}, false, [sign])
Fail Success: generateKey({hash: SHA-256, name: hmac}, true, [sign])
Fail Success: generateKey({hash: SHA-256, name: hmac}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-256, name: hmac}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-256, name: hmac}, false, [verify])
Fail Success: generateKey({hash: SHA-256, name: hmac}, true, [verify])
Fail Success: generateKey({hash: SHA-256, name: hmac}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-256, name: hmac}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-384, name: hmac}, false, [sign])
Fail Success: generateKey({hash: SHA-384, name: hmac}, true, [sign])
Fail Success: generateKey({hash: SHA-384, name: hmac}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-384, name: hmac}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-384, name: hmac}, false, [verify])
Fail Success: generateKey({hash: SHA-384, name: hmac}, true, [verify])
Fail Success: generateKey({hash: SHA-384, name: hmac}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-384, name: hmac}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-512, name: hmac}, false, [sign])
Fail Success: generateKey({hash: SHA-512, name: hmac}, true, [sign])
Fail Success: generateKey({hash: SHA-512, name: hmac}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-512, name: hmac}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-512, name: hmac}, false, [verify])
Fail Success: generateKey({hash: SHA-512, name: hmac}, true, [verify])
Fail Success: generateKey({hash: SHA-512, name: hmac}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-512, name: hmac}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: Hmac}, false, [sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: Hmac}, true, [sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: Hmac}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: Hmac}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-1, length: 160, name: Hmac}, false, [verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: Hmac}, true, [verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: Hmac}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-1, length: 160, name: Hmac}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: Hmac}, false, [sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: Hmac}, true, [sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: Hmac}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: Hmac}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-256, length: 256, name: Hmac}, false, [verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: Hmac}, true, [verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: Hmac}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-256, length: 256, name: Hmac}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: Hmac}, false, [sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: Hmac}, true, [sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: Hmac}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: Hmac}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-384, length: 384, name: Hmac}, false, [verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: Hmac}, true, [verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: Hmac}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-384, length: 384, name: Hmac}, true, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: Hmac}, false, [sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: Hmac}, true, [sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: Hmac}, false, [verify, sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: Hmac}, true, [verify, sign])
Pass Success: generateKey({hash: SHA-512, length: 512, name: Hmac}, false, [verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: Hmac}, true, [verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: Hmac}, false, [sign, verify, sign, verify])
Pass Success: generateKey({hash: SHA-512, length: 512, name: Hmac}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-1, name: Hmac}, false, [sign])
Fail Success: generateKey({hash: SHA-1, name: Hmac}, true, [sign])
Fail Success: generateKey({hash: SHA-1, name: Hmac}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-1, name: Hmac}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-1, name: Hmac}, false, [verify])
Fail Success: generateKey({hash: SHA-1, name: Hmac}, true, [verify])
Fail Success: generateKey({hash: SHA-1, name: Hmac}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-1, name: Hmac}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-256, name: Hmac}, false, [sign])
Fail Success: generateKey({hash: SHA-256, name: Hmac}, true, [sign])
Fail Success: generateKey({hash: SHA-256, name: Hmac}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-256, name: Hmac}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-256, name: Hmac}, false, [verify])
Fail Success: generateKey({hash: SHA-256, name: Hmac}, true, [verify])
Fail Success: generateKey({hash: SHA-256, name: Hmac}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-256, name: Hmac}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-384, name: Hmac}, false, [sign])
Fail Success: generateKey({hash: SHA-384, name: Hmac}, true, [sign])
Fail Success: generateKey({hash: SHA-384, name: Hmac}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-384, name: Hmac}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-384, name: Hmac}, false, [verify])
Fail Success: generateKey({hash: SHA-384, name: Hmac}, true, [verify])
Fail Success: generateKey({hash: SHA-384, name: Hmac}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-384, name: Hmac}, true, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-512, name: Hmac}, false, [sign])
Fail Success: generateKey({hash: SHA-512, name: Hmac}, true, [sign])
Fail Success: generateKey({hash: SHA-512, name: Hmac}, false, [verify, sign])
Fail Success: generateKey({hash: SHA-512, name: Hmac}, true, [verify, sign])
Fail Success: generateKey({hash: SHA-512, name: Hmac}, false, [verify])
Fail Success: generateKey({hash: SHA-512, name: Hmac}, true, [verify])
Fail Success: generateKey({hash: SHA-512, name: Hmac}, false, [sign, verify, sign, verify])
Fail Success: generateKey({hash: SHA-512, name: Hmac}, true, [sign, verify, sign, verify])
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<meta charset=utf-8>
<title>WebCryptoAPI: generateKey() Successful Calls</title>
<meta name="timeout" content="long">
<script>
self.GLOBAL = {
isWindow: function() { return true; },
isWorker: function() { return false; },
isShadowRealm: function() { return false; },
};
</script>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<script src="../util/helpers.js"></script>
<script src="../../common/subset-tests.js"></script>
<script src="successes.js"></script>
<div id=log></div>
<script src="../../WebCryptoAPI/generateKey/successes_HMAC.https.any.js"></script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// META: title=WebCryptoAPI: generateKey() Successful Calls
// META: timeout=long
// META: script=../util/helpers.js
// META: script=/common/subset-tests.js
// META: script=successes.js
run_test(["HMAC"]);

0 comments on commit f5d3d6a

Please sign in to comment.