]> Cypherpunks.ru repositories - gogost.git/blob - gost3410/private.go
18549a29cf1f0f8718af45fef3059ba0183c7977
[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 NewPrivateKeyLE(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 // Unmarshal big-endian private key. "raw" must be c.PointSize() length.
49 func NewPrivateKeyBE(c *Curve, raw []byte) (*PrivateKey, error) {
50         pointSize := c.PointSize()
51         if len(raw) != pointSize {
52                 return nil, fmt.Errorf("gogost/gost3410: len(key)=%d != %d", len(raw), pointSize)
53         }
54         k := bytes2big(raw)
55         if k.Cmp(zero) == 0 {
56                 return nil, errors.New("gogost/gost3410: zero private key")
57         }
58         return &PrivateKey{c, k.Mod(k, c.Q)}, nil
59 }
60
61 // This is an alias for NewPrivateKeyLE().
62 func NewPrivateKey(c *Curve, raw []byte) (*PrivateKey, error) {
63         return NewPrivateKeyLE(c, raw)
64 }
65
66 func GenPrivateKey(c *Curve, rand io.Reader) (*PrivateKey, error) {
67         raw := make([]byte, c.PointSize())
68         if _, err := io.ReadFull(rand, raw); err != nil {
69                 return nil, fmt.Errorf("gogost/gost3410.GenPrivateKey: %w", err)
70         }
71         return NewPrivateKey(c, raw)
72 }
73
74 // Marshal little-endian private key. raw will be prv.C.PointSize() length.
75 func (prv *PrivateKey) RawLE() (raw []byte) {
76         raw = pad(prv.Key.Bytes(), prv.C.PointSize())
77         reverse(raw)
78         return raw
79 }
80
81 // Marshal big-endian private key. raw will be prv.C.PointSize() length.
82 func (prv *PrivateKey) RawBE() (raw []byte) {
83         return pad(prv.Key.Bytes(), prv.C.PointSize())
84 }
85
86 // This is an alias for RawLE().
87 func (prv *PrivateKey) Raw() []byte {
88         return prv.RawLE()
89 }
90
91 func (prv *PrivateKey) PublicKey() (*PublicKey, error) {
92         x, y, err := prv.C.Exp(prv.Key, prv.C.X, prv.C.Y)
93         if err != nil {
94                 return nil, fmt.Errorf("gogost/gost3410.PrivateKey.PublicKey: %w", err)
95         }
96         return &PublicKey{prv.C, x, y}, nil
97 }
98
99 func (prv *PrivateKey) SignDigest(digest []byte, rand io.Reader) ([]byte, error) {
100         e := bytes2big(digest)
101         e.Mod(e, prv.C.Q)
102         if e.Cmp(zero) == 0 {
103                 e = big.NewInt(1)
104         }
105         kRaw := make([]byte, prv.C.PointSize())
106         var err error
107         var k *big.Int
108         var r *big.Int
109         d := big.NewInt(0)
110         s := big.NewInt(0)
111 Retry:
112         if _, err = io.ReadFull(rand, kRaw); err != nil {
113                 return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err)
114         }
115         k = bytes2big(kRaw)
116         k.Mod(k, prv.C.Q)
117         if k.Cmp(zero) == 0 {
118                 goto Retry
119         }
120         r, _, err = prv.C.Exp(k, prv.C.X, prv.C.Y)
121         if err != nil {
122                 return nil, fmt.Errorf("gogost/gost3410.PrivateKey.SignDigest: %w", err)
123         }
124         r.Mod(r, prv.C.Q)
125         if r.Cmp(zero) == 0 {
126                 goto Retry
127         }
128         d.Mul(prv.Key, r)
129         k.Mul(k, e)
130         s.Add(d, k)
131         s.Mod(s, prv.C.Q)
132         if s.Cmp(zero) == 0 {
133                 goto Retry
134         }
135         pointSize := prv.C.PointSize()
136         return append(
137                 pad(s.Bytes(), pointSize),
138                 pad(r.Bytes(), pointSize)...,
139         ), nil
140 }
141
142 // Sign the digest. opts argument is unused.
143 func (prv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
144         return prv.SignDigest(digest, rand)
145 }
146
147 func (prv *PrivateKey) Public() crypto.PublicKey {
148         pub, err := prv.PublicKey()
149         if err != nil {
150                 panic(err)
151         }
152         return pub
153 }
154
155 type PrivateKeyReverseDigest struct {
156         Prv *PrivateKey
157 }
158
159 func (prv *PrivateKeyReverseDigest) Public() crypto.PublicKey {
160         return prv.Prv.Public()
161 }
162
163 func (prv *PrivateKeyReverseDigest) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
164         d := make([]byte, len(digest))
165         copy(d, digest)
166         reverse(d)
167         return prv.Prv.Sign(rand, d, opts)
168 }
169
170 type PrivateKeyReverseDigestAndSignature struct {
171         Prv *PrivateKey
172 }
173
174 func (prv *PrivateKeyReverseDigestAndSignature) Public() crypto.PublicKey {
175         return prv.Prv.Public()
176 }
177
178 func (prv *PrivateKeyReverseDigestAndSignature) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
179         d := make([]byte, len(digest))
180         copy(d, digest)
181         reverse(d)
182         sign, err := prv.Prv.Sign(rand, d, opts)
183         if err != nil {
184                 return sign, err
185         }
186         reverse(sign)
187         return sign, err
188 }