github.com/trustbloc/kms-go@v1.1.2/crypto/tinkcrypto/primitive/bbs/bbs_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 bbs
     8  
     9  import (
    10  	"github.com/trustbloc/bbs-signature-go/bbs12381g2pub"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/golang/protobuf/proto"
    15  	commonpb "github.com/google/tink/go/proto/common_go_proto"
    16  	"github.com/google/tink/go/subtle"
    17  	"github.com/stretchr/testify/require"
    18  
    19  	bbspb "github.com/trustbloc/kms-go/crypto/tinkcrypto/primitive/proto/bbs_go_proto"
    20  )
    21  
    22  func TestVerifierKeyManager_Primitive(t *testing.T) {
    23  	km := newBBSVerifierKeyManager()
    24  
    25  	t.Run("Test verifier key manager Primitive() with empty serialized key", func(t *testing.T) {
    26  		p, err := km.Primitive([]byte(""))
    27  		require.EqualError(t, err, errInvalidBBSVerifierKey.Error(),
    28  			"newBBSVerifierKeyManager primitive from empty serialized key must fail")
    29  		require.Empty(t, p)
    30  	})
    31  
    32  	t.Run("Test public key manager Primitive() with bad serialize key", func(t *testing.T) {
    33  		p, err := km.Primitive([]byte("bad.data"))
    34  		require.EqualError(t, err, errInvalidBBSVerifierKey.Error(),
    35  			"newBBSVerifierKeyManager primitive from bad serialized key must fail")
    36  		require.Empty(t, p)
    37  	})
    38  
    39  	flagTests := []struct {
    40  		tcName     string
    41  		version    uint32
    42  		hashType   commonpb.HashType
    43  		curveType  bbspb.BBSCurveType
    44  		groupField bbspb.GroupField
    45  	}{
    46  		{
    47  			tcName:     "verifier key manager Primitive() success",
    48  			version:    0,
    49  			hashType:   commonpb.HashType_SHA256,
    50  			curveType:  bbspb.BBSCurveType_BLS12_381,
    51  			groupField: bbspb.GroupField_G2,
    52  		},
    53  		{
    54  			tcName:     "verifier key manager Primitive() using key with bad version",
    55  			version:    9999,
    56  			hashType:   commonpb.HashType_SHA256,
    57  			curveType:  bbspb.BBSCurveType_BLS12_381,
    58  			groupField: bbspb.GroupField_G2,
    59  		},
    60  		{
    61  			tcName:     "verifier key manager Primitive() using key with bad hash type",
    62  			version:    0,
    63  			hashType:   commonpb.HashType_UNKNOWN_HASH,
    64  			curveType:  bbspb.BBSCurveType_BLS12_381,
    65  			groupField: bbspb.GroupField_G2,
    66  		},
    67  		{
    68  			tcName:     "verifier key manager Primitive() using key with bad curve",
    69  			version:    0,
    70  			hashType:   commonpb.HashType_SHA256,
    71  			curveType:  bbspb.BBSCurveType_UNKNOWN_BBS_CURVE_TYPE,
    72  			groupField: bbspb.GroupField_G2,
    73  		},
    74  		{
    75  			tcName:     "signer key manager Primitive() using key with bad group",
    76  			version:    0,
    77  			hashType:   commonpb.HashType_SHA256,
    78  			curveType:  bbspb.BBSCurveType_BLS12_381,
    79  			groupField: bbspb.GroupField_UNKNOWN_GROUP_FIELD,
    80  		},
    81  	}
    82  
    83  	for _, tc := range flagTests {
    84  		tt := tc
    85  		t.Run("Test "+tt.tcName, func(t *testing.T) {
    86  			h := tt.hashType
    87  			v := tt.version
    88  
    89  			// temporarily reset hashType if its unknown type so manual key creation below doesn't fail
    90  			if tt.hashType == commonpb.HashType_UNKNOWN_HASH {
    91  				h = commonpb.HashType_SHA256
    92  			}
    93  
    94  			pubKey, _, err := bbs12381g2pub.GenerateKeyPair(subtle.GetHashFunc(h.String()), nil)
    95  			require.NoError(t, err)
    96  
    97  			pubKeyBytes, err := pubKey.Marshal()
    98  			require.NoError(t, err)
    99  
   100  			h = tt.hashType
   101  
   102  			// set back hashType if it was unknown to proceed with the test
   103  			if tt.hashType.String() == commonpb.HashType_UNKNOWN_HASH.String() {
   104  				h = tt.hashType
   105  			}
   106  
   107  			pubKeyProto := &bbspb.BBSPublicKey{
   108  				Version: v,
   109  				Params: &bbspb.BBSParams{
   110  					HashType: h,
   111  					Curve:    tt.curveType,
   112  					Group:    tt.groupField,
   113  				},
   114  				KeyValue: pubKeyBytes,
   115  			}
   116  
   117  			sPubKey, err := proto.Marshal(pubKeyProto)
   118  			require.NoError(t, err)
   119  
   120  			p, err := km.Primitive(sPubKey)
   121  			if strings.Contains(tt.tcName, "success") {
   122  				require.NoError(t, err)
   123  				require.NotEmpty(t, p)
   124  				return
   125  			}
   126  
   127  			require.Errorf(t, err, tt.tcName)
   128  			require.Empty(t, p)
   129  		})
   130  	}
   131  }
   132  
   133  func TestVerifierKeyManager_DoesSupport(t *testing.T) {
   134  	km := newBBSVerifierKeyManager()
   135  	require.False(t, km.DoesSupport("bad/url"))
   136  	require.True(t, km.DoesSupport(bbsVerifierKeyTypeURL))
   137  }
   138  
   139  func TestVerifierKeyManager_NewKeyAndNewKeyData(t *testing.T) {
   140  	km := newBBSVerifierKeyManager()
   141  
   142  	t.Run("Test public key manager NewKey()", func(t *testing.T) {
   143  		k, err := km.NewKey(nil)
   144  		require.EqualError(t, err, "bbs_verifier_key_manager: NewKey not implemented")
   145  		require.Empty(t, k)
   146  	})
   147  
   148  	t.Run("Test private key manager NewKeyData()", func(t *testing.T) {
   149  		p, err := km.NewKeyData(nil)
   150  		require.EqualError(t, err, "bbs_verifier_key_manager: NewKeyData not implemented")
   151  		require.Empty(t, p)
   152  	})
   153  }