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/>.
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) {
46 var _ cipher.Block = NewCipher(make([]byte, KeySize))
49 func TestRandom(t *testing.T) {
50 data := make([]byte, BlockSize)
51 f := func(key [KeySize]byte, pt [BlockSize]byte) bool {
52 io.ReadFull(rand.Reader, key[:])
53 c := NewCipher(key[:])
54 c.Encrypt(data, pt[:])
56 return bytes.Compare(data, pt[:]) == 0
58 if err := quick.Check(f, nil); err != nil {
63 func BenchmarkEncrypt(b *testing.B) {
64 key := make([]byte, KeySize)
65 io.ReadFull(rand.Reader, key)
67 blk := make([]byte, BlockSize)
69 for i := 0; i < b.N; i++ {
74 func BenchmarkDecrypt(b *testing.B) {
75 key := make([]byte, KeySize)
76 io.ReadFull(rand.Reader, key)
78 blk := make([]byte, BlockSize)
80 for i := 0; i < b.N; i++ {
85 func TestS(t *testing.T) {
86 blk := [BlockSize]byte{
87 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88,
88 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x00,
91 if bytes.Compare(blk[:], []byte{
92 0xb6, 0x6c, 0xd8, 0x88, 0x7d, 0x38, 0xe8, 0xd7,
93 0x77, 0x65, 0xae, 0xea, 0x0c, 0x9a, 0x7e, 0xfc,
98 if bytes.Compare(blk[:], []byte{
99 0x55, 0x9d, 0x8d, 0xd7, 0xbd, 0x06, 0xcb, 0xfe,
100 0x7e, 0x7b, 0x26, 0x25, 0x23, 0x28, 0x0d, 0x39,
105 if bytes.Compare(blk[:], []byte{
106 0x0c, 0x33, 0x22, 0xfe, 0xd5, 0x31, 0xe4, 0x63,
107 0x0d, 0x80, 0xef, 0x5c, 0x5a, 0x81, 0xc5, 0x0b,
112 if bytes.Compare(blk[:], []byte{
113 0x23, 0xae, 0x65, 0x63, 0x3f, 0x84, 0x2d, 0x29,
114 0xc5, 0xdf, 0x52, 0x9c, 0x13, 0xf5, 0xac, 0xda,
120 func R(blk *[BlockSize]byte) {
124 func TestR(t *testing.T) {
125 blk := [BlockSize]byte{
126 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
127 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
130 if bytes.Compare(blk[:], []byte{
131 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
132 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
137 if bytes.Compare(blk[:], []byte{
138 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
144 if bytes.Compare(blk[:], []byte{
145 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
146 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
151 if bytes.Compare(blk[:], []byte{
152 0x0d, 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00,
153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159 func TestL(t *testing.T) {
160 blk := [BlockSize]byte{
161 0x64, 0xa5, 0x94, 0x00, 0x00, 0x00, 0x00, 0x00,
162 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
165 if bytes.Compare(blk[:], []byte{
166 0xd4, 0x56, 0x58, 0x4d, 0xd0, 0xe3, 0xe8, 0x4c,
167 0xc3, 0x16, 0x6e, 0x4b, 0x7f, 0xa2, 0x89, 0x0d,
172 if bytes.Compare(blk[:], []byte{
173 0x79, 0xd2, 0x62, 0x21, 0xb8, 0x7b, 0x58, 0x4c,
174 0xd4, 0x2f, 0xbc, 0x4f, 0xfe, 0xa5, 0xde, 0x9a,
179 if bytes.Compare(blk[:], []byte{
180 0x0e, 0x93, 0x69, 0x1a, 0x0c, 0xfc, 0x60, 0x40,
181 0x8b, 0x7b, 0x68, 0xf6, 0x6b, 0x51, 0x3c, 0x13,
186 if bytes.Compare(blk[:], []byte{
187 0xe6, 0xa8, 0x09, 0x4f, 0xee, 0x0a, 0xa2, 0x04,
188 0xfd, 0x97, 0xbc, 0xb0, 0xb4, 0x4b, 0x85, 0x80,
194 func TestC(t *testing.T) {
195 if bytes.Compare(cBlk[0][:], []byte{
196 0x6e, 0xa2, 0x76, 0x72, 0x6c, 0x48, 0x7a, 0xb8,
197 0x5d, 0x27, 0xbd, 0x10, 0xdd, 0x84, 0x94, 0x01,
201 if bytes.Compare(cBlk[1][:], []byte{
202 0xdc, 0x87, 0xec, 0xe4, 0xd8, 0x90, 0xf4, 0xb3,
203 0xba, 0x4e, 0xb9, 0x20, 0x79, 0xcb, 0xeb, 0x02,
207 if bytes.Compare(cBlk[2][:], []byte{
208 0xb2, 0x25, 0x9a, 0x96, 0xb4, 0xd8, 0x8e, 0x0b,
209 0xe7, 0x69, 0x04, 0x30, 0xa4, 0x4f, 0x7f, 0x03,
213 if bytes.Compare(cBlk[3][:], []byte{
214 0x7b, 0xcd, 0x1b, 0x0b, 0x73, 0xe3, 0x2b, 0xa5,
215 0xb7, 0x9c, 0xb1, 0x40, 0xf2, 0x55, 0x15, 0x04,
219 if bytes.Compare(cBlk[4][:], []byte{
220 0x15, 0x6f, 0x6d, 0x79, 0x1f, 0xab, 0x51, 0x1d,
221 0xea, 0xbb, 0x0c, 0x50, 0x2f, 0xd1, 0x81, 0x05,
225 if bytes.Compare(cBlk[5][:], []byte{
226 0xa7, 0x4a, 0xf7, 0xef, 0xab, 0x73, 0xdf, 0x16,
227 0x0d, 0xd2, 0x08, 0x60, 0x8b, 0x9e, 0xfe, 0x06,
231 if bytes.Compare(cBlk[6][:], []byte{
232 0xc9, 0xe8, 0x81, 0x9d, 0xc7, 0x3b, 0xa5, 0xae,
233 0x50, 0xf5, 0xb5, 0x70, 0x56, 0x1a, 0x6a, 0x07,
237 if bytes.Compare(cBlk[7][:], []byte{
238 0xf6, 0x59, 0x36, 0x16, 0xe6, 0x05, 0x56, 0x89,
239 0xad, 0xfb, 0xa1, 0x80, 0x27, 0xaa, 0x2a, 0x08,
245 func TestRoundKeys(t *testing.T) {
247 if bytes.Compare(c.ks[0][:], []byte{
248 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
249 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
253 if bytes.Compare(c.ks[1][:], []byte{
254 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
255 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
259 if bytes.Compare(c.ks[2][:], []byte{
260 0xdb, 0x31, 0x48, 0x53, 0x15, 0x69, 0x43, 0x43,
261 0x22, 0x8d, 0x6a, 0xef, 0x8c, 0xc7, 0x8c, 0x44,
265 if bytes.Compare(c.ks[3][:], []byte{
266 0x3d, 0x45, 0x53, 0xd8, 0xe9, 0xcf, 0xec, 0x68,
267 0x15, 0xeb, 0xad, 0xc4, 0x0a, 0x9f, 0xfd, 0x04,
271 if bytes.Compare(c.ks[4][:], []byte{
272 0x57, 0x64, 0x64, 0x68, 0xc4, 0x4a, 0x5e, 0x28,
273 0xd3, 0xe5, 0x92, 0x46, 0xf4, 0x29, 0xf1, 0xac,
277 if bytes.Compare(c.ks[5][:], []byte{
278 0xbd, 0x07, 0x94, 0x35, 0x16, 0x5c, 0x64, 0x32,
279 0xb5, 0x32, 0xe8, 0x28, 0x34, 0xda, 0x58, 0x1b,
283 if bytes.Compare(c.ks[6][:], []byte{
284 0x51, 0xe6, 0x40, 0x75, 0x7e, 0x87, 0x45, 0xde,
285 0x70, 0x57, 0x27, 0x26, 0x5a, 0x00, 0x98, 0xb1,
289 if bytes.Compare(c.ks[7][:], []byte{
290 0x5a, 0x79, 0x25, 0x01, 0x7b, 0x9f, 0xdd, 0x3e,
291 0xd7, 0x2a, 0x91, 0xa2, 0x22, 0x86, 0xf9, 0x84,
295 if bytes.Compare(c.ks[8][:], []byte{
296 0xbb, 0x44, 0xe2, 0x53, 0x78, 0xc7, 0x31, 0x23,
297 0xa5, 0xf3, 0x2f, 0x73, 0xcd, 0xb6, 0xe5, 0x17,
301 if bytes.Compare(c.ks[9][:], []byte{
302 0x72, 0xe9, 0xdd, 0x74, 0x16, 0xbc, 0xf4, 0x5b,
303 0x75, 0x5d, 0xba, 0xa8, 0x8e, 0x4a, 0x40, 0x43,
309 func TestVectorEncrypt(t *testing.T) {
311 dst := make([]byte, BlockSize)
312 c.Encrypt(dst, pt[:])
313 if bytes.Compare(dst, ct[:]) != 0 {
318 func TestVectorDecrypt(t *testing.T) {
320 dst := make([]byte, BlockSize)
321 c.Decrypt(dst, ct[:])
322 if bytes.Compare(dst, pt[:]) != 0 {