1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2023 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
26 type PrivateKey struct {
31 // Unmarshal little-endian private key. "raw" must be c.PointSize() length.
32 func NewPrivateKey(c *Curve, raw []byte) (*PrivateKey, error) {
33 pointSize := c.PointSize()
34 if len(raw) != pointSize {
35 return nil, fmt.Errorf("gogost/gost3410: len(key)=%d != %d", len(raw), pointSize)
37 key := make([]byte, pointSize)
38 for i := 0; i < len(key); i++ {
39 key[i] = raw[len(raw)-i-1]
43 return nil, errors.New("gogost/gost3410: zero private key")
45 return &PrivateKey{c, k.Mod(k, c.Q)}, nil
48 func GenPrivateKey(c *Curve, rand io.Reader) (*PrivateKey, error) {
49 raw := make([]byte, c.PointSize())
50 if _, err := io.ReadFull(rand, raw); err != nil {
51 return nil, fmt.Errorf("gogost/gost3410.GenPrivateKey: %w", err)
53 return NewPrivateKey(c, raw)
56 // Marshal little-endian private key. raw will be prv.C.PointSize() length.
57 func (prv *PrivateKey) Raw() (raw []byte) {
58 raw = pad(prv.Key.Bytes(), prv.C.PointSize())
63 func (prv *PrivateKey) PublicKey() (*PublicKey, error) {
64 x, y, err := prv.C.Exp(prv.Key, prv.C.X, prv.C.Y)
66 return nil, fmt.Errorf("gogost/gost3410.PrivateKey.PublicKey: %w", err)
68 return &PublicKey{prv.C, x, y}, nil
71 func (prv *PrivateKey) SignDigest(digest []byte, rand io.Reader) ([]byte, error) {
72 e := bytes2big(digest)
77 kRaw := make([]byte, prv.C.PointSize())
84 if _, err = io.ReadFull(rand, kRaw); err != nil {
85 return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err)
92 r, _, err = prv.C.Exp(k, prv.C.X, prv.C.Y)
94 return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err)
104 if s.Cmp(zero) == 0 {
107 pointSize := prv.C.PointSize()
109 pad(s.Bytes(), pointSize),
110 pad(r.Bytes(), pointSize)...,
114 // Sign the digest. opts argument is unused.
115 func (prv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
116 return prv.SignDigest(digest, rand)
119 func (prv *PrivateKey) Public() crypto.PublicKey {
120 pub, err := prv.PublicKey()
127 type PrivateKeyReverseDigest struct {
131 func (prv *PrivateKeyReverseDigest) Public() crypto.PublicKey {
132 return prv.Prv.Public()
135 func (prv *PrivateKeyReverseDigest) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
136 d := make([]byte, len(digest))
139 return prv.Prv.Sign(rand, d, opts)
142 type PrivateKeyReverseDigestAndSignature struct {
146 func (prv *PrivateKeyReverseDigestAndSignature) Public() crypto.PublicKey {
147 return prv.Prv.Public()
150 func (prv *PrivateKeyReverseDigestAndSignature) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
151 d := make([]byte, len(digest))
154 sign, err := prv.Prv.Sign(rand, d, opts)