github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric/bccsp/idemix/handlers/user.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  package handlers
     7  
     8  import (
     9  	"github.com/hellobchain/newcryptosm"
    10  	"github.com/hellobchain/third_party/hyperledger/fabric/bccsp"
    11  	"github.com/pkg/errors"
    12  )
    13  
    14  // userSecretKey contains the User secret key
    15  type userSecretKey struct {
    16  	// sk is the idemix reference to the User key
    17  	sk Big
    18  	// Exportable if true, sk can be exported via the Bytes function
    19  	exportable bool
    20  }
    21  
    22  func NewUserSecretKey(sk Big, exportable bool) *userSecretKey {
    23  	return &userSecretKey{sk: sk, exportable: exportable}
    24  }
    25  
    26  func (k *userSecretKey) Bytes() ([]byte, error) {
    27  	if k.exportable {
    28  		return k.sk.Bytes()
    29  	}
    30  
    31  	return nil, errors.New("not exportable")
    32  }
    33  
    34  func (k *userSecretKey) SKI() []byte {
    35  	raw, err := k.sk.Bytes()
    36  	if err != nil {
    37  		return nil
    38  	}
    39  	hash := newcryptosm.SHA256.New()
    40  	hash.Write(raw)
    41  	return hash.Sum(nil)
    42  }
    43  
    44  func (*userSecretKey) Symmetric() bool {
    45  	return true
    46  }
    47  
    48  func (*userSecretKey) Private() bool {
    49  	return true
    50  }
    51  
    52  func (k *userSecretKey) PublicKey() (bccsp.Key, error) {
    53  	return nil, errors.New("cannot call this method on a symmetric key")
    54  }
    55  
    56  type UserKeyGen struct {
    57  	// Exportable is a flag to allow an issuer secret key to be marked as Exportable.
    58  	// If a secret key is marked as Exportable, its Bytes method will return the key's byte representation.
    59  	Exportable bool
    60  	// User implements the underlying cryptographic algorithms
    61  	User User
    62  }
    63  
    64  func (g *UserKeyGen) KeyGen(opts bccsp.KeyGenOpts) (bccsp.Key, error) {
    65  	sk, err := g.User.NewKey()
    66  	if err != nil {
    67  		return nil, err
    68  	}
    69  
    70  	return &userSecretKey{exportable: g.Exportable, sk: sk}, nil
    71  }
    72  
    73  // UserKeyImporter import user keys
    74  type UserKeyImporter struct {
    75  	// Exportable is a flag to allow a secret key to be marked as Exportable.
    76  	// If a secret key is marked as Exportable, its Bytes method will return the key's byte representation.
    77  	Exportable bool
    78  	// User implements the underlying cryptographic algorithms
    79  	User User
    80  }
    81  
    82  func (i *UserKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
    83  	der, ok := raw.([]byte)
    84  	if !ok {
    85  		return nil, errors.New("invalid raw, expected byte array")
    86  	}
    87  
    88  	if len(der) == 0 {
    89  		return nil, errors.New("invalid raw, it must not be nil")
    90  	}
    91  
    92  	sk, err := i.User.NewKeyFromBytes(raw.([]byte))
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  
    97  	return &userSecretKey{exportable: i.Exportable, sk: sk}, nil
    98  }