aes-cbc-import-jwk.html 1.62 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<!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 importing a JWK key for AES-CBC.");

jsTestIsAsync = true;

var extractable = true;

ap@apple.com's avatar
ap@apple.com committed
18 19
var iv = hexStringToUint8Array("000102030405060708090a0b0c0d0e0f");
var plaintext = hexStringToUint8Array("6bc1bee22e409f96e93d7e117393172aae2d8a571e03ac9c9eb76fac45af8e5130c81c46a35ce411e5fbc1191a0a52eff69f2445df4f9b17ad2b417be66c3710");
20 21 22 23 24

var jwkKey = {
    "kty": "oct",
    "alg": "A192CBC",
    "use": "enc",
25
    "ext": true,
26 27 28
    "k": "jnOw99oOZFLIEPMrgJB55WL46tJSLGt7"
};

ap@apple.com's avatar
ap@apple.com committed
29
var jwkKeyAsArrayBuffer = asciiToUint8Array(JSON.stringify(jwkKey));
30 31 32 33 34 35 36

debug("Importing a key...\n");
crypto.subtle.importKey("jwk", jwkKeyAsArrayBuffer, null, false, ["encrypt"]).then(function(result) {
    key = result;

    shouldBe("key.type", "'secret'");
    shouldBe("key.extractable", "false");
37
    shouldBe("key.algorithm.name", "'AES-CBC'");
38 39 40 41
    shouldBe("key.algorithm.length", "192");
    shouldBe("key.usages", '["encrypt"]');

    debug("\nUsing the key to encrypt plaintext...");
42
    return crypto.subtle.encrypt({name: "aes-cbc", iv: iv}, key, plaintext);
43 44
}).then(function(result) {
    encryptionResult = result;
ap@apple.com's avatar
ap@apple.com committed
45
    shouldBe("bytesToHexString(new Uint8Array(encryptionResult))", "'4f021db243bc633d7178183a9fa071e8b4d9ada9ad7dedf4e5e738763f69145a571b242012fb7ae07fa9baac3df102e008b0e27988598881d920a9e64f5615cd612ccd79224b350935d45dd6a98f8176'");
46 47 48 49 50 51 52
    finishJSTest();
});
</script>

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