-
Notifications
You must be signed in to change notification settings - Fork 0
/
digest.go
229 lines (185 loc) · 6.77 KB
/
digest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package jh
import (
"unsafe"
)
// The size of an JH checksum in bytes.
const Size = 32
// The blocksize of JH in bytes.
const BlockSize = 64
// For memset
var zeroBuf64Byte [64]byte
type digest struct {
s [64]byte
x [8][2]uint64
nx uint64
len uint64
}
func newDigest() *digest {
d := new(digest)
d.Reset()
return d
}
func (d *digest) Reset() {
d.len = 0
d.nx = 0
d.x = jh256H0
}
func (d *digest) Size() int {
return Size
}
func (d *digest) BlockSize() int {
return BlockSize
}
// hash each 512-bit message block, except the last partial block
func (d *digest) Write(data []byte) (n int, err error) {
var index uint64 = 0
plen := uint64(len(data)) * 8
d.len += plen
if d.nx > 0 && d.nx+plen < 512 {
if plen&7 == 0 {
copy(d.s[d.nx>>3:], data[:64-(d.nx>>3)])
} else {
copy(d.s[d.nx>>3:], data[:64-(d.nx>>3)+1])
}
d.nx += plen
plen = 0
}
if d.nx > 0 && d.nx + plen >= 512 {
copy(d.s[d.nx>>3:], data[:64-(d.nx>>3)])
index = 64 - (d.nx >> 3)
plen -= 512 - d.nx
d.f8()
d.nx = 0
}
for plen >= 512 {
copy(d.s[:], data[index:index+64])
d.f8()
index += 64
plen -= 512
}
if plen > 0 {
if plen&7 == 0 {
copy(d.s[:((plen&0x1ff)>>3)], data[index:])
} else {
copy(d.s[:((plen&0x1ff)>>3)+1], data[index:])
}
d.nx = plen
}
return len(data), nil
}
// Sum pads the message, process the padded block(s), truncate the hash value H to obtain the message digest
func (d *digest) Sum(in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing.
d0 := *d
hash := d0.checkSum()
return append(in, hash[:]...)
}
func (d *digest) checkSum() *[Size]byte {
var i uint64
if d.len&0x1ff == 0 {
d.s = zeroBuf64Byte
d.s[0] = 0x80
PUTU64BE(d.s[56:], d.len)
d.f8()
} else {
// set the rest of the bytes in the buffer to 0
if d.nx&7 == 0 {
for i = (d.len & 0x1ff) >> 3; i < 64; i++ {
d.s[i] = 0
}
} else {
for i = ((d.len & 0x1ff) >> 3) + 1; i < 64; i++ {
d.s[i] = 0
}
}
// pad and process the partial block when plen is not multiple of 512 bits, then hash the padded blocks
d.s[(d.len&0x1ff)>>3] |= 1 << (7 - (d.len & 7))
d.f8()
d.s = zeroBuf64Byte
PUTU64BE(d.s[56:], d.len)
d.f8()
}
return (*[32]byte)(unsafe.Pointer(&d.x[6][0]))
}
// The compression function F8.
func (d *digest) f8() {
var i uint64
for i = 0; i < 8; i++ {
d.x[i>>1][i&1] ^= GETU64(d.s[8*i:])
}
d.e8()
for i = 0; i < 8; i++ {
d.x[(8+i)>>1][(8+i)&1] ^= GETU64(d.s[8*i:])
}
}
// The bijective function E8, in bitslice form.
func (d *digest) e8() {
var i, roundnumber, temp0 uint64
for roundnumber = 0; roundnumber < 42; roundnumber += 7 {
// round 7*roundnumber+0: Sbox, MDS and Swapping layers
for i = 0; i < 2; i++ {
SS(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i], e8BitsliceRoundconstant[roundnumber+0][i], e8BitsliceRoundconstant[roundnumber+0][i+2])
L(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i])
SWAP1(&d.x[1][i])
SWAP1(&d.x[3][i])
SWAP1(&d.x[5][i])
SWAP1(&d.x[7][i])
}
// round 7*roundnumber+1: Sbox, MDS and Swapping layers
for i = 0; i < 2; i++ {
SS(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i], e8BitsliceRoundconstant[roundnumber+1][i], e8BitsliceRoundconstant[roundnumber+1][i+2])
L(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i])
SWAP2(&d.x[1][i])
SWAP2(&d.x[3][i])
SWAP2(&d.x[5][i])
SWAP2(&d.x[7][i])
}
// round 7*roundnumber+2: Sbox, MDS and Swapping layers
for i = 0; i < 2; i++ {
SS(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i], e8BitsliceRoundconstant[roundnumber+2][i], e8BitsliceRoundconstant[roundnumber+2][i+2])
L(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i])
SWAP4(&d.x[1][i])
SWAP4(&d.x[3][i])
SWAP4(&d.x[5][i])
SWAP4(&d.x[7][i])
}
// round 7*roundnumber+3: Sbox, MDS and Swapping layers
for i = 0; i < 2; i++ {
SS(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i], e8BitsliceRoundconstant[roundnumber+3][i], e8BitsliceRoundconstant[roundnumber+3][i+2])
L(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i])
SWAP8(&d.x[1][i])
SWAP8(&d.x[3][i])
SWAP8(&d.x[5][i])
SWAP8(&d.x[7][i])
}
// round 7*roundnumber+4: Sbox, MDS and Swapping layers
for i = 0; i < 2; i++ {
SS(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i], e8BitsliceRoundconstant[roundnumber+4][i], e8BitsliceRoundconstant[roundnumber+4][i+2])
L(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i])
SWAP16(&d.x[1][i])
SWAP16(&d.x[3][i])
SWAP16(&d.x[5][i])
SWAP16(&d.x[7][i])
}
// round 7*roundnumber+5: Sbox, MDS and Swapping layers
for i = 0; i < 2; i++ {
SS(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i], e8BitsliceRoundconstant[roundnumber+5][i], e8BitsliceRoundconstant[roundnumber+5][i+2])
L(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i])
SWAP32(&d.x[1][i])
SWAP32(&d.x[3][i])
SWAP32(&d.x[5][i])
SWAP32(&d.x[7][i])
}
// round 7*roundnumber+6: Sbox and MDS layers
for i = 0; i < 2; i++ {
SS(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i], e8BitsliceRoundconstant[roundnumber+6][i], e8BitsliceRoundconstant[roundnumber+6][i+2])
L(&d.x[0][i], &d.x[2][i], &d.x[4][i], &d.x[6][i], &d.x[1][i], &d.x[3][i], &d.x[5][i], &d.x[7][i])
}
// round 7*roundnumber+6: swapping layer
for i = 1; i < 8; i = i + 2 {
temp0 = d.x[i][0]
d.x[i][0] = d.x[i][1]
d.x[i][1] = temp0
}
}
}