Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Added ECDSA; Added RSA tests; Fixed linting errors; Handling all un-handled errors #35

Merged
merged 1 commit into from
Sep 28, 2018

Conversation

adam-hanna
Copy link
Contributor

  1. Added ECDSA
  2. Added RSA tests
  3. Fixed linting errors. Mostly just meant adding comments to exported types and functions
  4. Handled all un-handled errors. Please note that this is not backwards compatible as I had to add an error in the return of the KeyStretcher function. Every time m.Write(..) gets called (for example, here), the number of bytes written and an error gets returned. These returned values were just being ignored.

@adam-hanna
Copy link
Contributor Author

adam-hanna commented Jul 15, 2018

I think the build is failing because it's pulling github.com/libp2p/go-libp2p-crypto/pb which doesn't have the required types, rather than using the types in the included subdir.

If you clone and $ go test . everything should pass.

@adam-hanna
Copy link
Contributor Author

Merged #34 and #37

Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

Thanks! I think the main issue here is the dependence on RLP. While ethereum compatibility out of the box would be nice, I'm not convinced it's worth the added complexity (this won't only be used by eth).

ecdsa.go Outdated

sha256 "gx/ipfs/QmXTpwq2AkzQsPjKqFQDNY2bMdsAT53hUBETeyj8QRHTZU/sha256-simd"

"github.com/ethereum/go-ethereum/rlp"
Copy link
Member

Choose a reason for hiding this comment

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

This is going to be a bit of a problem. Pulling down all of go ethereum just to build this package isn't really an option and go ethereum is LGPL licensed (while this package is MIT licensed).

Copy link
Member

Choose a reason for hiding this comment

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

Also, looking at the RLP format documentation, I'd rather not touch that. If users need ethereum compatibility, they can convert between that format and whatever sane format we choose in this package.

Copy link
Member

Choose a reason for hiding this comment

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

We should probably use ASN.1 (that's what most systems use).

ecdsa.go Outdated
return false
}

return reflect.DeepEqual(ePub, oPub)
Copy link
Member

Choose a reason for hiding this comment

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

It's probably a better idea to just implement this manually. This assumes that two equivalent bigints always have the same internal structure.

ed25519.go Outdated
pb "github.com/libp2p/go-libp2p-crypto/pb"

"gx/ipfs/QmQ51pHe6u7CWodkUGDLqaCEMchkbMt7VEZnECF5mp6tVb/ed25519"
Copy link
Member

Choose a reason for hiding this comment

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

We leave this in the unrewritten state (except for in go-ipfs). Run gx-go rw --fix to undo this.

key.go Outdated
@@ -207,15 +219,21 @@ func KeyStretcher(cipherType string, hashType string, secret []byte) (StretchedK
}

m := hmac.New(h, secret)
m.Write(seed)
if _, err := m.Write(seed); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

This is guaranteed to never return an error.

key.go Outdated
// (myIV, theirIV, myCipherKey, theirCipherKey, myMACKey, theirMACKey)
func KeyStretcher(cipherType string, hashType string, secret []byte) (StretchedKeys, StretchedKeys) {
func KeyStretcher(cipherType string, hashType string, secret []byte) (*StretchedKeys, *StretchedKeys, error) {
Copy link
Member

Choose a reason for hiding this comment

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

This is a public interface so we probably shouldn't change it (unless there's a very good reason).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree, but there are quite a few errors that are being ignored in the function. Rather than ignoring them, I think we should return the error. For example, here.

Copy link
Member

@Stebalien Stebalien Aug 10, 2018

Choose a reason for hiding this comment

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

See my comment below (above?). Writes to hmac's can't fail (according to the documentation). We should probably have a comment stating this...

In this comment, I was referring to the switch to return by pointer instead of return by value (although I guess you probably figured that we might as well make that change along with the change to return errors).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My apologies! Didn't see your other comment and didn't know that about HMAC writes! TY!

I'll leave this function definition unchanged, then.

Copy link
Member

Choose a reason for hiding this comment

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

Np, no need to apologize. This is definitely non-obvious (I had to go check the docs thinking "we can't have been that lazy...").

@Stebalien
Copy link
Member

While you're at it, could you rebase on master and cleanup the git history a bit? Reverse merges get really messy and it would be nice to have all of the documentation/cleanup (thanks, by the way) changes in separate commits.

@adam-hanna
Copy link
Contributor Author

Thanks for the detailed comments! I'll get working on them!

Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

Sorry for the slow turnaround. A few style comments and some bugs I failed to notice.

(then this should be good to go).

Note: always feel free to bug me on IRC (or here) as frequently as you want.

secp256k1.go Outdated
func (k *Secp256k1PrivateKey) Equals(o Key) bool {
sk, ok := o.(*Secp256k1PrivateKey)
k, ok := o.(*Secp256k1PrivateKey)
Copy link
Member

Choose a reason for hiding this comment

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

Bug.

secp256k1.go Outdated
func (k *Secp256k1PublicKey) Equals(o Key) bool {
sk, ok := o.(*Secp256k1PublicKey)
k, ok := o.(*Secp256k1PublicKey)
Copy link
Member

Choose a reason for hiding this comment

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

Bug!

key.go Outdated

"github.com/gogo/protobuf/proto"
pb "github.com/libp2p/go-libp2p-crypto/pb"
Copy link
Member

Choose a reason for hiding this comment

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

FYI, we usually order our imports:

  1. stdlib
  2. local repo
  3. external repos

(with newlines in-between)

However, the separation between the crypto and non-crypto imports was non-idiomatic.

ecdsa.go Outdated
}

// GenerateECDSAKeyPairFromKey generates a new ecdsa private and public key from an input private key
func GenerateECDSAKeyPairFromKey(priv *ecdsa.PrivateKey) (PrivKey, PubKey, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe drop the Generate (we're not really generating a new key)?

ecdsa.go Outdated

// GenerateECDSAKeyPair generates a new ecdsa private and public key
func GenerateECDSAKeyPair(src io.Reader) (PrivKey, PubKey, error) {
priv, err := ecdsa.GenerateKey(ECDSACurve, src)
Copy link
Member

Choose a reason for hiding this comment

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

Can replace with return GenerateECDSAKeyPairWithCurve(ECDSACurve, src).

@adam-hanna
Copy link
Contributor Author

@Stebalien thanks, I'm on it!

1. Added ECDSA Keys
2. Removed GX import refs
3. Added RSA tests
4. Fixed linting warnings
@adam-hanna
Copy link
Contributor Author

@Stebalien, thanks again for all of your help, and sorry for introducing that bug. Great catch!

I just incorporated your latest round of feedback...

@Stebalien
Copy link
Member

I'd like to get @dignifiedquire to take a look at this before merging as he maintains the JS version. I also noticed that "secp256k1" is also ECDSA (with a specific curve, maybe some bitcoin-specific key formats?). Hopefully @dignifiedquire will know more.

@Stebalien
Copy link
Member

I've discussed this and, well, we do support a variant of ECDSA (Secp256k1) but we use a hard-coded curve and a minimal key format. The upside is that keys in that format are small. The downside, is that it's a bit less flexible. The downside of this format is that keys are large. However, it supports all "standard" keys so it should help with interoperability with existing systems.

Given that, we've (@dignifiedquire and I) have decided to merge this.

@Stebalien Stebalien merged commit 0f79fbe into libp2p:master Sep 28, 2018
@Stebalien
Copy link
Member

Thanks @adam-hanna for bearing with this!

@adam-hanna
Copy link
Contributor Author

adam-hanna commented Sep 29, 2018 via email

"hash": "QmdxUuburamoF6zF9qjeQC4WYcWGbWuRmdLacMEsW8ioD8",
"name": "gogo-protobuf",
"version": "0.0.0"
},
Copy link
Member

Choose a reason for hiding this comment

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

Why?

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.

2 participants