1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2019 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, either version 3 of the License, or
7 // (at your option) any later version.
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.
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/>.
29 key [KeySize]byte = [KeySize]byte{
30 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
31 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
32 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
33 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
35 pt [BlockSize]byte = [BlockSize]byte{
36 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00,
37 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
39 ct [BlockSize]byte = [BlockSize]byte{
40 0x7f, 0x67, 0x9d, 0x90, 0xbe, 0xbc, 0x24, 0x30,
41 0x5a, 0x46, 0x8d, 0x42, 0xb9, 0xd4, 0xed, 0xcd,
45 func TestCipherInterface(t *testing.T) {
47 var _ cipher.Block = NewCipher(key)
50 func TestRandom(t *testing.T) {
51 data := make([]byte, BlockSize)
52 f := func(key [KeySize]byte, pt [BlockSize]byte) bool {
53 io.ReadFull(rand.Reader, key[:])
55 c.Encrypt(data, pt[:])
57 return bytes.Compare(data, pt[:]) == 0
59 if err := quick.Check(f, nil); err != nil {
64 func BenchmarkEncrypt(b *testing.B) {
66 io.ReadFull(rand.Reader, key[:])
68 blk := make([]byte, BlockSize)
70 for i := 0; i < b.N; i++ {
75 func BenchmarkDecrypt(b *testing.B) {
77 io.ReadFull(rand.Reader, key[:])
79 blk := make([]byte, BlockSize)
81 for i := 0; i < b.N; i++ {
86 func TestS(t *testing.T) {
87 blk := [BlockSize]byte{
88 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
89 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00,
92 if bytes.Compare(blk[:], []byte{
93 0xb6, 0x6c, 0xd8, 0x88, 0x7d, 0x38, 0xe8, 0xd7,
94 0x77, 0x65, 0xae, 0xea, 0x0c, 0x9a, 0x7e, 0xfc,
99 if bytes.Compare(blk[:], []byte{
100 0x55, 0x9d, 0x8d, 0xd7, 0xbd, 0x06, 0xcb, 0xfe,
101 0x7e, 0x7b, 0x26, 0x25, 0x23, 0x28, 0x0d, 0x39,
106 if bytes.Compare(blk[:], []byte{
107 0x0c, 0x33, 0x22, 0xfe, 0xd5, 0x31, 0xe4, 0x63,
108 0x0d, 0x80, 0xef, 0x5c, 0x5a, 0x81, 0xc5, 0x0b,
113 if bytes.Compare(blk[:], []byte{
114 0x23, 0xae, 0x65, 0x63, 0x3f, 0x84, 0x2d, 0x29,
115 0xc5, 0xdf, 0x52, 0x9c, 0x13, 0xf5, 0xac, 0xda,
121 func R(blk *[BlockSize]byte) {
125 func TestR(t *testing.T) {
126 blk := [BlockSize]byte{
127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
128 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
131 if bytes.Compare(blk[:], []byte{
132 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
133 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
138 if bytes.Compare(blk[:], []byte{
139 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
140 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
145 if bytes.Compare(blk[:], []byte{
146 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
147 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
152 if bytes.Compare(blk[:], []byte{
153 0x0d, 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00,
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160 func TestL(t *testing.T) {
161 blk := [BlockSize]byte{
162 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
163 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
166 if bytes.Compare(blk[:], []byte{
167 0xd4, 0x56, 0x58, 0x4d, 0xd0, 0xe3, 0xe8, 0x4c,
168 0xc3, 0x16, 0x6e, 0x4b, 0x7f, 0xa2, 0x89, 0x0d,
173 if bytes.Compare(blk[:], []byte{
174 0x79, 0xd2, 0x62, 0x21, 0xb8, 0x7b, 0x58, 0x4c,
175 0xd4, 0x2f, 0xbc, 0x4f, 0xfe, 0xa5, 0xde, 0x9a,
180 if bytes.Compare(blk[:], []byte{
181 0x0e, 0x93, 0x69, 0x1a, 0x0c, 0xfc, 0x60, 0x40,
182 0x8b, 0x7b, 0x68, 0xf6, 0x6b, 0x51, 0x3c, 0x13,
187 if bytes.Compare(blk[:], []byte{
188 0xe6, 0xa8, 0x09, 0x4f, 0xee, 0x0a, 0xa2, 0x04,
189 0xfd, 0x97, 0xbc, 0xb0, 0xb4, 0x4b, 0x85, 0x80,
195 func TestC(t *testing.T) {
196 if bytes.Compare(cBlk[0][:], []byte{
197 0x6e, 0xa2, 0x76, 0x72, 0x6c, 0x48, 0x7a, 0xb8,
198 0x5d, 0x27, 0xbd, 0x10, 0xdd, 0x84, 0x94, 0x01,
202 if bytes.Compare(cBlk[1][:], []byte{
203 0xdc, 0x87, 0xec, 0xe4, 0xd8, 0x90, 0xf4, 0xb3,
204 0xba, 0x4e, 0xb9, 0x20, 0x79, 0xcb, 0xeb, 0x02,
208 if bytes.Compare(cBlk[2][:], []byte{
209 0xb2, 0x25, 0x9a, 0x96, 0xb4, 0xd8, 0x8e, 0x0b,
210 0xe7, 0x69, 0x04, 0x30, 0xa4, 0x4f, 0x7f, 0x03,
214 if bytes.Compare(cBlk[3][:], []byte{
215 0x7b, 0xcd, 0x1b, 0x0b, 0x73, 0xe3, 0x2b, 0xa5,
216 0xb7, 0x9c, 0xb1, 0x40, 0xf2, 0x55, 0x15, 0x04,
220 if bytes.Compare(cBlk[4][:], []byte{
221 0x15, 0x6f, 0x6d, 0x79, 0x1f, 0xab, 0x51, 0x1d,
222 0xea, 0xbb, 0x0c, 0x50, 0x2f, 0xd1, 0x81, 0x05,
226 if bytes.Compare(cBlk[5][:], []byte{
227 0xa7, 0x4a, 0xf7, 0xef, 0xab, 0x73, 0xdf, 0x16,
228 0x0d, 0xd2, 0x08, 0x60, 0x8b, 0x9e, 0xfe, 0x06,
232 if bytes.Compare(cBlk[6][:], []byte{
233 0xc9, 0xe8, 0x81, 0x9d, 0xc7, 0x3b, 0xa5, 0xae,
234 0x50, 0xf5, 0xb5, 0x70, 0x56, 0x1a, 0x6a, 0x07,
238 if bytes.Compare(cBlk[7][:], []byte{
239 0xf6, 0x59, 0x36, 0x16, 0xe6, 0x05, 0x56, 0x89,
240 0xad, 0xfb, 0xa1, 0x80, 0x27, 0xaa, 0x2a, 0x08,
246 func TestRoundKeys(t *testing.T) {
248 if bytes.Compare(c.ks[0][:], []byte{
249 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
250 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
254 if bytes.Compare(c.ks[1][:], []byte{
255 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
256 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
260 if bytes.Compare(c.ks[2][:], []byte{
261 0xdb, 0x31, 0x48, 0x53, 0x15, 0x69, 0x43, 0x43,
262 0x22, 0x8d, 0x6a, 0xef, 0x8c, 0xc7, 0x8c, 0x44,
266 if bytes.Compare(c.ks[3][:], []byte{
267 0x3d, 0x45, 0x53, 0xd8, 0xe9, 0xcf, 0xec, 0x68,
268 0x15, 0xeb, 0xad, 0xc4, 0x0a, 0x9f, 0xfd, 0x04,
272 if bytes.Compare(c.ks[4][:], []byte{
273 0x57, 0x64, 0x64, 0x68, 0xc4, 0x4a, 0x5e, 0x28,
274 0xd3, 0xe5, 0x92, 0x46, 0xf4, 0x29, 0xf1, 0xac,
278 if bytes.Compare(c.ks[5][:], []byte{
279 0xbd, 0x07, 0x94, 0x35, 0x16, 0x5c, 0x64, 0x32,
280 0xb5, 0x32, 0xe8, 0x28, 0x34, 0xda, 0x58, 0x1b,
284 if bytes.Compare(c.ks[6][:], []byte{
285 0x51, 0xe6, 0x40, 0x75, 0x7e, 0x87, 0x45, 0xde,
286 0x70, 0x57, 0x27, 0x26, 0x5a, 0x00, 0x98, 0xb1,
290 if bytes.Compare(c.ks[7][:], []byte{
291 0x5a, 0x79, 0x25, 0x01, 0x7b, 0x9f, 0xdd, 0x3e,
292 0xd7, 0x2a, 0x91, 0xa2, 0x22, 0x86, 0xf9, 0x84,
296 if bytes.Compare(c.ks[8][:], []byte{
297 0xbb, 0x44, 0xe2, 0x53, 0x78, 0xc7, 0x31, 0x23,
298 0xa5, 0xf3, 0x2f, 0x73, 0xcd, 0xb6, 0xe5, 0x17,
302 if bytes.Compare(c.ks[9][:], []byte{
303 0x72, 0xe9, 0xdd, 0x74, 0x16, 0xbc, 0xf4, 0x5b,
304 0x75, 0x5d, 0xba, 0xa8, 0x8e, 0x4a, 0x40, 0x43,
310 func TestVectorEncrypt(t *testing.T) {
312 dst := make([]byte, BlockSize)
313 c.Encrypt(dst, pt[:])
314 if bytes.Compare(dst, ct[:]) != 0 {
319 func TestVectorDecrypt(t *testing.T) {
321 dst := make([]byte, BlockSize)
322 c.Decrypt(dst, ct[:])
323 if bytes.Compare(dst, pt[:]) != 0 {