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 var kPbkdf2SuccessVectors = [ |
| 21 // Non-ascii password |
| 22 { |
| 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" |
| 29 }, |
| 30 |
| 31 // Empty salt |
| 32 { |
| 33 password: "pass\0word", |
| 34 salt: "", |
| 35 c: 20, |
| 36 dkLen: 16, |
| 37 hash: "SHA-1", |
| 38 derived_key: "7deaf8b4a801011c1cd27f36e3bfc962" |
| 39 }, |
| 40 |
| 41 // SHA-256 |
| 42 { |
| 43 password: "password", |
| 44 salt: "salt", |
| 45 c: 20, |
| 46 dkLen: 16, |
| 47 hash: "SHA-256", |
| 48 derived_key: "83eb100b6a3a975f0fe3ffcdc2419852" |
| 49 }, |
| 50 |
| 51 // SHA-512 |
| 52 { |
| 53 password: "password", |
| 54 salt: "salt", |
| 55 c: 20, |
| 56 dkLen: 16, |
| 57 hash: "SHA-512", |
| 58 derived_key: "e4dfce3830983830c50c351a0b0f79e1" |
| 59 }, |
| 60 ]; |
| 61 |
| 62 function runPbkdf2SuccessTestCase(testCase) |
| 63 { |
| 64 var algorithm = {name: 'PBKDF2'}; |
| 65 |
| 66 var key = null; |
| 67 var password = null; |
| 68 if (typeof testCase.password === 'string') |
| 69 password = asciiToUint8Array(testCase.password); |
| 70 else if (Array.isArray(testCase.password)) |
| 71 password = new Uint8Array(testCase.password); |
| 72 |
| 73 var usages = ['deriveBits', 'deriveKey']; |
| 74 var extractable = false; |
| 75 |
| 76 var params = { |
| 77 name: 'PBKDF2', |
| 78 salt: asciiToUint8Array(testCase.salt), |
| 79 iterations: testCase.c, |
| 80 hash: {name: testCase.hash} |
| 81 }; |
| 82 // (1) Import the password |
| 83 return crypto.subtle.importKey('raw', password, algorithm, extractable, usag
es).then(function(result) { |
| 84 key = result; |
| 85 // shouldBe() can only resolve variables in global context. |
| 86 tmpKey = key; |
| 87 shouldEvaluateAs("tmpKey.type", "secret"); |
| 88 shouldEvaluateAs("tmpKey.extractable", false); |
| 89 shouldEvaluateAs("tmpKey.algorithm.name", "PBKDF2"); |
| 90 shouldEvaluateAs("tmpKey.usages.join(',')", "deriveKey,deriveBits"); |
| 91 |
| 92 // (2) Derive bits |
| 93 return crypto.subtle.deriveBits(params, key, testCase.dkLen*8); |
| 94 }).then(function(result) { |
| 95 bytesShouldMatchHexString("deriveBits", testCase.derived_key, result); |
| 96 return crypto.subtle.deriveBits(params, key, 0); |
| 97 }).then(function(result) { |
| 98 derivedBits = result; |
| 99 shouldBe("derivedBits.byteLength", "0"); |
| 100 }); |
| 101 } |
| 102 |
| 103 var lastPromise = Promise.resolve(null); |
| 104 |
| 105 kPbkdf2SuccessVectors.forEach(function(test) { |
| 106 lastPromise = lastPromise.then(runPbkdf2SuccessTestCase.bind(null, test)); |
| 107 }); |
| 108 |
| 109 lastPromise.then(finishJSTest, failAndFinishJSTest); |
| 110 |
| 111 </script> |
| 112 |
| 113 </body> |
| 114 </html> |
OLD | NEW |