File: hmac_test.go

package info (click to toggle)
golang-github-tink-crypto-tink-go 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,952 kB
  • sloc: sh: 864; makefile: 6
file content (208 lines) | stat: -rw-r--r-- 6,343 bytes parent folder | download
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hmac_test

import (
	"encoding/hex"
	"strings"
	"testing"

	"github.com/tink-crypto/tink-go/v2/internal/mac/hmac"
	"github.com/tink-crypto/tink-go/v2/subtle/random"
)

var key, _ = hex.DecodeString("000102030405060708090a0b0c0d0e0f")
var data = []byte("Hello")
var hmacTests = []struct {
	desc        string
	hashAlg     string
	tagSize     uint32
	key         []byte
	data        []byte
	expectedMac string
}{
	{
		desc:        "with SHA256 and 32 byte tag",
		hashAlg:     "SHA256",
		tagSize:     32,
		data:        data,
		key:         key,
		expectedMac: "e0ff02553d9a619661026c7aa1ddf59b7b44eac06a9908ff9e19961d481935d4",
	},
	{
		desc:    "with SHA512 and 64 byte tag",
		hashAlg: "SHA512",
		tagSize: 64,
		data:    data,
		key:     key,
		expectedMac: "481e10d823ba64c15b94537a3de3f253c16642451ac45124dd4dde120bf1e5c15" +
			"e55487d55ba72b43039f235226e7954cd5854b30abc4b5b53171a4177047c9b",
	},
	// empty data
	{
		desc:        "empty data",
		hashAlg:     "SHA256",
		tagSize:     32,
		data:        []byte{},
		key:         key,
		expectedMac: "07eff8b326b7798c9ccfcbdbe579489ac785a7995a04618b1a2813c26744777d",
	},
}

func TestHMACBasic(t *testing.T) {
	for _, test := range hmacTests {
		t.Run(test.desc, func(t *testing.T) {
			cipher, err := hmac.New(test.hashAlg, test.key, test.tagSize)
			if err != nil {
				t.Fatalf("hmac.New() err = %q, want nil", err)
			}
			mac, err := cipher.ComputeMAC(test.data)
			if err != nil {
				t.Fatalf("cipher.ComputeMAC() err = %q, want nil", err)
			}
			if hex.EncodeToString(mac) != test.expectedMac {
				t.Errorf("hex.EncodeToString(mac) = %q, want %q",
					hex.EncodeToString(mac), test.expectedMac)
			}
			if err := cipher.VerifyMAC(mac, test.data); err != nil {
				t.Errorf("cipher.VerifyMAC() err = %q, want nil", err)
			}
		})
	}
}

func TestNewHMACWithInvalidInput(t *testing.T) {
	// invalid hash algorithm
	_, err := hmac.New("MD5", random.GetRandomBytes(16), 32)
	if err == nil || !strings.Contains(err.Error(), "invalid hash algorithm") {
		t.Errorf("expect an error when hash algorithm is invalid")
	}
	// key too short
	_, err = hmac.New("SHA256", random.GetRandomBytes(1), 32)
	if err == nil || !strings.Contains(err.Error(), "key too short") {
		t.Errorf("expect an error when key is too short")
	}
	// tag too short
	_, err = hmac.New("SHA256", random.GetRandomBytes(16), 9)
	if err == nil || !strings.Contains(err.Error(), "tag size too small") {
		t.Errorf("expect an error when tag size is too small")
	}
	// tag too big
	_, err = hmac.New("SHA1", random.GetRandomBytes(16), 21)
	if err == nil || !strings.Contains(err.Error(), "tag size too big") {
		t.Errorf("expect an error when tag size is too big")
	}
	_, err = hmac.New("SHA256", random.GetRandomBytes(16), 33)
	if err == nil || !strings.Contains(err.Error(), "tag size too big") {
		t.Errorf("expect an error when tag size is too big")
	}
	_, err = hmac.New("SHA512", random.GetRandomBytes(16), 65)
	if err == nil || !strings.Contains(err.Error(), "tag size too big") {
		t.Errorf("expect an error when tag size is too big")
	}
}

func TestHMACWithNilHashFunc(t *testing.T) {
	cipher, err := hmac.New("SHA256", random.GetRandomBytes(32), 32)
	if err != nil {
		t.Fatalf("hmac.New() err = %v", err)
	}

	// Modify exported field.
	cipher.HashFunc = nil

	if _, err := cipher.ComputeMAC([]byte{}); err == nil {
		t.Errorf("cipher.ComputerMAC() err = nil, want not nil")
	}
}

func TestHMAComputeVerifyWithNilInput(t *testing.T) {
	cipher, err := hmac.New("SHA256", random.GetRandomBytes(16), 32)
	if err != nil {
		t.Errorf("unexpected error when creating new HMAC")
	}
	tag, err := cipher.ComputeMAC(nil)
	if err != nil {
		t.Errorf("cipher.ComputeMAC(nil) failed: %v", err)
	}
	if err := cipher.VerifyMAC(tag, nil); err != nil {
		t.Errorf("cipher.VerifyMAC(tag, nil) failed: %v", err)
	}
}

func TestVerifyMACWithInvalidInput(t *testing.T) {
	cipher, err := hmac.New("SHA256", random.GetRandomBytes(16), 32)
	if err != nil {
		t.Errorf("unexpected error when creating new HMAC")
	}
	if err := cipher.VerifyMAC(nil, []byte{1}); err == nil {
		t.Errorf("expect an error when mac is nil")
	}
	if err := cipher.VerifyMAC([]byte{1}, nil); err == nil {
		t.Errorf("expect an error when data is nil")
	}
	if err := cipher.VerifyMAC(nil, nil); err == nil {
		t.Errorf("cipher.VerifyMAC(nil, nil) succeeded unexpectedly")
	}
}

func TestHMACModification(t *testing.T) {
	for _, test := range hmacTests {
		t.Run(test.desc, func(t *testing.T) {
			cipher, err := hmac.New(test.hashAlg, test.key, test.tagSize)
			if err != nil {
				t.Fatalf("hmac.New() err = %q, want nil", err)
			}
			mac, err := cipher.ComputeMAC(test.data)
			if err != nil {
				t.Fatalf("cipher.ComputeMAC() err = %q, want nil", err)
			}
			for i := 0; i < len(mac); i++ {
				tmp := mac[i]
				for j := 0; j < 8; j++ {
					mac[i] ^= 1 << uint8(j)
					err := cipher.VerifyMAC(mac, test.data)
					if err == nil {
						t.Errorf("cipher.VerifyMAC() of valid mac modified at position (%d, %d) is nil, want error", i, j)
					}
					mac[i] = tmp
				}
			}
		})
	}
}

func TestHMACTruncation(t *testing.T) {
	for i, test := range hmacTests {
		t.Run(test.desc, func(t *testing.T) {
			cipher, err := hmac.New(test.hashAlg, test.key, test.tagSize)
			if err != nil {
				t.Fatalf("hmac.New() err = %q, want nil", err)
			}
			mac, err := cipher.ComputeMAC(test.data)
			if err != nil {
				t.Fatalf("cipher.ComputeMAC() err = %q, want nil", err)
			}
			for truncatedLen := 1; i < len(mac); i++ {
				truncatedMAC := mac[:truncatedLen]
				err := cipher.VerifyMAC(truncatedMAC, test.data)
				if err == nil {
					t.Errorf("cipher.VerifyMAC() of a valid mac truncated to %d bytes is nil, want error",
						truncatedLen)
				}
			}
		})
	}
}