Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / crypto / subtle / pbkdf2 / deriveBits.html
blob4adaee84c48ef5ff302935e9daacaa00e1fc7c83
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../../resources/js-test.js"></script>
5 <script src="../resources/common.js"></script>
6 </head>
7 <body>
8 <p id="description"></p>
9 <div id="console"></div>
11 <script>
12 description("Tests importKey/deriveBits for PBKDF2");
14 jsTestIsAsync = true;
16 // -------------------------------------------------
17 // Successful password import and bits derivation
18 // -------------------------------------------------
20 var kPbkdf2SuccessVectors = [
21 // Non-ascii password
23 password: [200, 201, 202, 203, 204, 205, 206, 207],
24 salt: "salt",
25 c: 20,
26 dkLen: 16,
27 hash: "SHA-1",
28 derived_key: "a7950c143ec64e2b8d4bb1db8677188b"
31 // Empty salt
33 password: "pass\0word",
34 salt: "",
35 c: 20,
36 dkLen: 16,
37 hash: "SHA-1",
38 derived_key: "7deaf8b4a801011c1cd27f36e3bfc962"
41 // SHA-256
43 password: "password",
44 salt: "salt",
45 c: 20,
46 dkLen: 16,
47 hash: "SHA-256",
48 derived_key: "83eb100b6a3a975f0fe3ffcdc2419852"
51 // SHA-512
53 password: "password",
54 salt: "salt",
55 c: 20,
56 dkLen: 16,
57 hash: "SHA-512",
58 derived_key: "e4dfce3830983830c50c351a0b0f79e1"
61 // Empty password.
63 password: [],
64 salt: "salt",
65 c: 20,
66 dkLen: 16,
67 hash: "SHA-384",
68 derived_key: "750261780a187897a9978371599db5d1"
71 // Derive zero bytes.
73 password: "password",
74 salt: "salt",
75 c: 4096,
76 dkLen: 0,
77 hash: "SHA-512",
78 derived_key: ""
82 function runPbkdf2SuccessTestCase(testCase)
84 var algorithm = {name: 'PBKDF2'};
86 var key = null;
87 var password = null;
88 if (typeof testCase.password === 'string')
89 password = asciiToUint8Array(testCase.password);
90 else if (Array.isArray(testCase.password))
91 password = new Uint8Array(testCase.password);
93 var usages = ['deriveBits', 'deriveKey'];
94 var extractable = false;
96 var params = {
97 name: 'PBKDF2',
98 salt: asciiToUint8Array(testCase.salt),
99 iterations: testCase.c,
100 hash: {name: testCase.hash}
102 // (1) Import the password
103 return crypto.subtle.importKey('raw', password, algorithm, extractable, usages).then(function(result) {
104 key = result;
105 // shouldBe() can only resolve variables in global context.
106 tmpKey = key;
107 shouldEvaluateAs("tmpKey.type", "secret");
108 shouldEvaluateAs("tmpKey.extractable", false);
109 shouldEvaluateAs("tmpKey.algorithm.name", "PBKDF2");
110 shouldEvaluateAs("tmpKey.usages.join(',')", "deriveKey,deriveBits");
112 // (2) Derive bits
113 return crypto.subtle.deriveBits(params, key, testCase.dkLen*8);
114 }).then(function(result) {
115 bytesShouldMatchHexString("deriveBits", testCase.derived_key, result);
116 return crypto.subtle.deriveBits(params, key, 0);
117 }).then(function(result) {
118 derivedBits = result;
119 shouldBe("derivedBits.byteLength", "0");
123 var lastPromise = Promise.resolve(null);
125 kPbkdf2SuccessVectors.forEach(function(test) {
126 lastPromise = lastPromise.then(runPbkdf2SuccessTestCase.bind(null, test));
129 lastPromise.then(finishJSTest, failAndFinishJSTest);
131 </script>
133 </body>
134 </html>