]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost3410/public.go
Make Public/PrivateKey structure elements public for convenience
[gogost.git] / src / cypherpunks.ru / gogost / gost3410 / public.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         "math/big"
22 )
23
24 type PublicKey struct {
25         C    *Curve
26         Mode Mode
27         X    *big.Int
28         Y    *big.Int
29 }
30
31 func NewPublicKey(curve *Curve, mode Mode, raw []byte) (*PublicKey, error) {
32         key := make([]byte, 2*int(mode))
33         if len(raw) != len(key) {
34                 return nil, errors.New("Invalid public key length")
35         }
36         for i := 0; i < len(key); i++ {
37                 key[i] = raw[len(raw)-i-1]
38         }
39         return &PublicKey{
40                 curve,
41                 mode,
42                 bytes2big(key[int(mode) : 2*int(mode)]),
43                 bytes2big(key[:int(mode)]),
44         }, nil
45 }
46
47 func (pub *PublicKey) Raw() []byte {
48         raw := append(
49                 pad(pub.Y.Bytes(), int(pub.Mode)),
50                 pad(pub.X.Bytes(), int(pub.Mode))...,
51         )
52         reverse(raw)
53         return raw
54 }
55
56 func (pub *PublicKey) VerifyDigest(digest, signature []byte) (bool, error) {
57         if len(signature) != 2*int(pub.Mode) {
58                 return false, errors.New("Invalid signature length")
59         }
60         s := bytes2big(signature[:pub.Mode])
61         r := bytes2big(signature[pub.Mode:])
62         if r.Cmp(zero) <= 0 || r.Cmp(pub.C.Q) >= 0 || s.Cmp(zero) <= 0 || s.Cmp(pub.C.Q) >= 0 {
63                 return false, nil
64         }
65         e := bytes2big(digest)
66         e.Mod(e, pub.C.Q)
67         if e.Cmp(zero) == 0 {
68                 e = big.NewInt(1)
69         }
70         v := big.NewInt(0)
71         v.ModInverse(e, pub.C.Q)
72         z1 := big.NewInt(0)
73         z2 := big.NewInt(0)
74         z1.Mul(s, v)
75         z1.Mod(z1, pub.C.Q)
76         z2.Mul(r, v)
77         z2.Mod(z2, pub.C.Q)
78         z2.Sub(pub.C.Q, z2)
79         p1x, p1y, err := pub.C.Exp(z1, pub.C.X, pub.C.Y)
80         if err != nil {
81                 return false, err
82         }
83         q1x, q1y, err := pub.C.Exp(z2, pub.X, pub.Y)
84         if err != nil {
85                 return false, err
86         }
87         lm := big.NewInt(0)
88         lm.Sub(q1x, p1x)
89         if lm.Cmp(zero) < 0 {
90                 lm.Add(lm, pub.C.P)
91         }
92         lm.ModInverse(lm, pub.C.P)
93         z1.Sub(q1y, p1y)
94         lm.Mul(lm, z1)
95         lm.Mod(lm, pub.C.P)
96         lm.Mul(lm, lm)
97         lm.Mod(lm, pub.C.P)
98         lm.Sub(lm, p1x)
99         lm.Sub(lm, q1x)
100         lm.Mod(lm, pub.C.P)
101         if lm.Cmp(zero) < 0 {
102                 lm.Add(lm, pub.C.P)
103         }
104         lm.Mod(lm, pub.C.Q)
105         return lm.Cmp(r) == 0, nil
106 }