Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit f3c76e1

Browse files
juergwcopybara-github
authored andcommitted
Add Benchmark tests for MAC in Golang.
It tests computeMAC and verifyMAC for different key types and input length. Output: cpu: AMD EPYC 7B12 BenchmarkComputeMac/HMAC_SHA256_16-8 450417 2641 ns/op 552 B/op 6 allocs/op BenchmarkComputeMac/HMAC_SHA512_16-8 348763 3637 ns/op 816 B/op 6 allocs/op BenchmarkComputeMac/AES_CMAC_16-8 5366028 224.6 ns/op 72 B/op 4 allocs/op BenchmarkComputeMac/HMAC_SHA256_16k-8 29568 40542 ns/op 552 B/op 6 allocs/op BenchmarkComputeMac/HMAC_SHA512_16k-8 35130 34828 ns/op 816 B/op 6 allocs/op BenchmarkComputeMac/AES_CMAC_16k-8 18364 66723 ns/op 72 B/op 4 allocs/op BenchmarkVerifyMac/HMAC_SHA256_16-8 452025 2618 ns/op 528 B/op 5 allocs/op BenchmarkVerifyMac/HMAC_SHA512_16-8 366612 3376 ns/op 768 B/op 5 allocs/op BenchmarkVerifyMac/AES_CMAC_16-8 5455062 220.1 ns/op 48 B/op 3 allocs/op BenchmarkVerifyMac/HMAC_SHA256_16k-8 29738 40823 ns/op 528 B/op 5 allocs/op BenchmarkVerifyMac/HMAC_SHA512_16k-8 35286 35049 ns/op 768 B/op 5 allocs/op BenchmarkVerifyMac/AES_CMAC_16k-8 17973 66508 ns/op 48 B/op 3 allocs/op PiperOrigin-RevId: 625337887
1 parent 92dcf00 commit f3c76e1

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

go/mac/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ go_test(
4040
srcs = [
4141
"aes_cmac_key_manager_test.go",
4242
"hmac_key_manager_test.go",
43+
"mac_benchmark_test.go",
4344
"mac_factory_test.go",
4445
"mac_init_test.go",
4546
"mac_key_templates_test.go",

go/mac/mac_benchmark_test.go

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright 2024 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
// //////////////////////////////////////////////////////////////////////////////
16+
17+
package mac_test
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/tink/go/keyset"
23+
"github.com/google/tink/go/mac"
24+
"github.com/google/tink/go/subtle/random"
25+
tinkpb "github.com/google/tink/go/proto/tink_go_proto"
26+
)
27+
28+
// Benchmarks for MAC algorithms.
29+
30+
var benchmarkTestCases = []struct {
31+
name string
32+
template *tinkpb.KeyTemplate
33+
dataSize uint32
34+
}{
35+
{
36+
name: "HMAC_SHA256_16",
37+
template: mac.HMACSHA256Tag128KeyTemplate(),
38+
dataSize: 16,
39+
}, {
40+
name: "HMAC_SHA512_16",
41+
template: mac.HMACSHA512Tag256KeyTemplate(),
42+
dataSize: 16,
43+
}, {
44+
name: "AES_CMAC_16",
45+
template: mac.AESCMACTag128KeyTemplate(),
46+
dataSize: 16,
47+
}, {
48+
name: "HMAC_SHA256_16k",
49+
template: mac.HMACSHA256Tag128KeyTemplate(),
50+
dataSize: 16 * 1024,
51+
}, {
52+
name: "HMAC_SHA512_16k",
53+
template: mac.HMACSHA512Tag256KeyTemplate(),
54+
dataSize: 16 * 1024,
55+
}, {
56+
name: "AES_CMAC_16k",
57+
template: mac.AESCMACTag128KeyTemplate(),
58+
dataSize: 16 * 1024,
59+
},
60+
}
61+
62+
func BenchmarkComputeMac(b *testing.B) {
63+
for _, tc := range benchmarkTestCases {
64+
b.Run(tc.name, func(b *testing.B) {
65+
b.ReportAllocs()
66+
67+
handle, err := keyset.NewHandle(tc.template)
68+
if err != nil {
69+
b.Fatal(err)
70+
}
71+
primitive, err := mac.New(handle)
72+
if err != nil {
73+
b.Fatal(err)
74+
}
75+
data := random.GetRandomBytes(tc.dataSize)
76+
b.ResetTimer()
77+
for i := 0; i < b.N; i++ {
78+
_, err := primitive.ComputeMAC(data)
79+
if err != nil {
80+
b.Error(err)
81+
}
82+
}
83+
})
84+
}
85+
}
86+
87+
func BenchmarkVerifyMac(b *testing.B) {
88+
for _, tc := range benchmarkTestCases {
89+
b.Run(tc.name, func(b *testing.B) {
90+
b.ReportAllocs()
91+
92+
handle, err := keyset.NewHandle(tc.template)
93+
if err != nil {
94+
b.Fatal(err)
95+
}
96+
primitive, err := mac.New(handle)
97+
if err != nil {
98+
b.Fatal(err)
99+
}
100+
data := random.GetRandomBytes(tc.dataSize)
101+
tag, err := primitive.ComputeMAC(data)
102+
if err != nil {
103+
b.Fatal(err)
104+
}
105+
b.ResetTimer()
106+
for i := 0; i < b.N; i++ {
107+
if err = primitive.VerifyMAC(tag, data); err != nil {
108+
b.Error(err)
109+
}
110+
}
111+
})
112+
}
113+
}

0 commit comments

Comments
 (0)