-
Notifications
You must be signed in to change notification settings - Fork 25
/
gost.go
193 lines (156 loc) · 5.32 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
package gost
import (
"crypto/cipher"
"github.com/deatil/go-cryptobin/tool/alias"
)
// GOST 28147-89 defines a block size of 64 bits
const BlockSize = 8
// Internal state of the GOST block cipher
type gostCipher struct {
key []uint32 // Encryption key
s [][]byte // S-box provided as parameter
k [][]byte // Expanded s-box
}
// NewCipher creates and returns a new cipher.Block. The key argument
// should be the 32 byte GOST 28147-89 key. The sbox argument should be a
// 64 byte substitution table, represented as a two-dimensional array of 8 rows
// of 16 4-bit integers.
func NewCipher(key []byte, sbox [][]byte) (cipher.Block, error) {
if len(key) != 32 {
return nil, KeySizeError(len(key))
}
if len(sbox) != 8 {
return nil, SboxSizeError(len(sbox))
}
for i := 0; i < len(sbox); i++ {
if len(sbox[i]) != 16 {
return nil, SboxSizeError(len(sbox[i]))
}
}
newKey := bytesToUint32s(key)
kbox := sboxExpansion(sbox)
c := &gostCipher{
key: newKey,
s: sbox,
k: kbox,
}
return c, nil
}
func (this *gostCipher) BlockSize() int {
return BlockSize
}
func (this *gostCipher) Encrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/gost: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/gost: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/gost: invalid buffer overlap")
}
encSrc := bytesToUint32s(src)
encDst := make([]uint32, len(encSrc))
this.encrypt32(encDst, encSrc)
resBytes := uint32sToBytes(encDst)
copy(dst, resBytes)
}
func (this *gostCipher) Decrypt(dst, src []byte) {
if len(src) < BlockSize {
panic("cryptobin/gost: input not full block")
}
if len(dst) < BlockSize {
panic("cryptobin/gost: output not full block")
}
if alias.InexactOverlap(dst[:BlockSize], src[:BlockSize]) {
panic("cryptobin/gost: invalid buffer overlap")
}
encSrc := bytesToUint32s(src)
encDst := make([]uint32, len(encSrc))
this.decrypt32(encDst, encSrc)
resBytes := uint32sToBytes(encDst)
copy(dst, resBytes)
}
// GOST block cipher round function
func (this *gostCipher) f(x uint32) uint32 {
x = uint32(this.k[0][(x >> 24) & 255]) << 24 |
uint32(this.k[1][(x >> 16) & 255]) << 16 |
uint32(this.k[2][(x >> 8) & 255]) << 8 |
uint32(this.k[3][x & 255])
// rotate result left by 11 bits
return (x << 11) | (x >> (32 - 11))
}
// Encrypt one block from src into dst.
func (this *gostCipher) encrypt32(dst, src []uint32) {
n1, n2 := src[0], src[1]
n2 = n2 ^ this.f(n1 + this.key[0])
n1 = n1 ^ this.f(n2 + this.key[1])
n2 = n2 ^ this.f(n1 + this.key[2])
n1 = n1 ^ this.f(n2 + this.key[3])
n2 = n2 ^ this.f(n1 + this.key[4])
n1 = n1 ^ this.f(n2 + this.key[5])
n2 = n2 ^ this.f(n1 + this.key[6])
n1 = n1 ^ this.f(n2 + this.key[7])
n2 = n2 ^ this.f(n1 + this.key[0])
n1 = n1 ^ this.f(n2 + this.key[1])
n2 = n2 ^ this.f(n1 + this.key[2])
n1 = n1 ^ this.f(n2 + this.key[3])
n2 = n2 ^ this.f(n1 + this.key[4])
n1 = n1 ^ this.f(n2 + this.key[5])
n2 = n2 ^ this.f(n1 + this.key[6])
n1 = n1 ^ this.f(n2 + this.key[7])
n2 = n2 ^ this.f(n1 + this.key[0])
n1 = n1 ^ this.f(n2 + this.key[1])
n2 = n2 ^ this.f(n1 + this.key[2])
n1 = n1 ^ this.f(n2 + this.key[3])
n2 = n2 ^ this.f(n1 + this.key[4])
n1 = n1 ^ this.f(n2 + this.key[5])
n2 = n2 ^ this.f(n1 + this.key[6])
n1 = n1 ^ this.f(n2 + this.key[7])
n2 = n2 ^ this.f(n1 + this.key[7])
n1 = n1 ^ this.f(n2 + this.key[6])
n2 = n2 ^ this.f(n1 + this.key[5])
n1 = n1 ^ this.f(n2 + this.key[4])
n2 = n2 ^ this.f(n1 + this.key[3])
n1 = n1 ^ this.f(n2 + this.key[2])
n2 = n2 ^ this.f(n1 + this.key[1])
n1 = n1 ^ this.f(n2 + this.key[0])
dst[0], dst[1] = n2, n1
}
// Decrypt one block from src into dst.
func (this *gostCipher) decrypt32(dst, src []uint32) {
n1, n2 := src[0], src[1]
n2 = n2 ^ this.f(n1 + this.key[0])
n1 = n1 ^ this.f(n2 + this.key[1])
n2 = n2 ^ this.f(n1 + this.key[2])
n1 = n1 ^ this.f(n2 + this.key[3])
n2 = n2 ^ this.f(n1 + this.key[4])
n1 = n1 ^ this.f(n2 + this.key[5])
n2 = n2 ^ this.f(n1 + this.key[6])
n1 = n1 ^ this.f(n2 + this.key[7])
n2 = n2 ^ this.f(n1 + this.key[7])
n1 = n1 ^ this.f(n2 + this.key[6])
n2 = n2 ^ this.f(n1 + this.key[5])
n1 = n1 ^ this.f(n2 + this.key[4])
n2 = n2 ^ this.f(n1 + this.key[3])
n1 = n1 ^ this.f(n2 + this.key[2])
n2 = n2 ^ this.f(n1 + this.key[1])
n1 = n1 ^ this.f(n2 + this.key[0])
n2 = n2 ^ this.f(n1 + this.key[7])
n1 = n1 ^ this.f(n2 + this.key[6])
n2 = n2 ^ this.f(n1 + this.key[5])
n1 = n1 ^ this.f(n2 + this.key[4])
n2 = n2 ^ this.f(n1 + this.key[3])
n1 = n1 ^ this.f(n2 + this.key[2])
n2 = n2 ^ this.f(n1 + this.key[1])
n1 = n1 ^ this.f(n2 + this.key[0])
n2 = n2 ^ this.f(n1 + this.key[7])
n1 = n1 ^ this.f(n2 + this.key[6])
n2 = n2 ^ this.f(n1 + this.key[5])
n1 = n1 ^ this.f(n2 + this.key[4])
n2 = n2 ^ this.f(n1 + this.key[3])
n1 = n1 ^ this.f(n2 + this.key[2])
n2 = n2 ^ this.f(n1 + this.key[1])
n1 = n1 ^ this.f(n2 + this.key[0])
dst[0], dst[1] = n2, n1
}