@@ -23,6 +23,7 @@ import (
23
23
"crypto/elliptic"
24
24
"crypto/sha512"
25
25
"encoding/asn1"
26
+ "errors"
26
27
"io"
27
28
"math/big"
28
29
)
@@ -129,6 +130,8 @@ func fermatInverse(k, N *big.Int) *big.Int {
129
130
return new (big.Int ).Exp (k , nMinus2 , N )
130
131
}
131
132
133
+ var errZeroParam = errors .New ("zero parameter" )
134
+
132
135
// Sign signs an arbitrary length hash (which should be the result of hashing a
133
136
// larger message) using the private key, priv. It returns the signature as a
134
137
// pair of integers. The security of the private key depends on the entropy of
@@ -169,7 +172,9 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err
169
172
// See [NSA] 3.4.1
170
173
c := priv .PublicKey .Curve
171
174
N := c .Params ().N
172
-
175
+ if N .Sign () == 0 {
176
+ return nil , nil , errZeroParam
177
+ }
173
178
var k , kInv * big.Int
174
179
for {
175
180
for {
@@ -179,7 +184,7 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err
179
184
return
180
185
}
181
186
182
- kInv = fermatInverse (k , N )
187
+ kInv = fermatInverse (k , N ) // N != 0
183
188
r , _ = priv .Curve .ScalarBaseMult (k .Bytes ())
184
189
r .Mod (r , N )
185
190
if r .Sign () != 0 {
@@ -191,7 +196,7 @@ func Sign(rand io.Reader, priv *PrivateKey, hash []byte) (r, s *big.Int, err err
191
196
s = new (big.Int ).Mul (priv .D , r )
192
197
s .Add (s , e )
193
198
s .Mul (s , kInv )
194
- s .Mod (s , N )
199
+ s .Mod (s , N ) // N != 0
195
200
if s .Sign () != 0 {
196
201
break
197
202
}
0 commit comments