Skip to content
This repository has been archived by the owner on Jul 15, 2018. It is now read-only.

helpers: provide helpers to generate keys #31

Closed
odeke-em opened this issue Sep 7, 2017 · 3 comments
Closed

helpers: provide helpers to generate keys #31

odeke-em opened this issue Sep 7, 2017 · 3 comments

Comments

@odeke-em
Copy link
Contributor

odeke-em commented Sep 7, 2017

Am currently writing code to test out the bug fix for tendermint/tendermint#514 in which we have a dishonest peer requesting a non-existent block height from a reactor and that requires a whole lot of setup.

I currently see that we have helpers for PrivKeyEd25519

go-crypto/priv_key.go

Lines 99 to 114 in 1bc8de4

func GenPrivKeyEd25519() PrivKeyEd25519 {
privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], CRandBytes(32))
ed25519.MakePublicKey(privKeyBytes)
return PrivKeyEd25519(*privKeyBytes)
}
// NOTE: secret should be the output of a KDF like bcrypt,
// if it's derived from user input.
func GenPrivKeyEd25519FromSecret(secret []byte) PrivKeyEd25519 {
privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], privKey32)
ed25519.MakePublicKey(privKeyBytes)
return PrivKeyEd25519(*privKeyBytes)
}

However, trying to generate the public key using our package is quite a bit of work, IMHO we should make it simple to generate a public Ed25519 key that conforms to our package and not just to the Go crypto library. I suspect some other developers might need this in the future.

@odeke-em
Copy link
Contributor Author

odeke-em commented Sep 7, 2017

/cc @ethanfrey @ebuchman @cloudhead

odeke-em added a commit to orijtech/ed25519 that referenced this issue Sep 7, 2017
MakePublicKey takes in a privateKey and mutates it setting
the first 32 bytes to those original 32 bytes used to
generate the publicKey.
This wasn't made clear in the comments and required digging
into the actual implementation when I was advocating for
tendermint/go-crypto#31.
I noticed that in crypto
https://github.com/tendermint/go-crypto/blob/1bc8de4caa844f8b64c120e65b047898f22b7f3e/priv_key.go#L99-L114

there was an unexplained call to ed25519.MakePublicKey nor were
there any comments to explain that.
odeke-em added a commit that referenced this issue Sep 7, 2017
Fixes #31

Add a helper to be able to generate Ed25519 keys directly
from our crypto package, and with these helpers we give
more context to a previously undocumented and hanging
code statement in which while generating a private key
we invoked ed25519.MakePublicKey, which is correct but
seemed hanging. With this change, the helpers are:
```go
func GenEd25519KeyPair() (priv PrivKeyEd25519, pub PubKeyEd25519)
func GenEd25519KeyPairFromSecret(secret []byte) (priv PrivKeyEd25519, pub PubKeyEd25519)
```

which are invoked by:
- GenPrivKeyEd25519()
- GenPrivKeyEd25519FromSecret(secret []byte)
respectively and now with the pairings give more comfort knowing that
each private key for completion must be generated with its public key,
but we'll chose to discard the public key.
@ethanfrey
Copy link
Contributor

What about GenPrivKeyEd25519().PubKey()?

That's what I do, and your logic would do under the hood. Not too much work, no?

@odeke-em
Copy link
Contributor Author

Thanks @ethanfrey, and my apologies for the late reply, I was off from here since the weekend, but am back.

As per your recommendation and exhibit above, I've opened #35 in which we can provide a high level view of docs for go-crypto to easily transfer such knowledge to other developers and provide some more documentation. I'll close this issue in favor of the mentioned one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants