-
Notifications
You must be signed in to change notification settings - Fork 0
/
skein512.go
211 lines (170 loc) · 4.51 KB
/
skein512.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
package skein
import "github.com/deatil/go-hash/skein/threefish"
type hashFunc struct {
hashsize int
hVal, hValCpy [9]uint64
tweak [3]uint64
block [BlockSize]byte
off int
hasMsg bool
}
func (s *hashFunc) Reset() {
for i := range s.block {
s.block[i] = 0
}
s.off = 0
s.hasMsg = false
s.hVal = s.hValCpy
s.tweak[0] = 0
s.tweak[1] = CfgMessage<<56 | FirstBlock
}
func (s *hashFunc) Size() int {
return s.hashsize
}
func (s *hashFunc) BlockSize() int {
return BlockSize
}
func (s *hashFunc) Write(p []byte) (n int, err error) {
s.hasMsg = true
n = len(p)
var block [8]uint64
dif := BlockSize - s.off
if s.off > 0 && n > dif {
s.off += copy(s.block[s.off:], p[:dif])
p = p[dif:]
if s.off == BlockSize && len(p) > 0 {
bytesToBlock(&block, s.block[:])
s.update(&block)
s.off = 0
}
}
if length := len(p); length > BlockSize {
nn := length & (^(BlockSize - 1)) // length -= (length % BlockSize)
if length == nn {
nn -= BlockSize
}
for i := 0; i < len(p[:nn]); i += BlockSize {
bytesToBlock(&block, p[i:])
s.update(&block)
}
p = p[nn:]
}
if len(p) > 0 {
s.off += copy(s.block[s.off:], p)
}
return
}
func (s *hashFunc) Sum(in []byte) []byte {
// Make a copy of d so that caller can keep writing and summing.
d0 := *s
hash := d0.checkSum()
return append(in, hash[:]...)
}
func (s *hashFunc) checkSum() []byte {
if s.hasMsg {
s.finalizeHash()
}
var b []byte
var out [BlockSize]byte
var ctr uint64
for i := s.hashsize; i > 0; i -= BlockSize {
s.output(&out, ctr)
ctr++
b = append(b, out[:]...)
}
return b[:s.hashsize]
}
func (s *hashFunc) update(block *[8]uint64) {
threefish.IncrementTweak(&(s.tweak), BlockSize)
threefish.UBI512(block, &(s.hVal), &(s.tweak))
s.tweak[1] &^= FirstBlock
}
func (s *hashFunc) output(dst *[BlockSize]byte, counter uint64) {
var block [8]uint64
block[0] = counter
hVal := s.hVal
var outTweak = [3]uint64{8, CfgOutput<<56 | FirstBlock | FinalBlock, 0}
threefish.UBI512(&block, &hVal, &outTweak)
block[0] ^= counter
blockToBytes(dst[:], &block)
}
func (s *hashFunc) initialize(hashsize int, conf *Config) {
if hashsize < 1 {
panic("skein: invalid hashsize for Skein-512")
}
s.hashsize = hashsize
var key, pubKey, keyID, nonce, personal []byte
if conf != nil {
key = conf.Key
pubKey = conf.PublicKey
keyID = conf.KeyID
nonce = conf.Nonce
personal = conf.Personal
}
if len(key) > 0 {
s.tweak[0] = 0
s.tweak[1] = CfgKey<<56 | FirstBlock
s.Write(key)
s.finalizeHash()
}
var cfg [32]byte
schemaId := SchemaID
cfg[0] = byte(schemaId)
cfg[1] = byte(schemaId >> 8)
cfg[2] = byte(schemaId >> 16)
cfg[3] = byte(schemaId >> 24)
cfg[4] = byte(schemaId >> 32)
cfg[5] = byte(schemaId >> 40)
cfg[6] = byte(schemaId >> 48)
cfg[7] = byte(schemaId >> 56)
bits := uint64(s.hashsize * 8)
cfg[8] = byte(bits)
cfg[9] = byte(bits >> 8)
cfg[10] = byte(bits >> 16)
cfg[11] = byte(bits >> 24)
cfg[12] = byte(bits >> 32)
cfg[13] = byte(bits >> 40)
cfg[14] = byte(bits >> 48)
cfg[15] = byte(bits >> 56)
s.tweak[0] = 0
s.tweak[1] = CfgConfig<<56 | FirstBlock
s.Write(cfg[:])
s.finalizeHash()
if len(personal) > 0 {
s.tweak[0] = 0
s.tweak[1] = CfgPersonal<<56 | FirstBlock
s.Write(personal)
s.finalizeHash()
}
if len(pubKey) > 0 {
s.tweak[0] = 0
s.tweak[1] = CfgPublicKey<<56 | FirstBlock
s.Write(pubKey)
s.finalizeHash()
}
if len(keyID) > 0 {
s.tweak[0] = 0
s.tweak[1] = CfgKeyID<<56 | FirstBlock
s.Write(keyID)
s.finalizeHash()
}
if len(nonce) > 0 {
s.tweak[0] = 0
s.tweak[1] = CfgNonce<<56 | FirstBlock
s.Write(nonce)
s.finalizeHash()
}
s.hValCpy = s.hVal
s.Reset()
}
func (s *hashFunc) finalizeHash() {
threefish.IncrementTweak(&(s.tweak), uint64(s.off))
s.tweak[1] |= FinalBlock
for i := s.off; i < len(s.block); i++ {
s.block[i] = 0
}
s.off = 0
var block [8]uint64
bytesToBlock(&block, s.block[:])
threefish.UBI512(&block, &(s.hVal), &(s.tweak))
}