Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: LayoutTests/crypto/aes-cbc-encrypt-decrypt.html

Issue 206483010: [webcrypto] Refactor some layout tests. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
11 <script> 11 <script>
12 description("Tests cypto.subtle.encrypt and crypto.subtle.decrypt"); 12 description("Tests encrypt/decrypt for AES-CBC");
13 13
14 jsTestIsAsync = true; 14 jsTestIsAsync = true;
15 15
16 // A list of Promises for every test to run.
17 var allTests = [];
18
19 // ------------------------------------------------- 16 // -------------------------------------------------
20 // Successful encryption/decryption 17 // Successful encryption/decryption
21 // ------------------------------------------------- 18 // -------------------------------------------------
22 19
23 // Test vectors marked with [1] were copied from: 20 // Test vectors marked with [1] were copied from:
24 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf 21 // http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
25 // 22 //
26 // The NIST tests do not have a padding block. To match the WebCrypto 23 // The NIST tests do not have a padding block. To match the WebCrypto
27 // expectations, a PKCS#5 padding block has been added. 24 // expectations, a PKCS#5 padding block has been added.
28 25
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 // 128-bit key, with empty plaintext. 60 // 128-bit key, with empty plaintext.
64 // Derived from Chromium's EncryptorTest.EmptyEncrypt() (encryptor_unittest.cc ) 61 // Derived from Chromium's EncryptorTest.EmptyEncrypt() (encryptor_unittest.cc )
65 { 62 {
66 key: "3132383d5369787465656e4279746573", 63 key: "3132383d5369787465656e4279746573",
67 iv: "5377656574205369787465656e204956", 64 iv: "5377656574205369787465656e204956",
68 plainText: "", 65 plainText: "",
69 cipherText: "8518b8878d34e7185e300d0fcc426396" 66 cipherText: "8518b8878d34e7185e300d0fcc426396"
70 }, 67 },
71 ]; 68 ];
72 69
73 // These tests come from the NIST GCM test vectors:
74 // http://csrc.nist.gov/groups/STM/cavp/documents/mac/gcmtestvectors.zip
75 //
76 // Both encryption and decryption are expected to work.
77 var kAesGcmSuccessVectors =
78 [
79 // [Keylen = 128]
80 // [IVlen = 96]
81 // [PTlen = 0]
82 // [AADlen = 0]
83 // [Taglen = 128]
84 {
85 "key": "cf063a34d4a9a76c2c86787d3f96db71",
86 "iv": "113b9785971864c83b01c787",
87 "plainText": "",
88 "cipherText": "",
89 "additionalData": "",
90 "authenticationTag": "72ac8493e3a5228b5d130a69d2510e42"
91 },
92
93 // [Keylen = 128]
94 // [IVlen = 96]
95 // [PTlen = 0]
96 // [AADlen = 128]
97 // [Taglen = 120]
98 {
99 "key": "6dfa1a07c14f978020ace450ad663d18",
100 "iv": "34edfa462a14c6969a680ec1",
101 "plainText": "",
102 "cipherText": "",
103 "additionalData": "2a35c7f5f8578e919a581c60500c04f6",
104 "authenticationTag": "751f3098d59cf4ea1d2fb0853bde1c"
105 },
106
107 // [Keylen = 128]
108 // [IVlen = 96]
109 // [PTlen = 128]
110 // [AADlen = 128]
111 // [Taglen = 112]
112 {
113 "key": "ed6cd876ceba555706674445c229c12d",
114 "iv": "92ecbf74b765bc486383ca2e",
115 "plainText": "bfaaaea3880d72d4378561e2597a9b35",
116 "cipherText": "bdd2ed6c66fa087dce617d7fd1ff6d93",
117 "additionalData": "95bd10d77dbe0e87fb34217f1a2e5efe",
118 "authenticationTag": "ba82e49c55a22ed02ca67da4ec6f"
119 },
120
121 // [Keylen = 192]
122 // [IVlen = 96]
123 // [PTlen = 128]
124 // [AADlen = 384]
125 // [Taglen = 112]
126 {
127 "key": "ae7972c025d7f2ca3dd37dcc3d41c506671765087c6b61b8",
128 "iv": "984c1379e6ba961c828d792d",
129 "plainText": "d30b02c343487105219d6fa080acc743",
130 "cipherText": "c4489fa64a6edf80e7e6a3b8855bc37c",
131 "additionalData": "edd8f630f9bbc31b0acf122998f15589d6e6e3e1a3ec89e0c6a6ece75 1610ebbf57fdfb9d82028ff1d9faebe37a268c1",
132 "authenticationTag": "772ee7de0f91a981c36c93a35c88"
133 }
134 ];
135
136 function runAesCbcSuccessTestCase(testCase) 70 function runAesCbcSuccessTestCase(testCase)
137 { 71 {
138 var algorithm = {name: 'aes-cbc', iv: hexStringToUint8Array(testCase.iv)}; 72 var algorithm = {name: 'aes-cbc', iv: hexStringToUint8Array(testCase.iv)};
139 73
140 var key = null; 74 var key = null;
141 var keyData = hexStringToUint8Array(testCase.key); 75 var keyData = hexStringToUint8Array(testCase.key);
142 var usages = ['encrypt', 'decrypt']; 76 var usages = ['encrypt', 'decrypt'];
143 var extractable = false; 77 var extractable = false;
144 78
145 // (1) Import the key 79 // (1) Import the key
146 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage s).then(function(result) { 80 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage s).then(function(result) {
147 key = result; 81 key = result;
148 82
149 // shouldBe() can only resolve variables in global context. 83 // shouldBe() can only resolve variables in global context.
150 tmpKey = key; 84 tmpKey = key;
151 shouldBe("tmpKey.type", "'secret'"); 85 shouldEvaluateAs("tmpKey.type", "secret");
152 shouldBe("tmpKey.extractable", "false"); 86 shouldEvaluateAs("tmpKey.extractable", false);
153 shouldBe("tmpKey.algorithm.name", "'AES-CBC'"); 87 shouldEvaluateAs("tmpKey.algorithm.name", "AES-CBC");
154 shouldBe("tmpKey.algorithm.length", (keyData.byteLength * 8).toString()) ; 88 shouldEvaluateAs("tmpKey.algorithm.length", keyData.byteLength * 8);
155 shouldBe("tmpKey.usages.join(',')", "'encrypt,decrypt'"); 89 shouldEvaluateAs("tmpKey.usages.join(',')", "encrypt,decrypt");
156 90
157 // (2) Encrypt. 91 // (2) Encrypt.
158 return crypto.subtle.encrypt(algorithm, key, hexStringToUint8Array(testC ase.plainText)); 92 return crypto.subtle.encrypt(algorithm, key, hexStringToUint8Array(testC ase.plainText));
159 }).then(function(result) { 93 }).then(function(result) {
160 bytesShouldMatchHexString("Encryption", testCase.cipherText, result); 94 bytesShouldMatchHexString("Encryption", testCase.cipherText, result);
161 95
162 // (3) Decrypt 96 // (3) Decrypt
163 return crypto.subtle.decrypt(algorithm, key, hexStringToUint8Array(testC ase.cipherText)); 97 return crypto.subtle.decrypt(algorithm, key, hexStringToUint8Array(testC ase.cipherText));
164 }).then(function(result) { 98 }).then(function(result) {
165 bytesShouldMatchHexString("Decryption", testCase.plainText, result); 99 bytesShouldMatchHexString("Decryption", testCase.plainText, result);
166 }); 100 });
167 } 101 }
168 102
169 function runAesGcmSuccessTestCase(testCase) 103 var lastPromise = Promise.resolve(null);
170 {
171 var key = null;
172 var keyData = hexStringToUint8Array(testCase.key);
173 var iv = hexStringToUint8Array(testCase.iv);
174 var additionalData = hexStringToUint8Array(testCase.additionalData);
175 var tag = hexStringToUint8Array(testCase.authenticationTag);
176 var usages = ['encrypt', 'decrypt'];
177 var extractable = false;
178 104
179 var tagLengthBits = tag.byteLength * 8; 105 kAesCbcSuccessVectors.forEach(function(test) {
106 lastPromise = lastPromise.then(runAesCbcSuccessTestCase.bind(null, test));
107 });
180 108
181 var algorithm = {name: 'aes-gcm', iv: iv, additionalData: additionalData, ta gLength: tagLengthBits}; 109 lastPromise.then(finishJSTest, failAndFinishJSTest);
182
183 // (1) Import the key
184 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage s).then(function(result) {
185 key = result;
186
187 // shouldBe() can only resolve variables in global context.
188 tmpKey = key;
189 shouldBe("tmpKey.type", "'secret'");
190 shouldBe("tmpKey.extractable", "false");
191 shouldBe("tmpKey.algorithm.name", "'AES-GCM'");
192 shouldBe("tmpKey.usages.join(',')", "'encrypt,decrypt'");
193
194 // (2) Encrypt.
195 return crypto.subtle.encrypt(algorithm, key, hexStringToUint8Array(testC ase.plainText));
196 }).then(function(result) {
197 bytesShouldMatchHexString("Encryption", testCase.cipherText + testCase.a uthenticationTag, result);
198
199 // (3) Decrypt
200 return crypto.subtle.decrypt(algorithm, key, hexStringToUint8Array(testC ase.cipherText + testCase.authenticationTag));
201 }).then(function(result) {
202 bytesShouldMatchHexString("Decryption", testCase.plainText, result);
203 });
204 }
205
206 // Add all of the tests defined above.
207 for (var i = 0; i < kAesCbcSuccessVectors.length; ++i) {
208 addTask(runAesCbcSuccessTestCase(kAesCbcSuccessVectors[i]));
209 }
210
211 // Add all of the tests defined above.
212 for (var i = 0; i < kAesGcmSuccessVectors.length; ++i) {
213 addTask(runAesGcmSuccessTestCase(kAesGcmSuccessVectors[i]));
214 }
215
216 // -------------------------------------------------
217 // Failed key import.
218 // -------------------------------------------------
219
220 // Supported key lengths are 16 (128-bit), 32 (256-bit), 24 (192-bit),
221 // Try key lengths that are off by 1 from the supported ones.
222 var kUnsupportedKeyLengths = [
223 0, 1, 15, 17, 31, 33, 23, 25, 64
224 ];
225
226 function testInvalidKeyImport(keyLengthBytes)
227 {
228 var algorithm = {name: 'aes-cbc'};
229 var keyData = new Uint8Array(keyLengthBytes);
230
231 var usages = ['encrypt', 'decrypt'];
232 var extractable = false;
233
234 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage s).then(function(result) {
235 debug("FAIL: Successfully import key of length " + keyData.byteLength + " bytes");
236 }, function(result) {
237 debug("PASS: Failed to import key of length " + keyData.byteLength + " b ytes");
238 });
239 }
240
241 for (var i = 0; i < kUnsupportedKeyLengths.length; ++i) {
242 addTask(testInvalidKeyImport(kUnsupportedKeyLengths[i]));
243 }
244
245 // -------------------------------------------------
246 // Invalid cipher texts
247 // -------------------------------------------------
248
249 function testInvalidAesCbcDecryptions()
250 {
251 // 128-bit key with plaintext that is an exact multiple of block size.
252 // Derived from [1] F.2.1 (CBC-AES128.Encrypt), by adding padding block.
253 var iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f");
254 var keyData = hexStringToUint8Array("2b7e151628aed2a6abf7158809cf4f3c");
255 var cipherText = hexStringToUint8Array("7649abac8119b246cee98e9b12e9197d5086 cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120e ca307586e1a78cb82807230e1321d3fae00d18cc2012");
256
257 var key = null;
258 var usages = ['encrypt', 'decrypt'];
259 var extractable = false;
260 var algorithm = {name: 'aes-cbc', iv: iv};
261
262 function verifyDecryptionFails(newCipherTextLength)
263 {
264 var newCipherText = cipherText.subarray(0, newCipherTextLength);
265
266 var description = "ciphertext length: " + newCipherText.byteLength;
267 return crypto.subtle.decrypt(algorithm, key, newCipherText).then(functio n(result) {
268 debug("FAIL: decrypting succeeded. " + description);
269 }, function(result) {
270 debug("PASS: decrypting failed. " + description);
271 });
272 }
273
274 return crypto.subtle.importKey('raw', keyData, algorithm, extractable, usage s).then(function(result) {
275 key = result;
276
277 // Verify that decryption works with the original ciphertext.
278 return crypto.subtle.decrypt(algorithm, key, cipherText);
279 }).then(function(result) {
280 debug("PASS: Decryption succeeded");
281
282 // Try a number of bad ciphertexts.
283 return Promise.all([
284 verifyDecryptionFails(0),
285 verifyDecryptionFails(cipherText.byteLength - 1),
286
287 // Stripped a whole block. This new final block will result in a
288 // padding error.
289 verifyDecryptionFails(cipherText.byteLength - 16),
290 verifyDecryptionFails(1),
291 verifyDecryptionFails(15),
292 verifyDecryptionFails(16),
293 verifyDecryptionFails(17),
294 ]);
295 });
296 }
297
298 addTask(testInvalidAesCbcDecryptions());
299
300 function testNormalizationFailures(importedKeys)
301 {
302 keys = importedKeys;
303
304 data = asciiToUint8Array("hello");
305
306 // ---------------------------------------------------
307 // AES-CBC normalization failures (AesCbcParams)
308 // ---------------------------------------------------
309
310 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CBC', iv: nul l}, keys.aesCbc, data)");
311 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CBC'}, keys.a esCbc, data)");
312 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CBC', iv: 3}, keys.aesCbc, data)");
313 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array(0)}, keys.aesCbc, data)");
314
315 // ---------------------------------------------------
316 // AES-CTR normalization failures (AesCtrParams)
317 // ---------------------------------------------------
318
319 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter : null}, keys.aesCtr, data)");
320 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR'}, keys.a esCtr, data)");
321 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter : new Uint8Array(0)}, keys.aesCtr, data)");
322 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter : new Uint8Array(16), length: 0}, keys.aesCtr, data)");
323 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter : new Uint8Array(16), length: 18}, keys.aesCtr, data)");
324 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter : new Uint8Array(16), length: 256}, keys.aesCtr, data)");
325 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter : new Uint8Array(16), length: -3}, keys.aesCtr, data)");
326 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-CTR', counter : new Uint8Array(16), length: Infinity}, keys.aesCtr, data)");
327
328 // ---------------------------------------------------
329 // AES-CBC normalization failures (AesGcmParams)
330 // ---------------------------------------------------
331
332 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-gcm'}, keys.a esGcm, data)");
333 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-gcm', iv: 3}, keys.aesGcm, data)");
334 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-gcm', iv: 'fo o'}, keys.aesGcm, data)");
335 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-gcm', iv: new Uint8Array(16), additionalData: '5'}, keys.aesGcm, data)");
336 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-gcm', iv: new Uint8Array(16), additionalData: new Uint8Array(1), tagLength: 'foo'}, keys.aesG cm, data)");
337 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-gcm', iv: new Uint8Array(16), additionalData: new Uint8Array(1), tagLength: -1}, keys.aesGcm, data)");
338 shouldRejectPromiseWithNull("crypto.subtle.encrypt({name: 'AES-gcm', iv: new Uint8Array(16), additionalData: new Uint8Array(1), tagLength: 8000}, keys.aesGc m, data)");
339
340 // Try calling with the wrong key type.
341 aesCbc = {name: 'AES-CBC', iv: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])};
342 shouldRejectPromiseWithNull("crypto.subtle.encrypt(aesCbc, keys.hmacSha1, da ta)");
343
344 // Key doesn't support encrypt.
345 shouldRejectPromiseWithNull("crypto.subtle.encrypt(aesCbc, keys.aesCbcJustDe crypt, data)");
346
347 // If no key was specified AND the algorithm was bogus, should complain
348 // about the missing key first.
349 shouldThrow("crypto.subtle.encrypt({name: 'bogus'}, null, data)");
350 }
351
352 addTask(importTestKeys().then(testNormalizationFailures));
353
354 completeTestWhenAllTasksDone();
355 110
356 </script> 111 </script>
357 112
358 </body> 113 </body>
114 </html>
OLDNEW
« no previous file with comments | « LayoutTests/crypto/aes-cbc-decrypt-failure-expected.txt ('k') | LayoutTests/crypto/aes-cbc-encrypt-decrypt-expected.txt » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698