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

feat: refactor key interface #34

Merged
merged 4 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions ed25519.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/agl/ed25519"
extra "github.com/agl/ed25519/extra25519"
proto "github.com/gogo/protobuf/proto"
pb "github.com/libp2p/go-libp2p-crypto/pb"
)

Expand Down Expand Up @@ -36,16 +35,20 @@ func GenerateEd25519Key(src io.Reader) (PrivKey, PubKey, error) {
nil
}

func (k *Ed25519PrivateKey) Bytes() ([]byte, error) {
pbmes := new(pb.PrivateKey)
typ := pb.KeyType_Ed25519
pbmes.Type = &typ
func (sk *Ed25519PrivateKey) Type() pb.KeyType {
return pb.KeyType_Ed25519
}

func (sk *Ed25519PrivateKey) Bytes() ([]byte, error) {
return MarshalPrivateKey(sk)
}

func (k *Ed25519PrivateKey) Raw() ([]byte, error) {
buf := make([]byte, 96)
copy(buf, k.sk[:])
copy(buf[64:], k.pk[:])
pbmes.Data = buf
return proto.Marshal(pbmes)

return buf, nil
}

func (k *Ed25519PrivateKey) Equals(o Key) bool {
Expand All @@ -72,12 +75,16 @@ func (k *Ed25519PrivateKey) ToCurve25519() *[32]byte {
return &sk
}

func (k *Ed25519PublicKey) Bytes() ([]byte, error) {
pbmes := new(pb.PublicKey)
typ := pb.KeyType_Ed25519
pbmes.Type = &typ
pbmes.Data = (*k.k)[:]
return proto.Marshal(pbmes)
func (sk *Ed25519PublicKey) Type() pb.KeyType {
Copy link
Member

Choose a reason for hiding this comment

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

Should use k as the receiver.

return pb.KeyType_Ed25519
}

func (sk *Ed25519PublicKey) Bytes() ([]byte, error) {
return MarshalPublicKey(sk)
}

func (k *Ed25519PublicKey) Raw() ([]byte, error) {
return (*k.k)[:], nil
}

func (k *Ed25519PublicKey) Equals(o Key) bool {
Expand Down
35 changes: 24 additions & 11 deletions key.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ var PrivKeyUnmarshallers = map[pb.KeyType]PrivKeyUnmarshaller{
// Key represents a crypto key that can be compared to another key
type Key interface {
// Bytes returns a serialized, storeable representation of this key
// DEPRECATED in favor of Marshal / Unmarshal
Bytes() ([]byte, error)

// Equals checks whether two PubKeys are the same
Equals(Key) bool

// Raw returns the raw bytes of the key.
Raw() ([]byte, error)
Copy link
Member

Choose a reason for hiding this comment

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

Note that this is the inverse of the {Pub,Priv}KeyUnmarshaler.


// Type returns the protobof key type.
Type() pb.KeyType
}

// PrivKey represents a private key that can be used to generate a public key,
Expand Down Expand Up @@ -261,7 +268,15 @@ func UnmarshalPublicKey(data []byte) (PubKey, error) {
// MarshalPublicKey converts a public key object into a protobuf serialized
// public key
func MarshalPublicKey(k PubKey) ([]byte, error) {
return k.Bytes()
pbmes := new(pb.PublicKey)
pbmes.Type = k.Type().Enum()
data, err := k.Raw()
if err != nil {
return nil, err
}
pbmes.Data = data

return proto.Marshal(pbmes)
}

// UnmarshalPrivateKey converts a protobuf serialized private key into its
Expand All @@ -283,17 +298,15 @@ func UnmarshalPrivateKey(data []byte) (PrivKey, error) {

// MarshalPrivateKey converts a key object into its protobuf serialized form.
func MarshalPrivateKey(k PrivKey) ([]byte, error) {

switch k.(type) {
case *Ed25519PrivateKey:
return k.Bytes()
case *RsaPrivateKey:
return k.Bytes()
case *Secp256k1PrivateKey:
return k.Bytes()
default:
return nil, ErrBadKeyType
pbmes := new(pb.PrivateKey)
pbmes.Type = k.Type().Enum()
data, err := k.Raw()
if err != nil {
return nil, err
}

pbmes.Data = data
return proto.Marshal(pbmes)
}

// ConfigDecodeKey decodes from b64 (for config file), and unmarshals.
Expand Down
11 changes: 10 additions & 1 deletion key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package crypto_test
import (
. "github.com/libp2p/go-libp2p-crypto"
tu "github.com/libp2p/go-libp2p-crypto/test"

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

"bytes"
"crypto/rand"
"testing"
Expand Down Expand Up @@ -131,6 +132,14 @@ func (pk testkey) Bytes() ([]byte, error) {
return pk, nil
}

func (pk testkey) Type() pb.KeyType {
return pb.KeyType_RSA
}

func (pk testkey) Raw() ([]byte, error) {
return pk, nil
}

func (pk testkey) Equals(k Key) bool {
return KeyEqual(pk, k)
}
32 changes: 17 additions & 15 deletions rsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

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

proto "github.com/gogo/protobuf/proto"
sha256 "github.com/minio/sha256-simd"
)

Expand All @@ -31,17 +30,16 @@ func (pk *RsaPublicKey) Verify(data, sig []byte) (bool, error) {
return true, nil
}

func (pk *RsaPublicKey) Type() pb.KeyType {
return pb.KeyType_RSA
}

func (pk *RsaPublicKey) Bytes() ([]byte, error) {
b, err := x509.MarshalPKIXPublicKey(pk.k)
if err != nil {
return nil, err
}
return MarshalPublicKey(pk)
}

pbmes := new(pb.PublicKey)
typ := pb.KeyType_RSA
pbmes.Type = &typ
pbmes.Data = b
return proto.Marshal(pbmes)
func (pk *RsaPublicKey) Raw() ([]byte, error) {
return x509.MarshalPKIXPublicKey(pk.k)
}

func (pk *RsaPublicKey) Encrypt(b []byte) ([]byte, error) {
Expand Down Expand Up @@ -69,13 +67,17 @@ func (sk *RsaPrivateKey) Decrypt(b []byte) ([]byte, error) {
return rsa.DecryptPKCS1v15(rand.Reader, sk.sk, b)
}

func (sk *RsaPrivateKey) Type() pb.KeyType {
return pb.KeyType_RSA
}

func (sk *RsaPrivateKey) Bytes() ([]byte, error) {
return MarshalPrivateKey(sk)
}

func (sk *RsaPrivateKey) Raw() ([]byte, error) {
b := x509.MarshalPKCS1PrivateKey(sk.sk)
pbmes := new(pb.PrivateKey)
typ := pb.KeyType_RSA
pbmes.Type = &typ
pbmes.Data = b
return proto.Marshal(pbmes)
return b, nil
}

// Equals checks whether this key is equal to another
Expand Down
33 changes: 20 additions & 13 deletions secp256k1.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"

btcec "github.com/btcsuite/btcd/btcec"
proto "github.com/gogo/protobuf/proto"
pb "github.com/libp2p/go-libp2p-crypto/pb"
sha256 "github.com/minio/sha256-simd"
)
Expand Down Expand Up @@ -41,12 +40,16 @@ func UnmarshalSecp256k1PublicKey(data []byte) (PubKey, error) {
return (*Secp256k1PublicKey)(k), nil
}

func (k *Secp256k1PrivateKey) Bytes() ([]byte, error) {
pbmes := new(pb.PrivateKey)
typ := pb.KeyType_Secp256k1
pbmes.Type = &typ
pbmes.Data = (*btcec.PrivateKey)(k).Serialize()
return proto.Marshal(pbmes)
func (sk *Secp256k1PrivateKey) Type() pb.KeyType {
return pb.KeyType_Secp256k1
}

func (sk *Secp256k1PrivateKey) Bytes() ([]byte, error) {
return MarshalPrivateKey(sk)
}

func (k *Secp256k1PrivateKey) Raw() ([]byte, error) {
return (*btcec.PrivateKey)(k).Serialize(), nil
}

func (k *Secp256k1PrivateKey) Equals(o Key) bool {
Expand All @@ -72,12 +75,16 @@ func (k *Secp256k1PrivateKey) GetPublic() PubKey {
return (*Secp256k1PublicKey)((*btcec.PrivateKey)(k).PubKey())
}

func (k *Secp256k1PublicKey) Bytes() ([]byte, error) {
pbmes := new(pb.PublicKey)
typ := pb.KeyType_Secp256k1
pbmes.Type = &typ
pbmes.Data = (*btcec.PublicKey)(k).SerializeCompressed()
return proto.Marshal(pbmes)
func (sk *Secp256k1PublicKey) Type() pb.KeyType {
return pb.KeyType_Secp256k1
}

func (sk *Secp256k1PublicKey) Bytes() ([]byte, error) {
return MarshalPublicKey(sk)
}

func (k *Secp256k1PublicKey) Raw() ([]byte, error) {
return (*btcec.PublicKey)(k).SerializeCompressed(), nil
}

func (k *Secp256k1PublicKey) Equals(o Key) bool {
Expand Down