1018fd7b8fe9efa2a6ca4303f371522fd85be59c
[WebKit-https.git] / LayoutTests / crypto / subtle / aes-cbc-encrypt-decrypt.html
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <script src="../../resources/js-test-pre.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("Test AES-CBC encrypt and decrypt functions.");
13
14 jsTestIsAsync = true;
15
16 var keyData = hexStringToUint8Array("2b7e151628aed2a6abf7158809cf4f3c");
17 var iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f");
18 var plaintext = hexStringToUint8Array("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
19 var extractable = true;
20
21 debug("Importing a raw AES key from string literal...");
22 crypto.subtle.importKey("raw", keyData, "aes-cbc", extractable, ["encrypt", "decrypt"]).then(function(result) {
23     key = result;
24     shouldBe("key.type", "'secret'");
25     shouldBe("key.extractable", "true");
26     shouldBe("key.algorithm.name", "'aes-cbc'");
27     shouldBe("key.algorithm.length", "128");
28     shouldBe("key.usages", "['encrypt', 'decrypt']");
29
30     debug("Using the key to encrypt plaintext...");
31     return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, [plaintext]);
32 }).then(function(result) {
33     encryptionResult = result;
34     shouldBe("bytesToHexString(new Uint8Array(encryptionResult))", "'7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a78cb82807230e1321d3fae00d18cc2012'");
35
36     debug("Decrypting it back...");
37     return crypto.subtle.decrypt({name: "aes-cbc", iv: iv}, key, [result]);
38 }).then(function(result) {
39     decryptionResult = result;
40     shouldBe("new Uint8Array(decryptionResult)", "plaintext");
41
42     debug("Testing initialization vector bindings...");
43     shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: null}, key, [plaintext])");
44     shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC'}, key, [plaintext])");
45     shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: 3}, key, [plaintext])");
46     shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array([0])}, key, [plaintext])");
47
48     finishJSTest();
49 });
50 </script>
51
52 <script src="../../resources/js-test-post.js"></script>
53 </body>
54 </html>