]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost28147/mac_test.go
fe07bafce78199ddfb8e7553b49efabd97f7a18c
[gogost.git] / src / cypherpunks.ru / gogost / gost28147 / mac_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/rand"
22         "hash"
23         "testing"
24         "testing/quick"
25 )
26
27 func TestMACVectors(t *testing.T) {
28         key := []byte("This is message\xFF length\x0032 bytes")
29         c := NewCipher(key, SboxDefault)
30         var iv [8]byte
31         m, err := c.NewMAC(8, iv[:])
32         if err != nil {
33                 t.FailNow()
34         }
35
36         t.Run("a", func(t *testing.T) {
37                 m.Write([]byte("a"))
38                 if bytes.Compare(m.Sum(nil), []byte{0xbd, 0x5d, 0x3b, 0x5b, 0x2b, 0x7b, 0x57, 0xaf}) != 0 {
39                         t.FailNow()
40                 }
41         })
42
43         t.Run("abc", func(t *testing.T) {
44                 m.Reset()
45                 m.Write([]byte("abc"))
46                 if bytes.Compare(m.Sum(nil), []byte{0x28, 0x66, 0x1e, 0x40, 0x80, 0x5b, 0x1f, 0xf9}) != 0 {
47                         t.FailNow()
48                 }
49         })
50
51         t.Run("128U", func(t *testing.T) {
52                 m.Reset()
53                 for i := 0; i < 128; i++ {
54                         m.Write([]byte("U"))
55                 }
56                 if bytes.Compare(m.Sum(nil), []byte{0x1a, 0x06, 0xd1, 0xba, 0xd7, 0x45, 0x80, 0xef}) != 0 {
57                         t.FailNow()
58                 }
59         })
60
61         t.Run("xxxxxxxxxxxxx", func(t *testing.T) {
62                 m.Reset()
63                 for i := 0; i < 13; i++ {
64                         m.Write([]byte("x"))
65                 }
66                 if bytes.Compare(m.Sum(nil), []byte{0x91, 0x7e, 0xe1, 0xf1, 0xa6, 0x68, 0xfb, 0xd3}) != 0 {
67                         t.FailNow()
68                 }
69         })
70 }
71
72 func TestMACRandom(t *testing.T) {
73         var key [KeySize]byte
74         rand.Read(key[:])
75         c := NewCipher(key[:], SboxDefault)
76         f := func(iv [BlockSize]byte, data []byte) bool {
77                 if len(data) == 0 {
78                         return true
79                 }
80                 m, err := c.NewMAC(8, iv[:])
81                 if err != nil {
82                         return false
83                 }
84
85                 var tag1 []byte
86                 var tag2 []byte
87
88                 for _, b := range data {
89                         m.Write([]byte{b})
90                 }
91                 m.Sum(tag1)
92
93                 m.Reset()
94                 m.Write(data)
95                 m.Sum(tag2)
96
97                 return bytes.Compare(tag1, tag2) == 0
98         }
99         if err := quick.Check(f, nil); err != nil {
100                 t.Error(err)
101         }
102 }
103
104 func TestMACInterface(t *testing.T) {
105         var key [KeySize]byte
106         var iv [8]byte
107         c := NewCipher(key[:], SboxDefault)
108         m, _ := c.NewMAC(8, iv[:])
109         var _ hash.Hash = m
110 }
111
112 func BenchmarkMAC(b *testing.B) {
113         key := make([]byte, KeySize)
114         iv := make([]byte, BlockSize)
115         rand.Read(key)
116         rand.Read(iv)
117         b1 := make([]byte, BlockSize)
118         b2 := make([]byte, BlockSize)
119         rand.Read(b1)
120         rand.Read(b2)
121         c := NewCipher(key[:], SboxDefault)
122         mac, _ := c.NewMAC(BlockSize, iv)
123         b.ResetTimer()
124         for i := 0; i < b.N; i++ {
125                 mac.Write(b1)
126                 mac.Write(b2)
127                 mac.Sum(nil)
128         }
129 }