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