github.com/trustbloc/kms-go@v1.1.2/crypto/tinkcrypto/primitive/bbs/bbs_signer_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  	"strings"
    11  	"testing"
    12  
    13  	commonpb "github.com/google/tink/go/proto/common_go_proto"
    14  	tinkpb "github.com/google/tink/go/proto/tink_go_proto"
    15  	"github.com/stretchr/testify/require"
    16  	"google.golang.org/protobuf/proto"
    17  
    18  	bbspb "github.com/trustbloc/kms-go/crypto/tinkcrypto/primitive/proto/bbs_go_proto"
    19  )
    20  
    21  func TestBBSignerKeyManager_Primitive(t *testing.T) {
    22  	km := newBBSSignerKeyManager()
    23  
    24  	t.Run("Test signer key manager Primitive() with empty serialized key", func(t *testing.T) {
    25  		p, err := km.Primitive([]byte(""))
    26  		require.EqualError(t, err, errInvalidBBSSignerKey.Error(),
    27  			"bbsSignerKeyManager primitive from empty serialized key must fail")
    28  		require.Empty(t, p)
    29  	})
    30  
    31  	t.Run("Test signer key manager Primitive() with bad serialize key", func(t *testing.T) {
    32  		p, err := km.Primitive([]byte("bad.data"))
    33  		require.Contains(t, err.Error(), errInvalidBBSSignerKey.Error())
    34  		require.Contains(t, err.Error(), "invalid proto: proto:")
    35  		require.Contains(t, err.Error(), "cannot parse invalid wire-format data")
    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:     "signer 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:     "signer 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:     "signer 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:     "signer 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  			v := tt.version
    87  
    88  			privKeyProto := &bbspb.BBSPrivateKey{
    89  				Version: v,
    90  				PublicKey: &bbspb.BBSPublicKey{
    91  					Version: v,
    92  					Params: &bbspb.BBSParams{
    93  						HashType: tt.hashType,
    94  						Curve:    tt.curveType,
    95  						Group:    tt.groupField,
    96  					},
    97  				},
    98  			}
    99  
   100  			sPrivKey, err := proto.Marshal(privKeyProto)
   101  			require.NoError(t, err)
   102  
   103  			p, err := km.Primitive(sPrivKey)
   104  			if strings.Contains(tt.tcName, "success") {
   105  				require.NoError(t, err)
   106  				require.NotEmpty(t, p)
   107  				return
   108  			}
   109  
   110  			require.Errorf(t, err, tt.tcName)
   111  			require.Empty(t, p)
   112  		})
   113  	}
   114  }
   115  
   116  func TestBBSSignerKeyManager_DoesSupport(t *testing.T) {
   117  	km := newBBSSignerKeyManager()
   118  	require.False(t, km.DoesSupport("bad/url"))
   119  	require.True(t, km.DoesSupport(bbsSignerKeyTypeURL))
   120  }
   121  
   122  func TestBBSSignerKeyManager_NewKey(t *testing.T) {
   123  	km := newBBSSignerKeyManager()
   124  
   125  	t.Run("Test signer key manager NewKey() with nil key", func(t *testing.T) {
   126  		k, err := km.NewKey(nil)
   127  		require.EqualError(t, err, errInvalidBBSSignerKeyFormat.Error())
   128  		require.Empty(t, k)
   129  	})
   130  
   131  	t.Run("Test signer key manager NewKey() with bad serialize key", func(t *testing.T) {
   132  		p, err := km.NewKey([]byte("bad.data"))
   133  		require.Contains(t, err.Error(), errInvalidBBSSignerKey.Error())
   134  		require.Contains(t, err.Error(), "invalid proto: proto:")
   135  		require.Contains(t, err.Error(), "cannot parse invalid wire-format data")
   136  		require.Empty(t, p)
   137  	})
   138  
   139  	flagTests := []struct {
   140  		tcName     string
   141  		hashType   commonpb.HashType
   142  		curveType  bbspb.BBSCurveType
   143  		groupField bbspb.GroupField
   144  	}{
   145  		{
   146  			tcName:     "success signer key manager NewKey() and NewKeyData()",
   147  			hashType:   commonpb.HashType_SHA256,
   148  			curveType:  bbspb.BBSCurveType_BLS12_381,
   149  			groupField: bbspb.GroupField_G2,
   150  		},
   151  		{
   152  			tcName:     "signer key manager NewKey() and NewKeyData() using key with bad hash",
   153  			hashType:   commonpb.HashType_UNKNOWN_HASH,
   154  			curveType:  bbspb.BBSCurveType_BLS12_381,
   155  			groupField: bbspb.GroupField_G2,
   156  		},
   157  		{
   158  			tcName:     "signer key manager NewKey() and NewKeyData() using key with bad curve",
   159  			hashType:   commonpb.HashType_SHA256,
   160  			curveType:  bbspb.BBSCurveType_UNKNOWN_BBS_CURVE_TYPE,
   161  			groupField: bbspb.GroupField_G2,
   162  		},
   163  		{
   164  			tcName:     "signer key manager NewKey() and NewKeyData() using key with bad group",
   165  			hashType:   commonpb.HashType_SHA256,
   166  			curveType:  bbspb.BBSCurveType_BLS12_381,
   167  			groupField: bbspb.GroupField_UNKNOWN_GROUP_FIELD,
   168  		},
   169  	}
   170  
   171  	for _, tc := range flagTests {
   172  		tt := tc
   173  		t.Run("Test "+tt.tcName, func(t *testing.T) {
   174  			privKeyProto := &bbspb.BBSKeyFormat{
   175  				Params: &bbspb.BBSParams{
   176  					HashType: tt.hashType,
   177  					Curve:    tt.curveType,
   178  					Group:    tt.groupField,
   179  				},
   180  			}
   181  
   182  			sPrivKey, err := proto.Marshal(privKeyProto)
   183  			require.NoError(t, err)
   184  
   185  			p, err := km.NewKey(sPrivKey)
   186  			if strings.Contains(tt.tcName, "success") {
   187  				require.NoError(t, err)
   188  				require.NotEmpty(t, p)
   189  
   190  				sp, e := proto.Marshal(p)
   191  				require.NoError(t, e)
   192  				require.NotEmpty(t, sp)
   193  
   194  				// try PublicKeyData() with bad serialized private key
   195  				pubK, e := km.PublicKeyData([]byte("bad serialized private key"))
   196  				require.Error(t, e)
   197  				require.Empty(t, pubK)
   198  
   199  				// try PublicKeyData() with valid serialized private key
   200  				pubK, e = km.PublicKeyData(sp)
   201  				require.NoError(t, e)
   202  				require.NotEmpty(t, pubK)
   203  			}
   204  
   205  			kd, err := km.NewKeyData(sPrivKey)
   206  			if strings.Contains(tt.tcName, "success") {
   207  				require.NoError(t, err)
   208  				require.NotEmpty(t, kd)
   209  				require.Equal(t, kd.TypeUrl, bbsSignerKeyTypeURL)
   210  				require.Equal(t, kd.KeyMaterialType, tinkpb.KeyData_ASYMMETRIC_PRIVATE)
   211  				return
   212  			}
   213  
   214  			require.Errorf(t, err, tt.tcName)
   215  			require.Empty(t, p)
   216  		})
   217  	}
   218  }