Source file
src/crypto/hmac/hmac_wycheproof_test.go
1
2
3
4 package hmac_test
5
6 import (
7 "bytes"
8 "crypto/hmac"
9 "crypto/internal/cryptotest/wycheproof"
10 "crypto/sha1"
11 "crypto/sha256"
12 "crypto/sha3"
13 "crypto/sha512"
14 "hash"
15 "testing"
16 )
17
18 func TestHMACWycheproof(t *testing.T) {
19 filesToHash := map[string]func() hash.Hash{
20 "hmac_sha1_test.json": sha1.New,
21 "hmac_sha224_test.json": sha256.New224,
22 "hmac_sha256_test.json": sha256.New,
23 "hmac_sha3_224_test.json": func() hash.Hash { return sha3.New224() },
24 "hmac_sha3_256_test.json": func() hash.Hash { return sha3.New256() },
25 "hmac_sha3_384_test.json": func() hash.Hash { return sha3.New384() },
26 "hmac_sha3_512_test.json": func() hash.Hash { return sha3.New512() },
27 "hmac_sha384_test.json": sha512.New384,
28 "hmac_sha512_test.json": sha512.New,
29 "hmac_sha512_224_test.json": sha512.New512_224,
30 "hmac_sha512_256_test.json": sha512.New512_256,
31 }
32
33 for file, h := range filesToHash {
34 var testdata wycheproof.MacTestSchemaV1Json
35 wycheproof.LoadVectorFile(t, file, &testdata)
36
37 for _, tg := range testdata.TestGroups {
38 tagSize := tg.TagSize / 8
39
40 for _, tv := range tg.Tests {
41 t.Run(wycheproof.TestName(file, tv), func(t *testing.T) {
42 t.Parallel()
43
44 hm := hmac.New(h, wycheproof.MustDecodeHex(tv.Key))
45 hm.Write(wycheproof.MustDecodeHex(tv.Msg))
46
47 computedTag := hm.Sum(nil)[:tagSize]
48 got := bytes.Equal(wycheproof.MustDecodeHex(tv.Tag), computedTag)
49 want := wycheproof.ShouldPass(t, tv.Result, tv.Flags, nil)
50 if want != got {
51 t.Errorf("unexpected result")
52 }
53 })
54 }
55 }
56 }
57 }
58
View as plain text