Skip to content

Commit 155efbb

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

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/crypto/sha3/sha3.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ func sumSHAKE256(out, data []byte, length int) []byte {
9797
}
9898

9999
// SHA3 is an instance of a SHA-3 hash. It implements [hash.Hash].
100+
// The zero value is a usable SHA3-256 hash.
100101
type SHA3 struct {
101102
s sha3.Digest
102103
}
@@ -126,43 +127,57 @@ func New512() *SHA3 {
126127
return &SHA3{*sha3.New512()}
127128
}
128129

130+
func (s *SHA3) init() {
131+
if s.s.Size() == 0 {
132+
*s = *New256()
133+
}
134+
}
135+
129136
// Write absorbs more data into the hash's state.
130137
func (s *SHA3) Write(p []byte) (n int, err error) {
138+
s.init()
131139
return s.s.Write(p)
132140
}
133141

134142
// Sum appends the current hash to b and returns the resulting slice.
135143
func (s *SHA3) Sum(b []byte) []byte {
144+
s.init()
136145
return s.s.Sum(b)
137146
}
138147

139148
// Reset resets the hash to its initial state.
140149
func (s *SHA3) Reset() {
150+
s.init()
141151
s.s.Reset()
142152
}
143153

144154
// Size returns the number of bytes Sum will produce.
145155
func (s *SHA3) Size() int {
156+
s.init()
146157
return s.s.Size()
147158
}
148159

149160
// BlockSize returns the hash's rate.
150161
func (s *SHA3) BlockSize() int {
162+
s.init()
151163
return s.s.BlockSize()
152164
}
153165

154166
// MarshalBinary implements [encoding.BinaryMarshaler].
155167
func (s *SHA3) MarshalBinary() ([]byte, error) {
168+
s.init()
156169
return s.s.MarshalBinary()
157170
}
158171

159172
// AppendBinary implements [encoding.BinaryAppender].
160173
func (s *SHA3) AppendBinary(p []byte) ([]byte, error) {
174+
s.init()
161175
return s.s.AppendBinary(p)
162176
}
163177

164178
// UnmarshalBinary implements [encoding.BinaryUnmarshaler].
165179
func (s *SHA3) UnmarshalBinary(data []byte) error {
180+
s.init()
166181
return s.s.UnmarshalBinary(data)
167182
}
168183

src/crypto/sha3/sha3_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ const testString = "brekeccakkeccak koax koax"
2222
// with output-length equal to the KAT length for SHA-3, Keccak
2323
// and SHAKE instances.
2424
var testDigests = map[string]func() *SHA3{
25-
"SHA3-224": New224,
26-
"SHA3-256": New256,
27-
"SHA3-384": New384,
28-
"SHA3-512": New512,
25+
"SHA3-224": New224,
26+
"SHA3-256": New256,
27+
"SHA3-384": New384,
28+
"SHA3-512": New512,
29+
"SHA3-Zero": func() *SHA3 { return &SHA3{} },
2930
}
3031

3132
// testShakes contains functions that return *sha3.SHAKE instances for

0 commit comments

Comments
 (0)