Debian Codegraph @

skein/skein1024/skein1024.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 skein1024
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 [17]uint64
14 tweak [3]uint64
15 block [threefish.BlockSize1024]byte
16 off int
17 hasMsg bool
18 }
19
20 func (s *hashFunc) BlockSize() int { return threefish.BlockSize1024 }
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 [16]uint64
42
43 dif := threefish.BlockSize1024 - 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.BlockSize1024 && 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.BlockSize1024 {
55 nn := length & (^(threefish.BlockSize1024 - 1)) // length -= (length % BlockSize)
56 if length == nn {
57 nn -= threefish.BlockSize1024
58 }
59 for i := 0; i < len(p[:nn]); i += threefish.BlockSize1024 {
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.BlockSize1024]byte
80 var ctr uint64
81 for i := s0.hashsize; i > 0; i -= threefish.BlockSize1024 {
82 s0.output(&out, ctr)
83 ctr++
84 b = append(b, out[:]...)
85 }
86
87 return b[:s0.hashsize]
88 }
89
90 func (s *hashFunc) update(block *[16]uint64) {
91 threefish.IncrementTweak(&(s.tweak), threefish.BlockSize1024)
92
93 threefish.UBI1024(block, &(s.hVal), &(s.tweak))
94
95 s.tweak[1] &^= skein.FirstBlock
96 }
97
98 func (s *hashFunc) output(dst *[threefish.BlockSize1024]byte, counter uint64) {
99 var block [16]uint64
100 block[0] = counter
101
102 hVal := s.hVal
103 var outTweak = [3]uint64{8, skein.CfgOutput<<56 | skein.FirstBlock | skein.FinalBlock, 0}
104
105 threefish.UBI1024(&block, &hVal, &outTweak)
106 block[0] ^= counter
107
108 blockToBytes(dst[:], &block)
109 }
110
111 func (s *hashFunc) initialize(hashsize int, conf *skein.Config) {
112 if hashsize < 1 {
113 panic("skein1024: invalid hashsize for Skein-1024")
114 }
115
116 s.hashsize = hashsize
117
118 var key, pubKey, keyID, nonce, personal []byte
119 if conf != nil {
120 key = conf.Key
121 pubKey = conf.PublicKey
122 keyID = conf.KeyID
123 nonce = conf.Nonce
124 personal = conf.Personal
125 }
126
127 if len(key) > 0 {
128 s.tweak[0] = 0
129 s.tweak[1] = skein.CfgKey<<56 | skein.FirstBlock
130 s.Write(key)
131 s.finalizeHash()
132 }
133
134 var cfg [32]byte
135 schemaId := skein.SchemaID
136 cfg[0] = byte(schemaId)
137 cfg[1] = byte(schemaId >> 8)
138 cfg[2] = byte(schemaId >> 16)
139 cfg[3] = byte(schemaId >> 24)
140 cfg[4] = byte(schemaId >> 32)
141 cfg[5] = byte(schemaId >> 40)
142 cfg[6] = byte(schemaId >> 48)
143 cfg[7] = byte(schemaId >> 56)
144
145 bits := uint64(s.hashsize * 8)
146 cfg[8] = byte(bits)
147 cfg[9] = byte(bits >> 8)
148 cfg[10] = byte(bits >> 16)
149 cfg[11] = byte(bits >> 24)
150 cfg[12] = byte(bits >> 32)
151 cfg[13] = byte(bits >> 40)
152 cfg[14] = byte(bits >> 48)
153 cfg[15] = byte(bits >> 56)
154
155 s.tweak[0] = 0
156 s.tweak[1] = skein.CfgConfig<<56 | skein.FirstBlock
157 s.Write(cfg[:])
158 s.finalizeHash()
159
160 if len(personal) > 0 {
161 s.tweak[0] = 0
162 s.tweak[1] = skein.CfgPersonal<<56 | skein.FirstBlock
163 s.Write(personal)
164 s.finalizeHash()
165 }
166
167 if len(pubKey) > 0 {
168 s.tweak[0] = 0
169 s.tweak[1] = skein.CfgPublicKey<<56 | skein.FirstBlock
170 s.Write(pubKey)
171 s.finalizeHash()
172 }
173
174 if len(keyID) > 0 {
175 s.tweak[0] = 0
176 s.tweak[1] = skein.CfgKeyID<<56 | skein.FirstBlock
177 s.Write(keyID)
178 s.finalizeHash()
179 }
180
181 if len(nonce) > 0 {
182 s.tweak[0] = 0
183 s.tweak[1] = skein.CfgNonce<<56 | skein.FirstBlock
184 s.Write(nonce)
185 s.finalizeHash()
186 }
187
188 s.hValCpy = s.hVal
189
190 s.Reset()
191 }
192
193 func (s *hashFunc) finalizeHash() {
194 threefish.IncrementTweak(&(s.tweak), uint64(s.off))
195 s.tweak[1] |= skein.FinalBlock // set the last block flag
196
197 for i := s.off; i < len(s.block); i++ {
198 s.block[i] = 0
199 }
200 s.off = 0
201
202 var block [16]uint64
203 bytesToBlock(&block, s.block[:])
204
205 threefish.UBI1024(&block, &(s.hVal), &(s.tweak))
206 }