summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
Diffstat (limited to 'ext')
-rw-r--r--ext/crypto/00_crypto.js60
-rw-r--r--ext/crypto/lib.rs2
2 files changed, 60 insertions, 2 deletions
diff --git a/ext/crypto/00_crypto.js b/ext/crypto/00_crypto.js
index 68a8e4f9f..b3131a4f8 100644
--- a/ext/crypto/00_crypto.js
+++ b/ext/crypto/00_crypto.js
@@ -82,6 +82,7 @@
"RSA-PSS": "RsaHashedKeyGenParams",
"RSA-OAEP": "RsaHashedKeyGenParams",
"ECDSA": "EcKeyGenParams",
+ "ECDH": "EcKeyGenParams",
"AES-CTR": "AesKeyGenParams",
"AES-CBC": "AesKeyGenParams",
"AES-GCM": "AesKeyGenParams",
@@ -1575,7 +1576,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": {
diff --git a/ext/crypto/lib.rs b/ext/crypto/lib.rs
index cf2c379a0..47137b210 100644
--- a/ext/crypto/lib.rs
+++ b/ext/crypto/lib.rs
@@ -168,7 +168,7 @@ pub async fn op_crypto_generate_key(
private_key.to_pkcs1_der()?.as_ref().to_vec()
}
- Algorithm::Ecdsa => {
+ Algorithm::Ecdsa | Algorithm::Ecdh => {
let curve: &EcdsaSigningAlgorithm =
args.named_curve.ok_or_else(not_supported)?.into();
let rng = RingRand::SystemRandom::new();