| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package blake2b |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"encoding/hex" |
| 9 |
"testing" |
| 10 |
) |
| 11 |
|
| 12 |
func fromHex(s string) []byte { |
| 13 |
b, err := hex.DecodeString(s) |
| 14 |
if err != nil { |
| 15 |
panic(err) |
| 16 |
} |
| 17 |
return b |
| 18 |
} |
| 19 |
|
| 20 |
var testVectors = []struct { |
| 21 |
hashsize int |
| 22 |
conf *Config |
| 23 |
msg, hash string |
| 24 |
}{ |
| 25 |
|
| 26 |
{ |
| 27 |
hashsize: 64, |
| 28 |
msg: hex.EncodeToString([]byte("abc")), |
| 29 |
hash: "BA80A53F981C4D0D6A2797B69F12F6E94C212F14685AC4B74B12BB6FDBFFA2D1" + |
| 30 |
"7D87C5392AAB792DC252D5DE4533CC9518D38AA8DBF1925AB92386EDD4009923", |
| 31 |
}, |
| 32 |
|
| 33 |
|
| 34 |
{ |
| 35 |
hashsize: 64, |
| 36 |
conf: &Config{Key: fromHex("000102030405060708090a0b0c0d0e0f10111213141" + |
| 37 |
"5161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f30313233343" + |
| 38 |
"5363738393a3b3c3d3e3f")}, |
| 39 |
msg: hex.EncodeToString([]byte("")), |
| 40 |
hash: "10ebb67700b1868efb4417987acf4690ae9d972fb7a590c2f02871799aaa4786" + |
| 41 |
"b5e996e8f0f4eb981fc214b005f42d2ff4233499391653df7aefcbc13fc51568", |
| 42 |
}, |
| 43 |
{ |
| 44 |
hashsize: 64, |
| 45 |
conf: &Config{Key: fromHex("000102030405060708090a0b0c0d0e0f1011121314151617181" + |
| 46 |
"91a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a" + |
| 47 |
"3b3c3d3e3f")}, |
| 48 |
msg: "00", |
| 49 |
hash: "961f6dd1e4dd30f63901690c512e78e4b45e4742ed197c3c5e45c549fd25f2e4" + |
| 50 |
"187b0bc9fe30492b16b0d0bc4ef9b0f34c7003fac09a5ef1532e69430234cebd", |
| 51 |
}, |
| 52 |
{ |
| 53 |
hashsize: 64, |
| 54 |
conf: &Config{Key: fromHex("000102030405060708090a0b0c0d0e0f1011121314151617181" + |
| 55 |
"91a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a" + |
| 56 |
"3b3c3d3e3f")}, |
| 57 |
msg: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2" + |
| 58 |
"02122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f", |
| 59 |
hash: "65676d800617972fbd87e4b9514e1c67402b7a331096d3bfac22f1abb95374ab" + |
| 60 |
"c942f16e9ab0ead33b87c91968a6e509e119ff07787b3ef483e1dcdccf6e3022", |
| 61 |
}, |
| 62 |
{ |
| 63 |
hashsize: 64, |
| 64 |
conf: &Config{Key: fromHex("000102030405060708090a0b0c0d0e0f10111213141516171819" + |
| 65 |
"1a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b" + |
| 66 |
"3c3d3e3f")}, |
| 67 |
msg: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f" + |
| 68 |
"202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f4" + |
| 69 |
"04142434445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f60" + |
| 70 |
"6162636465666768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f808" + |
| 71 |
"182838485868788898a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1" + |
| 72 |
"a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c" + |
| 73 |
"2c3c4c5c6c7c8c9cacbcccdcecfd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2" + |
| 74 |
"e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfd", |
| 75 |
hash: "d444bfa2362a96df213d070e33fa841f51334e4e76866b8139e8af3bb3398be2" + |
| 76 |
"dfaddcbc56b9146de9f68118dc5829e74b0c28d7711907b121f9161cb92b69a9", |
| 77 |
}, |
| 78 |
{ |
| 79 |
hashsize: 64, |
| 80 |
conf: &Config{Key: fromHex("000102030405060708090a0b0c0d0e0f10111213141516171819" + |
| 81 |
"1a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b" + |
| 82 |
"3c3d3e3f")}, |
| 83 |
msg: "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f2" + |
| 84 |
"02122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f4041424" + |
| 85 |
"34445464748494a4b4c4d4e4f505152535455565758595a5b5c5d5e5f6061626364656" + |
| 86 |
"66768696a6b6c6d6e6f707172737475767778797a7b7c7d7e7f8081828384858687888" + |
| 87 |
"98a8b8c8d8e8f909192939495969798999a9b9c9d9e9fa0a1a2a3a4a5a6a7a8a9aaaba" + |
| 88 |
"cadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0c1c2c3c4c5c6c7c8c9cacbcccdcec" + |
| 89 |
"fd0d1d2d3d4d5d6d7d8d9dadbdcdddedfe0e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f" + |
| 90 |
"2f3f4f5f6f7f8f9fafbfcfdfe", |
| 91 |
hash: "142709d62e28fcccd0af97fad0f8465b971e82201dc51070faa0372aa43e9248" + |
| 92 |
"4be1c1e73ba10906d5d1853db6a4106e0a7bf9800d373d6dee2d46d62ef2a461", |
| 93 |
}, |
| 94 |
} |
| 95 |
|
| 96 |
func TestVectors(t *testing.T) { |
| 97 |
for i, v := range testVectors { |
| 98 |
h, err := New(v.hashsize, v.conf) |
| 99 |
if err != nil { |
| 100 |
t.Fatalf("Test vector %d : Failed to create new BLAKE2b instance: %s", i, err) |
| 101 |
} |
| 102 |
msg := fromHex(v.msg) |
| 103 |
expSum := fromHex(v.hash) |
| 104 |
|
| 105 |
h.Write(msg) |
| 106 |
sum := h.Sum(nil) |
| 107 |
if !bytes.Equal(sum, expSum) { |
| 108 |
t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(sum), hex.EncodeToString(expSum)) |
| 109 |
} |
| 110 |
|
| 111 |
sum, err = Sum(msg, v.hashsize, v.conf) |
| 112 |
if err != nil { |
| 113 |
t.Fatalf("Test vector %d : function Sum failed: %s", i, err) |
| 114 |
} |
| 115 |
if !bytes.Equal(sum, expSum) { |
| 116 |
t.Fatalf("Test vector %d : Hash does not match:\nFound: %s\nExpected: %s", i, hex.EncodeToString(sum), hex.EncodeToString(expSum)) |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func generateSequence(out []byte, seed uint32) { |
| 122 |
a := 0xDEAD4BAD * seed |
| 123 |
b := uint32(1) |
| 124 |
|
| 125 |
for i := range out { |
| 126 |
t := a + b |
| 127 |
a = b |
| 128 |
b = t |
| 129 |
out[i] = byte(t >> 24) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestSelfTest(t *testing.T) { |
| 134 |
var result = [32]byte{ |
| 135 |
0xC2, 0x3A, 0x78, 0x00, 0xD9, 0x81, 0x23, 0xBD, |
| 136 |
0x10, 0xF5, 0x06, 0xC6, 0x1E, 0x29, 0xDA, 0x56, |
| 137 |
0x03, 0xD7, 0x63, 0xB8, 0xBB, 0xAD, 0x2E, 0x73, |
| 138 |
0x7F, 0x5E, 0x76, 0x5A, 0x7B, 0xCC, 0xD4, 0x75, |
| 139 |
} |
| 140 |
var hashLens = [4]int{20, 32, 48, 64} |
| 141 |
var msgLens = [6]int{0, 3, 128, 129, 255, 1024} |
| 142 |
|
| 143 |
msg := make([]byte, 1024) |
| 144 |
key := make([]byte, 64) |
| 145 |
|
| 146 |
h, err := New(32, nil) |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("Failed to create BLAKE2b instance: %s", err) |
| 149 |
} |
| 150 |
for _, hashsize := range hashLens { |
| 151 |
for _, msgLength := range msgLens { |
| 152 |
generateSequence(msg[:msgLength], uint32(msgLength)) |
| 153 |
md, err := Sum(msg[:msgLength], hashsize, nil) |
| 154 |
if err != nil { |
| 155 |
t.Fatalf("Selftest failed: Failed to compute unkeyed hash: %s", err) |
| 156 |
} |
| 157 |
h.Write(md) |
| 158 |
|
| 159 |
generateSequence(key[:], uint32(hashsize)) |
| 160 |
md, err = Sum(msg[:msgLength], hashsize, &Config{Key: key[:hashsize]}) |
| 161 |
if err != nil { |
| 162 |
t.Fatalf("Selftest failed: Failed to compute keyed hash: %s", err) |
| 163 |
} |
| 164 |
h.Write(md) |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
sum := h.Sum(nil) |
| 169 |
if !bytes.Equal(sum, result[:]) { |
| 170 |
t.Fatalf("Selftest failed:\nFound: %s\nExpected: %s", hex.EncodeToString(sum), hex.EncodeToString(result[:])) |
| 171 |
} |
| 172 |
} |