X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=src%2Fcypherpunks.ru%2Fgogost%2Fgost3410%2Fprivate.go;h=045c8185b94458e7ad2c9be153bd37eeb99f04ed;hb=91562b3cf4aad503c493aa7b69abfbb07b46e63a;hp=9e501078650cf3da9a37cf0861007ee34e09907f;hpb=d1cbfa307ebcb72b61f926dfdbc21175a3eec1a8;p=gogost.git diff --git a/src/cypherpunks.ru/gogost/gost3410/private.go b/src/cypherpunks.ru/gogost/gost3410/private.go index 9e50107..045c818 100644 --- a/src/cypherpunks.ru/gogost/gost3410/private.go +++ b/src/cypherpunks.ru/gogost/gost3410/private.go @@ -1,10 +1,9 @@ // GoGOST -- Pure Go GOST cryptographic functions library -// Copyright (C) 2015-2018 Sergey Matveev +// Copyright (C) 2015-2019 Sergey Matveev // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. +// the Free Software Foundation, version 3 of the License. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -17,24 +16,26 @@ package gost3410 import ( + "crypto" "errors" "io" "math/big" ) type PrivateKey struct { - c *Curve - mode Mode - key *big.Int + C *Curve + Mode Mode + Key *big.Int } func NewPrivateKey(curve *Curve, mode Mode, raw []byte) (*PrivateKey, error) { if len(raw) != int(mode) { - errors.New("Invalid private key length") + return nil, errors.New("Invalid private key length") } key := make([]byte, int(mode)) - copy(key, raw) - reverse(key) + for i := 0; i < len(key); i++ { + key[i] = raw[len(raw)-i-1] + } k := bytes2big(key) if k.Cmp(zero) == 0 { return nil, errors.New("Zero private key") @@ -51,26 +52,26 @@ func GenPrivateKey(curve *Curve, mode Mode, rand io.Reader) (*PrivateKey, error) } func (prv *PrivateKey) Raw() []byte { - raw := pad(prv.key.Bytes(), int(prv.mode)) + raw := pad(prv.Key.Bytes(), int(prv.Mode)) reverse(raw) return raw } func (prv *PrivateKey) PublicKey() (*PublicKey, error) { - x, y, err := prv.c.Exp(prv.key, prv.c.Bx, prv.c.By) + x, y, err := prv.C.Exp(prv.Key, prv.C.X, prv.C.Y) if err != nil { return nil, err } - return &PublicKey{prv.c, prv.mode, x, y}, nil + return &PublicKey{prv.C, prv.Mode, x, y}, nil } func (prv *PrivateKey) SignDigest(digest []byte, rand io.Reader) ([]byte, error) { e := bytes2big(digest) - e.Mod(e, prv.c.Q) + e.Mod(e, prv.C.Q) if e.Cmp(zero) == 0 { e = big.NewInt(1) } - kRaw := make([]byte, int(prv.mode)) + kRaw := make([]byte, int(prv.Mode)) var err error var k *big.Int var r *big.Int @@ -81,27 +82,39 @@ Retry: return nil, err } k = bytes2big(kRaw) - k.Mod(k, prv.c.Q) + k.Mod(k, prv.C.Q) if k.Cmp(zero) == 0 { goto Retry } - r, _, err = prv.c.Exp(k, prv.c.Bx, prv.c.By) + r, _, err = prv.C.Exp(k, prv.C.X, prv.C.Y) if err != nil { return nil, err } - r.Mod(r, prv.c.Q) + r.Mod(r, prv.C.Q) if r.Cmp(zero) == 0 { goto Retry } - d.Mul(prv.key, r) + d.Mul(prv.Key, r) k.Mul(k, e) s.Add(d, k) - s.Mod(s, prv.c.Q) + s.Mod(s, prv.C.Q) if s.Cmp(zero) == 0 { goto Retry } return append( - pad(s.Bytes(), int(prv.mode)), - pad(r.Bytes(), int(prv.mode))..., + pad(s.Bytes(), int(prv.Mode)), + pad(r.Bytes(), int(prv.Mode))..., ), nil } + +func (prv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) { + return prv.SignDigest(digest, rand) +} + +func (prv *PrivateKey) Public() crypto.PublicKey { + pub, err := prv.PublicKey() + if err != nil { + panic(err) + } + return pub +}