2015-10-25 13:45:13 -07:00
|
|
|
package crypto
|
2015-10-25 13:42:49 -07:00
|
|
|
|
|
|
|
import (
|
2017-10-25 19:56:08 -07:00
|
|
|
"crypto/subtle"
|
2016-04-19 01:26:40 -07:00
|
|
|
|
2016-04-19 01:02:31 -07:00
|
|
|
secp256k1 "github.com/btcsuite/btcd/btcec"
|
2015-10-25 13:42:49 -07:00
|
|
|
"github.com/tendermint/ed25519"
|
|
|
|
"github.com/tendermint/ed25519/extra25519"
|
|
|
|
)
|
|
|
|
|
2017-04-09 00:32:54 -07:00
|
|
|
func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
|
2018-03-26 10:41:04 +02:00
|
|
|
err = cdc.UnmarshalBinaryBare(privKeyBytes, &privKey)
|
2017-04-09 00:32:54 -07:00
|
|
|
return
|
|
|
|
}
|
2017-03-29 15:17:06 +02:00
|
|
|
|
2017-04-09 00:32:54 -07:00
|
|
|
//----------------------------------------
|
2017-03-29 15:17:06 +02:00
|
|
|
|
2018-01-14 00:31:39 -08:00
|
|
|
type PrivKey interface {
|
2016-03-15 11:11:54 -07:00
|
|
|
Bytes() []byte
|
2018-06-10 10:01:41 +02:00
|
|
|
Sign(msg []byte) (Signature, error)
|
|
|
|
PubKey() (PubKey, error)
|
2016-04-19 01:26:40 -07:00
|
|
|
Equals(PrivKey) bool
|
2015-10-25 13:42:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
2018-01-14 00:31:39 -08:00
|
|
|
var _ PrivKey = PrivKeyEd25519{}
|
2017-04-10 13:05:52 +02:00
|
|
|
|
2015-10-25 13:42:49 -07:00
|
|
|
// Implements PrivKey
|
|
|
|
type PrivKeyEd25519 [64]byte
|
|
|
|
|
2016-03-15 11:11:54 -07:00
|
|
|
func (privKey PrivKeyEd25519) Bytes() []byte {
|
2018-06-10 10:01:41 +02:00
|
|
|
return cdc.MustMarshalBinaryBare(privKey)
|
2016-03-15 11:11:54 -07:00
|
|
|
}
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
func (privKey PrivKeyEd25519) Sign(msg []byte) (Signature, error) {
|
2016-04-19 01:02:31 -07:00
|
|
|
privKeyBytes := [64]byte(privKey)
|
2015-10-25 13:42:49 -07:00
|
|
|
signatureBytes := ed25519.Sign(&privKeyBytes, msg)
|
2018-06-10 10:01:41 +02:00
|
|
|
return SignatureEd25519(*signatureBytes), nil
|
2015-10-25 13:42:49 -07:00
|
|
|
}
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
func (privKey PrivKeyEd25519) PubKey() (PubKey, error) {
|
2015-10-25 13:42:49 -07:00
|
|
|
privKeyBytes := [64]byte(privKey)
|
2017-03-21 21:44:24 +01:00
|
|
|
pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
|
2018-06-10 10:01:41 +02:00
|
|
|
return PubKeyEd25519(pubBytes), nil
|
2015-10-25 13:42:49 -07:00
|
|
|
}
|
|
|
|
|
2017-12-21 19:51:57 -05:00
|
|
|
// Equals - you probably don't need to use this.
|
|
|
|
// Runs in constant time based on length of the keys.
|
2016-04-19 01:26:40 -07:00
|
|
|
func (privKey PrivKeyEd25519) Equals(other PrivKey) bool {
|
2018-01-14 00:31:39 -08:00
|
|
|
if otherEd, ok := other.(PrivKeyEd25519); ok {
|
2017-12-21 19:51:57 -05:00
|
|
|
return subtle.ConstantTimeCompare(privKey[:], otherEd[:]) == 1
|
2016-04-19 01:26:40 -07:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 13:42:49 -07:00
|
|
|
func (privKey PrivKeyEd25519) ToCurve25519() *[32]byte {
|
|
|
|
keyCurve25519 := new([32]byte)
|
|
|
|
privKeyBytes := [64]byte(privKey)
|
|
|
|
extra25519.PrivateKeyToCurve25519(keyCurve25519, &privKeyBytes)
|
|
|
|
return keyCurve25519
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deterministically generates new priv-key bytes from key.
|
2016-04-19 01:02:31 -07:00
|
|
|
func (privKey PrivKeyEd25519) Generate(index int) PrivKeyEd25519 {
|
2018-03-26 10:41:04 +02:00
|
|
|
bz, err := cdc.MarshalBinaryBare(struct {
|
2015-10-25 13:42:49 -07:00
|
|
|
PrivKey [64]byte
|
|
|
|
Index int
|
2016-04-19 01:02:31 -07:00
|
|
|
}{privKey, index})
|
2018-01-14 00:31:39 -08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
newBytes := Sha256(bz)
|
2018-05-05 19:17:21 -04:00
|
|
|
newKey := new([64]byte)
|
|
|
|
copy(newKey[:32], newBytes)
|
|
|
|
ed25519.MakePublicKey(newKey)
|
|
|
|
return PrivKeyEd25519(*newKey)
|
2015-10-25 13:42:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func GenPrivKeyEd25519() PrivKeyEd25519 {
|
|
|
|
privKeyBytes := new([64]byte)
|
|
|
|
copy(privKeyBytes[:32], CRandBytes(32))
|
|
|
|
ed25519.MakePublicKey(privKeyBytes)
|
|
|
|
return PrivKeyEd25519(*privKeyBytes)
|
|
|
|
}
|
|
|
|
|
2016-03-15 11:11:54 -07:00
|
|
|
// 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.
|
2015-10-25 13:42:49 -07:00
|
|
|
privKeyBytes := new([64]byte)
|
|
|
|
copy(privKeyBytes[:32], privKey32)
|
2016-02-08 02:18:06 -08:00
|
|
|
ed25519.MakePublicKey(privKeyBytes)
|
2015-10-25 13:42:49 -07:00
|
|
|
return PrivKeyEd25519(*privKeyBytes)
|
|
|
|
}
|
2016-04-19 01:02:31 -07:00
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
2018-01-14 00:31:39 -08:00
|
|
|
var _ PrivKey = PrivKeySecp256k1{}
|
2017-04-10 13:05:52 +02:00
|
|
|
|
2016-04-19 01:02:31 -07:00
|
|
|
// Implements PrivKey
|
|
|
|
type PrivKeySecp256k1 [32]byte
|
|
|
|
|
|
|
|
func (privKey PrivKeySecp256k1) Bytes() []byte {
|
2018-06-10 10:01:41 +02:00
|
|
|
return cdc.MustMarshalBinaryBare(privKey)
|
2016-04-19 01:02:31 -07:00
|
|
|
}
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
func (privKey PrivKeySecp256k1) Sign(msg []byte) (Signature, error) {
|
2016-04-19 01:02:31 -07:00
|
|
|
priv__, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
|
|
|
|
sig__, err := priv__.Sign(Sha256(msg))
|
|
|
|
if err != nil {
|
2018-06-10 10:01:41 +02:00
|
|
|
return nil, err
|
2016-04-19 01:02:31 -07:00
|
|
|
}
|
2018-06-10 10:01:41 +02:00
|
|
|
return SignatureSecp256k1(sig__.Serialize()), nil
|
2016-04-19 01:02:31 -07:00
|
|
|
}
|
|
|
|
|
2018-06-10 10:01:41 +02:00
|
|
|
func (privKey PrivKeySecp256k1) PubKey() (PubKey, error) {
|
2016-04-19 01:02:31 -07:00
|
|
|
_, pub__ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey[:])
|
2017-03-22 01:18:56 -04:00
|
|
|
var pub PubKeySecp256k1
|
|
|
|
copy(pub[:], pub__.SerializeCompressed())
|
2018-06-10 10:01:41 +02:00
|
|
|
return pub, nil
|
2016-04-19 01:02:31 -07:00
|
|
|
}
|
|
|
|
|
2017-12-21 19:51:57 -05:00
|
|
|
// Equals - you probably don't need to use this.
|
|
|
|
// Runs in constant time based on length of the keys.
|
2016-04-19 01:26:40 -07:00
|
|
|
func (privKey PrivKeySecp256k1) Equals(other PrivKey) bool {
|
2018-01-14 00:31:39 -08:00
|
|
|
if otherSecp, ok := other.(PrivKeySecp256k1); ok {
|
2017-12-21 19:51:57 -05:00
|
|
|
return subtle.ConstantTimeCompare(privKey[:], otherSecp[:]) == 1
|
2016-04-19 01:26:40 -07:00
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-19 01:02:31 -07:00
|
|
|
/*
|
|
|
|
// Deterministically generates new priv-key bytes from key.
|
|
|
|
func (key PrivKeySecp256k1) Generate(index int) PrivKeySecp256k1 {
|
2018-01-21 19:03:23 -08:00
|
|
|
newBytes := cdc.BinarySha256(struct {
|
2016-04-19 01:02:31 -07:00
|
|
|
PrivKey [64]byte
|
|
|
|
Index int
|
|
|
|
}{key, index})
|
|
|
|
var newKey [64]byte
|
|
|
|
copy(newKey[:], newBytes)
|
|
|
|
return PrivKeySecp256k1(newKey)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func GenPrivKeySecp256k1() PrivKeySecp256k1 {
|
|
|
|
privKeyBytes := [32]byte{}
|
|
|
|
copy(privKeyBytes[:], CRandBytes(32))
|
|
|
|
priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKeyBytes[:])
|
|
|
|
copy(privKeyBytes[:], priv.Serialize())
|
|
|
|
return PrivKeySecp256k1(privKeyBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: secret should be the output of a KDF like bcrypt,
|
|
|
|
// if it's derived from user input.
|
|
|
|
func GenPrivKeySecp256k1FromSecret(secret []byte) PrivKeySecp256k1 {
|
|
|
|
privKey32 := Sha256(secret) // Not Ripemd160 because we want 32 bytes.
|
|
|
|
priv, _ := secp256k1.PrivKeyFromBytes(secp256k1.S256(), privKey32)
|
|
|
|
privKeyBytes := [32]byte{}
|
|
|
|
copy(privKeyBytes[:], priv.Serialize())
|
|
|
|
return PrivKeySecp256k1(privKeyBytes)
|
|
|
|
}
|