1// Copyright 2018 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 signature_test 18 19import ( 20 "testing" 21 22 "google.golang.org/protobuf/proto" 23 "github.com/google/tink/go/core/registry" 24 "github.com/google/tink/go/testutil" 25 commonpb "github.com/google/tink/go/proto/common_go_proto" 26) 27 28func TestECDSAVerifyGetPrimitiveBasic(t *testing.T) { 29 testParams := genValidECDSAParams() 30 km, err := registry.GetKeyManager(testutil.ECDSAVerifierTypeURL) 31 if err != nil { 32 t.Errorf("cannot obtain ECDSAVerifier key manager: %s", err) 33 } 34 for i := 0; i < len(testParams); i++ { 35 serializedKey, _ := proto.Marshal(testutil.NewRandomECDSAPublicKey(testParams[i].hashType, testParams[i].curve)) 36 _, err := km.Primitive(serializedKey) 37 if err != nil { 38 t.Errorf("unexpect error in test case %d: %s ", i, err) 39 } 40 } 41} 42 43func TestECDSAVerifyWithInvalidPublicKeyFailsCreatingPrimitive(t *testing.T) { 44 km, err := registry.GetKeyManager(testutil.ECDSAVerifierTypeURL) 45 if err != nil { 46 t.Errorf("cannot obtain ECDSAVerifier key manager: %s", err) 47 } 48 pubKey := testutil.NewRandomECDSAPublicKey(commonpb.HashType_SHA256, commonpb.EllipticCurveType_NIST_P256) 49 pubKey.X = []byte{0, 32, 0} 50 pubKey.Y = []byte{0, 32, 0} 51 serializedPubKey, err := proto.Marshal(pubKey) 52 if err != nil { 53 t.Errorf("proto.Marhsal() err = %v, want nil", err) 54 } 55 if _, err := km.Primitive(serializedPubKey); err == nil { 56 t.Errorf("km.Primitive() err = nil, want error") 57 } 58} 59 60func TestECDSAVerifyGetPrimitiveWithInvalidInput(t *testing.T) { 61 testParams := genInvalidECDSAParams() 62 km, err := registry.GetKeyManager(testutil.ECDSAVerifierTypeURL) 63 if err != nil { 64 t.Errorf("cannot obtain ECDSAVerifier key manager: %s", err) 65 } 66 for i := 0; i < len(testParams); i++ { 67 serializedKey, _ := proto.Marshal(testutil.NewRandomECDSAPublicKey(testParams[i].hashType, testParams[i].curve)) 68 if _, err := km.Primitive(serializedKey); err == nil { 69 t.Errorf("expect an error in test case %d", i) 70 } 71 } 72 for _, tc := range genUnkownECDSAParams() { 73 k := testutil.NewRandomECDSAPublicKey(commonpb.HashType_SHA256, commonpb.EllipticCurveType_NIST_P256) 74 k.GetParams().Curve = tc.curve 75 k.GetParams().HashType = tc.hashType 76 serializedKey, _ := proto.Marshal(k) 77 if _, err := km.Primitive(serializedKey); err == nil { 78 t.Errorf("expect an error in test case with params: (curve = %q, hash = %q)", tc.curve, tc.hashType) 79 } 80 } 81 // invalid version 82 key := testutil.NewRandomECDSAPublicKey(commonpb.HashType_SHA256, 83 commonpb.EllipticCurveType_NIST_P256) 84 key.Version = testutil.ECDSAVerifierKeyVersion + 1 85 serializedKey, _ := proto.Marshal(key) 86 if _, err := km.Primitive(serializedKey); err == nil { 87 t.Errorf("expect an error when version is invalid") 88 } 89 // nil input 90 if _, err := km.Primitive(nil); err == nil { 91 t.Errorf("expect an error when input is nil") 92 } 93 if _, err := km.Primitive([]byte{}); err == nil { 94 t.Errorf("expect an error when input is empty slice") 95 } 96} 97