aes-kw-wrap-unwrap-aes.html 2.93 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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
<!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 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, "aes-kw", extractable, ["wrapKey", "unwrapKey"]).then(function(result) {
    kek = result;

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

    debug("Wrapping it...");
    return crypto.subtle.wrapKey("raw", key, kek, "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, "aes-kw", "aes-cbc", extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]);
}).then(function(result) {
    unwrappedKey = result;
    shouldBe("unwrappedKey.toString()", "'[object Key]'");
    shouldBe("unwrappedKey.type", "'secret'");
    shouldBe("unwrappedKey.extractable", "true");
42
    shouldBe("unwrappedKey.algorithm.name", "'AES-CBC'");
ap@apple.com's avatar
ap@apple.com committed
43
    shouldBe("unwrappedKey.algorithm.length", "128");
44
    shouldBe("unwrappedKey.usages", "['decrypt', 'encrypt', 'unwrapKey', 'wrapKey']");
ap@apple.com's avatar
ap@apple.com committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

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

    debug("\nWrapping it as JWK...");
    return crypto.subtle.wrapKey("jwk", key, kek, "aes-kw");
}).then(function(result) {
    wrappedKey = result;

    debug("Unwrapping it...");
    return crypto.subtle.unwrapKey("jwk", wrappedKey, kek, "aes-kw", "aes-cbc", extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"]);
}).then(function(result) {
    unwrappedKey = result;
    shouldBe("unwrappedKey.toString()", "'[object Key]'");
    shouldBe("unwrappedKey.type", "'secret'");
    shouldBe("unwrappedKey.extractable", "true");
64
    shouldBe("unwrappedKey.algorithm.name", "'AES-CBC'");
ap@apple.com's avatar
ap@apple.com committed
65
    shouldBe("unwrappedKey.algorithm.length", "128");
66
    shouldBe("unwrappedKey.usages", "['decrypt', 'encrypt', 'unwrapKey', 'wrapKey']");
ap@apple.com's avatar
ap@apple.com committed
67 68 69 70 71 72 73 74 75 76 77 78 79 80

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

    finishJSTest();
});
</script>

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