Skip to content

Commit 758205b

Browse files
committed
Merge pull request bnb-chain#648 from Gustav-Simonsson/forward_ecrecover_err_and_remove_dup_checks
Forward and log EC recover err and remove dup pubkey len check
2 parents f597863 + 2591883 commit 758205b

4 files changed

Lines changed: 28 additions & 13 deletions

File tree

‎core/types/transaction.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"github.com/ethereum/go-ethereum/common"
1010
"github.com/ethereum/go-ethereum/crypto"
1111
"github.com/ethereum/go-ethereum/crypto/secp256k1"
12+
"github.com/ethereum/go-ethereum/logger"
13+
"github.com/ethereum/go-ethereum/logger/glog"
1214
"github.com/ethereum/go-ethereum/rlp"
1315
)
1416

@@ -129,7 +131,12 @@ func (tx *Transaction) PublicKey() []byte {
129131

130132
//pubkey := crypto.Ecrecover(append(hash[:], sig...))
131133
//pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig)
132-
pubkey := crypto.FromECDSAPub(crypto.SigToPub(hash[:], sig))
134+
p, err := crypto.SigToPub(hash[:], sig)
135+
if err != nil {
136+
glog.V(logger.Error).Infof("Could not get pubkey from signature: ", err)
137+
return nil
138+
}
139+
pubkey := crypto.FromECDSAPub(p)
133140
return pubkey
134141
}
135142

‎core/vm/address.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55

66
"github.com/ethereum/go-ethereum/common"
77
"github.com/ethereum/go-ethereum/crypto"
8+
"github.com/ethereum/go-ethereum/logger"
9+
"github.com/ethereum/go-ethereum/logger/glog"
810
"github.com/ethereum/go-ethereum/params"
911
)
1012

@@ -80,9 +82,10 @@ func ecrecoverFunc(in []byte) []byte {
8082

8183
// v needs to be moved to the end
8284
rsv := append(in[64:128], byte(v.Uint64()))
83-
pubKey := crypto.Ecrecover(in[:32], rsv)
85+
pubKey, err := crypto.Ecrecover(in[:32], rsv)
8486
// make sure the public key is a valid one
85-
if pubKey == nil || len(pubKey) != 65 {
87+
if err != nil {
88+
glog.V(logger.Error).Infof("EC RECOVER FAIL: ", err)
8689
return nil
8790
}
8891

‎crypto/crypto.go‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,8 @@ func Ripemd160(data []byte) []byte {
6868
return ripemd.Sum(nil)
6969
}
7070

71-
func Ecrecover(hash, sig []byte) []byte {
72-
r, _ := secp256k1.RecoverPubkey(hash, sig)
73-
74-
return r
71+
func Ecrecover(hash, sig []byte) ([]byte, error) {
72+
return secp256k1.RecoverPubkey(hash, sig)
7573
}
7674

7775
// New methods using proper ecdsa keys from the stdlib
@@ -145,14 +143,14 @@ func GenerateKey() (*ecdsa.PrivateKey, error) {
145143
return ecdsa.GenerateKey(S256(), rand.Reader)
146144
}
147145

148-
func SigToPub(hash, sig []byte) *ecdsa.PublicKey {
149-
s := Ecrecover(hash, sig)
150-
if s == nil || len(s) != 65 {
151-
return nil
146+
func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
147+
s, err := Ecrecover(hash, sig)
148+
if err != nil {
149+
return nil, err
152150
}
153151

154152
x, y := elliptic.Unmarshal(S256(), s)
155-
return &ecdsa.PublicKey{S256(), x, y}
153+
return &ecdsa.PublicKey{S256(), x, y}, nil
156154
}
157155

158156
func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {

‎whisper/message.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"time"
66

77
"github.com/ethereum/go-ethereum/crypto"
8+
"github.com/ethereum/go-ethereum/logger"
9+
"github.com/ethereum/go-ethereum/logger/glog"
810
)
911

1012
type Message struct {
@@ -32,7 +34,12 @@ func (self *Message) sign(key *ecdsa.PrivateKey) (err error) {
3234

3335
func (self *Message) Recover() *ecdsa.PublicKey {
3436
defer func() { recover() }() // in case of invalid sig
35-
return crypto.SigToPub(self.hash(), self.Signature)
37+
pub, err := crypto.SigToPub(self.hash(), self.Signature)
38+
if err != nil {
39+
glog.V(logger.Error).Infof("Could not get pubkey from signature: ", err)
40+
return nil
41+
}
42+
return pub
3643
}
3744

3845
func (self *Message) Encrypt(to *ecdsa.PublicKey) (err error) {

0 commit comments

Comments
 (0)