123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- package sha3
- type spongeDirection int
- const (
-
- spongeAbsorbing spongeDirection = iota
-
- spongeSqueezing
- )
- const (
-
-
- maxRate = 168
- )
- type state struct {
-
- a [25]uint64
- buf []byte
- rate int
-
-
-
-
-
-
-
-
-
-
-
-
- dsbyte byte
- storage [maxRate]byte
-
- fixedOutput bool
- outputLen int
- state spongeDirection
- }
- func (d *state) BlockSize() int { return d.rate }
- func (d *state) Size() int { return d.outputLen }
- func (d *state) Reset() {
-
- for i := range d.a {
- d.a[i] = 0
- }
- d.state = spongeAbsorbing
- d.buf = d.storage[:0]
- }
- func (d *state) clone() *state {
- ret := *d
- if ret.state == spongeAbsorbing {
- ret.buf = ret.storage[:len(ret.buf)]
- } else {
- ret.buf = ret.storage[d.rate-cap(d.buf) : d.rate]
- }
- return &ret
- }
- func (d *state) permute() {
- switch d.state {
- case spongeAbsorbing:
-
-
- xorIn(d, d.buf)
- d.buf = d.storage[:0]
- keccakF1600(&d.a)
- case spongeSqueezing:
-
-
- keccakF1600(&d.a)
- d.buf = d.storage[:d.rate]
- copyOut(d, d.buf)
- }
- }
- func (d *state) padAndPermute(dsbyte byte) {
- if d.buf == nil {
- d.buf = d.storage[:0]
- }
-
-
-
-
- d.buf = append(d.buf, dsbyte)
- zerosStart := len(d.buf)
- d.buf = d.storage[:d.rate]
- for i := zerosStart; i < d.rate; i++ {
- d.buf[i] = 0
- }
-
-
-
- d.buf[d.rate-1] ^= 0x80
-
- d.permute()
- d.state = spongeSqueezing
- d.buf = d.storage[:d.rate]
- copyOut(d, d.buf)
- }
- func (d *state) Write(p []byte) (written int, err error) {
- if d.state != spongeAbsorbing {
- panic("sha3: write to sponge after read")
- }
- if d.buf == nil {
- d.buf = d.storage[:0]
- }
- written = len(p)
- for len(p) > 0 {
- if len(d.buf) == 0 && len(p) >= d.rate {
-
- xorIn(d, p[:d.rate])
- p = p[d.rate:]
- keccakF1600(&d.a)
- } else {
-
- todo := d.rate - len(d.buf)
- if todo > len(p) {
- todo = len(p)
- }
- d.buf = append(d.buf, p[:todo]...)
- p = p[todo:]
-
- if len(d.buf) == d.rate {
- d.permute()
- }
- }
- }
- return
- }
- func (d *state) Read(out []byte) (n int, err error) {
-
- if d.state == spongeAbsorbing {
- d.padAndPermute(d.dsbyte)
- }
- n = len(out)
-
- for len(out) > 0 {
- n := copy(out, d.buf)
- d.buf = d.buf[n:]
- out = out[n:]
-
- if len(d.buf) == 0 {
- d.permute()
- }
- }
- return
- }
- func (d *state) Sum(in []byte) []byte {
-
-
- dup := d.clone()
- hash := make([]byte, dup.outputLen)
- dup.Read(hash)
- return append(in, hash...)
- }
|