// -----------------------------------------------------------------------------
TestArray.addTest( "Generate an ECDH key and derive some bits",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
var pair;
function setKeyPair(x) { pair = x; }
function doDerive(n) {
return function() {
return crypto.subtle.deriveBits({ name: "ECDH", public: pair.publicKey }, pair.privateKey, n * 8);
};
}
crypto.subtle.generateKey(alg, false, ["deriveBits"])
.then(setKeyPair, error(that))
.then(doDerive(2), error(that))
.then(function(x) {
// Deriving less bytes works.
if (x.byteLength != 2) {
throw new Error("should have derived two bytes");
}
})
// Deriving more than the curve yields doesn't.
.then(doDerive(33), error(that))
.then(error(that), complete(that));
}
);
// -----------------------------------------------------------------------------
TestArray.addTest( "Test that ECDH deriveBits() fails when the public key is not an ECDH key",
function() { var that = this; var pubKey, privKey;
function setPub(x) { pubKey = x.publicKey; }
function setPriv(x) { privKey = x.privateKey; }
function doGenerateP256() { var alg = { name: "ECDH", namedCurve: "P-256" };
return crypto.subtle.generateKey(alg, false, ["deriveBits"]);
}
function doGenerateRSA() { var alg = {
name: "RSA-OAEP",
hash: "SHA-256",
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
};
return crypto.subtle.generateKey(alg, false, ["encrypt", "decrypt"]);
}
function doDerive() { var alg = { name: "ECDH", public: pubKey };
return crypto.subtle.deriveBits(alg, privKey, 16);
}
// -----------------------------------------------------------------------------
TestArray.addTest( "Test that ECDH deriveBits() fails when the given keys' curves don't match",
function() { var that = this; var pubKey, privKey;
function setPub(x) { pubKey = x.publicKey; }
function setPriv(x) { privKey = x.privateKey; }
function doGenerateP256() { var alg = { name: "ECDH", namedCurve: "P-256" };
return crypto.subtle.generateKey(alg, false, ["deriveBits"]);
}
function doGenerateP384() { var alg = { name: "ECDH", namedCurve: "P-384" };
return crypto.subtle.generateKey(alg, false, ["deriveBits"]);
}
function doDerive() { var alg = { name: "ECDH", public: pubKey };
return crypto.subtle.deriveBits(alg, privKey, 16);
}
// -----------------------------------------------------------------------------
TestArray.addTest( "Verify that ECDH import fails with a key with a mismatched 'crv' field",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-521"};
// -----------------------------------------------------------------------------
TestArray.addTest( "JWK import an ECDH public and private key and derive bits (P-256)",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
var pubKey, privKey;
function setPub(x) { pubKey = x; }
function setPriv(x) { privKey = x; }
// -----------------------------------------------------------------------------
TestArray.addTest( "JWK import an ECDH public and private key and derive bits (P-384)",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-384"};
var pubKey, privKey;
function setPub(x) { pubKey = x; }
function setPriv(x) { privKey = x; }
// -----------------------------------------------------------------------------
TestArray.addTest( "JWK import an ECDH public and private key and derive bits (P-521)",
function() { var that = this; var alg = { name: "ECDH", namedCurve : "P-521" };
var pubKey, privKey;
function setPub(x) { pubKey = x; }
function setPriv(x) { privKey = x; }
// -----------------------------------------------------------------------------
TestArray.addTest( "Test that importing bad JWKs fails",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" }; var tvs = tv.ecdh_p256_negative;
// -----------------------------------------------------------------------------
TestArray.addTest( "Derive an HMAC key from two ECDH keys and test sign/verify",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-521" }; var algDerived = { name: "HMAC", hash: {name: "SHA-1"} };
var pubKey, privKey;
function setPub(x) { pubKey = x; }
function setPriv(x) { privKey = x; }
function doDerive() {
return crypto.subtle.deriveKey({ name: "ECDH", public: pubKey }, privKey, algDerived, false, ["sign", "verify"])
.then(function(x) {
if (!hasKeyFields(x)) {
throw new Error("Invalid key; missing field(s)");
}
// 512 bit is the default for HMAC-SHA1.
if (x.algorithm.length != 512) {
throw new Error("Invalid key; incorrect length");
}
return x;
});
}
function doSignAndVerify(x) { var data = crypto.getRandomValues(new Uint8Array(1024));
return crypto.subtle.sign("HMAC", x, data)
.then(function(sig) {
return crypto.subtle.verify("HMAC", x, sig, data);
});
}
// -----------------------------------------------------------------------------
TestArray.addTest( "Derive an HKDF key from two ECDH keys and derive an HMAC key from that",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
async function doTest() {
let privKey = await crypto.subtle.importKey("jwk", tv.ecdh_p256.jwk_priv, alg, false, ["deriveKey"]);
let pubKey = await crypto.subtle.importKey("jwk", tv.ecdh_p256.jwk_pub, alg, false, []);
let ecdhAlg = { name: "ECDH", public: pubKey };
let hkdfAlg = { name: "HKDF", hash: "SHA-256", salt: new Uint8Array(), info: new Uint8Array() };
let hkdfKey = await crypto.subtle.deriveKey(ecdhAlg, privKey, hkdfAlg, false, ["deriveKey"]);
let hmacAlg = { name: "HMAC", hash: "SHA-256" };
let hmacKey = await crypto.subtle.deriveKey(hkdfAlg, hkdfKey, hmacAlg, false, ["sign"]);
return crypto.subtle.sign("HMAC", hmacKey, new Uint8Array());
}
const expected = util.hex2abv("acf62832fa93469824cd997593bc963b28a68e6f73f4516bbe51b35942fe9811");
doTest().then(memcmp_complete(that, expected), error(that));
}
);
// -----------------------------------------------------------------------------
TestArray.addTest( "SPKI import/export of public ECDH keys (P-256)",
function() { var that = this; var alg = { name: "ECDH" }; var keys = ["spki", "spki_id_ecpk"];
function doImport(key) {
return crypto.subtle.importKey("spki", tv.ecdh_p256[key], alg, true, []);
}
function doExport(x) {
return crypto.subtle.exportKey("spki", x);
}
function nextKey() { var key = keys.shift(); var imported = doImport(key); var derived = imported.then(doExport);
return derived.then(function(x) {
if (!util.memcmp(x, tv.ecdh_p256.spki_id_ecpk)) {
throw new Error("exported key is invalid");
}
if (keys.length) {
return nextKey();
}
return Promise.resolve();
});
}
nextKey().then(complete(that), error(that));
}
);
// -----------------------------------------------------------------------------
TestArray.addTest( "SPKI/JWK import ECDH keys (P-256) and derive a known secret",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
var pubKey, privKey;
function setPub(x) { pubKey = x; }
function setPriv(x) { privKey = x; }
// -----------------------------------------------------------------------------
TestArray.addTest( "Raw import/export of a public ECDH key (P-256)",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
function doExport(x) {
return crypto.subtle.exportKey("raw", x);
}
// -----------------------------------------------------------------------------
TestArray.addTest( "Test that importing bad raw ECDH keys fails",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
// -----------------------------------------------------------------------------
TestArray.addTest( "Test that importing ECDH keys with an unknown format fails",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
// -----------------------------------------------------------------------------
TestArray.addTest( "Test that importing too short raw ECDH keys fails",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
// -----------------------------------------------------------------------------
TestArray.addTest( "Test that importing too long raw ECDH keys fails",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
// -----------------------------------------------------------------------------
TestArray.addTest( "RAW/JWK import ECDH keys (P-256) and derive a known secret",
function() { var that = this; var alg = { name: "ECDH", namedCurve: "P-256" };
var pubKey, privKey;
function setPub(x) { pubKey = x; }
function setPriv(x) { privKey = x; }
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.