1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2024 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/>.
29 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
30 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
31 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
32 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
34 pt [BlockSize]byte = [BlockSize]byte{
35 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00,
36 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
38 ct [BlockSize]byte = [BlockSize]byte{
39 0x7f, 0x67, 0x9d, 0x90, 0xbe, 0xbc, 0x24, 0x30,
40 0x5a, 0x46, 0x8d, 0x42, 0xb9, 0xd4, 0xed, 0xcd,
44 func TestCipherInterface(t *testing.T) {
45 var _ cipher.Block = NewCipher(make([]byte, KeySize))
48 func TestRandom(t *testing.T) {
49 data := make([]byte, BlockSize)
50 f := func(key [KeySize]byte, pt [BlockSize]byte) bool {
51 io.ReadFull(rand.Reader, key[:])
52 c := NewCipher(key[:])
53 c.Encrypt(data, pt[:])
55 return bytes.Equal(data, pt[:])
57 if err := quick.Check(f, nil); err != nil {
62 func BenchmarkEncrypt(b *testing.B) {
63 key := make([]byte, KeySize)
64 io.ReadFull(rand.Reader, key)
66 blk := make([]byte, BlockSize)
68 for i := 0; i < b.N; i++ {
73 func BenchmarkDecrypt(b *testing.B) {
74 key := make([]byte, KeySize)
75 io.ReadFull(rand.Reader, key)
77 blk := make([]byte, BlockSize)
79 for i := 0; i < b.N; i++ {
84 func TestS(t *testing.T) {
85 blk := [BlockSize]byte{
86 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
87 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00,
90 if !bytes.Equal(blk[:], []byte{
91 0xb6, 0x6c, 0xd8, 0x88, 0x7d, 0x38, 0xe8, 0xd7,
92 0x77, 0x65, 0xae, 0xea, 0x0c, 0x9a, 0x7e, 0xfc,
97 if !bytes.Equal(blk[:], []byte{
98 0x55, 0x9d, 0x8d, 0xd7, 0xbd, 0x06, 0xcb, 0xfe,
99 0x7e, 0x7b, 0x26, 0x25, 0x23, 0x28, 0x0d, 0x39,
104 if !bytes.Equal(blk[:], []byte{
105 0x0c, 0x33, 0x22, 0xfe, 0xd5, 0x31, 0xe4, 0x63,
106 0x0d, 0x80, 0xef, 0x5c, 0x5a, 0x81, 0xc5, 0x0b,
111 if !bytes.Equal(blk[:], []byte{
112 0x23, 0xae, 0x65, 0x63, 0x3f, 0x84, 0x2d, 0x29,
113 0xc5, 0xdf, 0x52, 0x9c, 0x13, 0xf5, 0xac, 0xda,
121 for i := 0; i < 15; i++ {
122 t ^= gfCache[blk[i]][lc[i]]
128 func TestR(t *testing.T) {
129 blk := [BlockSize]byte{
130 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
134 if !bytes.Equal(blk[:], []byte{
135 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
136 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
141 if !bytes.Equal(blk[:], []byte{
142 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
148 if !bytes.Equal(blk[:], []byte{
149 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
155 if !bytes.Equal(blk[:], []byte{
156 0x0d, 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00,
157 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
163 func TestL(t *testing.T) {
164 blk := [BlockSize]byte{
165 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
166 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
169 if !bytes.Equal(blk[:], []byte{
170 0xd4, 0x56, 0x58, 0x4d, 0xd0, 0xe3, 0xe8, 0x4c,
171 0xc3, 0x16, 0x6e, 0x4b, 0x7f, 0xa2, 0x89, 0x0d,
176 if !bytes.Equal(blk[:], []byte{
177 0x79, 0xd2, 0x62, 0x21, 0xb8, 0x7b, 0x58, 0x4c,
178 0xd4, 0x2f, 0xbc, 0x4f, 0xfe, 0xa5, 0xde, 0x9a,
183 if !bytes.Equal(blk[:], []byte{
184 0x0e, 0x93, 0x69, 0x1a, 0x0c, 0xfc, 0x60, 0x40,
185 0x8b, 0x7b, 0x68, 0xf6, 0x6b, 0x51, 0x3c, 0x13,
190 if !bytes.Equal(blk[:], []byte{
191 0xe6, 0xa8, 0x09, 0x4f, 0xee, 0x0a, 0xa2, 0x04,
192 0xfd, 0x97, 0xbc, 0xb0, 0xb4, 0x4b, 0x85, 0x80,
198 func TestC(t *testing.T) {
199 if !bytes.Equal(cBlk[0][:], []byte{
200 0x6e, 0xa2, 0x76, 0x72, 0x6c, 0x48, 0x7a, 0xb8,
201 0x5d, 0x27, 0xbd, 0x10, 0xdd, 0x84, 0x94, 0x01,
205 if !bytes.Equal(cBlk[1][:], []byte{
206 0xdc, 0x87, 0xec, 0xe4, 0xd8, 0x90, 0xf4, 0xb3,
207 0xba, 0x4e, 0xb9, 0x20, 0x79, 0xcb, 0xeb, 0x02,
211 if !bytes.Equal(cBlk[2][:], []byte{
212 0xb2, 0x25, 0x9a, 0x96, 0xb4, 0xd8, 0x8e, 0x0b,
213 0xe7, 0x69, 0x04, 0x30, 0xa4, 0x4f, 0x7f, 0x03,
217 if !bytes.Equal(cBlk[3][:], []byte{
218 0x7b, 0xcd, 0x1b, 0x0b, 0x73, 0xe3, 0x2b, 0xa5,
219 0xb7, 0x9c, 0xb1, 0x40, 0xf2, 0x55, 0x15, 0x04,
223 if !bytes.Equal(cBlk[4][:], []byte{
224 0x15, 0x6f, 0x6d, 0x79, 0x1f, 0xab, 0x51, 0x1d,
225 0xea, 0xbb, 0x0c, 0x50, 0x2f, 0xd1, 0x81, 0x05,
229 if !bytes.Equal(cBlk[5][:], []byte{
230 0xa7, 0x4a, 0xf7, 0xef, 0xab, 0x73, 0xdf, 0x16,
231 0x0d, 0xd2, 0x08, 0x60, 0x8b, 0x9e, 0xfe, 0x06,
235 if !bytes.Equal(cBlk[6][:], []byte{
236 0xc9, 0xe8, 0x81, 0x9d, 0xc7, 0x3b, 0xa5, 0xae,
237 0x50, 0xf5, 0xb5, 0x70, 0x56, 0x1a, 0x6a, 0x07,
241 if !bytes.Equal(cBlk[7][:], []byte{
242 0xf6, 0x59, 0x36, 0x16, 0xe6, 0x05, 0x56, 0x89,
243 0xad, 0xfb, 0xa1, 0x80, 0x27, 0xaa, 0x2a, 0x08,
249 func TestRoundKeys(t *testing.T) {
251 if !bytes.Equal(c.ks[0][:], []byte{
252 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
253 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
257 if !bytes.Equal(c.ks[1][:], []byte{
258 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
259 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
263 if !bytes.Equal(c.ks[2][:], []byte{
264 0xdb, 0x31, 0x48, 0x53, 0x15, 0x69, 0x43, 0x43,
265 0x22, 0x8d, 0x6a, 0xef, 0x8c, 0xc7, 0x8c, 0x44,
269 if !bytes.Equal(c.ks[3][:], []byte{
270 0x3d, 0x45, 0x53, 0xd8, 0xe9, 0xcf, 0xec, 0x68,
271 0x15, 0xeb, 0xad, 0xc4, 0x0a, 0x9f, 0xfd, 0x04,
275 if !bytes.Equal(c.ks[4][:], []byte{
276 0x57, 0x64, 0x64, 0x68, 0xc4, 0x4a, 0x5e, 0x28,
277 0xd3, 0xe5, 0x92, 0x46, 0xf4, 0x29, 0xf1, 0xac,
281 if !bytes.Equal(c.ks[5][:], []byte{
282 0xbd, 0x07, 0x94, 0x35, 0x16, 0x5c, 0x64, 0x32,
283 0xb5, 0x32, 0xe8, 0x28, 0x34, 0xda, 0x58, 0x1b,
287 if !bytes.Equal(c.ks[6][:], []byte{
288 0x51, 0xe6, 0x40, 0x75, 0x7e, 0x87, 0x45, 0xde,
289 0x70, 0x57, 0x27, 0x26, 0x5a, 0x00, 0x98, 0xb1,
293 if !bytes.Equal(c.ks[7][:], []byte{
294 0x5a, 0x79, 0x25, 0x01, 0x7b, 0x9f, 0xdd, 0x3e,
295 0xd7, 0x2a, 0x91, 0xa2, 0x22, 0x86, 0xf9, 0x84,
299 if !bytes.Equal(c.ks[8][:], []byte{
300 0xbb, 0x44, 0xe2, 0x53, 0x78, 0xc7, 0x31, 0x23,
301 0xa5, 0xf3, 0x2f, 0x73, 0xcd, 0xb6, 0xe5, 0x17,
305 if !bytes.Equal(c.ks[9][:], []byte{
306 0x72, 0xe9, 0xdd, 0x74, 0x16, 0xbc, 0xf4, 0x5b,
307 0x75, 0x5d, 0xba, 0xa8, 0x8e, 0x4a, 0x40, 0x43,
313 func TestVectorEncrypt(t *testing.T) {
315 dst := make([]byte, BlockSize)
316 c.Encrypt(dst, pt[:])
317 if !bytes.Equal(dst, ct[:]) {
322 func TestVectorDecrypt(t *testing.T) {
324 dst := make([]byte, BlockSize)
325 c.Decrypt(dst, ct[:])
326 if !bytes.Equal(dst, pt[:]) {