github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric/bccsp/idemix/handlers/nym.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  // nymSecretKey contains the nym secret key
    15  type nymSecretKey struct {
    16  	// SKI of this key
    17  	ski []byte
    18  	// sk is the idemix reference to the nym secret
    19  	sk Big
    20  	// pk is the idemix reference to the nym public part
    21  	pk Ecp
    22  	// exportable if true, sk can be exported via the Bytes function
    23  	exportable bool
    24  }
    25  
    26  func computeSKI(serialise func() ([]byte, error)) ([]byte, error) {
    27  	raw, err := serialise()
    28  	if err != nil {
    29  		return nil, err
    30  	}
    31  
    32  	hash := newcryptosm.SHA256.New()
    33  	hash.Write(raw)
    34  	return hash.Sum(nil), nil
    35  
    36  }
    37  
    38  func NewNymSecretKey(sk Big, pk Ecp, exportable bool) (*nymSecretKey, error) {
    39  	ski, err := computeSKI(sk.Bytes)
    40  	if err != nil {
    41  		return nil, err
    42  	}
    43  
    44  	return &nymSecretKey{ski: ski, sk: sk, pk: pk, exportable: exportable}, nil
    45  }
    46  
    47  func (k *nymSecretKey) Bytes() ([]byte, error) {
    48  	if k.exportable {
    49  		return k.sk.Bytes()
    50  	}
    51  
    52  	return nil, errors.New("not supported")
    53  }
    54  
    55  func (k *nymSecretKey) SKI() []byte {
    56  	c := make([]byte, len(k.ski))
    57  	copy(c, k.ski)
    58  	return c
    59  }
    60  
    61  func (*nymSecretKey) Symmetric() bool {
    62  	return false
    63  }
    64  
    65  func (*nymSecretKey) Private() bool {
    66  	return true
    67  }
    68  
    69  func (k *nymSecretKey) PublicKey() (bccsp.Key, error) {
    70  	ski, err := computeSKI(k.pk.Bytes)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  	return &nymPublicKey{ski: ski, pk: k.pk}, nil
    75  }
    76  
    77  type nymPublicKey struct {
    78  	// SKI of this key
    79  	ski []byte
    80  	// pk is the idemix reference to the nym public part
    81  	pk Ecp
    82  }
    83  
    84  func NewNymPublicKey(pk Ecp) *nymPublicKey {
    85  	return &nymPublicKey{pk: pk}
    86  }
    87  
    88  func (k *nymPublicKey) Bytes() ([]byte, error) {
    89  	return k.pk.Bytes()
    90  }
    91  
    92  func (k *nymPublicKey) SKI() []byte {
    93  	c := make([]byte, len(k.ski))
    94  	copy(c, k.ski)
    95  	return c
    96  }
    97  
    98  func (*nymPublicKey) Symmetric() bool {
    99  	return false
   100  }
   101  
   102  func (*nymPublicKey) Private() bool {
   103  	return false
   104  }
   105  
   106  func (k *nymPublicKey) PublicKey() (bccsp.Key, error) {
   107  	return k, nil
   108  }
   109  
   110  // NymKeyDerivation derives nyms
   111  type NymKeyDerivation struct {
   112  	// Exportable is a flag to allow an issuer secret key to be marked as Exportable.
   113  	// If a secret key is marked as Exportable, its Bytes method will return the key's byte representation.
   114  	Exportable bool
   115  	// User implements the underlying cryptographic algorithms
   116  	User User
   117  }
   118  
   119  func (kd *NymKeyDerivation) KeyDeriv(k bccsp.Key, opts bccsp.KeyDerivOpts) (dk bccsp.Key, err error) {
   120  	userSecretKey, ok := k.(*userSecretKey)
   121  	if !ok {
   122  		return nil, errors.New("invalid key, expected *userSecretKey")
   123  	}
   124  	nymKeyDerivationOpts, ok := opts.(*bccsp.IdemixNymKeyDerivationOpts)
   125  	if !ok {
   126  		return nil, errors.New("invalid options, expected *IdemixNymKeyDerivationOpts")
   127  	}
   128  	if nymKeyDerivationOpts.IssuerPK == nil {
   129  		return nil, errors.New("invalid options, missing issuer public key")
   130  	}
   131  	issuerPK, ok := nymKeyDerivationOpts.IssuerPK.(*issuerPublicKey)
   132  	if !ok {
   133  		return nil, errors.New("invalid options, expected IssuerPK as *issuerPublicKey")
   134  	}
   135  
   136  	Nym, RandNym, err := kd.User.MakeNym(userSecretKey.sk, issuerPK.pk)
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	return NewNymSecretKey(RandNym, Nym, kd.Exportable)
   142  }
   143  
   144  // NymPublicKeyImporter imports nym public keys
   145  type NymPublicKeyImporter struct {
   146  	// User implements the underlying cryptographic algorithms
   147  	User User
   148  }
   149  
   150  func (i *NymPublicKeyImporter) KeyImport(raw interface{}, opts bccsp.KeyImportOpts) (k bccsp.Key, err error) {
   151  	bytes, ok := raw.([]byte)
   152  	if !ok {
   153  		return nil, errors.New("invalid raw, expected byte array")
   154  	}
   155  
   156  	if len(bytes) == 0 {
   157  		return nil, errors.New("invalid raw, it must not be nil")
   158  	}
   159  
   160  	pk, err := i.User.NewPublicNymFromBytes(bytes)
   161  	if err != nil {
   162  		return nil, err
   163  	}
   164  
   165  	return &nymPublicKey{pk: pk}, nil
   166  }