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