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: replace elliptic with noble curves #42

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
76 changes: 65 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"typescript": "^5.5.3"
},
"dependencies": {
"elliptic": "^6.5.5"
"@noble/curves": "^1.6.0"
Copy link
Member

Choose a reason for hiding this comment

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

if you only need secp256k1, you should be able to use @noble/secp256k1 instead.

},
"engines": {
"node": ">=18.x",
Expand Down
43 changes: 20 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ec as EC } from "elliptic";

const ec = new EC("secp256k1");
import { secp256k1 } from "@noble/curves/secp256k1";
// eslint-disable-next-line @typescript-eslint/no-explicit-any, n/no-unsupported-features/node-builtins
const browserCrypto = globalThis.crypto || (globalThis as any).msCrypto || {};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -43,7 +41,7 @@
}
let res = 0;
for (let i = 0; i < b1.length; i++) {
res |= b1[i] ^ b2[i]; // jshint ignore:line

Check warning on line 44 in src/index.ts

View workflow job for this annotation

GitHub Actions / run tests (20.x, ubuntu-latest)

Generic Object Injection Sink

Check warning on line 44 in src/index.ts

View workflow job for this annotation

GitHub Actions / run tests (20.x, ubuntu-latest)

Generic Object Injection Sink
}

return res === 0;
Expand Down Expand Up @@ -76,7 +74,7 @@

function getAes(op: "encrypt" | "decrypt"): AesFunctionType {
return async function (iv: Buffer, key: Buffer, data: Buffer) {
if (subtle && subtle[op] && subtle.importKey) {

Check warning on line 77 in src/index.ts

View workflow job for this annotation

GitHub Actions / run tests (20.x, ubuntu-latest)

Generic Object Injection Sink
const importAlgorithm = {
name: "AES-CBC",
};
Expand All @@ -86,7 +84,7 @@
iv,
};
// encrypt and decrypt ops are not implemented in react-native-quick-crypto yet.
const result = await subtle[op](encAlgorithm, cryptoKey, data);

Check warning on line 87 in src/index.ts

View workflow job for this annotation

GitHub Actions / run tests (20.x, ubuntu-latest)

Function Call Object Injection Sink
return Buffer.from(new Uint8Array(result));
} else if (op === "encrypt" && browserCrypto.createCipheriv) {
// This is available if crypto is polyfilled in react native environment
Expand Down Expand Up @@ -147,7 +145,7 @@
assert(isValidPrivateKey(privateKey), "Bad private key");
// XXX(Kagami): `elliptic.utils.encode` returns array for every
// encoding except `hex`.
return Buffer.from(ec.keyFromPrivate(privateKey).getPublic("array"));
return Buffer.from(secp256k1.getPublicKey(privateKey, false));
};

/**
Expand All @@ -159,7 +157,7 @@
assert(isValidPrivateKey(privateKey), "Bad private key");
// See https://github.com/wanderer/secp256k1-node/issues/46
const compressed = true;
return Buffer.from(ec.keyFromPrivate(privateKey).getPublic(compressed, "array"));
return Buffer.from(secp256k1.getPublicKey(privateKey, compressed));
};

// NOTE(Kagami): We don't use promise shim in Browser implementation
Expand All @@ -172,13 +170,7 @@
assert(isValidPrivateKey(privateKey), "Bad private key");
assert(msg.length > 0, "Message should not be empty");
assert(msg.length <= 32, "Message is too long");
return Buffer.from(
ec
.sign(msg, privateKey, {
canonical: true,
})
.toDER()
);
return Buffer.from(secp256k1.sign(msg, privateKey).toDERRawBytes());
};

export const verify = async function (publicKey: Buffer, msg: Buffer, sig: Buffer): Promise<null> {
Expand All @@ -191,9 +183,7 @@
}
assert(msg.length > 0, "Message should not be empty");
assert(msg.length <= 32, "Message is too long");
if (ec.verify(msg, sig, publicKey)) {
return null;
}
if (secp256k1.verify(sig, msg, publicKey)) return null;
throw new Error("Bad signature");
};

Expand All @@ -209,10 +199,19 @@
if (publicKeyB.length === 33) {
assert(publicKeyB[0] === 2 || publicKeyB[0] === 3, "Bad public key");
}
const keyA = ec.keyFromPrivate(privateKeyA);
const keyB = ec.keyFromPublic(publicKeyB);
const Px = keyA.derive(keyB.getPublic()); // BN instance
return Buffer.from(Px.toArray());

// unpad to match previous implementation
// elliptic return BN and we return Buffer(BN.toArray())
// match by unpadding
const sharedSecret = secp256k1.getSharedSecret(privateKeyA, publicKeyB);
const Px = sharedSecret.subarray(sharedSecret.length - 32);

let i = 0;
while (i < Px.length && Px[i] === 0) {

Check warning on line 210 in src/index.ts

View workflow job for this annotation

GitHub Actions / run tests (20.x, ubuntu-latest)

Generic Object Injection Sink
i++;
}

return Buffer.from(Px).subarray(i);
};

export const deriveUnpadded = derive;
Expand All @@ -229,10 +228,8 @@
if (publicKeyB.length === 33) {
assert(publicKeyB[0] === 2 || publicKeyB[0] === 3, "Bad public key");
}
const keyA = ec.keyFromPrivate(privateKeyA);
const keyB = ec.keyFromPublic(publicKeyB);
const Px = keyA.derive(keyB.getPublic()); // BN instance
return Buffer.from(Px.toString(16, 64), "hex");
const Px = secp256k1.getSharedSecret(privateKeyA, publicKeyB);
return Buffer.from(Px).subarray(Px.length - 32);
};

export const encrypt = async function (publicKeyTo: Buffer, msg: Buffer, opts?: { iv?: Buffer; ephemPrivateKey?: Buffer }): Promise<Ecies> {
Expand Down
Loading