1
2
3
4
5
6
7 package sha3
8
9 import (
10 "crypto"
11 "crypto/internal/fips140/sha3"
12 "hash"
13 _ "unsafe"
14 )
15
16 func init() {
17 crypto.RegisterHash(crypto.SHA3_224, func() hash.Hash { return New224() })
18 crypto.RegisterHash(crypto.SHA3_256, func() hash.Hash { return New256() })
19 crypto.RegisterHash(crypto.SHA3_384, func() hash.Hash { return New384() })
20 crypto.RegisterHash(crypto.SHA3_512, func() hash.Hash { return New512() })
21 }
22
23
24 func Sum224(data []byte) [28]byte {
25 var out [28]byte
26 h := sha3.New224()
27 h.Write(data)
28 h.Sum(out[:0])
29 return out
30 }
31
32
33 func Sum256(data []byte) [32]byte {
34 var out [32]byte
35 h := sha3.New256()
36 h.Write(data)
37 h.Sum(out[:0])
38 return out
39 }
40
41
42 func Sum384(data []byte) [48]byte {
43 var out [48]byte
44 h := sha3.New384()
45 h.Write(data)
46 h.Sum(out[:0])
47 return out
48 }
49
50
51 func Sum512(data []byte) [64]byte {
52 var out [64]byte
53 h := sha3.New512()
54 h.Write(data)
55 h.Sum(out[:0])
56 return out
57 }
58
59
60
61 func SumSHAKE128(data []byte, length int) []byte {
62
63 out := make([]byte, 32)
64 return sumSHAKE128(out, data, length)
65 }
66
67 func sumSHAKE128(out, data []byte, length int) []byte {
68 if len(out) < length {
69 out = make([]byte, length)
70 } else {
71 out = out[:length]
72 }
73 h := sha3.NewShake128()
74 h.Write(data)
75 h.Read(out)
76 return out
77 }
78
79
80
81 func SumSHAKE256(data []byte, length int) []byte {
82
83 out := make([]byte, 64)
84 return sumSHAKE256(out, data, length)
85 }
86
87 func sumSHAKE256(out, data []byte, length int) []byte {
88 if len(out) < length {
89 out = make([]byte, length)
90 } else {
91 out = out[:length]
92 }
93 h := sha3.NewShake256()
94 h.Write(data)
95 h.Read(out)
96 return out
97 }
98
99
100
101 type SHA3 struct {
102 s sha3.Digest
103 }
104
105
106 func fips140hash_sha3Unwrap(sha3 *SHA3) *sha3.Digest {
107 return &sha3.s
108 }
109
110
111 func New224() *SHA3 {
112 return &SHA3{*sha3.New224()}
113 }
114
115
116 func New256() *SHA3 {
117 return &SHA3{*sha3.New256()}
118 }
119
120
121 func New384() *SHA3 {
122 return &SHA3{*sha3.New384()}
123 }
124
125
126 func New512() *SHA3 {
127 return &SHA3{*sha3.New512()}
128 }
129
130 func (s *SHA3) init() {
131 if s.s.Size() == 0 {
132 *s = *New256()
133 }
134 }
135
136
137 func (s *SHA3) Write(p []byte) (n int, err error) {
138 s.init()
139 return s.s.Write(p)
140 }
141
142
143 func (s *SHA3) Sum(b []byte) []byte {
144 s.init()
145 return s.s.Sum(b)
146 }
147
148
149 func (s *SHA3) Reset() {
150 s.init()
151 s.s.Reset()
152 }
153
154
155 func (s *SHA3) Size() int {
156 s.init()
157 return s.s.Size()
158 }
159
160
161 func (s *SHA3) BlockSize() int {
162 s.init()
163 return s.s.BlockSize()
164 }
165
166
167 func (s *SHA3) MarshalBinary() ([]byte, error) {
168 s.init()
169 return s.s.MarshalBinary()
170 }
171
172
173 func (s *SHA3) AppendBinary(p []byte) ([]byte, error) {
174 s.init()
175 return s.s.AppendBinary(p)
176 }
177
178
179 func (s *SHA3) UnmarshalBinary(data []byte) error {
180 s.init()
181 return s.s.UnmarshalBinary(data)
182 }
183
184
185 func (d *SHA3) Clone() (hash.Cloner, error) {
186 r := *d
187 return &r, nil
188 }
189
190
191
192 type SHAKE struct {
193 s sha3.SHAKE
194 }
195
196 func (s *SHAKE) init() {
197 if s.s.Size() == 0 {
198 *s = *NewSHAKE256()
199 }
200 }
201
202
203 func NewSHAKE128() *SHAKE {
204 return &SHAKE{*sha3.NewShake128()}
205 }
206
207
208 func NewSHAKE256() *SHAKE {
209 return &SHAKE{*sha3.NewShake256()}
210 }
211
212
213
214
215
216
217 func NewCSHAKE128(N, S []byte) *SHAKE {
218 return &SHAKE{*sha3.NewCShake128(N, S)}
219 }
220
221
222
223
224
225
226 func NewCSHAKE256(N, S []byte) *SHAKE {
227 return &SHAKE{*sha3.NewCShake256(N, S)}
228 }
229
230
231
232
233 func (s *SHAKE) Write(p []byte) (n int, err error) {
234 s.init()
235 return s.s.Write(p)
236 }
237
238
239
240
241 func (s *SHAKE) Read(p []byte) (n int, err error) {
242 s.init()
243 return s.s.Read(p)
244 }
245
246
247 func (s *SHAKE) Reset() {
248 s.init()
249 s.s.Reset()
250 }
251
252
253 func (s *SHAKE) BlockSize() int {
254 s.init()
255 return s.s.BlockSize()
256 }
257
258
259 func (s *SHAKE) MarshalBinary() ([]byte, error) {
260 s.init()
261 return s.s.MarshalBinary()
262 }
263
264
265 func (s *SHAKE) AppendBinary(p []byte) ([]byte, error) {
266 s.init()
267 return s.s.AppendBinary(p)
268 }
269
270
271 func (s *SHAKE) UnmarshalBinary(data []byte) error {
272 s.init()
273 return s.s.UnmarshalBinary(data)
274 }
275
View as plain text