]> Cypherpunks.ru repositories - gogost.git/blob - gost3412128/cipher.go
e3759530db0cd5532f1ce4ff651a45b238c9693d
[gogost.git] / gost3412128 / cipher.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2019 Sergey Matveev <stargrave@stargrave.org>
3 //
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.
7 //
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.
12 //
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/>.
15
16 // GOST 34.12-2015 128-bit (Кузнечик (Kuznechik)) block cipher.
17 package gost3412128
18
19 const (
20         BlockSize = 16
21         KeySize   = 32
22 )
23
24 var (
25         lc [BlockSize]byte = [BlockSize]byte{
26                 148, 32, 133, 16, 194, 192, 1, 251, 1, 192, 194, 16,
27                 133, 32, 148, 1,
28         }
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,
53         }
54         piInv [256]byte
55         cBlk  [32]*[BlockSize]byte
56 )
57
58 func gf(a, b byte) (c byte) {
59         for b > 0 {
60                 if b&1 > 0 {
61                         c ^= a
62                 }
63                 if a&0x80 > 0 {
64                         a = (a << 1) ^ 0xC3
65                 } else {
66                         a <<= 1
67                 }
68                 b >>= 1
69         }
70         return
71 }
72
73 func l(blk *[BlockSize]byte, rounds int) {
74         var t byte
75         var i int
76         for ; rounds > 0; rounds-- {
77                 t = blk[15]
78                 for i = 14; i >= 0; i-- {
79                         blk[i+1] = blk[i]
80                         t ^= gf(blk[i], lc[i])
81                 }
82                 blk[0] = t
83         }
84 }
85
86 func lInv(blk *[BlockSize]byte) {
87         var t byte
88         var i int
89         for n := 0; n < BlockSize; n++ {
90                 t = blk[0]
91                 for i = 0; i < 15; i++ {
92                         blk[i] = blk[i+1]
93                         t ^= gf(blk[i], lc[i])
94                 }
95                 blk[15] = t
96         }
97 }
98
99 func init() {
100         piInvP := new([256]byte)
101         for i := 0; i < 256; i++ {
102                 piInvP[int(pi[i])] = byte(i)
103         }
104         piInv = *piInvP
105         CP := new([32]*[BlockSize]byte)
106         for i := 0; i < 32; i++ {
107                 CP[i] = new([BlockSize]byte)
108                 CP[i][15] = byte(i) + 1
109                 l(CP[i], 16)
110         }
111         cBlk = *CP
112 }
113
114 func s(blk *[BlockSize]byte) {
115         for i := 0; i < BlockSize; i++ {
116                 blk[i] = pi[int(blk[i])]
117         }
118 }
119
120 func xor(dst, src1, src2 *[BlockSize]byte) {
121         for i := 0; i < BlockSize; i++ {
122                 dst[i] = src1[i] ^ src2[i]
123         }
124 }
125
126 type Cipher struct {
127         ks [10]*[BlockSize]byte
128 }
129
130 func (c *Cipher) BlockSize() int {
131         return BlockSize
132 }
133
134 func NewCipher(key []byte) *Cipher {
135         if len(key) != KeySize {
136                 panic("invalid key size")
137         }
138         ks := new([10]*[BlockSize]byte)
139         kr0 := new([BlockSize]byte)
140         kr1 := new([BlockSize]byte)
141         krt := new([BlockSize]byte)
142         copy(kr0[:], key[:BlockSize])
143         copy(kr1[:], key[BlockSize:])
144         ks[0] = new([BlockSize]byte)
145         ks[1] = new([BlockSize]byte)
146         copy(ks[0][:], kr0[:])
147         copy(ks[1][:], kr1[:])
148         for i := 0; i < 4; i++ {
149                 for j := 0; j < 8; j++ {
150                         xor(krt, kr0, cBlk[8*i+j])
151                         s(krt)
152                         l(krt, 16)
153                         xor(krt, krt, kr1)
154                         copy(kr1[:], kr0[:])
155                         copy(kr0[:], krt[:])
156                 }
157                 ks[2+2*i] = new([BlockSize]byte)
158                 copy(ks[2+2*i][:], kr0[:])
159                 ks[2+2*i+1] = new([BlockSize]byte)
160                 copy(ks[2+2*i+1][:], kr1[:])
161         }
162         return &Cipher{*ks}
163 }
164
165 func (c *Cipher) Encrypt(dst, src []byte) {
166         blk := new([BlockSize]byte)
167         copy(blk[:], src)
168         for i := 0; i < 9; i++ {
169                 xor(blk, blk, c.ks[i])
170                 s(blk)
171                 l(blk, 16)
172         }
173         xor(blk, blk, c.ks[9])
174         copy(dst[:BlockSize], blk[:])
175 }
176
177 func (c *Cipher) Decrypt(dst, src []byte) {
178         blk := new([BlockSize]byte)
179         copy(blk[:], src)
180         var n int
181         for i := 9; i > 0; i-- {
182                 xor(blk, blk, c.ks[i])
183                 lInv(blk)
184                 for n = 0; n < BlockSize; n++ {
185                         blk[n] = piInv[int(blk[n])]
186                 }
187         }
188         xor(blk, blk, c.ks[0])
189         copy(dst[:BlockSize], blk[:])
190 }