Skip to content

Commit

Permalink
[release-branch.go1.18] crypto/elliptic: tolerate zero-padded scalars…
Browse files Browse the repository at this point in the history
… in generic P-256

Updates #52075
Fixes #52077
Fixes CVE-2022-28327

Change-Id: I595a7514c9a0aa1b9c76aedfc2307e1124271f27
Reviewed-on: https://go-review.googlesource.com/c/go/+/397137
Trust: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Julie Qiu <julie@golang.org>
  • Loading branch information
FiloSottile authored and cherrymui committed Apr 6, 2022
1 parent f92bfa4 commit c9b9a01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/crypto/elliptic/p256.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func p256GetScalar(out *[32]byte, in []byte) {
n := new(big.Int).SetBytes(in)
var scalarBytes []byte

if n.Cmp(p256Params.N) >= 0 {
if n.Cmp(p256Params.N) >= 0 || len(in) > len(out) {
n.Mod(n, p256Params.N)
scalarBytes = n.Bytes()
} else {
Expand Down
14 changes: 14 additions & 0 deletions src/crypto/elliptic/p256_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,17 @@ func TestP256CombinedMult(t *testing.T) {
t.Errorf("1×G + (-1)×G = (%d, %d), should be ∞", x, y)
}
}

func TestIssue52075(t *testing.T) {
Gx, Gy := P256().Params().Gx, P256().Params().Gy
scalar := make([]byte, 33)
scalar[32] = 1
x, y := P256().ScalarBaseMult(scalar)
if x.Cmp(Gx) != 0 || y.Cmp(Gy) != 0 {
t.Errorf("unexpected output (%v,%v)", x, y)
}
x, y = P256().ScalarMult(Gx, Gy, scalar)
if x.Cmp(Gx) != 0 || y.Cmp(Gy) != 0 {
t.Errorf("unexpected output (%v,%v)", x, y)
}
}

0 comments on commit c9b9a01

Please sign in to comment.