tendermint/pub_key.go

226 lines
5.5 KiB
Go
Raw Normal View History

2015-10-25 13:45:13 -07:00
package crypto
2015-10-25 13:42:49 -07:00
import (
"bytes"
"crypto/sha256"
2015-10-25 13:42:49 -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"
. "github.com/tendermint/go-common"
2017-02-22 14:15:10 -08:00
data "github.com/tendermint/go-data"
2015-10-25 13:45:13 -07:00
"github.com/tendermint/go-wire"
"golang.org/x/crypto/ripemd160"
2015-10-25 13:42:49 -07:00
)
2017-03-29 06:17:06 -07:00
/*
DO NOT USE this interface.
It is public by necessity but should never be used directly
outside of this package.
Only use the PubKey, never the PubKeyInner
*/
type PubKeyInner interface {
2015-10-25 13:42:49 -07:00
Address() []byte
2016-03-15 11:11:54 -07:00
Bytes() []byte
2016-02-08 00:50:52 -08:00
KeyString() string
2015-10-25 13:42:49 -07:00
VerifyBytes(msg []byte, sig Signature) bool
2016-03-13 09:40:15 -07:00
Equals(PubKey) bool
2015-10-25 13:42:49 -07:00
}
2017-02-22 14:15:10 -08:00
var pubKeyMapper data.Mapper
// register both public key types with go-data (and thus go-wire)
func init() {
pubKeyMapper = data.NewMapper(PubKey{}).
RegisterImplementation(PubKeyEd25519{}, NameEd25519, TypeEd25519).
RegisterImplementation(PubKeySecp256k1{}, NameSecp256k1, TypeSecp256k1)
2017-02-22 14:15:10 -08:00
}
2017-03-29 06:17:06 -07:00
// PubKey should be used instead of an interface in all external packages
// unless you demand a concrete implementation, then use that directly.
type PubKey struct {
PubKeyInner `json:"unwrap"`
2017-02-22 14:15:10 -08:00
}
2017-03-29 06:17:06 -07:00
// WrapPubKey goes from concrete implementation to "interface" struct
func WrapPubKey(pk PubKeyInner) PubKey {
if wrap, ok := pk.(PubKey); ok {
pk = wrap.Unwrap()
}
return PubKey{pk}
}
2017-03-29 06:17:06 -07:00
// Unwrap recovers the concrete interface safely (regardless of levels of embeds)
func (p PubKey) Unwrap() PubKeyInner {
pk := p.PubKeyInner
for wrap, ok := pk.(PubKey); ok; wrap, ok = pk.(PubKey) {
pk = wrap.PubKeyInner
}
return pk
}
func (p PubKey) MarshalJSON() ([]byte, error) {
return pubKeyMapper.ToJSON(p.PubKeyInner)
2017-02-22 14:15:10 -08:00
}
func (p *PubKey) UnmarshalJSON(data []byte) (err error) {
2017-02-22 14:15:10 -08:00
parsed, err := pubKeyMapper.FromJSON(data)
if err == nil && parsed != nil {
p.PubKeyInner = parsed.(PubKeyInner)
2017-02-22 14:15:10 -08:00
}
return
}
2015-10-25 13:42:49 -07:00
func (p PubKey) Empty() bool {
return p.PubKeyInner == nil
}
2016-03-15 11:11:54 -07:00
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
return
}
2015-10-25 13:42:49 -07:00
//-------------------------------------
// Implements PubKeyInner
2015-10-25 13:42:49 -07:00
type PubKeyEd25519 [32]byte
func (pubKey PubKeyEd25519) Address() []byte {
2016-03-15 15:58:43 -07:00
w, n, err := new(bytes.Buffer), new(int), new(error)
wire.WriteBinary(pubKey[:], w, n, err)
if *err != nil {
PanicCrisis(*err)
}
// append type byte
2017-02-23 15:31:43 -08:00
encodedPubkey := append([]byte{TypeEd25519}, w.Bytes()...)
2015-10-25 13:42:49 -07:00
hasher := ripemd160.New()
2016-03-15 15:58:43 -07:00
hasher.Write(encodedPubkey) // does not error
2015-10-25 13:42:49 -07:00
return hasher.Sum(nil)
}
2016-03-15 11:11:54 -07:00
func (pubKey PubKeyEd25519) Bytes() []byte {
return wire.BinaryBytes(PubKey{pubKey})
2016-03-15 11:11:54 -07:00
}
2015-10-25 13:42:49 -07:00
func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
2017-02-22 14:43:26 -08:00
// make sure we use the same algorithm to sign
sig, ok := sig_.Unwrap().(SignatureEd25519)
2015-10-25 13:42:49 -07:00
if !ok {
return false
}
pubKeyBytes := [32]byte(pubKey)
sigBytes := [64]byte(sig)
return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
}
2017-02-22 14:15:10 -08:00
func (p PubKeyEd25519) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(p[:])
}
func (p *PubKeyEd25519) UnmarshalJSON(enc []byte) error {
var ref []byte
err := data.Encoder.Unmarshal(&ref, enc)
copy(p[:], ref)
return err
}
2015-10-25 13:42:49 -07:00
// For use with golang/crypto/nacl/box
// If error, returns nil.
func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
if !ok {
return nil
}
return keyCurve25519
}
func (pubKey PubKeyEd25519) String() string {
return Fmt("PubKeyEd25519{%X}", pubKey[:])
}
// Must return the full bytes in hex.
// Used for map keying, etc.
func (pubKey PubKeyEd25519) KeyString() string {
return Fmt("%X", pubKey[:])
}
func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
if otherEd, ok := other.Unwrap().(PubKeyEd25519); ok {
2015-10-25 13:42:49 -07:00
return bytes.Equal(pubKey[:], otherEd[:])
} else {
return false
}
}
2016-04-19 01:02:31 -07:00
//-------------------------------------
// Implements PubKey.
// Compressed pubkey (just the x-cord),
// prefixed with 0x02 or 0x03, depending on the y-cord.
type PubKeySecp256k1 [33]byte
2016-04-19 01:02:31 -07:00
// Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
2016-04-19 01:02:31 -07:00
func (pubKey PubKeySecp256k1) Address() []byte {
hasherSHA256 := sha256.New()
hasherSHA256.Write(pubKey[:]) // does not error
sha := hasherSHA256.Sum(nil)
hasherRIPEMD160 := ripemd160.New()
hasherRIPEMD160.Write(sha) // does not error
return hasherRIPEMD160.Sum(nil)
2016-04-19 01:02:31 -07:00
}
func (pubKey PubKeySecp256k1) Bytes() []byte {
return wire.BinaryBytes(PubKey{pubKey})
2016-04-19 01:02:31 -07:00
}
func (pubKey PubKeySecp256k1) VerifyBytes(msg []byte, sig_ Signature) bool {
2017-02-22 14:43:26 -08:00
// and assert same algorithm to sign and verify
sig, ok := sig_.Unwrap().(SignatureSecp256k1)
2016-04-19 01:02:31 -07:00
if !ok {
return false
}
2017-02-22 14:43:26 -08:00
pub__, err := secp256k1.ParsePubKey(pubKey[:], secp256k1.S256())
2017-02-22 14:43:26 -08:00
if err != nil {
return false
}
2016-04-19 01:02:31 -07:00
sig__, err := secp256k1.ParseDERSignature(sig[:], secp256k1.S256())
if err != nil {
return false
}
return sig__.Verify(Sha256(msg), pub__)
}
2017-02-22 14:15:10 -08:00
func (p PubKeySecp256k1) MarshalJSON() ([]byte, error) {
return data.Encoder.Marshal(p[:])
}
func (p *PubKeySecp256k1) UnmarshalJSON(enc []byte) error {
var ref []byte
err := data.Encoder.Unmarshal(&ref, enc)
copy(p[:], ref)
return err
}
2016-04-19 01:02:31 -07:00
func (pubKey PubKeySecp256k1) String() string {
return Fmt("PubKeySecp256k1{%X}", pubKey[:])
}
// Must return the full bytes in hex.
// Used for map keying, etc.
func (pubKey PubKeySecp256k1) KeyString() string {
return Fmt("%X", pubKey[:])
}
func (pubKey PubKeySecp256k1) Equals(other PubKey) bool {
if otherSecp, ok := other.Unwrap().(PubKeySecp256k1); ok {
2016-04-19 01:26:40 -07:00
return bytes.Equal(pubKey[:], otherSecp[:])
2016-04-19 01:02:31 -07:00
} else {
return false
}
}