Merge pull request #648 from Gustav-Simonsson/forward_ecrecover_err_and_remove_dup_checks

Forward and log EC recover err and remove dup pubkey len check
This commit is contained in:
Jeffrey Wilcke 2015-04-07 20:48:56 +02:00
commit 758205b187
4 changed files with 28 additions and 13 deletions

View File

@ -9,6 +9,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/rlp"
)
@ -129,7 +131,12 @@ func (tx *Transaction) PublicKey() []byte {
//pubkey := crypto.Ecrecover(append(hash[:], sig...))
//pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig)
pubkey := crypto.FromECDSAPub(crypto.SigToPub(hash[:], sig))
p, err := crypto.SigToPub(hash[:], sig)
if err != nil {
glog.V(logger.Error).Infof("Could not get pubkey from signature: ", err)
return nil
}
pubkey := crypto.FromECDSAPub(p)
return pubkey
}

View File

@ -5,6 +5,8 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/params"
)
@ -80,9 +82,10 @@ func ecrecoverFunc(in []byte) []byte {
// v needs to be moved to the end
rsv := append(in[64:128], byte(v.Uint64()))
pubKey := crypto.Ecrecover(in[:32], rsv)
pubKey, err := crypto.Ecrecover(in[:32], rsv)
// make sure the public key is a valid one
if pubKey == nil || len(pubKey) != 65 {
if err != nil {
glog.V(logger.Error).Infof("EC RECOVER FAIL: ", err)
return nil
}

View File

@ -68,10 +68,8 @@ func Ripemd160(data []byte) []byte {
return ripemd.Sum(nil)
}
func Ecrecover(hash, sig []byte) []byte {
r, _ := secp256k1.RecoverPubkey(hash, sig)
return r
func Ecrecover(hash, sig []byte) ([]byte, error) {
return secp256k1.RecoverPubkey(hash, sig)
}
// New methods using proper ecdsa keys from the stdlib
@ -145,14 +143,14 @@ func GenerateKey() (*ecdsa.PrivateKey, error) {
return ecdsa.GenerateKey(S256(), rand.Reader)
}
func SigToPub(hash, sig []byte) *ecdsa.PublicKey {
s := Ecrecover(hash, sig)
if s == nil || len(s) != 65 {
return nil
func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) {
s, err := Ecrecover(hash, sig)
if err != nil {
return nil, err
}
x, y := elliptic.Unmarshal(S256(), s)
return &ecdsa.PublicKey{S256(), x, y}
return &ecdsa.PublicKey{S256(), x, y}, nil
}
func Sign(hash []byte, prv *ecdsa.PrivateKey) (sig []byte, err error) {

View File

@ -5,6 +5,8 @@ import (
"time"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
)
type Message struct {
@ -32,7 +34,12 @@ func (self *Message) sign(key *ecdsa.PrivateKey) (err error) {
func (self *Message) Recover() *ecdsa.PublicKey {
defer func() { recover() }() // in case of invalid sig
return crypto.SigToPub(self.hash(), self.Signature)
pub, err := crypto.SigToPub(self.hash(), self.Signature)
if err != nil {
glog.V(logger.Error).Infof("Could not get pubkey from signature: ", err)
return nil
}
return pub
}
func (self *Message) Encrypt(to *ecdsa.PublicKey) (err error) {