-
Notifications
You must be signed in to change notification settings - Fork 0
/
ripemd128.go
215 lines (177 loc) · 4.88 KB
/
ripemd128.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
package ripemd
import (
"hash"
"math/bits"
)
const (
// The size of the checksum in bytes.
Size128 = 16
// The block size of the hash algorithm in bytes.
BlockSize128 = 64
)
// Sum128 returns checksum of the data.
func Sum128(data []byte) (sum [Size128]byte) {
var h digest128
h.Reset()
h.Write(data)
hash := h.Sum(nil)
copy(sum[:], hash)
return
}
type digest128 struct {
s [4]uint32 // running context
x [BlockSize128]byte // temporary buffer
nx int // index into buffer
len uint64 // total count of bytes processed
}
func New128() hash.Hash {
d := new(digest128)
d.Reset()
return d
}
func (d *digest128) Reset() {
d.s[0], d.s[1], d.s[2], d.s[3] = s0, s1, s2, s3
d.nx = 0
d.len = 0
}
func (d *digest128) Size() int {
return Size128
}
func (d *digest128) BlockSize() int {
return BlockSize128
}
func (d *digest128) Write(p []byte) (nn int, err error) {
nn = len(p)
d.len += uint64(nn)
if d.nx > 0 {
n := len(p)
if n > BlockSize128-d.nx {
n = BlockSize128 - d.nx
}
for i := 0; i < n; i++ {
d.x[d.nx+i] = p[i]
}
d.nx += n
if d.nx == BlockSize128 {
d.block(d.x[0:])
d.nx = 0
}
p = p[n:]
}
n := d.block(p)
p = p[n:]
if len(p) > 0 {
d.nx = copy(d.x[:], p)
}
return
}
func (d *digest128) 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 *digest128) checkSum() [Size128]byte {
// Padding. Add a 1 bit and 0 bits until 56 bytes mod 64.
tc := d.len
var tmp [64]byte
tmp[0] = 0x80
if tc%64 < 56 {
d.Write(tmp[0 : 56-tc%64])
} else {
d.Write(tmp[0 : 64+56-tc%64])
}
// Length in bits.
tc <<= 3
for i := uint(0); i < 8; i++ {
tmp[i] = byte(tc >> (8 * i))
}
d.Write(tmp[0:8])
if d.nx != 0 {
panic("d.nx != 0")
}
var digest [Size128]byte
for i, s := range d.s {
digest[i*4] = byte(s)
digest[i*4+1] = byte(s >> 8)
digest[i*4+2] = byte(s >> 16)
digest[i*4+3] = byte(s >> 24)
}
return digest
}
func (md *digest128) block(p []byte) int {
n := 0
var x [16]uint32
var alpha uint32
for len(p) >= BlockSize128 {
a, b, c, d := md.s[0], md.s[1], md.s[2], md.s[3]
aa, bb, cc, dd := a, b, c, d
j := 0
for i := 0; i < 16; i++ {
x[i] = uint32(p[j]) | uint32(p[j+1])<<8 | uint32(p[j+2])<<16 | uint32(p[j+3])<<24
j += 4
}
// round 1
i := 0
for i < 16 {
alpha = a + (b ^ c ^ d) + x[sbox128n0[i]]
s := int(sbox128r0[i])
alpha = bits.RotateLeft32(alpha, s)
a, b, c, d = d, alpha, b, c
// parallel line
alpha = aa + (cc ^ (dd & (bb ^ cc))) + x[sbox128n1[i]] + 0x50a28be6
s = int(sbox128r1[i])
alpha = bits.RotateLeft32(alpha, s)
aa, bb, cc, dd = dd, alpha, bb, cc
i++
}
// round 2
for i < 32 {
alpha = a + (d ^ (b & (c ^ d))) + x[sbox128n0[i]] + 0x5a827999
s := int(sbox128r0[i])
alpha = bits.RotateLeft32(alpha, s)
a, b, c, d = d, alpha, b, c
// parallel line
alpha = aa + (dd ^ (bb | ^cc)) + x[sbox128n1[i]] + 0x5c4dd124
s = int(sbox128r1[i])
alpha = bits.RotateLeft32(alpha, s)
aa, bb, cc, dd = dd, alpha, bb, cc
i++
}
// round 3
for i < 48 {
alpha = a + (d ^ (b | ^c)) + x[sbox128n0[i]] + 0x6ed9eba1
s := int(sbox128r0[i])
alpha = bits.RotateLeft32(alpha, s)
a, b, c, d = d, alpha, b, c
// parallel line
alpha = aa + (dd ^ (bb & (cc ^ dd))) + x[sbox128n1[i]] + 0x6d703ef3
s = int(sbox128r1[i])
alpha = bits.RotateLeft32(alpha, s)
aa, bb, cc, dd = dd, alpha, bb, cc
i++
}
// round 4
for i < 64 {
alpha = a + (c ^ (d & (b ^ c))) + x[sbox128n0[i]] + 0x8f1bbcdc
s := int(sbox128r0[i])
alpha = bits.RotateLeft32(alpha, s)
a, b, c, d = d, alpha, b, c
// parallel line
alpha = aa + (bb ^ cc ^ dd) + x[sbox128n1[i]]
s = int(sbox128r1[i])
alpha = bits.RotateLeft32(alpha, s)
aa, bb, cc, dd = dd, alpha, bb, cc
i++
}
// combine results
c = md.s[1] + c + dd
md.s[1] = md.s[2] + d + aa
md.s[2] = md.s[3] + a + bb
md.s[3] = md.s[0] + b + cc
md.s[0] = c
p = p[BlockSize128:]
n += BlockSize128
}
return n
}