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 encrypt/decrypt for AES-CBC"); |
| 13 |
| 14 jsTestIsAsync = true; |
| 15 |
| 16 // ------------------------------------------------- |
| 17 // Successful password import and key 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_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 var key = null; |
| 82 var dkey = null; |
| 83 |
| 84 function runPbkdf2SuccessTestCase(testCase) |
| 85 { |
| 86 var algorithm = {name: 'PBKDF2'}; |
| 87 |
| 88 var password = asciiToUint8Array(testCase.password); |
| 89 var usages = ['deriveBits', 'deriveKey']; |
| 90 var extractable = false; |
| 91 |
| 92 var params = { |
| 93 name: 'PBKDF2', |
| 94 salt: asciiToUint8Array(testCase.salt), |
| 95 iterations: testCase.c, |
| 96 hash: {name: testCase.hash} |
| 97 }; |
| 98 // (1) Import the password |
| 99 return crypto.subtle.importKey('raw', password, algorithm, extractable, usag
es).then(function(result) { |
| 100 key = result; |
| 101 // shouldBe() can only resolve variables in global context. |
| 102 tmpKey = key; |
| 103 shouldEvaluateAs("tmpKey.type", "secret"); |
| 104 shouldEvaluateAs("tmpKey.extractable", false); |
| 105 shouldEvaluateAs("tmpKey.algorithm.name", "PBKDF2"); |
| 106 shouldEvaluateAs("tmpKey.usages.join(',')", "deriveKey,deriveBits"); |
| 107 |
| 108 var derivedAlgorithm = {name: 'aes-cbc', length: 128}; |
| 109 // (2) Derive key for aes-cbc |
| 110 return crypto.subtle.deriveKey(params, key, derivedAlgorithm, true, ['en
crypt']); |
| 111 }).then(function(result) { |
| 112 dkey = result; |
| 113 |
| 114 // Verify the key's properties. |
| 115 shouldEvaluateAs("dkey.type", "secret"); |
| 116 shouldEvaluateAs("dkey.extractable", true); |
| 117 shouldEvaluateAs("dkey.algorithm.name", "AES-CBC"); |
| 118 shouldEvaluateAs("dkey.algorithm.length", 128); |
| 119 shouldEvaluateAs("dkey.usages.join(',')", "encrypt"); |
| 120 |
| 121 // Export the key and check its bytes. |
| 122 return crypto.subtle.exportKey("raw",dkey); |
| 123 }).then(function(result) { |
| 124 bytesShouldMatchHexString("Derivedkey", testCase.derived_key_full_length
.substring(0, 32), result); |
| 125 }); |
| 126 } |
| 127 |
| 128 var lastPromise = Promise.resolve(null); |
| 129 |
| 130 kPbkdf2SuccessVectors.forEach(function(test) { |
| 131 lastPromise = lastPromise.then(runPbkdf2SuccessTestCase.bind(null, test)); |
| 132 }); |
| 133 |
| 134 lastPromise.then(finishJSTest, failAndFinishJSTest); |
| 135 |
| 136 </script> |
| 137 |
| 138 </body> |
| 139 </html> |
OLD | NEW |