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 deriving HKDF keys with deriveKey()");
16 var extractable
= true;
17 var derivingKeyAlgorithm
= {
20 salt
: new Uint8Array(),
21 info
: new Uint8Array()
24 var privateKeyJSON
= {
27 "d": "1mGcHOo_eTxXMTgSjWLLa5DCZIf0o8NNySooVNefCIw",
28 "x": "QcD58SaUjtYiUaUnTaOMWC7YKyfQ5yD0-8F9RStayBU",
29 "y": "CjHmp9BL54FleRbhJVQb1MgVu5YsFn7tJt0VWof_jj0"
35 "x": "AGzVMZDcNVrG7e8m9bLFTy7Si7o1IcU-SNyI8_Up62E",
36 "y": "bSVna0fR_BTIgH5--cEXXgOB3J2IlxKTmb8YeOZ7UKE"
39 var secret
= hexStringToUint8Array("82b17497eefdeee07fb108496b1e88b1975e42a98046b5521d18edc96a639fea");
44 salt
: new Uint8Array(),
45 info
: new Uint8Array()
48 Promise
.resolve(null).then(function(result
) {
49 return crypto
.subtle
.importKey("jwk", privateKeyJSON
, {name
: "ECDH", namedCurve
: "P-256"}, true, ["deriveKey"]);
50 }).then(function(result
) {
53 return crypto
.subtle
.importKey("jwk", publicKeyJSON
, {name
: "ECDH", namedCurve
: "P-256"}, true, []);
54 }).then(function(result
) {
57 debug("Derive an HKDF key from ECDH keys");
58 return crypto
.subtle
.deriveKey({name
: "ECDH", namedCurve
: "P-256", public: publicKey
}, privateKey
, "HKDF", true, ['deriveKey', 'deriveBits']);
59 }).then(function(result
) {
62 shouldEvaluateAs("hkdfKey.algorithm.name", "HKDF");
63 shouldEvaluateAs("hkdfKey.extractable", true);
64 shouldEvaluateAs("hkdfKey.usages.join(',')", "deriveKey,deriveBits");
66 debug("\nDerive 128 bits from the HKDF key");
67 return crypto
.subtle
.deriveBits(hkdfAlgorithm
, hkdfKey
, 128);
68 }).then(function(result
) {
71 return crypto
.subtle
.importKey("raw", secret
, hkdfAlgorithm
, true, ['deriveBits']);
72 }).then(function(hkdfKey
) {
73 return crypto
.subtle
.deriveBits(hkdfAlgorithm
, hkdfKey
, 128);
74 }).then(function(result
) {
75 expectedDerivedBits
= result
;
77 shouldEvaluateAs("bytesToHexString(derivedBits)", bytesToHexString(expectedDerivedBits
));
78 }).then(finishJSTest
, failAndFinishJSTest
);