fix: remove public key (#193)

This commit is contained in:
Richard Ramos 2022-02-21 15:21:31 -04:00 committed by GitHub
parent 459bed2a40
commit e9dafb6038
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 130 deletions

View File

@ -11,6 +11,7 @@ import (
"github.com/ethereum/go-ethereum/p2p/enode"
"github.com/ethereum/go-ethereum/p2p/enr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"go.uber.org/zap"
@ -125,7 +126,8 @@ func GetENRandIP(addr ma.Multiaddr, wakuFlags WakuEnrBitfield, privK *ecdsa.Priv
}
func EnodeToMultiAddr(node *enode.Node) (ma.Multiaddr, error) {
peerID, err := peer.IDFromPublicKey(&ECDSAPublicKey{node.Pubkey()})
pubKey := (*crypto.Secp256k1PublicKey)(node.Pubkey())
peerID, err := peer.IDFromPublicKey(pubKey)
if err != nil {
return nil, err
}
@ -134,7 +136,8 @@ func EnodeToMultiAddr(node *enode.Node) (ma.Multiaddr, error) {
}
func Multiaddress(node *enode.Node) ([]ma.Multiaddr, error) {
peerID, err := peer.IDFromPublicKey(&ECDSAPublicKey{node.Pubkey()})
pubKey := (*crypto.Secp256k1PublicKey)(node.Pubkey())
peerID, err := peer.IDFromPublicKey(pubKey)
if err != nil {
return nil, err
}

View File

@ -1,79 +0,0 @@
package utils
import (
"crypto/ecdsa"
"crypto/subtle"
"encoding/asn1"
"errors"
"math/big"
ethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/libp2p/go-libp2p-core/crypto"
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
"github.com/minio/sha256-simd"
)
// Taken from: https://github.com/libp2p/go-libp2p-core/blob/094b0d3f8ba2934339cb35e1a875b11ab6d08839/crypto/ecdsa.go as
// they don't provide a way to set the key
var ErrNilSig = errors.New("sig is nil")
// ECDSASig holds the r and s values of an ECDSA signature
type ECDSASig struct {
R, S *big.Int
}
// ECDSAPublicKey is an implementation of an ECDSA public key
type ECDSAPublicKey struct {
pub *ecdsa.PublicKey
}
// Type returns the key type
func (ePub *ECDSAPublicKey) Type() pb.KeyType {
return pb.KeyType_Secp256k1
}
// Raw returns x509 bytes from a public key
func (ePub *ECDSAPublicKey) Raw() ([]byte, error) {
return ethcrypto.CompressPubkey(ePub.pub), nil
}
// Bytes returns the public key as protobuf bytes
func (ePub *ECDSAPublicKey) Bytes() ([]byte, error) {
return crypto.MarshalPublicKey(ePub)
}
// Equals compares to public keys
func (ePub *ECDSAPublicKey) Equals(o crypto.Key) bool {
return basicEquals(ePub, o)
}
// Verify compares data to a signature
func (ePub *ECDSAPublicKey) Verify(data, sigBytes []byte) (bool, error) {
sig := new(ECDSASig)
if _, err := asn1.Unmarshal(sigBytes, sig); err != nil {
return false, err
}
if sig == nil {
return false, ErrNilSig
}
hash := sha256.Sum256(data)
return ecdsa.Verify(ePub.pub, hash[:], sig.R, sig.S), nil
}
func basicEquals(k1, k2 crypto.Key) bool {
if k1.Type() != k2.Type() {
return false
}
a, err := k1.Raw()
if err != nil {
return false
}
b, err := k2.Raw()
if err != nil {
return false
}
return subtle.ConstantTimeCompare(a, b) == 1
}

View File

@ -1,49 +0,0 @@
package utils
import (
"crypto/ecdsa"
"crypto/rand"
"testing"
"github.com/libp2p/go-libp2p-core/crypto"
pb "github.com/libp2p/go-libp2p-core/crypto/pb"
"github.com/minio/sha256-simd"
"github.com/stretchr/testify/require"
)
func TestBasicEquals(t *testing.T) {
_, pub1, err := crypto.GenerateECDSAKeyPair(rand.Reader)
require.NoError(t, err)
_, pub2, err := crypto.GenerateECDSAKeyPair(rand.Reader)
require.NoError(t, err)
require.False(t, basicEquals(pub1, pub2))
require.True(t, basicEquals(pub1, pub1))
}
func TestSignAndVerify(t *testing.T) {
priv1, err := ecdsa.GenerateKey(crypto.ECDSACurve, rand.Reader)
require.NoError(t, err)
pub1 := ECDSAPublicKey{pub: &priv1.PublicKey}
require.Equal(t, pb.KeyType_Secp256k1, pub1.Type())
msg := []byte("hello world")
data := sha256.Sum256(msg)
sig, err := priv1.Sign(rand.Reader, data[:], nil)
if err != nil {
t.Fatal(err)
}
ok, err := pub1.Verify(msg, sig)
require.NoError(t, err)
require.True(t, ok)
// change data
data[0] = ^data[0]
ok, err = pub1.Verify(data[:], sig)
require.NoError(t, err)
require.False(t, ok)
}