Skip to content

Commit 9570036

Browse files
qiulaidongfengseankhliao
authored andcommitted
crypto/sha3: make the zero value of SHAKE useable
For #75154 Change-Id: Iee5a11ebea8c74b9abab4c077dc7990fe8f562dd Reviewed-on: https://go-review.googlesource.com/c/go/+/718521 Reviewed-by: Sean Liao <[email protected]> Reviewed-by: Mark Freeman <[email protected]> Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 155efbb commit 9570036

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/crypto/sha3/sha3.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,17 @@ func (d *SHA3) Clone() (hash.Cloner, error) {
188188
}
189189

190190
// SHAKE is an instance of a SHAKE extendable output function.
191+
// The zero value is a usable SHAKE256 hash.
191192
type SHAKE struct {
192193
s sha3.SHAKE
193194
}
194195

196+
func (s *SHAKE) init() {
197+
if s.s.Size() == 0 {
198+
*s = *NewSHAKE256()
199+
}
200+
}
201+
195202
// NewSHAKE128 creates a new SHAKE128 XOF.
196203
func NewSHAKE128() *SHAKE {
197204
return &SHAKE{*sha3.NewShake128()}
@@ -224,37 +231,44 @@ func NewCSHAKE256(N, S []byte) *SHAKE {
224231
//
225232
// It panics if any output has already been read.
226233
func (s *SHAKE) Write(p []byte) (n int, err error) {
234+
s.init()
227235
return s.s.Write(p)
228236
}
229237

230238
// Read squeezes more output from the XOF.
231239
//
232240
// Any call to Write after a call to Read will panic.
233241
func (s *SHAKE) Read(p []byte) (n int, err error) {
242+
s.init()
234243
return s.s.Read(p)
235244
}
236245

237246
// Reset resets the XOF to its initial state.
238247
func (s *SHAKE) Reset() {
248+
s.init()
239249
s.s.Reset()
240250
}
241251

242252
// BlockSize returns the rate of the XOF.
243253
func (s *SHAKE) BlockSize() int {
254+
s.init()
244255
return s.s.BlockSize()
245256
}
246257

247258
// MarshalBinary implements [encoding.BinaryMarshaler].
248259
func (s *SHAKE) MarshalBinary() ([]byte, error) {
260+
s.init()
249261
return s.s.MarshalBinary()
250262
}
251263

252264
// AppendBinary implements [encoding.BinaryAppender].
253265
func (s *SHAKE) AppendBinary(p []byte) ([]byte, error) {
266+
s.init()
254267
return s.s.AppendBinary(p)
255268
}
256269

257270
// UnmarshalBinary implements [encoding.BinaryUnmarshaler].
258271
func (s *SHAKE) UnmarshalBinary(data []byte) error {
272+
s.init()
259273
return s.s.UnmarshalBinary(data)
260274
}

src/crypto/sha3/sha3_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ var testShakes = map[string]struct {
3737
defCustomStr string
3838
}{
3939
// NewCSHAKE without customization produces same result as SHAKE
40-
"SHAKE128": {NewCSHAKE128, "", ""},
41-
"SHAKE256": {NewCSHAKE256, "", ""},
42-
"cSHAKE128": {NewCSHAKE128, "CSHAKE128", "CustomString"},
43-
"cSHAKE256": {NewCSHAKE256, "CSHAKE256", "CustomString"},
40+
"SHAKE128": {NewCSHAKE128, "", ""},
41+
"SHAKE256": {NewCSHAKE256, "", ""},
42+
"cSHAKE128": {NewCSHAKE128, "CSHAKE128", "CustomString"},
43+
"cSHAKE256": {NewCSHAKE256, "CSHAKE256", "CustomString"},
44+
"SHAKE-Zero": {func(N []byte, S []byte) *SHAKE { return &SHAKE{} }, "", ""},
4445
}
4546

4647
func TestSHA3Hash(t *testing.T) {

0 commit comments

Comments
 (0)