Skip to content

Commit

Permalink
fix: unpad sharedsecret to match prev elliptic
Browse files Browse the repository at this point in the history
  • Loading branch information
ieow committed Sep 25, 2024
1 parent 37c254a commit 83e0c72
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,19 @@ export const derive = async function (privateKeyA: Buffer, publicKeyB: Buffer):
if (publicKeyB.length === 33) {
assert(publicKeyB[0] === 2 || publicKeyB[0] === 3, "Bad public key");
}
// should we unpadde it?
const Px = secp256k1.getSharedSecret(privateKeyA, publicKeyB);
return Buffer.from(Px).subarray(Px.length - 32);

// 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 Down

0 comments on commit 83e0c72

Please sign in to comment.