2014-12-31 22:59:00 +00:00
|
|
|
package keccak
|
|
|
|
|
|
|
|
import "hash"
|
|
|
|
|
2014-12-31 23:19:23 +00:00
|
|
|
const Size = 256 / 8
|
2014-12-31 22:59:00 +00:00
|
|
|
|
|
|
|
const BlockSize = 1600/8 - Size*2
|
|
|
|
|
2015-01-01 00:40:56 +00:00
|
|
|
var round = roundGen
|
|
|
|
|
2014-12-31 22:59:00 +00:00
|
|
|
// digest implements hash.Hash
|
|
|
|
type digest struct {
|
2014-12-31 23:19:23 +00:00
|
|
|
a [5][5]uint64 // a[y][x][z]
|
2014-12-31 22:59:00 +00:00
|
|
|
buf [BlockSize]byte
|
|
|
|
len int
|
|
|
|
}
|
|
|
|
|
|
|
|
func New() hash.Hash {
|
|
|
|
return &digest{}
|
|
|
|
}
|
|
|
|
|
2014-12-31 23:19:23 +00:00
|
|
|
func (d *digest) Size() int { return Size }
|
2014-12-31 22:59:00 +00:00
|
|
|
func (d *digest) BlockSize() int { return BlockSize }
|
|
|
|
|
|
|
|
func (d *digest) Reset() {
|
|
|
|
*d = digest{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *digest) Write(b []byte) (int, error) {
|
|
|
|
written := len(b)
|
|
|
|
for len(b) > 0 {
|
|
|
|
n := copy(d.buf[d.len:], b)
|
|
|
|
d.len += n
|
|
|
|
b = b[n:]
|
|
|
|
if d.len == BlockSize {
|
|
|
|
d.flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *digest) flush() {
|
|
|
|
b := d.buf[:]
|
|
|
|
loop:
|
2014-12-31 23:15:49 +00:00
|
|
|
for y := range d.a {
|
2014-12-31 23:19:23 +00:00
|
|
|
for x := range d.a[0] {
|
2014-12-31 22:59:00 +00:00
|
|
|
if len(b) == 0 {
|
|
|
|
break loop
|
|
|
|
}
|
2014-12-31 23:15:49 +00:00
|
|
|
d.a[y][x] ^= le64dec(b)
|
2014-12-31 22:59:00 +00:00
|
|
|
b = b[8:]
|
|
|
|
}
|
|
|
|
}
|
2014-12-31 23:17:55 +00:00
|
|
|
keccakf(&d.a)
|
2014-12-31 22:59:00 +00:00
|
|
|
d.len = 0
|
|
|
|
}
|
|
|
|
|
2014-12-31 23:17:55 +00:00
|
|
|
func keccakf(a *[5][5]uint64) {
|
2014-12-31 22:59:00 +00:00
|
|
|
for i := 0; i < 24; i++ {
|
2015-01-01 00:40:56 +00:00
|
|
|
round(a)
|
2014-12-31 22:59:00 +00:00
|
|
|
a[0][0] ^= RC[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d0 *digest) Sum(b []byte) []byte {
|
|
|
|
d := *d0
|
|
|
|
d.buf[d.len] = 0x01
|
2014-12-31 23:19:23 +00:00
|
|
|
for i := d.len + 1; i < BlockSize; i++ {
|
2014-12-31 22:59:00 +00:00
|
|
|
d.buf[i] = 0
|
|
|
|
}
|
|
|
|
d.buf[BlockSize-1] |= 0x80
|
|
|
|
d.flush()
|
|
|
|
|
|
|
|
b = le64enc(b, d.a[0][0])
|
2014-12-31 23:15:49 +00:00
|
|
|
b = le64enc(b, d.a[0][1])
|
|
|
|
b = le64enc(b, d.a[0][2])
|
|
|
|
b = le64enc(b, d.a[0][3])
|
2014-12-31 22:59:00 +00:00
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2014-12-31 23:11:40 +00:00
|
|
|
func le64dec(b []byte) uint64 {
|
2014-12-31 23:19:23 +00:00
|
|
|
return uint64(b[0])<<0 | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
|
2014-12-31 23:11:40 +00:00
|
|
|
}
|
|
|
|
|
2014-12-31 22:59:00 +00:00
|
|
|
func le64enc(b []byte, x uint64) []byte {
|
|
|
|
return append(b, byte(x), byte(x>>8), byte(x>>16), byte(x>>24), byte(x>>32), byte(x>>40), byte(x>>48), byte(x>>56))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *digest) writeByte(b byte) {
|
|
|
|
var tmp = [1]byte{b}
|
|
|
|
d.Write(tmp[:])
|
|
|
|
}
|