mirror of
https://github.com/golang/go
synced 2024-11-24 09:30:07 -07:00
crypto/elliptic: tolerate zero-padded scalars in generic P-256
Fixes #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>
This commit is contained in:
parent
5138401209
commit
37065847d8
@ -51,7 +51,7 @@ func p256GetScalar(out *[32]byte, in []byte) {
|
|||||||
n := new(big.Int).SetBytes(in)
|
n := new(big.Int).SetBytes(in)
|
||||||
var scalarBytes []byte
|
var scalarBytes []byte
|
||||||
|
|
||||||
if n.Cmp(p256Params.N) >= 0 {
|
if n.Cmp(p256Params.N) >= 0 || len(in) > len(out) {
|
||||||
n.Mod(n, p256Params.N)
|
n.Mod(n, p256Params.N)
|
||||||
scalarBytes = n.Bytes()
|
scalarBytes = n.Bytes()
|
||||||
} else {
|
} else {
|
||||||
|
@ -136,3 +136,17 @@ func TestP256CombinedMult(t *testing.T) {
|
|||||||
t.Errorf("1×G + (-1)×G = (%d, %d), should be ∞", x, y)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user