summaryrefslogtreecommitdiff
path: root/ext/node/polyfills/internal/crypto
diff options
context:
space:
mode:
authorSatya Rohith <me@satyarohith.com>2024-03-18 13:20:10 +0530
committerGitHub <noreply@github.com>2024-03-18 13:20:10 +0530
commitbecdad531f2b56684133b3b7ea25169c7102f765 (patch)
treee03e3e411fdc888ccd2200bab9aa9721b34c92dc /ext/node/polyfills/internal/crypto
parent9c5ddf7c69f0d3ddaa93b194f0020944569e0e3e (diff)
fix(ext/node): support public key point encoding in ECDH.generateKeys() (#22976)
Towards https://github.com/denoland/deno/issues/22921 Co-authored-by: Divy Srivastava <dj.srivastava23@gmail.com>
Diffstat (limited to 'ext/node/polyfills/internal/crypto')
-rw-r--r--ext/node/polyfills/internal/crypto/diffiehellman.ts8
-rw-r--r--ext/node/polyfills/internal/crypto/types.ts2
-rw-r--r--ext/node/polyfills/internal/crypto/util.ts6
3 files changed, 14 insertions, 2 deletions
diff --git a/ext/node/polyfills/internal/crypto/diffiehellman.ts b/ext/node/polyfills/internal/crypto/diffiehellman.ts
index 4b105e575..da7907734 100644
--- a/ext/node/polyfills/internal/crypto/diffiehellman.ts
+++ b/ext/node/polyfills/internal/crypto/diffiehellman.ts
@@ -1236,12 +1236,18 @@ export class ECDH {
generateKeys(encoding: BinaryToTextEncoding, format?: ECDHKeyFormat): string;
generateKeys(
encoding?: BinaryToTextEncoding,
- _format?: ECDHKeyFormat,
+ format: ECDHKeyFormat = "uncompressed",
): Buffer | string {
+ this.#pubbuf = Buffer.alloc(
+ format.trim() == "compressed"
+ ? this.#curve.publicKeySizeCompressed
+ : this.#curve.publicKeySize,
+ );
op_node_ecdh_generate_keys(
this.#curve.name,
this.#pubbuf,
this.#privbuf,
+ format,
);
if (encoding !== undefined) {
diff --git a/ext/node/polyfills/internal/crypto/types.ts b/ext/node/polyfills/internal/crypto/types.ts
index a6a366348..2b3ce34fe 100644
--- a/ext/node/polyfills/internal/crypto/types.ts
+++ b/ext/node/polyfills/internal/crypto/types.ts
@@ -16,7 +16,7 @@ export type Encoding =
| CharacterEncoding
| LegacyCharacterEncoding;
-export type ECDHKeyFormat = "compressed" | "uncompressed" | "hybrid";
+export type ECDHKeyFormat = "compressed" | "uncompressed";
export type BinaryLike = string | ArrayBufferView;
diff --git a/ext/node/polyfills/internal/crypto/util.ts b/ext/node/polyfills/internal/crypto/util.ts
index a68ac3682..a39b031ee 100644
--- a/ext/node/polyfills/internal/crypto/util.ts
+++ b/ext/node/polyfills/internal/crypto/util.ts
@@ -25,6 +25,7 @@ export type EllipticCurve = {
ephemeral: boolean;
privateKeySize: number;
publicKeySize: number;
+ publicKeySizeCompressed: number;
sharedSecretSize: number;
};
@@ -33,30 +34,35 @@ export const ellipticCurves: Array<EllipticCurve> = [
name: "secp256k1",
privateKeySize: 32,
publicKeySize: 65,
+ publicKeySizeCompressed: 33,
sharedSecretSize: 32,
}, // Weierstrass-class EC used by Bitcoin
{
name: "prime256v1",
privateKeySize: 32,
publicKeySize: 65,
+ publicKeySizeCompressed: 33,
sharedSecretSize: 32,
}, // NIST P-256 EC
{
name: "secp256r1",
privateKeySize: 32,
publicKeySize: 65,
+ publicKeySizeCompressed: 33,
sharedSecretSize: 32,
}, // NIST P-256 EC (same as above)
{
name: "secp384r1",
privateKeySize: 48,
publicKeySize: 97,
+ publicKeySizeCompressed: 49,
sharedSecretSize: 48,
}, // NIST P-384 EC
{
name: "secp224r1",
privateKeySize: 28,
publicKeySize: 57,
+ publicKeySizeCompressed: 29,
sharedSecretSize: 28,
}, // NIST P-224 EC
];