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 prf_test 18 19import ( 20 "bytes" 21 "testing" 22 23 "github.com/google/tink/go/keyset" 24 "github.com/google/tink/go/prf" 25 tinkpb "github.com/google/tink/go/proto/tink_go_proto" 26) 27 28func TestKeyTemplates(t *testing.T) { 29 var testCases = []struct { 30 name string 31 template *tinkpb.KeyTemplate 32 }{ 33 {name: "HMAC_SHA256_PRF", 34 template: prf.HMACSHA256PRFKeyTemplate()}, 35 {name: "HMAC_SHA512_PRF", 36 template: prf.HMACSHA512PRFKeyTemplate()}, 37 {name: "HKDF_SHA256", 38 template: prf.HKDFSHA256PRFKeyTemplate()}, 39 {name: "AES_CMAC_PRF", 40 template: prf.AESCMACPRFKeyTemplate()}, 41 } 42 for _, tc := range testCases { 43 t.Run(tc.name, func(t *testing.T) { 44 handle, err := keyset.NewHandle(tc.template) 45 if err != nil { 46 t.Errorf("keyset.NewHandle(tc.template) failed: %s", err) 47 } 48 prfset, err := prf.NewPRFSet(handle) 49 if err != nil { 50 t.Errorf("prf.NewPRFSet(handle) failed: %s", err) 51 } 52 53 var testInputs = []struct { 54 message1 []byte 55 message2 []byte 56 }{ 57 { 58 message1: []byte("this data needs to be authenticated"), 59 message2: []byte("this data needs to be authenticated"), 60 }, { 61 message1: []byte(""), 62 message2: []byte(""), 63 }, { 64 message1: []byte(""), 65 message2: nil, 66 }, { 67 message1: nil, 68 message2: []byte(""), 69 }, { 70 message1: nil, 71 message2: nil, 72 }, 73 } 74 for _, ti := range testInputs { 75 output, err := prfset.ComputePrimaryPRF(ti.message1, 16) 76 if err != nil { 77 t.Errorf("prfset.ComputePrimaryPRF(ti.message1, 16) failed: %s", err) 78 } 79 if len(output) != 16 { 80 t.Errorf("len(output) = %d, want 16", len(output)) 81 } 82 output2, err := prfset.ComputePrimaryPRF(ti.message2, 16) 83 if err != nil { 84 t.Errorf("prfset.ComputePrimaryPRF(ti.message2, 16) failed: %s", err) 85 } 86 if !bytes.Equal(output2, output) { 87 t.Errorf("equivalent inputs did not produce equivalent outputs, got: %q, want: %q", output2, output) 88 } 89 } 90 }) 91 } 92} 93