-
Notifications
You must be signed in to change notification settings - Fork 21
/
gost.go
357 lines (283 loc) · 8.33 KB
/
gost.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package gost
import (
"io"
"fmt"
"errors"
"crypto"
"math/big"
"encoding/asn1"
)
// GOST R 34.10-2001 (RFC 5832),
// GOST R 34.10-2012 (RFC 7091) signature algorithms and
// VKO GOST R 34.10-2001 (RFC 4357),
// VKO GOST R 34.10-2012 (RFC 7836) key agreement algorithms.
// r and s data
type gostSignature struct {
S, R *big.Int
}
// PublicKey represents an GOST public key.
type PublicKey struct {
Curve *Curve
X, Y *big.Int
}
// Equal reports whether pub and x have the same value.
func (pub *PublicKey) Equal(x crypto.PublicKey) bool {
xx, ok := x.(*PublicKey)
if !ok {
return false
}
return pub.X.Cmp(xx.X) == 0 &&
pub.Y.Cmp(xx.Y) == 0 &&
pub.Curve.Equal(xx.Curve)
}
// Verify verifies the signature in hash using the public key, pub. It
// reports whether the signature is valid.
func (pub *PublicKey) Verify(digest, signature []byte) (bool, error) {
pointSize := pub.Curve.PointSize()
if len(signature) != 2*pointSize {
return false, fmt.Errorf("cryptobin/gost: len(signature)=%d != %d", len(signature), 2*pointSize)
}
s := bytesToBigint(signature[:pointSize])
r := bytesToBigint(signature[pointSize:])
verify, err := VerifyWithRS(pub, digest, r, s)
if err != nil {
return false, errors.New("cryptobin/gost: " + err.Error())
}
return verify, nil
}
// Verify asn.1 signed data
func (pub *PublicKey) VerifyASN1(digest, signature []byte) (bool, error) {
var sign gostSignature
_, err := asn1.Unmarshal(signature, &sign)
if err != nil {
return false, err
}
verify, err := VerifyWithRS(pub, digest, sign.R, sign.S)
if err != nil {
return false, errors.New("cryptobin/gost: " + err.Error())
}
return verify, nil
}
// PrivateKey represents an GOST private key.
type PrivateKey struct {
PublicKey
D *big.Int
}
// Equal reports whether priv and x have the same value.
func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool {
xx, ok := x.(*PrivateKey)
if !ok {
return false
}
return priv.D.Cmp(xx.D) == 0 &&
priv.PublicKey.Equal(&xx.PublicKey)
}
// Public returns the public key corresponding to priv.
func (priv *PrivateKey) Public() crypto.PublicKey {
return &priv.PublicKey
}
// Sign signs digest with priv, reading randomness from rand. The opts argument
// is not currently used but, in keeping with the crypto.Signer interface,
// should be the hash function used to digest the message.
// sig is s + r bytes
func (priv *PrivateKey) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
r, s, err := SignToRS(rand, priv, digest)
if err != nil {
return nil, err
}
pointSize := priv.Curve.PointSize()
signed := make([]byte, 2*pointSize)
s.FillBytes(signed[ 0: pointSize])
r.FillBytes(signed[pointSize:2*pointSize])
return signed, nil
}
// Sign data to asn.1
func (priv *PrivateKey) SignASN1(rand io.Reader, digest []byte, opts crypto.SignerOpts) ([]byte, error) {
r, s, err := SignToRS(rand, priv, digest)
if err != nil {
return nil, err
}
signed, err := asn1.Marshal(gostSignature{
R: r,
S: s,
})
if err != nil {
return nil, err
}
return signed, nil
}
func newPrivateKey(curve *Curve, raw []byte) (*PrivateKey, error) {
k := bytesToBigint(raw)
if k.Cmp(zero) == 0 {
return nil, errors.New("cryptobin/gost: zero private key")
}
k = k.Mod(k, curve.Q)
x, y, err := curve.Exp(k, curve.X, curve.Y)
if err != nil {
return nil, err
}
priv := new(PrivateKey)
priv.D = k
priv.PublicKey.Curve = curve
priv.PublicKey.X = x
priv.PublicKey.Y = y
return priv, nil
}
// GenerateKey generates a random GOST private key.
func GenerateKey(rand io.Reader, curve *Curve) (*PrivateKey, error) {
private := make([]byte, curve.PointSize())
if _, err := io.ReadFull(rand, private); err != nil {
return nil, fmt.Errorf("cryptobin/gost: %w", err)
}
return newPrivateKey(curve, private)
}
// Unmarshal private key
func NewPrivateKey(curve *Curve, raw []byte) (*PrivateKey, error) {
return newPrivateKey(curve, Reverse(raw))
}
// Marshal private key
func ToPrivateKey(priv *PrivateKey) []byte {
privateKey := make([]byte, priv.Curve.PointSize())
priv.D.FillBytes(privateKey)
return Reverse(privateKey)
}
// Unmarshal public key
func NewPublicKey(curve *Curve, data []byte) (*PublicKey, error) {
x, y := Unmarshal(curve, data)
if x == nil || y == nil {
return nil, errors.New("cryptobin/gost: publicKey is incorrect.")
}
pub := &PublicKey{
Curve: curve,
X: x,
Y: y,
}
return pub, nil
}
// Marshal public key
func ToPublicKey(pub *PublicKey) []byte {
return Marshal(pub.Curve, pub.X, pub.Y)
}
// Sign hash
func Sign(rand io.Reader, priv *PrivateKey, hash []byte) ([]byte, error) {
if priv == nil {
return nil, errors.New("cryptobin/gost: Private Key is error")
}
return priv.Sign(rand, hash, nil)
}
// Verify hash
func Verify(pub *PublicKey, hash, sig []byte) (bool, error) {
if pub == nil {
return false, errors.New("cryptobin/gost: Public Key is error")
}
return pub.Verify(hash, sig)
}
// SignASN1 signs a hash (which should be the result of hashing a larger message)
// using the private key, priv. If the hash is longer than the bit-length of the
// private key's curve order, the hash will be truncated to that length. It
// returns the ASN.1 encoded signature.
func SignASN1(rand io.Reader, priv *PrivateKey, hash []byte) ([]byte, error) {
if priv == nil {
return nil, errors.New("cryptobin/gost: Private Key is error")
}
return priv.SignASN1(rand, hash, nil)
}
// VerifyASN1 verifies the ASN.1 encoded signature, sig, of hash using the
// public key, pub. Its return value records whether the signature is valid.
func VerifyASN1(pub *PublicKey, hash, sig []byte) (bool, error) {
if pub == nil {
return false, errors.New("cryptobin/gost: Public Key is error")
}
return pub.VerifyASN1(hash, sig)
}
// SignToRS
func SignToRS(rand io.Reader, priv *PrivateKey, digest []byte) (*big.Int, *big.Int, error) {
digest = Reverse(digest)
e := bytesToBigint(digest)
e.Mod(e, priv.Curve.Q)
if e.Cmp(zero) == 0 {
e = big.NewInt(1)
}
kRaw := make([]byte, priv.Curve.PointSize())
var err error
var k *big.Int
var r *big.Int
d := big.NewInt(0)
s := big.NewInt(0)
Retry:
if _, err = io.ReadFull(rand, kRaw); err != nil {
return nil, nil, fmt.Errorf("cryptobin/gost: %w", err)
}
k = bytesToBigint(kRaw)
k.Mod(k, priv.Curve.Q)
if k.Cmp(zero) == 0 {
goto Retry
}
r, _, err = priv.Curve.Exp(k, priv.Curve.X, priv.Curve.Y)
if err != nil {
return nil, nil, err
}
r.Mod(r, priv.Curve.Q)
if r.Cmp(zero) == 0 {
goto Retry
}
d.Mul(priv.D, r)
k.Mul(k, e)
s.Add(d, k)
s.Mod(s, priv.Curve.Q)
if s.Cmp(zero) == 0 {
goto Retry
}
return r, s, nil
}
// VerifyWithRS
func VerifyWithRS(pub *PublicKey, digest []byte, r, s *big.Int) (bool, error) {
if r.Cmp(zero) <= 0 ||
r.Cmp(pub.Curve.Q) >= 0 ||
s.Cmp(zero) <= 0 ||
s.Cmp(pub.Curve.Q) >= 0 {
return false, nil
}
digest = Reverse(digest)
e := bytesToBigint(digest)
e.Mod(e, pub.Curve.Q)
if e.Cmp(zero) == 0 {
e = big.NewInt(1)
}
v := big.NewInt(0)
v.ModInverse(e, pub.Curve.Q)
z1 := big.NewInt(0)
z2 := big.NewInt(0)
z1.Mul(s, v)
z1.Mod(z1, pub.Curve.Q)
z2.Mul(r, v)
z2.Mod(z2, pub.Curve.Q)
z2.Sub(pub.Curve.Q, z2)
p1x, p1y, err := pub.Curve.Exp(z1, pub.Curve.X, pub.Curve.Y)
if err != nil {
return false, err
}
q1x, q1y, err := pub.Curve.Exp(z2, pub.X, pub.Y)
if err != nil {
return false, err
}
lm := big.NewInt(0)
lm.Sub(q1x, p1x)
if lm.Cmp(zero) < 0 {
lm.Add(lm, pub.Curve.P)
}
lm.ModInverse(lm, pub.Curve.P)
z1.Sub(q1y, p1y)
lm.Mul(lm, z1)
lm.Mod(lm, pub.Curve.P)
lm.Mul(lm, lm)
lm.Mod(lm, pub.Curve.P)
lm.Sub(lm, p1x)
lm.Sub(lm, q1x)
lm.Mod(lm, pub.Curve.P)
if lm.Cmp(zero) < 0 {
lm.Add(lm, pub.Curve.P)
}
lm.Mod(lm, pub.Curve.Q)
return lm.Cmp(r) == 0, nil
}