]> Cypherpunks.ru repositories - gogost.git/blob - gost3410/private.go
983d8d2849399c80a5617e8a5917180f9b96eb75
[gogost.git] / gost3410 / private.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2023 Sergey Matveev <stargrave@stargrave.org>
3 //
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.
7 //
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.
12 //
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/>.
15
16 package gost3410
17
18 import (
19         "crypto"
20         "errors"
21         "fmt"
22         "io"
23         "math/big"
24 )
25
26 type PrivateKey struct {
27         C   *Curve
28         Key *big.Int
29 }
30
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)
36         }
37         key := make([]byte, pointSize)
38         for i := 0; i < len(key); i++ {
39                 key[i] = raw[len(raw)-i-1]
40         }
41         k := bytes2big(key)
42         if k.Cmp(zero) == 0 {
43                 return nil, errors.New("gogost/gost3410: zero private key")
44         }
45         return &PrivateKey{c, k.Mod(k, c.Q)}, nil
46 }
47
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)
52         }
53         return NewPrivateKey(c, raw)
54 }
55
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())
59         reverse(raw)
60         return raw
61 }
62
63 func (prv *PrivateKey) PublicKey() (*PublicKey, error) {
64         x, y, err := prv.C.Exp(prv.Key, prv.C.X, prv.C.Y)
65         if err != nil {
66                 return nil, fmt.Errorf("gogost/gost3410.PrivateKey.PublicKey: %w", err)
67         }
68         return &PublicKey{prv.C, x, y}, nil
69 }
70
71 func (prv *PrivateKey) SignDigest(digest []byte, rand io.Reader) ([]byte, error) {
72         e := bytes2big(digest)
73         e.Mod(e, prv.C.Q)
74         if e.Cmp(zero) == 0 {
75                 e = big.NewInt(1)
76         }
77         kRaw := make([]byte, prv.C.PointSize())
78         var err error
79         var k *big.Int
80         var r *big.Int
81         d := big.NewInt(0)
82         s := big.NewInt(0)
83 Retry:
84         if _, err = io.ReadFull(rand, kRaw); err != nil {
85                 return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err)
86         }
87         k = bytes2big(kRaw)
88         k.Mod(k, prv.C.Q)
89         if k.Cmp(zero) == 0 {
90                 goto Retry
91         }
92         r, _, err = prv.C.Exp(k, prv.C.X, prv.C.Y)
93         if err != nil {
94                 return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err)
95         }
96         r.Mod(r, prv.C.Q)
97         if r.Cmp(zero) == 0 {
98                 goto Retry
99         }
100         d.Mul(prv.Key, r)
101         k.Mul(k, e)
102         s.Add(d, k)
103         s.Mod(s, prv.C.Q)
104         if s.Cmp(zero) == 0 {
105                 goto Retry
106         }
107         pointSize := prv.C.PointSize()
108         return append(
109                 pad(s.Bytes(), pointSize),
110                 pad(r.Bytes(), pointSize)...,
111         ), nil
112 }
113
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)
117 }
118
119 func (prv *PrivateKey) Public() crypto.PublicKey {
120         pub, err := prv.PublicKey()
121         if err != nil {
122                 panic(err)
123         }
124         return pub
125 }
126
127 type PrivateKeyReverseDigest struct {
128         Prv *PrivateKey
129 }
130
131 func (prv *PrivateKeyReverseDigest) Public() crypto.PublicKey {
132         return prv.Prv.Public()
133 }
134
135 func (prv *PrivateKeyReverseDigest) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
136         d := make([]byte, len(digest))
137         copy(d, digest)
138         reverse(d)
139         return prv.Prv.Sign(rand, d, opts)
140 }
141
142 type PrivateKeyReverseDigestAndSignature struct {
143         Prv *PrivateKey
144 }
145
146 func (prv *PrivateKeyReverseDigestAndSignature) Public() crypto.PublicKey {
147         return prv.Prv.Public()
148 }
149
150 func (prv *PrivateKeyReverseDigestAndSignature) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
151         d := make([]byte, len(digest))
152         copy(d, digest)
153         reverse(d)
154         sign, err := prv.Prv.Sign(rand, d, opts)
155         if err != nil {
156                 return sign, err
157         }
158         reverse(sign)
159         return sign, err
160 }