-
Notifications
You must be signed in to change notification settings - Fork 1
/
block.go
162 lines (132 loc) · 3.71 KB
/
block.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
package gost
import "crypto/cipher"
// GOST 28147-89 defines a block size of 64 bits
const BlockSize = 8
// Internal state of the GOST block cipher
type _GOST struct {
key []uint32 // Encryption key
s [][]byte // S-box provided as parameter
k [][]byte // Expanded s-box
}
func (g *_GOST) BlockSize() int {
return BlockSize
}
func (g *_GOST) Encrypt(dst, src []byte) {
encSrc := bytesToUint32s(src)
encDst := make([]uint32, len(encSrc))
g.encrypt32(encDst, encSrc)
resBytes := uint32sToBytes(encDst)
copy(dst, resBytes)
}
func (g *_GOST) Decrypt(dst, src []byte) {
encSrc := bytesToUint32s(src)
encDst := make([]uint32, len(encSrc))
g.decrypt32(encDst, encSrc)
resBytes := uint32sToBytes(encDst)
copy(dst, resBytes)
}
// GOST block cipher round function
func (g *_GOST) f(x uint32) uint32 {
x = uint32(g.k[0][(x>>24)&255])<<24 | uint32(g.k[1][(x>>16)&255])<<16 |
uint32(g.k[2][(x>>8)&255])<<8 | uint32(g.k[3][x&255])
// rotate result left by 11 bits
return (x << 11) | (x >> (32 - 11))
}
// Encrypt one block from src into dst.
func (g *_GOST) encrypt32(dst, src []uint32) {
n1, n2 := src[0], src[1]
n2 = n2 ^ g.f(n1+g.key[0])
n1 = n1 ^ g.f(n2+g.key[1])
n2 = n2 ^ g.f(n1+g.key[2])
n1 = n1 ^ g.f(n2+g.key[3])
n2 = n2 ^ g.f(n1+g.key[4])
n1 = n1 ^ g.f(n2+g.key[5])
n2 = n2 ^ g.f(n1+g.key[6])
n1 = n1 ^ g.f(n2+g.key[7])
n2 = n2 ^ g.f(n1+g.key[0])
n1 = n1 ^ g.f(n2+g.key[1])
n2 = n2 ^ g.f(n1+g.key[2])
n1 = n1 ^ g.f(n2+g.key[3])
n2 = n2 ^ g.f(n1+g.key[4])
n1 = n1 ^ g.f(n2+g.key[5])
n2 = n2 ^ g.f(n1+g.key[6])
n1 = n1 ^ g.f(n2+g.key[7])
n2 = n2 ^ g.f(n1+g.key[0])
n1 = n1 ^ g.f(n2+g.key[1])
n2 = n2 ^ g.f(n1+g.key[2])
n1 = n1 ^ g.f(n2+g.key[3])
n2 = n2 ^ g.f(n1+g.key[4])
n1 = n1 ^ g.f(n2+g.key[5])
n2 = n2 ^ g.f(n1+g.key[6])
n1 = n1 ^ g.f(n2+g.key[7])
n2 = n2 ^ g.f(n1+g.key[7])
n1 = n1 ^ g.f(n2+g.key[6])
n2 = n2 ^ g.f(n1+g.key[5])
n1 = n1 ^ g.f(n2+g.key[4])
n2 = n2 ^ g.f(n1+g.key[3])
n1 = n1 ^ g.f(n2+g.key[2])
n2 = n2 ^ g.f(n1+g.key[1])
n1 = n1 ^ g.f(n2+g.key[0])
dst[0], dst[1] = n2, n1
}
// Decrypt one block from src into dst.
func (g *_GOST) decrypt32(dst, src []uint32) {
n1, n2 := src[0], src[1]
n2 = n2 ^ g.f(n1+g.key[0])
n1 = n1 ^ g.f(n2+g.key[1])
n2 = n2 ^ g.f(n1+g.key[2])
n1 = n1 ^ g.f(n2+g.key[3])
n2 = n2 ^ g.f(n1+g.key[4])
n1 = n1 ^ g.f(n2+g.key[5])
n2 = n2 ^ g.f(n1+g.key[6])
n1 = n1 ^ g.f(n2+g.key[7])
n2 = n2 ^ g.f(n1+g.key[7])
n1 = n1 ^ g.f(n2+g.key[6])
n2 = n2 ^ g.f(n1+g.key[5])
n1 = n1 ^ g.f(n2+g.key[4])
n2 = n2 ^ g.f(n1+g.key[3])
n1 = n1 ^ g.f(n2+g.key[2])
n2 = n2 ^ g.f(n1+g.key[1])
n1 = n1 ^ g.f(n2+g.key[0])
n2 = n2 ^ g.f(n1+g.key[7])
n1 = n1 ^ g.f(n2+g.key[6])
n2 = n2 ^ g.f(n1+g.key[5])
n1 = n1 ^ g.f(n2+g.key[4])
n2 = n2 ^ g.f(n1+g.key[3])
n1 = n1 ^ g.f(n2+g.key[2])
n2 = n2 ^ g.f(n1+g.key[1])
n1 = n1 ^ g.f(n2+g.key[0])
n2 = n2 ^ g.f(n1+g.key[7])
n1 = n1 ^ g.f(n2+g.key[6])
n2 = n2 ^ g.f(n1+g.key[5])
n1 = n1 ^ g.f(n2+g.key[4])
n2 = n2 ^ g.f(n1+g.key[3])
n1 = n1 ^ g.f(n2+g.key[2])
n2 = n2 ^ g.f(n1+g.key[1])
n1 = n1 ^ g.f(n2+g.key[0])
dst[0], dst[1] = n2, n1
}
// NewBlockCipher 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 NewBlockCipher(key []byte, sbox [][]byte) (cipher.Block, error) {
if len(key) != (256 / 8) {
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]))
}
}
kbox := sboxExpansion(sbox)
g := &_GOST{
key: bytesToUint32s(key),
s: sbox,
k: kbox,
}
return g, nil
}