]> Cypherpunks.ru repositories - gogost.git/blob - internal/gost34112012/hash_test.go
Raise copyright years
[gogost.git] / internal / gost34112012 / hash_test.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2021 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, version 3 of the License.
7 //
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16 package gost34112012
17
18 import (
19         "bytes"
20         "crypto/rand"
21         "encoding"
22         "hash"
23         "testing"
24         "testing/quick"
25 )
26
27 func TestHashInterface(t *testing.T) {
28         h := New(64)
29         var _ hash.Hash = h
30         var _ encoding.BinaryMarshaler = h
31         var _ encoding.BinaryUnmarshaler = h
32 }
33
34 func TestVectors(t *testing.T) {
35         h512 := New(64)
36         h256 := New(32)
37
38         t.Run("1", func(t *testing.T) {
39                 m := []byte{
40                         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
41                         0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
42                         0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32, 0x33,
43                         0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30, 0x31,
44                         0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
45                         0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
46                         0x38, 0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35,
47                         0x36, 0x37, 0x38, 0x39, 0x30, 0x31, 0x32,
48                 }
49                 h512.Write(m)
50                 if bytes.Compare(h512.Sum(nil), []byte{
51                         0x1b, 0x54, 0xd0, 0x1a, 0x4a, 0xf5, 0xb9, 0xd5,
52                         0xcc, 0x3d, 0x86, 0xd6, 0x8d, 0x28, 0x54, 0x62,
53                         0xb1, 0x9a, 0xbc, 0x24, 0x75, 0x22, 0x2f, 0x35,
54                         0xc0, 0x85, 0x12, 0x2b, 0xe4, 0xba, 0x1f, 0xfa,
55                         0x00, 0xad, 0x30, 0xf8, 0x76, 0x7b, 0x3a, 0x82,
56                         0x38, 0x4c, 0x65, 0x74, 0xf0, 0x24, 0xc3, 0x11,
57                         0xe2, 0xa4, 0x81, 0x33, 0x2b, 0x08, 0xef, 0x7f,
58                         0x41, 0x79, 0x78, 0x91, 0xc1, 0x64, 0x6f, 0x48,
59                 }) != 0 {
60                         t.FailNow()
61                 }
62                 h256.Write(m)
63                 if bytes.Compare(h256.Sum(nil), []byte{
64                         0x9d, 0x15, 0x1e, 0xef, 0xd8, 0x59, 0x0b, 0x89,
65                         0xda, 0xa6, 0xba, 0x6c, 0xb7, 0x4a, 0xf9, 0x27,
66                         0x5d, 0xd0, 0x51, 0x02, 0x6b, 0xb1, 0x49, 0xa4,
67                         0x52, 0xfd, 0x84, 0xe5, 0xe5, 0x7b, 0x55, 0x00,
68                 }) != 0 {
69                         t.FailNow()
70                 }
71         })
72
73         t.Run("Се ветри", func(t *testing.T) {
74                 // It is CP1251-encoded "Се ветри, Стрибожи внуци, веютъ с моря
75                 // стрелами на храбрыя плъкы Игоревы" string
76                 h512.Reset()
77                 h256.Reset()
78                 m := []byte{
79                         0xd1, 0xe5, 0x20, 0xe2, 0xe5, 0xf2, 0xf0, 0xe8,
80                         0x2c, 0x20, 0xd1, 0xf2, 0xf0, 0xe8, 0xe1, 0xee,
81                         0xe6, 0xe8, 0x20, 0xe2, 0xed, 0xf3, 0xf6, 0xe8,
82                         0x2c, 0x20, 0xe2, 0xe5, 0xfe, 0xf2, 0xfa, 0x20,
83                         0xf1, 0x20, 0xec, 0xee, 0xf0, 0xff, 0x20, 0xf1,
84                         0xf2, 0xf0, 0xe5, 0xeb, 0xe0, 0xec, 0xe8, 0x20,
85                         0xed, 0xe0, 0x20, 0xf5, 0xf0, 0xe0, 0xe1, 0xf0,
86                         0xfb, 0xff, 0x20, 0xef, 0xeb, 0xfa, 0xea, 0xfb,
87                         0x20, 0xc8, 0xe3, 0xee, 0xf0, 0xe5, 0xe2, 0xfb,
88                 }
89                 h512.Write(m)
90                 if bytes.Compare(h512.Sum(nil), []byte{
91                         0x1e, 0x88, 0xe6, 0x22, 0x26, 0xbf, 0xca, 0x6f,
92                         0x99, 0x94, 0xf1, 0xf2, 0xd5, 0x15, 0x69, 0xe0,
93                         0xda, 0xf8, 0x47, 0x5a, 0x3b, 0x0f, 0xe6, 0x1a,
94                         0x53, 0x00, 0xee, 0xe4, 0x6d, 0x96, 0x13, 0x76,
95                         0x03, 0x5f, 0xe8, 0x35, 0x49, 0xad, 0xa2, 0xb8,
96                         0x62, 0x0f, 0xcd, 0x7c, 0x49, 0x6c, 0xe5, 0xb3,
97                         0x3f, 0x0c, 0xb9, 0xdd, 0xdc, 0x2b, 0x64, 0x60,
98                         0x14, 0x3b, 0x03, 0xda, 0xba, 0xc9, 0xfb, 0x28,
99                 }) != 0 {
100                         t.FailNow()
101                 }
102                 h256.Write(m)
103                 if bytes.Compare(h256.Sum(nil), []byte{
104                         0x9d, 0xd2, 0xfe, 0x4e, 0x90, 0x40, 0x9e, 0x5d,
105                         0xa8, 0x7f, 0x53, 0x97, 0x6d, 0x74, 0x05, 0xb0,
106                         0xc0, 0xca, 0xc6, 0x28, 0xfc, 0x66, 0x9a, 0x74,
107                         0x1d, 0x50, 0x06, 0x3c, 0x55, 0x7e, 0x8f, 0x50,
108                 }) != 0 {
109                         t.FailNow()
110                 }
111         })
112
113         t.Run("https://habr.com/ru/post/450024/", func(t *testing.T) {
114                 h256.Reset()
115                 m := []byte{
116                         0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1,
117                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
118                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
119                         0x3e, 0x00, 0x03, 0x00, 0xfe, 0xff, 0x09, 0x00,
120                         0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
121                         0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
122                         0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
123                         0x00, 0x10, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00,
124                         0x01, 0x00, 0x00, 0x00, 0xfe, 0xff, 0xff, 0xff,
125                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
127                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
128                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
129                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
130                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
131                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
132                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
133                         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
134                 }
135                 h256.Write(m)
136                 if bytes.Compare(h256.Sum(nil), []byte{
137                         0xc7, 0x66, 0x08, 0x55, 0x40, 0xca, 0xaa, 0x89,
138                         0x53, 0xbf, 0xcf, 0x7a, 0x1b, 0xa2, 0x20, 0x61,
139                         0x9c, 0xee, 0x50, 0xd6, 0x5d, 0xc2, 0x42, 0xf8,
140                         0x2f, 0x23, 0xba, 0x4b, 0x18, 0x0b, 0x18, 0xe0,
141                 }) != 0 {
142                         t.FailNow()
143                 }
144         })
145 }
146
147 func TestBlocksized(t *testing.T) {
148         h := New(64)
149         m := make([]byte, BlockSize)
150         for i := 0; i < BlockSize; i++ {
151                 m[i] = byte(i)
152         }
153         h.Write(m)
154         if bytes.Compare(h.Sum(nil), []byte{
155                 0x2a, 0xe5, 0x81, 0xf1, 0x8a, 0xe8, 0x5e, 0x35,
156                 0x96, 0xc9, 0x36, 0xac, 0xbe, 0xf9, 0x10, 0xf2,
157                 0xed, 0x70, 0xdc, 0xf9, 0x1e, 0xd5, 0xd2, 0x4b,
158                 0x39, 0xa5, 0xaf, 0x65, 0x7b, 0xf8, 0x23, 0x2a,
159                 0x30, 0x3d, 0x68, 0x60, 0x56, 0xc8, 0xc0, 0x0b,
160                 0xf3, 0x0d, 0x42, 0xe1, 0x6c, 0xe2, 0x55, 0x42,
161                 0x6f, 0xa8, 0xa1, 0x55, 0xdc, 0xb3, 0xeb, 0x82,
162                 0x2d, 0x92, 0x58, 0x08, 0xf7, 0xc7, 0xe3, 0x45,
163         }) != 0 {
164                 t.FailNow()
165         }
166 }
167
168 func TestBehaviour(t *testing.T) {
169         h := New(64)
170         // Sum does not change the state
171         hsh1 := h.Sum(nil)
172         if bytes.Compare(h.Sum(nil), hsh1) != 0 {
173                 t.FailNow()
174         }
175         // No data equals to no state changing
176         h.Write([]byte{})
177         if bytes.Compare(h.Sum(nil), hsh1) != 0 {
178                 t.FailNow()
179         }
180         // Just to be sure
181         h.Write([]byte{})
182         if bytes.Compare(h.Sum(nil), hsh1) != 0 {
183                 t.FailNow()
184         }
185 }
186
187 func TestRandom(t *testing.T) {
188         h := New(64)
189         f := func(data []byte) bool {
190                 h.Reset()
191                 h.Write(data)
192                 d1 := h.Sum(nil)
193                 h1Raw, err := h.MarshalBinary()
194                 if err != nil {
195                         return false
196                 }
197                 h.Reset()
198                 for _, c := range data {
199                         h.Write([]byte{c})
200                 }
201                 d2 := h.Sum(nil)
202                 if bytes.Compare(d1, d2) != 0 {
203                         return false
204                 }
205                 h2Raw, err := h.MarshalBinary()
206                 if err != nil {
207                         return false
208                 }
209                 if bytes.Compare(h1Raw, h2Raw) != 0 {
210                         return false
211                 }
212                 hNew := New(64)
213                 if err = hNew.UnmarshalBinary(h1Raw); err != nil {
214                         return false
215                 }
216                 return bytes.Compare(hNew.Sum(nil), d1) == 0
217         }
218         if err := quick.Check(f, nil); err != nil {
219                 t.Error(err)
220         }
221 }
222
223 func BenchmarkHash(b *testing.B) {
224         h := New(64)
225         src := make([]byte, BlockSize+1)
226         rand.Read(src)
227         b.ResetTimer()
228         for i := 0; i < b.N; i++ {
229                 h.Write(src)
230                 h.Sum(nil)
231         }
232 }