aes-cbc-encrypt-decrypt.html 2.16 KB
Newer Older
ap@apple.com's avatar
ap@apple.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<!DOCTYPE html>
<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test AES-CBC encrypt and decrypt functions.");

jsTestIsAsync = true;

ap@apple.com's avatar
ap@apple.com committed
16 17 18
var keyData = hexStringToUint8Array("2b7e151628aed2a6abf7158809cf4f3c");
var iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f");
var plaintext = hexStringToUint8Array("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
ap@apple.com's avatar
ap@apple.com committed
19 20 21 22 23 24 25
var extractable = true;

debug("Importing a raw AES key from string literal...");
crypto.subtle.importKey("raw", keyData, "aes-cbc", extractable, ["encrypt", "decrypt"]).then(function(result) {
    key = result;
    shouldBe("key.type", "'secret'");
    shouldBe("key.extractable", "true");
26
    shouldBe("key.algorithm.name", "'AES-CBC'");
ap@apple.com's avatar
ap@apple.com committed
27
    shouldBe("key.algorithm.length", "128");
28
    shouldBe("key.usages", "['decrypt', 'encrypt']");
ap@apple.com's avatar
ap@apple.com committed
29 30

    debug("Using the key to encrypt plaintext...");
31
    return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, plaintext);
ap@apple.com's avatar
ap@apple.com committed
32 33
}).then(function(result) {
    encryptionResult = result;
ap@apple.com's avatar
ap@apple.com committed
34
    shouldBe("bytesToHexString(new Uint8Array(encryptionResult))", "'7649abac8119b246cee98e9b12e9197d5086cb9b507219ee95db113a917678b273bed6b8e3c1743b7116e69e222295163ff1caa1681fac09120eca307586e1a78cb82807230e1321d3fae00d18cc2012'");
ap@apple.com's avatar
ap@apple.com committed
35 36

    debug("Decrypting it back...");
37
    return crypto.subtle.decrypt({name: "aes-cbc", iv: iv}, key, result);
ap@apple.com's avatar
ap@apple.com committed
38 39 40 41 42
}).then(function(result) {
    decryptionResult = result;
    shouldBe("new Uint8Array(decryptionResult)", "plaintext");

    debug("Testing initialization vector bindings...");
43 44 45 46
    shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: null}, key, plaintext)");
    shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC'}, key, plaintext)");
    shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: 3}, key, plaintext)");
    shouldThrow("crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array([0])}, key, plaintext)");
ap@apple.com's avatar
ap@apple.com committed
47 48 49 50 51 52 53 54

    finishJSTest();
});
</script>

<script src="../../resources/js-test-post.js"></script>
</body>
</html>