rsa-oaep-wrap-unwrap-aes.html 5.25 KB
Newer Older
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
<!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 AES keys with RSA-OAEP.");

jsTestIsAsync = true;

var publicKeyJSON = {
    kty: "RSA",
    alg: "RSA-OAEP",
    n: "rcCUCv7Oc1HVam1DIhCzqknThWawOp8QLk8Ziy2p10ByjQFCajoFiyuAWl-R1WXZaf4xitLRracT9agpzIzc-MbLSHIGgWQGO21lGiImy5ftZ-D8bHAqRz2y15pzD4c4CEou7XSSLDoRnR0QG5MsDhD6s2gV9mwHkrtkCxtMWdBi-77as8wGmlNRldcOSgZDLK8UnCSgA1OguZ989bFyc8tOOEIb0xUSfPSz3LPSCnyYz68aDjmKVeNH-ig857OScyWbGyEy3Biw64qun3juUlNWsJ3zngkOdteYWytx5Qr4XKNs6R-Myyq72KUp02mJDZiiyiglxML_i3-_CeecCw",
    e: "AQAB"
};

var privateKeyJSON = {
    kty: "RSA",
    alg: "RSA-OAEP",
    n: "rcCUCv7Oc1HVam1DIhCzqknThWawOp8QLk8Ziy2p10ByjQFCajoFiyuAWl-R1WXZaf4xitLRracT9agpzIzc-MbLSHIGgWQGO21lGiImy5ftZ-D8bHAqRz2y15pzD4c4CEou7XSSLDoRnR0QG5MsDhD6s2gV9mwHkrtkCxtMWdBi-77as8wGmlNRldcOSgZDLK8UnCSgA1OguZ989bFyc8tOOEIb0xUSfPSz3LPSCnyYz68aDjmKVeNH-ig857OScyWbGyEy3Biw64qun3juUlNWsJ3zngkOdteYWytx5Qr4XKNs6R-Myyq72KUp02mJDZiiyiglxML_i3-_CeecCw",
    e: "A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wmnytqNcrMCEwTm7ByA2eU6nWbQrULvf7m9_kzfLUcjsnpAVlBQG5JMXMy0Sq4ptwbywsa5-G8KAOOOR2L3v4hC-Eys9ftgFM_3i0o40eeQH4b3haPbntrIeMg8IzlOuVYKf9-2QuKDoWeRdd7NsdxTk"
};
var wrapAlgorithm = {name: "RSA-OAEP", hash: "sha-1"};
var extractable = true;
var nonExtractable = false;

debug("Importing RSA keys...");
crypto.subtle.importKey("jwk", asciiToUint8Array(JSON.stringify(publicKeyJSON)), null, extractable, ["wrapKey", "unwrapKey"]).then(function(result) {
    publicKey = result;
    return crypto.subtle.importKey("jwk", asciiToUint8Array(JSON.stringify(privateKeyJSON)), null, extractable, ["wrapKey", "unwrapKey"]);
}).then(function(result) {
    privateKey = result;

    debug("Importing an AES key...");
    aesKeyData = hexStringToUint8Array("603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4");
    return crypto.subtle.importKey("raw", aesKeyData, "aes-cbc", extractable, ["encrypt", "decrypt", "wrapKey", "unwrapKey"])
}).then(function(result) {
    aesKey = result;
    debug("Wrapping it...");

    return crypto.subtle.wrapKey("raw", aesKey, publicKey, wrapAlgorithm);
}).then(function(result) {
    wrappedKey = result;
    shouldBe("wrappedKey.toString()", "'[object ArrayBuffer]'");

    debug("Unwrapping it...");
    return crypto.subtle.unwrapKey("raw", wrappedKey, privateKey, wrapAlgorithm, "aes-cbc", extractable, ["encrypt", "decrypt"]);
}).then(function(result) {
    unwrappedKey = result;
    shouldBe("unwrappedKey.toString()", "'[object Key]'");
    shouldBe("unwrappedKey.type", "'secret'");
    shouldBe("unwrappedKey.extractable", "true");
65
    shouldBe("unwrappedKey.algorithm.name", "'AES-CBC'");
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    shouldBe("unwrappedKey.algorithm.length", "256");
    shouldBe("unwrappedKey.usages", "['encrypt', 'decrypt']");

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

    debug("\nWrapping the same key as JWK...");
    return crypto.subtle.wrapKey("jwk", aesKey, publicKey, wrapAlgorithm);
}).then(function(result) {
    wrappedKey = result;
    shouldBe("wrappedKey.toString()", "'[object ArrayBuffer]'");

    debug("Unwrapping it...");
    return crypto.subtle.unwrapKey("jwk", wrappedKey, privateKey, wrapAlgorithm, "aes-cbc", extractable, ["encrypt", "decrypt"]);
}).then(function(result) {
    unwrappedKey = result;
    shouldBe("unwrappedKey.toString()", "'[object Key]'");
    shouldBe("unwrappedKey.type", "'secret'");
    shouldBe("unwrappedKey.extractable", "true");
88
    shouldBe("unwrappedKey.algorithm.name", "'AES-CBC'");
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    shouldBe("unwrappedKey.algorithm.length", "256");
    shouldBe("unwrappedKey.usages", "['encrypt', 'decrypt']");

    debug("Exporting it...");
    return crypto.subtle.exportKey("raw", unwrappedKey);
}).then(function(result) {
    unwrappedKeyData = result;
    shouldBe("bytesToHexString(unwrappedKeyData)", "bytesToHexString(aesKeyData)");
    finishJSTest();
});
</script>

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