]> Cypherpunks.ru repositories - gogost.git/blob - gost3410/public.go
Shorter unified curve argument name
[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         "crypto"
20         "fmt"
21         "math/big"
22 )
23
24 type PublicKey struct {
25         C *Curve
26         X *big.Int
27         Y *big.Int
28 }
29
30 func NewPublicKey(c *Curve, raw []byte) (*PublicKey, error) {
31         pointSize := c.PointSize()
32         key := make([]byte, 2*pointSize)
33         if len(raw) != len(key) {
34                 return nil, fmt.Errorf("gogost/gost3410: len(key) != %d", len(key))
35         }
36         for i := 0; i < len(key); i++ {
37                 key[i] = raw[len(raw)-i-1]
38         }
39         return &PublicKey{
40                 c,
41                 bytes2big(key[pointSize : 2*pointSize]),
42                 bytes2big(key[:pointSize]),
43         }, nil
44 }
45
46 func (pub *PublicKey) Raw() []byte {
47         pointSize := pub.C.PointSize()
48         raw := append(
49                 pad(pub.Y.Bytes(), pointSize),
50                 pad(pub.X.Bytes(), pointSize)...,
51         )
52         reverse(raw)
53         return raw
54 }
55
56 func (pub *PublicKey) VerifyDigest(digest, signature []byte) (bool, error) {
57         pointSize := pub.C.PointSize()
58         if len(signature) != 2*pointSize {
59                 return false, fmt.Errorf("gogost/gost3410: len(signature) != %d", 2*pointSize)
60         }
61         s := bytes2big(signature[:pointSize])
62         r := bytes2big(signature[pointSize:])
63         if r.Cmp(zero) <= 0 ||
64                 r.Cmp(pub.C.Q) >= 0 ||
65                 s.Cmp(zero) <= 0 ||
66                 s.Cmp(pub.C.Q) >= 0 {
67                 return false, nil
68         }
69         e := bytes2big(digest)
70         e.Mod(e, pub.C.Q)
71         if e.Cmp(zero) == 0 {
72                 e = big.NewInt(1)
73         }
74         v := big.NewInt(0)
75         v.ModInverse(e, pub.C.Q)
76         z1 := big.NewInt(0)
77         z2 := big.NewInt(0)
78         z1.Mul(s, v)
79         z1.Mod(z1, pub.C.Q)
80         z2.Mul(r, v)
81         z2.Mod(z2, pub.C.Q)
82         z2.Sub(pub.C.Q, z2)
83         p1x, p1y, err := pub.C.Exp(z1, pub.C.X, pub.C.Y)
84         if err != nil {
85                 return false, err
86         }
87         q1x, q1y, err := pub.C.Exp(z2, pub.X, pub.Y)
88         if err != nil {
89                 return false, err
90         }
91         lm := big.NewInt(0)
92         lm.Sub(q1x, p1x)
93         if lm.Cmp(zero) < 0 {
94                 lm.Add(lm, pub.C.P)
95         }
96         lm.ModInverse(lm, pub.C.P)
97         z1.Sub(q1y, p1y)
98         lm.Mul(lm, z1)
99         lm.Mod(lm, pub.C.P)
100         lm.Mul(lm, lm)
101         lm.Mod(lm, pub.C.P)
102         lm.Sub(lm, p1x)
103         lm.Sub(lm, q1x)
104         lm.Mod(lm, pub.C.P)
105         if lm.Cmp(zero) < 0 {
106                 lm.Add(lm, pub.C.P)
107         }
108         lm.Mod(lm, pub.C.Q)
109         return lm.Cmp(r) == 0, nil
110 }
111
112 func (our *PublicKey) Equal(theirKey crypto.PublicKey) bool {
113         their, ok := theirKey.(*PublicKey)
114         if !ok {
115                 return false
116         }
117         return our.X.Cmp(their.X) == 0 && our.Y.Cmp(their.Y) == 0 && our.C.Equal(their.C)
118 }