OLD | NEW |
---|---|
(Empty) | |
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> | |
10 | |
11 <script> | |
12 description("Tests importKey/deriveBits for PBKDF2"); | |
13 | |
14 jsTestIsAsync = true; | |
15 | |
16 // ------------------------------------------------- | |
17 // Successful password import and bits derivation | |
18 // ------------------------------------------------- | |
19 | |
20 // Test vectors were copied from: | |
21 // https://tools.ietf.org/html/rfc6070 | |
22 // | |
23 | |
24 var kPbkdf2SuccessVectors = [ | |
25 { | |
26 password: "password", | |
27 salt: "salt", | |
28 c: 1, | |
29 dkLen: 20, | |
eroman
2015/01/07 06:48:52
What is the use of dkLen, it looks like it is alwa
xun.sun
2015/01/09 16:17:50
It is used as key length input to deriveBits() and
| |
30 hash: "SHA-1", | |
31 derived_key_full_length: "0c60c80f961f0e71f3a9b524af6012062fe037a6" | |
32 }, | |
33 | |
34 { | |
35 password: "password", | |
36 salt: "salt", | |
37 c: 2, | |
38 dkLen: 20, | |
39 hash: "SHA-1", | |
40 derived_key_full_length: "ea6c014dc72d6f8ccd1ed92ace1d41f0d8de8957" | |
41 }, | |
42 | |
43 { | |
44 password: "password", | |
45 salt: "salt", | |
46 c: 4096, | |
47 dkLen: 20, | |
48 hash: "SHA-1", | |
49 derived_key_full_length: "4b007901b765489abead49d926f721d065a429c1" | |
50 }, | |
51 | |
52 // 16777216 iterations would timeout | |
53 // { | |
54 // password: "password", | |
55 // salt: "salt", | |
56 // c: 16777216, | |
57 // dkLen: 20, | |
58 // hash: "SHA-1", | |
59 // derived_key_full_length: "eefe3d61cd4da4e4e9945b3d6ba2158c2634e984" | |
60 // }, | |
61 | |
62 { | |
63 password: "passwordPASSWORDpassword", | |
64 salt: "saltSALTsaltSALTsaltSALTsaltSALTsalt", | |
65 c: 4096, | |
66 dkLen: 25, | |
67 hash: "SHA-1", | |
68 derived_key_full_length: "3d2eec4fe41c849b80c8d83662c0e44a8b291a964cf2f07038 " | |
69 }, | |
70 | |
71 { | |
72 password: "pass\0word", | |
73 salt: "sa\0lt", | |
74 c: 4096, | |
75 dkLen: 16, | |
76 hash: "SHA-1", | |
77 derived_key_full_length: "56fa6aa75548099dcc37d7f03425e0c3" | |
78 }, | |
79 ]; | |
80 | |
81 function runPbkdf2SuccessTestCase(testCase) | |
82 { | |
83 var algorithm = {name: 'PBKDF2'}; | |
84 | |
85 var key = null; | |
86 var password = asciiToUint8Array(testCase.password); | |
87 var usages = ['deriveBits', 'deriveKey']; | |
88 var extractable = false; | |
89 | |
90 var params = { | |
91 name: 'PBKDF2', | |
92 salt: asciiToUint8Array(testCase.salt), | |
93 iterations: testCase.c, | |
94 hash: {name: testCase.hash} | |
95 }; | |
96 // (1) Import the password | |
97 return crypto.subtle.importKey('raw', password, algorithm, extractable, usag es).then(function(result) { | |
98 key = result; | |
99 // shouldBe() can only resolve variables in global context. | |
100 tmpKey = key; | |
101 shouldEvaluateAs("tmpKey.type", "secret"); | |
102 shouldEvaluateAs("tmpKey.extractable", false); | |
103 shouldEvaluateAs("tmpKey.algorithm.name", "PBKDF2"); | |
104 shouldEvaluateAs("tmpKey.usages.join(',')", "deriveKey,deriveBits"); | |
105 | |
106 // (2) Derive bits | |
107 return crypto.subtle.deriveBits(params, key, testCase.dkLen*8); | |
108 }).then(function(result) { | |
109 bytesShouldMatchHexString("deriveBits", testCase.derived_key_full_length , result); | |
110 }); | |
111 } | |
112 | |
113 var lastPromise = Promise.resolve(null); | |
114 | |
115 kPbkdf2SuccessVectors.forEach(function(test) { | |
116 lastPromise = lastPromise.then(runPbkdf2SuccessTestCase.bind(null, test)); | |
117 }); | |
118 | |
119 lastPromise.then(finishJSTest, failAndFinishJSTest); | |
120 | |
121 </script> | |
122 | |
123 </body> | |
124 </html> | |
OLD | NEW |