125 lines
4.9 KiB
Go
125 lines
4.9 KiB
Go
package keccak
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestShake(t *testing.T) {
|
|
tests := []struct {
|
|
function string
|
|
F, S []byte
|
|
input string
|
|
digest string
|
|
}{
|
|
{
|
|
function: "shake128",
|
|
digest: "027cfb9e61ea1419dfce3b48e6bcb0b6b692fa2904751e5391c9ed468e010d5946f6f7c2be344c3ac2d722d96cf2a9a115681e22fcb43fd162a830b2079b527b",
|
|
},
|
|
{
|
|
function: "shake256",
|
|
digest: "f08a3125f880d9a58e0603ed5ae3778d33b8ccc795e0c18c9b9d735caa079440c6ab577eca4e0d898d966c4b730aee69307af26e459a0332367129476c381817",
|
|
},
|
|
|
|
{
|
|
function: "shake256",
|
|
input: "odd output",
|
|
digest: "678cc171bbc31a40c50d5e",
|
|
},
|
|
|
|
{
|
|
function: "cshake256",
|
|
F: []byte("FunctionName"),
|
|
S: []byte("CustomizationString"),
|
|
digest: "e80473cc7466ed4de29611752502ab9c5597818d",
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
want := tt.digest
|
|
var sh *Shake
|
|
switch tt.function {
|
|
case "shake128":
|
|
sh = NewShake128(nil, nil)
|
|
case "shake256":
|
|
sh = NewShake256(nil, nil)
|
|
case "cshake256":
|
|
sh = NewShake256(tt.F, tt.S)
|
|
}
|
|
if tt.input == "" {
|
|
sh.Write([]byte("hello keccak"))
|
|
} else {
|
|
sh.Write([]byte(tt.input))
|
|
}
|
|
out := make([]byte, len(want)/2)
|
|
sh.Read(out)
|
|
got := fmt.Sprintf("%x", out)
|
|
if want != got {
|
|
t.Errorf("%s: got %s want %s", tt.function, got, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestShakeNIST(t *testing.T) {
|
|
sh := NewShake256(nil, nil)
|
|
want := shake256_digest_m0[:]
|
|
out := make([]byte, len(want))
|
|
n, err := sh.Read(out)
|
|
if n != len(out) || err != nil {
|
|
t.Errorf("unexpected return value: want (0, nil) got (%v,%v)", n, err)
|
|
}
|
|
if !bytes.Equal(want, out) {
|
|
t.Errorf("hash output:\nwant %x\ngot %x", want, out)
|
|
for i := range want {
|
|
if want[i] != out[i] {
|
|
t.Logf("note: first difference is at index %d", i)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// NIST test vector
|
|
var shake256_digest_m0 = [...]byte{
|
|
0x46, 0xB9, 0xDD, 0x2B, 0x0B, 0xA8, 0x8D, 0x13, 0x23, 0x3B, 0x3F, 0xEB, 0x74,
|
|
0x3E, 0xEB, 0x24, 0x3F, 0xCD, 0x52, 0xEA, 0x62, 0xB8, 0x1B, 0x82, 0xB5, 0x0C,
|
|
0x27, 0x64, 0x6E, 0xD5, 0x76, 0x2F, 0xD7, 0x5D, 0xC4, 0xDD, 0xD8, 0xC0, 0xF2,
|
|
0x00, 0xCB, 0x05, 0x01, 0x9D, 0x67, 0xB5, 0x92, 0xF6, 0xFC, 0x82, 0x1C, 0x49,
|
|
0x47, 0x9A, 0xB4, 0x86, 0x40, 0x29, 0x2E, 0xAC, 0xB3, 0xB7, 0xC4, 0xBE, 0x14,
|
|
0x1E, 0x96, 0x61, 0x6F, 0xB1, 0x39, 0x57, 0x69, 0x2C, 0xC7, 0xED, 0xD0, 0xB4,
|
|
0x5A, 0xE3, 0xDC, 0x07, 0x22, 0x3C, 0x8E, 0x92, 0x93, 0x7B, 0xEF, 0x84, 0xBC,
|
|
0x0E, 0xAB, 0x86, 0x28, 0x53, 0x34, 0x9E, 0xC7, 0x55, 0x46, 0xF5, 0x8F, 0xB7,
|
|
0xC2, 0x77, 0x5C, 0x38, 0x46, 0x2C, 0x50, 0x10, 0xD8, 0x46, 0xC1, 0x85, 0xC1,
|
|
0x51, 0x11, 0xE5, 0x95, 0x52, 0x2A, 0x6B, 0xCD, 0x16, 0xCF, 0x86, 0xF3, 0xD1,
|
|
0x22, 0x10, 0x9E, 0x3B, 0x1F, 0xDD, 0x94, 0x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