Skip to content

Commit 3706584

Browse files
committed
crypto/elliptic: tolerate zero-padded scalars in generic P-256
Fixes golang#52075 Change-Id: I595a7514c9a0aa1b9c76aedfc2307e1124271f27 Reviewed-on: https://go-review.googlesource.com/c/go/+/397135 Trust: Filippo Valsorda <filippo@golang.org> Run-TryBot: Filippo Valsorda <filippo@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Roland Shoemaker <roland@golang.org>
1 parent 5138401 commit 3706584

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/crypto/elliptic/p256.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func p256GetScalar(out *[32]byte, in []byte) {
5151
n := new(big.Int).SetBytes(in)
5252
var scalarBytes []byte
5353

54-
if n.Cmp(p256Params.N) >= 0 {
54+
if n.Cmp(p256Params.N) >= 0 || len(in) > len(out) {
5555
n.Mod(n, p256Params.N)
5656
scalarBytes = n.Bytes()
5757
} else {

src/crypto/elliptic/p256_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,17 @@ func TestP256CombinedMult(t *testing.T) {
136136
t.Errorf("1×G + (-1)×G = (%d, %d), should be ∞", x, y)
137137
}
138138
}
139+
140+
func TestIssue52075(t *testing.T) {
141+
Gx, Gy := P256().Params().Gx, P256().Params().Gy
142+
scalar := make([]byte, 33)
143+
scalar[32] = 1
144+
x, y := P256().ScalarBaseMult(scalar)
145+
if x.Cmp(Gx) != 0 || y.Cmp(Gy) != 0 {
146+
t.Errorf("unexpected output (%v,%v)", x, y)
147+
}
148+
x, y = P256().ScalarMult(Gx, Gy, scalar)
149+
if x.Cmp(Gx) != 0 || y.Cmp(Gy) != 0 {
150+
t.Errorf("unexpected output (%v,%v)", x, y)
151+
}
152+
}

0 commit comments

Comments
 (0)