You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ecdh.setPublicKey is actually useful and should be undeprecated #18977
So ecdh.setPublicKey has been deprecated since v5.2.0, but there is a certain use case where it is very useful: when you need to uncompress a compressed public key.
Here's how I would uncompress such a public key:
constcrypto=require('crypto');letecdh=crypto.createECDH('secp256k1');ecdh.generateKeys();letprivate_key_compressed=ecdh.getPrivateKey(null,'compressed');letpublic_key_compressed=ecdh.getPublicKey(null,'compressed');// Public key is a buffer of 33 bytesconsole.log('Compressed public key:',public_key_compressed.length,public_key_compressed);// Create a new ecdh objectecdh=crypto.createECDH('secp256k1')// Use the compressed public key to set the public keyecdh.setPublicKey(public_key_compressed);// Get the uncompressed getletpublic_key_uncompressed=ecdh.getPublicKey();// The returned key is a buffer of 66 bytes longconsole.log('Uncomrpessed public key:',public_key_uncompressed.length,public_key_uncompressed);
If ecdh.setPublicKey where to disappear I would require another library just for this simple task.
Ah well, this is related to my comment on issue #18147 "Signing with a ecdh type private key":
Since crypto's Sign#sign method needs a valid ASN.1 wrapper, but currently there is no built-in way of generating this, I'm using the jsrsasign module to do the signing, and that requires an uncompressed key.
But yeah, an new method for converting the compressed key would be better :)
@wuweiweiwu I'd make it a static method (i.e. on ECDH, not ECDH.prototype) and you should name it something like convertKey() because the conversion goes both ways.
(Three ways actually because node can also convert to a hybrid format.)
Just a question about converting buffer to point. Currently I have ConvertKey in node_crypto.cc as a static method. I was planning on using ECDH::BufferToPoint to convert the input key to EC_POINT However that function is a protected function in ECDH class.
Should I declare ConvertKey as protected function of ECDH as well and then just bind it as a static method in diffiehellman.js or should I figure out a way without using the ECDH member functions since ConvertKey is going to be a static method?
Perhaps using EC_GROUP_new_by_curve_name(nid) and have the user input the ECDH curve name associated with the key?
I'd probably move the shared logic into a static ECDH member function. It doesn't matter if it's not a member, as long as there isn't code duplication.
Activity
MylesBorins commentedon Feb 25, 2018
/cc @nodejs/crypto
bnoordhuis commentedon Feb 26, 2018
Technically correct but it's probably better to add an explicit conversion method instead of undeprecating
ECDH#setPublicKey().@skerit When does this come up? I'd expect most applications to be agnostic to the point format.
skerit commentedon Feb 26, 2018
Ah well, this is related to my comment on issue #18147 "Signing with a ecdh type private key":
Since
crypto'sSign#signmethod needs a valid ASN.1 wrapper, but currently there is no built-in way of generating this, I'm using the jsrsasign module to do the signing, and that requires an uncompressed key.But yeah, an new method for converting the compressed key would be better :)
bnoordhuis commentedon Feb 26, 2018
Okay, I've added labels. Pull request welcome and happy to answer questions.
wuweiweiwu commentedon Feb 27, 2018
@bnoordhuis Can I pick this up? I think it'll be a good first issue :)
bnoordhuis commentedon Feb 27, 2018
@wuweiweiwu Sure thing.
wuweiweiwu commentedon Feb 28, 2018
@bnoordhuis I was thinking of adding another method
uncompressKeyin theECDHclass in https://github.com/nodejs/node/blob/master/src/node_crypto.ccIs that a good place to start?
Thank you
bnoordhuis commentedon Mar 1, 2018
@wuweiweiwu I'd make it a static method (i.e. on
ECDH, notECDH.prototype) and you should name it something likeconvertKey()because the conversion goes both ways.(Three ways actually because node can also convert to a hybrid format.)
wuweiweiwu commentedon Mar 1, 2018
@bnoordhuis sounds good! I will work on the compress and uncompress. Where can I find more information on the hybrid format?
And is it ok if I put the tests in
test/parallel?bnoordhuis commentedon Mar 1, 2018
It's the
'hybrid'option toECDH.prototype.getPublicKey.Yep.
wuweiweiwu commentedon Mar 1, 2018
Just a question about converting buffer to point. Currently I have
ConvertKeyinnode_crypto.ccas a static method. I was planning on usingECDH::BufferToPointto convert the input key toEC_POINTHowever that function is a protected function in ECDH class.Should I declare
ConvertKeyas protected function of ECDH as well and then just bind it as a static method indiffiehellman.jsor should I figure out a way without using the ECDH member functions sinceConvertKeyis going to be a static method?Perhaps using
EC_GROUP_new_by_curve_name(nid)and have the user input the ECDH curve name associated with the key?Thank you!
bnoordhuis commentedon Mar 2, 2018
I'd probably move the shared logic into a static ECDH member function. It doesn't matter if it's not a member, as long as there isn't code duplication.
wuweiweiwu commentedon Mar 2, 2018
Sounds good! On it
crypto: ECDH function to convert public keys
crypto: add ECDH.convertKey to convert public keys
crypto: add ECDH.convertKey to convert public keys