Skip to content

Commit 7139e8b

Browse files
FiloSottilecherrymui
authored andcommitted
[release-branch.go1.17] crypto/elliptic: tolerate zero-padded scalars in generic P-256
Updates #52075 Fixes #52076 Fixes CVE-2022-28327 Change-Id: I595a7514c9a0aa1b9c76aedfc2307e1124271f27 Reviewed-on: https://go-review.googlesource.com/c/go/+/397136 Trust: Filippo Valsorda <[email protected]> Reviewed-by: Julie Qiu <[email protected]>
1 parent eb75219 commit 7139e8b

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/crypto/elliptic/p256.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func p256GetScalar(out *[32]byte, in []byte) {
5252
n := new(big.Int).SetBytes(in)
5353
var scalarBytes []byte
5454

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

src/crypto/elliptic/p256_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,17 @@ func TestP256CombinedMult(t *testing.T) {
153153
t.Errorf("1×G + (-1)×G = (%d, %d), should be ∞", x, y)
154154
}
155155
}
156+
157+
func TestIssue52075(t *testing.T) {
158+
Gx, Gy := P256().Params().Gx, P256().Params().Gy
159+
scalar := make([]byte, 33)
160+
scalar[32] = 1
161+
x, y := P256().ScalarBaseMult(scalar)
162+
if x.Cmp(Gx) != 0 || y.Cmp(Gy) != 0 {
163+
t.Errorf("unexpected output (%v,%v)", x, y)
164+
}
165+
x, y = P256().ScalarMult(Gx, Gy, scalar)
166+
if x.Cmp(Gx) != 0 || y.Cmp(Gy) != 0 {
167+
t.Errorf("unexpected output (%v,%v)", x, y)
168+
}
169+
}

0 commit comments

Comments
 (0)