]> Cypherpunks.ru repositories - gogost.git/blob - gost3410/public.go
Raise copyright years
[gogost.git] / gost3410 / public.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2021 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, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package gost3410
17
18 import (
19         "fmt"
20         "math/big"
21 )
22
23 type PublicKey struct {
24         C *Curve
25         X *big.Int
26         Y *big.Int
27 }
28
29 func NewPublicKey(curve *Curve, raw []byte) (*PublicKey, error) {
30         pointSize := curve.PointSize()
31         key := make([]byte, 2*pointSize)
32         if len(raw) != len(key) {
33                 return nil, fmt.Errorf("gogost/gost3410: len(key) != %d", len(key))
34         }
35         for i := 0; i < len(key); i++ {
36                 key[i] = raw[len(raw)-i-1]
37         }
38         return &PublicKey{
39                 curve,
40                 bytes2big(key[pointSize : 2*pointSize]),
41                 bytes2big(key[:pointSize]),
42         }, nil
43 }
44
45 func (pub *PublicKey) Raw() []byte {
46         pointSize := pub.C.PointSize()
47         raw := append(
48                 pad(pub.Y.Bytes(), pointSize),
49                 pad(pub.X.Bytes(), pointSize)...,
50         )
51         reverse(raw)
52         return raw
53 }
54
55 func (pub *PublicKey) VerifyDigest(digest, signature []byte) (bool, error) {
56         pointSize := pub.C.PointSize()
57         if len(signature) != 2*pointSize {
58                 return false, fmt.Errorf("gogost/gost3410: len(signature) != %d", 2*pointSize)
59         }
60         s := bytes2big(signature[:pointSize])
61         r := bytes2big(signature[pointSize:])
62         if r.Cmp(zero) <= 0 ||
63                 r.Cmp(pub.C.Q) >= 0 ||
64                 s.Cmp(zero) <= 0 ||
65                 s.Cmp(pub.C.Q) >= 0 {
66                 return false, nil
67         }
68         e := bytes2big(digest)
69         e.Mod(e, pub.C.Q)
70         if e.Cmp(zero) == 0 {
71                 e = big.NewInt(1)
72         }
73         v := big.NewInt(0)
74         v.ModInverse(e, pub.C.Q)
75         z1 := big.NewInt(0)
76         z2 := big.NewInt(0)
77         z1.Mul(s, v)
78         z1.Mod(z1, pub.C.Q)
79         z2.Mul(r, v)
80         z2.Mod(z2, pub.C.Q)
81         z2.Sub(pub.C.Q, z2)
82         p1x, p1y, err := pub.C.Exp(z1, pub.C.X, pub.C.Y)
83         if err != nil {
84                 return false, err
85         }
86         q1x, q1y, err := pub.C.Exp(z2, pub.X, pub.Y)
87         if err != nil {
88                 return false, err
89         }
90         lm := big.NewInt(0)
91         lm.Sub(q1x, p1x)
92         if lm.Cmp(zero) < 0 {
93                 lm.Add(lm, pub.C.P)
94         }
95         lm.ModInverse(lm, pub.C.P)
96         z1.Sub(q1y, p1y)
97         lm.Mul(lm, z1)
98         lm.Mod(lm, pub.C.P)
99         lm.Mul(lm, lm)
100         lm.Mod(lm, pub.C.P)
101         lm.Sub(lm, p1x)
102         lm.Sub(lm, q1x)
103         lm.Mod(lm, pub.C.P)
104         if lm.Cmp(zero) < 0 {
105                 lm.Add(lm, pub.C.P)
106         }
107         lm.Mod(lm, pub.C.Q)
108         return lm.Cmp(r) == 0, nil
109 }