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

ecdh.setPublicKey is actually useful and should be undeprecated #18977

Closed
skerit opened this issue Feb 24, 2018 · 13 comments
Closed

ecdh.setPublicKey is actually useful and should be undeprecated #18977

skerit opened this issue Feb 24, 2018 · 13 comments
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors.

Comments

@skerit
Copy link

skerit commented Feb 24, 2018

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:

const crypto = require('crypto');

let ecdh = crypto.createECDH('secp256k1');
ecdh.generateKeys();

let private_key_compressed = ecdh.getPrivateKey(null, 'compressed');
let public_key_compressed = ecdh.getPublicKey(null, 'compressed');

// Public key is a buffer of 33 bytes
console.log('Compressed public key:', public_key_compressed.length, public_key_compressed);

// Create a new ecdh object
ecdh = crypto.createECDH('secp256k1')

// Use the compressed public key to set the public key
ecdh.setPublicKey(public_key_compressed);

// Get the uncompressed get
let public_key_uncompressed = ecdh.getPublicKey();

// The returned key is a buffer of 66 bytes long
console.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.

@mscdex mscdex added the crypto Issues and PRs related to the crypto subsystem. label Feb 24, 2018
@MylesBorins
Copy link
Member

/cc @nodejs/crypto

@bnoordhuis
Copy link
Member

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
Copy link
Author

skerit commented Feb 26, 2018

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 :)

@bnoordhuis bnoordhuis added c++ Issues and PRs that require attention from people who are familiar with C++. good first issue Issues that are suitable for first-time contributors. feature request Issues that request new features to be added to Node.js. mentor-available labels Feb 26, 2018
@bnoordhuis
Copy link
Member

Okay, I've added labels. Pull request welcome and happy to answer questions.

@wuweiweiwu
Copy link
Contributor

@bnoordhuis Can I pick this up? I think it'll be a good first issue :)

@bnoordhuis
Copy link
Member

@wuweiweiwu Sure thing.

@wuweiweiwu
Copy link
Contributor

@bnoordhuis I was thinking of adding another method uncompressKey in the ECDH class in https://github.com/nodejs/node/blob/master/src/node_crypto.cc

Is that a good place to start?

Thank you

@bnoordhuis
Copy link
Member

@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.)

@wuweiweiwu
Copy link
Contributor

@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
Copy link
Member

It's the 'hybrid' option to ECDH.prototype.getPublicKey.

And is it ok if I put the tests in test/parallel?

Yep.

@wuweiweiwu
Copy link
Contributor

wuweiweiwu commented Mar 1, 2018

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?

Thank you!

@bnoordhuis
Copy link
Member

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
Copy link
Contributor

Sounds good! On it

wuweiweiwu added a commit to wuweiweiwu/node that referenced this issue Mar 10, 2018
ECDH.convertKey is used to convert public keys
between different formats.

Fixes: nodejs#18977
targos pushed a commit to targos/node that referenced this issue Apr 4, 2018
ECDH.convertKey is used to convert public keys between different
formats.

PR-URL: nodejs#19080
Fixes: nodejs#18977
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
targos pushed a commit that referenced this issue Apr 12, 2018
ECDH.convertKey is used to convert public keys between different
formats.

PR-URL: #19080
Fixes: #18977
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>

Backport-PR-URL: #19745
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. crypto Issues and PRs related to the crypto subsystem. feature request Issues that request new features to be added to Node.js. good first issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

No branches or pull requests

5 participants