-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
crypto: add generatePublicKey to ECDH, fix corner cases #1020
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4025,6 +4025,7 @@ void ECDH::Initialize(Environment* env, Handle<Object> target) { | |
t->InstanceTemplate()->SetInternalFieldCount(1); | ||
|
||
env->SetProtoMethod(t, "generateKeys", GenerateKeys); | ||
env->SetProtoMethod(t, "generatePublicKey", GeneratePublicKey); | ||
env->SetProtoMethod(t, "computeSecret", ComputeSecret); | ||
env->SetProtoMethod(t, "getPublicKey", GetPublicKey); | ||
env->SetProtoMethod(t, "getPrivateKey", GetPrivateKey); | ||
|
@@ -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; | ||
} | ||
|
||
void ECDH::GeneratePublicKey(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
ECDH* ecdh = Unwrap<ECDH>(args.Holder()); | ||
|
||
const BIGNUM* priv = EC_KEY_get0_private_key(ecdh->key_); | ||
if (priv == nullptr) | ||
return env->ThrowError("Failed to get ECDH private key"); | ||
|
||
EC_POINT* pub = EC_POINT_new(ecdh->group_); | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Style: space after 'if' (here and below.) |
||
EC_POINT_free(pub); | ||
return env->ThrowError("Failed to generate ECDH public key"); | ||
} | ||
|
||
if(!EC_KEY_set_public_key(ecdh->key_, pub)) { | ||
EC_POINT_free(pub); | ||
return env->ThrowError("Failed to convert EC_POINT to a public key"); | ||
} | ||
|
||
EC_POINT_free(pub); | ||
} | ||
|
||
EC_POINT* ECDH::BufferToPoint(char* data, size_t len) { | ||
EC_POINT* pub; | ||
|
@@ -4095,7 +4119,6 @@ EC_POINT* ECDH::BufferToPoint(char* data, size_t len) { | |
return nullptr; | ||
} | ||
|
||
|
||
void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
|
@@ -4124,7 +4147,6 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) { | |
args.GetReturnValue().Set(Buffer::Use(env, out, out_len)); | ||
} | ||
|
||
|
||
void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
||
|
@@ -4133,9 +4155,6 @@ void ECDH::GetPublicKey(const FunctionCallbackInfo<Value>& args) { | |
|
||
ECDH* ecdh = Unwrap<ECDH>(args.Holder()); | ||
|
||
if (!ecdh->generated_) | ||
return env->ThrowError("You should generate ECDH keys first"); | ||
|
||
const EC_POINT* pub = EC_KEY_get0_public_key(ecdh->key_); | ||
if (pub == nullptr) | ||
return env->ThrowError("Failed to get ECDH public key"); | ||
|
@@ -4168,9 +4187,6 @@ void ECDH::GetPrivateKey(const FunctionCallbackInfo<Value>& args) { | |
|
||
ECDH* ecdh = Unwrap<ECDH>(args.Holder()); | ||
|
||
if (!ecdh->generated_) | ||
return env->ThrowError("You should generate ECDH keys first"); | ||
|
||
const BIGNUM* b = EC_KEY_get0_private_key(ecdh->key_); | ||
if (b == nullptr) | ||
return env->ThrowError("Failed to get ECDH private key"); | ||
|
@@ -4224,7 +4240,7 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) { | |
int r = EC_KEY_set_public_key(ecdh->key_, pub); | ||
EC_POINT_free(pub); | ||
if (!r) | ||
return env->ThrowError("Failed to convert BN to a private key"); | ||
return env->ThrowError("Failed to convert EC_POINT to a public key"); | ||
} | ||
|
||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,3 +178,9 @@ ecdh4.setPublicKey(ecdh1.getPublicKey()); | |
assert.throws(function() { | ||
ecdh4.setPublicKey(ecdh3.getPublicKey()); | ||
}); | ||
|
||
// ECDH should be able to generate public key from private key | ||
var ecdh5 = crypto.createECDH('prime256v1'); | ||
ecdh5.setPrivateKey(ecdh4.getPrivateKey()); | ||
|
||
assert(ecdh5.generatePublicKey('hex') === ecdh5.getPublicKey('hex')); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you use assert.equal here? |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
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
andGeneratedPublic
?There was a problem hiding this comment.
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).
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?
There was a problem hiding this comment.
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?