github.com/trustbloc/kms-go@v1.1.2/crypto/tinkcrypto/primitive/secp256k1/secp256k1_key_templates_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 "fmt" 11 "testing" 12 13 "github.com/google/tink/go/keyset" 14 tinkpb "github.com/google/tink/go/proto/tink_go_proto" 15 "github.com/stretchr/testify/require" 16 17 "github.com/trustbloc/kms-go/crypto/tinkcrypto/primitive/secp256k1" 18 ) 19 20 func TestKeyTemplates(t *testing.T) { 21 derKeyTemplate, err := secp256k1.DERKeyTemplate() 22 require.NoError(t, err) 23 24 ieeeKeyTempalte, err := secp256k1.IEEEP1363KeyTemplate() 25 require.NoError(t, err) 26 27 testCases := []struct { 28 name string 29 template *tinkpb.KeyTemplate 30 }{ 31 { 32 name: "SECP256K1", 33 template: derKeyTemplate, 34 }, 35 { 36 name: "SECP256K1", 37 template: ieeeKeyTempalte, 38 }, 39 } 40 41 for _, tc := range testCases { 42 t.Run(tc.name, func(t *testing.T) { 43 var kh *keyset.Handle 44 45 kh, err = keyset.NewHandle(tc.template) 46 require.NoError(t, err) 47 48 err = testSignVerify(kh) 49 require.NoError(t, err) 50 }) 51 } 52 } 53 54 func testSignVerify(privateHandle *keyset.Handle) error { 55 signer, err := secp256k1.NewSigner(privateHandle) 56 if err != nil { 57 return fmt.Errorf("signature.NewSigner(privateHandle) failed: %w", err) 58 } 59 60 publicHandle, err := privateHandle.Public() 61 if err != nil { 62 return fmt.Errorf("privateHandle.Public() failed: %w", err) 63 } 64 65 verifier, err := secp256k1.NewVerifier(publicHandle) 66 if err != nil { 67 return fmt.Errorf("signature.NewVerifier(publicHandle) failed: %w", err) 68 } 69 70 testInputs := []struct { 71 message1 []byte 72 message2 []byte 73 }{ 74 { 75 message1: []byte("this data needs to be signed"), 76 message2: []byte("this data needs to be signed"), 77 }, 78 { 79 message1: []byte(""), 80 message2: []byte(""), 81 }, 82 { 83 message1: []byte(""), 84 message2: nil, 85 }, 86 { 87 message1: nil, 88 message2: []byte(""), 89 }, 90 { 91 message1: nil, 92 message2: nil, 93 }, 94 } 95 96 for _, ti := range testInputs { 97 sig, e := signer.Sign(ti.message1) 98 if e != nil { 99 return fmt.Errorf("signer.Sign(ti.message1) failed: %w", e) 100 } 101 102 if err = verifier.Verify(sig, ti.message2); err != nil { 103 return fmt.Errorf("verifier.Verify(sig, ti.message2) failed: %w", err) 104 } 105 } 106 107 return nil 108 }