github.com/trustbloc/kms-go@v1.1.2/crypto/tinkcrypto/primitive/secp256k1/secp256k1_verifier_key_manager_test.go (about) 1 /* 2 Copyright SecureKey Technologies Inc. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package secp256k1_test 8 9 import ( 10 "testing" 11 12 "github.com/golang/protobuf/proto" 13 "github.com/google/tink/go/core/registry" 14 commonpb "github.com/google/tink/go/proto/common_go_proto" 15 "github.com/google/tink/go/testutil" 16 "github.com/stretchr/testify/require" 17 18 secp256k1pb "github.com/trustbloc/kms-go/crypto/tinkcrypto/primitive/proto/secp256k1_go_proto" 19 ) 20 21 const secp256k1VerifierTypeURL = "type.googleapis.com/google.crypto.tink.secp256k1PublicKey" 22 23 func TestSecp256K1VerifyGetPrimitiveBasic(t *testing.T) { 24 testParams := genValidSecp256k1Params() 25 km, err := registry.GetKeyManager(secp256k1VerifierTypeURL) 26 require.NoError(t, err, "cannot obtain secp256K1Verifier key manager") 27 28 for i := 0; i < len(testParams); i++ { 29 serializedKey, e := proto.Marshal(NewRandomSecp256K1PublicKey(testParams[i].hashType, testParams[i].curve)) 30 require.NoError(t, e) 31 32 _, err = km.Primitive(serializedKey) 33 require.NoErrorf(t, err, "unexpect error in test case %d ", i) 34 } 35 } 36 37 func TestECDSAVerifyGetPrimitiveWithInvalidInput(t *testing.T) { 38 testParams := genInvalidSecp256k1Params() 39 km, err := registry.GetKeyManager(secp256k1VerifierTypeURL) 40 require.NoError(t, err) 41 42 for i := 0; i < len(testParams); i++ { 43 serializedKey, e := proto.Marshal(NewRandomSecp256K1PrivateKey(testParams[i].hashType, testParams[i].curve)) 44 if testParams[i].curve != secp256k1pb.BitcoinCurveType_INVALID_BITCOIN_CURVE { 45 require.NoError(t, e) 46 } 47 48 _, err = km.Primitive(serializedKey) 49 require.Errorf(t, err, "expect an error in test case %d", i) 50 } 51 52 // invalid version 53 key := NewRandomSecp256K1PublicKey(commonpb.HashType_SHA256, secp256k1pb.BitcoinCurveType_SECP256K1) 54 key.Version = testutil.ECDSAVerifierKeyVersion + 1 55 56 serializedKey, e := proto.Marshal(key) 57 require.NoError(t, e) 58 59 _, err = km.Primitive(serializedKey) 60 require.Error(t, err, "expect an error when version is invalid") 61 62 // nil input 63 _, err = km.Primitive(nil) 64 require.Error(t, err, "expect an error when input is nil") 65 66 _, err = km.Primitive([]byte{}) 67 require.Error(t, err, "expect an error when input is empty slice") 68 } 69 70 // NewRandomSecp256K1PublicKey creates an Secp256K1PublicKey with randomly generated key material. 71 func NewRandomSecp256K1PublicKey(hashType commonpb.HashType, 72 curve secp256k1pb.BitcoinCurveType) *secp256k1pb.Secp256K1PublicKey { 73 return NewRandomSecp256K1PrivateKey(hashType, curve).PublicKey 74 }