chromium/third_party/blink/web_tests/crypto/subtle/ecdh/import-export-raw.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 importing and exporting an EC public key in raw format.");

jsTestIsAsync = true;

// P-256 key in uncompressed form
var kUncompressedHex = "044EA34391AA73885454BC45DF3FDCC4A70262FA4621FFE25B5790590C340A4BD9265EF2B3F9A86E2959A960D90323465D60CD4A90D314C5DE3F869AD0D4BF6C10";

// The same key as above but in compressed form.
var kCompressedHex = "024ea34391aa73885454bc45df3fdcc4a70262fa4621ffe25b5790590c340a4bd9";

var algorithm = {name: "ECDH", namedCurve: "P-256"};
var extractable = true;

debug("Importing raw (uncompressed) public key...");
crypto.subtle.importKey("raw", hexStringToUint8Array(kUncompressedHex), algorithm, extractable, []).then(function(result) {
    publicKey = result;
    shouldBe("publicKey.toString()", "'[object CryptoKey]'");
    shouldBe("publicKey.type", "'public'");
    shouldBe("publicKey.usages", "[]");
    shouldBe("publicKey.algorithm.name", "'ECDH'");
    shouldBe("publicKey.algorithm.namedCurve", "'P-256'");

    debug("Exporting to raw...");
    return crypto.subtle.exportKey("raw", publicKey);
}).then(function(result) {
    bytesShouldMatchHexString("Exported to raw", kUncompressedHex, result)

    debug("Importing raw (compressed) public key...");
    return crypto.subtle.importKey("raw", hexStringToUint8Array(kCompressedHex), algorithm, extractable, []);
}).then(function(result) {
    publicKey = result;
    shouldBe("publicKey.toString()", "'[object CryptoKey]'");
    shouldBe("publicKey.type", "'public'");
    shouldBe("publicKey.usages", "[]");
    shouldBe("publicKey.algorithm.name", "'ECDH'");
    shouldBe("publicKey.algorithm.namedCurve", "'P-256'");

    debug("Exporting to raw...");
    return crypto.subtle.exportKey("raw", publicKey);
}).then(function(result) {
    bytesShouldMatchHexString("Exported to raw", kUncompressedHex, result)

    debug("Importing invalid raw public key...");
    return crypto.subtle.importKey("raw", hexStringToUint8Array("040708"), algorithm, extractable, []);
}).then(failAndFinishJSTest, function(result) {
    logError(result);
}).then(finishJSTest, failAndFinishJSTest);
</script>

</body>
</html>