]> Cypherpunks.ru repositories - gogost.git/blob - gost341194/hash.go
Use more modern Go version
[gogost.git] / gost341194 / hash.go
1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2023 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 // GOST R 34.11-94 hash function.
17 // RFC 5831.
18 package gost341194
19
20 import (
21         "encoding/binary"
22         "math/big"
23
24         "go.cypherpunks.ru/gogost/v5/gost28147"
25 )
26
27 const (
28         BlockSize = 32
29         Size      = 32
30 )
31
32 var (
33         SboxDefault *gost28147.Sbox = &gost28147.SboxIdGostR341194TestParamSet
34
35         c2 [BlockSize]byte = [BlockSize]byte{
36                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
37                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
39                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
40         }
41         c3 [BlockSize]byte = [BlockSize]byte{
42                 0xff, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff,
43                 0xff, 0x00, 0x00, 0xff, 0x00, 0xff, 0xff, 0x00,
44                 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff,
45                 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00,
46         }
47         c4 [BlockSize]byte = [BlockSize]byte{
48                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
50                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
52         }
53
54         big256 *big.Int = big.NewInt(0).SetBit(big.NewInt(0), 256, 1)
55 )
56
57 type Hash struct {
58         sbox *gost28147.Sbox
59         size uint64
60         hsh  [BlockSize]byte
61         chk  *big.Int
62         buf  []byte
63         tmp  [BlockSize]byte
64 }
65
66 func New(sbox *gost28147.Sbox) *Hash {
67         h := Hash{sbox: sbox}
68         h.Reset()
69         return &h
70 }
71
72 func (h *Hash) Reset() {
73         h.size = 0
74         h.hsh = [BlockSize]byte{
75                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
78                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79         }
80         h.chk = big.NewInt(0)
81         h.buf = h.buf[:0]
82 }
83
84 func (h *Hash) BlockSize() int {
85         return BlockSize
86 }
87
88 func (h *Hash) Size() int {
89         return BlockSize
90 }
91
92 func fA(in *[BlockSize]byte) *[BlockSize]byte {
93         out := new([BlockSize]byte)
94         out[0] = in[16+0] ^ in[24+0]
95         out[1] = in[16+1] ^ in[24+1]
96         out[2] = in[16+2] ^ in[24+2]
97         out[3] = in[16+3] ^ in[24+3]
98         out[4] = in[16+4] ^ in[24+4]
99         out[5] = in[16+5] ^ in[24+5]
100         out[6] = in[16+6] ^ in[24+6]
101         out[7] = in[16+7] ^ in[24+7]
102         copy(out[8:], in[0:24])
103         return out
104 }
105
106 func fP(in *[BlockSize]byte) *[BlockSize]byte {
107         return &[BlockSize]byte{
108                 in[0], in[8], in[16], in[24], in[1], in[9], in[17],
109                 in[25], in[2], in[10], in[18], in[26], in[3],
110                 in[11], in[19], in[27], in[4], in[12], in[20],
111                 in[28], in[5], in[13], in[21], in[29], in[6],
112                 in[14], in[22], in[30], in[7], in[15], in[23], in[31],
113         }
114 }
115
116 func fChi(in *[BlockSize]byte) *[BlockSize]byte {
117         out := new([BlockSize]byte)
118         out[0] = in[32-2] ^ in[32-4] ^ in[32-6] ^ in[32-8] ^ in[32-32] ^ in[32-26]
119         out[1] = in[32-1] ^ in[32-3] ^ in[32-5] ^ in[32-7] ^ in[32-31] ^ in[32-25]
120         copy(out[2:32], in[0:30])
121         return out
122 }
123
124 func blockReverse(dst, src []byte) {
125         for i, j := 0, BlockSize-1; i < j; i, j = i+1, j-1 {
126                 dst[i], dst[j] = src[j], src[i]
127         }
128 }
129
130 func blockXor(dst, a, b *[BlockSize]byte) {
131         for i := 0; i < BlockSize; i++ {
132                 dst[i] = a[i] ^ b[i]
133         }
134 }
135
136 func (h *Hash) step(hin, m [BlockSize]byte) [BlockSize]byte {
137         out := new([BlockSize]byte)
138         u := new([BlockSize]byte)
139         v := new([BlockSize]byte)
140         k := new([BlockSize]byte)
141         (*u) = hin
142         (*v) = m
143         blockXor(k, u, v)
144         k = fP(k)
145         blockReverse(k[:], k[:])
146         c := gost28147.NewCipher(k[:], h.sbox)
147         s := make([]byte, gost28147.BlockSize)
148         c.Encrypt(s, []byte{
149                 hin[31], hin[30], hin[29], hin[28], hin[27], hin[26], hin[25], hin[24],
150         })
151         out[31] = s[0]
152         out[30] = s[1]
153         out[29] = s[2]
154         out[28] = s[3]
155         out[27] = s[4]
156         out[26] = s[5]
157         out[25] = s[6]
158         out[24] = s[7]
159
160         blockXor(u, fA(u), &c2)
161         v = fA(fA(v))
162         blockXor(k, u, v)
163         k = fP(k)
164         blockReverse(k[:], k[:])
165         c = gost28147.NewCipher(k[:], h.sbox)
166         c.Encrypt(s, []byte{
167                 hin[23], hin[22], hin[21], hin[20], hin[19], hin[18], hin[17], hin[16],
168         })
169         out[23] = s[0]
170         out[22] = s[1]
171         out[21] = s[2]
172         out[20] = s[3]
173         out[19] = s[4]
174         out[18] = s[5]
175         out[17] = s[6]
176         out[16] = s[7]
177
178         blockXor(u, fA(u), &c3)
179         v = fA(fA(v))
180         blockXor(k, u, v)
181         k = fP(k)
182         blockReverse(k[:], k[:])
183         c = gost28147.NewCipher(k[:], h.sbox)
184         c.Encrypt(s, []byte{
185                 hin[15], hin[14], hin[13], hin[12], hin[11], hin[10], hin[9], hin[8],
186         })
187         out[15] = s[0]
188         out[14] = s[1]
189         out[13] = s[2]
190         out[12] = s[3]
191         out[11] = s[4]
192         out[10] = s[5]
193         out[9] = s[6]
194         out[8] = s[7]
195
196         blockXor(u, fA(u), &c4)
197         v = fA(fA(v))
198         blockXor(k, u, v)
199         k = fP(k)
200         blockReverse(k[:], k[:])
201         c = gost28147.NewCipher(k[:], h.sbox)
202         c.Encrypt(s, []byte{
203                 hin[7], hin[6], hin[5], hin[4], hin[3], hin[2], hin[1], hin[0],
204         })
205         out[7] = s[0]
206         out[6] = s[1]
207         out[5] = s[2]
208         out[4] = s[3]
209         out[3] = s[4]
210         out[2] = s[5]
211         out[1] = s[6]
212         out[0] = s[7]
213
214         for i := 0; i < 12; i++ {
215                 out = fChi(out)
216         }
217         blockXor(out, out, &m)
218         out = fChi(out)
219         blockXor(out, out, &hin)
220         for i := 0; i < 61; i++ {
221                 out = fChi(out)
222         }
223         return *out
224 }
225
226 func (h *Hash) chkAdd(data []byte) *big.Int {
227         i := big.NewInt(0).SetBytes(data)
228         i.Add(i, h.chk)
229         if i.Cmp(big256) != -1 {
230                 i.Sub(i, big256)
231         }
232         return i
233 }
234
235 func (h *Hash) Write(data []byte) (int, error) {
236         h.buf = append(h.buf, data...)
237         for len(h.buf) >= BlockSize {
238                 h.size += BlockSize * 8
239                 blockReverse(h.tmp[:], h.buf[:BlockSize])
240                 h.chk = h.chkAdd(h.tmp[:])
241                 h.buf = h.buf[BlockSize:]
242                 h.hsh = h.step(h.hsh, h.tmp)
243         }
244         return len(data), nil
245 }
246
247 func (h *Hash) Sum(in []byte) []byte {
248         size := h.size
249         chk := h.chk
250         hsh := h.hsh
251         block := new([BlockSize]byte)
252         if len(h.buf) != 0 {
253                 size += uint64(len(h.buf)) * 8
254                 copy(block[:], h.buf)
255                 blockReverse(block[:], block[:])
256                 chk = h.chkAdd(block[:])
257                 hsh = h.step(hsh, *block)
258                 block = new([BlockSize]byte)
259         }
260         binary.BigEndian.PutUint64(block[24:], size)
261         hsh = h.step(hsh, *block)
262         block = new([BlockSize]byte)
263         chkBytes := chk.Bytes()
264         copy(block[BlockSize-len(chkBytes):], chkBytes)
265         hsh = h.step(hsh, *block)
266         blockReverse(hsh[:], hsh[:])
267         return append(in, hsh[:]...)
268 }