aboutsummaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorFelix Lange <fjl@users.noreply.github.com>2017-12-12 05:49:09 +0800
committerGitHub <noreply@github.com>2017-12-12 05:49:09 +0800
commit1a32bdf92cceb7a42e5636e12d95609e17b8f786 (patch)
treeb46644b4e2cd5cfdb44f8ede88c66c05fbbaec9c /crypto
parent2499b1b139d82f2f266ce9c79aebca1568396a51 (diff)
downloaddexon-1a32bdf92cceb7a42e5636e12d95609e17b8f786.tar
dexon-1a32bdf92cceb7a42e5636e12d95609e17b8f786.tar.gz
dexon-1a32bdf92cceb7a42e5636e12d95609e17b8f786.tar.bz2
dexon-1a32bdf92cceb7a42e5636e12d95609e17b8f786.tar.lz
dexon-1a32bdf92cceb7a42e5636e12d95609e17b8f786.tar.xz
dexon-1a32bdf92cceb7a42e5636e12d95609e17b8f786.tar.zst
dexon-1a32bdf92cceb7a42e5636e12d95609e17b8f786.zip
crypto: fix error check in toECDSA (#15632)
With this change, key, err := crypto.HexToECDSA("000000...") returns nil key and an error instead of a non-nil key with nil X and Y inside. Issue found by @guidovranken.
Diffstat (limited to 'crypto')
-rw-r--r--crypto/crypto.go3
-rw-r--r--crypto/crypto_test.go9
2 files changed, 12 insertions, 0 deletions
diff --git a/crypto/crypto.go b/crypto/crypto.go
index 8161769d3..3a98bfb50 100644
--- a/crypto/crypto.go
+++ b/crypto/crypto.go
@@ -98,6 +98,9 @@ func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {
}
priv.D = new(big.Int).SetBytes(d)
priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d)
+ if priv.PublicKey.X == nil {
+ return nil, errors.New("invalid private key")
+ }
return priv, nil
}
diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go
index b4c441e5f..835035462 100644
--- a/crypto/crypto_test.go
+++ b/crypto/crypto_test.go
@@ -40,6 +40,15 @@ func TestKeccak256Hash(t *testing.T) {
checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp)
}
+func TestToECDSAErrors(t *testing.T) {
+ if _, err := HexToECDSA("0000000000000000000000000000000000000000000000000000000000000000"); err == nil {
+ t.Fatal("HexToECDSA should've returned error")
+ }
+ if _, err := HexToECDSA("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); err == nil {
+ t.Fatal("HexToECDSA should've returned error")
+ }
+}
+
func BenchmarkSha3(b *testing.B) {
a := []byte("hello world")
for i := 0; i < b.N; i++ {