xref: /aosp_15_r20/external/tink/go/aead/subtle/polyval_test.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1// Copyright 2020 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
17package subtle_test
18
19import (
20	"bytes"
21	"encoding/hex"
22	"testing"
23
24	"github.com/google/tink/go/aead/subtle"
25	"github.com/google/tink/go/subtle/random"
26)
27
28// These test vectors have been taken from Appendix C in go/rfc/8452.
29var testVectors = []struct {
30	Key, Input, Hash string
31}{
32	{ // Test Case 0
33		Key:   "25629347589242761d31f826ba4b757b",
34		Input: "4f4f95668c83dfb6401762bb2d01a262d1a24ddd2721d006bbe45f20d3c9f362",
35		Hash:  "f7a3b47b846119fae5b7866cf5e5b77e",
36	},
37	{ // Test Case 1
38		Key:   "d9b360279694941ac5dbc6987ada7377",
39		Input: "00000000000000000000000000000000",
40		Hash:  "00000000000000000000000000000000",
41	},
42	{ // Test Case 2
43		Key:   "d9b360279694941ac5dbc6987ada7377",
44		Input: "01000000000000000000000000000000000000000000000040",
45		Hash:  "eb93b7740962c5e49d2a90a7dc5cec74",
46	},
47	{ // Test Case 3
48		Key:   "d9b360279694941ac5dbc6987ada7377",
49		Input: "01000000000000000000000000000000000000000000000060",
50		Hash:  "48eb6c6c5a2dbe4a1dde508fee06361b",
51	},
52	{ // Test Case 4
53		Key:   "d9b360279694941ac5dbc6987ada7377",
54		Input: "01000000000000000000000000000000000000000000000080",
55		Hash:  "20806c26e3c1de019e111255708031d6",
56	},
57	{ // Test Case 5
58		Key:   "d9b360279694941ac5dbc6987ada7377",
59		Input: "010000000000000000000000000000000200000000000000000000000000000000000000000000000001",
60		Hash:  "ce6edc9a50b36d9a98986bbf6a261c3b",
61	},
62	{ // Test Case 6
63		Key:   "0533fd71f4119257361a3ff1469dd4e5",
64		Input: "489c8fde2be2cf97e74e932d4ed87d00c9882e5386fd9f92ec00000000000000780000000000000048",
65		Hash:  "bf160bc9ded8c63057d2c38aae552fb4",
66	},
67	{ // Test Case 7
68		Key:   "64779ab10ee8a280272f14cc8851b727",
69		Input: "0da55210cc1c1b0abde3b2f204d1e9f8b06bc47f0000000000000000000000001db2316fd568378da107b52b00000000a00000000000000060",
70		Hash:  "cc86ee22c861e1fd474c84676b42739c",
71	},
72	{ // Test Case 8
73		Key:   "27c2959ed4daea3b1f52e849478de376",
74		Input: "f37de21c7ff901cfe8a69615a93fdf7a98cad481796245709f0000000000000021702de0de18baa9c9596291b0846600c80000000000000078",
75		Hash:  "c4fa5e5b713853703bcf8e6424505fa5",
76	},
77	{ // Test Case 9
78		Key:   "670b98154076ddb59b7a9137d0dcc0f0",
79		Input: "9c2159058b1f0fe91433a5bdc20e214eab7fecef4454a10ef0657df21ac70000b202b370ef9768ec6561c4fe6b7e7296fa850000000000000000000000000000f00000000000000090",
80		Hash:  "4e4108f09f41d797dc9256f8da8d58c7",
81	},
82	{ // Test Case 10
83		Key:   "cb8c3aa3f8dbaeb4b28a3e86ff6625f8",
84		Input: "734320ccc9d9bbbb19cb81b2af4ecbc3e72834321f7aa0f70b7282b4f33df23f16754100000000000000000000000000ced532ce4159b035277d4dfbb7db62968b13cd4eec00000000000000000000001801000000000000a8",
85		Hash:  "ffd503c7dd712eb3791b7114b17bb0cf",
86	},
87}
88
89func TestPolyval(t *testing.T) {
90	for id, tc := range testVectors {
91		key, err := hex.DecodeString(tc.Key)
92		if err != nil {
93			t.Errorf("cannot decode key in test case %d: %s", id, err)
94			continue
95		}
96		input, err := hex.DecodeString(tc.Input)
97		if err != nil {
98			t.Errorf("cannot decode aad in test case %d: %s", id, err)
99			continue
100		}
101		expectedHash, err := hex.DecodeString(tc.Hash)
102		if err != nil {
103			t.Errorf("cannot decode msg in test case %d: %s", id, err)
104			continue
105		}
106
107		p, err := subtle.NewPolyval(key)
108		if err != nil {
109			t.Errorf("Unexpected error: %v", err)
110			continue
111		}
112
113		p.Update(input)
114		hash := p.Finish()
115		actualHash := hash[:]
116
117		if !bytes.Equal(actualHash, expectedHash) {
118			t.Errorf("Hash values don't match in test case %d: actual %s, expected %s",
119				id, hex.EncodeToString(actualHash), hex.EncodeToString(expectedHash))
120		}
121	}
122}
123
124func TestPolyvalRejectsInvalidKeyLength(t *testing.T) {
125	invalidKeySizes := []uint32{4, 8, 12, 15, 17, 24, 32}
126
127	for id, keySize := range invalidKeySizes {
128		key := random.GetRandomBytes(keySize)
129
130		_, err := subtle.NewPolyval(key)
131		if err == nil {
132			t.Errorf("Expected error with invalid key-size %d case %d", keySize, id)
133		}
134	}
135}
136