Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ext/crypto): generate ECDH keys #11870

Merged
merged 2 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion ext/crypto/00_crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"RSA-PSS": "RsaHashedKeyGenParams",
"RSA-OAEP": "RsaHashedKeyGenParams",
"ECDSA": "EcKeyGenParams",
"ECDH": "EcKeyGenParams",
"AES-CTR": "AesKeyGenParams",
"AES-CBC": "AesKeyGenParams",
"AES-GCM": "AesKeyGenParams",
Expand Down Expand Up @@ -1418,7 +1419,64 @@
// 17-20.
return { publicKey, privateKey };
}
// TODO(lucacasonato): ECDH
case "ECDH": {
// 1.
if (
ArrayPrototypeFind(
usages,
(u) => !ArrayPrototypeIncludes(["deriveKey", "deriveBits"], u),
) !== undefined
) {
throw new DOMException("Invalid key usages", "SyntaxError");
}

// 2-3.
const handle = {};
if (
ArrayPrototypeIncludes(
supportedNamedCurves,
normalizedAlgorithm.namedCurve,
)
) {
const keyData = await core.opAsync("op_crypto_generate_key", {
name: "ECDH",
namedCurve: normalizedAlgorithm.namedCurve,
});
WeakMapPrototypeSet(KEY_STORE, handle, {
type: "pkcs8",
data: keyData,
});
} else {
throw new DOMException("Curve not supported", "NotSupportedError");
}

// 4-6.
const algorithm = {
name: "ECDH",
namedCurve: normalizedAlgorithm.namedCurve,
};

// 7-11.
const publicKey = constructKey(
"public",
true,
usageIntersection(usages, []),
algorithm,
handle,
);

// 12-16.
const privateKey = constructKey(
"private",
extractable,
usageIntersection(usages, ["deriveKey", "deriveBits"]),
algorithm,
handle,
);

// 17-20.
return { publicKey, privateKey };
}
case "AES-CTR":
case "AES-CBC":
case "AES-GCM": {
Expand Down
2 changes: 1 addition & 1 deletion ext/crypto/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub async fn op_crypto_generate_key(

private_key.to_pkcs8_der()?.as_ref().to_vec()
}
Algorithm::Ecdsa => {
Algorithm::Ecdsa | Algorithm::Ecdh => {
Copy link
Member

@littledivy littledivy Aug 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ECKeyPair is generated using ring::signature::EcdsaKeyPair::generate_pkcs8. Is this the correct way?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The spec says to generate an "Elliptic Curve key pair' for both cases. ECDSA and ECDH are two algorithms for encryption/decryption, and key agreement respectively. They both operate using EC keys though.

let curve: &EcdsaSigningAlgorithm =
args.named_curve.ok_or_else(not_supported)?.into();
let rng = RingRand::SystemRandom::new();
Expand Down
121 changes: 26 additions & 95 deletions tools/wpt/expectation.json
Original file line number Diff line number Diff line change
Expand Up @@ -12312,100 +12312,6 @@
"failures_AES-GCM.https.any.html": true,
"failures_AES-KW.https.any.html": true,
"failures_ECDH.https.any.html": [
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, deriveKey, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, deriveKey, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, deriveKey, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, deriveKey, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, deriveKey, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, deriveKey, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveBits, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-256}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, deriveKey, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, deriveKey, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, deriveKey, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, deriveKey, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, deriveKey, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, deriveKey, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveBits, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-384}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, deriveKey, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, encrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, deriveKey, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, decrypt])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, deriveKey, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, sign])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, deriveKey, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, verify])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, deriveKey, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, wrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, deriveKey, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, unwrapKey])",
"Bad usages: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits, unwrapKey])",
"Empty usages: generateKey({name: ECDH, namedCurve: P-256}, false, [])",
"Empty usages: generateKey({name: ECDH, namedCurve: P-256}, true, [])",
"Empty usages: generateKey({name: ECDH, namedCurve: P-384}, false, [])",
"Empty usages: generateKey({name: ECDH, namedCurve: P-384}, true, [])",
"Empty usages: generateKey({name: ECDH, namedCurve: P-521}, false, [])",
"Empty usages: generateKey({name: ECDH, namedCurve: P-521}, true, [])"
],
Expand All @@ -12422,7 +12328,32 @@
"successes_AES-GCM.https.any.html": true,
"successes_AES-KW.https.any.html": true,
"successes_HMAC.https.any.html": true,
"successes_ECDH.https.any.html": false,
"successes_ECDH.https.any.html": [
"Success: generateKey({name: ECDH, namedCurve: P-521}, false, [deriveKey])",
"Success: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey])",
"Success: generateKey({name: ECDH, namedCurve: P-521}, false, [deriveBits, deriveKey])",
"Success: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits, deriveKey])",
"Success: generateKey({name: ECDH, namedCurve: P-521}, false, [deriveBits])",
"Success: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveBits])",
"Success: generateKey({name: ECDH, namedCurve: P-521}, false, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits])",
"Success: generateKey({name: ECDH, namedCurve: P-521}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits])",
"Success: generateKey({name: ecdh, namedCurve: P-521}, false, [deriveKey])",
"Success: generateKey({name: ecdh, namedCurve: P-521}, true, [deriveKey])",
"Success: generateKey({name: ecdh, namedCurve: P-521}, false, [deriveBits, deriveKey])",
"Success: generateKey({name: ecdh, namedCurve: P-521}, true, [deriveBits, deriveKey])",
"Success: generateKey({name: ecdh, namedCurve: P-521}, false, [deriveBits])",
"Success: generateKey({name: ecdh, namedCurve: P-521}, true, [deriveBits])",
"Success: generateKey({name: ecdh, namedCurve: P-521}, false, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits])",
"Success: generateKey({name: ecdh, namedCurve: P-521}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits])",
"Success: generateKey({name: Ecdh, namedCurve: P-521}, false, [deriveKey])",
"Success: generateKey({name: Ecdh, namedCurve: P-521}, true, [deriveKey])",
"Success: generateKey({name: Ecdh, namedCurve: P-521}, false, [deriveBits, deriveKey])",
"Success: generateKey({name: Ecdh, namedCurve: P-521}, true, [deriveBits, deriveKey])",
"Success: generateKey({name: Ecdh, namedCurve: P-521}, false, [deriveBits])",
"Success: generateKey({name: Ecdh, namedCurve: P-521}, true, [deriveBits])",
"Success: generateKey({name: Ecdh, namedCurve: P-521}, false, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits])",
"Success: generateKey({name: Ecdh, namedCurve: P-521}, true, [deriveKey, deriveBits, deriveKey, deriveBits, deriveKey, deriveBits])"
],
"successes_ECDSA.https.any.html": [
"Success: generateKey({name: ECDSA, namedCurve: P-521}, false, [sign])",
"Success: generateKey({name: ECDSA, namedCurve: P-521}, true, [sign])",
Expand Down