Skip to content

Commit 22d7a77

Browse files
conradoplgFiloSottile
authored andcommitted
sha3: fix bug in cSHAKE Clone()
Clone() made a copy of the Keccak state after invoking clone(), which is not supported, since the "buf" slice in the Keccak state must point to the "storage" array, and if the state is copied directly it will keep pointing to the storage returned by clone(). Fix it by embedding a pointer to the Keccak state instead of the state itself. Change-Id: I7d392963ec65d784a360f6c12a7935a9a9a788b5 Reviewed-on: https://go-review.googlesource.com/c/crypto/+/173018 Reviewed-by: Filippo Valsorda <[email protected]> Run-TryBot: Filippo Valsorda <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent cbcb750 commit 22d7a77

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

Diff for: sha3/sha3_test.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -338,22 +338,25 @@ func TestReset(t *testing.T) {
338338
func TestClone(t *testing.T) {
339339
out1 := make([]byte, 16)
340340
out2 := make([]byte, 16)
341-
in := sequentialBytes(0x100)
342341

343-
for _, v := range testShakes {
344-
h1 := v.constructor(nil, []byte{0x01})
345-
h1.Write([]byte{0x01})
342+
// Test for sizes smaller and larger than block size.
343+
for _, size := range []int{0x1, 0x100} {
344+
in := sequentialBytes(size)
345+
for _, v := range testShakes {
346+
h1 := v.constructor(nil, []byte{0x01})
347+
h1.Write([]byte{0x01})
346348

347-
h2 := h1.Clone()
349+
h2 := h1.Clone()
348350

349-
h1.Write(in)
350-
h1.Read(out1)
351+
h1.Write(in)
352+
h1.Read(out1)
351353

352-
h2.Write(in)
353-
h2.Read(out2)
354+
h2.Write(in)
355+
h2.Read(out2)
354356

355-
if !bytes.Equal(out1, out2) {
356-
t.Error("\nExpected:\n", hex.EncodeToString(out1), "\ngot:\n", hex.EncodeToString(out2))
357+
if !bytes.Equal(out1, out2) {
358+
t.Error("\nExpected:\n", hex.EncodeToString(out1), "\ngot:\n", hex.EncodeToString(out2))
359+
}
357360
}
358361
}
359362
}

Diff for: sha3/shake.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type ShakeHash interface {
4141

4242
// cSHAKE specific context
4343
type cshakeState struct {
44-
state // SHA-3 state context and Read/Write operations
44+
*state // SHA-3 state context and Read/Write operations
4545

4646
// initBlock is the cSHAKE specific initialization set of bytes. It is initialized
4747
// by newCShake function and stores concatenation of N followed by S, encoded
@@ -82,7 +82,7 @@ func leftEncode(value uint64) []byte {
8282
}
8383

8484
func newCShake(N, S []byte, rate int, dsbyte byte) ShakeHash {
85-
c := cshakeState{state: state{rate: rate, dsbyte: dsbyte}}
85+
c := cshakeState{state: &state{rate: rate, dsbyte: dsbyte}}
8686

8787
// leftEncode returns max 9 bytes
8888
c.initBlock = make([]byte, 0, 9*2+len(N)+len(S))
@@ -104,7 +104,7 @@ func (c *cshakeState) Reset() {
104104
func (c *cshakeState) Clone() ShakeHash {
105105
b := make([]byte, len(c.initBlock))
106106
copy(b, c.initBlock)
107-
return &cshakeState{state: *c.clone(), initBlock: b}
107+
return &cshakeState{state: c.clone(), initBlock: b}
108108
}
109109

110110
// Clone returns copy of SHAKE context within its current state.

0 commit comments

Comments
 (0)