From 32e1b104f8fbc0f80bf2b6a93492aa01fa323e35 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Mon, 1 Jun 2015 20:27:20 +0200 Subject: Add EC signature validations before call to libsecp256k1 --- crypto/crypto.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'crypto') diff --git a/crypto/crypto.go b/crypto/crypto.go index 4bbd62f7f..d5291fe1d 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -10,6 +10,7 @@ import ( "fmt" "io" "io/ioutil" + "math/big" "os" "encoding/hex" @@ -151,6 +152,19 @@ func GenerateKey() (*ecdsa.PrivateKey, error) { return ecdsa.GenerateKey(S256(), rand.Reader) } +func ValidateSignatureValues(v byte, r, s *big.Int) bool { + secp256k1n := common.String2Big("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141") + vint := uint32(v) + if r.Cmp(common.Big0) == 0 || s.Cmp(common.Big0) == 0 { + return false + } + if r.Cmp(secp256k1n) < 0 && s.Cmp(secp256k1n) < 0 && (vint == 27 || vint == 28) { + return true + } else { + return false + } +} + func SigToPub(hash, sig []byte) (*ecdsa.PublicKey, error) { s, err := Ecrecover(hash, sig) if err != nil { -- cgit v1.2.3 From edbd902a1b5e2d8d1fdff8e876594eb1859839e8 Mon Sep 17 00:00:00 2001 From: Gustav Simonsson Date: Wed, 3 Jun 2015 14:44:29 +0200 Subject: Initialise curve N value in package init --- crypto/crypto.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crypto') diff --git a/crypto/crypto.go b/crypto/crypto.go index d5291fe1d..9aef44863 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -27,9 +27,12 @@ import ( "golang.org/x/crypto/ripemd160" ) +var secp256k1n *big.Int + func init() { // specify the params for the s256 curve ecies.AddParamsForCurve(S256(), ecies.ECIES_AES128_SHA256) + secp256k1n = common.String2Big("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141") } func Sha3(data ...[]byte) []byte { @@ -153,7 +156,6 @@ func GenerateKey() (*ecdsa.PrivateKey, error) { } func ValidateSignatureValues(v byte, r, s *big.Int) bool { - secp256k1n := common.String2Big("0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141") vint := uint32(v) if r.Cmp(common.Big0) == 0 || s.Cmp(common.Big0) == 0 { return false -- cgit v1.2.3