poly1305: add additional test cases
Increase the number of test vectors in this package to provide better validation of new SIMD implementations. Change-Id: Ia89883609e78cef53ba40a9cae41f4e0a3bccc80 Reviewed-on: https://go-review.googlesource.com/112855 Run-TryBot: Michael Munday <mike.munday@ibm.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
This commit is contained in:
Родитель
4eb8c2c8d8
Коммит
425cc7d9a7
|
@ -5,7 +5,6 @@
|
||||||
package poly1305
|
package poly1305
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
"flag"
|
"flag"
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -14,80 +13,51 @@ import (
|
||||||
|
|
||||||
var stressFlag = flag.Bool("stress", false, "run slow stress tests")
|
var stressFlag = flag.Bool("stress", false, "run slow stress tests")
|
||||||
|
|
||||||
var testData = []struct {
|
type test struct {
|
||||||
in, k, correct []byte
|
in string
|
||||||
}{
|
key string
|
||||||
{
|
tag string
|
||||||
[]byte("Hello world!"),
|
}
|
||||||
[]byte("this is 32-byte key for Poly1305"),
|
|
||||||
[]byte{0xa6, 0xf7, 0x45, 0x00, 0x8f, 0x81, 0xc9, 0x16, 0xa2, 0x0d, 0xcc, 0x74, 0xee, 0xf2, 0xb2, 0xf0},
|
func (t *test) Input() []byte {
|
||||||
},
|
in, err := hex.DecodeString(t.in)
|
||||||
{
|
if err != nil {
|
||||||
make([]byte, 32),
|
panic(err)
|
||||||
[]byte("this is 32-byte key for Poly1305"),
|
}
|
||||||
[]byte{0x49, 0xec, 0x78, 0x09, 0x0e, 0x48, 0x1e, 0xc6, 0xc2, 0x6b, 0x33, 0xb9, 0x1c, 0xcc, 0x03, 0x07},
|
return in
|
||||||
},
|
}
|
||||||
{
|
|
||||||
make([]byte, 2007),
|
func (t *test) Key() [32]byte {
|
||||||
[]byte("this is 32-byte key for Poly1305"),
|
buf, err := hex.DecodeString(t.key)
|
||||||
[]byte{0xda, 0x84, 0xbc, 0xab, 0x02, 0x67, 0x6c, 0x38, 0xcd, 0xb0, 0x15, 0x60, 0x42, 0x74, 0xc2, 0xaa},
|
if err != nil {
|
||||||
},
|
panic(err)
|
||||||
{
|
}
|
||||||
make([]byte, 2007),
|
var key [32]byte
|
||||||
make([]byte, 32),
|
copy(key[:], buf[:32])
|
||||||
make([]byte, 16),
|
return key
|
||||||
},
|
}
|
||||||
{
|
|
||||||
// This test triggers an edge-case. See https://go-review.googlesource.com/#/c/30101/.
|
func (t *test) Tag() [16]byte {
|
||||||
[]byte{0x81, 0xd8, 0xb2, 0xe4, 0x6a, 0x25, 0x21, 0x3b, 0x58, 0xfe, 0xe4, 0x21, 0x3a, 0x2a, 0x28, 0xe9, 0x21, 0xc1, 0x2a, 0x96, 0x32, 0x51, 0x6d, 0x3b, 0x73, 0x27, 0x27, 0x27, 0xbe, 0xcf, 0x21, 0x29},
|
buf, err := hex.DecodeString(t.tag)
|
||||||
[]byte{0x3b, 0x3a, 0x29, 0xe9, 0x3b, 0x21, 0x3a, 0x5c, 0x5c, 0x3b, 0x3b, 0x05, 0x3a, 0x3a, 0x8c, 0x0d},
|
if err != nil {
|
||||||
[]byte{0x6d, 0xc1, 0x8b, 0x8c, 0x34, 0x4c, 0xd7, 0x99, 0x27, 0x11, 0x8b, 0xbe, 0x84, 0xb7, 0xf3, 0x14},
|
panic(err)
|
||||||
},
|
}
|
||||||
{
|
var tag [16]byte
|
||||||
// This test generates a result of (2^130-1) % (2^130-5).
|
copy(tag[:], buf[:16])
|
||||||
[]byte{
|
return tag
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
},
|
|
||||||
[]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
||||||
[]byte{4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// This test generates a result of (2^130-6) % (2^130-5).
|
|
||||||
[]byte{
|
|
||||||
0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
},
|
|
||||||
[]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
||||||
[]byte{0xfa, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
// This test generates a result of (2^130-5) % (2^130-5).
|
|
||||||
[]byte{
|
|
||||||
0xfb, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
|
||||||
},
|
|
||||||
[]byte{1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
||||||
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func testSum(t *testing.T, unaligned bool, sumImpl func(tag *[TagSize]byte, msg []byte, key *[32]byte)) {
|
func testSum(t *testing.T, unaligned bool, sumImpl func(tag *[TagSize]byte, msg []byte, key *[32]byte)) {
|
||||||
var out [16]byte
|
var tag [16]byte
|
||||||
var key [32]byte
|
|
||||||
|
|
||||||
for i, v := range testData {
|
for i, v := range testData {
|
||||||
in := v.in
|
in := v.Input()
|
||||||
if unaligned {
|
if unaligned {
|
||||||
in = unalignBytes(in)
|
in = unalignBytes(in)
|
||||||
}
|
}
|
||||||
copy(key[:], v.k)
|
key := v.Key()
|
||||||
sumImpl(&out, in, &key)
|
sumImpl(&tag, in, &key)
|
||||||
if !bytes.Equal(out[:], v.correct) {
|
if tag != v.Tag() {
|
||||||
t.Errorf("%d: expected %x, got %x", i, v.correct, out[:])
|
t.Errorf("%d: expected %x, got %x", i, v.Tag(), tag[:])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
Загрузка…
Ссылка в новой задаче