-
Notifications
You must be signed in to change notification settings - Fork 0
/
vmac_test.go
70 lines (61 loc) · 1.75 KB
/
vmac_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package vmac
import (
"fmt"
"strings"
"testing"
)
var key = []byte("abcdefghijklmnop")
var nonce = []byte("bcdefghi")
type vmacTest struct {
size int
message string
sum string
}
// Tests from http://fastcrypto.org/vmac/draft-krovetz-vmac-01.txt
var vmacTests = []vmacTest{
{8, "", "2576BE1C56D8B81B"},
{16, "", "472766C70F74ED23481D6D7DE4E80DAC"},
{8, "abc", "2D376CF5B1813CE5"},
{16, "abc", "4EE815A06A1D71EDD36FC75D51188A42"},
{8, strings.Repeat("abc", 16), "E8421F61D573D298"},
{16, strings.Repeat("abc", 16), "09F2C80C8E1007A0C12FAE19FE4504AE"},
{8, strings.Repeat("abc", 100), "4492DF6C5CAC1BBE"},
{16, strings.Repeat("abc", 100), "66438817154850C61D8A412164803BCB"},
{8, strings.Repeat("abc", 1000000), "09BA597DD7601113"},
{16, strings.Repeat("abc", 1000000), "2B6B02288FFC461B75485DE893C629DC"},
}
func TestVmac(t *testing.T) {
for i, test := range vmacTests {
h, err := New(key, nonce, test.size)
if err != nil {
t.Errorf("Test %d: New(%x, %x, %d) failed with error=%s\n", i, key, nonce, test.size, err)
continue
}
// Iterate twice to test Reset()
for j := 0; j < 2; j++ {
n, err := h.Write([]byte(test.message))
if n != len(test.message) || err != nil {
t.Errorf("Test %d.%d: Write('abc' * %d) failed with n=%d and error=%s\n", i, j, len(test.message)/3, n, err)
continue
}
// Sum should be idempotent
for k := 0; k < 2; k++ {
sum := fmt.Sprintf("%X", h.Sum())
if sum != test.sum {
t.Errorf("Test %d.%d.%d: Sum() returned %s, expected %s\n", i, j, k, sum, test.sum)
}
}
h.Reset()
}
}
}
func BenchmarkVmac(b *testing.B) {
b.StopTimer()
test := vmacTests[7]
h, _ := New(key, nonce, test.size)
h.Write([]byte(test.message))
b.StartTimer()
for i := 0; i < b.N; i++ {
h.Sum()
}
}