OLD | NEW |
1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
2 <html> | 2 <html> |
3 <head> | 3 <head> |
4 <script src="../resources/js-test.js"></script> | 4 <script src="../resources/js-test.js"></script> |
5 <script src="resources/common.js"></script> | 5 <script src="resources/common.js"></script> |
6 </head> | 6 </head> |
7 <body> | 7 <body> |
8 <p id="description"></p> | 8 <p id="description"></p> |
9 <div id="console"></div> | 9 <div id="console"></div> |
10 | 10 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 var keyData = hexStringToUint8Array(testCase.key); | 78 var keyData = hexStringToUint8Array(testCase.key); |
79 var usages = ['encrypt', 'decrypt']; | 79 var usages = ['encrypt', 'decrypt']; |
80 var extractable = false; | 80 var extractable = false; |
81 | 81 |
82 // (1) Import the key | 82 // (1) Import the key |
83 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage
s).then(function(result) { | 83 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage
s).then(function(result) { |
84 key = result; | 84 key = result; |
85 | 85 |
86 // shouldBe() can only resolve variables in global context. | 86 // shouldBe() can only resolve variables in global context. |
87 tmpKey = key; | 87 tmpKey = key; |
88 shouldBe("tmpKey.type", "'secret'") | 88 shouldBe("tmpKey.type", "'secret'"); |
89 shouldBe("tmpKey.extractable", "false") | 89 shouldBe("tmpKey.extractable", "false"); |
90 shouldBe("tmpKey.algorithm.name", "'AES-CBC'") | 90 shouldBe("tmpKey.algorithm.name", "'AES-CBC'"); |
91 shouldBe("tmpKey.usages.join(',')", "'decrypt,encrypt'") | 91 shouldBe("tmpKey.usages.join(',')", "'decrypt,encrypt'"); |
92 | 92 |
93 // (2) Encrypt. | 93 // (2) Encrypt. |
94 return crypto.subtle.encrypt(algorithm, key, hexStringToUint8Array(testC
ase.plainText)); | 94 return crypto.subtle.encrypt(algorithm, key, hexStringToUint8Array(testC
ase.plainText)); |
95 }).then(function(result) { | 95 }).then(function(result) { |
96 bytesShouldMatchHexString("Encryption", testCase.cipherText, result); | 96 bytesShouldMatchHexString("Encryption", testCase.cipherText, result); |
97 | 97 |
98 // (3) Decrypt | 98 // (3) Decrypt |
99 return crypto.subtle.decrypt(algorithm, key, hexStringToUint8Array(testC
ase.cipherText)); | 99 return crypto.subtle.decrypt(algorithm, key, hexStringToUint8Array(testC
ase.cipherText)); |
100 }).then(function(result) { | 100 }).then(function(result) { |
101 bytesShouldMatchHexString("Decryption", testCase.plainText, result); | 101 bytesShouldMatchHexString("Decryption", testCase.plainText, result); |
102 }); | 102 }); |
103 } | 103 } |
104 | 104 |
105 // Add all of the tests defined above. | 105 // Add all of the tests defined above. |
106 for (var i = 0; i < kSuccessTestVectors.length; ++i) { | 106 for (var i = 0; i < kSuccessTestVectors.length; ++i) { |
107 allTests.push(runSuccessTestCase(kSuccessTestVectors[i])); | 107 addTask(runSuccessTestCase(kSuccessTestVectors[i])); |
108 } | 108 } |
109 | 109 |
110 // ------------------------------------------------- | 110 // ------------------------------------------------- |
111 // Failed key import. | 111 // Failed key import. |
112 // ------------------------------------------------- | 112 // ------------------------------------------------- |
113 | 113 |
114 // Supported key lengths are 16 (128-bit), 32 (256-bit), 24 (192-bit), | 114 // Supported key lengths are 16 (128-bit), 32 (256-bit), 24 (192-bit), |
115 // Try key lengths that are off by 1 from the supported ones. | 115 // Try key lengths that are off by 1 from the supported ones. |
116 var kUnsupportedKeyLengths = [ | 116 var kUnsupportedKeyLengths = [ |
117 0, 1, 15, 17, 31, 33, 23, 25, 64 | 117 0, 1, 15, 17, 31, 33, 23, 25, 64 |
118 ]; | 118 ]; |
119 | 119 |
120 function testInvalidKeyImport(keyLengthBytes) | 120 function testInvalidKeyImport(keyLengthBytes) |
121 { | 121 { |
122 var algorithm = {name: 'aes-cbc'}; | 122 var algorithm = {name: 'aes-cbc'}; |
123 var keyData = new Uint8Array(keyLengthBytes); | 123 var keyData = new Uint8Array(keyLengthBytes); |
124 | 124 |
125 var usages = ['encrypt', 'decrypt']; | 125 var usages = ['encrypt', 'decrypt']; |
126 var extractable = false; | 126 var extractable = false; |
127 | 127 |
128 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage
s).then(function(result) { | 128 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage
s).then(function(result) { |
129 debug("FAIL: Successfully import key of length " + keyData.byteLength +
" bytes"); | 129 debug("FAIL: Successfully import key of length " + keyData.byteLength +
" bytes"); |
130 }, function(result) { | 130 }, function(result) { |
131 debug("PASS: Failed to import key of length " + keyData.byteLength + " b
ytes"); | 131 debug("PASS: Failed to import key of length " + keyData.byteLength + " b
ytes"); |
132 }); | 132 }); |
133 } | 133 } |
134 | 134 |
135 for (var i = 0; i < kUnsupportedKeyLengths.length; ++i) { | 135 for (var i = 0; i < kUnsupportedKeyLengths.length; ++i) { |
136 allTests.push(testInvalidKeyImport(kUnsupportedKeyLengths[i])); | 136 addTask(testInvalidKeyImport(kUnsupportedKeyLengths[i])); |
137 } | 137 } |
138 | 138 |
139 // ------------------------------------------------- | 139 // ------------------------------------------------- |
140 // Invalid cipher texts | 140 // Invalid cipher texts |
141 // ------------------------------------------------- | 141 // ------------------------------------------------- |
142 | 142 |
143 function testInvalidDecryptions() | 143 function testInvalidDecryptions() |
144 { | 144 { |
145 // 128-bit key with plaintext that is an exact multiple of block size. | 145 // 128-bit key with plaintext that is an exact multiple of block size. |
146 // Derived from [1] F.2.1 (CBC-AES128.Encrypt), by adding padding block. | 146 // Derived from [1] F.2.1 (CBC-AES128.Encrypt), by adding padding block. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 // padding error. | 182 // padding error. |
183 verifyDecryptionFails(cipherText.byteLength - 16), | 183 verifyDecryptionFails(cipherText.byteLength - 16), |
184 verifyDecryptionFails(1), | 184 verifyDecryptionFails(1), |
185 verifyDecryptionFails(15), | 185 verifyDecryptionFails(15), |
186 verifyDecryptionFails(16), | 186 verifyDecryptionFails(16), |
187 verifyDecryptionFails(17), | 187 verifyDecryptionFails(17), |
188 ]); | 188 ]); |
189 }); | 189 }); |
190 } | 190 } |
191 | 191 |
192 allTests.push(testInvalidDecryptions()); | 192 addTask(testInvalidDecryptions()); |
193 | 193 |
194 function testNormalizationFailures(importedKeys) | 194 function testNormalizationFailures(importedKeys) |
195 { | 195 { |
196 keys = importedKeys; | 196 keys = importedKeys; |
197 | 197 |
198 data = asciiToUint8Array("hello"); | 198 data = asciiToUint8Array("hello"); |
199 | 199 |
200 // --------------------------------------------------- | 200 // --------------------------------------------------- |
201 // AES-CBC normalization failures (AesCbcParams) | 201 // AES-CBC normalization failures (AesCbcParams) |
202 // --------------------------------------------------- | 202 // --------------------------------------------------- |
203 | 203 |
204 shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: null}, keys.aesCbc,
data)"); | 204 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CBC', iv: nul
l}, keys.aesCbc, data)"); |
205 shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC'}, keys.aesCbc, data)"); | 205 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CBC'}, keys.a
esCbc, data)"); |
206 shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: 3}, keys.aesCbc, da
ta)"); | 206 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CBC', iv: 3},
keys.aesCbc, data)"); |
207 shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array[0]},
keys.aesCbc, data)"); | 207 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CBC', iv: new
Uint8Array(0)}, keys.aesCbc, data)"); |
208 | 208 |
209 // --------------------------------------------------- | 209 // --------------------------------------------------- |
210 // AES-CTR normalization failures (AesCtrParams) | 210 // AES-CTR normalization failures (AesCtrParams) |
211 // --------------------------------------------------- | 211 // --------------------------------------------------- |
212 | 212 |
213 shouldThrow("crypto.subtle.encrypt({name: 'AES-CTR', counter: null}, keys.ae
sCtr, data)"); | 213 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter
: null}, keys.aesCtr, data)"); |
214 shouldThrow("crypto.subtle.encrypt({name: 'AES-CTR'}, keys.aesCtr, data)"); | 214 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR'}, keys.a
esCtr, data)"); |
215 shouldThrow("crypto.subtle.encrypt({name: 'AES-CTR', counter: new Uint8Array
(0)}, keys.aesCtr, data)"); | 215 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter
: new Uint8Array(0)}, keys.aesCtr, data)"); |
216 shouldThrow("crypto.subtle.encrypt({name: 'AES-CTR', counter: new Uint8Array
(16), length: 0}, keys.aesCtr, data)"); | 216 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter
: new Uint8Array(16), length: 0}, keys.aesCtr, data)"); |
217 shouldThrow("crypto.subtle.encrypt({name: 'AES-CTR', counter: new Uint8Array
(16), length: 18}, keys.aesCtr, data)"); | 217 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter
: new Uint8Array(16), length: 18}, keys.aesCtr, data)"); |
218 shouldThrow("crypto.subtle.encrypt({name: 'AES-CTR', counter: new Uint8Array
(16), length: 256}, keys.aesCtr, data)"); | 218 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter
: new Uint8Array(16), length: 256}, keys.aesCtr, data)"); |
219 shouldThrow("crypto.subtle.encrypt({name: 'AES-CTR', counter: new Uint8Array
(16), length: -3}, keys.aesCtr, data)"); | 219 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter
: new Uint8Array(16), length: -3}, keys.aesCtr, data)"); |
220 shouldThrow("crypto.subtle.encrypt({name: 'AES-CTR', counter: new Uint8Array
(16), length: Infinity}, keys.aesCtr, data)"); | 220 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter
: new Uint8Array(16), length: Infinity}, keys.aesCtr, data)"); |
221 | 221 |
222 // Try calling with the wrong key type. | 222 // Try calling with the wrong key type. |
223 aesCbc = {name: 'AES-CBC', iv: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15])}; | 223 aesCbc = {name: 'AES-CBC', iv: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15])}; |
224 shouldThrow("crypto.subtle.encrypt(aesCbc, keys.hmacSha1, data)"); | 224 shouldRejectPromiseWithNull("crypto.subtle.encrypt(aesCbc, keys.hmacSha1, da
ta)"); |
225 | 225 |
226 // Key doesn't support encrypt. | 226 // Key doesn't support encrypt. |
227 shouldThrow("crypto.subtle.encrypt(aesCbc, keys.aesCbcJustDecrypt, data)"); | 227 shouldRejectPromiseWithNull("crypto.subtle.encrypt(aesCbc, keys.aesCbcJustDe
crypt, data)"); |
228 | 228 |
229 // If no key was specified AND the algorithm was bogus, should complain | 229 // If no key was specified AND the algorithm was bogus, should complain |
230 // about the missing key first. | 230 // about the missing key first. |
231 shouldThrow("crypto.subtle.encrypt({name: 'bogus'}, null, data)"); | 231 shouldThrow("crypto.subtle.encrypt({name: 'bogus'}, null, data)"); |
232 } | 232 } |
233 | 233 |
234 allTests.push(importTestKeys().then(testNormalizationFailures)); | 234 addTask(importTestKeys().then(testNormalizationFailures)); |
235 | 235 |
236 // ------------------------------------------------- | 236 completeTestWhenAllTasksDone(); |
237 // Wait until all the tests have been run. | |
238 // ------------------------------------------------- | |
239 | |
240 Promise.all(allTests).then(finishJSTest, failAndFinishJSTest); | |
241 | 237 |
242 </script> | 238 </script> |
243 | 239 |
244 </body> | 240 </body> |
OLD | NEW |