Skip to content
This repository has been archived by the owner on Sep 6, 2022. It is now read-only.

generate ecdsa public key from an input public key #219

Merged
merged 1 commit into from
Dec 2, 2021
Merged

generate ecdsa public key from an input public key #219

merged 1 commit into from
Dec 2, 2021

Conversation

richard-ramos
Copy link
Contributor

No description provided.

Copy link
Contributor

@marten-seemann marten-seemann left a comment

Choose a reason for hiding this comment

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

This is not incorrect, but where do we need this? We don't have a corresponding method for RSA, for example.

@richard-ramos
Copy link
Contributor Author

I created this function because currently I'm working on a project where I need to generate a peer.ID using a known ecdsa public key, and found that there was no way to initialize a ECDSAPublicKey from a ecdsa.PublicKey. With this PR I could do something like this:

pubKey, err := ECDSAPublicKeyFromPubKey(someECDSAPubKey)
thePeerId, err := peer.IDFromPublicKey(pubKey)

Should I implement a similar function for RSA and Ed25519 too?

@marten-seemann
Copy link
Contributor

I created this function because currently I'm working on a project where I need to generate a peer.ID using a known ecdsa public key, and found that there was no way to initialize a ECDSAPublicKey from a ecdsa.PublicKey.

Ok, that's legit.

Should I implement a similar function for RSA and Ed25519 too?

No need to.

Can we have a test please?

@richard-ramos
Copy link
Contributor Author

I've added the required test

crypto/ecdsa.go Outdated
@@ -67,6 +67,15 @@ func ECDSAKeyPairFromKey(priv *ecdsa.PrivateKey) (PrivKey, PubKey, error) {
return &ECDSAPrivateKey{priv}, &ECDSAPublicKey{&priv.PublicKey}, nil
}

// ECDSAPublicKeyFromPubKey generates a new ecdsa public key from an input public key
func ECDSAPublicKeyFromPubKey(pub *ecdsa.PublicKey) (PubKey, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

From some conversation during triage:

It is likely better to dereference they key before it's passed in to reduce the likelihood of hard to track down bugs. Also the keys aren't too big

crypto/ecdsa.go Outdated
Comment on lines 71 to 73
func ECDSAPublicKeyFromPubKey(pub *ecdsa.PublicKey) (PubKey, error) {
if pub == nil {
return nil, ErrNilPublicKey
}

return &ECDSAPublicKey{pub}, nil
}
Copy link
Contributor

Choose a reason for hiding this comment

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

For context, the bug I see here is that someone could pass a key into it and since it's by reference modify it while libp2p is using it (libp2p expects key to be constant and not change).

If it's passed by value then a copy is made and this can't happen.

Suggested change
func ECDSAPublicKeyFromPubKey(pub *ecdsa.PublicKey) (PubKey, error) {
if pub == nil {
return nil, ErrNilPublicKey
}
return &ECDSAPublicKey{pub}, nil
}
func ECDSAPublicKeyFromPubKey(pub ecdsa.PublicKey) (PubKey, error) {
return &ECDSAPublicKey{pub:&pub}, nil
}

@richard-ramos
Copy link
Contributor Author

Thank you for the code review, @aschmahmann and @Jorropo . I have done the requested changes.

Should I open a separate PR for doing the same change in here?:

// ECDSAKeyPairFromKey generates a new ecdsa private and public key from an input private key
func ECDSAKeyPairFromKey(priv *ecdsa.PrivateKey) (PrivKey, PubKey, error) {
	if priv == nil {
		return nil, nil, ErrNilPrivateKey
	}

	return &ECDSAPrivateKey{priv}, &ECDSAPublicKey{&priv.PublicKey}, nil
}

@Jorropo
Copy link
Contributor

Jorropo commented Nov 19, 2021

@richard-ramos no you should rebase or amend commit.

First, on your ecdsa-pubkey branch create a commit fixing that and do a git rebase -i master and read the comments, you will want to fixup the new commit second commit into the first one, then we can git push --force this branch.

This will change this PR to a new commit that is just like you did it right the first time (sometime you want to do "history" based commits so you would just make a new commit on top of the old one and explain your thought process in the commit description, for something that small it's obvious you can just force push).

@richard-ramos
Copy link
Contributor Author

@Jorropo but i did rebase this PR. Notice it's only a single commit containing the suggested changes

@marten-seemann marten-seemann merged commit 98db48e into libp2p:master Dec 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants