tendermint/pub_key.go

192 lines
4.8 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"
2017-12-27 14:37:37 -08:00
"fmt"
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"
2015-10-25 13:45:13 -07:00
"github.com/tendermint/go-wire"
2017-05-14 17:31:05 -07:00
data "github.com/tendermint/go-wire/data"
2017-12-27 14:37:37 -08:00
cmn "github.com/tendermint/tmlibs/common"
2015-10-25 13:45:13 -07:00
"golang.org/x/crypto/ripemd160"
2015-10-25 13:42:49 -07:00
)
2017-12-27 14:37:37 -08:00
// An address is a []byte, but hex-encoded even in JSON.
// []byte leaves us the option to change the address length.
type Address cmn.HexBytes
2017-04-09 00:32:54 -07:00
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
if err := wire.ReadBinaryBytes(pubKeyBytes, &pubKey); err != nil {
return PubKey{}, err
}
return pubKey, nil
2017-04-09 00:32:54 -07:00
}
2017-03-29 06:17:06 -07:00
2017-04-09 00:32:54 -07:00
//----------------------------------------
2017-03-29 06:17:06 -07:00
2017-04-09 00:32:54 -07:00
// DO NOT USE THIS INTERFACE.
// You probably want to use PubKey
2017-05-16 07:59:40 -07:00
// +gen wrapper:"PubKey,Impl[PubKeyEd25519,PubKeySecp256k1],ed25519,secp256k1"
type PubKeyInner interface {
2017-04-09 00:32:54 -07:00
AssertIsPubKeyInner()
2017-12-27 14:37:37 -08:00
Address() Address
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
2017-04-09 00:32:54 -07:00
Wrap() PubKey
2015-10-25 13:42:49 -07:00
}
//-------------------------------------
var _ PubKeyInner = PubKeyEd25519{}
// Implements PubKeyInner
2015-10-25 13:42:49 -07:00
type PubKeyEd25519 [32]byte
2017-04-09 00:32:54 -07:00
func (pubKey PubKeyEd25519) AssertIsPubKeyInner() {}
2017-12-27 14:37:37 -08:00
func (pubKey PubKeyEd25519) Address() Address {
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 {
2017-12-27 14:37:37 -08:00
panic(*err)
2016-03-15 15:58:43 -07:00
}
// 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
2017-12-27 14:37:37 -08:00
return Address(hasher.Sum(nil))
2015-10-25 13:42:49 -07:00
}
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 {
2017-12-27 14:37:37 -08:00
return fmt.Sprintf("PubKeyEd25519{%X}", pubKey[:])
2015-10-25 13:42:49 -07:00
}
// Must return the full bytes in hex.
// Used for map keying, etc.
func (pubKey PubKeyEd25519) KeyString() string {
2017-12-27 14:37:37 -08:00
return fmt.Sprintf("%X", pubKey[:])
2015-10-25 13:42:49 -07:00
}
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
//-------------------------------------
var _ PubKeyInner = PubKeySecp256k1{}
// 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
2017-04-09 00:32:54 -07:00
func (pubKey PubKeySecp256k1) AssertIsPubKeyInner() {}
// Implements Bitcoin style addresses: RIPEMD160(SHA256(pubkey))
2017-12-27 14:37:37 -08:00
func (pubKey PubKeySecp256k1) Address() Address {
hasherSHA256 := sha256.New()
hasherSHA256.Write(pubKey[:]) // does not error
sha := hasherSHA256.Sum(nil)
hasherRIPEMD160 := ripemd160.New()
hasherRIPEMD160.Write(sha) // does not error
2017-12-27 14:37:37 -08:00
return Address(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 {
2017-12-27 14:37:37 -08:00
return fmt.Sprintf("PubKeySecp256k1{%X}", pubKey[:])
2016-04-19 01:02:31 -07:00
}
// Must return the full bytes in hex.
// Used for map keying, etc.
func (pubKey PubKeySecp256k1) KeyString() string {
2017-12-27 14:37:37 -08:00
return fmt.Sprintf("%X", pubKey[:])
2016-04-19 01:02:31 -07:00
}
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
}
}