]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost28147/cfb_test.go
af527fbf74b99e57c73eefb777c10d94322ced0f
[gogost.git] / src / cypherpunks.ru / gogost / gost28147 / cfb_test.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, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 package gost28147
18
19 import (
20         "bytes"
21         "crypto/cipher"
22         "crypto/rand"
23         "testing"
24         "testing/quick"
25 )
26
27 func TestCFBCryptomanager(t *testing.T) {
28         key := [KeySize]byte{
29                 0x75, 0x71, 0x31, 0x34, 0xB6, 0x0F, 0xEC, 0x45,
30                 0xA6, 0x07, 0xBB, 0x83, 0xAA, 0x37, 0x46, 0xAF,
31                 0x4F, 0xF9, 0x9D, 0xA6, 0xD1, 0xB5, 0x3B, 0x5B,
32                 0x1B, 0x40, 0x2A, 0x1B, 0xAA, 0x03, 0x0D, 0x1B,
33         }
34         sbox := &SboxIdGostR341194TestParamSet
35         pt := []byte{
36                 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
37                 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0x80, 0x00, 0x00,
38         }
39         ct := []byte{
40                 0x6E, 0xE8, 0x45, 0x86, 0xDD, 0x2B, 0xCA, 0x0C,
41                 0xAD, 0x36, 0x16, 0x94, 0x0E, 0x16, 0x42, 0x42,
42         }
43         c := NewCipher(key, sbox)
44         iv := [8]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
45         tmp := make([]byte, 16)
46         fe := c.NewCFBEncrypter(iv)
47         fe.XORKeyStream(tmp, pt)
48         if bytes.Compare(tmp, ct) != 0 {
49                 t.Fail()
50         }
51         fd := c.NewCFBDecrypter(iv)
52         fd.XORKeyStream(tmp, ct)
53         if bytes.Compare(tmp, pt) != 0 {
54                 t.Fail()
55         }
56 }
57
58 func TestCFBRandom(t *testing.T) {
59         var key [KeySize]byte
60         rand.Read(key[:])
61         c := NewCipher(key, SboxDefault)
62         f := func(ivRaw []byte, pt []byte) bool {
63                 if len(pt) == 0 || len(ivRaw) < 8 {
64                         return true
65                 }
66                 var iv [8]byte
67                 copy(iv[:], ivRaw[:8])
68                 ct := make([]byte, len(pt))
69                 fe := c.NewCFBEncrypter(iv)
70                 fe.XORKeyStream(ct, pt)
71                 fd := c.NewCFBDecrypter(iv)
72                 pt2 := make([]byte, len(ct))
73                 fd.XORKeyStream(pt2, ct)
74                 return bytes.Compare(pt2, pt) == 0
75         }
76         if err := quick.Check(f, nil); err != nil {
77                 t.Error(err)
78         }
79 }
80
81 func TestCFBInterface(t *testing.T) {
82         var key [32]byte
83         var iv [8]byte
84         c := NewCipher(key, SboxDefault)
85         var _ cipher.Stream = c.NewCFBEncrypter(iv)
86         var _ cipher.Stream = c.NewCFBDecrypter(iv)
87 }