]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost341194/hash_test.go
d2214cfded19d553c08d6ed47a0c2ef7c63145b8
[gogost.git] / src / cypherpunks.ru / gogost / gost341194 / hash_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 gost341194
18
19 import (
20         "bytes"
21         "crypto/rand"
22         "hash"
23         "testing"
24         "testing/quick"
25
26         "cypherpunks.ru/gogost/gost28147"
27 )
28
29 func TestHashInterface(t *testing.T) {
30         h := New(SboxDefault)
31         var _ hash.Hash = h
32 }
33
34 func TestVectors(t *testing.T) {
35         h := New(SboxDefault)
36
37         t.Run("empty", func(t *testing.T) {
38                 if bytes.Compare(h.Sum(nil), []byte{
39                         0xce, 0x85, 0xb9, 0x9c, 0xc4, 0x67, 0x52, 0xff,
40                         0xfe, 0xe3, 0x5c, 0xab, 0x9a, 0x7b, 0x02, 0x78,
41                         0xab, 0xb4, 0xc2, 0xd2, 0x05, 0x5c, 0xff, 0x68,
42                         0x5a, 0xf4, 0x91, 0x2c, 0x49, 0x49, 0x0f, 0x8d,
43                 }) != 0 {
44                         t.FailNow()
45                 }
46         })
47
48         t.Run("a", func(t *testing.T) {
49                 h.Reset()
50                 h.Write([]byte("a"))
51                 if bytes.Compare(h.Sum(nil), []byte{
52                         0xd4, 0x2c, 0x53, 0x9e, 0x36, 0x7c, 0x66, 0xe9,
53                         0xc8, 0x8a, 0x80, 0x1f, 0x66, 0x49, 0x34, 0x9c,
54                         0x21, 0x87, 0x1b, 0x43, 0x44, 0xc6, 0xa5, 0x73,
55                         0xf8, 0x49, 0xfd, 0xce, 0x62, 0xf3, 0x14, 0xdd,
56                 }) != 0 {
57                         t.FailNow()
58                 }
59         })
60
61         t.Run("abc", func(t *testing.T) {
62                 h.Reset()
63                 h.Write([]byte("abc"))
64                 if bytes.Compare(h.Sum(nil), []byte{
65                         0xf3, 0x13, 0x43, 0x48, 0xc4, 0x4f, 0xb1, 0xb2,
66                         0xa2, 0x77, 0x72, 0x9e, 0x22, 0x85, 0xeb, 0xb5,
67                         0xcb, 0x5e, 0x0f, 0x29, 0xc9, 0x75, 0xbc, 0x75,
68                         0x3b, 0x70, 0x49, 0x7c, 0x06, 0xa4, 0xd5, 0x1d,
69                 }) != 0 {
70                         t.FailNow()
71                 }
72         })
73
74         t.Run("message digest", func(t *testing.T) {
75                 h.Reset()
76                 h.Write([]byte("message digest"))
77                 if bytes.Compare(h.Sum(nil), []byte{
78                         0xad, 0x44, 0x34, 0xec, 0xb1, 0x8f, 0x2c, 0x99,
79                         0xb6, 0x0c, 0xbe, 0x59, 0xec, 0x3d, 0x24, 0x69,
80                         0x58, 0x2b, 0x65, 0x27, 0x3f, 0x48, 0xde, 0x72,
81                         0xdb, 0x2f, 0xde, 0x16, 0xa4, 0x88, 0x9a, 0x4d,
82                 }) != 0 {
83                         t.FailNow()
84                 }
85         })
86
87         t.Run("128U", func(t *testing.T) {
88                 h.Reset()
89                 for i := 0; i < 128; i++ {
90                         h.Write([]byte("U"))
91                 }
92                 if bytes.Compare(h.Sum(nil), []byte{
93                         0x53, 0xa3, 0xa3, 0xed, 0x25, 0x18, 0x0c, 0xef,
94                         0x0c, 0x1d, 0x85, 0xa0, 0x74, 0x27, 0x3e, 0x55,
95                         0x1c, 0x25, 0x66, 0x0a, 0x87, 0x06, 0x2a, 0x52,
96                         0xd9, 0x26, 0xa9, 0xe8, 0xfe, 0x57, 0x33, 0xa4,
97                 }) != 0 {
98                         t.FailNow()
99                 }
100         })
101
102         t.Run("lazy dog", func(t *testing.T) {
103                 h.Reset()
104                 h.Write([]byte("The quick brown fox jumps over the lazy dog"))
105                 if bytes.Compare(h.Sum(nil), []byte{
106                         0x77, 0xb7, 0xfa, 0x41, 0x0c, 0x9a, 0xc5, 0x8a,
107                         0x25, 0xf4, 0x9b, 0xca, 0x7d, 0x04, 0x68, 0xc9,
108                         0x29, 0x65, 0x29, 0x31, 0x5e, 0xac, 0xa7, 0x6b,
109                         0xd1, 0xa1, 0x0f, 0x37, 0x6d, 0x1f, 0x42, 0x94,
110                 }) != 0 {
111                         t.FailNow()
112                 }
113         })
114
115         t.Run("lazy cog", func(t *testing.T) {
116                 h.Reset()
117                 h.Write([]byte("The quick brown fox jumps over the lazy cog"))
118                 if bytes.Compare(h.Sum(nil), []byte{
119                         0xa3, 0xeb, 0xc4, 0xda, 0xaa, 0xb7, 0x8b, 0x0b,
120                         0xe1, 0x31, 0xda, 0xb5, 0x73, 0x7a, 0x7f, 0x67,
121                         0xe6, 0x02, 0x67, 0x0d, 0x54, 0x35, 0x21, 0x31,
122                         0x91, 0x50, 0xd2, 0xe1, 0x4e, 0xee, 0xc4, 0x45,
123                 }) != 0 {
124                         t.FailNow()
125                 }
126         })
127
128         t.Run("32", func(t *testing.T) {
129                 h.Reset()
130                 h.Write([]byte("This is message, length=32 bytes"))
131                 if bytes.Compare(h.Sum(nil), []byte{
132                         0xb1, 0xc4, 0x66, 0xd3, 0x75, 0x19, 0xb8, 0x2e,
133                         0x83, 0x19, 0x81, 0x9f, 0xf3, 0x25, 0x95, 0xe0,
134                         0x47, 0xa2, 0x8c, 0xb6, 0xf8, 0x3e, 0xff, 0x1c,
135                         0x69, 0x16, 0xa8, 0x15, 0xa6, 0x37, 0xff, 0xfa,
136                 }) != 0 {
137                         t.FailNow()
138                 }
139         })
140
141         t.Run("50", func(t *testing.T) {
142                 h.Reset()
143                 h.Write([]byte("Suppose the original message has length = 50 bytes"))
144                 if bytes.Compare(h.Sum(nil), []byte{
145                         0x47, 0x1a, 0xba, 0x57, 0xa6, 0x0a, 0x77, 0x0d,
146                         0x3a, 0x76, 0x13, 0x06, 0x35, 0xc1, 0xfb, 0xea,
147                         0x4e, 0xf1, 0x4d, 0xe5, 0x1f, 0x78, 0xb4, 0xae,
148                         0x57, 0xdd, 0x89, 0x3b, 0x62, 0xf5, 0x52, 0x08,
149                 }) != 0 {
150                         t.FailNow()
151                 }
152         })
153 }
154
155 func TestVectorsCryptoPro(t *testing.T) {
156         h := New(&gost28147.SboxIdGostR341194CryptoProParamSet)
157
158         t.Run("empty", func(t *testing.T) {
159                 if bytes.Compare(h.Sum(nil), []byte{
160                         0x98, 0x1e, 0x5f, 0x3c, 0xa3, 0x0c, 0x84, 0x14,
161                         0x87, 0x83, 0x0f, 0x84, 0xfb, 0x43, 0x3e, 0x13,
162                         0xac, 0x11, 0x01, 0x56, 0x9b, 0x9c, 0x13, 0x58,
163                         0x4a, 0xc4, 0x83, 0x23, 0x4c, 0xd6, 0x56, 0xc0,
164                 }) != 0 {
165                         t.FailNow()
166                 }
167         })
168
169         t.Run("a", func(t *testing.T) {
170                 h.Reset()
171                 h.Write([]byte("a"))
172                 if bytes.Compare(h.Sum(nil), []byte{
173                         0xe7, 0x4c, 0x52, 0xdd, 0x28, 0x21, 0x83, 0xbf,
174                         0x37, 0xaf, 0x00, 0x79, 0xc9, 0xf7, 0x80, 0x55,
175                         0x71, 0x5a, 0x10, 0x3f, 0x17, 0xe3, 0x13, 0x3c,
176                         0xef, 0xf1, 0xaa, 0xcf, 0x2f, 0x40, 0x30, 0x11,
177                 }) != 0 {
178                         t.FailNow()
179                 }
180         })
181
182         t.Run("abc", func(t *testing.T) {
183                 h.Reset()
184                 h.Write([]byte("abc"))
185                 if bytes.Compare(h.Sum(nil), []byte{
186                         0xb2, 0x85, 0x05, 0x6d, 0xbf, 0x18, 0xd7, 0x39,
187                         0x2d, 0x76, 0x77, 0x36, 0x95, 0x24, 0xdd, 0x14,
188                         0x74, 0x74, 0x59, 0xed, 0x81, 0x43, 0x99, 0x7e,
189                         0x16, 0x3b, 0x29, 0x86, 0xf9, 0x2f, 0xd4, 0x2c,
190                 }) != 0 {
191                         t.FailNow()
192                 }
193         })
194
195         t.Run("message digest", func(t *testing.T) {
196                 h.Reset()
197                 h.Write([]byte("message digest"))
198                 if bytes.Compare(h.Sum(nil), []byte{
199                         0xbc, 0x60, 0x41, 0xdd, 0x2a, 0xa4, 0x01, 0xeb,
200                         0xfa, 0x6e, 0x98, 0x86, 0x73, 0x41, 0x74, 0xfe,
201                         0xbd, 0xb4, 0x72, 0x9a, 0xa9, 0x72, 0xd6, 0x0f,
202                         0x54, 0x9a, 0xc3, 0x9b, 0x29, 0x72, 0x1b, 0xa0,
203                 }) != 0 {
204                         t.FailNow()
205                 }
206         })
207
208         t.Run("lazy dog", func(t *testing.T) {
209                 h.Reset()
210                 h.Write([]byte("The quick brown fox jumps over the lazy dog"))
211                 if bytes.Compare(h.Sum(nil), []byte{
212                         0x90, 0x04, 0x29, 0x4a, 0x36, 0x1a, 0x50, 0x8c,
213                         0x58, 0x6f, 0xe5, 0x3d, 0x1f, 0x1b, 0x02, 0x74,
214                         0x67, 0x65, 0xe7, 0x1b, 0x76, 0x54, 0x72, 0x78,
215                         0x6e, 0x47, 0x70, 0xd5, 0x65, 0x83, 0x0a, 0x76,
216                 }) != 0 {
217                         t.FailNow()
218                 }
219         })
220
221         t.Run("32", func(t *testing.T) {
222                 h.Reset()
223                 h.Write([]byte("This is message, length=32 bytes"))
224                 if bytes.Compare(h.Sum(nil), []byte{
225                         0x2c, 0xef, 0xc2, 0xf7, 0xb7, 0xbd, 0xc5, 0x14,
226                         0xe1, 0x8e, 0xa5, 0x7f, 0xa7, 0x4f, 0xf3, 0x57,
227                         0xe7, 0xfa, 0x17, 0xd6, 0x52, 0xc7, 0x5f, 0x69,
228                         0xcb, 0x1b, 0xe7, 0x89, 0x3e, 0xde, 0x48, 0xeb,
229                 }) != 0 {
230                         t.FailNow()
231                 }
232         })
233
234         t.Run("50", func(t *testing.T) {
235                 h.Reset()
236                 h.Write([]byte("Suppose the original message has length = 50 bytes"))
237                 if bytes.Compare(h.Sum(nil), []byte{
238                         0xc3, 0x73, 0x0c, 0x5c, 0xbc, 0xca, 0xcf, 0x91,
239                         0x5a, 0xc2, 0x92, 0x67, 0x6f, 0x21, 0xe8, 0xbd,
240                         0x4e, 0xf7, 0x53, 0x31, 0xd9, 0x40, 0x5e, 0x5f,
241                         0x1a, 0x61, 0xdc, 0x31, 0x30, 0xa6, 0x50, 0x11,
242                 }) != 0 {
243                         t.FailNow()
244                 }
245         })
246
247         t.Run("128U", func(t *testing.T) {
248                 h.Reset()
249                 for i := 0; i < 128; i++ {
250                         h.Write([]byte{'U'})
251                 }
252                 if bytes.Compare(h.Sum(nil), []byte{
253                         0x1c, 0x4a, 0xc7, 0x61, 0x46, 0x91, 0xbb, 0xf4,
254                         0x27, 0xfa, 0x23, 0x16, 0x21, 0x6b, 0xe8, 0xf1,
255                         0x0d, 0x92, 0xed, 0xfd, 0x37, 0xcd, 0x10, 0x27,
256                         0x51, 0x4c, 0x10, 0x08, 0xf6, 0x49, 0xc4, 0xe8,
257                 }) != 0 {
258                         t.FailNow()
259                 }
260         })
261 }
262
263 func TestRandom(t *testing.T) {
264         h := New(SboxDefault)
265         f := func(data []byte) bool {
266                 h.Reset()
267                 h.Write(data)
268                 d1 := h.Sum(nil)
269                 h.Reset()
270                 for _, c := range data {
271                         h.Write([]byte{c})
272                 }
273                 d2 := h.Sum(nil)
274                 return bytes.Compare(d1, d2) == 0
275         }
276         if err := quick.Check(f, nil); err != nil {
277                 t.Error(err)
278         }
279 }
280
281 func BenchmarkHash(b *testing.B) {
282         h := New(SboxDefault)
283         src := make([]byte, BlockSize+1)
284         rand.Read(src)
285         b.ResetTimer()
286         for i := 0; i < b.N; i++ {
287                 h.Write(src)
288                 h.Sum(nil)
289         }
290 }