Skip to content

Commit

Permalink
add functions for converting libp2p keys to stdlib variants (#125)
Browse files Browse the repository at this point in the history
  • Loading branch information
vibhavp authored Mar 6, 2020
1 parent 532eb83 commit 878b651
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/crypto/ecdsa.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ var (
ErrNilSig = errors.New("sig is nil")
// ErrNilPrivateKey is returned when a nil private key is provided
ErrNilPrivateKey = errors.New("private key is nil")
// ErrNilPublicKey is returned when a nil public key is provided
ErrNilPublicKey = errors.New("public key is nil")
// ECDSACurve is the default ecdsa curve used
ECDSACurve = elliptic.P256()
)
Expand Down
40 changes: 40 additions & 0 deletions core/crypto/key_not_openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,43 @@ func KeyPairFromStdKey(priv crypto.PrivateKey) (PrivKey, PubKey, error) {
return nil, nil, ErrBadKeyType
}
}

// PrivKeyToStdKey converts libp2p/go-libp2p-core/crypto private keys to standard library (and secp256k1) private keys
func PrivKeyToStdKey(priv PrivKey) (crypto.PrivateKey, error) {
if priv == nil {
return nil, ErrNilPrivateKey
}

switch p := priv.(type) {
case *RsaPrivateKey:
return &p.sk, nil
case *ECDSAPrivateKey:
return p.priv, nil
case *Ed25519PrivateKey:
return &p.k, nil
case *Secp256k1PrivateKey:
return p, nil
default:
return nil, ErrBadKeyType
}
}

// PubKeyToStdKey converts libp2p/go-libp2p-core/crypto private keys to standard library (and secp256k1) public keys
func PubKeyToStdKey(pub PubKey) (crypto.PublicKey, error) {
if pub == nil {
return nil, ErrNilPublicKey
}

switch p := pub.(type) {
case *RsaPublicKey:
return &p.k, nil
case *ECDSAPublicKey:
return p.pub, nil
case *Ed25519PublicKey:
return p.k, nil
case *Secp256k1PublicKey:
return p, nil
default:
return nil, ErrBadKeyType
}
}
47 changes: 47 additions & 0 deletions core/crypto/key_openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,50 @@ func KeyPairFromStdKey(priv crypto.PrivateKey) (PrivKey, PubKey, error) {
return nil, nil, ErrBadKeyType
}
}

// PrivKeyToStdKey converts libp2p/go-libp2p-core/crypto private keys to standard library (and secp256k1) private keys
func PrivKeyToStdKey(priv PrivKey) (crypto.PrivateKey, error) {
if priv == nil {
return nil, ErrNilPrivateKey
}
switch p := priv.(type) {
case *opensslPrivateKey:
raw, err := p.Raw()
if err != nil {
return nil, err
}
return x509.ParsePKCS1PrivateKey(raw)
case *ECDSAPrivateKey:
return p.priv, nil
case *Ed25519PrivateKey:
return &p.k, nil
case *Secp256k1PrivateKey:
return p, nil
default:
return nil, ErrBadKeyType
}
}

// PubKeyToStdKey converts libp2p/go-libp2p-core/crypto private keys to standard library (and secp256k1) public keys
func PubKeyToStdKey(pub PubKey) (crypto.PublicKey, error) {
if pub == nil {
return nil, ErrNilPublicKey
}

switch p := pub.(type) {
case *opensslPublicKey:
raw, err := p.Raw()
if err != nil {
return nil, err
}
return x509.ParsePKIXPublicKey(raw)
case *ECDSAPublicKey:
return p.pub, nil
case *Ed25519PublicKey:
return p.k, nil
case *Secp256k1PublicKey:
return p, nil
default:
return nil, ErrBadKeyType
}
}
61 changes: 61 additions & 0 deletions core/crypto/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"fmt"
"reflect"
"testing"

btcec "github.com/btcsuite/btcd/btcec"
Expand Down Expand Up @@ -111,6 +113,65 @@ func TestKeyPairFromKey(t *testing.T) {
if !v {
t.Error("signature was not verified")
}

stdPub, err := PubKeyToStdKey(pub)
if stdPub == nil {
t.Errorf("err getting std public key from key: %v", err)
}

var stdPubBytes []byte

switch p := stdPub.(type) {
case *Secp256k1PublicKey:
stdPubBytes, err = p.Raw()
case ed25519.PublicKey:
stdPubBytes = []byte(p)
default:
stdPubBytes, err = x509.MarshalPKIXPublicKey(stdPub)
}

if err != nil {
t.Errorf("Error while marshaling %v key: %v", reflect.TypeOf(stdPub), err)
}

pubBytes, err := pub.Raw()
if err != nil {
t.Errorf("err getting raw bytes for %v key: %v", reflect.TypeOf(pub), err)
}
if !bytes.Equal(stdPubBytes, pubBytes) {
t.Errorf("err roundtripping %v key", reflect.TypeOf(pub))
}

stdPriv, err := PrivKeyToStdKey(priv)
if stdPub == nil {
t.Errorf("err getting std private key from key: %v", err)
}

var stdPrivBytes []byte

switch p := stdPriv.(type) {
case *Secp256k1PrivateKey:
stdPrivBytes, err = p.Raw()
case *ecdsa.PrivateKey:
stdPrivBytes, err = x509.MarshalECPrivateKey(p)
case *ed25519.PrivateKey:
stdPrivBytes = *p
case *rsa.PrivateKey:
stdPrivBytes = x509.MarshalPKCS1PrivateKey(p)
}

if err != nil {
t.Errorf("err marshaling %v key: %v", reflect.TypeOf(stdPriv), err)
}

privBytes, err := priv.Raw()
if err != nil {
t.Errorf("err getting raw bytes for %v key: %v", reflect.TypeOf(priv), err)
}

if !bytes.Equal(stdPrivBytes, privBytes) {
t.Errorf("err roundtripping %v key", reflect.TypeOf(priv))
}
})
}
}
Expand Down

0 comments on commit 878b651

Please sign in to comment.