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