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