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/>.
27 func TestMACVectors(t *testing.T) {
28 key := []byte("This is message\xFF length\x0032 bytes")
29 c := NewCipher(key, SboxDefault)
31 m, err := c.NewMAC(8, iv[:])
36 t.Run("a", func(t *testing.T) {
38 if bytes.Compare(m.Sum(nil), []byte{0xbd, 0x5d, 0x3b, 0x5b, 0x2b, 0x7b, 0x57, 0xaf}) != 0 {
43 t.Run("abc", func(t *testing.T) {
45 m.Write([]byte("abc"))
46 if bytes.Compare(m.Sum(nil), []byte{0x28, 0x66, 0x1e, 0x40, 0x80, 0x5b, 0x1f, 0xf9}) != 0 {
51 t.Run("128U", func(t *testing.T) {
53 for i := 0; i < 128; i++ {
56 if bytes.Compare(m.Sum(nil), []byte{0x1a, 0x06, 0xd1, 0xba, 0xd7, 0x45, 0x80, 0xef}) != 0 {
61 t.Run("xxxxxxxxxxxxx", func(t *testing.T) {
63 for i := 0; i < 13; i++ {
66 if bytes.Compare(m.Sum(nil), []byte{0x91, 0x7e, 0xe1, 0xf1, 0xa6, 0x68, 0xfb, 0xd3}) != 0 {
72 func TestMACRandom(t *testing.T) {
75 c := NewCipher(key[:], SboxDefault)
76 f := func(iv [BlockSize]byte, data []byte) bool {
80 m, err := c.NewMAC(8, iv[:])
88 for _, b := range data {
97 return bytes.Compare(tag1, tag2) == 0
99 if err := quick.Check(f, nil); err != nil {
104 func TestMACInterface(t *testing.T) {
105 var key [KeySize]byte
107 c := NewCipher(key[:], SboxDefault)
108 m, _ := c.NewMAC(8, iv[:])
112 func BenchmarkMAC(b *testing.B) {
113 key := make([]byte, KeySize)
114 iv := make([]byte, BlockSize)
117 b1 := make([]byte, BlockSize)
118 b2 := make([]byte, BlockSize)
121 c := NewCipher(key[:], SboxDefault)
122 mac, _ := c.NewMAC(BlockSize, iv)
124 for i := 0; i < b.N; i++ {