1 // GoGOST -- Pure Go GOST cryptographic functions library
2 // Copyright (C) 2015-2020 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
16 // GOST R 34.11-94 hash function.
24 "go.cypherpunks.ru/gogost/v4/gost28147"
33 SboxDefault *gost28147.Sbox = &gost28147.SboxIdGostR341194TestParamSet
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,
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,
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,
54 big256 *big.Int = big.NewInt(0).SetBit(big.NewInt(0), 256, 1)
66 func New(sbox *gost28147.Sbox) *Hash {
72 func (h *Hash) Reset() {
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,
84 func (h *Hash) BlockSize() int {
88 func (h *Hash) Size() int {
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])
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],
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])
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]
130 func blockXor(dst, a, b *[BlockSize]byte) {
131 for i := 0; i < BlockSize; i++ {
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)
146 blockReverse(k[:], k[:])
147 c := gost28147.NewCipher(k[:], h.sbox)
148 s := make([]byte, gost28147.BlockSize)
150 hin[31], hin[30], hin[29], hin[28], hin[27], hin[26], hin[25], hin[24],
161 blockXor(u, fA(u), &c2)
165 blockReverse(k[:], k[:])
166 c = gost28147.NewCipher(k[:], h.sbox)
168 hin[23], hin[22], hin[21], hin[20], hin[19], hin[18], hin[17], hin[16],
179 blockXor(u, fA(u), &c3)
183 blockReverse(k[:], k[:])
184 c = gost28147.NewCipher(k[:], h.sbox)
186 hin[15], hin[14], hin[13], hin[12], hin[11], hin[10], hin[9], hin[8],
197 blockXor(u, fA(u), &c4)
201 blockReverse(k[:], k[:])
202 c = gost28147.NewCipher(k[:], h.sbox)
204 hin[7], hin[6], hin[5], hin[4], hin[3], hin[2], hin[1], hin[0],
215 for i := 0; i < 12; i++ {
218 blockXor(out, out, &m)
220 blockXor(out, out, &hin)
221 for i := 0; i < 61; i++ {
227 func (h *Hash) chkAdd(data []byte) *big.Int {
228 i := big.NewInt(0).SetBytes(data)
230 if i.Cmp(big256) != -1 {
236 func (h *Hash) Write(data []byte) (int, error) {
237 h.buf = append(h.buf, data...)
238 for len(h.buf) >= BlockSize {
239 h.size += BlockSize * 8
240 blockReverse(h.tmp[:], h.buf[:BlockSize])
241 h.chk = h.chkAdd(h.tmp[:])
242 h.buf = h.buf[BlockSize:]
243 h.hsh = h.step(h.hsh, h.tmp)
245 return len(data), nil
248 func (h *Hash) Sum(in []byte) []byte {
252 block := new([BlockSize]byte)
254 size += uint64(len(h.buf)) * 8
255 copy(block[:], h.buf)
256 blockReverse(block[:], block[:])
257 chk = h.chkAdd(block[:])
258 hsh = h.step(hsh, *block)
259 block = new([BlockSize]byte)
261 binary.BigEndian.PutUint64(block[24:], size)
262 hsh = h.step(hsh, *block)
263 block = new([BlockSize]byte)
264 chkBytes := chk.Bytes()
265 copy(block[BlockSize-len(chkBytes):], chkBytes)
266 hsh = h.step(hsh, *block)
267 blockReverse(hsh[:], hsh[:])
268 return append(in, hsh[:]...)