Debian Codegraph @

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