]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost3410/private.go
2af8a4a012bd39539ee91b8d959e674d900119c2
[gogost.git] / src / cypherpunks.ru / gogost / gost3410 / private.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2019 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         mode Mode
28         key  *big.Int
29 }
30
31 func NewPrivateKey(curve *Curve, mode Mode, raw []byte) (*PrivateKey, error) {
32         if len(raw) != int(mode) {
33                 errors.New("Invalid private key length")
34         }
35         key := make([]byte, int(mode))
36         copy(key, raw)
37         reverse(key)
38         k := bytes2big(key)
39         if k.Cmp(zero) == 0 {
40                 return nil, errors.New("Zero private key")
41         }
42         return &PrivateKey{curve, mode, k}, nil
43 }
44
45 func GenPrivateKey(curve *Curve, mode Mode, rand io.Reader) (*PrivateKey, error) {
46         raw := make([]byte, int(mode))
47         if _, err := io.ReadFull(rand, raw); err != nil {
48                 return nil, err
49         }
50         return NewPrivateKey(curve, mode, raw)
51 }
52
53 func (prv *PrivateKey) Raw() []byte {
54         raw := pad(prv.key.Bytes(), int(prv.mode))
55         reverse(raw)
56         return raw
57 }
58
59 func (prv *PrivateKey) PublicKey() (*PublicKey, error) {
60         x, y, err := prv.c.Exp(prv.key, prv.c.Bx, prv.c.By)
61         if err != nil {
62                 return nil, err
63         }
64         return &PublicKey{prv.c, prv.mode, x, y}, nil
65 }
66
67 func (prv *PrivateKey) SignDigest(digest []byte, rand io.Reader) ([]byte, error) {
68         e := bytes2big(digest)
69         e.Mod(e, prv.c.Q)
70         if e.Cmp(zero) == 0 {
71                 e = big.NewInt(1)
72         }
73         kRaw := make([]byte, int(prv.mode))
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, prv.c.Q)
85         if k.Cmp(zero) == 0 {
86                 goto Retry
87         }
88         r, _, err = prv.c.Exp(k, prv.c.Bx, prv.c.By)
89         if err != nil {
90                 return nil, err
91         }
92         r.Mod(r, prv.c.Q)
93         if r.Cmp(zero) == 0 {
94                 goto Retry
95         }
96         d.Mul(prv.key, r)
97         k.Mul(k, e)
98         s.Add(d, k)
99         s.Mod(s, prv.c.Q)
100         if s.Cmp(zero) == 0 {
101                 goto Retry
102         }
103         return append(
104                 pad(s.Bytes(), int(prv.mode)),
105                 pad(r.Bytes(), int(prv.mode))...,
106         ), nil
107 }