]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost3410/public.go
6af21af83b9156384a0bc25f88bbf8eaae185d4c
[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         if len(raw) != 2*int(mode) {
33                 return nil, errors.New("Invalid public key length")
34         }
35         key := make([]byte, 2*int(mode))
36         copy(key, raw)
37         reverse(key)
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, errors.New("Invalid signature length")
58         }
59         s := bytes2big(signature[:pub.mode])
60         r := bytes2big(signature[pub.mode:])
61         if r.Cmp(zero) <= 0 || r.Cmp(pub.c.Q) >= 0 || s.Cmp(zero) <= 0 || s.Cmp(pub.c.Q) >= 0 {
62                 return false, nil
63         }
64         e := bytes2big(digest)
65         e.Mod(e, pub.c.Q)
66         if e.Cmp(zero) == 0 {
67                 e = big.NewInt(1)
68         }
69         v := big.NewInt(0)
70         v.ModInverse(e, pub.c.Q)
71         z1 := big.NewInt(0)
72         z2 := big.NewInt(0)
73         z1.Mul(s, v)
74         z1.Mod(z1, pub.c.Q)
75         z2.Mul(r, v)
76         z2.Mod(z2, pub.c.Q)
77         z2.Sub(pub.c.Q, z2)
78         p1x, p1y, err := pub.c.Exp(z1, pub.c.Bx, pub.c.By)
79         if err != nil {
80                 return false, err
81         }
82         q1x, q1y, err := pub.c.Exp(z2, pub.x, pub.y)
83         if err != nil {
84                 return false, err
85         }
86         lm := big.NewInt(0)
87         lm.Sub(q1x, p1x)
88         if lm.Cmp(zero) < 0 {
89                 lm.Add(lm, pub.c.P)
90         }
91         lm.ModInverse(lm, pub.c.P)
92         z1.Sub(q1y, p1y)
93         lm.Mul(lm, z1)
94         lm.Mod(lm, pub.c.P)
95         lm.Mul(lm, lm)
96         lm.Mod(lm, pub.c.P)
97         lm.Sub(lm, p1x)
98         lm.Sub(lm, q1x)
99         lm.Mod(lm, pub.c.P)
100         if lm.Cmp(zero) < 0 {
101                 lm.Add(lm, pub.c.P)
102         }
103         lm.Mod(lm, pub.c.Q)
104         return lm.Cmp(r) == 0, nil
105 }