createECDH
createECDH copied to clipboard
Unlike Node's own crypto, curve.getPublicKey fails after curve.setPublicKey
Hi all,
I'm running the following code snippet to uncompress an EC public key:
const assert = require('assert')
const crypto = require('crypto')
const pubkey_short = Buffer.from([0x03, 0x4b, 0x6f, 0xde, 0x37, 0x3a, 0x59, 0x1a, 0xa6, 0x05, 0x3e, 0x2b, 0x7c, 0xaa, 0x03, 0x62, 0x58, 0x9d, 0x44, 0x9d, 0xb1, 0xe4, 0x2f, 0xc5, 0x2d, 0xe5, 0xaf, 0xf2, 0x3d, 0xb1, 0xe5, 0xb8, 0x52])
const pubkey_long = Buffer.from([0x04, 0x4b, 0x6f, 0xde, 0x37, 0x3a, 0x59, 0x1a, 0xa6, 0x05, 0x3e, 0x2b, 0x7c, 0xaa, 0x03, 0x62, 0x58, 0x9d, 0x44, 0x9d, 0xb1, 0xe4, 0x2f, 0xc5, 0x2d, 0xe5, 0xaf, 0xf2, 0x3d, 0xb1, 0xe5, 0xb8, 0x52, 0x6c, 0x12, 0xa4, 0xa3, 0xb3, 0xce, 0xf7, 0x1f, 0x21, 0x8f, 0xb3, 0x45, 0xc6, 0x47, 0x15, 0x27, 0x23, 0x52, 0x8c, 0x44, 0x41, 0x78, 0xed, 0xc8, 0xb6, 0x4d, 0xed, 0x4b, 0x24, 0xe2, 0x4b, 0x4b])
const curve = crypto.createECDH('prime256v1')
curve.setPublicKey(pubkey_short)
const pk = curve.getPublicKey(null, 'uncompressed')
assert(pk.equals(pubkey_long))
console.log('It works')
Here, pubkey_short is a compressed public key, and pubkey_long is the same key, uncompressed. In Node.js, this code works, however after using browserify on it the program fails with the following error:
/Users/mvasilkov/c/OpenBalkans/bug_browserify.js:10447
this.keys._importPublic(pub)
^
TypeError: Cannot read property '_importPublic' of undefined
at ECDH.setPublicKey (/Users/mvasilkov/c/OpenBalkans/bug_browserify.js:10447:13)
at Object.<anonymous> (/Users/mvasilkov/c/OpenBalkans/bug_browserify.js:10:7)
at Object.1.assert (/Users/mvasilkov/c/OpenBalkans/bug_browserify.js:15:4)
<cut for brevity>
Empirically I've found the following workaround:
// curve.setPublicKey(pubkey_short)
curve.keys = curve.curve.keyFromPublic(pubkey_short)
Ideally I'd like to resolve it upstream. Would you be interested in a pull request fixing this issue?