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

crypto: add generatePublicKey to ECDH, fix corner cases #1020

Closed
wants to merge 2 commits into from
Closed

crypto: add generatePublicKey to ECDH, fix corner cases #1020

wants to merge 2 commits into from

Conversation

mbullington
Copy link

ECDH.generatePublicKey can get the public key using the curve
and private key. This allows usage of the crypto module where
a developer imports a private key, and then generates a public
key without needing it stored.

Removed the generated_ boolean from the ECDH class, and stopped
checking for it with getPrivateKey() and getPublicKey(). This
allows you to import public and private keys without having to
generate first, which would just rewrite the generated keys
regardless.

An error message was changed to accurately reflect what the error
was.

ECDH.generatePublicKey can get the public key using the curve
and private key. This allows usage of the crypto module where
a developer imports a private key, and then generates a public
key without needing it stored.

Removed the generated_ boolean from the ECDH class, and stopped
checking for it with getPrivateKey() and getPublicKey(). This
allows you to import public and private keys without having to
generate first, which would just rewrite the generated keys
regardless.

An error message was changed to accurately reflect what the error
was.
@tellnes tellnes added the crypto Issues and PRs related to the crypto subsystem. label Mar 1, 2015
@@ -535,6 +535,12 @@ ECDH.prototype.generateKeys = function generateKeys(encoding, format) {
return this.getPublicKey(encoding, format);
};

ECDH.prototype.generatePublicKey = function generateKeys(encoding, format) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the function name here needs to be changed from generateKeys to generatePublicKey

@brendanashworth brendanashworth added the semver-minor PRs that contain new features and should be released in the next minor version. label Mar 2, 2015
Change the function name of generatePublicKey to be 
generatePublicKey instead of generateKeys.
@brendanashworth
Copy link
Contributor

cc'ing @iojs/crypto

if (pub == nullptr)
return env->ThrowError("Failed to allocate EC_POINT for a public key");

if(EC_POINT_mul(ecdh->group_, pub, priv, nullptr, nullptr, nullptr) <= 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Style: space after 'if' (here and below.)

@bnoordhuis
Copy link
Member

LGTM sans nits.

@@ -4062,10 +4063,33 @@ void ECDH::GenerateKeys(const FunctionCallbackInfo<Value>& args) {

if (!EC_KEY_generate_key(ecdh->key_))
return env->ThrowError("Failed to generate EC_KEY");

ecdh->generated_ = true;
Copy link
Member

Choose a reason for hiding this comment

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

Why?

Copy link
Member

Choose a reason for hiding this comment

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

I mean, perhaps it should be set from GeneratePublicKey too? Or be a flag field with two possible flags: GeneratedPrivate and GeneratedPublic?

Copy link
Author

Choose a reason for hiding this comment

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

If I have something like the following code sample, getPrivateKey() will return an error if you haven't generated the keys, which is a pain for imported keys. I'd have to generate a keypair, then overwrite the keys (wasting a keypair just to toggle the bool).

var ecdh = crypto.createECDH(curve);
ecdh.setPrivateKey(privateKey);
ecdh.setPublicKey(publicKey);

ecdh.getPrivateKey(); // error

I could also import only the private key then use generatePublicKey to toggle the bool, but the getPrivateKey/getPublicKey functionality with the bool initially can be unexpected behavior.

Perhaps computeSecret could just check for the private key and throw an error based on that?

Copy link
Member

Choose a reason for hiding this comment

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

Ok, if setPrivateKey will set both Private and Public flag - will that work?

@brendanashworth
Copy link
Contributor

Reiterating, @mbullington if you could make the requested changes, I could try and get this merged in soon!

@Qard
Copy link
Member

Qard commented Jun 26, 2015

This PR is getting stale.

@mbullington Can you update the PR based on the feedback provided?

@mbullington
Copy link
Author

I'll update the PR based on master and the feedback provided. Thank you for being patient, I'm very sorry if having this PR open for this long caused any inconvenience.

@Qard
Copy link
Member

Qard commented Jun 26, 2015

No worries. I just want to make sure you get your contributions in and that they aren't forgotten. :)

@Qard
Copy link
Member

Qard commented Aug 8, 2015

@nodejs/crypto Anyone want to update and retarget this?

@Fishrock123
Copy link
Contributor

@mbullington could you make a new PR against master? Thanks!

@mruddy
Copy link

mruddy commented Oct 25, 2015

Please see #3511. It's a little different, but solves many of the same things.

@Trott
Copy link
Member

Trott commented Dec 3, 2015

I rebased against master (only conflicts were in the markdown file), fixed up the nits (except for the ecdh->generated_ = true; removal that @indutny calls out) as well as some linting errors and re-animated this in #4124. Hope that's OK (and happy to close it if it's not for some reason).

@bnoordhuis
Copy link
Member

Closing, obsolete now that #3511 has landed.

@bnoordhuis bnoordhuis closed this Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto Issues and PRs related to the crypto subsystem. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants