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("Test bad inputs to HKDF deriveBits()");
16 kHkdfKey
= "0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b";
18 var extractable
= true;
19 Promise
.resolve(null).then(function(result
) {
20 // Set up the test by creating an HKDF key...
21 return crypto
.subtle
.importKey("raw", hexStringToUint8Array(kHkdfKey
), {name
: "HKDF"}, extractable
, ['deriveKey', 'deriveBits']);
22 }).then(function(result
) {
25 // ... and an ECDH key.
26 return crypto
.subtle
.generateKey({name
: "ECDH", namedCurve
: "P-256"}, true, ['deriveBits']);
27 }).then(function(result
) {
30 // Should throw a NotSupportedError if hash does not describe a recognized
31 // algorithm that supports the digest operation.
32 debug("\nderiveBits() with an unsupported hash...");
33 return crypto
.subtle
.deriveBits({name
: "HKDF", hash
: "HMAC", salt
: new Uint8Array(), info
: new Uint8Array()}, hkdfKey
, 8);
34 }).then(failAndFinishJSTest
, function(result
) {
37 // Should throw an InvalidAccessError if key doesn't match the algorithm
38 debug("\nderiveBits() with an ECDH key...");
39 return crypto
.subtle
.deriveBits({name
: "HKDF", hash
: "SHA-256", salt
: new Uint8Array(), info
: new Uint8Array()}, ecdhKey
.privateKey
, 8);
40 }).then(failAndFinishJSTest
, function(result
) {
43 // Should throw an OperationError if the key derivation operation
44 // fails. The key derivation operation will fail here because the length
47 // The maximum length (in bytes) of output material for HKDF is 255 times
48 // the digest length. In this case, the digest length (in bytes) of
49 // SHA-256 is 32; 32*255 = 8160. deriveBits expects the length to be in
50 // bits, so 8160*8=65280 and add 1 to exceed the maximum length.
51 debug("\nderiveBits() with length of 65281...");
52 return crypto
.subtle
.deriveBits({name
: "HKDF", hash
: "SHA-256", salt
: new Uint8Array(), info
: new Uint8Array()}, hkdfKey
, 65281);
53 }).then(failAndFinishJSTest
, function(result
) {
55 }).then(finishJSTest
, failAndFinishJSTest
);