-
Notifications
You must be signed in to change notification settings - Fork 21
/
vko.go
151 lines (119 loc) · 4.23 KB
/
vko.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
package gost
import (
"fmt"
"errors"
"math/big"
"crypto/cipher"
cipher_gost "github.com/deatil/go-cryptobin/cipher/gost"
"github.com/deatil/go-cryptobin/hash/gost/gost341194"
"github.com/deatil/go-cryptobin/hash/gost/gost34112012256"
"github.com/deatil/go-cryptobin/hash/gost/gost34112012512"
)
func NewUKM(raw []byte) *big.Int {
t := make([]byte, len(raw))
for i := 0; i < len(t); i++ {
t[i] = raw[len(raw)-i-1]
}
return bytesToBigint(t)
}
func (prv *PrivateKey) KEK(pub *PublicKey, ukm *big.Int) ([]byte, error) {
if pub == nil {
return nil, fmt.Errorf("cryptobin/gost.KEK: PublicKey empty")
}
if !prv.Curve.Equal(pub.Curve) {
return nil, fmt.Errorf("cryptobin/gost.KEK: PublicKey not same Curve")
}
keyX, keyY, err := prv.Curve.Exp(prv.D, pub.X, pub.Y)
if err != nil {
return nil, fmt.Errorf("cryptobin/gost.KEK: %w", err)
}
u := new(big.Int).Set(ukm).Mul(ukm, prv.Curve.Co)
if u.Cmp(bigInt1) != 0 {
keyX, keyY, err = prv.Curve.Exp(u, keyX, keyY)
if err != nil {
return nil, fmt.Errorf("cryptobin/gost.KEK: %w", err)
}
}
return Marshal(prv.Curve, keyX, keyY), nil
}
func KEK(prv *PrivateKey, pub *PublicKey, ukm *big.Int) ([]byte, error) {
if prv == nil {
return nil, fmt.Errorf("cryptobin/gost.KEK: PrivateKey empty")
}
if pub == nil {
return nil, fmt.Errorf("cryptobin/gost.KEK: PublicKey empty")
}
return prv.KEK(pub, ukm)
}
// RFC 4357 VKO GOST R 34.10-2001 key agreement function.
// UKM is user keying material, also called VKO-factor.
func (prv *PrivateKey) KEK2001(pub *PublicKey, ukm *big.Int) ([]byte, error) {
if prv.Curve.PointSize() != 32 {
return nil, errors.New("cryptobin/gost.KEK2001: KEK2001 is only for 256-bit curves")
}
key, err := prv.KEK(pub, ukm)
if err != nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2001: %w", err)
}
h := gost341194.New(func(key []byte) cipher.Block {
cip, _ := cipher_gost.NewCipher(key, cipher_gost.SboxGostR341194CryptoProParamSet)
return cip
})
if _, err = h.Write(key); err != nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2001: %w", err)
}
return h.Sum(key[:0]), nil
}
func KEK2001(prv *PrivateKey, pub *PublicKey, ukm *big.Int) ([]byte, error) {
if prv == nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2001: PrivateKey empty")
}
if pub == nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2001: PublicKey empty")
}
return prv.KEK2001(pub, ukm)
}
// RFC 7836 VKO GOST R 34.10-2012 256-bit key agreement function.
// UKM is user keying material, also called VKO-factor.
func (prv *PrivateKey) KEK2012256(pub *PublicKey, ukm *big.Int) ([]byte, error) {
key, err := prv.KEK(pub, ukm)
if err != nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2012256: %w", err)
}
h := gost34112012256.New()
if _, err = h.Write(key); err != nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2012256: %w", err)
}
return h.Sum(key[:0]), nil
}
func KEK2012256(prv *PrivateKey, pub *PublicKey, ukm *big.Int) ([]byte, error) {
if prv == nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2012256: PrivateKey empty")
}
if pub == nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2012256: PublicKey empty")
}
return prv.KEK2012256(pub, ukm)
}
// RFC 7836 VKO GOST R 34.10-2012 512-bit key agreement function.
// UKM is user keying material, also called VKO-factor.
func (prv *PrivateKey) KEK2012512(pub *PublicKey, ukm *big.Int) ([]byte, error) {
key, err := prv.KEK(pub, ukm)
if err != nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2012512: %w", err)
}
h := gost34112012512.New()
if _, err = h.Write(key); err != nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2012512: %w", err)
}
return h.Sum(key[:0]), nil
}
func KEK2012512(prv *PrivateKey, pub *PublicKey, ukm *big.Int) ([]byte, error) {
if prv == nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2012512: PrivateKey empty")
}
if pub == nil {
return nil, fmt.Errorf("cryptobin/gost.KEK2012512: PublicKey empty")
}
return prv.KEK2012512(pub, ukm)
}