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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
|
// Copyright 2025 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 aescmac_test
import (
"bytes"
"encoding/hex"
"fmt"
"slices"
"testing"
"github.com/tink-crypto/tink-go/v2/core/cryptofmt"
"github.com/tink-crypto/tink-go/v2/insecuresecretdataaccess"
"github.com/tink-crypto/tink-go/v2/internal/internalapi"
"github.com/tink-crypto/tink-go/v2/keyset"
"github.com/tink-crypto/tink-go/v2/mac/aescmac"
"github.com/tink-crypto/tink-go/v2/mac"
"github.com/tink-crypto/tink-go/v2/secretdata"
"github.com/tink-crypto/tink-go/v2/testutil"
)
type testVector struct {
name string
keyBytes []byte
message []byte
tag []byte
variant aescmac.Variant
idRequirement uint32
tagSize uint32
}
func mustHexDecode(t *testing.T, s string) []byte {
t.Helper()
b, err := hex.DecodeString(s)
if err != nil {
t.Fatalf("failed to decode hex string %q: %v", s, err)
}
return b
}
func testVectors(t *testing.T) []testVector {
testVectors := []testVector{}
for _, tagSize := range []uint32{10, 16} {
for _, variant := range []struct {
value aescmac.Variant
prefix []byte
idRequirement uint32
}{
{aescmac.VariantNoPrefix, nil, 0},
{aescmac.VariantTink, slices.Concat([]byte{cryptofmt.TinkStartByte}, []byte{0x01, 0x02, 0x03, 0x04}), 0x01020304},
{aescmac.VariantCrunchy, slices.Concat([]byte{cryptofmt.LegacyStartByte}, []byte{0x01, 0x02, 0x03, 0x04}), 0x01020304},
} {
// https://github.com/C2SP/wycheproof/blob/cd27d6419bedd83cbd24611ec54b6d4bfdb0cdca/testvectors/aes_cmac_test.json#L1851
testVectors = append(testVectors, testVector{
name: fmt.Sprintf("test_vector_1: Variant=%s TagSize=%d", variant.value, tagSize),
keyBytes: mustHexDecode(t, "7bf9e536b66a215c22233fe2daaa743a898b9acb9f7802de70b40e3d6e43ef97"),
message: nil,
tag: slices.Concat(variant.prefix, mustHexDecode(t, "736c7b56957db774c5ddf7c7a70ba8a8")[:tagSize]),
variant: variant.value,
idRequirement: variant.idRequirement,
tagSize: tagSize,
})
// https://github.com/C2SP/wycheproof/blob/cd27d6419bedd83cbd24611ec54b6d4bfdb0cdca/testvectors/aes_cmac_test.json#L1860
testVectors = append(testVectors, testVector{
name: fmt.Sprintf("test_vector_2: Variant=%s TagSize=%d", variant.value, tagSize),
keyBytes: mustHexDecode(t, "e754076ceab3fdaf4f9bcab7d4f0df0cbbafbc87731b8f9b7cd2166472e8eebc"),
message: mustHexDecode(t, "40"),
tag: slices.Concat(variant.prefix, mustHexDecode(t, "9d47482c2d9252bace43a75a8335b8b8")[:tagSize]),
variant: variant.value,
idRequirement: variant.idRequirement,
tagSize: tagSize,
})
// https://github.com/C2SP/wycheproof/blob/cd27d6419bedd83cbd24611ec54b6d4bfdb0cdca/testvectors/aes_cmac_test.json#L1968
testVectors = append(testVectors, testVector{
name: fmt.Sprintf("test_vector_3: Variant=%s TagSize=%d", variant.value, tagSize),
keyBytes: mustHexDecode(t, "abab815d51df29f740e4e2079fb798e0152836e6ab57d1536ae8929e52c06eb8"),
message: mustHexDecode(t, "f0058d412a104e53d820b95a7f"),
tag: slices.Concat(variant.prefix, mustHexDecode(t, "1fa24c6625a0f8e1fc37827ac84d3cc4")[:tagSize]),
variant: variant.value,
idRequirement: variant.idRequirement,
tagSize: tagSize,
})
}
// Legacy,
prefix := slices.Concat([]byte{cryptofmt.LegacyStartByte}, []byte{0x01, 0x02, 0x03, 0x04})
testVectors = append(testVectors, testVector{
name: fmt.Sprintf("test_vector_1: Variant=%s TagSize=%d", aescmac.VariantLegacy, tagSize),
keyBytes: mustHexDecode(t, "7bf9e536b66a215c22233fe2daaa743a898b9acb9f7802de70b40e3d6e43ef97"),
message: nil,
tag: slices.Concat(prefix, mustHexDecode(t, "f463275d38b29925a8e7c9841d20fc71")[:tagSize]),
variant: aescmac.VariantLegacy,
idRequirement: 0x01020304,
tagSize: tagSize,
})
testVectors = append(testVectors, testVector{
name: fmt.Sprintf("test_vector_1: Variant=%s TagSize=%d", aescmac.VariantLegacy, tagSize),
keyBytes: mustHexDecode(t, "e754076ceab3fdaf4f9bcab7d4f0df0cbbafbc87731b8f9b7cd2166472e8eebc"),
message: mustHexDecode(t, "40"),
tag: slices.Concat(prefix, mustHexDecode(t, "9d651692e63c5ee8197e4bbfeafcf264")[:tagSize]),
variant: aescmac.VariantLegacy,
idRequirement: 0x01020304,
tagSize: tagSize,
})
testVectors = append(testVectors, testVector{
name: fmt.Sprintf("test_vector_1: Variant=%s TagSize=%d", aescmac.VariantLegacy, tagSize),
keyBytes: mustHexDecode(t, "abab815d51df29f740e4e2079fb798e0152836e6ab57d1536ae8929e52c06eb8"),
message: mustHexDecode(t, "f0058d412a104e53d820b95a7f"),
tag: slices.Concat(prefix, mustHexDecode(t, "f6b809c40ca31e5dbbb72f364e58cd38")[:tagSize]),
variant: aescmac.VariantLegacy,
idRequirement: 0x01020304,
tagSize: tagSize,
})
}
return testVectors
}
func TestMACTestVectors(t *testing.T) {
for _, tc := range testVectors(t) {
t.Run(tc.name, func(t *testing.T) {
params, err := aescmac.NewParameters(aescmac.ParametersOpts{
KeySizeInBytes: len(tc.keyBytes),
TagSizeInBytes: int(tc.tagSize),
Variant: tc.variant,
})
if err != nil {
t.Fatalf("aescmac.NewParameters(%v, %v) err = %v, want nil", tc.variant, 16, err)
}
key, err := aescmac.NewKey(secretdata.NewBytesFromData(tc.keyBytes, insecuresecretdataaccess.Token{}), params, tc.idRequirement)
if err != nil {
t.Fatalf("aescmac.NewKey(%v, %v, %v) err = %v, want nil", tc.keyBytes, params, tc.idRequirement, err)
}
mac, err := aescmac.NewMAC(key, internalapi.Token{})
if err != nil {
t.Fatalf("aescmac.NewMAC(%v, %v) err = %v, want nil", key, internalapi.Token{}, err)
}
tag, err := mac.ComputeMAC(tc.message)
if err != nil {
t.Fatalf("mac.ComputeMAC(%v) err = %v, want nil", tc.message, err)
}
if !bytes.Equal(tag, tc.tag) {
t.Errorf("mac.ComputeMAC(%v) = %x, want %x", tc.message, tag, tc.tag)
}
if err := mac.VerifyMAC(tag, tc.message); err != nil {
t.Errorf("mac.VerifyMAC(%v, %v) err = %v, want nil", tag, tc.message, err)
}
})
}
}
func TestMACFromPublicAPITestVectors(t *testing.T) {
for _, tc := range testVectors(t) {
t.Run(tc.name, func(t *testing.T) {
params, err := aescmac.NewParameters(aescmac.ParametersOpts{
KeySizeInBytes: len(tc.keyBytes),
TagSizeInBytes: int(tc.tagSize),
Variant: tc.variant,
})
if err != nil {
t.Fatalf("aescmac.NewParameters(%v, %v) err = %v, want nil", tc.variant, 16, err)
}
key, err := aescmac.NewKey(secretdata.NewBytesFromData(tc.keyBytes, insecuresecretdataaccess.Token{}), params, tc.idRequirement)
if err != nil {
t.Fatalf("aescmac.NewKey(%v, %v, %v) err = %v, want nil", tc.keyBytes, params, tc.idRequirement, err)
}
km := keyset.NewManager()
id, err := km.AddKey(key)
if err != nil {
t.Fatalf("km.AddKey(%v) err = %v, want nil", key, err)
}
if err := km.SetPrimary(id); err != nil {
t.Fatalf("km.SetPrimary(%v) err = %v, want nil", id, err)
}
handle, err := km.Handle()
if err != nil {
t.Fatalf("km.Handle() err = %v, want nil", err)
}
mac, err := mac.New(handle)
if err != nil {
t.Fatalf("mac.New(handle) err = %v, want nil", err)
}
tag, err := mac.ComputeMAC(tc.message)
if err != nil {
t.Fatalf("mac.ComputeMAC(%v) err = %v, want nil", tc.message, err)
}
if !bytes.Equal(tag, tc.tag) {
t.Errorf("mac.ComputeMAC(%v) = %v, want %v", tc.message, tag, tc.tag)
}
if err := mac.VerifyMAC(tag, tc.message); err != nil {
t.Errorf("mac.VerifyMAC(%v, %v) err = %v, want nil", tag, tc.message, err)
}
})
}
}
func TestDecryptFailsWithInvalidInputs(t *testing.T) {
for _, variant := range []aescmac.Variant{
aescmac.VariantNoPrefix,
aescmac.VariantTink,
aescmac.VariantCrunchy,
aescmac.VariantLegacy,
} {
t.Run(variant.String(), func(t *testing.T) {
params, err := aescmac.NewParameters(aescmac.ParametersOpts{
KeySizeInBytes: 32,
TagSizeInBytes: 16,
Variant: variant,
})
if err != nil {
t.Fatalf("aescmac.NewParameters() err = %v, want nil", err)
}
keyBytes := secretdata.NewBytesFromData([]byte("01010101010101010101010101010101"), insecuresecretdataaccess.Token{})
key, err := aescmac.NewKey(keyBytes, params, 0)
if err != nil {
t.Fatalf("aescmac.NewKey() err = %v, want nil", err)
}
m, err := aescmac.NewMAC(key, internalapi.Token{})
if err != nil {
t.Fatalf("aescmac.NewMAC(%v, %v) err = %v, want nil", key, internalapi.Token{}, err)
}
message := []byte("Some data to sign.")
tag, err := m.ComputeMAC(message)
if err != nil {
t.Fatalf("m.ComputeMAC(message) err = %v, want nil", err)
}
prefix := tag[:len(key.OutputPrefix())]
rawTag := tag[len(prefix):]
// Invalid prefix.
if len(prefix) > 0 {
wrongPrefix := []byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF}
if err := m.VerifyMAC(slices.Concat(wrongPrefix, rawTag), message); err == nil {
t.Errorf("m.VerifyMAC() err = nil, want error")
}
}
// Corrupted tag.
wrongTag := bytes.Clone(rawTag)
wrongTag[0] ^= 1
if err := m.VerifyMAC(slices.Concat(prefix, wrongTag), message); err == nil {
t.Errorf("m.VerifyMAC() err = nil, want error")
}
// Truncated tag.
for i := 1; i < len(tag); i++ {
if err := m.VerifyMAC(tag[:i], message); err == nil {
t.Errorf("m.VerifyMAC(tag[:%d], message) err = nil, want error", i)
}
}
// Invalid message.
if err := m.VerifyMAC(tag, []byte("invalid")); err == nil {
t.Errorf("m.VerifyMAC() err = nil, want error")
}
})
}
}
type AESCMACSuite struct {
Algorithm string
GeneratorVersion string
NumberOfTests uint32
TestGroups []*testgroup
}
type testgroup struct {
KeySize uint32
TagSize uint32
Type string
Tests []*testcase
}
type testcase struct {
Comment string
Key string
Msg string
Result string
Tag string
TcID uint32
}
func TestVectorsWycheproof(t *testing.T) {
suite := new(AESCMACSuite)
if err := testutil.PopulateSuite(suite, "aes_cmac_test.json"); err != nil {
t.Fatalf("testutil.PopulateSuite: %v", err)
}
for _, g := range suite.TestGroups {
for _, tc := range g.Tests {
if g.KeySize != 256 {
t.Logf("Key size for test case %d (%s) is not 256, but %d", tc.TcID, tc.Comment, g.KeySize)
continue
}
if g.TagSize%8 != 0 {
t.Errorf("Requested tag size for test case %d (%s) is not a multiple of 8, but %d", tc.TcID, tc.Comment, g.TagSize)
continue
}
keyBytes := mustHexDecode(t, tc.Key)
msg := mustHexDecode(t, tc.Msg)
tag := mustHexDecode(t, tc.Tag)
t.Run(fmt.Sprintf("test_case_%d", tc.TcID), func(t *testing.T) {
params := mustCreateParameters(t, aescmac.ParametersOpts{
KeySizeInBytes: len(keyBytes),
TagSizeInBytes: int(g.TagSize / 8),
Variant: aescmac.VariantNoPrefix,
})
key := mustCreateKey(t, secretdata.NewBytesFromData(keyBytes, insecuresecretdataaccess.Token{}), params, 0)
valid := tc.Result == "valid"
mac, err := aescmac.NewMAC(key, internalapi.Token{})
if valid && err != nil {
t.Fatalf("aescmac.NewMAC(%v, %v) err = %v, want nil", key, internalapi.Token{}, err)
}
if err == nil {
res, err := mac.ComputeMAC(msg)
if valid && err != nil {
t.Errorf("mac.ComputeMAC(msg) err = %v, want nil", err)
}
if valid && !bytes.Equal(res, tag) {
t.Errorf("mac.ComputeMAC(msg) = %v, want %v", res, tag)
}
if !valid && bytes.Equal(res, tag) && err == nil {
t.Errorf("Compute AES-CMAC and invalid expected (%s) match:\nComputed: %q\nExpected: %q", tc.Comment, res, tag)
}
err = mac.VerifyMAC(tag, msg)
if valid && err != nil {
t.Errorf("mac.VerifyMAC(tag, msg) err = %v, want nil", err)
}
if !valid && err == nil {
t.Errorf("mac.VerifyMAC(tag, msg) err = nil, want error")
}
}
})
}
}
}
|