1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2021 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.Compare(data, pt[:]) == 0
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.Compare(blk[:], []byte{
91 0xb6, 0x6c, 0xd8, 0x88, 0x7d, 0x38, 0xe8, 0xd7,
92 0x77, 0x65, 0xae, 0xea, 0x0c, 0x9a, 0x7e, 0xfc,
97 if bytes.Compare(blk[:], []byte{
98 0x55, 0x9d, 0x8d, 0xd7, 0xbd, 0x06, 0xcb, 0xfe,
99 0x7e, 0x7b, 0x26, 0x25, 0x23, 0x28, 0x0d, 0x39,
104 if bytes.Compare(blk[:], []byte{
105 0x0c, 0x33, 0x22, 0xfe, 0xd5, 0x31, 0xe4, 0x63,
106 0x0d, 0x80, 0xef, 0x5c, 0x5a, 0x81, 0xc5, 0x0b,
111 if bytes.Compare(blk[:], []byte{
112 0x23, 0xae, 0x65, 0x63, 0x3f, 0x84, 0x2d, 0x29,
113 0xc5, 0xdf, 0x52, 0x9c, 0x13, 0xf5, 0xac, 0xda,
119 func R(blk *[BlockSize]byte) {
123 func TestR(t *testing.T) {
124 blk := [BlockSize]byte{
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
129 if bytes.Compare(blk[:], []byte{
130 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
136 if bytes.Compare(blk[:], []byte{
137 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
143 if bytes.Compare(blk[:], []byte{
144 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
145 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
150 if bytes.Compare(blk[:], []byte{
151 0x0d, 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00,
152 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
158 func TestL(t *testing.T) {
159 blk := [BlockSize]byte{
160 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
164 if bytes.Compare(blk[:], []byte{
165 0xd4, 0x56, 0x58, 0x4d, 0xd0, 0xe3, 0xe8, 0x4c,
166 0xc3, 0x16, 0x6e, 0x4b, 0x7f, 0xa2, 0x89, 0x0d,
171 if bytes.Compare(blk[:], []byte{
172 0x79, 0xd2, 0x62, 0x21, 0xb8, 0x7b, 0x58, 0x4c,
173 0xd4, 0x2f, 0xbc, 0x4f, 0xfe, 0xa5, 0xde, 0x9a,
178 if bytes.Compare(blk[:], []byte{
179 0x0e, 0x93, 0x69, 0x1a, 0x0c, 0xfc, 0x60, 0x40,
180 0x8b, 0x7b, 0x68, 0xf6, 0x6b, 0x51, 0x3c, 0x13,
185 if bytes.Compare(blk[:], []byte{
186 0xe6, 0xa8, 0x09, 0x4f, 0xee, 0x0a, 0xa2, 0x04,
187 0xfd, 0x97, 0xbc, 0xb0, 0xb4, 0x4b, 0x85, 0x80,
193 func TestC(t *testing.T) {
194 if bytes.Compare(cBlk[0][:], []byte{
195 0x6e, 0xa2, 0x76, 0x72, 0x6c, 0x48, 0x7a, 0xb8,
196 0x5d, 0x27, 0xbd, 0x10, 0xdd, 0x84, 0x94, 0x01,
200 if bytes.Compare(cBlk[1][:], []byte{
201 0xdc, 0x87, 0xec, 0xe4, 0xd8, 0x90, 0xf4, 0xb3,
202 0xba, 0x4e, 0xb9, 0x20, 0x79, 0xcb, 0xeb, 0x02,
206 if bytes.Compare(cBlk[2][:], []byte{
207 0xb2, 0x25, 0x9a, 0x96, 0xb4, 0xd8, 0x8e, 0x0b,
208 0xe7, 0x69, 0x04, 0x30, 0xa4, 0x4f, 0x7f, 0x03,
212 if bytes.Compare(cBlk[3][:], []byte{
213 0x7b, 0xcd, 0x1b, 0x0b, 0x73, 0xe3, 0x2b, 0xa5,
214 0xb7, 0x9c, 0xb1, 0x40, 0xf2, 0x55, 0x15, 0x04,
218 if bytes.Compare(cBlk[4][:], []byte{
219 0x15, 0x6f, 0x6d, 0x79, 0x1f, 0xab, 0x51, 0x1d,
220 0xea, 0xbb, 0x0c, 0x50, 0x2f, 0xd1, 0x81, 0x05,
224 if bytes.Compare(cBlk[5][:], []byte{
225 0xa7, 0x4a, 0xf7, 0xef, 0xab, 0x73, 0xdf, 0x16,
226 0x0d, 0xd2, 0x08, 0x60, 0x8b, 0x9e, 0xfe, 0x06,
230 if bytes.Compare(cBlk[6][:], []byte{
231 0xc9, 0xe8, 0x81, 0x9d, 0xc7, 0x3b, 0xa5, 0xae,
232 0x50, 0xf5, 0xb5, 0x70, 0x56, 0x1a, 0x6a, 0x07,
236 if bytes.Compare(cBlk[7][:], []byte{
237 0xf6, 0x59, 0x36, 0x16, 0xe6, 0x05, 0x56, 0x89,
238 0xad, 0xfb, 0xa1, 0x80, 0x27, 0xaa, 0x2a, 0x08,
244 func TestRoundKeys(t *testing.T) {
246 if bytes.Compare(c.ks[0][:], []byte{
247 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
248 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
252 if bytes.Compare(c.ks[1][:], []byte{
253 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
254 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
258 if bytes.Compare(c.ks[2][:], []byte{
259 0xdb, 0x31, 0x48, 0x53, 0x15, 0x69, 0x43, 0x43,
260 0x22, 0x8d, 0x6a, 0xef, 0x8c, 0xc7, 0x8c, 0x44,
264 if bytes.Compare(c.ks[3][:], []byte{
265 0x3d, 0x45, 0x53, 0xd8, 0xe9, 0xcf, 0xec, 0x68,
266 0x15, 0xeb, 0xad, 0xc4, 0x0a, 0x9f, 0xfd, 0x04,
270 if bytes.Compare(c.ks[4][:], []byte{
271 0x57, 0x64, 0x64, 0x68, 0xc4, 0x4a, 0x5e, 0x28,
272 0xd3, 0xe5, 0x92, 0x46, 0xf4, 0x29, 0xf1, 0xac,
276 if bytes.Compare(c.ks[5][:], []byte{
277 0xbd, 0x07, 0x94, 0x35, 0x16, 0x5c, 0x64, 0x32,
278 0xb5, 0x32, 0xe8, 0x28, 0x34, 0xda, 0x58, 0x1b,
282 if bytes.Compare(c.ks[6][:], []byte{
283 0x51, 0xe6, 0x40, 0x75, 0x7e, 0x87, 0x45, 0xde,
284 0x70, 0x57, 0x27, 0x26, 0x5a, 0x00, 0x98, 0xb1,
288 if bytes.Compare(c.ks[7][:], []byte{
289 0x5a, 0x79, 0x25, 0x01, 0x7b, 0x9f, 0xdd, 0x3e,
290 0xd7, 0x2a, 0x91, 0xa2, 0x22, 0x86, 0xf9, 0x84,
294 if bytes.Compare(c.ks[8][:], []byte{
295 0xbb, 0x44, 0xe2, 0x53, 0x78, 0xc7, 0x31, 0x23,
296 0xa5, 0xf3, 0x2f, 0x73, 0xcd, 0xb6, 0xe5, 0x17,
300 if bytes.Compare(c.ks[9][:], []byte{
301 0x72, 0xe9, 0xdd, 0x74, 0x16, 0xbc, 0xf4, 0x5b,
302 0x75, 0x5d, 0xba, 0xa8, 0x8e, 0x4a, 0x40, 0x43,
308 func TestVectorEncrypt(t *testing.T) {
310 dst := make([]byte, BlockSize)
311 c.Encrypt(dst, pt[:])
312 if bytes.Compare(dst, ct[:]) != 0 {
317 func TestVectorDecrypt(t *testing.T) {
319 dst := make([]byte, BlockSize)
320 c.Decrypt(dst, ct[:])
321 if bytes.Compare(dst, pt[:]) != 0 {