From cccb9890e1c06f5fd3c8e994737d613114af8555 Mon Sep 17 00:00:00 2001 From: Kris Kwiatkowski Date: Sat, 3 Oct 2020 05:40:47 +0100 Subject: [PATCH] add function for one-off calculation --- hash/sha3/sha3.go | 46 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/hash/sha3/sha3.go b/hash/sha3/sha3.go index 664a6ce..14be08c 100644 --- a/hash/sha3/sha3.go +++ b/hash/sha3/sha3.go @@ -132,6 +132,24 @@ func (c *state) Write(in []byte) (nwrite int, err error) { return nwrite, nil } +func (c *state) finalize_sha3() { + buf := c.data.asBytes()[:] + rate := c.BlockSize() + + // there is at least one byte free, otherise + // buf would be squezed already + for i := c.idx + 1; i < rate; i++ { + buf[i] = 0 + } + buf[c.idx] = c.sfx + buf[rate-1] |= 0x80 + xorIn(c, buf[:rate]) + keccakF1600(&c.a) + copyOut(c, buf[:rate]) + c.idx = rate // now, idx indicates unconsumed amount of data + c.isSquezing = true +} + // Read changes state of the hash if called first time. It will // return len(out) bytes of data. Never fails. func (c *state) Read(out []byte) (nread int, err error) { @@ -140,18 +158,7 @@ func (c *state) Read(out []byte) (nread int, err error) { nread = len(out) if !c.isSquezing { - // there is at least one byte free, otherise - // buf would be squezed already - for i := c.idx + 1; i < rate; i++ { - buf[i] = 0 - } - buf[c.idx] = c.sfx - buf[rate-1] |= 0x80 - xorIn(c, buf[:rate]) - keccakF1600(&c.a) - copyOut(c, buf[:rate]) - c.idx = rate // now, idx indicates unconsumed amount of data - c.isSquezing = true + c.finalize_sha3() } // Copy-out bytes that are still kept in the buffer @@ -211,6 +218,21 @@ func (c *state) Sum(in []byte) []byte { return in } +func (c *state) digest(out, in []byte) { + nread := len(out) + rate := c.BlockSize() + nblocks := nread / rate + c.Write(in) + c.finalize_sha3() + for i := 0; i < nblocks-1; i++ { + keccakF1600(&c.a) + copyOut(c, out[:]) + out = out[rate:] + } + keccakF1600(&c.a) + copyOut(c, out[:len(out)]) +} + // New224 creates a new SHA3-224 hash. // Its generic security strength is 224 bits against preimage attacks, // and 112 bits against collision attacks.