1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2022 Sergey Matveev <stargrave@stargrave.org>
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, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // GOST 34.12-2015 128-bit (Кузнечик (Kuznechik)) block cipher.
25 lc [BlockSize]byte = [BlockSize]byte{
26 148, 32, 133, 16, 194, 192, 1, 251, 1, 192, 194, 16,
29 pi [256]byte = [256]byte{
30 252, 238, 221, 17, 207, 110, 49, 22, 251, 196, 250,
31 218, 35, 197, 4, 77, 233, 119, 240, 219, 147, 46,
32 153, 186, 23, 54, 241, 187, 20, 205, 95, 193, 249,
33 24, 101, 90, 226, 92, 239, 33, 129, 28, 60, 66, 139,
34 1, 142, 79, 5, 132, 2, 174, 227, 106, 143, 160, 6,
35 11, 237, 152, 127, 212, 211, 31, 235, 52, 44, 81,
36 234, 200, 72, 171, 242, 42, 104, 162, 253, 58, 206,
37 204, 181, 112, 14, 86, 8, 12, 118, 18, 191, 114, 19,
38 71, 156, 183, 93, 135, 21, 161, 150, 41, 16, 123,
39 154, 199, 243, 145, 120, 111, 157, 158, 178, 177,
40 50, 117, 25, 61, 255, 53, 138, 126, 109, 84, 198,
41 128, 195, 189, 13, 87, 223, 245, 36, 169, 62, 168,
42 67, 201, 215, 121, 214, 246, 124, 34, 185, 3, 224,
43 15, 236, 222, 122, 148, 176, 188, 220, 232, 40, 80,
44 78, 51, 10, 74, 167, 151, 96, 115, 30, 0, 98, 68,
45 26, 184, 56, 130, 100, 159, 38, 65, 173, 69, 70,
46 146, 39, 94, 85, 47, 140, 163, 165, 125, 105, 213,
47 149, 59, 7, 88, 179, 64, 134, 172, 29, 247, 48, 55,
48 107, 228, 136, 217, 231, 137, 225, 27, 131, 73, 76,
49 63, 248, 254, 141, 83, 170, 144, 202, 216, 133, 97,
50 32, 113, 103, 164, 45, 43, 9, 91, 203, 155, 37, 208,
51 190, 229, 108, 82, 89, 166, 116, 210, 230, 244, 180,
52 192, 209, 102, 175, 194, 57, 75, 99, 182,
55 cBlk [32]*[BlockSize]byte
56 gfCache [256][256]byte
59 func gf(a, b byte) (c byte) {
74 func l(blk *[BlockSize]byte) {
75 for n := 0; n < BlockSize; n++ {
92 gfCache[blk[0]][lc[0]] ^
93 gfCache[blk[1]][lc[1]] ^
94 gfCache[blk[2]][lc[2]] ^
95 gfCache[blk[3]][lc[3]] ^
96 gfCache[blk[4]][lc[4]] ^
97 gfCache[blk[5]][lc[5]] ^
98 gfCache[blk[6]][lc[6]] ^
99 gfCache[blk[7]][lc[7]] ^
100 gfCache[blk[8]][lc[8]] ^
101 gfCache[blk[9]][lc[9]] ^
102 gfCache[blk[10]][lc[10]] ^
103 gfCache[blk[11]][lc[11]] ^
104 gfCache[blk[12]][lc[12]] ^
105 gfCache[blk[13]][lc[13]] ^
106 gfCache[blk[14]][lc[14]]),
125 func lInv(blk *[BlockSize]byte) {
127 for n := 0; n < BlockSize; n++ {
129 copy(blk[:], blk[1:])
130 t ^= gfCache[blk[0]][lc[0]]
131 t ^= gfCache[blk[1]][lc[1]]
132 t ^= gfCache[blk[2]][lc[2]]
133 t ^= gfCache[blk[3]][lc[3]]
134 t ^= gfCache[blk[4]][lc[4]]
135 t ^= gfCache[blk[5]][lc[5]]
136 t ^= gfCache[blk[6]][lc[6]]
137 t ^= gfCache[blk[7]][lc[7]]
138 t ^= gfCache[blk[8]][lc[8]]
139 t ^= gfCache[blk[9]][lc[9]]
140 t ^= gfCache[blk[10]][lc[10]]
141 t ^= gfCache[blk[11]][lc[11]]
142 t ^= gfCache[blk[12]][lc[12]]
143 t ^= gfCache[blk[13]][lc[13]]
144 t ^= gfCache[blk[14]][lc[14]]
149 func s(blk *[BlockSize]byte) {
150 blk[0] = pi[int(blk[0])]
151 blk[1] = pi[int(blk[1])]
152 blk[2] = pi[int(blk[2])]
153 blk[3] = pi[int(blk[3])]
154 blk[4] = pi[int(blk[4])]
155 blk[5] = pi[int(blk[5])]
156 blk[6] = pi[int(blk[6])]
157 blk[7] = pi[int(blk[7])]
158 blk[8] = pi[int(blk[8])]
159 blk[9] = pi[int(blk[9])]
160 blk[10] = pi[int(blk[10])]
161 blk[11] = pi[int(blk[11])]
162 blk[12] = pi[int(blk[12])]
163 blk[13] = pi[int(blk[13])]
164 blk[14] = pi[int(blk[14])]
165 blk[15] = pi[int(blk[15])]
168 func sInv(blk *[BlockSize]byte) {
169 for n := 0; n < BlockSize; n++ {
170 blk[n] = piInv[int(blk[n])]
175 for a := 0; a < 256; a++ {
176 for b := 0; b < 256; b++ {
177 gfCache[a][b] = gf(byte(a), byte(b))
180 for i := 0; i < 256; i++ {
181 piInv[int(pi[i])] = byte(i)
183 for i := 0; i < 32; i++ {
184 cBlk[i] = new([BlockSize]byte)
185 cBlk[i][15] = byte(i) + 1
191 ks [10][BlockSize]byte
194 func (c *Cipher) BlockSize() int {
198 func NewCipher(key []byte) *Cipher {
199 if len(key) != KeySize {
200 panic("invalid key size")
202 var ks [10][BlockSize]byte
203 var kr0 [BlockSize]byte
204 var kr1 [BlockSize]byte
205 var krt [BlockSize]byte
206 copy(kr0[:], key[:BlockSize])
207 copy(kr1[:], key[BlockSize:])
208 copy(ks[0][:], kr0[:])
209 copy(ks[1][:], kr1[:])
210 for i := 0; i < 4; i++ {
211 for j := 0; j < 8; j++ {
212 xor(krt[:], kr0[:], cBlk[8*i+j][:])
215 xor(krt[:], krt[:], kr1[:])
219 copy(ks[2+2*i][:], kr0[:])
220 copy(ks[2+2*i+1][:], kr1[:])
225 func (c *Cipher) Encrypt(dst, src []byte) {
226 blk := new([BlockSize]byte)
228 for i := 0; i < 9; i++ {
229 xor(blk[:], blk[:], c.ks[i][:])
233 xor(blk[:], blk[:], c.ks[9][:])
237 func (c *Cipher) Decrypt(dst, src []byte) {
238 blk := new([BlockSize]byte)
240 for i := 9; i > 0; i-- {
241 xor(blk[:], blk[:], c.ks[i][:])
245 xor(dst, blk[:], c.ks[0][:])