]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost3410/private.go
1.1 release is ready
[gogost.git] / src / cypherpunks.ru / gogost / gost3410 / private.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2016 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, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package gost3410
18
19 import (
20         "errors"
21         "io"
22         "math/big"
23 )
24
25 type PrivateKey struct {
26         c   *Curve
27         ds  int
28         key *big.Int
29 }
30
31 func NewPrivateKey(curve *Curve, ds DigestSize, raw []byte) (*PrivateKey, error) {
32         key := make([]byte, len(raw))
33         copy(key, raw)
34         reverse(key)
35         k := bytes2big(key)
36         if k.Cmp(zero) == 0 {
37                 return nil, errors.New("zero private key")
38         }
39         return &PrivateKey{curve, int(ds), k}, nil
40 }
41
42 func GenPrivateKey(curve *Curve, ds DigestSize, rand io.Reader) (*PrivateKey, error) {
43         raw := make([]byte, int(ds))
44         if _, err := io.ReadFull(rand, raw); err != nil {
45                 return nil, err
46         }
47         return NewPrivateKey(curve, ds, raw)
48 }
49
50 func (pk *PrivateKey) Raw() []byte {
51         raw := pad(pk.key.Bytes(), pk.ds)
52         reverse(raw)
53         return raw
54 }
55
56 func (pk *PrivateKey) PublicKey() (*PublicKey, error) {
57         x, y, err := pk.c.Exp(pk.key, pk.c.Bx, pk.c.By)
58         if err != nil {
59                 return nil, err
60         }
61         return &PublicKey{pk.c, pk.ds, x, y}, nil
62 }
63
64 func (pk *PrivateKey) SignDigest(digest []byte, rand io.Reader) ([]byte, error) {
65         if len(digest) != pk.ds {
66                 return nil, errors.New("Invalid input digest length")
67         }
68         e := bytes2big(digest)
69         e.Mod(e, pk.c.Q)
70         if e.Cmp(zero) == 0 {
71                 e = big.NewInt(1)
72         }
73         kRaw := make([]byte, pk.ds)
74         var err error
75         var k *big.Int
76         var r *big.Int
77         d := big.NewInt(0)
78         s := big.NewInt(0)
79 Retry:
80         if _, err = io.ReadFull(rand, kRaw); err != nil {
81                 return nil, err
82         }
83         k = bytes2big(kRaw)
84         k.Mod(k, pk.c.Q)
85         if k.Cmp(zero) == 0 {
86                 goto Retry
87         }
88         r, _, err = pk.c.Exp(k, pk.c.Bx, pk.c.By)
89         if err != nil {
90                 return nil, err
91         }
92         r.Mod(r, pk.c.Q)
93         if r.Cmp(zero) == 0 {
94                 goto Retry
95         }
96         d.Mul(pk.key, r)
97         k.Mul(k, e)
98         s.Add(d, k)
99         s.Mod(s, pk.c.Q)
100         if s.Cmp(zero) == 0 {
101                 goto Retry
102         }
103         return append(pad(s.Bytes(), pk.ds), pad(r.Bytes(), pk.ds)...), nil
104 }