]> Cypherpunks.ru repositories - gogost.git/blob - src/cypherpunks.ru/gogost/gost341194/hash.go
1.1 release is ready
[gogost.git] / src / cypherpunks.ru / gogost / gost341194 / hash.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 // GOST R 34.11-94 hash function.
18 // RFC 5831.
19 package gost341194
20
21 import (
22         "encoding/binary"
23         "math/big"
24
25         "cypherpunks.ru/gogost/gost28147"
26 )
27
28 const (
29         BlockSize = 32
30 )
31
32 var (
33         SboxDefault *gost28147.Sbox = &gost28147.GostR3411_94_TestParamSet
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 = nil
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         return
135 }
136
137 func (h *Hash) step(hin, m [BlockSize]byte) [BlockSize]byte {
138         out := new([BlockSize]byte)
139         u := new([BlockSize]byte)
140         v := new([BlockSize]byte)
141         k := new([BlockSize]byte)
142         (*u) = hin
143         (*v) = m
144         blockXor(k, u, v)
145         k = fP(k)
146         blockReverse(k[:], k[:])
147         kk := *k
148         c := gost28147.NewCipher(kk, h.sbox)
149         s := make([]byte, gost28147.BlockSize)
150         c.Encrypt(s, []byte{
151                 hin[31], hin[30], hin[29], hin[28], hin[27], hin[26], hin[25], hin[24],
152         })
153         out[31] = s[0]
154         out[30] = s[1]
155         out[29] = s[2]
156         out[28] = s[3]
157         out[27] = s[4]
158         out[26] = s[5]
159         out[25] = s[6]
160         out[24] = s[7]
161
162         blockXor(u, fA(u), &c2)
163         v = fA(fA(v))
164         blockXor(k, u, v)
165         k = fP(k)
166         blockReverse(k[:], k[:])
167         c = gost28147.NewCipher(*k, h.sbox)
168         c.Encrypt(s, []byte{
169                 hin[23], hin[22], hin[21], hin[20], hin[19], hin[18], hin[17], hin[16],
170         })
171         out[23] = s[0]
172         out[22] = s[1]
173         out[21] = s[2]
174         out[20] = s[3]
175         out[19] = s[4]
176         out[18] = s[5]
177         out[17] = s[6]
178         out[16] = s[7]
179
180         blockXor(u, fA(u), &c3)
181         v = fA(fA(v))
182         blockXor(k, u, v)
183         k = fP(k)
184         blockReverse(k[:], k[:])
185         c = gost28147.NewCipher(*k, h.sbox)
186         c.Encrypt(s, []byte{
187                 hin[15], hin[14], hin[13], hin[12], hin[11], hin[10], hin[9], hin[8],
188         })
189         out[15] = s[0]
190         out[14] = s[1]
191         out[13] = s[2]
192         out[12] = s[3]
193         out[11] = s[4]
194         out[10] = s[5]
195         out[9] = s[6]
196         out[8] = s[7]
197
198         blockXor(u, fA(u), &c4)
199         v = fA(fA(v))
200         blockXor(k, u, v)
201         k = fP(k)
202         blockReverse(k[:], k[:])
203         c = gost28147.NewCipher(*k, h.sbox)
204         c.Encrypt(s, []byte{
205                 hin[7], hin[6], hin[5], hin[4], hin[3], hin[2], hin[1], hin[0],
206         })
207         out[7] = s[0]
208         out[6] = s[1]
209         out[5] = s[2]
210         out[4] = s[3]
211         out[3] = s[4]
212         out[2] = s[5]
213         out[1] = s[6]
214         out[0] = s[7]
215
216         for i := 0; i < 12; i++ {
217                 out = fChi(out)
218         }
219         blockXor(out, out, &m)
220         out = fChi(out)
221         blockXor(out, out, &hin)
222         for i := 0; i < 61; i++ {
223                 out = fChi(out)
224         }
225         return *out
226 }
227
228 func (h *Hash) chkAdd(data []byte) *big.Int {
229         i := big.NewInt(0).SetBytes(data)
230         i.Add(i, h.chk)
231         if i.Cmp(big256) != -1 {
232                 i.Sub(i, big256)
233         }
234         return i
235 }
236
237 func (h *Hash) Write(data []byte) (int, error) {
238         h.buf = append(h.buf, data...)
239         for len(h.buf) >= BlockSize {
240                 h.size += BlockSize * 8
241                 blockReverse(h.tmp[:], h.buf[:BlockSize])
242                 h.chk = h.chkAdd(h.tmp[:])
243                 h.buf = h.buf[BlockSize:]
244                 h.hsh = h.step(h.hsh, h.tmp)
245         }
246         return len(data), nil
247 }
248
249 func (h *Hash) Sum(in []byte) []byte {
250         size := h.size
251         chk := h.chk
252         hsh := h.hsh
253         block := new([BlockSize]byte)
254         if len(h.buf) != 0 {
255                 size += uint64(len(h.buf)) * 8
256                 copy(block[:], h.buf)
257                 blockReverse(block[:], block[:])
258                 chk = h.chkAdd(block[:])
259                 hsh = h.step(hsh, *block)
260                 block = new([BlockSize]byte)
261         }
262         binary.BigEndian.PutUint64(block[24:], size)
263         hsh = h.step(hsh, *block)
264         block = new([BlockSize]byte)
265         chkBytes := chk.Bytes()
266         copy(block[BlockSize-len(chkBytes):], chkBytes)
267         hsh = h.step(hsh, *block)
268         return append(in, hsh[:]...)
269 }