1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2016 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
25 type PrivateKey struct {
31 func NewPrivateKey(curve *Curve, ds DigestSize, raw []byte) (*PrivateKey, error) {
32 key := make([]byte, len(raw))
37 return nil, errors.New("zero private key")
39 return &PrivateKey{curve, int(ds), k}, nil
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 {
47 return NewPrivateKey(curve, ds, raw)
50 func (pk *PrivateKey) Raw() []byte {
51 raw := pad(pk.key.Bytes(), pk.ds)
56 func (pk *PrivateKey) PublicKey() (*PublicKey, error) {
57 x, y, err := pk.c.Exp(pk.key, pk.c.Bx, pk.c.By)
61 return &PublicKey{pk.c, pk.ds, x, y}, nil
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")
68 e := bytes2big(digest)
73 kRaw := make([]byte, pk.ds)
80 if _, err = io.ReadFull(rand, kRaw); err != nil {
88 r, _, err = pk.c.Exp(k, pk.c.Bx, pk.c.By)
100 if s.Cmp(zero) == 0 {
103 return append(pad(s.Bytes(), pk.ds), pad(r.Bytes(), pk.ds)...), nil