| 1 |
|
| 2 |
|
| 3 |
package siphash |
| 4 |
|
| 5 |
import "hash" |
| 6 |
|
| 7 |
|
| 8 |
func New(key *[16]byte) hash.Hash64 { |
| 9 |
h := new(hashFunc) |
| 10 |
h.key[0] = uint64(key[0]) | uint64(key[1])<<8 | uint64(key[2])<<16 | uint64(key[3])<<24 | |
| 11 |
uint64(key[4])<<32 | uint64(key[5])<<40 | uint64(key[6])<<48 | uint64(key[7])<<56 |
| 12 |
h.key[1] = uint64(key[8]) | uint64(key[9])<<8 | uint64(key[10])<<16 | uint64(key[11])<<24 | |
| 13 |
uint64(key[12])<<32 | uint64(key[13])<<40 | uint64(key[14])<<48 | uint64(key[15])<<56 |
| 14 |
h.Reset() |
| 15 |
return h |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
func Sum(out *[TagSize]byte, msg []byte, key *[16]byte) { |
| 21 |
r := Sum64(msg, key) |
| 22 |
|
| 23 |
out[0] = byte(r) |
| 24 |
out[1] = byte(r >> 8) |
| 25 |
out[2] = byte(r >> 16) |
| 26 |
out[3] = byte(r >> 24) |
| 27 |
out[4] = byte(r >> 32) |
| 28 |
out[5] = byte(r >> 40) |
| 29 |
out[6] = byte(r >> 48) |
| 30 |
out[7] = byte(r >> 56) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
func Sum64(msg []byte, key *[16]byte) uint64 { |
| 36 |
k0 := uint64(key[0]) | uint64(key[1])<<8 | uint64(key[2])<<16 | uint64(key[3])<<24 | |
| 37 |
uint64(key[4])<<32 | uint64(key[5])<<40 | uint64(key[6])<<48 | uint64(key[7])<<56 |
| 38 |
k1 := uint64(key[8]) | uint64(key[9])<<8 | uint64(key[10])<<16 | uint64(key[11])<<24 | |
| 39 |
uint64(key[12])<<32 | uint64(key[13])<<40 | uint64(key[14])<<48 | uint64(key[15])<<56 |
| 40 |
|
| 41 |
var hVal [4]uint64 |
| 42 |
hVal[0] = k0 ^ c0 |
| 43 |
hVal[1] = k1 ^ c1 |
| 44 |
hVal[2] = k0 ^ c2 |
| 45 |
hVal[3] = k1 ^ c3 |
| 46 |
|
| 47 |
n := len(msg) |
| 48 |
ctr := byte(n) |
| 49 |
|
| 50 |
if n >= TagSize { |
| 51 |
n &= (^(TagSize - 1)) |
| 52 |
core(&hVal, msg[:n]) |
| 53 |
msg = msg[n:] |
| 54 |
} |
| 55 |
|
| 56 |
var block [TagSize]byte |
| 57 |
for i, v := range msg { |
| 58 |
block[i] = v |
| 59 |
} |
| 60 |
block[7] = ctr |
| 61 |
|
| 62 |
return finalize(&hVal, &block) |
| 63 |
} |