4 <script src=
"../../../resources/js-test.js"></script>
5 <script src=
"../resources/common.js"></script>
8 <p id=
"description"></p>
9 <div id=
"console"></div>
12 description("Tests structured de-cloning of empty HMAC keys");
16 // It used to be possible to import empty HMAC keys, so it is possible that
17 // such keys were persisted to storage. This test verifies that such keys can
18 // still be successfully de-serialized and used.
20 // The version number of the serialized format used is 7.
22 function deserializeTestKeys()
24 function createKeyFromSerialized(serializedBytesHex
)
26 return internals
.deserializeBuffer(hexStringToUint8Array(serializedBytesHex
).buffer
);
29 debug("\nDeserializing empty HMAC SHA-1 key...");
30 var sha1Key
= createKeyFromSerialized("ff073f004b0200051900");
33 shouldEvaluateAs("key.type", "secret");
34 shouldEvaluateAs("key.extractable", true);
35 shouldEvaluateAs("key.algorithm.name", "HMAC");
36 shouldEvaluateAs("key.algorithm.length", 0);
37 shouldEvaluateAs("key.algorithm.hash.name", "SHA-1");
38 shouldBe("key.usages", '["sign", "verify"]');
40 debug("\nDeserializing empty HMAC SHA-256 key...");
41 var sha256Key
= createKeyFromSerialized("ff073f004b0200061900");
44 shouldEvaluateAs("key.type", "secret");
45 shouldEvaluateAs("key.extractable", true);
46 shouldEvaluateAs("key.algorithm.name", "HMAC");
47 shouldEvaluateAs("key.algorithm.length", 0);
48 shouldEvaluateAs("key.algorithm.hash.name", "SHA-256");
49 shouldBe("key.usages", '["sign", "verify"]');
58 Promise
.resolve(deserializeTestKeys()).then(function(result
) {
61 debug("\ncalling verify() with a valid signature (SHA-1) ...");
62 return crypto
.subtle
.verify("HMAC", keys
.sha1
, hexStringToUint8Array("fbdb1d1b18aa6c08324b7d64b71fb76370690e1d"), hexStringToUint8Array(""));
63 }).then(function(result
) {
64 verifyResult
= result
;
65 shouldEvaluateAs("verifyResult", true);
67 debug("\ncalling verify() with an invalid signature (SHA-1) ...");
68 return crypto
.subtle
.verify("HMAC", keys
.sha1
, hexStringToUint8Array("fbdb1d1b18aa6c08324b7d64b71fb76370690e1e"), hexStringToUint8Array(""));
69 }).then(function(result
) {
70 verifyResult
= result
;
71 shouldEvaluateAs("verifyResult", false);
73 debug("\ncalling sign() (SHA-1) over empty input...");
74 return crypto
.subtle
.sign("HMAC", keys
.sha1
, hexStringToUint8Array(""));
75 }).then(function(result
) {
76 bytesShouldMatchHexString("signature", "fbdb1d1b18aa6c08324b7d64b71fb76370690e1d", result
);
78 debug("\ncalling verify() with a valid signature (SHA-256) ...");
79 return crypto
.subtle
.verify("HMAC", keys
.sha256
, hexStringToUint8Array("b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad"), hexStringToUint8Array(""));
80 }).then(function(result
) {
81 verifyResult
= result
;
82 shouldEvaluateAs("verifyResult", true);
84 debug("\ncalling verify() with an invalid signature (SHA-256) ...");
85 return crypto
.subtle
.verify("HMAC", keys
.sha256
, hexStringToUint8Array("0000"), hexStringToUint8Array(""));
86 }).then(function(result
) {
87 verifyResult
= result
;
88 shouldEvaluateAs("verifyResult", false);
90 debug("\ncalling sign() (SHA-256) over empty input...");
91 return crypto
.subtle
.sign("HMAC", keys
.sha256
, hexStringToUint8Array(""));
92 }).then(function(result
) {
93 bytesShouldMatchHexString("signature", "b613679a0814d9ec772f95d778c35fc5ff1697c493715653c6c712144292c5ad", result
);
94 }).then(finishJSTest
, failAndFinishJSTest
);