From: Sergey Matveev Date: Wed, 14 Dec 2016 08:48:24 +0000 (+0300) Subject: Fix private key value's assertion: it can not be equal to zero, not to the one X-Git-Tag: 3.0~40 X-Git-Url: http://www.git.cypherpunks.ru/?p=gogost.git;a=commitdiff_plain;h=b7c8ff70c07784ae7d8d854f02c628e88c32a8a2 Fix private key value's assertion: it can not be equal to zero, not to the one --- diff --git a/src/cypherpunks.ru/gogost/gost3410/2001_test.go b/src/cypherpunks.ru/gogost/gost3410/2001_test.go index 3c13122..a21c195 100644 --- a/src/cypherpunks.ru/gogost/gost3410/2001_test.go +++ b/src/cypherpunks.ru/gogost/gost3410/2001_test.go @@ -161,3 +161,24 @@ func BenchmarkVerify2001(b *testing.B) { pub.VerifyDigest(digest, sign) } } + +func TestPrvEqualsTo1(t *testing.T) { + c, _ := NewCurveFromParams(CurveParamsGostR34102001Test) + prv, err := NewPrivateKey(c, Mode2001, []byte{0x01}) + if err != nil { + t.FailNow() + } + pub, err := prv.PublicKey() + if err != nil { + t.FailNow() + } + digest := []byte{0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01} + sign, err := prv.SignDigest(digest, rand.Reader) + if err != nil { + t.FailNow() + } + valid, err := pub.VerifyDigest(digest, sign) + if err != nil || !valid { + t.FailNow() + } +} diff --git a/src/cypherpunks.ru/gogost/gost3410/curve.go b/src/cypherpunks.ru/gogost/gost3410/curve.go index 70f5a66..fb00375 100644 --- a/src/cypherpunks.ru/gogost/gost3410/curve.go +++ b/src/cypherpunks.ru/gogost/gost3410/curve.go @@ -116,14 +116,14 @@ func (c *Curve) add(p1x, p1y, p2x, p2y *big.Int) { } func (c *Curve) Exp(degree, xS, yS *big.Int) (*big.Int, *big.Int, error) { + if degree.Cmp(zero) == 0 { + return nil, nil, errors.New("Bad degree value") + } dg := big.NewInt(0).Sub(degree, bigInt1) tx := big.NewInt(0).Set(xS) ty := big.NewInt(0).Set(yS) cx := big.NewInt(0).Set(xS) cy := big.NewInt(0).Set(yS) - if dg.Cmp(zero) == 0 { - return nil, nil, errors.New("Bad degree value") - } for dg.Cmp(zero) != 0 { if dg.Bit(0) == 1 { c.add(tx, ty, cx, cy)