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