chromium/third_party/blink/web_tests/crypto/subtle/aes-kw/wrap-unwrap-aes.html

<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
<script src="../resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>

<script>
description("Test wrapping and unwrapping keys with AES-KW.");

jsTestIsAsync = true;

var kekData = hexStringToUint8Array("000102030405060708090A0B0C0D0E0F");
var keyData = hexStringToUint8Array("00112233445566778899AABBCCDDEEFF");
var extractable = true;

debug("Importing key encryption key...");
crypto.subtle.importKey("raw", kekData, {name: "aes-kw"}, extractable, ["wrapKey", "unwrapKey"]).then(function(result) {
    kek = result;

    debug("Importing a key to be wrapped...");
    return crypto.subtle.importKey("raw", keyData, {name: "aes-cbc"}, extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]);
}).then(function(result) {
    key = result;

    debug("Wrapping it...");
    return crypto.subtle.wrapKey("raw", key, kek, {name: "aes-kw"});
}).then(function(result) {
    wrappedKey = result;
    shouldBe("bytesToHexString(wrappedKey)", "'1fa68b0a8112b447aef34bd8fb5a7b829d3e862371d2cfe5'"); // Result from RFC 3394.

    debug("Unwrapping it...");
    return crypto.subtle.unwrapKey("raw", wrappedKey, kek, {name: "aes-kw"}, {name: "aes-cbc"}, extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]);
}).then(function(result) {
    unwrappedKey = result;
    shouldBe("unwrappedKey.toString()", "'[object CryptoKey]'");
    shouldBe("unwrappedKey.type", "'secret'");
    shouldBe("unwrappedKey.extractable", "true");
    shouldBe("unwrappedKey.algorithm.name", "'AES-CBC'");
    shouldBe("unwrappedKey.algorithm.length", "128");
    shouldBe("unwrappedKey.usages", "['encrypt', 'decrypt', 'wrapKey', 'unwrapKey']");

    debug("Exporting it...");
    return crypto.subtle.exportKey("raw", unwrappedKey);
}).then(function(result) {
    unwrappedKeyData = result;
    shouldBe("bytesToHexString(unwrappedKeyData)", "bytesToHexString(keyData)");

    finishJSTest();
});
</script>

</body>
</html>