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