Skip to content

Commit 5defcc1

Browse files
committed
sha3: fix Sum results for SHAKE functions on s390x
Sum was taking the digest from the state which is correct for SHA-3 functions but not for SHAKE functions. Updates golang/go#66804 Change-Id: If782464d773262075950e3168128c0d46e4a6530 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/578715 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Cherry Mui <[email protected]> Reviewed-by: Than McIntosh <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Filippo Valsorda <[email protected]> Run-TryBot: Michael Munday <[email protected]>
1 parent d042a39 commit 5defcc1

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

sha3/sha3_s390x.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ func (s *asmState) Write(b []byte) (int, error) {
143143

144144
// Read squeezes an arbitrary number of bytes from the sponge.
145145
func (s *asmState) Read(out []byte) (n int, err error) {
146+
// The 'compute last message digest' instruction only stores the digest
147+
// at the first operand (dst) for SHAKE functions.
148+
if s.function != shake_128 && s.function != shake_256 {
149+
panic("sha3: can only call Read for SHAKE functions")
150+
}
151+
146152
n = len(out)
147153

148154
// need to pad if we were absorbing
@@ -202,8 +208,17 @@ func (s *asmState) Sum(b []byte) []byte {
202208

203209
// Hash the buffer. Note that we don't clear it because we
204210
// aren't updating the state.
205-
klmd(s.function, &a, nil, s.buf)
206-
return append(b, a[:s.outputLen]...)
211+
switch s.function {
212+
case sha3_224, sha3_256, sha3_384, sha3_512:
213+
klmd(s.function, &a, nil, s.buf)
214+
return append(b, a[:s.outputLen]...)
215+
case shake_128, shake_256:
216+
d := make([]byte, s.outputLen, 64)
217+
klmd(s.function, &a, d, s.buf)
218+
return append(b, d[:s.outputLen]...)
219+
default:
220+
panic("sha3: unknown function")
221+
}
207222
}
208223

209224
// Reset resets the Hash to its initial state.

sha3/sha3_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,34 @@ func TestKeccak(t *testing.T) {
188188
}
189189
}
190190

191+
// TestShakeSum tests that the output of Sum matches the output of Read.
192+
func TestShakeSum(t *testing.T) {
193+
tests := [...]struct {
194+
name string
195+
hash ShakeHash
196+
expectedLen int
197+
}{
198+
{"SHAKE128", NewShake128(), 32},
199+
{"SHAKE256", NewShake256(), 64},
200+
{"cSHAKE128", NewCShake128([]byte{'X'}, nil), 32},
201+
{"cSHAKE256", NewCShake256([]byte{'X'}, nil), 64},
202+
}
203+
204+
for _, test := range tests {
205+
t.Run(test.name, func(t *testing.T) {
206+
s := test.hash.Sum(nil)
207+
if len(s) != test.expectedLen {
208+
t.Errorf("Unexpected digest length: got %d, want %d", len(s), test.expectedLen)
209+
}
210+
r := make([]byte, test.expectedLen)
211+
test.hash.Read(r)
212+
if !bytes.Equal(s, r) {
213+
t.Errorf("Mismatch between Sum and Read:\nSum: %s\nRead: %s", hex.EncodeToString(s), hex.EncodeToString(r))
214+
}
215+
})
216+
}
217+
}
218+
191219
// TestUnalignedWrite tests that writing data in an arbitrary pattern with
192220
// small input buffers.
193221
func TestUnalignedWrite(t *testing.T) {

0 commit comments

Comments
 (0)