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