]> Cypherpunks.ru repositories - gogost.git/blob - gost28147/cfb_test.go
Raise copyright years
[gogost.git] / gost28147 / cfb_test.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2020 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 package gost28147
17
18 import (
19         "bytes"
20         "crypto/cipher"
21         "testing"
22         "testing/quick"
23 )
24
25 func TestCFBCryptomanager(t *testing.T) {
26         key := []byte{
27                 0x75, 0x71, 0x31, 0x34, 0xB6, 0x0F, 0xEC, 0x45,
28                 0xA6, 0x07, 0xBB, 0x83, 0xAA, 0x37, 0x46, 0xAF,
29                 0x4F, 0xF9, 0x9D, 0xA6, 0xD1, 0xB5, 0x3B, 0x5B,
30                 0x1B, 0x40, 0x2A, 0x1B, 0xAA, 0x03, 0x0D, 0x1B,
31         }
32         sbox := &SboxIdGostR341194TestParamSet
33         pt := []byte{
34                 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
35                 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0x80, 0x00, 0x00,
36         }
37         ct := []byte{
38                 0x6E, 0xE8, 0x45, 0x86, 0xDD, 0x2B, 0xCA, 0x0C,
39                 0xAD, 0x36, 0x16, 0x94, 0x0E, 0x16, 0x42, 0x42,
40         }
41         c := NewCipher(key, sbox)
42         iv := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
43         tmp := make([]byte, 16)
44         fe := c.NewCFBEncrypter(iv)
45         fe.XORKeyStream(tmp, pt)
46         if bytes.Compare(tmp, ct) != 0 {
47                 t.Fatal("encryption failed")
48         }
49         fd := c.NewCFBDecrypter(iv)
50         fd.XORKeyStream(tmp, ct)
51         if bytes.Compare(tmp, pt) != 0 {
52                 t.Fatal("decryption failed")
53         }
54 }
55
56 func TestCFBRandom(t *testing.T) {
57         f := func(key [KeySize]byte, iv [BlockSize]byte, pt []byte) bool {
58                 if len(pt) == 0 {
59                         return true
60                 }
61                 c := NewCipher(key[:], SboxDefault)
62                 ct := make([]byte, len(pt))
63                 fe := c.NewCFBEncrypter(iv[:])
64                 fe.XORKeyStream(ct, pt)
65                 fd := c.NewCFBDecrypter(iv[:])
66                 pt2 := make([]byte, len(ct))
67                 fd.XORKeyStream(pt2, ct)
68                 return bytes.Compare(pt2, pt) == 0
69         }
70         if err := quick.Check(f, nil); err != nil {
71                 t.Error(err)
72         }
73 }
74
75 func TestCFBInterface(t *testing.T) {
76         var key [32]byte
77         var iv [8]byte
78         c := NewCipher(key[:], SboxDefault)
79         var _ cipher.Stream = c.NewCFBEncrypter(iv[:])
80         var _ cipher.Stream = c.NewCFBDecrypter(iv[:])
81 }