@@ -6,7 +6,6 @@ package elliptic
6
6
7
7
import (
8
8
"crypto/elliptic/internal/nistec"
9
- "crypto/rand"
10
9
"errors"
11
10
"math/big"
12
11
)
@@ -173,39 +172,22 @@ func (curve *nistCurve[Point]) pointToAffine(p Point) (x, y *big.Int) {
173
172
return x , y
174
173
}
175
174
176
- // randomPoint returns a random point on the curve. It's used when Add,
177
- // Double, or ScalarMult are fed a point not on the curve, which is undefined
178
- // behavior. Originally, we used to do the math on it anyway (which allows
179
- // invalid curve attacks) and relied on the caller and Unmarshal to avoid this
180
- // happening in the first place. Now, we just can't construct a nistec Point
181
- // for an invalid pair of coordinates, because that API is safer. If we panic,
182
- // we risk introducing a DoS. If we return nil, we risk a panic. If we return
183
- // the input, ecdsa.Verify might fail open. The safest course seems to be to
184
- // return a valid, random point, which hopefully won't help the attacker.
185
- func (curve * nistCurve [Point ]) randomPoint () (x , y * big.Int ) {
186
- _ , x , y , err := GenerateKey (curve , rand .Reader )
187
- if err != nil {
188
- panic ("crypto/elliptic: failed to generate random point" )
189
- }
190
- return x , y
191
- }
192
-
193
175
func (curve * nistCurve [Point ]) Add (x1 , y1 , x2 , y2 * big.Int ) (* big.Int , * big.Int ) {
194
176
p1 , err := curve .pointFromAffine (x1 , y1 )
195
177
if err != nil {
196
- return curve . randomPoint ( )
178
+ panic ( "crypto/elliptic: Add was called on an invalid point" )
197
179
}
198
180
p2 , err := curve .pointFromAffine (x2 , y2 )
199
181
if err != nil {
200
- return curve . randomPoint ( )
182
+ panic ( "crypto/elliptic: Add was called on an invalid point" )
201
183
}
202
184
return curve .pointToAffine (p1 .Add (p1 , p2 ))
203
185
}
204
186
205
187
func (curve * nistCurve [Point ]) Double (x1 , y1 * big.Int ) (* big.Int , * big.Int ) {
206
188
p , err := curve .pointFromAffine (x1 , y1 )
207
189
if err != nil {
208
- return curve . randomPoint ( )
190
+ panic ( "crypto/elliptic: Double was called on an invalid point" )
209
191
}
210
192
return curve .pointToAffine (p .Double (p ))
211
193
}
@@ -228,12 +210,12 @@ func (curve *nistCurve[Point]) normalizeScalar(scalar []byte) []byte {
228
210
func (curve * nistCurve [Point ]) ScalarMult (Bx , By * big.Int , scalar []byte ) (* big.Int , * big.Int ) {
229
211
p , err := curve .pointFromAffine (Bx , By )
230
212
if err != nil {
231
- return curve . randomPoint ( )
213
+ panic ( "crypto/elliptic: ScalarMult was called on an invalid point" )
232
214
}
233
215
scalar = curve .normalizeScalar (scalar )
234
216
p , err = p .ScalarMult (p , scalar )
235
217
if err != nil {
236
- panic ("elliptic: nistec rejected normalized scalar" )
218
+ panic ("crypto/ elliptic: nistec rejected normalized scalar" )
237
219
}
238
220
return curve .pointToAffine (p )
239
221
}
@@ -242,7 +224,7 @@ func (curve *nistCurve[Point]) ScalarBaseMult(scalar []byte) (*big.Int, *big.Int
242
224
scalar = curve .normalizeScalar (scalar )
243
225
p , err := curve .newPoint ().ScalarBaseMult (scalar )
244
226
if err != nil {
245
- panic ("elliptic: nistec rejected normalized scalar" )
227
+ panic ("crypto/ elliptic: nistec rejected normalized scalar" )
246
228
}
247
229
return curve .pointToAffine (p )
248
230
}
@@ -253,16 +235,16 @@ func (curve *nistCurve[Point]) CombinedMult(Px, Py *big.Int, s1, s2 []byte) (x,
253
235
s1 = curve .normalizeScalar (s1 )
254
236
q , err := curve .newPoint ().ScalarBaseMult (s1 )
255
237
if err != nil {
256
- panic ("elliptic: nistec rejected normalized scalar" )
238
+ panic ("crypto/ elliptic: nistec rejected normalized scalar" )
257
239
}
258
240
p , err := curve .pointFromAffine (Px , Py )
259
241
if err != nil {
260
- return curve . randomPoint ( )
242
+ panic ( "crypto/elliptic: CombinedMult was called on an invalid point" )
261
243
}
262
244
s2 = curve .normalizeScalar (s2 )
263
245
p , err = p .ScalarMult (p , s2 )
264
246
if err != nil {
265
- panic ("elliptic: nistec rejected normalized scalar" )
247
+ panic ("crypto/ elliptic: nistec rejected normalized scalar" )
266
248
}
267
249
return curve .pointToAffine (p .Add (p , q ))
268
250
}
@@ -299,15 +281,15 @@ func (curve *nistCurve[Point]) UnmarshalCompressed(data []byte) (x, y *big.Int)
299
281
func bigFromDecimal (s string ) * big.Int {
300
282
b , ok := new (big.Int ).SetString (s , 10 )
301
283
if ! ok {
302
- panic ("invalid encoding" )
284
+ panic ("crypto/elliptic: internal error: invalid encoding" )
303
285
}
304
286
return b
305
287
}
306
288
307
289
func bigFromHex (s string ) * big.Int {
308
290
b , ok := new (big.Int ).SetString (s , 16 )
309
291
if ! ok {
310
- panic ("invalid encoding" )
292
+ panic ("crypto/elliptic: internal error: invalid encoding" )
311
293
}
312
294
return b
313
295
}
0 commit comments