X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=gost3410%2Fprivate.go;h=7629563c1c736857c5033cb5a12f4a7126939bbb;hb=c19a111af499e05273906e9d5f7c059ab38d6f02;hp=689a9612d7ba3d2ad1f5fe5620ee046876aac6e7;hpb=7ed4c1e0857134c14ef5c03dee48c1cc7a555e98;p=gogost.git diff --git a/gost3410/private.go b/gost3410/private.go index 689a961..7629563 100644 --- a/gost3410/private.go +++ b/gost3410/private.go @@ -1,5 +1,5 @@ // GoGOST -- Pure Go GOST cryptographic functions library -// Copyright (C) 2015-2020 Sergey Matveev +// Copyright (C) 2015-2023 Sergey Matveev // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by @@ -28,10 +28,10 @@ type PrivateKey struct { Key *big.Int } -func NewPrivateKey(curve *Curve, raw []byte) (*PrivateKey, error) { - pointSize := curve.PointSize() +func NewPrivateKey(c *Curve, raw []byte) (*PrivateKey, error) { + pointSize := c.PointSize() if len(raw) != pointSize { - return nil, fmt.Errorf("gogost/gost3410: len(key) != %d", pointSize) + return nil, fmt.Errorf("gogost/gost3410: len(key)=%d != %d", len(raw), pointSize) } key := make([]byte, pointSize) for i := 0; i < len(key); i++ { @@ -41,15 +41,15 @@ func NewPrivateKey(curve *Curve, raw []byte) (*PrivateKey, error) { if k.Cmp(zero) == 0 { return nil, errors.New("gogost/gost3410: zero private key") } - return &PrivateKey{curve, k}, nil + return &PrivateKey{c, k.Mod(k, c.Q)}, nil } -func GenPrivateKey(curve *Curve, rand io.Reader) (*PrivateKey, error) { - raw := make([]byte, curve.PointSize()) +func GenPrivateKey(c *Curve, rand io.Reader) (*PrivateKey, error) { + raw := make([]byte, c.PointSize()) if _, err := io.ReadFull(rand, raw); err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.GenPrivateKey: %w", err) } - return NewPrivateKey(curve, raw) + return NewPrivateKey(c, raw) } func (prv *PrivateKey) Raw() []byte { @@ -61,7 +61,7 @@ func (prv *PrivateKey) Raw() []byte { func (prv *PrivateKey) PublicKey() (*PublicKey, error) { x, y, err := prv.C.Exp(prv.Key, prv.C.X, prv.C.Y) if err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.PublicKey: %w", err) } return &PublicKey{prv.C, x, y}, nil } @@ -80,7 +80,7 @@ func (prv *PrivateKey) SignDigest(digest []byte, rand io.Reader) ([]byte, error) s := big.NewInt(0) Retry: if _, err = io.ReadFull(rand, kRaw); err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err) } k = bytes2big(kRaw) k.Mod(k, prv.C.Q) @@ -89,7 +89,7 @@ Retry: } r, _, err = prv.C.Exp(k, prv.C.X, prv.C.Y) if err != nil { - return nil, err + return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err) } r.Mod(r, prv.C.Q) if r.Cmp(zero) == 0 {