Skip to content

Commit 6cd6b92

Browse files
dindinwfjl
authored andcommitted
crypto: ensure private keys are < N (#15745)
Fixes #15744
1 parent 908faf8 commit 6cd6b92

1 file changed

Lines changed: 10 additions & 0 deletions

File tree

‎crypto/crypto.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,16 @@ func toECDSA(d []byte, strict bool) (*ecdsa.PrivateKey, error) {
9797
return nil, fmt.Errorf("invalid length, need %d bits", priv.Params().BitSize)
9898
}
9999
priv.D = new(big.Int).SetBytes(d)
100+
101+
// The priv.D must < N
102+
if priv.D.Cmp(secp256k1_N) >= 0 {
103+
return nil, fmt.Errorf("invalid private key, >=N")
104+
}
105+
// The priv.D must not be zero or negative.
106+
if priv.D.Sign() <= 0 {
107+
return nil, fmt.Errorf("invalid private key, zero or negative")
108+
}
109+
100110
priv.PublicKey.X, priv.PublicKey.Y = priv.PublicKey.Curve.ScalarBaseMult(d)
101111
if priv.PublicKey.X == nil {
102112
return nil, errors.New("invalid private key")

0 commit comments

Comments
 (0)