key_not_openssl.go 1.81 KB
Newer Older
1 2 3 4 5 6 7
// +build !openssl

package crypto

import (
	"crypto"
	"crypto/ecdsa"
8
	"crypto/ed25519"
9 10 11 12 13
	"crypto/rsa"

	btcec "github.com/btcsuite/btcd/btcec"
)

tavit ohanian's avatar
tavit ohanian committed
14
// KeyPairFromStdKey wraps standard library (and secp256k1) private keys in p2p/go-p2p-core/crypto keys
15
func KeyPairFromStdKey(priv crypto.PrivateKey) (PrivKey, PubKey, error) {
16 17 18 19 20 21
	if priv == nil {
		return nil, nil, ErrNilPrivateKey
	}

	switch p := priv.(type) {
	case *rsa.PrivateKey:
22
		return &RsaPrivateKey{*p}, &RsaPublicKey{k: p.PublicKey}, nil
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40

	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
	}
}
41

tavit ohanian's avatar
tavit ohanian committed
42
// PrivKeyToStdKey converts p2p/go-p2p-core/crypto private keys to standard library (and secp256k1) private keys
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
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
	}
}

tavit ohanian's avatar
tavit ohanian committed
62
// PubKeyToStdKey converts p2p/go-p2p-core/crypto private keys to standard library (and secp256k1) public keys
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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
	}
}