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