]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost34112012/hash_test.go
1.1 release is ready
[gogost.git] / src / cypherpunks.ru / gogost / gost34112012 / hash_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 gost34112012
18
19 import (
20         "bytes"
21         "crypto/rand"
22         "hash"
23         "testing"
24         "testing/quick"
25 )
26
27 func TestHashInterface(t *testing.T) {
28         h := New(512)
29         var _ hash.Hash = h
30 }
31
32 func TestVectors(t *testing.T) {
33         h512 := New(512)
34         h256 := New(256)
35
36         // First vector
37         m := []byte{
38                 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
39                 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
40                 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33,
41                 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
42                 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
43                 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
44                 0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
45                 0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32,
46         }
47         h512.Write(m)
48         if bytes.Compare(h512.Sum(nil), []byte{
49                 0x1b, 0x54, 0xd0, 0x1a, 0x4a, 0xf5, 0xb9, 0xd5,
50                 0xcc, 0x3d, 0x86, 0xd6, 0x8d, 0x28, 0x54, 0x62,
51                 0xb1, 0x9a, 0xbc, 0x24, 0x75, 0x22, 0x2f, 0x35,
52                 0xc0, 0x85, 0x12, 0x2b, 0xe4, 0xba, 0x1f, 0xfa,
53                 0x00, 0xad, 0x30, 0xf8, 0x76, 0x7b, 0x3a, 0x82,
54                 0x38, 0x4c, 0x65, 0x74, 0xf0, 0x24, 0xc3, 0x11,
55                 0xe2, 0xa4, 0x81, 0x33, 0x2b, 0x08, 0xef, 0x7f,
56                 0x41, 0x79, 0x78, 0x91, 0xc1, 0x64, 0x6f, 0x48,
57         }) != 0 {
58                 t.Fail()
59         }
60         h256.Write(m)
61         if bytes.Compare(h256.Sum(nil), []byte{
62                 0x9d, 0x15, 0x1e, 0xef, 0xd8, 0x59, 0x0b, 0x89,
63                 0xda, 0xa6, 0xba, 0x6c, 0xb7, 0x4a, 0xf9, 0x27,
64                 0x5d, 0xd0, 0x51, 0x02, 0x6b, 0xb1, 0x49, 0xa4,
65                 0x52, 0xfd, 0x84, 0xe5, 0xe5, 0x7b, 0x55, 0x00,
66         }) != 0 {
67                 t.Fail()
68         }
69
70         // Second vector
71         h512.Reset()
72         h256.Reset()
73         m = []byte{
74                 0xd1, 0xe5, 0x20, 0xe2, 0xe5, 0xf2, 0xf0, 0xe8,
75                 0x2c, 0x20, 0xd1, 0xf2, 0xf0, 0xe8, 0xe1, 0xee,
76                 0xe6, 0xe8, 0x20, 0xe2, 0xed, 0xf3, 0xf6, 0xe8,
77                 0x2c, 0x20, 0xe2, 0xe5, 0xfe, 0xf2, 0xfa, 0x20,
78                 0xf1, 0x20, 0xec, 0xee, 0xf0, 0xff, 0x20, 0xf1,
79                 0xf2, 0xf0, 0xe5, 0xeb, 0xe0, 0xec, 0xe8, 0x20,
80                 0xed, 0xe0, 0x20, 0xf5, 0xf0, 0xe0, 0xe1, 0xf0,
81                 0xfb, 0xff, 0x20, 0xef, 0xeb, 0xfa, 0xea, 0xfb,
82                 0x20, 0xc8, 0xe3, 0xee, 0xf0, 0xe5, 0xe2, 0xfb,
83         }
84         h512.Write(m)
85         if bytes.Compare(h512.Sum(nil), []byte{
86                 0x1e, 0x88, 0xe6, 0x22, 0x26, 0xbf, 0xca, 0x6f,
87                 0x99, 0x94, 0xf1, 0xf2, 0xd5, 0x15, 0x69, 0xe0,
88                 0xda, 0xf8, 0x47, 0x5a, 0x3b, 0x0f, 0xe6, 0x1a,
89                 0x53, 0x00, 0xee, 0xe4, 0x6d, 0x96, 0x13, 0x76,
90                 0x03, 0x5f, 0xe8, 0x35, 0x49, 0xad, 0xa2, 0xb8,
91                 0x62, 0x0f, 0xcd, 0x7c, 0x49, 0x6c, 0xe5, 0xb3,
92                 0x3f, 0x0c, 0xb9, 0xdd, 0xdc, 0x2b, 0x64, 0x60,
93                 0x14, 0x3b, 0x03, 0xda, 0xba, 0xc9, 0xfb, 0x28,
94         }) != 0 {
95                 t.Fail()
96         }
97         h256.Write(m)
98         if bytes.Compare(h256.Sum(nil), []byte{
99                 0x9d, 0xd2, 0xfe, 0x4e, 0x90, 0x40, 0x9e, 0x5d,
100                 0xa8, 0x7f, 0x53, 0x97, 0x6d, 0x74, 0x05, 0xb0,
101                 0xc0, 0xca, 0xc6, 0x28, 0xfc, 0x66, 0x9a, 0x74,
102                 0x1d, 0x50, 0x06, 0x3c, 0x55, 0x7e, 0x8f, 0x50,
103         }) != 0 {
104                 t.Fail()
105         }
106 }
107
108 func TestBlocksized(t *testing.T) {
109         h := New(512)
110         m := make([]byte, BlockSize)
111         for i := 0; i < BlockSize; i++ {
112                 m[i] = byte(i)
113         }
114         h.Write(m)
115         if bytes.Compare(h.Sum(nil), []byte{
116                 0x2a, 0xe5, 0x81, 0xf1, 0x8a, 0xe8, 0x5e, 0x35,
117                 0x96, 0xc9, 0x36, 0xac, 0xbe, 0xf9, 0x10, 0xf2,
118                 0xed, 0x70, 0xdc, 0xf9, 0x1e, 0xd5, 0xd2, 0x4b,
119                 0x39, 0xa5, 0xaf, 0x65, 0x7b, 0xf8, 0x23, 0x2a,
120                 0x30, 0x3d, 0x68, 0x60, 0x56, 0xc8, 0xc0, 0x0b,
121                 0xf3, 0x0d, 0x42, 0xe1, 0x6c, 0xe2, 0x55, 0x42,
122                 0x6f, 0xa8, 0xa1, 0x55, 0xdc, 0xb3, 0xeb, 0x82,
123                 0x2d, 0x92, 0x58, 0x08, 0xf7, 0xc7, 0xe3, 0x45,
124         }) != 0 {
125                 t.Fail()
126         }
127 }
128
129 func TestBehaviour(t *testing.T) {
130         h := New(512)
131         // Sum does not change the state
132         hsh1 := h.Sum(nil)
133         if bytes.Compare(h.Sum(nil), hsh1) != 0 {
134                 t.Fail()
135         }
136         // No data equals to no state changing
137         h.Write([]byte{})
138         if bytes.Compare(h.Sum(nil), hsh1) != 0 {
139                 t.Fail()
140         }
141         // Just to be sure
142         h.Write([]byte{})
143         if bytes.Compare(h.Sum(nil), hsh1) != 0 {
144                 t.Fail()
145         }
146 }
147
148 func TestRandom(t *testing.T) {
149         h := New(512)
150         f := func(data []byte) bool {
151                 h.Reset()
152                 h.Write(data)
153                 d1 := h.Sum(nil)
154                 h.Reset()
155                 for _, c := range data {
156                         h.Write([]byte{c})
157                 }
158                 d2 := h.Sum(nil)
159                 return bytes.Compare(d1, d2) == 0
160         }
161         if err := quick.Check(f, nil); err != nil {
162                 t.Error(err)
163         }
164 }
165
166 func BenchmarkHash(b *testing.B) {
167         h := New(512)
168         src := make([]byte, BlockSize+1)
169         rand.Read(src)
170         b.ResetTimer()
171         for i := 0; i < b.N; i++ {
172                 h.Write(src)
173                 h.Sum(nil)
174         }
175 }