...
1
2
3
4
5 package wycheproof
6
7 import (
8 "crypto/hmac"
9 "testing"
10 )
11
12 func TestHMAC(t *testing.T) {
13
14 type MacTestVector struct {
15
16
17 Comment string `json:"comment,omitempty"`
18
19
20 Flags []string `json:"flags,omitempty"`
21
22
23 Key string `json:"key,omitempty"`
24
25
26 Msg string `json:"msg,omitempty"`
27
28
29 Result string `json:"result,omitempty"`
30
31
32 Tag string `json:"tag,omitempty"`
33
34
35 TcId int `json:"tcId,omitempty"`
36 }
37
38
39 type MacTestGroup struct {
40
41
42 KeySize int `json:"keySize,omitempty"`
43
44
45 TagSize int `json:"tagSize,omitempty"`
46 Tests []*MacTestVector `json:"tests,omitempty"`
47 Type interface{} `json:"type,omitempty"`
48 }
49
50
51 type Notes struct {
52 }
53
54
55 type Root struct {
56
57
58 Algorithm string `json:"algorithm,omitempty"`
59
60
61 GeneratorVersion string `json:"generatorVersion,omitempty"`
62
63
64 Header []string `json:"header,omitempty"`
65
66
67 Notes *Notes `json:"notes,omitempty"`
68
69
70 NumberOfTests int `json:"numberOfTests,omitempty"`
71 Schema interface{} `json:"schema,omitempty"`
72 TestGroups []*MacTestGroup `json:"testGroups,omitempty"`
73 }
74
75 fileHashAlgs := map[string]string{
76 "hmac_sha1_test.json": "SHA-1",
77 "hmac_sha224_test.json": "SHA-224",
78 "hmac_sha256_test.json": "SHA-256",
79 "hmac_sha384_test.json": "SHA-384",
80 "hmac_sha512_test.json": "SHA-512",
81 }
82
83 for f := range fileHashAlgs {
84 var root Root
85 readTestVector(t, f, &root)
86 for _, tg := range root.TestGroups {
87 h := parseHash(fileHashAlgs[f])
88
89
90
91 if tg.TagSize/8 != h.Size() {
92 continue
93 }
94 for _, tv := range tg.Tests {
95 hm := hmac.New(h.New, decodeHex(tv.Key))
96 hm.Write(decodeHex(tv.Msg))
97 tag := hm.Sum(nil)
98 got := hmac.Equal(decodeHex(tv.Tag), tag)
99 if want := shouldPass(tv.Result, tv.Flags, nil); want != got {
100 t.Errorf("%s, tcid: %d, type: %s, comment: %q, unexpected result", f, tv.TcId, tv.Result, tv.Comment)
101 }
102 }
103 }
104 }
105 }
106
View as plain text