Debian Codegraph @

skein/skein256/skein256.go

raw symbols SCIP
1 // Use of this source code is governed by a license
2 // that can be found in the LICENSE file
3
4 package skein256
5
6 import (
7 "github.com/enceve/crypto/skein"
8 "github.com/enceve/crypto/skein/threefish"
9 )
10
11 type hashFunc struct {
12 hashsize int
13 hVal, hValCpy [5]uint64
14 tweak [3]uint64
15 block [threefish.BlockSize256]byte
16 off int
17 hasMsg bool
18 }
19
20 func (s *hashFunc) BlockSize() int { return threefish.BlockSize256 }
21
22 func (s *hashFunc) Size() int { return s.hashsize }
23
24 func (s *hashFunc) Reset() {
25 for i := range s.block {
26 s.block[i] = 0
27 }
28 s.off = 0
29 s.hasMsg = false
30
31 s.hVal = s.hValCpy
32
33 s.tweak[0] = 0
34 s.tweak[1] = skein.CfgMessage<<56 | skein.FirstBlock
35 }
36
37 func (s *hashFunc) Write(p []byte) (n int, err error) {
38 s.hasMsg = true
39
40 n = len(p)
41 var block [4]uint64
42
43 dif := threefish.BlockSize256 - s.off
44 if s.off > 0 && n > dif {
45 s.off += copy(s.block[s.off:], p[:dif])
46 p = p[dif:]
47 if s.off == threefish.BlockSize256 && len(p) > 0 {
48 bytesToBlock(&block, s.block[:])
49 s.update(&block)
50 s.off = 0
51 }
52 }
53
54 if length := len(p); length > threefish.BlockSize256 {
55 nn := length & (^(threefish.BlockSize256 - 1)) // length -= (length % BlockSize)
56 if length == nn {
57 nn -= threefish.BlockSize256
58 }
59 for i := 0; i < len(p[:nn]); i += threefish.BlockSize256 {
60 bytesToBlock(&block, p[i:])
61 s.update(&block)
62 }
63 p = p[nn:]
64 }
65
66 if len(p) > 0 {
67 s.off += copy(s.block[s.off:], p)
68 }
69 return
70 }
71
72 func (s *hashFunc) Sum(b []byte) []byte {
73 s0 := *s // copy
74
75 if s0.hasMsg {
76 s0.finalizeHash()
77 }
78
79 var out [threefish.BlockSize256]byte
80 var ctr uint64
81
82 for i := s0.hashsize; i > 0; i -= threefish.BlockSize256 {
83 s0.output(&out, ctr)
84 ctr++
85 b = append(b, out[:]...)
86 }
87
88 return b[:s0.hashsize]
89 }
90
91 func (s *hashFunc) update(block *[4]uint64) {
92 threefish.IncrementTweak(&(s.tweak), threefish.BlockSize256)
93
94 threefish.UBI256(block, &(s.hVal), &(s.tweak))
95
96 s.tweak[1] &^= skein.FirstBlock
97 }
98
99 func (s *hashFunc) output(dst *[threefish.BlockSize256]byte, counter uint64) {
100 var block [4]uint64
101 block[0] = counter
102
103 hVal := s.hVal
104 var outTweak = [3]uint64{8, skein.CfgOutput<<56 | skein.FirstBlock | skein.FinalBlock, 0}
105
106 threefish.UBI256(&block, &hVal, &outTweak)
107 block[0] ^= counter
108
109 blockToBytes(dst[:], &block)
110 }
111
112 func (s *hashFunc) initialize(hashsize int, conf *skein.Config) {
113 if hashsize < 1 {
114 panic("skein256: invalid hashsize for Skein-256")
115 }
116
117 s.hashsize = hashsize
118
119 var key, pubKey, keyID, nonce, personal []byte
120 if conf != nil {
121 key = conf.Key
122 pubKey = conf.PublicKey
123 keyID = conf.KeyID
124 nonce = conf.Nonce
125 personal = conf.Personal
126 }
127
128 if len(key) > 0 {
129 s.tweak[0] = 0
130 s.tweak[1] = skein.CfgKey<<56 | skein.FirstBlock
131 s.Write(key)
132 s.finalizeHash()
133 }
134
135 var cfg [32]byte
136 schemaId := skein.SchemaID
137 cfg[0] = byte(schemaId)
138 cfg[1] = byte(schemaId >> 8)
139 cfg[2] = byte(schemaId >> 16)
140 cfg[3] = byte(schemaId >> 24)
141 cfg[4] = byte(schemaId >> 32)
142 cfg[5] = byte(schemaId >> 40)
143 cfg[6] = byte(schemaId >> 48)
144 cfg[7] = byte(schemaId >> 56)
145
146 bits := uint64(s.hashsize * 8)
147 cfg[8] = byte(bits)
148 cfg[9] = byte(bits >> 8)
149 cfg[10] = byte(bits >> 16)
150 cfg[11] = byte(bits >> 24)
151 cfg[12] = byte(bits >> 32)
152 cfg[13] = byte(bits >> 40)
153 cfg[14] = byte(bits >> 48)
154 cfg[15] = byte(bits >> 56)
155
156 s.tweak[0] = 0
157 s.tweak[1] = skein.CfgConfig<<56 | skein.FirstBlock
158 s.Write(cfg[:])
159 s.finalizeHash()
160
161 if len(personal) > 0 {
162 s.tweak[0] = 0
163 s.tweak[1] = skein.CfgPersonal<<56 | skein.FirstBlock
164 s.Write(personal)
165 s.finalizeHash()
166 }
167
168 if len(pubKey) > 0 {
169 s.tweak[0] = 0
170 s.tweak[1] = skein.CfgPublicKey<<56 | skein.FirstBlock
171 s.Write(pubKey)
172 s.finalizeHash()
173 }
174
175 if len(keyID) > 0 {
176 s.tweak[0] = 0
177 s.tweak[1] = skein.CfgKeyID<<56 | skein.FirstBlock
178 s.Write(keyID)
179 s.finalizeHash()
180 }
181
182 if len(nonce) > 0 {
183 s.tweak[0] = 0
184 s.tweak[1] = skein.CfgNonce<<56 | skein.FirstBlock
185 s.Write(nonce)
186 s.finalizeHash()
187 }
188
189 s.hValCpy = s.hVal
190
191 s.Reset()
192 }
193
194 func (s *hashFunc) finalizeHash() {
195 threefish.IncrementTweak(&(s.tweak), uint64(s.off))
196 s.tweak[1] |= skein.FinalBlock // set the last block flag
197
198 for i := s.off; i < len(s.block); i++ {
199 s.block[i] = 0
200 }
201 s.off = 0
202
203 var block [4]uint64
204 bytesToBlock(&block, s.block[:])
205
206 threefish.UBI256(&block, &(s.hVal), &(s.tweak))
207 }