Skip to content

Commit

Permalink
Merge pull request #59 from adam-hanna/master
Browse files Browse the repository at this point in the history
adds ability to create keys from crypto.PrivateKey types
  • Loading branch information
Stebalien committed Sep 30, 2019
2 parents 234a74e + 3082b65 commit 5bc6981
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 0 deletions.
40 changes: 40 additions & 0 deletions core/crypto/key_not_openssl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// +build !openssl

package crypto

import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"

btcec "github.com/btcsuite/btcd/btcec"
"golang.org/x/crypto/ed25519"
)

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

switch p := priv.(type) {
case *rsa.PrivateKey:
return &RsaPrivateKey{*p}, &RsaPublicKey{p.PublicKey}, nil

case *ecdsa.PrivateKey:
return &ECDSAPrivateKey{p}, &ECDSAPublicKey{&p.PublicKey}, nil

case *ed25519.PrivateKey:
pubIfc := p.Public()
pub, _ := pubIfc.(ed25519.PublicKey)
return &Ed25519PrivateKey{*p}, &Ed25519PublicKey{pub}, nil

case *btcec.PrivateKey:
sPriv := Secp256k1PrivateKey(*p)
sPub := Secp256k1PublicKey(*p.PubKey())
return &sPriv, &sPub, nil

default:
return nil, nil, ErrBadKeyType
}
}
47 changes: 47 additions & 0 deletions core/crypto/key_openssl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// +build openssl

package crypto

import (
"crypto"
"crypto/ecdsa"
"crypto/rsa"
"crypto/x509"

btcec "github.com/btcsuite/btcd/btcec"
openssl "github.com/libp2p/go-openssl"
"golang.org/x/crypto/ed25519"
)

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

switch p := priv.(type) {
case *rsa.PrivateKey:
pk, err := openssl.LoadPrivateKeyFromDER(x509.MarshalPKCS1PrivateKey(p))
if err != nil {
return nil, nil, err
}

return &opensslPrivateKey{pk}, &opensslPublicKey{pk}, nil

case *ecdsa.PrivateKey:
return &ECDSAPrivateKey{p}, &ECDSAPublicKey{&p.PublicKey}, nil

case *ed25519.PrivateKey:
pubIfc := p.Public()
pub, _ := pubIfc.(ed25519.PublicKey)
return &Ed25519PrivateKey{*p}, &Ed25519PublicKey{pub}, nil

case *btcec.PrivateKey:
sPriv := Secp256k1PrivateKey(*p)
sPub := Secp256k1PublicKey(*p.PubKey())
return &sPriv, &sPub, nil

default:
return nil, nil, ErrBadKeyType
}
}
99 changes: 99 additions & 0 deletions core/crypto/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@ package crypto_test

import (
"bytes"
"crypto"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/rsa"
"fmt"
"testing"

btcec "github.com/btcsuite/btcd/btcec"
. "github.com/libp2p/go-libp2p-core/crypto"
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
"github.com/libp2p/go-libp2p-core/test"
sha256 "github.com/minio/sha256-simd"
"golang.org/x/crypto/ed25519"
)

func TestKeys(t *testing.T) {
Expand All @@ -16,6 +24,97 @@ func TestKeys(t *testing.T) {
}
}

func TestKeyPairFromKey(t *testing.T) {
var (
data = []byte(`hello world`)
hashed = sha256.Sum256(data)
)

privk, err := btcec.NewPrivateKey(btcec.S256())
if err != nil {
t.Fatalf("err generating btcec priv key:\n%v", err)
}
sigK, err := privk.Sign(hashed[:])
if err != nil {
t.Fatalf("err generating btcec sig:\n%v", err)
}

eKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
t.Fatalf("err generating ecdsa priv key:\n%v", err)
}
sigE, err := eKey.Sign(rand.Reader, hashed[:], crypto.SHA256)
if err != nil {
t.Fatalf("err generating ecdsa sig:\n%v", err)
}

rKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
t.Fatalf("err generating rsa priv key:\n%v", err)
}
sigR, err := rKey.Sign(rand.Reader, hashed[:], crypto.SHA256)
if err != nil {
t.Fatalf("err generating rsa sig:\n%v", err)
}

_, edKey, err := ed25519.GenerateKey(rand.Reader)
sigEd := ed25519.Sign(edKey, data[:])
if err != nil {
t.Fatalf("err generating ed25519 sig:\n%v", err)
}

for i, tt := range []struct {
in crypto.PrivateKey
typ pb.KeyType
sig []byte
}{
{
eKey,
ECDSA,
sigE,
},
{
privk,
Secp256k1,
sigK.Serialize(),
},
{
rKey,
RSA,
sigR,
},
{
&edKey,
Ed25519,
sigEd,
},
} {
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
priv, pub, err := KeyPairFromStdKey(tt.in)
if err != nil {
t.Fatal(err)
}

if priv == nil || pub == nil {
t.Errorf("received nil private key or public key: %v, %v", priv, pub)
}

if priv == nil || priv.Type() != tt.typ {
t.Errorf("want %v; got %v", tt.typ, priv.Type())
}

v, err := pub.Verify(data[:], tt.sig)
if err != nil {
t.Error(err)
}

if !v {
t.Error("signature was not verified")
}
})
}
}

func testKeyType(typ int, t *testing.T) {
bits := 512
if typ == RSA {
Expand Down

0 comments on commit 5bc6981

Please sign in to comment.