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