| 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 // Empty password. | |
| 62 { | |
| 63 password: [], | |
| 64 salt: "salt", | |
| 65 c: 20, | |
| 66 dkLen: 16, | |
| 67 hash: "SHA-384", | |
| 68 derived_key: "750261780a187897a9978371599db5d1" | |
| 69 }, | |
| 70 | |
| 71 // Derive zero bytes. | |
| 72 { | |
| 73 password: "password", | |
| 74 salt: "salt", | |
| 75 c: 4096, | |
| 76 dkLen: 0, | |
| 77 hash: "SHA-512", | |
| 78 derived_key: "" | |
| 79 }, | |
| 80 ]; | |
| 81 | |
| 82 function runPbkdf2SuccessTestCase(testCase) | |
| 83 { | |
| 84 var algorithm = {name: 'PBKDF2'}; | |
| 85 | |
| 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); | |
| 92 | |
| 93 var usages = ['deriveBits', 'deriveKey']; | |
| 94 var extractable = false; | |
| 95 | |
| 96 var params = { | |
| 97 name: 'PBKDF2', | |
| 98 salt: asciiToUint8Array(testCase.salt), | |
| 99 iterations: testCase.c, | |
| 100 hash: {name: testCase.hash} | |
| 101 }; | |
| 102 // (1) Import the password | |
| 103 return crypto.subtle.importKey('raw', password, algorithm, extractable, usag
es).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"); | |
| 111 | |
| 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"); | |
| 120 }); | |
| 121 } | |
| 122 | |
| 123 var lastPromise = Promise.resolve(null); | |
| 124 | |
| 125 kPbkdf2SuccessVectors.forEach(function(test) { | |
| 126 lastPromise = lastPromise.then(runPbkdf2SuccessTestCase.bind(null, test)); | |
| 127 }); | |
| 128 | |
| 129 lastPromise.then(finishJSTest, failAndFinishJSTest); | |
| 130 | |
| 131 </script> | |
| 132 | |
| 133 </body> | |
| 134 </html> | |
| OLD | NEW |